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#include <opm/input/eclipse/Deck/value_status.hpp>
26
27#include <string>
28#include <vector>
29#include <optional>
30#include <array>
31#include <algorithm>
32#include <stdexcept>
33
34namespace Opm
35{
36namespace Fieldprops
37{
38 template<typename T>
39 static void compress(std::vector<T>& data, const std::vector<bool>& active_map) {
40 std::size_t shift = 0;
41 for (std::size_t g = 0; g < active_map.size(); g++) {
42 if (active_map[g] && shift > 0) {
43 data[g - shift] = data[g];
44 continue;
45 }
46
47 if (!active_map[g])
48 shift += 1;
49 }
50
51 data.resize(data.size() - shift);
52 }
53
54 template<typename T>
55 struct FieldData {
56 std::vector<T> data;
57 std::vector<value::status> value_status;
59 std::optional<std::vector<T>> global_data;
60 std::optional<std::vector<value::status>> global_value_status;
61 mutable bool all_set;
62
63 bool operator==(const FieldData& other) const {
64 return this->data == other.data &&
65 this->value_status == other.value_status &&
66 this->kw_info == other.kw_info &&
67 this->global_data == other.global_data &&
68 this->global_value_status == other.global_value_status;
69 }
70
71
72 FieldData() = default;
73
74 FieldData(const keywords::keyword_info<T>& info, std::size_t active_size, std::size_t global_size) :
75 data(std::vector<T>(active_size)),
76 value_status(active_size, value::status::uninitialized),
77 kw_info(info),
78 all_set(false)
79 {
80 if (global_size != 0) {
81 this->global_data = std::vector<T>(global_size);
82 this->global_value_status = std::vector<value::status>(global_size, value::status::uninitialized);
83 }
84
85 if (info.scalar_init)
86 this->default_assign( *info.scalar_init );
87 }
88
89
90 std::size_t size() const {
91 return this->data.size();
92 }
93
94 bool valid() const {
95 if (this->all_set)
96 return true;
97
98 static const std::array<value::status,2> invalid_value = {value::status::uninitialized, value::status::empty_default};
99 const auto& it = std::find_first_of(this->value_status.begin(), this->value_status.end(), invalid_value.begin(), invalid_value.end());
100 this->all_set = (it == this->value_status.end());
101
102 return this->all_set;
103 }
104
105 bool valid_default() const {
106 return std::all_of( this->value_status.begin(), this->value_status.end(), [] (const value::status& status) {return status == value::status::valid_default; });
107 }
108
109
110 void compress(const std::vector<bool>& active_map) {
111 Fieldprops::compress(this->data, active_map);
112 Fieldprops::compress(this->value_status, active_map);
113 }
114
115 void copy(const FieldData<T>& src, const std::vector<Box::cell_index>& index_list) {
116 for (const auto& ci : index_list) {
117 this->data[ci.active_index] = src.data[ci.active_index];
118 this->value_status[ci.active_index] = src.value_status[ci.active_index];
119 }
120 }
121
122 void default_assign(T value) {
123 std::fill(this->data.begin(), this->data.end(), value);
124 std::fill(this->value_status.begin(), this->value_status.end(), value::status::valid_default);
125
126 if (this->global_data) {
127 std::fill(this->global_data->begin(), this->global_data->end(), value);
128 std::fill(this->global_value_status->begin(), this->global_value_status->end(), value::status::valid_default);
129 }
130 }
131
132 void default_assign(const std::vector<T>& src) {
133 if (src.size() != this->size())
134 throw std::invalid_argument("Size mismatch got: " + std::to_string(src.size()) + " expected: " + std::to_string(this->size()));
135
136 std::copy(src.begin(), src.end(), this->data.begin());
137 std::fill(this->value_status.begin(), this->value_status.end(), value::status::valid_default);
138 }
139
140 void default_update(const std::vector<T>& src) {
141 if (src.size() != this->size())
142 throw std::invalid_argument("Size mismatch got: " + std::to_string(src.size()) + " expected: " + std::to_string(this->size()));
143
144 for (std::size_t i = 0; i < src.size(); i++) {
145 if (!value::has_value(this->value_status[i])) {
146 this->value_status[i] = value::status::valid_default;
147 this->data[i] = src[i];
148 }
149 }
150 }
151
152 void update(std::size_t index, T value, value::status status) {
153 this->data[index] = value;
154 this->value_status[index] = status;
155 }
156
157 };
158} // end namespace Fieldprops
159} // end namespace Opm
160#endif // FIELD_DATA_HPP
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Definition FieldData.hpp:55
Definition Keywords.hpp:31