My Project
Loading...
Searching...
No Matches
FieldData.hpp
1/*
2 Copyright 2020 Equinor AS.
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 FIELD_DATA_HPP
21#define FIELD_DATA_HPP
22
23#include <opm/input/eclipse/EclipseState/Grid/Box.hpp>
24#include <opm/input/eclipse/EclipseState/Grid/Keywords.hpp>
25
26#include <opm/input/eclipse/Deck/value_status.hpp>
27
28#include <algorithm>
29#include <array>
30#include <cstddef>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <vector>
35
36namespace Opm {
37 class KeywordLocation;
38} // namespace Opm
39
40namespace Opm::Fieldprops {
41
42 template <typename T>
43 static void compress(std::vector<T>& data,
44 const std::vector<bool>& active_map,
45 const std::size_t values_per_cell = 1)
46 {
47 const std::size_t num_cells = active_map.size();
48 if (data.size() != num_cells * values_per_cell) {
49 throw std::invalid_argument("Data size does not match the size of active_map times values_per_cell.");
50 }
51
52 std::size_t shift = 0;
53 for (std::size_t value_index = 0; value_index < values_per_cell; ++value_index) {
54 for (std::size_t g = 0; g < active_map.size(); ++g) {
55 if (active_map[g] && shift > 0) {
56 const std::size_t orig_index = value_index * num_cells + g;
57 data[orig_index - shift] = data[orig_index];
58 continue;
59 }
60 if (!active_map[g]) {
61 shift += 1;
62 }
63 }
64 }
65
66 data.resize(data.size() - shift);
67 }
68
69 template <typename T>
70 struct FieldData
71 {
72 std::vector<T> data{};
73 std::vector<value::status> value_status{};
75 std::optional<std::vector<T>> global_data{};
76 std::optional<std::vector<value::status>> global_value_status{std::nullopt};
77 mutable bool all_set{false};
78
79 bool operator==(const FieldData& other) const
80 {
81 return this->data == other.data &&
82 this->value_status == other.value_status &&
83 this->kw_info == other.kw_info &&
84 this->global_data == other.global_data &&
85 this->global_value_status == other.global_value_status;
86 }
87
88 FieldData() = default;
89
91 const std::size_t active_size,
92 const std::size_t global_size)
93 : data (active_size * info.num_value)
94 , value_status(active_size * info.num_value, value::status::uninitialized)
95 , kw_info (info)
96 , all_set (false)
97 {
98 if (global_size != 0) {
99 this->global_data.emplace(global_size * this->numValuePerCell());
100 this->global_value_status.emplace(global_size * this->numValuePerCell(), value::status::uninitialized);
101 }
102
103 if (info.scalar_init) {
104 this->default_assign(*info.scalar_init);
105 }
106 }
107
108 std::size_t numCells() const
109 {
110 return this->data.size() / this->numValuePerCell();
111 }
112
113 std::size_t dataSize() const
114 {
115 return this->data.size();
116 }
117
118 std::size_t numValuePerCell() const
119 {
120 return this->kw_info.num_value;
121 }
122
123 bool valid() const
124 {
125 if (this->all_set) {
126 return true;
127 }
128
129 // Object is "valid" if the 'value_status' of every element is
130 // neither uninitialised nor empty.
131 return this->all_set =
132 std::none_of(this->value_status.begin(), this->value_status.end(),
133 [](const value::status& status)
134 {
135 return (status == value::status::uninitialized)
136 || (status == value::status::empty_default);
137 });
138 }
139
140 bool valid_default() const
141 {
142 return std::all_of(this->value_status.begin(), this->value_status.end(),
143 [](const value::status& status)
144 {
145 return status == value::status::valid_default;
146 });
147 }
148
149 void compress(const std::vector<bool>& active_map)
150 {
151 Fieldprops::compress(this->data, active_map, this->numValuePerCell());
152 Fieldprops::compress(this->value_status, active_map, this->numValuePerCell());
153 }
154
155 void checkInitialisedCopy(const FieldData& src,
156 const std::vector<Box::cell_index>& index_list,
157 const std::string& from,
158 const std::string& to,
159 const KeywordLocation& loc,
160 const bool global = false);
161
162 void default_assign(T value)
163 {
164 std::fill(this->data.begin(), this->data.end(), value);
165 std::fill(this->value_status.begin(),
166 this->value_status.end(),
167 value::status::valid_default);
168
169 if (this->global_data) {
170 std::fill(this->global_data->begin(),
171 this->global_data->end(), value);
172
173 std::fill(this->global_value_status->begin(),
174 this->global_value_status->end(),
175 value::status::valid_default);
176 }
177 }
178
179 void default_assign(const std::vector<T>& src)
180 {
181 if (src.size() != this->dataSize()) {
182 throw std::invalid_argument {
183 "Size mismatch got: " + std::to_string(src.size()) +
184 ", expected: " + std::to_string(this->dataSize())
185 };
186 }
187
188 std::copy(src.begin(), src.end(), this->data.begin());
189 std::fill(this->value_status.begin(), this->value_status.end(),
190 value::status::valid_default);
191 }
192
193 void default_update(const std::vector<T>& src)
194 {
195 if (src.size() != this->dataSize()) {
196 throw std::invalid_argument {
197 "Size mismatch got: " + std::to_string(src.size()) +
198 ", expected: " + std::to_string(this->dataSize())
199 };
200 }
201
202 for (std::size_t i = 0; i < src.size(); ++i) {
203 if (!value::has_value(this->value_status[i])) {
204 this->value_status[i] = value::status::valid_default;
205 this->data[i] = src[i];
206 }
207 }
208 }
209
210 void update(const std::size_t index,
211 T value,
212 const value::status status)
213 {
214 this->data[index] = value;
215 this->value_status[index] = status;
216 }
217 };
218
219} // namespace Opm::Fieldprops
220
221#endif // FIELD_DATA_HPP
Definition KeywordLocation.hpp:27
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Definition FieldData.hpp:71
Definition Keywords.hpp:31