GroupStateHelper.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor 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 OPM_GROUPSTATEHELPER_HEADER_INCLUDED
20#define OPM_GROUPSTATEHELPER_HEADER_INCLUDED
21#if HAVE_MPI
22#define RESERVOIR_COUPLING_ENABLED
23#endif
24#ifdef RESERVOIR_COUPLING_ENABLED
28#endif
29
30#include <opm/common/TimingMacros.hpp>
31#include <opm/input/eclipse/EclipseState/Grid/FieldPropsManager.hpp>
32#include <opm/input/eclipse/Schedule/Group/GPMaint.hpp>
33#include <opm/input/eclipse/Schedule/Group/GSatProd.hpp>
34#include <opm/input/eclipse/Schedule/Group/GuideRate.hpp>
35#include <opm/input/eclipse/Schedule/Schedule.hpp>
36#include <opm/input/eclipse/Schedule/ScheduleState.hpp>
37#include <opm/input/eclipse/Schedule/SummaryState.hpp>
38#include <opm/material/fluidsystems/PhaseUsageInfo.hpp>
43
45
46#include <algorithm>
47#include <map>
48#include <memory>
49#include <optional>
50#include <stdexcept>
51#include <string>
52#include <vector>
53
54namespace Opm
55{
56
57template <typename Scalar, typename IndexTraits>
59{
60public:
61 // RAII guard for temporarily setting wellstate pointer
63 {
64 public:
66 : groupStateHelper_ {groupStateHelper}
67 , previous_state_ptr_ {groupStateHelper_.well_state_}
68 {
69 // Set the new state directly
70 groupStateHelper_.well_state_ = &well_state;
71 }
72
74 {
75 // Restore the previous state
76 groupStateHelper_.well_state_ = previous_state_ptr_;
77 }
78
79 // Delete copy and move operations
84
85 private:
86 GroupStateHelper& groupStateHelper_;
87 const WellState<Scalar, IndexTraits>* previous_state_ptr_;
88 };
89
90 // RAII guard for temporarily setting groupstate pointer
92 {
93 public:
94 GroupStateGuard(GroupStateHelper& group_state_helper, GroupState<Scalar>& group_state)
95 : group_state_helper_ {group_state_helper}
96 , previous_state_ptr_ {group_state_helper.group_state_}
97 {
98 // Set the new state directly
99 group_state_helper_.group_state_ = &group_state;
100 }
101
103 {
104 // Restore the previous state
105 group_state_helper_.group_state_ = previous_state_ptr_;
106 }
107
108 // Delete copy and move operations
113
114 private:
115 GroupStateHelper& group_state_helper_;
116 GroupState<Scalar>* previous_state_ptr_ {nullptr};
117 };
118
120 GroupState<Scalar>& group_state,
121 const Schedule& schedule,
122 const SummaryState& summary_state,
123 const GuideRate& guide_rate,
124 const PhaseUsageInfo<IndexTraits>& phase_usage_info);
125
126 void accumulateGroupEfficiencyFactor(const Group& group, Scalar& factor) const;
127
128 std::pair<bool, Scalar> checkGroupConstraintsInj(const std::string& name,
129 const std::string& parent,
130 const Group& group,
131 const Scalar* rates,
132 const Phase injection_phase,
133 const Scalar efficiency_factor,
134 const std::vector<Scalar>& resv_coeff,
135 const bool check_guide_rate,
136 DeferredLogger& deferred_logger) const;
137
138 std::pair<bool, Scalar> checkGroupConstraintsProd(const std::string& name,
139 const std::string& parent,
140 const Group& group,
141 const Scalar* rates,
142 const Scalar efficiency_factor,
143 const std::vector<Scalar>& resv_coeff,
144 const bool check_guide_rate,
145 DeferredLogger& deferred_logger) const;
146
147 std::map<std::string, Scalar> computeNetworkPressures(const Network::ExtNetwork& network,
148 const VFPProdProperties<Scalar>& vfp_prod_props,
149 const Parallel::Communication& comm) const;
150
151 Scalar getGuideRate(const std::string& name, const GuideRateModel::Target target) const;
152
153 GuideRate::RateVector getProductionGroupRateVector(const std::string& group_name) const;
154
155 std::optional<Scalar> getWellGroupTargetInjector(const std::string& name,
156 const std::string& parent,
157 const Group& group,
158 const Scalar* rates,
159 const Phase injection_phase,
160 const Scalar efficiency_factor,
161 const std::vector<Scalar>& resv_coeff,
162 DeferredLogger& deferred_logger) const;
163
164 std::optional<Scalar> getWellGroupTargetProducer(const std::string& name,
165 const std::string& parent,
166 const Group& group,
167 const Scalar* rates,
168 const Scalar efficiency_factor,
169 const std::vector<Scalar>& resv_coeff,
170 DeferredLogger& deferred_logger) const;
171
172 GuideRate::RateVector getWellRateVector(const std::string& name) const;
173
174 std::vector<std::string> groupChainTopBot(const std::string& bottom, const std::string& top) const;
175
178 int groupControlledWells(const std::string& group_name,
179 const std::string& always_included_child,
180 const bool is_production_group,
181 const Phase injection_phase) const;
182
184 {
185 return *this->group_state_;
186 }
187
189 {
190 return this->phase_usage_info_;
191 }
192
194 {
195 return GroupStateGuard(*this, group_state);
196 }
197
199 {
200 return WellStateGuard(*this, well_state);
201 }
202
203 int reportStepIdx() const
204 {
205 return report_step_;
206 }
207
208 const Schedule& schedule() const
209 {
210 return this->schedule_;
211 }
212
213 const GuideRate& guideRate() const
214 {
215 return this->guide_rate_;
216 }
217
219 return this->phase_usage_info_;
220 }
221
222#ifdef RESERVOIR_COUPLING_ENABLED
224 {
225 return *this->reservoir_coupling_master_;
226 }
227
229 {
230 return *this->reservoir_coupling_slave_;
231 }
232#endif
233
234 void setCmodeGroup(const Group& group);
235
236 template <class AverageRegionalPressureType>
237 void
239 const FieldPropsManager& fp,
240 std::map<std::string, std::unique_ptr<AverageRegionalPressureType>>&
241 regional_average_pressure_calculator) const;
242
243 void setReportStep(int report_step)
244 {
245 report_step_ = report_step;
246 }
247
248#ifdef RESERVOIR_COUPLING_ENABLED
250 {
251 reservoir_coupling_master_ = reservoir_coupling_master;
252 }
253
255 {
256 reservoir_coupling_slave_ = reservoir_coupling_slave;
257 }
258#endif
259
260 const SummaryState& summaryState() const
261 {
262 return this->summary_state_;
263 }
264
265 Scalar sumSolventRates(const Group& group, const bool is_injector) const;
266
267 Scalar sumWellResRates(const Group& group, const int phase_pos, const bool injector) const;
268
269 Scalar sumWellSurfaceRates(const Group& group, const int phase_pos, const bool injector) const;
270
271 Scalar sumWellPhaseRates(bool res_rates,
272 const Group& group,
273 const int phase_pos,
274 const bool injector,
275 const bool network = false) const;
276
277 template <class RegionalValues>
278 void updateGpMaintTargetForGroups(const Group& group,
279 const RegionalValues& regional_values,
280 const double dt);
281
284 int updateGroupControlledWells(const bool is_production_group,
285 const Phase injection_phase,
286 DeferredLogger& deferred_logger);
287
288 void updateGroupProductionRates(const Group& group);
289
290 void updateGroupTargetReduction(const Group& group,
291 const bool is_injector);
292
294
295 void updateREINForGroups(const Group& group, bool sum_rank);
296
297 void updateReservoirRatesInjectionGroups(const Group& group);
298
300
301 void updateSurfaceRatesInjectionGroups(const Group& group);
302
303 void updateVREPForGroups(const Group& group);
304
305 void updateWellRates(const Group& group,
306 const WellState<Scalar, IndexTraits>& well_state_nupcol,
307 WellState<Scalar, IndexTraits>& well_state) const;
308
310 {
311 return *this->well_state_;
312 }
313
314 void updateWellRatesFromGroupTargetScale(const Scalar scale,
315 const Group& group,
316 bool is_injector,
317 WellState<Scalar, IndexTraits>& well_state) const;
318
320 std::pair<std::optional<std::string>, Scalar>
321 worstOffendingWell(const Group& group,
322 const Group::ProductionCMode& offended_control,
323 const Parallel::Communication& comm,
324 DeferredLogger& deferred_logger) const;
325
326private:
327 std::string controlGroup_(const Group& group) const;
328
329 GuideRate::RateVector getGuideRateVector_(const std::vector<Scalar>& rates) const;
330
332 bool isInGroupChainTopBot_(const std::string& bottom, const std::string& top) const;
333
334 int phaseToActivePhaseIdx_(const Phase phase) const;
335
336 Scalar satelliteInjectionRate_(const ScheduleState& sched,
337 const Group& group,
338 const int phase_pos,
339 bool res_rates) const;
340
341 Scalar satelliteProductionRate_(const ScheduleState& sched,
342 const Group& group,
343 const GSatProd::GSatProdGroupProp::Rate rate_comp,
344 bool res_rates) const;
345
346 std::optional<GSatProd::GSatProdGroupProp::Rate> selectRateComponent_(const int phase_pos) const;
347
348 int updateGroupControlledWellsRecursive_(const std::string& group_name,
349 const bool is_production_group,
350 const Phase injection_phase,
351 DeferredLogger& deferred_logger);
352
353 void updateGroupTargetReductionRecursive_(const Group& group,
354 const bool is_injector,
355 std::vector<Scalar>& group_target_reduction);
356
357 const WellState<Scalar, IndexTraits>* well_state_ {nullptr};
358 GroupState<Scalar>* group_state_ {nullptr};
359 const Schedule& schedule_;
360 const SummaryState& summary_state_;
361 const GuideRate& guide_rate_;
362 int report_step_ {0};
363 // NOTE: The phase usage info seems to be read-only throughout the simulation, so it should be safe
364 // to store a reference to it here.
365 const PhaseUsageInfo<IndexTraits>& phase_usage_info_;
366#ifdef RESERVOIR_COUPLING_ENABLED
367 ReservoirCouplingMaster<Scalar>* reservoir_coupling_master_{nullptr};
368 ReservoirCouplingSlave<Scalar>* reservoir_coupling_slave_{nullptr};
369#endif
370};
371
372// -----------------------------------------------------------------------------
373// Template member function implementations
374// -----------------------------------------------------------------------------
375
376// NOTE: This template member function is defined here in the header because the
377// AverageRegionalPressureType type parameter depends on derived class types that are
378// not available when GroupStateHelper.cpp is compiled. Template functions
379// must be visible at their instantiation point.
380// See GroupStateHelper.cpp for detailed rationale.
381template <typename Scalar, typename IndexTraits>
382template <class AverageRegionalPressureType>
383void
385 const Group& group,
386 const FieldPropsManager& fp,
387 std::map<std::string, std::unique_ptr<AverageRegionalPressureType>>& regional_average_pressure_calculator)
388 const
389{
390 for (const std::string& groupName : group.groups()) {
391 this->setRegionAveragePressureCalculator(this->schedule_.getGroup(groupName, this->report_step_),
392 fp,
393 regional_average_pressure_calculator);
394 }
395 const auto& gpm = group.gpmaint();
396 if (!gpm)
397 return;
398
399 const auto& reg = gpm->region();
400 if (!reg)
401 return;
402
403 if (regional_average_pressure_calculator.count(reg->first) == 0) {
404 const std::string name = (reg->first.rfind("FIP", 0) == 0) ? reg->first : "FIP" + reg->first;
405 const auto& fipnum = fp.get_int(name);
406 regional_average_pressure_calculator[reg->first]
407 = std::make_unique<AverageRegionalPressureType>(fipnum);
408 }
409}
410
411// NOTE: This template member function is defined in the header because the
412// RegionalValues type parameter depends on derived class types that are
413// not available when GroupStateHelper.cpp is compiled. Template functions
414// must be visible at their instantiation point.
415// See GroupStateHelper.cpp for detailed rationale.
416template <typename Scalar, typename IndexTraits>
417template <class RegionalValues>
418void
420 const RegionalValues& regional_values,
421 const double dt)
422{
423 OPM_TIMEFUNCTION();
424 for (const std::string& group_name : group.groups()) {
425 const Group& group_tmp = this->schedule_.getGroup(group_name, this->report_step_);
426 this->updateGpMaintTargetForGroups(group_tmp, regional_values, dt);
427 }
428 const auto& gpm = group.gpmaint();
429 if (!gpm)
430 return;
431
432 const auto& region = gpm->region();
433 if (!region)
434 return;
435
436 const auto [name, number] = *region;
437 const Scalar error = gpm->pressure_target() - regional_values.at(name)->pressure(number);
438 Scalar current_rate = 0.0;
439 const auto& pu = this->phase_usage_info_;
440 bool injection = true;
441 Scalar sign = 1.0;
442 switch (gpm->flow_target()) {
443 case GPMaint::FlowTarget::RESV_PROD: {
444 current_rate = -this->groupState().injection_vrep_rate(group.name());
445 injection = false;
446 sign = -1.0;
447 break;
448 }
449 case GPMaint::FlowTarget::RESV_OINJ: {
450 if (pu.phaseIsActive(IndexTraits::oilPhaseIdx)) {
451 const auto io = pu.canonicalToActivePhaseIdx(IndexTraits::oilPhaseIdx);
452 current_rate = this->groupState().injection_reservoir_rates(group.name())[io];
453 }
454 break;
455 }
456 case GPMaint::FlowTarget::RESV_WINJ: {
457 if (pu.phaseIsActive(IndexTraits::waterPhaseIdx)) {
458 const auto iw = pu.canonicalToActivePhaseIdx(IndexTraits::waterPhaseIdx);
459 current_rate = this->groupState().injection_reservoir_rates(group.name())[iw];
460 }
461 break;
462 }
463 case GPMaint::FlowTarget::RESV_GINJ: {
464 if (pu.phaseIsActive(IndexTraits::gasPhaseIdx)) {
465 const auto ig = pu.canonicalToActivePhaseIdx(IndexTraits::gasPhaseIdx);
466 current_rate = this->groupState().injection_reservoir_rates(group.name())[ig];
467 }
468 break;
469 }
470 case GPMaint::FlowTarget::SURF_OINJ: {
471 if (pu.phaseIsActive(IndexTraits::oilPhaseIdx)) {
472 const auto io = pu.canonicalToActivePhaseIdx(IndexTraits::oilPhaseIdx);
473 current_rate = this->groupState().injection_surface_rates(group.name())[io];
474 }
475 break;
476 }
477 case GPMaint::FlowTarget::SURF_WINJ: {
478 if (pu.phaseIsActive(IndexTraits::waterPhaseIdx)) {
479 const auto iw = pu.canonicalToActivePhaseIdx(IndexTraits::waterPhaseIdx);
480 current_rate = this->groupState().injection_surface_rates(group.name())[iw];
481 }
482 break;
483 }
484 case GPMaint::FlowTarget::SURF_GINJ: {
485 if (pu.phaseIsActive(IndexTraits::gasPhaseIdx)) {
486 const auto ig = pu.canonicalToActivePhaseIdx(IndexTraits::gasPhaseIdx);
487 current_rate = this->groupState().injection_surface_rates(group.name())[ig];
488 }
489 break;
490 }
491 default:
492 throw std::invalid_argument("Invalid Flow target type in GPMAINT");
493 }
494 auto& gpmaint_state = this->groupState().gpmaint(group.name());
495 // we only activate gpmaint if pressure is lower than the target regional pressure for injectors
496 // (i.e. error > 0) and higher for producers.
497 bool activate = (injection && error > 0) || (!injection && error < 0);
498 Scalar rate = 0.0;
499 if (activate) {
500 rate = gpm->rate(gpmaint_state, current_rate, error, dt);
501 } else {
502 gpm->resetState(gpmaint_state);
503 }
504 this->groupState().update_gpmaint_target(group.name(), std::max(Scalar {0.0}, sign * rate));
505}
506
507} // namespace Opm
508
509#endif // OPM_GROUPSTATE_HELPER_HEADER_INCLUDED
Definition: DeferredLogger.hpp:57
Definition: GroupStateHelper.hpp:92
GroupStateGuard(GroupStateGuard &&)=delete
GroupStateGuard & operator=(GroupStateGuard &&)=delete
GroupStateGuard(GroupStateHelper &group_state_helper, GroupState< Scalar > &group_state)
Definition: GroupStateHelper.hpp:94
GroupStateGuard(const GroupStateGuard &)=delete
~GroupStateGuard()
Definition: GroupStateHelper.hpp:102
GroupStateGuard & operator=(const GroupStateGuard &)=delete
Definition: GroupStateHelper.hpp:63
WellStateGuard(WellStateGuard &&)=delete
WellStateGuard(const WellStateGuard &)=delete
~WellStateGuard()
Definition: GroupStateHelper.hpp:73
WellStateGuard & operator=(const WellStateGuard &)=delete
WellStateGuard & operator=(WellStateGuard &&)=delete
WellStateGuard(GroupStateHelper &groupStateHelper, WellState< Scalar, IndexTraits > &well_state)
Definition: GroupStateHelper.hpp:65
Definition: GroupStateHelper.hpp:59
void updateWellRatesFromGroupTargetScale(const Scalar scale, const Group &group, bool is_injector, WellState< Scalar, IndexTraits > &well_state) const
void setRegionAveragePressureCalculator(const Group &group, const FieldPropsManager &fp, std::map< std::string, std::unique_ptr< AverageRegionalPressureType > > &regional_average_pressure_calculator) const
Definition: GroupStateHelper.hpp:384
void setCmodeGroup(const Group &group)
std::vector< std::string > groupChainTopBot(const std::string &bottom, const std::string &top) const
void updateVREPForGroups(const Group &group)
void updateWellRates(const Group &group, const WellState< Scalar, IndexTraits > &well_state_nupcol, WellState< Scalar, IndexTraits > &well_state) const
std::map< std::string, Scalar > computeNetworkPressures(const Network::ExtNetwork &network, const VFPProdProperties< Scalar > &vfp_prod_props, const Parallel::Communication &comm) const
ReservoirCouplingMaster< Scalar > & reservoirCouplingMaster()
Definition: GroupStateHelper.hpp:223
GroupState< Scalar > & groupState() const
Definition: GroupStateHelper.hpp:183
Scalar sumWellResRates(const Group &group, const int phase_pos, const bool injector) const
void accumulateGroupEfficiencyFactor(const Group &group, Scalar &factor) const
void setReservoirCouplingMaster(ReservoirCouplingMaster< Scalar > *reservoir_coupling_master)
Definition: GroupStateHelper.hpp:249
int groupControlledWells(const std::string &group_name, const std::string &always_included_child, const bool is_production_group, const Phase injection_phase) const
const PhaseUsageInfo< IndexTraits > & phaseUsage() const
Definition: GroupStateHelper.hpp:218
std::pair< bool, Scalar > checkGroupConstraintsProd(const std::string &name, const std::string &parent, const Group &group, const Scalar *rates, const Scalar efficiency_factor, const std::vector< Scalar > &resv_coeff, const bool check_guide_rate, DeferredLogger &deferred_logger) const
const SummaryState & summaryState() const
Definition: GroupStateHelper.hpp:260
const GuideRate & guideRate() const
Definition: GroupStateHelper.hpp:213
void updateReservoirRatesInjectionGroups(const Group &group)
const WellState< Scalar, IndexTraits > & wellState() const
Definition: GroupStateHelper.hpp:309
const PhaseUsageInfo< IndexTraits > & phaseUsageInfo() const
Definition: GroupStateHelper.hpp:188
GuideRate::RateVector getWellRateVector(const std::string &name) const
int updateGroupControlledWells(const bool is_production_group, const Phase injection_phase, DeferredLogger &deferred_logger)
Scalar sumSolventRates(const Group &group, const bool is_injector) const
const Schedule & schedule() const
Definition: GroupStateHelper.hpp:208
void updateGroupTargetReduction(const Group &group, const bool is_injector)
std::optional< Scalar > getWellGroupTargetProducer(const std::string &name, const std::string &parent, const Group &group, const Scalar *rates, const Scalar efficiency_factor, const std::vector< Scalar > &resv_coeff, DeferredLogger &deferred_logger) const
GuideRate::RateVector getProductionGroupRateVector(const std::string &group_name) const
void updateGroupProductionRates(const Group &group)
GroupStateHelper(WellState< Scalar, IndexTraits > &well_state, GroupState< Scalar > &group_state, const Schedule &schedule, const SummaryState &summary_state, const GuideRate &guide_rate, const PhaseUsageInfo< IndexTraits > &phase_usage_info)
void updateGpMaintTargetForGroups(const Group &group, const RegionalValues &regional_values, const double dt)
Definition: GroupStateHelper.hpp:419
Scalar sumWellSurfaceRates(const Group &group, const int phase_pos, const bool injector) const
int reportStepIdx() const
Definition: GroupStateHelper.hpp:203
std::pair< std::optional< std::string >, Scalar > worstOffendingWell(const Group &group, const Group::ProductionCMode &offended_control, const Parallel::Communication &comm, DeferredLogger &deferred_logger) const
Returns the name of the worst offending well and its fraction (i.e. violated_phase / preferred_phase)
Scalar getGuideRate(const std::string &name, const GuideRateModel::Target target) const
void setReportStep(int report_step)
Definition: GroupStateHelper.hpp:243
void updateState(WellState< Scalar, IndexTraits > &well_state, GroupState< Scalar > &group_state)
void updateNetworkLeafNodeProductionRates()
WellStateGuard pushWellState(WellState< Scalar, IndexTraits > &well_state)
Definition: GroupStateHelper.hpp:198
GroupStateGuard pushGroupState(GroupState< Scalar > &group_state)
Definition: GroupStateHelper.hpp:193
ReservoirCouplingSlave< Scalar > & reservoirCouplingSlave()
Definition: GroupStateHelper.hpp:228
std::optional< Scalar > getWellGroupTargetInjector(const std::string &name, const std::string &parent, const Group &group, const Scalar *rates, const Phase injection_phase, const Scalar efficiency_factor, const std::vector< Scalar > &resv_coeff, DeferredLogger &deferred_logger) const
Scalar sumWellPhaseRates(bool res_rates, const Group &group, const int phase_pos, const bool injector, const bool network=false) const
void updateREINForGroups(const Group &group, bool sum_rank)
std::pair< bool, Scalar > checkGroupConstraintsInj(const std::string &name, const std::string &parent, const Group &group, const Scalar *rates, const Phase injection_phase, const Scalar efficiency_factor, const std::vector< Scalar > &resv_coeff, const bool check_guide_rate, DeferredLogger &deferred_logger) const
void setReservoirCouplingSlave(ReservoirCouplingSlave< Scalar > *reservoir_coupling_slave)
Definition: GroupStateHelper.hpp:254
void updateSurfaceRatesInjectionGroups(const Group &group)
Definition: GroupState.hpp:41
Definition: GasLiftGroupInfo.hpp:37
Definition: ReservoirCouplingMaster.hpp:38
Definition: ReservoirCouplingSlave.hpp:40
Definition: VFPProdProperties.hpp:38
Definition: WellState.hpp:66
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:141
Definition: blackoilbioeffectsmodules.hh:43