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
23
24#include <opm/common/TimingMacros.hpp>
25#include <opm/input/eclipse/Schedule/ResCoup/GrupSlav.hpp>
26#include <opm/input/eclipse/EclipseState/Grid/FieldPropsManager.hpp>
27#include <opm/input/eclipse/Schedule/Group/GPMaint.hpp>
28#include <opm/input/eclipse/Schedule/Group/GSatProd.hpp>
29#include <opm/input/eclipse/Schedule/Group/GuideRate.hpp>
30#include <opm/input/eclipse/Schedule/Schedule.hpp>
31#include <opm/input/eclipse/Schedule/ScheduleState.hpp>
32#include <opm/input/eclipse/Schedule/SummaryState.hpp>
33#include <opm/material/fluidsystems/PhaseUsageInfo.hpp>
40
42
43#include <algorithm>
44#include <map>
45#include <memory>
46#include <optional>
47#include <stdexcept>
48#include <string>
49#include <unordered_set>
50#include <utility>
51#include <vector>
52
53namespace Opm
54{
55template <typename Scalar, typename IndexTraits>
57{
58public:
59 // RAII guard for temporarily setting wellstate pointer
61 {
62 public:
64 : groupStateHelper_ {groupStateHelper}
65 , previous_state_ptr_ {groupStateHelper_.well_state_}
66 {
67 // Set the new state directly
68 groupStateHelper_.well_state_ = &well_state;
69 }
70
72 {
73 // Restore the previous state
74 groupStateHelper_.well_state_ = previous_state_ptr_;
75 }
76
77 // Delete copy and move operations
82
83 private:
84 GroupStateHelper& groupStateHelper_;
85 const WellState<Scalar, IndexTraits>* previous_state_ptr_;
86 };
87
88 // RAII guard for temporarily setting groupstate pointer
90 {
91 public:
92 GroupStateGuard(GroupStateHelper& group_state_helper, GroupState<Scalar>& group_state)
93 : group_state_helper_ {group_state_helper}
94 , previous_state_ptr_ {group_state_helper.group_state_}
95 {
96 // Set the new state directly
97 group_state_helper_.group_state_ = &group_state;
98 }
99
101 {
102 // Restore the previous state
103 group_state_helper_.group_state_ = previous_state_ptr_;
104 }
105
106 // Delete copy and move operations
111
112 private:
113 GroupStateHelper& group_state_helper_;
114 GroupState<Scalar>* previous_state_ptr_ {nullptr};
115 };
116
130 {
131 public:
137 explicit ScopedLoggerGuard(const GroupStateHelper& helper, bool do_mpi_gather = true)
138 : helper_(&helper)
139 , previous_(helper.deferred_logger_)
140 , do_mpi_gather_(do_mpi_gather)
141 {
142 helper_->deferred_logger_ = &logger_;
143 }
144
146 {
147 if (helper_) {
148 if (do_mpi_gather_) {
149 // 1. Gather messages across MPI ranks
150 DeferredLogger global = gatherDeferredLogger(logger_, helper_->comm());
151
152 // 2. Log on rank 0 (if terminal_output enabled)
153 if (helper_->terminalOutput()) {
154 global.logMessages();
155 }
156 } else {
157 // Just log locally without MPI gather
158 if (helper_->terminalOutput()) {
159 logger_.logMessages();
160 }
161 }
162
163 // 3. Restore previous logger
164 helper_->deferred_logger_ = previous_;
165 }
166 }
167
168 // Delete copy operations and move assignment
172
173 // Move constructor required for pushLogger() return-by-value (must be
174 // available even if elided by RVO)
176 : helper_(other.helper_)
177 , logger_(std::move(other.logger_))
178 , previous_(other.previous_)
179 , do_mpi_gather_(other.do_mpi_gather_)
180 {
181 // Update the helper's pointer to our moved logger
182 if (helper_) {
183 helper_->deferred_logger_ = &logger_;
184 }
185 other.helper_ = nullptr;
186 other.previous_ = nullptr;
187 }
188
189 private:
190 // Pointer (not reference) to allow nulling in move constructor
191 const GroupStateHelper* helper_{nullptr};
192 DeferredLogger logger_; // Owned logger
193 DeferredLogger* previous_{nullptr}; // For restore
194 bool do_mpi_gather_{true}; // Whether to gather messages across MPI ranks
195 };
196
198
200 GroupState<Scalar>& group_state,
201 const Schedule& schedule,
202 const SummaryState& summary_state,
203 const GuideRate& guide_rate,
204 const PhaseUsageInfo<IndexTraits>& phase_usage_info,
206 bool terminal_output);
207
208 void accumulateGroupEfficiencyFactor(const Group& group, Scalar& factor) const;
209
233 std::pair<bool, Scalar> checkGroupConstraintsInj(const std::string& name,
234 const std::string& parent,
235 const Group& group,
236 const Scalar* rates,
237 const Phase injection_phase,
238 const Scalar efficiency_factor,
239 const std::vector<Scalar>& resv_coeff,
240 const bool check_guide_rate,
241 const bool assume_fld_control = false) const;
242
243 std::pair<bool, Scalar> checkGroupConstraintsProd(const std::string& name,
244 const std::string& parent,
245 const Group& group,
246 const Scalar* rates,
247 const Scalar efficiency_factor,
248 const std::vector<Scalar>& resv_coeff,
249 const bool check_guide_rate) const;
250
251 std::pair<Group::ProductionCMode, Scalar>
252 checkGroupProductionConstraints(const Group& group) const;
253
254 const Parallel::Communication& comm() const { return this->comm_; }
255
259 {
260 if (this->deferred_logger_ == nullptr) {
261 throw std::logic_error("DeferredLogger not set. Call pushLogger() first.");
262 }
263 return *this->deferred_logger_;
264 }
265
270 std::pair<std::optional<Scalar>, std::optional<Scalar>>
271 getGasLiftGroupLimits(const Group& group) const;
272
273 std::vector<Scalar> getGroupRatesAvailableForHigherLevelControl(const Group& group, const bool is_injector) const;
274
275 Scalar getInjectionGroupTarget(const Group& group,
276 const Phase& injection_phase,
277 const std::vector<Scalar>& resv_coeff) const;
278
282 GuideRateModel::Target getInjectionGuideTargetMode(Phase injection_phase) const;
283
284 Scalar getProductionGroupTarget(const Group& group) const;
285
287 Scalar getProductionGroupTargetForMode(const Group& group,
288 const Group::ProductionCMode cmode) const;
289
299 std::optional<Scalar> getProductionGroupRateLimit(const Group& group,
300 const Group::ProductionCMode rate_type) const;
301
305 GuideRateModel::Target getProductionGuideTargetMode(const Group& group) const;
306
307 GuideRateModel::Target
308 getProductionGuideTargetModeFromControlMode(const Group::ProductionCMode cmode) const;
309
310 std::pair<Scalar, Group::ProductionCMode>
311 getAutoChokeGroupProductionTargetRate(const Group& bottom_group,
312 const Group& group,
313 const std::vector<Scalar>& resv_coeff,
314 Scalar efficiencyFactor) const;
315
316 GuideRate::RateVector getProductionGroupRateVector(const std::string& group_name) const;
317
318 std::optional<GroupTarget> getWellGroupTargetInjector(const std::string& name,
319 const std::string& parent,
320 const Group& group,
321 const Scalar* rates,
322 const Phase injection_phase,
323 const Scalar efficiency_factor,
324 const std::vector<Scalar>& resv_coeff) const;
325
326 std::optional<GroupTarget> getWellGroupTargetProducer(const std::string& name,
327 const std::string& parent,
328 const Group& group,
329 const Scalar* rates,
330 const Scalar efficiency_factor,
331 const std::vector<Scalar>& resv_coeff,
332 std::optional<Group::ProductionCMode> target_cmode = std::nullopt) const;
333
334 GuideRate::RateVector getWellRateVector(const std::string& name) const;
335
336 std::vector<std::string> groupChainTopBot(const std::string& bottom, const std::string& top) const;
337
340 int groupControlledWells(const std::string& group_name,
341 const std::string& always_included_child,
342 const bool is_production_group,
343 const Phase injection_phase) const;
344
346 {
347 return *this->group_state_;
348 }
349
350 const GuideRate& guideRate() const
351 {
352 return this->guide_rate_;
353 }
354
364 bool isMasterGroupEligibleForGuideRate(const std::string& group_name) const;
365
375 bool isMasterGroupEligibleForInjectionGuideRate(const std::string& group_name,
376 Phase injection_phase) const;
377
378 bool isRank0() const
379 {
380 return this->well_state_->isRank0();
381 }
382
383 bool isReservoirCouplingMaster() const { return rescoup_.isMaster(); }
384
385 bool isReservoirCouplingMasterGroup(const Group& group) const { return rescoup_.isMasterGroup(group.name()); }
386
387 bool isReservoirCouplingSlave() const { return rescoup_.isSlave(); }
388
389 bool isReservoirCouplingSlaveGroup(const Group& group) const { return rescoup_.isSlaveGroup(group.name()); }
390
391 constexpr int numPhases() const {
392 return this->wellState().numPhases();
393 }
394
395 int phaseToActivePhaseIdx(const Phase phase) const;
396
400 Scalar reservoirCouplingMasterGroupGasLiftRate(const Group& group, const bool network) const;
401
403 return this->phase_usage_info_;
404 }
405
407 {
408 return GroupStateGuard(*this, group_state);
409 }
410
424 ScopedLoggerGuard pushLogger(bool do_mpi_gather = true) const
425 {
426 return ScopedLoggerGuard(*this, do_mpi_gather);
427 }
428
430 {
431 return WellStateGuard(*this, well_state);
432 }
433
434 int reportStepIdx() const
435 {
436 return report_step_;
437 }
438
439 const Schedule& schedule() const
440 {
441 return this->schedule_;
442 }
443
445 return rescoup_;
446 }
447
449 return rescoup_;
450 }
451
453 return rescoup_.master();
454 }
455
457 return rescoup_.master();
458 }
459
461 return rescoup_.slave();
462 }
464 return rescoup_.slave();
465 }
466
467#ifdef RESERVOIR_COUPLING_ENABLED
468 void setReservoirCouplingMaster(ReservoirCouplingMaster<Scalar>* master) {
469 rescoup_.setMaster(master);
470 }
471 void setReservoirCouplingSlave(ReservoirCouplingSlave<Scalar>* slave) {
472 rescoup_.setSlave(slave);
473 }
474#endif
475
476 void setCmodeGroup(const Group& group);
477
478 template <class AverageRegionalPressureType>
479 void
481 const FieldPropsManager& fp,
482 std::map<std::string, std::unique_ptr<AverageRegionalPressureType>>&
483 regional_average_pressure_calculator) const;
484
485 void setReportStep(int report_step)
486 {
487 report_step_ = report_step;
488 }
489
490 const SummaryState& summaryState() const
491 {
492 return this->summary_state_;
493 }
494
495 Scalar sumSolventRates(const Group& group, const bool is_injector) const;
496
497 Scalar sumWellResRates(const Group& group, const int phase_pos, const bool injector) const;
498
499 Scalar sumWellSurfaceRates(const Group& group, const int phase_pos, const bool injector) const;
500
507 Scalar sumWellGasLiftHeadroom(const Group& group) const;
508
509 Scalar sumWellPhaseRates(bool res_rates,
510 const Group& group,
511 const int phase_pos,
512 const bool injector,
513 const bool network = false) const;
514
523 Scalar sumWellGasLiftRates(const Group& group, const bool network = false) const;
524
525 bool terminalOutput() const
526 {
527 return this->terminal_output_;
528 }
529
530 template <class RegionalValues>
531 void updateGpMaintTargetForGroups(const Group& group,
532 const RegionalValues& regional_values,
533 const double dt);
534
537 int updateGroupControlledWells(const bool is_production_group,
538 const Phase injection_phase);
539
540 void updateGroupProductionRates(const Group& group);
541
542 void updatePreviousGroupProductionRates(const Group& group);
543
544 void updateGroupTargetReduction(const Group& group,
545 const bool is_injector);
546
548
561
562 void updateREINForGroups(const Group& group, bool sum_rank);
563
564 void updateReservoirRatesInjectionGroups(const Group& group);
565
566#ifdef RESERVOIR_COUPLING_ENABLED
576 void updateSlaveGroupCmodesFromMaster();
577#endif
578
580
581 void updateSurfaceRatesInjectionGroups(const Group& group);
582
583 void updateVREPForGroups(const Group& group);
584
585 void updateWellRates(const Group& group,
586 const WellState<Scalar, IndexTraits>& well_state_nupcol,
587 WellState<Scalar, IndexTraits>& well_state) const;
588
590 {
591 return *this->well_state_;
592 }
593
594 void updateWellRatesFromGroupTargetScale(const Scalar scale,
595 const Group& group,
596 bool is_injector,
597 WellState<Scalar, IndexTraits>& well_state) const;
598
617 Scalar wellRateContributionToGroup(const std::string& well_name,
618 const int phase_pos,
619 const bool res_rates,
620 const bool is_injector,
621 const bool network = false,
622 const WellState<Scalar, IndexTraits>* well_state = nullptr) const;
623
625 std::pair<std::optional<std::string>, Scalar>
626 worstOffendingWell(const Group& group,
627 const Group::ProductionCMode& offended_control) const;
628
629private:
630
652 template<typename ReductionLambda, typename FractionLambda>
653 Scalar applyReductionsAndFractions_(const std::vector<std::string>& chain,
654 Scalar orig_target,
655 Scalar current_rate_available,
656 std::size_t local_reduction_level,
657 bool is_production_group,
658 Phase injection_phase,
659 ReductionLambda&& local_reduction_lambda,
660 FractionLambda&& local_fraction_lambda,
661 bool do_addback) const;
662
663 std::pair<Group::ProductionCMode, Scalar>
664 checkProductionRateConstraint_(const Group& group,
665 Group::ProductionCMode cmode,
666 Group::ProductionCMode currentControl,
667 Scalar target,
668 Scalar current_rate) const;
669
677 std::unordered_set<std::string> collectTargetedProductionGroups_() const;
678
689 Scalar computeAddbackEfficiency_(const std::vector<std::string>& chain,
690 std::size_t local_reduction_level) const;
691
692 std::string controlGroup_(const Group& group) const;
693
694 GuideRate::RateVector getGuideRateVector_(const std::vector<Scalar>& rates) const;
695
696 Scalar getInjectionGroupTargetForMode_(const Group& group,
697 const Phase& injection_phase,
698 const std::vector<Scalar>& resv_coeff,
699 const Group::InjectionCMode cmode) const;
700
711 std::size_t getLocalReductionLevel_(const std::vector<std::string>& chain,
712 bool is_production_group,
713 Phase injection_phase) const;
714
715 Scalar getProductionConstraintTarget_(const Group& group,
716 Group::ProductionCMode cmode,
717 const Group::ProductionControls& controls) const;
718
719 Scalar getProductionGroupTargetForMode_(const Group& group, const Group::ProductionCMode cmode) const;
720
721 Scalar getSatelliteRate_(const Group& group,
722 const int phase_pos,
723 const bool res_rates,
724 const bool is_injector) const;
725
729 bool isAutoChokeGroupUnderperforming_(const Group& group) const;
730
732 bool isInGroupChainTopBot_(const std::string& bottom, const std::string& top) const;
733
734 bool isSatelliteGroup_(const Group& group) const;
735
736 Scalar satelliteInjectionRate_(const ScheduleState& sched,
737 const Group& group,
738 const int phase_pos,
739 bool res_rates) const;
740
741 Scalar satelliteProductionRate_(const ScheduleState& sched,
742 const Group& group,
743 const GSatProd::Rate rate_comp,
744 bool res_rates) const;
745
746 std::optional<GSatProd::Rate> selectRateComponent_(const int phase_pos) const;
747
758 Scalar subtractOtherPhaseResvInjection_(
759 Phase injection_phase,
760 Scalar base_reservoir_rate,
761 const std::vector<Scalar>& group_injection_reservoir_rates) const;
762
767 template <class WellValue, class MasterValue>
768 Scalar sumOverGasLiftWells_(const Group& group,
769 const bool network,
770 const WellValue& value,
771 const MasterValue& master_value) const;
772
773 Scalar sumProductionRateForControlMode_(const Group& group, Group::ProductionCMode cmode) const;
774
775 int updateGroupControlledWellsRecursive_(const std::string& group_name,
776 const bool is_production_group,
777 const Phase injection_phase);
778
779 void updateGroupTargetReductionRecursive_(const Group& group,
780 const bool is_injector,
781 std::vector<Scalar>& group_target_reduction);
782
783 // Validate that GCONINJE top-up phases (RESV/VREP) are consistent
784 // across the group hierarchy. Called once from setCmodeGroup() for FIELD.
785 void validateInjectionTopupPhases_(const Group& group) const;
786
787 // Recursive helper for validateInjectionTopupPhases_()
788 void validateInjectionTopupPhasesRecursive_(const Group& group,
789 std::optional<Phase> inherited_topup_phase,
790 const std::string& inherited_topup_group) const;
791
792 // --- Reservoir coupling private methods ---
793#ifdef RESERVOIR_COUPLING_ENABLED
798 ReservoirCoupling::Phase activePhaseIdxToRescoupPhase_(int phase_pos) const;
799
808 std::unordered_set<std::string> collectMasterGroupHierarchy_() const;
809
835 int getMasterGroupEffectiveGCW_(const std::string& group_name,
836 bool is_production_group,
837 Phase injection_phase) const;
838
852 Scalar getEffectiveProductionLimit_(const std::string& gname,
853 Group::ProductionCMode rate_type,
854 Scalar slave_local_target) const;
855
856 ReservoirCoupling::GrupSlav::FilterFlag getInjectionFilterFlag_(const std::string& group_name,
857 const Phase injection_phase) const;
858
859 ReservoirCoupling::GrupSlav::FilterFlag getProductionFilterFlag_(const std::string& group_name,
860 const Group::ProductionCMode cmode) const;
861
862 Scalar getReservoirCouplingMasterGroupRate_(const Group& group,
863 const int phase_pos,
864 const ReservoirCoupling::RateKind kind) const;
865#endif // RESERVOIR_COUPLING_ENABLED
866
867 const WellState<Scalar, IndexTraits>* well_state_ {nullptr};
868 GroupState<Scalar>* group_state_ {nullptr};
869 const Schedule& schedule_;
870 const SummaryState& summary_state_;
871 const GuideRate& guide_rate_;
872 // NOTE: The deferred logger does not change the object "meaningful" state, so it should be ok to
873 // make it mutable and store a pointer to it here.
874 mutable DeferredLogger* deferred_logger_ {nullptr};
875 // NOTE: The phase usage info seems to be read-only throughout the simulation, so it should be safe
876 // to store a reference to it here.
877 const PhaseUsageInfo<IndexTraits>& phase_usage_info_;
878 const Parallel::Communication& comm_;
879 bool terminal_output_ {false};
880 int report_step_ {0};
881 ReservoirCoupling::Proxy<Scalar> rescoup_{};
882};
883
884// -----------------------------------------------------------------------------
885// Template member function implementations
886// -----------------------------------------------------------------------------
887
888// NOTE: This template member function is defined here in the header because the
889// AverageRegionalPressureType type parameter depends on derived class types that are
890// not available when GroupStateHelper.cpp is compiled. Template functions
891// must be visible at their instantiation point.
892// See GroupStateHelper.cpp for detailed rationale.
893template <typename Scalar, typename IndexTraits>
894template <class AverageRegionalPressureType>
895void
897 const Group& group,
898 const FieldPropsManager& fp,
899 std::map<std::string, std::unique_ptr<AverageRegionalPressureType>>& regional_average_pressure_calculator)
900 const
901{
902 for (const std::string& groupName : group.groups()) {
903 this->setRegionAveragePressureCalculator(this->schedule_.getGroup(groupName, this->report_step_),
904 fp,
905 regional_average_pressure_calculator);
906 }
907 const auto& gpm = group.gpmaint();
908 if (!gpm)
909 return;
910
911 const auto& reg = gpm->region();
912 if (!reg)
913 return;
914
915 if (regional_average_pressure_calculator.count(reg->first) == 0) {
916 const std::string name = (reg->first.rfind("FIP", 0) == 0) ? reg->first : "FIP" + reg->first;
917 const auto& fipnum = fp.get_int(name);
918 regional_average_pressure_calculator[reg->first]
919 = std::make_unique<AverageRegionalPressureType>(fipnum);
920 }
921}
922
923// NOTE: This template member function is defined in the header because the
924// RegionalValues type parameter depends on derived class types that are
925// not available when GroupStateHelper.cpp is compiled. Template functions
926// must be visible at their instantiation point.
927// See GroupStateHelper.cpp for detailed rationale.
928template <typename Scalar, typename IndexTraits>
929template <class RegionalValues>
930void
932 const RegionalValues& regional_values,
933 const double dt)
934{
935 OPM_TIMEFUNCTION();
936 for (const std::string& group_name : group.groups()) {
937 const Group& group_tmp = this->schedule_.getGroup(group_name, this->report_step_);
938 this->updateGpMaintTargetForGroups(group_tmp, regional_values, dt);
939 }
940 const auto& gpm = group.gpmaint();
941 if (!gpm)
942 return;
943
944 const auto& region = gpm->region();
945 if (!region)
946 return;
947 if (this->isReservoirCouplingMasterGroup(group)) {
948 // GPMAINT is not supported for reservoir coupling master groups since master groups do not have
949 // subordinate wells in the master reservoir, so the slaves cannot influence the master reservoir's
950 // average pressure.
951 // Even specifying GPMAINT on a group superior to the master group might not make sense, since if the
952 // superior target is distributed down to the master group with guide rate fractions, adjusting
953 // the master group's target (that is sent to the slave) could only indirectly influence the master
954 // reservoir's average pressure by affecting the guide rate fractions distributed to actual wells
955 // in the master reservoir.
957 std::runtime_error,
958 "GPMAINT is not supported for reservoir coupling master groups.",
959 this->deferredLogger()
960 );
961 return;
962 }
963 else if (this->isReservoirCouplingSlaveGroup(group)) {
964 // GPMAINT is not supported for reservoir coupling slave groups since their targets will be overridden
965 // by the corresponding master group's target anyway.
967 std::runtime_error,
968 "GPMAINT is not supported for reservoir coupling slave groups.",
969 this->deferredLogger()
970 );
971 return;
972 }
973 const auto [name, number] = *region;
974 const Scalar error = gpm->pressure_target() - regional_values.at(name)->pressure(number);
975 Scalar current_rate = 0.0;
976 const auto& pu = this->phase_usage_info_;
977 bool injection = true;
978 Scalar sign = 1.0;
979 switch (gpm->flow_target()) {
980 case GPMaint::FlowTarget::RESV_PROD: {
981 current_rate = -this->groupState().injection_vrep_rate(group.name());
982 injection = false;
983 sign = -1.0;
984 break;
985 }
986 case GPMaint::FlowTarget::RESV_OINJ: {
987 if (pu.phaseIsActive(IndexTraits::oilPhaseIdx)) {
988 const auto io = pu.canonicalToActivePhaseIdx(IndexTraits::oilPhaseIdx);
989 current_rate = this->groupState().injection_reservoir_rates(group.name())[io];
990 }
991 break;
992 }
993 case GPMaint::FlowTarget::RESV_WINJ: {
994 if (pu.phaseIsActive(IndexTraits::waterPhaseIdx)) {
995 const auto iw = pu.canonicalToActivePhaseIdx(IndexTraits::waterPhaseIdx);
996 current_rate = this->groupState().injection_reservoir_rates(group.name())[iw];
997 }
998 break;
999 }
1000 case GPMaint::FlowTarget::RESV_GINJ: {
1001 if (pu.phaseIsActive(IndexTraits::gasPhaseIdx)) {
1002 const auto ig = pu.canonicalToActivePhaseIdx(IndexTraits::gasPhaseIdx);
1003 current_rate = this->groupState().injection_reservoir_rates(group.name())[ig];
1004 }
1005 break;
1006 }
1007 case GPMaint::FlowTarget::SURF_OINJ: {
1008 if (pu.phaseIsActive(IndexTraits::oilPhaseIdx)) {
1009 const auto io = pu.canonicalToActivePhaseIdx(IndexTraits::oilPhaseIdx);
1010 current_rate = this->groupState().injection_surface_rates(group.name())[io];
1011 }
1012 break;
1013 }
1014 case GPMaint::FlowTarget::SURF_WINJ: {
1015 if (pu.phaseIsActive(IndexTraits::waterPhaseIdx)) {
1016 const auto iw = pu.canonicalToActivePhaseIdx(IndexTraits::waterPhaseIdx);
1017 current_rate = this->groupState().injection_surface_rates(group.name())[iw];
1018 }
1019 break;
1020 }
1021 case GPMaint::FlowTarget::SURF_GINJ: {
1022 if (pu.phaseIsActive(IndexTraits::gasPhaseIdx)) {
1023 const auto ig = pu.canonicalToActivePhaseIdx(IndexTraits::gasPhaseIdx);
1024 current_rate = this->groupState().injection_surface_rates(group.name())[ig];
1025 }
1026 break;
1027 }
1028 default:
1029 throw std::invalid_argument("Invalid Flow target type in GPMAINT");
1030 }
1031 auto& gpmaint_state = this->groupState().gpmaint(group.name());
1032 // we only activate gpmaint if pressure is lower than the target regional pressure for injectors
1033 // (i.e. error > 0) and higher for producers.
1034 bool activate = (injection && error > 0) || (!injection && error < 0);
1035 Scalar rate = 0.0;
1036 if (activate) {
1037 rate = gpm->rate(gpmaint_state, current_rate, error, dt);
1038 } else {
1039 gpm->resetState(gpmaint_state);
1040 }
1041 this->groupState().update_gpmaint_target(group.name(), std::max(Scalar {0.0}, sign * rate));
1042}
1043
1044} // namespace Opm
1045
1046#endif // OPM_GROUPSTATE_HELPER_HEADER_INCLUDED
#define OPM_DEFLOG_THROW(Exception, message, deferred_logger)
Definition: DeferredLoggingErrorHelpers.hpp:47
Definition: DeferredLogger.hpp:57
Definition: GroupStateHelper.hpp:90
GroupStateGuard(GroupStateGuard &&)=delete
GroupStateGuard & operator=(GroupStateGuard &&)=delete
GroupStateGuard(GroupStateHelper &group_state_helper, GroupState< Scalar > &group_state)
Definition: GroupStateHelper.hpp:92
GroupStateGuard(const GroupStateGuard &)=delete
~GroupStateGuard()
Definition: GroupStateHelper.hpp:100
GroupStateGuard & operator=(const GroupStateGuard &)=delete
RAII guard that owns a DeferredLogger and auto-gathers on destruction.
Definition: GroupStateHelper.hpp:130
ScopedLoggerGuard & operator=(const ScopedLoggerGuard &)=delete
ScopedLoggerGuard(ScopedLoggerGuard &&other) noexcept
Definition: GroupStateHelper.hpp:175
ScopedLoggerGuard(const ScopedLoggerGuard &)=delete
ScopedLoggerGuard(const GroupStateHelper &helper, bool do_mpi_gather=true)
Constructor for scoped logger guard.
Definition: GroupStateHelper.hpp:137
~ScopedLoggerGuard()
Definition: GroupStateHelper.hpp:145
ScopedLoggerGuard & operator=(ScopedLoggerGuard &&)=delete
Definition: GroupStateHelper.hpp:61
WellStateGuard(WellStateGuard &&)=delete
WellStateGuard(const WellStateGuard &)=delete
~WellStateGuard()
Definition: GroupStateHelper.hpp:71
WellStateGuard & operator=(const WellStateGuard &)=delete
WellStateGuard & operator=(WellStateGuard &&)=delete
WellStateGuard(GroupStateHelper &groupStateHelper, WellState< Scalar, IndexTraits > &well_state)
Definition: GroupStateHelper.hpp:63
Definition: GroupStateHelper.hpp:57
std::optional< Scalar > getProductionGroupRateLimit(const Group &group, const Group::ProductionCMode rate_type) const
Get the limit on one production rate type of a group, if any.
void updateWellRatesFromGroupTargetScale(const Scalar scale, const Group &group, bool is_injector, WellState< Scalar, IndexTraits > &well_state) const
std::pair< Scalar, Group::ProductionCMode > getAutoChokeGroupProductionTargetRate(const Group &bottom_group, const Group &group, const std::vector< Scalar > &resv_coeff, Scalar efficiencyFactor) const
GuideRateModel::Target getProductionGuideTargetModeFromControlMode(const Group::ProductionCMode cmode) const
Scalar wellRateContributionToGroup(const std::string &well_name, const int phase_pos, const bool res_rates, const bool is_injector, const bool network=false, const WellState< Scalar, IndexTraits > *well_state=nullptr) const
The contribution of a single well to a group rate sum.
void setRegionAveragePressureCalculator(const Group &group, const FieldPropsManager &fp, std::map< std::string, std::unique_ptr< AverageRegionalPressureType > > &regional_average_pressure_calculator) const
Definition: GroupStateHelper.hpp:896
void setCmodeGroup(const Group &group)
Scalar reservoirCouplingMasterGroupGasLiftRate(const Group &group, const bool network) const
Lift gas rate the slave reported for a reservoir coupling master 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
ReservoirCouplingMaster< Scalar > & reservoirCouplingMaster()
Definition: GroupStateHelper.hpp:452
GroupState< Scalar > & groupState() const
Definition: GroupStateHelper.hpp:345
Scalar sumWellResRates(const Group &group, const int phase_pos, const bool injector) const
void accumulateGroupEfficiencyFactor(const Group &group, Scalar &factor) const
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:402
const ReservoirCouplingMaster< Scalar > & reservoirCouplingMaster() const
Definition: GroupStateHelper.hpp:456
const SummaryState & summaryState() const
Definition: GroupStateHelper.hpp:490
const GuideRate & guideRate() const
Definition: GroupStateHelper.hpp:350
std::pair< std::optional< std::string >, Scalar > worstOffendingWell(const Group &group, const Group::ProductionCMode &offended_control) const
Returns the name of the worst offending well and its fraction (i.e. violated_phase / preferred_phase)
ReservoirCoupling::Proxy< Scalar > & rescoup()
Definition: GroupStateHelper.hpp:444
std::pair< std::optional< Scalar >, std::optional< Scalar > > getGasLiftGroupLimits(const Group &group) const
Gas lift limits of a group: maximum lift gas and maximum total gas.
void updateReservoirRatesInjectionGroups(const Group &group)
void updatePreviousGroupProductionRates(const Group &group)
bool isReservoirCouplingSlaveGroup(const Group &group) const
Definition: GroupStateHelper.hpp:389
const WellState< Scalar, IndexTraits > & wellState() const
Definition: GroupStateHelper.hpp:589
constexpr int numPhases() const
Definition: GroupStateHelper.hpp:391
GuideRate::RateVector getWellRateVector(const std::string &name) const
ScopedLoggerGuard pushLogger(bool do_mpi_gather=true) const
Push a new logger onto the stack with auto-cleanup on destruction.
Definition: GroupStateHelper.hpp:424
const Parallel::Communication & comm() const
Definition: GroupStateHelper.hpp:254
GuideRateModel::Target getInjectionGuideTargetMode(Phase injection_phase) const
Get the guide rate target mode for an injection phase.
bool isRank0() const
Definition: GroupStateHelper.hpp:378
std::pair< Group::ProductionCMode, Scalar > checkGroupProductionConstraints(const Group &group) const
Scalar sumSolventRates(const Group &group, const bool is_injector) const
const Schedule & schedule() const
Definition: GroupStateHelper.hpp:439
void updateGroupTargetReduction(const Group &group, const bool is_injector)
bool isMasterGroupEligibleForInjectionGuideRate(const std::string &group_name, Phase injection_phase) const
Whether a reservoir-coupling master group is eligible to participate in its parent's injection guide-...
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, const bool assume_fld_control=false) const
Check whether a well or group violates an injection constraint of an ancestor group.
Scalar sumWellGasLiftHeadroom(const Group &group) const
Sum how much more lift gas the gas lift wells below a group can take.
GuideRate::RateVector getProductionGroupRateVector(const std::string &group_name) const
void updateGroupProductionRates(const Group &group)
bool terminalOutput() const
Definition: GroupStateHelper.hpp:525
typename SingleWellState< Scalar, IndexTraits >::GroupTarget GroupTarget
Definition: GroupStateHelper.hpp:197
bool isMasterGroupEligibleForGuideRate(const std::string &group_name) const
Whether a reservoir-coupling master group is eligible to participate in its parent's guide-rate distr...
void updateGpMaintTargetForGroups(const Group &group, const RegionalValues &regional_values, const double dt)
Definition: GroupStateHelper.hpp:931
int phaseToActivePhaseIdx(const Phase phase) const
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) const
DeferredLogger & deferredLogger() const
Get the deferred logger.
Definition: GroupStateHelper.hpp:258
Scalar sumWellSurfaceRates(const Group &group, const int phase_pos, const bool injector) const
void updateNetworkLeafNodeRates()
int reportStepIdx() const
Definition: GroupStateHelper.hpp:434
Scalar sumWellGasLiftRates(const Group &group, const bool network=false) const
Sum the lift gas rates (ALQ) of the open producers below a group.
void setReportStep(int report_step)
Definition: GroupStateHelper.hpp:485
int updateGroupControlledWells(const bool is_production_group, const Phase injection_phase)
void updateState(WellState< Scalar, IndexTraits > &well_state, GroupState< Scalar > &group_state)
std::vector< Scalar > getGroupRatesAvailableForHigherLevelControl(const Group &group, const bool is_injector) const
bool isReservoirCouplingMasterGroup(const Group &group) const
Definition: GroupStateHelper.hpp:385
const ReservoirCoupling::Proxy< Scalar > & rescoup() const
Definition: GroupStateHelper.hpp:448
bool isReservoirCouplingMaster() const
Definition: GroupStateHelper.hpp:383
const ReservoirCouplingSlave< Scalar > & reservoirCouplingSlave() const
Definition: GroupStateHelper.hpp:463
Scalar getProductionGroupTargetForMode(const Group &group, const Group::ProductionCMode cmode) const
Get the production target for a specific control mode (not necessarily the active one).
WellStateGuard pushWellState(WellState< Scalar, IndexTraits > &well_state)
Definition: GroupStateHelper.hpp:429
Scalar getInjectionGroupTarget(const Group &group, const Phase &injection_phase, const std::vector< Scalar > &resv_coeff) const
GroupStateGuard pushGroupState(GroupState< Scalar > &group_state)
Definition: GroupStateHelper.hpp:406
ReservoirCouplingSlave< Scalar > & reservoirCouplingSlave()
Definition: GroupStateHelper.hpp:460
Scalar sumWellPhaseRates(bool res_rates, const Group &group, const int phase_pos, const bool injector, const bool network=false) const
std::optional< GroupTarget > 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, std::optional< Group::ProductionCMode > target_cmode=std::nullopt) const
void updateREINForGroups(const Group &group, bool sum_rank)
bool isReservoirCouplingSlave() const
Definition: GroupStateHelper.hpp:387
GuideRateModel::Target getProductionGuideTargetMode(const Group &group) const
Get the guide rate target mode for a production group.
void updateNONEProductionGroups()
Set production control to NONE for groups not targeting any well.
Scalar getProductionGroupTarget(const Group &group) const
std::optional< GroupTarget > 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) const
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, const Parallel::Communication &comm, bool terminal_output)
void updateSurfaceRatesInjectionGroups(const Group &group)
Definition: GroupState.hpp:42
Definition: GasLiftGroupInfo.hpp:40
Thin proxy for reservoir coupling master/slave pointers.
Definition: RescoupProxy.hpp:54
Definition: ReservoirCouplingMaster.hpp:43
Definition: ReservoirCouplingSlave.hpp:40
Definition: WellState.hpp:68
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
RateKind
Selects which kind of rate to retrieve from slave group data.
Definition: ReservoirCoupling.hpp:196
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:184
Definition: blackoilbioeffectsmodules.hh:45
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Parallel::Communication communicator)
Create a global log combining local logs.
Definition: SingleWellState.hpp:153