My Project
Loading...
Searching...
No Matches
GuideRateValue.hpp
1/*
2 Copyright (c) 2020 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
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 OPM_OUTPUT_DATA_GUIDERATEVALUE_HPP
21#define OPM_OUTPUT_DATA_GUIDERATEVALUE_HPP
22
23#include <array>
24#include <bitset>
25#include <cstddef>
26#include <stdexcept>
27#include <string>
28#include <opm/json/JsonObject.hpp>
29
30namespace Opm { namespace data {
31
33 public:
34 enum class Item : std::size_t {
35 Oil, Gas, Water, ResV,
36
37 // -- Must be last enumerator --
38 NumItems,
39 };
40
41 void clear()
42 {
43 this->mask_.reset();
44 this->value_.fill(0.0);
45 }
46
47 constexpr bool has(const Item p) const
48 {
49 const auto i = this->index(p);
50
51 return (i < Size) && this->mask_[i];
52 }
53
54 bool operator==(const GuideRateValue& vec) const
55 {
56 return (this->mask_ == vec.mask_)
57 && (this->value_ == vec.value_);
58 }
59
60 double get(const Item p) const
61 {
62 if (! this->has(p)) {
63 throw std::invalid_argument {
64 "Request for Unset Item Value for " + this->itemName(p)
65 };
66 }
67
68 return this->value_[ this->index(p) ];
69 }
70
71 GuideRateValue& set(const Item p, const double value)
72 {
73 const auto i = this->index(p);
74
75 if (i >= Size) {
76 throw std::invalid_argument {
77 "Cannot Assign Item Value for Unsupported Item '"
78 + this->itemName(p) + '\''
79 };
80 }
81
82 this->mask_.set(i);
83 this->value_[i] = value;
84
85 return *this;
86 }
87
88 GuideRateValue& operator+=(const GuideRateValue& rhs)
89 {
90 for (auto i = 0*Size; i < Size; ++i) {
91 if (rhs.mask_[i]) {
92 this->mask_.set(i);
93 this->value_[i] += rhs.value_[i];
94 }
95 }
96
97 return *this;
98 }
99
100 template <class MessageBufferType>
101 void write(MessageBufferType& buffer) const
102 {
103 auto maskrep = this->mask_.to_ullong();
104 buffer.write(maskrep);
105
106 for (const auto& x : this->value_) {
107 buffer.write(x);
108 }
109 }
110
111 template <class MessageBufferType>
112 void read(MessageBufferType& buffer)
113 {
114 this->clear();
115
116 {
117 auto mask = 0ull;
118 buffer.read(mask);
119
120 this->mask_ = std::bitset<Size>(mask);
121 }
122
123 for (auto& x : this->value_) {
124 buffer.read(x);
125 }
126 }
127
128 void init_json(Json::JsonObject& json_data) const {
129 for (const auto& item : {Item::Oil, Item::Gas, Item::Water, Item::ResV}) {
130 if (this->has(item))
131 json_data.add_item(this->itemName(item), this->get(item));
132 }
133 }
134
135 template<class Serializer>
136 void serializeOp(Serializer& serializer)
137 {
138 serializer(mask_);
139 serializer(value_);
140 }
141
142 static GuideRateValue serializationTestObject()
143 {
144 GuideRateValue val;
145 val.mask_ = std::bitset<Size>(1234);
146 val.value_ = {1,2,3,4};
147
148 return val;
149 }
150
151 private:
152 enum { Size = static_cast<std::size_t>(Item::NumItems) };
153
154 std::bitset<Size> mask_{};
155 std::array<double, Size> value_{};
156
157 constexpr std::size_t index(const Item p) const noexcept
158 {
159 return static_cast<std::size_t>(p);
160 }
161
162 std::string itemName(const Item p) const
163 {
164 switch (p) {
165 case Item::Oil: return "Oil";
166 case Item::Gas: return "Gas";
167 case Item::Water: return "Water";
168 case Item::ResV: return "ResV";
169
170 case Item::NumItems:
171 return "Out of bounds (NumItems)";
172 }
173
174 return "Unknown (" + std::to_string(this->index(p)) + ')';
175 }
176 };
177
178}} // namespace Opm::data
179
180#endif // OPM_OUTPUT_DATA_GUIDERATEVALUE_HPP
Definition JsonObject.hpp:31
Class for (de-)serializing.
Definition Serializer.hpp:84
Definition GuideRateValue.hpp:32
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30