My Project
FileDeck.hpp
1 /*
2  Copyright 2021 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef FILE_DECK_HPP
21 #define FILE_DECK_HPP
22 
23 #include <optional>
24 #include <string>
25 #include <unordered_set>
26 #include <fstream>
27 #include <iostream>
28 #include <fmt/format.h>
29 
30 #include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
31 #include <opm/parser/eclipse/Deck/FileDeck.hpp>
32 #include <opm/common/utility/FileSystem.hpp>
33 
34 namespace fs = Opm::filesystem;
35 
36 namespace Opm {
37 class Deck;
38 
39 class FileDeck {
40 public:
41  static const std::unordered_set<std::string> rst_keep_in_solution;
42 
43 
44 enum class OutputMode {
45  INLINE = 1,
46  SHARE = 2,
47  COPY = 3
48 };
49 
50 
51 
52 struct Index {
53  std::size_t file_index;
54  std::size_t keyword_index;
55 
56  Index(std::size_t file_index_arg, std::size_t keyword_index_arg, const FileDeck* deck_arg)
57  : file_index(file_index_arg)
58  , keyword_index(keyword_index_arg)
59  , deck(deck_arg)
60  {}
61 
62  Index& operator--();
63  Index operator--(int);
64  Index& operator++();
65  Index operator++(int);
66  bool operator==(const Index& other) const;
67  bool operator!=(const Index& other) const;
68  bool operator<(const Index& other) const;
69 
70 
71 private:
72  const FileDeck * deck;
73 };
74 
75 
76 
77 class Block {
78 public:
79  explicit Block(const std::string& filename);
80  std::size_t size() const;
81  void load(const Deck& deck, std::size_t deck_index);
82  std::optional<std::size_t> find(const std::string& keyword) const;
83  bool empty() const;
84  void erase(const FileDeck::Index& index);
85  void insert(std::size_t keyword_index, const DeckKeyword& keyword);
86  void dump(DeckOutput& out) const;
87 
88 private:
89  std::string fname;
90  std::vector<DeckKeyword> keywords;
91 
92 friend FileDeck;
93 };
94 
95 
96  explicit FileDeck(const Deck& deck);
97  std::optional<Index> find(const std::string& keyword) const;
98  void erase(const Index& index);
99  void erase(const Index& begin, const Index& end);
100  void insert(const Index& index, const DeckKeyword& keyword);
101 
102  void dump_stdout(const std::string& output_dir, OutputMode mode) const;
103  void dump(const std::string& dir, const std::string& fname, OutputMode mode) const;
104  const DeckKeyword& operator[](const Index& index) const;
105  const Index start() const;
106  const Index stop() const;
107 
108  void rst_solution(const std::string& rst_base, int report_step);
109  void insert_skiprest();
110 
111 private:
112  std::vector<Block> blocks;
113  std::string input_directory;
114  std::unordered_set<std::string> modified_files;
115  DeckTree deck_tree;
116 
117  struct DumpContext {
118  std::unordered_map<std::string, std::ofstream> stream_map;
119  std::unordered_map<std::string, std::string> file_map;
120 
121  bool has_file(const std::string& fname) const {
122  return this->file_map.count(fname) > 0;
123  }
124 
125  std::optional<std::ofstream *> get_stream(const std::string& deck_name) {
126  auto name_iter = this->file_map.find(deck_name);
127  if (name_iter == this->file_map.end())
128  return {};
129 
130  return &this->stream_map.at(name_iter->second);
131  }
132 
133 
134  std::ofstream& open_file(const std::string& deck_name, const fs::path& output_file)
135  {
136  if (this->stream_map.count(output_file.string()) == 0) {
137  this->file_map.insert(std::make_pair( deck_name, output_file.string() ));
138 
139  if (!fs::is_directory(output_file.parent_path()))
140  fs::create_directories(output_file.parent_path());
141 
142  std::ofstream stream{output_file};
143  if (!stream)
144  throw std::logic_error(fmt::format("Opening {} for writing failed", output_file.string()));
145  this->stream_map.insert(std::make_pair(output_file.string(), std::move(stream)));
146  }
147  return this->stream_map.at(output_file.string());
148  }
149 
150  };
151 
152  void dump(std::ostream& os) const;
153  void dump_shared(std::ostream& stream, const std::string& output_dir) const;
154  void dump_inline() const;
155  std::string dump_block(const Block& block, const std::string& dir, const std::optional<std::string>& fname, DumpContext& context) const;
156  void include_block(const std::string& source_file, const std::string& target_file, const std::string& dir, DumpContext& context) const;
157 };
158 
159 }
160 
161 #endif
Definition: DeckKeyword.hpp:36
Definition: DeckOutput.hpp:29
Definition: DeckTree.hpp:38
Definition: Deck.hpp:119
Definition: FileDeck.hpp:77
Definition: FileDeck.hpp:39
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Definition: FileDeck.hpp:52