opm-common
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 <opm/input/eclipse/Deck/DeckKeyword.hpp>
24 #include <opm/input/eclipse/Deck/DeckTree.hpp>
25 
26 #include <cstddef>
27 #include <filesystem>
28 #include <fstream>
29 #include <iosfwd>
30 #include <iterator>
31 #include <optional>
32 #include <string>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <vector>
36 
37 namespace Opm {
38  class Deck;
39 } // namespace Opm
40 
41 namespace Opm {
42 
43 class FileDeck
44 {
45 public:
46  enum class OutputMode
47  {
48  INLINE = 1,
49  SHARE = 2,
50  COPY = 3,
51  };
52 
53  struct Index
54  {
55  std::size_t file_index{};
56  std::size_t keyword_index{};
57 
58  Index(const std::size_t file_index_arg,
59  const std::size_t keyword_index_arg,
60  const FileDeck* deck_arg)
61  : file_index { file_index_arg }
62  , keyword_index { keyword_index_arg }
63  , deck { deck_arg }
64  {}
65 
66  Index& operator--();
67  Index operator--(int);
68  Index& operator++();
69  Index operator++(int);
70  bool operator==(const Index& other) const;
71  bool operator!=(const Index& other) const;
72  bool operator<(const Index& other) const;
73  Index operator+(std::size_t shift) const;
74 
75  private:
76  const FileDeck* deck { nullptr };
77  };
78 
79  static const std::unordered_set<std::string>& rst_keep_in_solution();
80 
81  explicit FileDeck(const Deck& deck);
82 
83  std::optional<Index> find(const std::string& keyword, const Index& offset) const;
84  std::optional<Index> find(const std::string& keyword) const;
85  std::size_t count(const std::string& keyword) const;
86  void erase(const Index& index);
87  void erase(const Index& begin, const Index& end);
88  void insert(const Index& index, const DeckKeyword& keyword);
89 
90  void dump_stdout(const std::string& output_dir, OutputMode mode) const;
91  void dump(const std::string& dir, const std::string& fname, OutputMode mode) const;
92  const DeckKeyword& operator[](const Index& index) const;
93  Index start() const;
94  Index stop() const;
95 
96  void rst_solution(const std::string& rst_base, int report_step);
97  void insert_skiprest();
98  void skip(int report_step);
99 
100 private:
101  class Block
102  {
103  public:
104  explicit Block(const std::string& filename);
105 
106  std::size_t size() const;
107  void load(const Deck& deck, std::size_t deck_index);
108 
109  std::optional<std::size_t>
110  find(const std::string& keyword, std::size_t keyword_index) const;
111 
112  bool empty() const;
113  void erase(const FileDeck::Index& index);
114  void insert(std::size_t keyword_index, const DeckKeyword& keyword);
115  void dump(DeckOutput& out) const;
116 
117  private:
118  std::string fname;
119  std::vector<DeckKeyword> keywords;
120  bool is_binary {false};
121  friend FileDeck;
122  };
123 
124  class DumpContext
125  {
126  public:
127  bool has_file(const std::string& fname) const;
128 
129  std::ofstream* get_stream(const std::string& deck_name);
130  std::ofstream& open_file(const std::string& deck_name,
131  const std::filesystem::path& output_file);
132 
133  private:
134  std::unordered_map<std::string, std::ofstream> stream_map_{};
135  std::unordered_map<std::string, std::string> file_map_{};
136  };
137 
138  std::vector<Block> blocks;
139  std::string input_directory;
140  std::unordered_set<std::string> modified_files;
141 
142  DeckTree deck_tree;
143 
144  void dump(std::ostream& os) const;
145  void dump_shared(std::ostream& stream, const std::string& output_dir) const;
146  void dump_inline() const;
147 
148  std::string dump_block(const Block& block,
149  const std::string& dir,
150  const std::optional<std::string>& fname,
151  DumpContext& context) const;
152 
153  void include_block(const std::string& source_file,
154  const std::string& target_file,
155  const std::string& dir,
156  DumpContext& context) const;
157 };
158 
159 } // namespace Opm
160 
161 #endif // FILE_DECK_HPP
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: FileDeck.hpp:43
Definition: DeckOutput.hpp:29
Definition: FileDeck.hpp:53
Definition: Deck.hpp:46
Definition: DeckKeyword.hpp:36