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