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/parser/eclipse/EclipseState/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 
65  struct GroupGuideRates {
66  GuideRateValue production{};
67  GuideRateValue injection{};
68 
69  template <class MessageBufferType>
70  void write(MessageBufferType& buffer) const
71  {
72  this->production.write(buffer);
73  this->injection .write(buffer);
74  }
75 
76  template <class MessageBufferType>
77  void read(MessageBufferType& buffer)
78  {
79  this->production.read(buffer);
80  this->injection .read(buffer);
81  }
82 
83  bool operator==(const GroupGuideRates& other) const
84  {
85  return this->production == other.production
86  && this->injection == other.injection;
87  }
88 
89  void init_json(Json::JsonObject& json_data) const {
90  auto json_prod = json_data.add_object("production");
91  this->production.init_json(json_prod);
92 
93  auto json_inj = json_data.add_object("injection");
94  this->injection.init_json(json_inj);
95  }
96  };
97 
98  struct GroupData {
99  GroupConstraints currentControl;
100  GroupGuideRates guideRates{};
101 
102  template <class MessageBufferType>
103  void write(MessageBufferType& buffer) const
104  {
105  this->currentControl.write(buffer);
106  this->guideRates .write(buffer);
107  }
108 
109  template <class MessageBufferType>
110  void read(MessageBufferType& buffer)
111  {
112  this->currentControl.read(buffer);
113  this->guideRates .read(buffer);
114  }
115 
116  bool operator==(const GroupData& other) const
117  {
118  return this->currentControl == other.currentControl
119  && this->guideRates == other.guideRates;
120  }
121 
122 
123  void init_json(Json::JsonObject& json_data) const {
124  auto json_constraints = json_data.add_object("constraints");
125  this->currentControl.init_json(json_constraints);
126 
127  auto json_gr = json_data.add_object("guide_rate");
128  this->guideRates.init_json(json_gr);
129  }
130  };
131 
132  struct NodeData {
133  double pressure { 0.0 };
134 
135  template <class MessageBufferType>
136  void write(MessageBufferType& buffer) const
137  {
138  buffer.write(this->pressure);
139  }
140 
141  template <class MessageBufferType>
142  void read(MessageBufferType& buffer)
143  {
144  buffer.read(this->pressure);
145  }
146 
147  bool operator==(const NodeData& other) const
148  {
149  return this->pressure == other.pressure;
150  }
151 
152  void init_json(Json::JsonObject& json_data) const {
153  json_data.add_item("pressure", this->pressure);
154  }
155  };
156 
158  public:
159  std::map<std::string, GroupData> groupData {};
160  std::map<std::string, NodeData> nodeData {};
161 
162  template <class MessageBufferType>
163  void write(MessageBufferType& buffer) const
164  {
165  this->writeMap(this->groupData, buffer);
166  this->writeMap(this->nodeData, buffer);
167  }
168 
169  template <class MessageBufferType>
170  void read(MessageBufferType& buffer)
171  {
172  this->readMap(buffer, this->groupData);
173  this->readMap(buffer, this->nodeData);
174  }
175 
176  bool operator==(const GroupAndNetworkValues& other) const
177  {
178  return (this->groupData == other.groupData)
179  && (this->nodeData == other.nodeData);
180  }
181 
182  void clear()
183  {
184  this->groupData.clear();
185  this->nodeData.clear();
186  }
187 
188  void init_json(Json::JsonObject& json_data) const {
189  auto group_data = json_data.add_object("group_data");
190  for (const auto& [gname, gdata] : this->groupData) {
191  auto group_json_data = group_data.add_object(gname);
192  gdata.init_json( group_json_data );
193  }
194 
195  auto node_data = json_data.add_object("node_data");
196  for (const auto& [gname, ndata] : this->nodeData) {
197  auto node_json_data = node_data.add_object(gname);
198  ndata.init_json( node_json_data );
199  }
200  }
201 
202  Json::JsonObject json() const {
203  Json::JsonObject json_data;
204  this->init_json(json_data);
205  return json_data;
206  }
207 
208  private:
209  template <class MessageBufferType, class ValueType>
210  void writeMap(const std::map<std::string, ValueType>& map,
211  MessageBufferType& buffer) const
212  {
213  const unsigned int size = map.size();
214  buffer.write(size);
215 
216  for (const auto& [name, elm] : map) {
217  buffer.write(name);
218  elm .write(buffer);
219  }
220  }
221 
222  template <class MessageBufferType, class ValueType>
223  void readMap(MessageBufferType& buffer,
224  std::map<std::string, ValueType>& map)
225  {
226  unsigned int size;
227  buffer.read(size);
228 
229  for (std::size_t i = 0; i < size; ++i) {
230  std::string name;
231  buffer.read(name);
232 
233  auto elm = ValueType{};
234  elm.read(buffer);
235 
236  map.emplace(name, std::move(elm));
237  }
238  }
239  };
240 
241  /* IMPLEMENTATIONS */
242 
243  template <class MessageBufferType>
244  void GroupConstraints::write(MessageBufferType& buffer) const {
245  buffer.write(this->currentProdConstraint);
246  buffer.write(this->currentGasInjectionConstraint);
247  buffer.write(this->currentWaterInjectionConstraint);
248  }
249 
250  template <class MessageBufferType>
251  void GroupConstraints::read(MessageBufferType& buffer) {
252  buffer.read(this->currentProdConstraint);
253  buffer.read(this->currentGasInjectionConstraint);
254  buffer.read(this->currentWaterInjectionConstraint);
255  }
256 
257  inline GroupConstraints&
258  GroupConstraints::set(Opm::Group::ProductionCMode cpc,
259  Opm::Group::InjectionCMode cgic,
260  Opm::Group::InjectionCMode cwic)
261  {
262  this->currentGasInjectionConstraint = cgic;
263  this->currentWaterInjectionConstraint = cwic;
264  this->currentProdConstraint = cpc;
265 
266  return *this;
267  }
268 
269 }} // Opm::data
270 
271 #endif //OPM_OUTPUT_GROUPS_HPP
Definition: JsonObject.hpp:31
Definition: Groups.hpp:157
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:98
Definition: Groups.hpp:65
Definition: Groups.hpp:132