WellState.hpp
Go to the documentation of this file.
1/*
2 Copyright 2014 SINTEF ICT, Applied Mathematics.
3 Copyright 2017 IRIS AS
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_WELLSTATEFULLYIMPLICITBLACKOIL_HEADER_INCLUDED
22#define OPM_WELLSTATEFULLYIMPLICITBLACKOIL_HEADER_INCLUDED
23
24#include <dune/common/version.hh>
25#include <dune/common/parallel/mpihelper.hh>
26
27#include <opm/common/ErrorMacros.hpp>
28
29#include <opm/material/fluidsystems/PhaseUsageInfo.hpp>
30
31#include <opm/input/eclipse/Schedule/Events.hpp>
32
33#include <opm/output/data/Wells.hpp>
34
40
42
43#include <algorithm>
44#include <cstddef>
45#include <cstdint>
46#include <functional>
47#include <map>
48#include <optional>
49#include <string>
50#include <utility>
51#include <vector>
52
53namespace Opm {
54
55template<class Scalar> class ParallelWellInfo;
56template<class Scalar> struct PerforationData;
57template<class Scalar> class ConnFracStatistics;
58class Schedule;
59enum class WellStatus : std::uint8_t;
60
61} // namespace Opm
62
63namespace Opm {
66template<typename Scalar, typename IndexTraits>
68{
69public:
71 {
72 RsConstInfo() = default;
73 RsConstInfo(const bool en, const Scalar val)
74 : enabled { en }
75 , value { val }
76 {}
77
78 // True when RSCONST-based reporting should be applied.
79 bool enabled {false};
80
81 // Constant Rs value (surface gas volume per surface oil volume).
82 Scalar value {};
83 };
84
85 static const std::uint64_t event_mask = ScheduleEvents::WELL_STATUS_CHANGE
86 | ScheduleEvents::PRODUCTION_UPDATE
87 | ScheduleEvents::INJECTION_UPDATE;
88
89 // TODO: same definition with WellInterface, eventually they should go to a common header file.
90
91 // TODO: trying to PhasedIdx to make it more explicit as phase index, will evaluate how it goes.
92 // TODO: eventually, we should distinguish phase index and component index
93 static const int waterPhaseIdx = IndexTraits::waterPhaseIdx;
94 static const int oilPhaseIdx = IndexTraits::oilPhaseIdx;
95 static const int gasPhaseIdx = IndexTraits::gasPhaseIdx;
96
97 // Only usable for testing purposes
98 explicit WellState(const ParallelWellInfo<Scalar>& pinfo);
99
101 : phaseUsageInfo_(pu)
102 {}
103
105
106 std::size_t size() const
107 {
108 return this->wells_.size();
109 }
110
111 std::vector<std::string> wells() const
112 {
113 return this->wells_.wells();
114 }
115
116 int numWells() const
117 {
118 return this->size();
119 }
120
121 // TODO: temporary naming during development, will change to phaseUsage() later
123 {
124 return this->phaseUsageInfo_;
125 }
126
127 const ParallelWellInfo<Scalar>& parallelWellInfo(std::size_t well_index) const;
128
132 void init(const std::vector<Scalar>& cellPressures,
133 const std::vector<Scalar>& cellTemperatures,
134 const Schedule& schedule,
135 const std::vector<Well>& wells_ecl,
136 const std::vector<std::reference_wrapper<ParallelWellInfo<Scalar>>>& parallel_well_info,
137 const int report_step,
138 const WellState* prevState,
139 const std::vector<std::vector<PerforationData<Scalar>>>& well_perf_data,
140 const SummaryState& summary_state,
141 const bool enableDistributedWells);
142
143 void resize(const std::vector<Well>& wells_ecl,
144 const std::vector<std::reference_wrapper<ParallelWellInfo<Scalar>>>& parallel_well_info,
145 const Schedule& schedule,
146 const bool handle_ms_well,
147 const std::size_t numCells,
148 const std::vector<std::vector<PerforationData<Scalar>>>& well_perf_data,
149 const SummaryState& summary_state,
150 const bool enable_distributed_wells,
151 const bool thermal);
152
153 void setCurrentWellRates(const std::string& wellName,
154 const std::vector<Scalar>& new_rates)
155 {
156 auto& [owner, rates] = this->well_rates.at(wellName);
157 if (owner)
158 rates = new_rates;
159 }
160
161 const std::vector<Scalar>& currentWellRates(const std::string& wellName) const;
162
163 bool hasWellRates(const std::string& wellName) const
164 {
165 return this->well_rates.find(wellName) != this->well_rates.end();
166 }
167
169 {
170 this->well_rates.clear();
171 }
172
173 void gatherVectorsOnRoot(const std::vector<data::Connection>& from_connections,
174 std::vector<data::Connection>& to_connections,
175 const Parallel::Communication& comm) const;
176
177 data::Wells
178 report(const int* globalCellIdxMap,
179 const std::function<bool(const std::size_t)>& wasDynamicallyClosed,
180 const RsConstInfo& rsConst = RsConstInfo{}) const;
181
183 const std::function<bool(const std::string&)>& wasDynamicallyClosed,
184 const std::function<double(const std::string&)>& wellEffFac,
185 const std::function<void(int, double)>& copt) const;
186
192 void initWellStateMSWell(const std::vector<Well>& wells_ecl,
193 const WellState* prev_well_state,
194 const bool thermal);
195
197 const std::vector<std::vector<int>>& segment_inlets,
198 const std::vector<std::vector<int>>& segment_perforations,
199 const std::vector<Scalar>& perforation_rates,
200 const int np,
201 const int segment,
202 std::vector<Scalar>& segment_rates);
203
204
206
208 const std::vector<WellStatus>& well_status);
209 void updateEfficiencyScalingFactor(const std::string& wellName,
210 const Scalar value);
211
212 bool isInjectionGrup(const std::string& name) const
213 {
214 return this->global_well_info.value().in_injecting_group(name);
215 }
216
217 bool isProductionGrup(const std::string& name) const
218 {
219 return this->global_well_info.value().in_producing_group(name);
220 }
221
222 bool isOpen(const std::string& name) const
223 {
224 return this->global_well_info.value().is_open(name);
225 }
226
227 Scalar getGlobalEfficiencyScalingFactor(const std::string& name) const
228 {
229 return this->global_well_info.value().efficiency_scaling_factor(name);
230 }
231
232 // If the ALQ has changed since the previous time step,
233 // reset current_alq and update default_alq. ALQ is used for
234 // constant lift gas injection and for gas lift optimization
235 // (THP controlled wells).
236 void updateWellsDefaultALQ(const Schedule& schedule,
237 const int report_step,
238 const SummaryState& summary_state);
239
241 {
242 for (size_t i = 0; i < this->size(); ++i) {
243 this->wells_[i].alq_state.reset_count();
244 }
245 }
246
247
248 int wellNameToGlobalIdx(const std::string& name)
249 {
250 return this->global_well_info.value().well_index(name);
251 }
252
253 std::string globalIdxToWellName(const int index)
254 {
255 return this->global_well_info.value().well_name(index);
256 }
257
258 bool wellIsOwned(std::size_t well_index,
259 const std::string& wellName) const;
260
261 bool wellIsOwned(const std::string& wellName) const;
262
263 bool isRank0() const {
264 return this->global_well_info.value().isRank0();
265 }
266
267 void updateStatus(std::size_t well_index, WellStatus status);
268
269 void openWell(std::size_t well_index);
270 void shutWell(std::size_t well_index);
271 void stopWell(std::size_t well_index);
272
274 constexpr int numPhases() const
275 {
276 return phaseUsageInfo_.numActivePhases();
277 }
278
280 std::vector<Scalar>& wellRates(std::size_t well_index)
281 { return this->wells_[well_index].surface_rates; }
282 const std::vector<Scalar>& wellRates(std::size_t well_index) const
283 { return this->wells_[well_index].surface_rates; }
284
285 const std::string& name(std::size_t well_index) const
286 {
287 return this->wells_.well_name(well_index);
288 }
289
290 std::optional<std::size_t> index(const std::string& well_name) const
291 {
292 return this->wells_.well_index(well_name);
293 }
294
295 const SingleWellState<Scalar, IndexTraits>& operator[](std::size_t well_index) const
296 {
297 return this->wells_[well_index];
298 }
299
300 const SingleWellState<Scalar, IndexTraits>& operator[](const std::string& well_name) const
301 {
302 return this->wells_[well_name];
303 }
304
306 {
307 return this->wells_[well_index];
308 }
309
311 {
312 return this->wells_[well_name];
313 }
314
315 const SingleWellState<Scalar, IndexTraits>& well(std::size_t well_index) const
316 {
317 return this->operator[](well_index);
318 }
319
320 const SingleWellState<Scalar, IndexTraits>& well(const std::string& well_name) const
321 {
322 return this->operator[](well_name);
323 }
324
326 {
327 return this->operator[](well_index);
328 }
329
330 SingleWellState<Scalar, IndexTraits>& well(const std::string& well_name)
331 {
332 return this->operator[](well_name);
333 }
334
335 bool has(const std::string& well_name) const
336 {
337 return this->wells_.has(well_name);
338 }
339
340 bool operator==(const WellState&) const;
341
342 template<class Serializer>
343 void serializeOp(Serializer& serializer)
344 {
345 serializer(well_rates);
346 if (serializer.isSerializing()) {
347 serializer(wells_.size());
348 } else {
349 std::size_t serialized_size = 0;
350 serializer(serialized_size);
351 if (serialized_size != wells_.size()) {
352 OPM_THROW(std::runtime_error, "Error deserializing WellState: size mismatch");
353 }
354 }
355 for (auto& w : wells_) {
356 serializer(w);
357 }
358 serializer(permanently_inactive_well_names_);
359 }
360
361 bool is_permanently_inactive_well(const std::string& wname) const {
362 return std::ranges::find(this->permanently_inactive_well_names_, wname) !=
363 this->permanently_inactive_well_names_.end();
364 }
365
366private:
367 bool enableDistributedWells_ = false;
368
369 PhaseUsageInfo<IndexTraits> phaseUsageInfo_;
370
371 // The wells_ variable is essentially a map of all the wells on the current
372 // process. Observe that since a well can be split over several processes a
373 // well might appear in the WellContainer on different processes.
375
376 // The members global_well_info and well_rates are map like
377 // structures which will have entries for *all* the wells in the system.
378
379 // Use of std::optional<> here is a technical crutch, the
380 // WellStateFullyImplicitBlackoil class should be default constructible,
381 // whereas the GlobalWellInfo is not.
382 std::optional<GlobalWellInfo<Scalar>> global_well_info;
383
384 // The well_rates variable is defined for all wells on all processors. The
385 // bool in the value pair is whether the current process owns the well or
386 // not.
387 std::map<std::string, std::pair<bool, std::vector<Scalar>>> well_rates;
388
389 // Keep track of permanently inactive well names
390 std::vector<std::string> permanently_inactive_well_names_;
391
397 void base_init(const std::vector<Scalar>& cellPressures,
398 const std::vector<Scalar>& cellTemperatures,
399 const std::vector<Well>& wells_ecl,
400 const std::vector<std::reference_wrapper<ParallelWellInfo<Scalar>>>& parallel_well_info,
401 const std::vector<std::vector<PerforationData<Scalar>>>& well_perf_data,
402 const SummaryState& summary_state);
403
404 void initSingleWell(const std::vector<Scalar>& cellPressures,
405 const std::vector<Scalar>& cellTemperatures,
406 const Well& well,
407 const std::vector<PerforationData<Scalar>>& well_perf_data,
408 const ParallelWellInfo<Scalar>& well_info,
409 const SummaryState& summary_state);
410
411 void initSingleProducer(const Well& well,
412 const ParallelWellInfo<Scalar>& well_info,
413 Scalar pressure_first_connection,
414 Scalar temperature_first_connection,
415 const std::vector<PerforationData<Scalar>>& well_perf_data,
416 const SummaryState& summary_state);
417
418 void initSingleInjector(const Well& well,
419 const ParallelWellInfo<Scalar>& well_info,
420 Scalar pressure_first_connection,
421 Scalar temperature_first_connection,
422 const std::vector<PerforationData<Scalar>>& well_perf_data,
423 const SummaryState& summary_state);
424
425 static void calculateSegmentRatesBeforeSum(const ParallelWellInfo<Scalar>& pw_info,
426 const std::vector<std::vector<int>>& segment_inlets,
427 const std::vector<std::vector<int>>& segment_perforations,
428 const std::vector<Scalar>& perforation_rates,
429 const int np,
430 const int segment,
431 std::vector<Scalar>& segment_rates);
432
433 void reportConnections(std::vector<data::Connection>& connections,
434 std::size_t well_index,
435 const int* globalCellIdxMap) const;
436
437 data::Segment
438 reportSegmentResults(const int well_id,
439 const int seg_ix,
440 const int seg_no) const;
441
442 void reportConnectionFactors(const std::size_t well_index,
443 std::vector<data::Connection>& connections) const;
444
445 void reportConnectionPressuresAndRates(const std::size_t well_index,
446 std::vector<data::Connection>& connections) const;
447
448 void reportConnectionFilterCake(const std::size_t well_index,
449 std::vector<data::Connection>& connections) const;
450
451 void reportConnectionFracture(const std::size_t well_index,
452 std::vector<data::Connection>& connections) const;
453
454 void reportFractureStatistics(const std::vector<ConnFracStatistics<Scalar>>& stats,
455 std::vector<data::Connection>& connections) const;
456};
457
458} // namespace Opm
459
460// TODO: we are missing pu, while it was not there in the first place
461#endif // OPM_WELLSTATEFULLYIMPLICITBLACKOIL_HEADER_INCLUDED
Definition: ConnFracStatistics.hpp:37
Class encapsulating some information about parallel wells.
Definition: ParallelWellInfo.hpp:217
Definition: GasLiftGroupInfo.hpp:38
Definition: SingleWellState.hpp:44
Definition: WellContainer.hpp:46
Definition: WellState.hpp:68
bool has(const std::string &well_name) const
Definition: WellState.hpp:335
void openWell(std::size_t well_index)
void clearWellRates()
Definition: WellState.hpp:168
bool isProductionGrup(const std::string &name) const
Definition: WellState.hpp:217
bool wellIsOwned(std::size_t well_index, const std::string &wellName) const
SingleWellState< Scalar, IndexTraits > & operator[](std::size_t well_index)
Definition: WellState.hpp:305
constexpr int numPhases() const
The number of phases present.
Definition: WellState.hpp:274
SingleWellState< Scalar, IndexTraits > & well(std::size_t well_index)
Definition: WellState.hpp:325
bool hasWellRates(const std::string &wellName) const
Definition: WellState.hpp:163
bool is_permanently_inactive_well(const std::string &wname) const
Definition: WellState.hpp:361
std::string globalIdxToWellName(const int index)
Definition: WellState.hpp:253
const ParallelWellInfo< Scalar > & parallelWellInfo(std::size_t well_index) const
void updateGlobalIsGrup(const Parallel::Communication &comm, const std::vector< WellStatus > &well_status)
void communicateGroupRates(const Parallel::Communication &comm)
static const int gasPhaseIdx
Definition: WellState.hpp:95
bool isInjectionGrup(const std::string &name) const
Definition: WellState.hpp:212
int wellNameToGlobalIdx(const std::string &name)
Definition: WellState.hpp:248
static const int waterPhaseIdx
Definition: WellState.hpp:93
void reportIntervalConnectionOilProduction(const double dt, const std::function< bool(const std::string &)> &wasDynamicallyClosed, const std::function< double(const std::string &)> &wellEffFac, const std::function< void(int, double)> &copt) const
void gatherVectorsOnRoot(const std::vector< data::Connection > &from_connections, std::vector< data::Connection > &to_connections, const Parallel::Communication &comm) const
void gliftTimeStepInit()
Definition: WellState.hpp:240
const SingleWellState< Scalar, IndexTraits > & operator[](const std::string &well_name) const
Definition: WellState.hpp:300
bool operator==(const WellState &) const
WellState(const ParallelWellInfo< Scalar > &pinfo)
void setCurrentWellRates(const std::string &wellName, const std::vector< Scalar > &new_rates)
Definition: WellState.hpp:153
int numWells() const
Definition: WellState.hpp:116
static const int oilPhaseIdx
Definition: WellState.hpp:94
Scalar getGlobalEfficiencyScalingFactor(const std::string &name) const
Definition: WellState.hpp:227
const std::vector< Scalar > & currentWellRates(const std::string &wellName) const
const PhaseUsageInfo< IndexTraits > & phaseUsageInfo() const
Definition: WellState.hpp:122
std::vector< std::string > wells() const
Definition: WellState.hpp:111
bool isRank0() const
Definition: WellState.hpp:263
const SingleWellState< Scalar, IndexTraits > & well(const std::string &well_name) const
Definition: WellState.hpp:320
static const std::uint64_t event_mask
Definition: WellState.hpp:85
static WellState serializationTestObject(const ParallelWellInfo< Scalar > &pinfo)
std::size_t size() const
Definition: WellState.hpp:106
void stopWell(std::size_t well_index)
WellState(const PhaseUsageInfo< IndexTraits > &pu)
Definition: WellState.hpp:100
static void calculateSegmentRates(const ParallelWellInfo< Scalar > &pw_info, const std::vector< std::vector< int > > &segment_inlets, const std::vector< std::vector< int > > &segment_perforations, const std::vector< Scalar > &perforation_rates, const int np, const int segment, std::vector< Scalar > &segment_rates)
void updateEfficiencyScalingFactor(const std::string &wellName, const Scalar value)
const SingleWellState< Scalar, IndexTraits > & well(std::size_t well_index) const
Definition: WellState.hpp:315
const SingleWellState< Scalar, IndexTraits > & operator[](std::size_t well_index) const
Definition: WellState.hpp:295
bool isOpen(const std::string &name) const
Definition: WellState.hpp:222
void shutWell(std::size_t well_index)
bool wellIsOwned(const std::string &wellName) const
const std::vector< Scalar > & wellRates(std::size_t well_index) const
Definition: WellState.hpp:282
std::vector< Scalar > & wellRates(std::size_t well_index)
One rate per well and phase.
Definition: WellState.hpp:280
void resize(const std::vector< Well > &wells_ecl, const std::vector< std::reference_wrapper< ParallelWellInfo< Scalar > > > &parallel_well_info, const Schedule &schedule, const bool handle_ms_well, const std::size_t numCells, const std::vector< std::vector< PerforationData< Scalar > > > &well_perf_data, const SummaryState &summary_state, const bool enable_distributed_wells, const bool thermal)
void updateWellsDefaultALQ(const Schedule &schedule, const int report_step, const SummaryState &summary_state)
void serializeOp(Serializer &serializer)
Definition: WellState.hpp:343
SingleWellState< Scalar, IndexTraits > & operator[](const std::string &well_name)
Definition: WellState.hpp:310
std::optional< std::size_t > index(const std::string &well_name) const
Definition: WellState.hpp:290
void initWellStateMSWell(const std::vector< Well > &wells_ecl, const WellState *prev_well_state, const bool thermal)
void init(const std::vector< Scalar > &cellPressures, const std::vector< Scalar > &cellTemperatures, const Schedule &schedule, const std::vector< Well > &wells_ecl, const std::vector< std::reference_wrapper< ParallelWellInfo< Scalar > > > &parallel_well_info, const int report_step, const WellState *prevState, const std::vector< std::vector< PerforationData< Scalar > > > &well_perf_data, const SummaryState &summary_state, const bool enableDistributedWells)
void updateStatus(std::size_t well_index, WellStatus status)
SingleWellState< Scalar, IndexTraits > & well(const std::string &well_name)
Definition: WellState.hpp:330
const std::string & name(std::size_t well_index) const
Definition: WellState.hpp:285
data::Wells report(const int *globalCellIdxMap, const std::function< bool(const std::size_t)> &wasDynamicallyClosed, const RsConstInfo &rsConst=RsConstInfo{}) const
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilbioeffectsmodules.hh:45
Static data associated with a well perforation.
Definition: PerforationData.hpp:30
Definition: WellState.hpp:71
Scalar value
Definition: WellState.hpp:82
RsConstInfo(const bool en, const Scalar val)
Definition: WellState.hpp:73
bool enabled
Definition: WellState.hpp:79