My Project
GroupState.hpp
1 /*
2  Copyright 2021 Equinor
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_GROUPSTATE_HEADER_INCLUDED
21 #define OPM_GROUPSTATE_HEADER_INCLUDED
22 
23 #include <map>
24 #include <vector>
25 
26 #include <opm/core/props/BlackoilPhases.hpp>
27 #include <opm/parser/eclipse/EclipseState/Schedule/Group/Group.hpp>
28 #include <opm/parser/eclipse/EclipseState/Runspec.hpp>
29 #include <opm/parser/eclipse/EclipseState/Schedule/Group/GPMaint.hpp>
30 #include <opm/simulators/wells/WellContainer.hpp>
31 
32 namespace Opm {
33 
34 class GroupState {
35 public:
36  explicit GroupState(std::size_t num_phases);
37  bool operator==(const GroupState& other) const;
38 
39  bool has_production_rates(const std::string& gname) const;
40  void update_production_rates(const std::string& gname, const std::vector<double>& rates);
41  const std::vector<double>& production_rates(const std::string& gname) const;
42 
43  bool has_production_reduction_rates(const std::string& gname) const;
44  void update_production_reduction_rates(const std::string& gname, const std::vector<double>& rates);
45  const std::vector<double>& production_reduction_rates(const std::string& gname) const;
46 
47  bool has_injection_reduction_rates(const std::string& gname) const;
48  void update_injection_reduction_rates(const std::string& gname, const std::vector<double>& rates);
49  const std::vector<double>& injection_reduction_rates(const std::string& gname) const;
50 
51  bool has_injection_reservoir_rates(const std::string& gname) const;
52  void update_injection_reservoir_rates(const std::string& gname, const std::vector<double>& rates);
53  const std::vector<double>& injection_reservoir_rates(const std::string& gname) const;
54 
55  bool has_injection_surface_rates(const std::string& gname) const;
56  void update_injection_surface_rates(const std::string& gname, const std::vector<double>& rates);
57  const std::vector<double>& injection_surface_rates(const std::string& gname) const;
58 
59 
60  void update_injection_rein_rates(const std::string& gname, const std::vector<double>& rates);
61  const std::vector<double>& injection_rein_rates(const std::string& gname) const;
62 
63  void update_injection_vrep_rate(const std::string& gname, double rate);
64  double injection_vrep_rate(const std::string& gname) const;
65 
66  void update_grat_sales_target(const std::string& gname, double target);
67  double grat_sales_target(const std::string& gname) const;
68  bool has_grat_sales_target(const std::string& gname) const;
69 
70  void update_gpmaint_target(const std::string& gname, double target);
71  double gpmaint_target(const std::string& gname) const;
72  bool has_gpmaint_target(const std::string& gname) const;
73 
74  bool has_production_control(const std::string& gname) const;
75  void production_control(const std::string& gname, Group::ProductionCMode cmode);
76  Group::ProductionCMode production_control(const std::string& gname) const;
77 
78  bool has_injection_control(const std::string& gname, Phase phase) const;
79  void injection_control(const std::string& gname, Phase phase, Group::InjectionCMode cmode);
80  Group::InjectionCMode injection_control(const std::string& gname, Phase phase) const;
81 
82  std::size_t data_size() const;
83  std::size_t collect(double * data) const;
84  std::size_t distribute(const double * data);
85 
86  GPMaint::State& gpmaint(const std::string& gname);
87 
88 
89  template<class Comm>
90  void communicate_rates(const Comm& comm)
91  {
92  // Note that injection_group_vrep_rates is handled separate from
93  // the forAllGroupData() function, since it contains single doubles,
94  // not vectors.
95 
96  // Create a function that calls some function
97  // for all the individual data items to simplify
98  // the further code.
99  auto iterateContainer = [](auto& container, auto& func) {
100  for (auto& x : container) {
101  func(x.second);
102  }
103  };
104 
105 
106  auto forAllGroupData = [&](auto& func) {
107  iterateContainer(m_production_rates, func);
108  iterateContainer(prod_red_rates, func);
109  iterateContainer(inj_red_rates, func);
110  iterateContainer(inj_resv_rates, func);
111  iterateContainer(inj_rein_rates, func);
112  iterateContainer(inj_surface_rates, func);
113  };
114 
115  // Compute the size of the data.
116  std::size_t sz = 0;
117  auto computeSize = [&sz](const auto& v) {
118  sz += v.size();
119  };
120  forAllGroupData(computeSize);
121  sz += this->inj_vrep_rate.size();
122 
123  // Make a vector and collect all data into it.
124  std::vector<double> data(sz);
125  std::size_t pos = 0;
126 
127 
128 
129  // That the collect function mutates the vector v is an artifact for
130  // testing.
131  auto collect = [&data, &pos](auto& v) {
132  for (auto& x : v) {
133  data[pos++] = x;
134  x = -1;
135  }
136  };
137  forAllGroupData(collect);
138  for (const auto& x : this->inj_vrep_rate) {
139  data[pos++] = x.second;
140  }
141  if (pos != sz)
142  throw std::logic_error("Internal size mismatch when collecting groupData");
143 
144  // Communicate it with a single sum() call.
145  comm.sum(data.data(), data.size());
146 
147  // Distribute the summed vector to the data items.
148  pos = 0;
149  auto distribute = [&data, &pos](auto& v) {
150  for (auto& x : v) {
151  x = data[pos++];
152  }
153  };
154  forAllGroupData(distribute);
155  for (auto& x : this->inj_vrep_rate) {
156  x.second = data[pos++];
157  }
158  if (pos != sz)
159  throw std::logic_error("Internal size mismatch when distributing groupData");
160  }
161 
162  std::string dump() const;
163 
164 
165 private:
166  std::size_t num_phases;
167  std::map<std::string, std::vector<double>> m_production_rates;
168  std::map<std::string, Group::ProductionCMode> production_controls;
169  std::map<std::string, std::vector<double>> prod_red_rates;
170  std::map<std::string, std::vector<double>> inj_red_rates;
171  std::map<std::string, std::vector<double>> inj_surface_rates;
172  std::map<std::string, std::vector<double>> inj_resv_rates;
173  std::map<std::string, std::vector<double>> inj_rein_rates;
174  std::map<std::string, double> inj_vrep_rate;
175  std::map<std::string, double> m_grat_sales_target;
176  std::map<std::string, double> m_gpmaint_target;
177 
178 
179  std::map<std::pair<Phase, std::string>, Group::InjectionCMode> injection_controls;
180  WellContainer<GPMaint::State> gpmaint_state;
181 };
182 
183 }
184 
185 #endif
Definition: GroupState.hpp:34
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:26