FieldProps.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019 Equinor 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 it under the terms
7 of the GNU General Public License as published by the Free Software
8 Foundation, either version 3 of the License, or (at your option) any later
9 version.
10
11 OPM is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 OPM. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef FIELDPROPS_HPP
20#define FIELDPROPS_HPP
21
22#include <string>
23#include <unordered_set>
24#include <vector>
25
31
32namespace Opm {
33
34class Deck;
35class EclipseGrid;
36class TableManager;
37
39public:
40
43 double multiplier;
45
46
47 MultregpRecord(int rv, double m, const std::string& rn) :
48 region_value(rv),
49 multiplier(m),
50 region_name(rn)
51 {}
52
53 };
54
55 enum class ScalarOperation {
56 ADD = 1,
57 EQUAL = 2,
58 MUL = 3,
59 MIN = 4,
60 MAX = 5
61 };
62
63 template<typename T>
64 static void compress(std::vector<T>& data, const std::vector<bool>& active_map) {
65 std::size_t shift = 0;
66 for (std::size_t g = 0; g < active_map.size(); g++) {
67 if (active_map[g] && shift > 0) {
68 data[g - shift] = data[g];
69 continue;
70 }
71
72 if (!active_map[g])
73 shift += 1;
74 }
75
76 data.resize(data.size() - shift);
77 }
78
79 enum class GetStatus {
80 OK = 1,
81 INVALID_DATA = 2, // std::runtime_error
82 MISSING_KEYWORD = 3, // std::out_of_range
83 NOT_SUPPPORTED_KEYWORD = 4 // std::logic_error
84 };
85
86
87
88
89 template<typename T>
90 struct FieldData {
91 std::vector<T> data;
92 std::vector<value::status> value_status;
93 mutable bool all_set;
94
95 FieldData() = default;
96
97 FieldData(std::size_t active_size) :
98 data(std::vector<T>(active_size)),
99 value_status(active_size, value::status::uninitialized),
101 {
102 }
103
104
105 std::size_t size() const {
106 return this->data.size();
107 }
108
109 bool valid() const {
110 if (this->all_set)
111 return true;
112
113 static const std::array<value::status,2> invalid_value = {value::status::uninitialized, value::status::empty_default};
114 const auto& it = std::find_first_of(this->value_status.begin(), this->value_status.end(), invalid_value.begin(), invalid_value.end());
115 this->all_set = (it == this->value_status.end());
116
117 return this->all_set;
118 }
119
120 void compress(const std::vector<bool>& active_map) {
121 FieldProps::compress(this->data, active_map);
122 FieldProps::compress(this->value_status, active_map);
123 }
124
125 void copy(const FieldData<T>& src, const std::vector<Box::cell_index>& index_list) {
126 for (const auto& ci : index_list) {
127 this->data[ci.active_index] = src.data[ci.active_index];
128 this->value_status[ci.active_index] = src.value_status[ci.active_index];
129 }
130 }
131
133 std::fill(this->data.begin(), this->data.end(), value);
134 std::fill(this->value_status.begin(), this->value_status.end(), value::status::valid_default);
135 }
136
137 void default_assign(const std::vector<T>& src) {
138 if (src.size() != this->size())
139 throw std::invalid_argument("Size mismatch got: " + std::to_string(src.size()) + " expected: " + std::to_string(this->size()));
140
141 std::copy(src.begin(), src.end(), this->data.begin());
142 std::fill(this->value_status.begin(), this->value_status.end(), value::status::valid_default);
143 }
144
145 void default_update(const std::vector<T>& src) {
146 if (src.size() != this->size())
147 throw std::invalid_argument("Size mismatch got: " + std::to_string(src.size()) + " expected: " + std::to_string(this->size()));
148
149 for (std::size_t i = 0; i < src.size(); i++) {
150 if (!value::has_value(this->value_status[i])) {
151 this->value_status[i] = value::status::valid_default;
152 this->data[i] = src[i];
153 }
154 }
155 }
156
157 void update(std::size_t index, T value, value::status status) {
158 this->data[index] = value;
159 this->value_status[index] = status;
160 }
161
162 };
163
164
165 template<typename T>
170
172 keyword(k),
173 status(s),
174 data_ptr(d)
175 { }
176
177
178 void verify_status() const {
179 switch (status) {
181 return;
183 throw std::runtime_error("The keyword: " + keyword + " has not been fully initialized");
185 throw std::out_of_range("No such keyword in deck: " + keyword);
187 throw std::logic_error("The kewyord " + keyword + " is not supported");
188 }
189 }
190
191 const std::vector<T>* ptr() const {
192 if (this->data_ptr)
193 return std::addressof(this->data_ptr->data);
194 else
195 return nullptr;
196 }
197
198 const std::vector<T>& data() const {
199 this->verify_status();
200 return this->data_ptr->data;
201 }
202
203 const FieldData<T>& field_data() const {
204 this->verify_status();
205 return *this->data_ptr;
206 }
207
208 bool valid() const {
209 return (this->status == GetStatus::OK);
210 }
211
212 };
213
214
215
216
217 FieldProps(const Deck& deck, const Phases& phases, const EclipseGrid& grid, const TableManager& table_arg);
218 void reset_actnum(const std::vector<int>& actnum);
219
221
222 std::vector<int> actnum();
223
224 template <typename T>
225 static bool supported(const std::string& keyword);
226
227 template <typename T>
228 bool has(const std::string& keyword) const;
229
230 template <typename T>
231 std::vector<std::string> keys() const;
232
233
234 template <typename T>
236 if (!FieldProps::supported<T>(keyword))
238
239 const FieldData<T> * field_data;
240 bool has0 = this->has<T>(keyword);
241
242 field_data = std::addressof(this->init_get<T>(keyword));
243 if (field_data->valid())
244 return FieldDataManager<T>(keyword, GetStatus::OK, field_data);
245
246 if (!has0) {
247 this->erase<T>(keyword);
249 }
250
252 }
253
254
255 template <typename T>
256 const std::vector<T>& get(const std::string& keyword) {
257 const auto& data = this->try_get<T>(keyword);
258 return data.data();
259 }
260
261
262
263 template <typename T>
264 std::vector<T> get_copy(const std::string& keyword, bool global) {
265 bool has0 = this->has<T>(keyword);
266 const auto& data = this->get<T>(keyword);
267
268 if (has0) {
269 if (global)
270 return this->global_copy(data);
271 else
272 return data;
273 } else {
274 if (global)
275 return this->global_copy(this->extract<T>(keyword));
276 else
277 return this->extract<T>(keyword);
278 }
279 }
280
281
282 template <typename T>
283 std::vector<bool> defaulted(const std::string& keyword) {
284 const auto& field = this->init_get<T>(keyword);
285 std::vector<bool> def(field.size());
286
287 for (std::size_t i=0; i < def.size(); i++)
288 def[i] = value::defaulted( field.value_status[i]);
289
290 return def;
291 }
292
293
294 template <typename T>
295 std::vector<T> global_copy(const std::vector<T>& data) const {
296 std::vector<T> global_data(this->global_size);
297 std::size_t i = 0;
298 for (std::size_t g = 0; g < this->global_size; g++) {
299 if (this->m_actnum[g]) {
300 global_data[g] = data[i];
301 i++;
302 }
303 }
304 return global_data;
305 }
306
307 std::size_t active_size;
308 std::size_t global_size;
309
310 std::size_t num_int() const {
311 return this->int_data.size();
312 }
313
314 std::size_t num_double() const {
315 return this->double_data.size();
316 }
317
318private:
319 void scanGRIDSection(const GRIDSection& grid_section);
320 void scanEDITSection(const EDITSection& edit_section);
321 void scanPROPSSection(const PROPSSection& props_section);
322 void scanREGIONSSection(const REGIONSSection& regions_section);
323 void scanSOLUTIONSection(const SOLUTIONSection& solution_section);
324 void scanSCHEDULESection(const SCHEDULESection& schedule_section);
325 double getSIValue(const std::string& keyword, double raw_value) const;
326 template <typename T>
327 void erase(const std::string& keyword);
328
329 template <typename T>
330 std::vector<T> extract(const std::string& keyword);
331
332 template <typename T>
333 void apply(const DeckRecord& record, FieldData<T>& target_data, const FieldData<T>& src_data, const std::vector<Box::cell_index>& index_list);
334
335 template <typename T>
336 static void apply(ScalarOperation op, FieldData<T>& data, T scalar_value, const std::vector<Box::cell_index>& index_list);
337
338 template <typename T>
339 FieldData<T>& init_get(const std::string& keyword);
340
341 std::vector<Box::cell_index> region_index( const DeckItem& regionItem, int region_value );
342 std::vector<Box::cell_index> region_index( const std::string& region_name, int region_value );
343 void handle_operation(const DeckKeyword& keyword, Box box);
344 void handle_region_operation(const DeckKeyword& keyword);
345 void handle_COPY(const DeckKeyword& keyword, Box box, bool region);
346 void distribute_toplayer(FieldProps::FieldData<double>& field_data, const std::vector<double>& deck_data, const Box& box);
347
348 void handle_keyword(const DeckKeyword& keyword, Box& box);
349 void handle_double_keyword(Section section, const DeckKeyword& keyword, const Box& box);
350 void handle_int_keyword(const DeckKeyword& keyword, const Box& box);
351 void init_satfunc(const std::string& keyword, FieldData<double>& satfunc);
352 void init_porv(FieldData<double>& porv);
353 void init_tempi(FieldData<double>& tempi);
354 void subtract_swl(FieldProps::FieldData<double>& sogcr, const std::string& swl_kw);
355
356 const UnitSystem unit_system;
357 std::size_t nx,ny,nz;
358 Phases m_phases;
359 std::vector<int> m_actnum;
360 std::vector<double> cell_volume;
361 std::vector<double> cell_depth;
362 const std::string m_default_region;
363 const EclipseGrid * grid_ptr; // A bit undecided whether to properly use the grid or not ...
364 const TableManager& tables;
365 std::vector<MultregpRecord> multregp;
366 std::unordered_map<std::string, FieldData<int>> int_data;
367 std::unordered_map<std::string, FieldData<double>> double_data;
368};
369
370}
371#endif
int index
Definition: cJSON.h:168
const char *const string
Definition: cJSON.h:170
Definition: Box.hpp:31
Definition: DeckItem.hpp:37
Definition: DeckKeyword.hpp:38
Definition: Deck.hpp:115
Definition: DeckRecord.hpp:32
Definition: DeckSection.hpp:85
Definition: EclipseGrid.hpp:54
Definition: FieldProps.hpp:38
void reset_actnum(const std::vector< int > &actnum)
std::vector< std::string > keys() const
std::vector< bool > defaulted(const std::string &keyword)
Definition: FieldProps.hpp:283
std::vector< T > global_copy(const std::vector< T > &data) const
Definition: FieldProps.hpp:295
bool has(const std::string &keyword) const
const std::string & default_region() const
static bool supported(const std::string &keyword)
std::size_t global_size
Definition: FieldProps.hpp:308
const std::vector< T > & get(const std::string &keyword)
Definition: FieldProps.hpp:256
FieldProps(const Deck &deck, const Phases &phases, const EclipseGrid &grid, const TableManager &table_arg)
GetStatus
Definition: FieldProps.hpp:79
std::size_t active_size
Definition: FieldProps.hpp:307
static void compress(std::vector< T > &data, const std::vector< bool > &active_map)
Definition: FieldProps.hpp:64
ScalarOperation
Definition: FieldProps.hpp:55
std::size_t num_double() const
Definition: FieldProps.hpp:314
FieldDataManager< T > try_get(const std::string &keyword)
Definition: FieldProps.hpp:235
std::size_t num_int() const
Definition: FieldProps.hpp:310
std::vector< int > actnum()
std::vector< T > get_copy(const std::string &keyword, bool global)
Definition: FieldProps.hpp:264
Definition: DeckSection.hpp:79
Definition: DeckSection.hpp:91
Definition: Runspec.hpp:52
Definition: DeckSection.hpp:97
Definition: DeckSection.hpp:115
Definition: DeckSection.hpp:103
Definition: TableManager.hpp:63
Definition: UnitSystem.hpp:32
#define false
Definition: msvc_stdbool.h:19
UDAKeyword keyword(UDAControl control)
status
Definition: value_status.hpp:27
bool defaulted(status st)
Definition: value_status.hpp:33
bool has_value(status st)
Definition: value_status.hpp:44
Definition: A.hpp:4
Section
Definition: DeckSection.hpp:29
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
static std::string data()
Definition: exprtk.hpp:40022
Definition: FieldProps.hpp:166
GetStatus status
Definition: FieldProps.hpp:168
FieldDataManager(const std::string &k, GetStatus s, const FieldData< T > *d)
Definition: FieldProps.hpp:171
const std::vector< T > * ptr() const
Definition: FieldProps.hpp:191
void verify_status() const
Definition: FieldProps.hpp:178
bool valid() const
Definition: FieldProps.hpp:208
const std::vector< T > & data() const
Definition: FieldProps.hpp:198
const FieldData< T > & field_data() const
Definition: FieldProps.hpp:203
const std::string & keyword
Definition: FieldProps.hpp:167
const FieldData< T > * data_ptr
Definition: FieldProps.hpp:169
Definition: FieldProps.hpp:90
std::size_t size() const
Definition: FieldProps.hpp:105
std::vector< T > data
Definition: FieldProps.hpp:91
void update(std::size_t index, T value, value::status status)
Definition: FieldProps.hpp:157
void default_assign(T value)
Definition: FieldProps.hpp:132
bool all_set
Definition: FieldProps.hpp:93
bool valid() const
Definition: FieldProps.hpp:109
void compress(const std::vector< bool > &active_map)
Definition: FieldProps.hpp:120
std::vector< value::status > value_status
Definition: FieldProps.hpp:92
void copy(const FieldData< T > &src, const std::vector< Box::cell_index > &index_list)
Definition: FieldProps.hpp:125
void default_assign(const std::vector< T > &src)
Definition: FieldProps.hpp:137
FieldData(std::size_t active_size)
Definition: FieldProps.hpp:97
void default_update(const std::vector< T > &src)
Definition: FieldProps.hpp:145
Definition: FieldProps.hpp:41
double multiplier
Definition: FieldProps.hpp:43
int region_value
Definition: FieldProps.hpp:42
MultregpRecord(int rv, double m, const std::string &rn)
Definition: FieldProps.hpp:47
std::string region_name
Definition: FieldProps.hpp:44