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