My Project
Loading...
Searching...
No Matches
WellTestState.hpp
1/*
2 Copyright 2018 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#ifndef WELLTEST_STATE_H
20#define WELLTEST_STATE_H
21
22#include <cstddef>
23#include <optional>
24#include <string>
25#include <unordered_map>
26#include <vector>
27#include <opm/io/eclipse/rst/state.hpp>
28
29namespace {
30
31template<class BufferType, class M>
32void pack_map(BufferType& buffer, const M& m) {
33 buffer.write(m.size());
34 for (const auto& [k,v] : m) {
35 buffer.write(k);
36 v.pack(buffer);
37 }
38}
39
40template<class BufferType, class M>
41void unpack_map(BufferType& buffer, M& m) {
42 typename M::size_type size;
43 buffer.read(size);
44 for (std::size_t i = 0; i < size; i++) {
45 typename M::key_type k;
46 typename M::mapped_type v;
47 buffer.read(k);
48 v.unpack(buffer);
49 m.emplace(std::move(k), std::move(v));
50 }
51}
52
53}
54
55namespace Opm {
56
57class WellTestConfig;
58namespace WTest { enum class Reason; }
59
61public:
62 /*
63 This class implements a small mutable state object which keeps track of
64 which wells have been automatically closed by the simulator through the
65 WTEST mechanism.
66
67 The default behavior of the container is to manage *closed* wells, but
68 since the counter mechanism for the maximum number of opening attempts
69 should maintain the same counter through open/close events we need to
70 manage the well object also after they have been opened up again.
71 */
72 struct RestartWell {
73 std::string name;
74 double test_interval;
75 int num_test;
76 double startup_time;
77
78 int config_reasons;
79 int close_reason;
80
81 RestartWell(const std::string& wname, double ti, int num, double st, int r1, int r2)
82 : name(wname)
83 , test_interval(ti)
84 , num_test(num)
85 , startup_time(st)
86 , config_reasons(r1)
87 , close_reason(r2)
88 {};
89 };
90
91 struct WTestWell {
92 std::string name;
93 WTest::Reason reason;
94 double last_test;
95
96 int num_attempt{0};
97 bool closed{true};
98 std::optional<int> wtest_report_step;
99
100 WTestWell() = default;
101 WTestWell(const std::string& wname, WTest::Reason reason_, double last_test);
102
103 int int_reason() const;
104 static WTest::Reason inverse_ecl_reason(int ecl_reason);
105
106 bool operator==(const WTestWell& other) const {
107 return this->name == other.name &&
108 this->reason == other.reason &&
109 this->last_test == other.last_test &&
110 this->num_attempt == other.num_attempt &&
111 this->closed == other.closed &&
112 this->wtest_report_step == other.wtest_report_step;
113 }
114
115 static WTestWell serializationTestObject();
116
117 template<class Serializer>
118 void serializeOp(Serializer& serializer)
119 {
120 serializer(this->name);
121 serializer(this->reason);
122 serializer(this->last_test);
123 serializer(this->num_attempt);
124 serializer(this->closed);
125 serializer(this->wtest_report_step);
126 }
127
128 template<class BufferType>
129 void pack(BufferType& buffer) const {
130 buffer.write(this->name);
131 buffer.write(this->reason);
132 buffer.write(this->last_test);
133 buffer.write(this->num_attempt);
134 buffer.write(this->closed);
135 buffer.write(this->wtest_report_step);
136 }
137
138 template<class BufferType>
139 void unpack(BufferType& buffer) {
140 buffer.read(this->name);
141 buffer.read(this->reason);
142 buffer.read(this->last_test);
143 buffer.read(this->num_attempt);
144 buffer.read(this->closed);
145 buffer.read(this->wtest_report_step);
146 }
147 };
148
149
151 std::string wellName;
152 int complnum;
153 double last_test;
154 int num_attempt;
155
156 bool operator==(const ClosedCompletion& other) const {
157 return this->wellName == other.wellName &&
158 this->complnum == other.complnum &&
159 this->last_test == other.last_test &&
160 this->num_attempt == other.num_attempt;
161 }
162
163 static ClosedCompletion serializationTestObject();
164
165 template<class Serializer>
166 void serializeOp(Serializer& serializer)
167 {
168 serializer(this->wellName);
169 serializer(this->complnum);
170 serializer(this->last_test);
171 serializer(this->num_attempt);
172 }
173
174 template<class BufferType>
175 void pack(BufferType& buffer) const {
176 buffer.write(this->wellName);
177 buffer.write(this->complnum);
178 buffer.write(this->last_test);
179 buffer.write(this->num_attempt);
180 }
181
182 template<class BufferType>
183 void unpack(BufferType& buffer) {
184 buffer.read(this->wellName);
185 buffer.read(this->complnum);
186 buffer.read(this->last_test);
187 buffer.read(this->num_attempt);
188 }
189 };
190
191 WellTestState() = default;
192 WellTestState(std::time_t start_time, const RestartIO::RstState& rst_state);
193
194
195 std::vector<std::string> test_wells(const WellTestConfig& config, double sim_time);
196 void filter_wells(const std::vector<std::string>& existing_wells);
197 /*
198 The purpose of this container is to manage explicitly *closed wells*,
199 since the class has no relation to the set of of wells defined in the
200 Schedule section the concept of open wells and totally unknown wells is
201 slightly murky:
202
203 well_is_closed("UNKOWN_WELL") -> false
204
205 This implies that we have not explicitly closed a well with name
206 'UNKNOWN_WELL', but since we do not know whether the well is at all
207 defined it does not make sense to extrapolate to:
208
209 well_is_open("UNKNOWN_WELL") -> true.
210
211 That is the reason we do not have any xxx_is_open() predicates.
212 */
213 void close_well(const std::string& well_name, WTest::Reason reason, double sim_time);
214 bool well_is_closed(const std::string& well_name) const;
215 void open_well(const std::string& well_name);
216 std::size_t num_closed_wells() const;
217 double lastTestTime(const std::string& well_name) const;
218
219 void close_completion(const std::string& well_name, int complnum, double sim_time);
220 void open_completion(const std::string& well_name, int complnum);
221 void open_completions(const std::string& well_name);
222 bool completion_is_closed(const std::string& well_name, const int complnum) const;
223 std::size_t num_closed_completions() const;
224
225 void clear();
226
227
228 template<class BufferType>
229 void pack(BufferType& buffer) const {
230 pack_map(buffer, this->wells);
231
232 buffer.write(this->completions.size());
233 for (const auto& [well, cmap] : this->completions) {
234 buffer.write(well);
235 pack_map(buffer, cmap);
236 }
237 }
238
239 template<class BufferType>
240 void unpack(BufferType& buffer) {
241 unpack_map(buffer, this->wells);
242 std::size_t size;
243 buffer.read(size);
244 for (std::size_t i = 0; i < size; i++) {
245 std::string well;
246 std::unordered_map<int, ClosedCompletion> cmap;
247
248 buffer.read(well);
249 unpack_map(buffer, cmap);
250 this->completions.emplace(std::move(well), std::move(cmap));
251 }
252 }
253
254 template<class Serializer>
255 void serializeOp(Serializer& serializer)
256 {
257 serializer(this->wells);
258 if (serializer.isSerializing()) {
259 std::size_t size = this->completions.size();
260 serializer(size);
261 for (auto& [well, comp_map] : this->completions) {
262 serializer(well);
263 serializer(comp_map);
264 }
265 } else {
266 std::size_t size = 0;
267 serializer(size);
268 for (std::size_t i=0; i < size; i++) {
269 std::string well;
270 std::unordered_map<int, ClosedCompletion> comp_map;
271
272 serializer(well);
273 serializer(comp_map);
274 this->completions.emplace(well, std::move(comp_map));
275 }
276 }
277 }
278
279
280 static WellTestState serializationTestObject();
281 bool operator==(const WellTestState& other) const;
282
283 std::optional<WellTestState::RestartWell> restart_well(const Opm::WellTestConfig& config, const std::string& wname) const;
284
285private:
286 std::unordered_map<std::string, WTestWell> wells;
287 std::unordered_map<std::string, std::unordered_map<int, ClosedCompletion>> completions;
288
289 std::vector<std::pair<std::string, int>> updateCompletion(const WellTestConfig& config, double sim_time);
290};
291
292
293}
294
295#endif
296
Class for (de-)serializing.
Definition Serializer.hpp:84
Definition WellTestConfig.hpp:64
Definition WellTestState.hpp:60
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Definition state.hpp:54
Definition WellTestState.hpp:150
Definition WellTestState.hpp:72
Definition WellTestState.hpp:91