ReservoirCouplingSlaveReportStep.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
20#ifndef OPM_RESERVOIR_COUPLING_SLAVE_REPORT_STEP_HPP
21#define OPM_RESERVOIR_COUPLING_SLAVE_REPORT_STEP_HPP
22
24#include <opm/input/eclipse/EclipseState/Phase.hpp>
25#include <opm/input/eclipse/Schedule/Group/Group.hpp>
26
27#include <map>
28#include <string>
29#include <tuple>
30#include <utility>
31
32namespace Opm {
33
34// Avoid including the complete definition of ReservoirCouplingSlave here to avoid circular dependency.
35template <class Scalar> class ReservoirCouplingSlave;
36
55template <class Scalar>
57public:
65
70 );
71
74 const Parallel::Communication &comm() const { return this->slave_.getComm(); }
75
78 MPI_Comm getSlaveMasterComm() const { return this->slave_.getMasterComm(); }
79
84 bool hasMasterInjectionTarget(const std::string& gname, const Phase phase) const;
85
89 bool hasMasterGroupNodePressure(const std::string& gname) const;
90
94 bool hasMasterProductionLimits(const std::string& gname) const;
95
99 bool hasMasterProductionTarget(const std::string& gname) const;
100
106 bool isFirstSubstepOfSyncTimestep() const { return is_first_substep_of_sync_timestep_; }
107
115 bool isLastSubstepOfSyncTimestep() const { return is_last_substep_of_sync_timestep_; }
116
119 ReservoirCoupling::Logger& logger() const { return this->slave_.logger(); }
120
125 Scalar masterGroupNodePressure(const std::string& gname) const;
126
129 const std::map<std::string, Scalar>& masterGroupNodePressures() const
130 { return master_group_node_pressures_; }
131
137 std::pair<Scalar, Group::InjectionCMode> masterInjectionTarget(
138 const std::string& gname, const Phase phase) const;
139
144 const MasterProductionLimits& masterProductionLimits(const std::string& gname) const;
145
150 std::pair<Scalar, Group::ProductionCMode> masterProductionTarget(const std::string& gname) const;
151
156 void receiveMasterGroupNodePressuresFromMaster(std::size_t num_pressures);
157
161 std::pair<std::size_t, bool> receiveNumMasterGroupNodePressuresFromMaster();
162
170
178 { return last_received_master_group_node_pressures_is_final_; }
179
188 { return connected_to_master_coupled_network_; }
189
192 std::tuple<std::size_t, std::size_t, std::size_t> receiveNumGroupConstraintsFromMaster() const;
193
197 void receiveGasLiftGroupLimitsFromMaster(std::size_t num_limits);
198
203
206 void receiveInjectionGroupTargetsFromMaster(std::size_t num_targets);
207
210 void receiveProductionGroupConstraintsFromMaster(std::size_t num_targets);
211
222 void sendProductionDataToMaster(const std::vector<SlaveGroupProductionData> &production_data) const;
223
234 void sendInjectionDataToMaster(const std::vector<SlaveGroupInjectionData> &injection_data) const;
235
238 void setFirstSubstepOfSyncTimestep(bool value) { is_first_substep_of_sync_timestep_ = value; }
239
243 void setLastSubstepOfSyncTimestep(bool value) { is_last_substep_of_sync_timestep_ = value; }
244
251 void markSlaveGroupsInSchedule(Schedule& schedule, int report_step_idx);
252
255 const std::string& slaveName() const { return this->slave_.getSlaveName(); }
256
260 void setMasterGroupNodePressure(const std::string& gname, const Scalar pressure);
261
268 const std::string& gname, const Phase phase, const Scalar target, const Group::InjectionCMode cmode);
269
273 void setMasterProductionLimits(const std::string& gname, const MasterProductionLimits& limits);
274
279 void setMasterProductionTarget(const std::string& gname, const Scalar target, const Group::ProductionCMode cmode);
280
283 void setWellsSolvedThisSyncStep(bool value) { wells_solved_this_sync_step_ = value; }
284
290 bool wellsSolvedThisSyncStep() const { return wells_solved_this_sync_step_; }
291
292
293private:
310 template <class DataType>
311 void sendDataToMaster_(
312 const std::vector<DataType>& data,
313 MessageTag tag,
314 const std::string& data_type_name
315 ) const;
316
319 // Flag to track if this is the first substep within a "sync" timestep.
320 // Used to control reservoir coupling synchronization.
321 bool is_first_substep_of_sync_timestep_{true};
322 // Flag to track if this is the last substep within a "sync" timestep.
323 // Used to control reservoir coupling synchronization of summary data sent from
324 // the slave to the master process.
325 bool is_last_substep_of_sync_timestep_{false};
326 // Flag to track whether the initial well solve of this sync timestep has run.
327 // Cleared before the pre-solve send of slave group data and set after the solve;
328 // see wellsSolvedThisSyncStep().
329 bool wells_solved_this_sync_step_{false};
330
331 // Master-imposed targets and corresponding control modes, received from the master
332 // process at the beginning of each sync timestep. Cleared and repopulated on every
333 // receive cycle. Used by GroupStateHelper to override slave group targets.
334 //
335 // Key: slave group name. Value: (target rate, production control mode).
336 std::map<std::string, std::pair<Scalar, Group::ProductionCMode>> master_production_targets_;
337 // Key: (injection phase, slave group name). Value: (target rate, injection control mode).
338 std::map<std::pair<Phase, std::string>, std::pair<Scalar, Group::InjectionCMode>> master_injection_targets_;
339 // Per-rate-type production limits from master hierarchy. Key: slave group name.
340 // A limit of -1 means no limit defined in the hierarchy for that rate type.
341 std::map<std::string, MasterProductionLimits> master_production_limits_;
342 // Gas lift limits (GLIFTOPT) from the master. Key: slave group name.
343 std::map<std::string, ReservoirCoupling::GasLiftGroupLimits<Scalar>> master_gas_lift_limits_;
344 // Master-computed network-leaf node pressures keyed by slave group name. Only
345 // populated for master groups that are leaf nodes in the master's extended
346 // network. Cleared and repopulated on every receive cycle.
347 std::map<std::string, Scalar> master_group_node_pressures_;
348 // Whether *this* slave is connected to the master's cross-rescoup network
349 // this sync step. Set once per sync step by
350 // receiveCoupledNetworkActiveStatusFromMaster() from the master's per-slave
351 // flag (BlackoilWellModelRescoup::masterNetworkHasMasterGroupLeavesForSlave_()).
352 // A slave that is not connected does not participate in the cross-rescoup
353 // exchange at all and balances only its own (local) network.
354 //
355 // This is deliberately separate from
356 // last_received_master_group_node_pressures_is_final_ below: connectivity is
357 // a per-sync-step property, whereas is_final is the per-iteration
358 // termination signal (only meaningful while connected).
359 bool connected_to_master_coupled_network_{false};
360 // The iteration-termination flag. Initialized for each sync step through:
361 // beginTimeStep() -> BlackoilWellModelRescoup::sendCoupledNetworkActiveStatus()
362 // to is_final = !connected (so an unconnected slave sees is_final = true and
363 // never enters the per-iteration receive). For a connected slave it then
364 // tracks the per-iteration is_final from the node-pressure header:
365 //
366 // In that case it is set to "true" in the course of the network iteration
367 // in BlackoilWellModel::assemble() -> BlackoilWellModel::updateWellControlsAndNetworkIteration()
368 // based on the "more_network_update" return value of BlackoilWellModelNetwork::update().
369 // If it returns "false" the flag is reset to "true" and stays true for the rest of the sync timestep.
370 //
371 // Connected slaves use this variable to determine when to exit the outer
372 // network loop such that the MPI master-slave communication protocol is kept in sync.
373 bool last_received_master_group_node_pressures_is_final_{true};
374};
375} // namespace Opm
376#endif // OPM_RESERVOIR_COUPLING_SLAVE_REPORT_STEP_HPP
Definition: ReservoirCoupling.hpp:42
Definition: ReservoirCouplingSlave.hpp:40
Manages slave-side reservoir coupling operations for a single report step.
Definition: ReservoirCouplingSlaveReportStep.hpp:56
MPI_Comm getSlaveMasterComm() const
Get the MPI communicator for slave-master communication.
Definition: ReservoirCouplingSlaveReportStep.hpp:78
const ReservoirCoupling::GasLiftGroupLimits< Scalar > * masterGasLiftLimits(const std::string &gname) const
Gas lift limits (GLIFTOPT) the master imposes on a slave group, if any.
const std::string & slaveName() const
Get the name of this slave process.
Definition: ReservoirCouplingSlaveReportStep.hpp:255
bool wellsSolvedThisSyncStep() const
True once the initial well solve for this sync step has run.
Definition: ReservoirCouplingSlaveReportStep.hpp:290
std::pair< Scalar, Group::ProductionCMode > masterProductionTarget(const std::string &gname) const
Get the master-imposed production target and control mode for a group.
void setMasterInjectionTarget(const std::string &gname, const Phase phase, const Scalar target, const Group::InjectionCMode cmode)
Store a master-imposed injection target for a group and phase.
void setMasterGroupNodePressure(const std::string &gname, const Scalar pressure)
Store a master-computed network-leaf node pressure for a master group.
bool hasMasterInjectionTarget(const std::string &gname, const Phase phase) const
Check if a master-imposed injection target exists for a group and phase.
void setFirstSubstepOfSyncTimestep(bool value)
Set whether this is the first substep within a "sync" timestep.
Definition: ReservoirCouplingSlaveReportStep.hpp:238
std::tuple< std::size_t, std::size_t, std::size_t > receiveNumGroupConstraintsFromMaster() const
Receive the number of injection and production constraints from master.
void receiveProductionGroupConstraintsFromMaster(std::size_t num_targets)
Receive production group constraints from master and store them locally.
const MasterProductionLimits & masterProductionLimits(const std::string &gname) const
Get the master-imposed per-rate-type production limits for a group.
void receiveMasterGroupNodePressuresFromMaster(std::size_t num_pressures)
Receive master-computed network-leaf node pressures from master and store them locally....
void receiveGasLiftGroupLimitsFromMaster(std::size_t num_limits)
Receive the gas lift limits for the slave groups from master and store them.
bool hasMasterProductionLimits(const std::string &gname) const
Check if master-imposed per-rate-type production limits exist for a group.
bool hasMasterGroupNodePressure(const std::string &gname) const
Check if a master-computed network-leaf node pressure exists for a master group.
const Parallel::Communication & comm() const
Get the MPI communicator for intra-slave communication.
Definition: ReservoirCouplingSlaveReportStep.hpp:74
std::pair< std::size_t, bool > receiveNumMasterGroupNodePressuresFromMaster()
Receive the master-group-node-pressures header from master.
void setWellsSolvedThisSyncStep(bool value)
Record whether the initial well solve for this sync step has run.
Definition: ReservoirCouplingSlaveReportStep.hpp:283
bool lastReceivedMasterGroupNodePressuresIsFinal() const
Whether the most recent master-group-node-pressures receive carried the is_final flag....
Definition: ReservoirCouplingSlaveReportStep.hpp:177
void setLastSubstepOfSyncTimestep(bool value)
Set whether this is the last substep within a "sync" timestep.
Definition: ReservoirCouplingSlaveReportStep.hpp:243
void setMasterProductionTarget(const std::string &gname, const Scalar target, const Group::ProductionCMode cmode)
Store a master-imposed production target for a group.
const std::map< std::string, Scalar > & masterGroupNodePressures() const
Get the full map of master-computed network-leaf node pressures.
Definition: ReservoirCouplingSlaveReportStep.hpp:129
bool isLastSubstepOfSyncTimestep() const
Check if this is the last substep within a "sync" timestep.
Definition: ReservoirCouplingSlaveReportStep.hpp:115
Scalar masterGroupNodePressure(const std::string &gname) const
Get the master-computed network-leaf node pressure for a master group.
void receiveCoupledNetworkActiveStatusFromMaster()
Receive the master's single-bool flag for the current sync timestep and mirror it into last_received_...
ReservoirCoupling::Logger & logger() const
Get the logger for reservoir coupling operations.
Definition: ReservoirCouplingSlaveReportStep.hpp:119
void sendProductionDataToMaster(const std::vector< SlaveGroupProductionData > &production_data) const
Send production data to the master process.
std::pair< Scalar, Group::InjectionCMode > masterInjectionTarget(const std::string &gname, const Phase phase) const
Get the master-imposed injection target and control mode for a group and phase.
bool hasMasterProductionTarget(const std::string &gname) const
Check if a master-imposed production target exists for a group.
bool connectedToMasterCoupledNetwork() const
Whether this slave is connected to the master's cross-rescoup network this sync step (i....
Definition: ReservoirCouplingSlaveReportStep.hpp:187
void sendInjectionDataToMaster(const std::vector< SlaveGroupInjectionData > &injection_data) const
Send injection data to the master process.
void receiveInjectionGroupTargetsFromMaster(std::size_t num_targets)
Receive injection group targets from master and store them locally.
ReservoirCouplingSlaveReportStep(ReservoirCouplingSlave< Scalar > &slave)
Construct a report step manager for the slave process.
ReservoirCoupling::MessageTag MessageTag
Definition: ReservoirCouplingSlaveReportStep.hpp:61
bool isFirstSubstepOfSyncTimestep() const
Check if this is the first substep within a "sync" timestep.
Definition: ReservoirCouplingSlaveReportStep.hpp:106
void setMasterProductionLimits(const std::string &gname, const MasterProductionLimits &limits)
Store master-imposed per-rate-type production limits for a group.
void markSlaveGroupsInSchedule(Schedule &schedule, int report_step_idx)
Mark slave groups in the Schedule as production/injection groups.
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
MessageTag
Definition: ReservoirCoupling.hpp:138
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:184
Definition: blackoilbioeffectsmodules.hh:45
Gas lift limits (GLIFTOPT) the master imposes on a slave group.
Definition: ReservoirCoupling.hpp:336
Definition: ReservoirCoupling.hpp:305
Master-computed network-leaf node pressure for a single master group.
Definition: ReservoirCoupling.hpp:362
Per-rate-type production limits received from master hierarchy. A value of -1 means no limit defined ...
Definition: ReservoirCoupling.hpp:347
Definition: ReservoirCoupling.hpp:315
Definition: ReservoirCoupling.hpp:285
Definition: ReservoirCoupling.hpp:239