My Project
Loading...
Searching...
No Matches
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 <filesystem>
27#include <fstream>
28#include <iosfwd>
29#include <fmt/format.h>
30
31#include <opm/input/eclipse/Deck/DeckKeyword.hpp>
32#include <opm/input/eclipse/Deck/FileDeck.hpp>
33
34namespace fs = std::filesystem;
35
36namespace Opm {
37class Deck;
38
39class FileDeck {
40public:
41 static const std::unordered_set<std::string> rst_keep_in_solution;
42 static const std::unordered_set<std::string> rst_keep_in_schedule;
43
44enum class OutputMode {
45 INLINE = 1,
46 SHARE = 2,
47 COPY = 3
48};
49
50
51
52struct 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 Index operator+(std::size_t shift) const;
70
71private:
72 const FileDeck * deck;
73};
74
75
76
77class Block {
78public:
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, std::size_t keyword_index) 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
88private:
89 std::string fname;
90 std::vector<DeckKeyword> keywords;
91
92friend FileDeck;
93};
94
95
96 explicit FileDeck(const Deck& deck);
97 std::optional<Index> find(const std::string& keyword, const Index& offset) const;
98 std::optional<Index> find(const std::string& keyword) const;
99 std::size_t count(const std::string& keyword) const;
100 void erase(const Index& index);
101 void erase(const Index& begin, const Index& end);
102 void insert(const Index& index, const DeckKeyword& keyword);
103
104 void dump_stdout(const std::string& output_dir, OutputMode mode) const;
105 void dump(const std::string& dir, const std::string& fname, OutputMode mode) const;
106 const DeckKeyword& operator[](const Index& index) const;
107 const Index start() const;
108 const Index stop() const;
109
110 void rst_solution(const std::string& rst_base, int report_step);
111 void insert_skiprest();
112 void skip(int report_step);
113
114private:
115 std::vector<Block> blocks;
116 std::string input_directory;
117 std::unordered_set<std::string> modified_files;
118 DeckTree deck_tree;
119
120 struct DumpContext {
121 std::unordered_map<std::string, std::ofstream> stream_map;
122 std::unordered_map<std::string, std::string> file_map;
123
124 bool has_file(const std::string& fname) const {
125 return this->file_map.count(fname) > 0;
126 }
127
128 std::optional<std::ofstream *> get_stream(const std::string& deck_name) {
129 auto name_iter = this->file_map.find(deck_name);
130 if (name_iter == this->file_map.end())
131 return {};
132
133 return &this->stream_map.at(name_iter->second);
134 }
135
136
137 std::ofstream& open_file(const std::string& deck_name, const fs::path& output_file)
138 {
139 if (this->stream_map.count(output_file.string()) == 0) {
140 this->file_map.insert(std::make_pair( deck_name, output_file.string() ));
141
142 if (!fs::is_directory(output_file.parent_path()))
143 fs::create_directories(output_file.parent_path());
144
145 std::ofstream stream{output_file};
146 if (!stream)
147 throw std::logic_error(fmt::format("Opening {} for writing failed", output_file.string()));
148 this->stream_map.insert(std::make_pair(output_file.string(), std::move(stream)));
149 }
150 return this->stream_map.at(output_file.string());
151 }
152
153 };
154
155 void dump(std::ostream& os) const;
156 void dump_shared(std::ostream& stream, const std::string& output_dir) const;
157 void dump_inline() const;
158 std::string dump_block(const Block& block, const std::string& dir, const std::optional<std::string>& fname, DumpContext& context) const;
159 void include_block(const std::string& source_file, const std::string& target_file, const std::string& dir, DumpContext& context) const;
160};
161
162}
163
164#endif
Definition DeckKeyword.hpp:36
Definition DeckOutput.hpp:29
Definition DeckTree.hpp:38
Definition Deck.hpp:49
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:30
Definition FileDeck.hpp:52