My Project
Groups.hpp
1 /*
2  Copyright 2016 Statoil 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_GROUPS_HPP
21 #define OPM_OUTPUT_GROUPS_HPP
22 
23 #include <cstddef>
24 #include <map>
25 #include <stdexcept>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <opm/output/data/GuideRateValue.hpp>
31 #include <opm/json/JsonObject.hpp>
32 #include <opm/input/eclipse/Schedule/Group/Group.hpp>
33 
34 namespace Opm { namespace data {
35 
37  Opm::Group::ProductionCMode currentProdConstraint;
38  Opm::Group::InjectionCMode currentGasInjectionConstraint;
39  Opm::Group::InjectionCMode currentWaterInjectionConstraint;
40 
41  template <class MessageBufferType>
42  void write(MessageBufferType& buffer) const;
43 
44  template <class MessageBufferType>
45  void read(MessageBufferType& buffer);
46 
47  bool operator==(const GroupConstraints& other) const
48  {
49  return this->currentProdConstraint == other.currentProdConstraint &&
50  this->currentGasInjectionConstraint == other.currentGasInjectionConstraint &&
51  this->currentWaterInjectionConstraint == other.currentWaterInjectionConstraint;
52  }
53 
54  inline GroupConstraints& set(Opm::Group::ProductionCMode cpc,
55  Opm::Group::InjectionCMode cgic,
56  Opm::Group::InjectionCMode cwic);
57 
58  void init_json(Json::JsonObject& json_data) const {
59  json_data.add_item("prod", Opm::Group::ProductionCMode2String(this->currentProdConstraint));
60  json_data.add_item("water_inj", Opm::Group::InjectionCMode2String(this->currentGasInjectionConstraint));
61  json_data.add_item("gas_inj", Opm::Group::InjectionCMode2String(this->currentWaterInjectionConstraint));
62  }
63 
64  template<class Serializer>
65  void serializeOp(Serializer& serializer)
66  {
67  serializer(currentProdConstraint);
68  serializer(currentGasInjectionConstraint);
69  serializer(currentWaterInjectionConstraint);
70  }
71 
72  static GroupConstraints serializationTestObject()
73  {
74  return GroupConstraints{Group::ProductionCMode::GRAT,
75  Group::InjectionCMode::RATE,
76  Group::InjectionCMode::RESV};
77  }
78  };
79 
80  struct GroupGuideRates {
81  GuideRateValue production{};
82  GuideRateValue injection{};
83 
84  template <class MessageBufferType>
85  void write(MessageBufferType& buffer) const
86  {
87  this->production.write(buffer);
88  this->injection .write(buffer);
89  }
90 
91  template <class MessageBufferType>
92  void read(MessageBufferType& buffer)
93  {
94  this->production.read(buffer);
95  this->injection .read(buffer);
96  }
97 
98  bool operator==(const GroupGuideRates& other) const
99  {
100  return this->production == other.production
101  && this->injection == other.injection;
102  }
103 
104  void init_json(Json::JsonObject& json_data) const {
105  auto json_prod = json_data.add_object("production");
106  this->production.init_json(json_prod);
107 
108  auto json_inj = json_data.add_object("injection");
109  this->injection.init_json(json_inj);
110  }
111 
112  template<class Serializer>
113  void serializeOp(Serializer& serializer)
114  {
115  serializer(production);
116  serializer(injection);
117  }
118 
119  static GroupGuideRates serializationTestObject()
120  {
121  return GroupGuideRates{GuideRateValue::serializationTestObject(),
122  GuideRateValue::serializationTestObject()};
123  }
124  };
125 
126  struct GroupData {
127  GroupConstraints currentControl;
128  GroupGuideRates guideRates{};
129 
130  template <class MessageBufferType>
131  void write(MessageBufferType& buffer) const
132  {
133  this->currentControl.write(buffer);
134  this->guideRates .write(buffer);
135  }
136 
137  template <class MessageBufferType>
138  void read(MessageBufferType& buffer)
139  {
140  this->currentControl.read(buffer);
141  this->guideRates .read(buffer);
142  }
143 
144  bool operator==(const GroupData& other) const
145  {
146  return this->currentControl == other.currentControl
147  && this->guideRates == other.guideRates;
148  }
149 
150 
151  void init_json(Json::JsonObject& json_data) const {
152  auto json_constraints = json_data.add_object("constraints");
153  this->currentControl.init_json(json_constraints);
154 
155  auto json_gr = json_data.add_object("guide_rate");
156  this->guideRates.init_json(json_gr);
157  }
158 
159  template<class Serializer>
160  void serializeOp(Serializer& serializer)
161  {
162  serializer(currentControl);
163  serializer(guideRates);
164  }
165 
166  static GroupData serializationTestObject()
167  {
168  return GroupData{GroupConstraints::serializationTestObject(),
169  GroupGuideRates::serializationTestObject()};
170  }
171  };
172 
173  struct NodeData {
174  double pressure { 0.0 };
175 
176  template <class MessageBufferType>
177  void write(MessageBufferType& buffer) const
178  {
179  buffer.write(this->pressure);
180  }
181 
182  template <class MessageBufferType>
183  void read(MessageBufferType& buffer)
184  {
185  buffer.read(this->pressure);
186  }
187 
188  bool operator==(const NodeData& other) const
189  {
190  return this->pressure == other.pressure;
191  }
192 
193  void init_json(Json::JsonObject& json_data) const {
194  json_data.add_item("pressure", this->pressure);
195  }
196 
197  template<class Serializer>
198  void serializeOp(Serializer& serializer)
199  {
200  serializer(pressure);
201  }
202 
203  static NodeData serializationTestObject()
204  {
205  return NodeData{10.0};
206  }
207  };
208 
210  public:
211  std::map<std::string, GroupData> groupData {};
212  std::map<std::string, NodeData> nodeData {};
213 
214  template <class MessageBufferType>
215  void write(MessageBufferType& buffer) const
216  {
217  this->writeMap(this->groupData, buffer);
218  this->writeMap(this->nodeData, buffer);
219  }
220 
221  template <class MessageBufferType>
222  void read(MessageBufferType& buffer)
223  {
224  this->readMap(buffer, this->groupData);
225  this->readMap(buffer, this->nodeData);
226  }
227 
228  bool operator==(const GroupAndNetworkValues& other) const
229  {
230  return (this->groupData == other.groupData)
231  && (this->nodeData == other.nodeData);
232  }
233 
234  void clear()
235  {
236  this->groupData.clear();
237  this->nodeData.clear();
238  }
239 
240  void init_json(Json::JsonObject& json_data) const {
241  auto group_data = json_data.add_object("group_data");
242  for (const auto& [gname, gdata] : this->groupData) {
243  auto group_json_data = group_data.add_object(gname);
244  gdata.init_json( group_json_data );
245  }
246 
247  auto node_data = json_data.add_object("node_data");
248  for (const auto& [gname, ndata] : this->nodeData) {
249  auto node_json_data = node_data.add_object(gname);
250  ndata.init_json( node_json_data );
251  }
252  }
253 
254  Json::JsonObject json() const {
255  Json::JsonObject json_data;
256  this->init_json(json_data);
257  return json_data;
258  }
259 
260  template<class Serializer>
261  void serializeOp(Serializer& serializer)
262  {
263  serializer(groupData);
264  serializer(nodeData);
265  }
266 
267  static GroupAndNetworkValues serializationTestObject()
268  {
269  return GroupAndNetworkValues{
270  {{"test_data", GroupData::serializationTestObject()}},
271  {{"test_node", NodeData::serializationTestObject()}}
272  };
273  }
274 
275  private:
276  template <class MessageBufferType, class ValueType>
277  void writeMap(const std::map<std::string, ValueType>& map,
278  MessageBufferType& buffer) const
279  {
280  const unsigned int size = map.size();
281  buffer.write(size);
282 
283  for (const auto& [name, elm] : map) {
284  buffer.write(name);
285  elm .write(buffer);
286  }
287  }
288 
289  template <class MessageBufferType, class ValueType>
290  void readMap(MessageBufferType& buffer,
291  std::map<std::string, ValueType>& map)
292  {
293  unsigned int size;
294  buffer.read(size);
295 
296  for (std::size_t i = 0; i < size; ++i) {
297  std::string name;
298  buffer.read(name);
299 
300  auto elm = ValueType{};
301  elm.read(buffer);
302 
303  map.emplace(name, std::move(elm));
304  }
305  }
306  };
307 
308  /* IMPLEMENTATIONS */
309 
310  template <class MessageBufferType>
311  void GroupConstraints::write(MessageBufferType& buffer) const {
312  buffer.write(this->currentProdConstraint);
313  buffer.write(this->currentGasInjectionConstraint);
314  buffer.write(this->currentWaterInjectionConstraint);
315  }
316 
317  template <class MessageBufferType>
318  void GroupConstraints::read(MessageBufferType& buffer) {
319  buffer.read(this->currentProdConstraint);
320  buffer.read(this->currentGasInjectionConstraint);
321  buffer.read(this->currentWaterInjectionConstraint);
322  }
323 
324  inline GroupConstraints&
325  GroupConstraints::set(Opm::Group::ProductionCMode cpc,
326  Opm::Group::InjectionCMode cgic,
327  Opm::Group::InjectionCMode cwic)
328  {
329  this->currentGasInjectionConstraint = cgic;
330  this->currentWaterInjectionConstraint = cwic;
331  this->currentProdConstraint = cpc;
332 
333  return *this;
334  }
335 
336 }} // Opm::data
337 
338 #endif //OPM_OUTPUT_GROUPS_HPP
Definition: JsonObject.hpp:32
Class for (de-)serializing.
Definition: Serializer.hpp:75
Definition: Groups.hpp:209
Definition: GuideRateValue.hpp:32
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Definition: Groups.hpp:36
Definition: Groups.hpp:126
Definition: Groups.hpp:80
Definition: Groups.hpp:173