ReservoirCouplingMaster.hpp
Go to the documentation of this file.
1/*
2 Copyright 2024 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_MASTER_HPP
21#define OPM_RESERVOIR_COUPLING_MASTER_HPP
22
27#include <opm/input/eclipse/EclipseState/Phase.hpp>
28#include <opm/input/eclipse/Schedule/Schedule.hpp>
29#include <opm/common/OpmLog/OpmLog.hpp>
30
31#include <mpi.h>
32
33#include <filesystem>
34#include <map>
35#include <string>
36#include <tuple>
37#include <utility>
38#include <vector>
39
40namespace Opm {
41
42template <class Scalar>
44public:
53
55 const Parallel::Communication &comm,
56 const Schedule &schedule,
57 int argc, char **argv
58 );
59
60 bool activated() { return this->activated_; }
61 void addSlaveCommunicator(MPI_Comm comm) {
62 this->master_slave_comm_.push_back(comm);
63 }
64 void addSlaveName(const std::string &name) { this->slave_names_.push_back(name); }
65 void addSlaveActivationDate(double date) { this->slave_activation_dates_.push_back(date); }
66 void addSlaveStartDate(std::time_t date) { this->slave_start_dates_.push_back(date); }
68
81 int effectiveGCW(const std::string& group_name) const {
82 const auto it = this->effective_gcw_.find(group_name);
83 return (it == this->effective_gcw_.end()) ? 1 : it->second;
84 }
85 void setEffectiveGCW(const std::string& group_name, int value) {
86 this->effective_gcw_[group_name] = value;
87 }
88
99 int effectiveInjectionGCW(const std::string& group_name, Phase phase) const {
100 const auto it = this->effective_injection_gcw_.find({group_name, phase});
101 return (it == this->effective_injection_gcw_.end()) ? 1 : it->second;
102 }
103 void setEffectiveInjectionGCW(const std::string& group_name, Phase phase, int value) {
104 this->effective_injection_gcw_[{group_name, phase}] = value;
105 }
106
111 this->effective_gcw_.clear();
112 this->effective_injection_gcw_.clear();
113 }
114
115 double getActivationDate() const { return this->activation_date_; }
116 int getArgc() const { return this->argc_; }
117 char *getArgv(int index) const { return this->argv_[index]; }
118 char **getArgv() const { return this->argv_; }
119 const Parallel::Communication &getComm() const { return this->comm_; }
120
133 const std::vector<std::string>& getMasterGroupNamesForSlave(std::size_t slave_idx) const;
134
142 const std::string &slave_name, const std::string &master_group_name) const;
143
145 const std::string &group_name, ReservoirCoupling::Phase phase, ReservoirCoupling::RateKind kind) const;
147 bool hasSlaveGroupProductionData(const std::string &group_name) const {
148 return this->report_step_data_->hasSlaveGroupProductionData(group_name);
149 }
151 Scalar getMasterGroupGasLiftRate(const std::string &group_name, bool network) const {
152 return this->report_step_data_->getMasterGroupGasLiftRate(group_name, network);
153 }
155 Scalar getMasterGroupGasLiftHeadroom(const std::string &group_name) const {
156 return this->report_step_data_->getMasterGroupGasLiftHeadroom(group_name);
157 }
158 std::map<std::string, std::string>& getMasterGroupToSlaveNameMap() {
159 return this->master_group_slave_names_;
160 }
162 { return TimeService::to_time_t(this->schedule_.getStartTime()); }
163 double getSlaveActivationDate(int index) const { return this->slave_activation_dates_[index]; }
164 const double *getSlaveActivationDates() const { return this->slave_activation_dates_.data(); }
165 MPI_Comm getSlaveComm(int index) const { return this->master_slave_comm_[index]; }
166 std::map<std::string, std::vector<std::string>> &getSlaveNameToMasterGroupsMap() {
167 return this->slave_name_to_master_groups_map_;
168 }
169 int getSlaveGroupNumGroupControlledInjectors(const std::string &master_group_name,
170 ReservoirCoupling::Phase phase) const;
171 int getSlaveGroupNumGroupControlledProducers(const std::string &master_group_name) const;
172 const Potentials& getSlaveGroupPotentials(const std::string &master_group_name) const;
173 int getSlaveIdx(const std::string &slave_name) const;
174 const std::string &getSlaveName(int index) const { return this->slave_names_[index]; }
175 double getSlaveStartDate(int index) const { return this->slave_start_dates_[index]; }
176 const double *getSlaveStartDates() { return this->slave_start_dates_.data(); }
177 void initStartOfReportStep(int report_step_idx);
180 bool isMasterGroup(const std::string &group_name) const;
181
189
193 void setNeedsSlaveDataReceive(bool value);
194
195 ReservoirCoupling::Logger& logger() { return this->logger_; }
196 ReservoirCoupling::Logger& logger() const { return this->logger_; }
197 void maybeActivate(int report_step);
199 double maybeChopSubStep(double suggested_timestep, double current_time) const;
200 void maybeSpawnSlaveProcesses(int report_step);
201 std::size_t numSlaveGroups(unsigned int index);
202 std::size_t numSlaves() const { return this->numSlavesStarted(); }
203 std::size_t numSlavesStarted() const;
204
208 std::size_t numCoupledSlaves() const;
213 void resizeNextReportDates(int size);
214 void resizeSlaveActivationDates(int size) { this->slave_activation_dates_.resize(size); }
215 void resizeSlaveStartDates(int size) { this->slave_start_dates_.resize(size); }
216 const Schedule& schedule() const { return this->schedule_; }
217 void sendNextTimeStepToSlaves(double next_time_step) {
218 this->report_step_data_->setSyncStepLength(next_time_step);
219 this->time_stepper_->sendNextTimeStepToSlaves(next_time_step);
220 }
221
224 void saveGroupFlowsAtStartOfSyncStep() { this->report_step_data_->saveGroupFlowsAtStartOfSyncStep(); }
225
228 void updateFlowChangeTimeStepLimit() { this->report_step_data_->updateFlowChangeTimeStepLimit(); }
229
231 double flowChangeTimeStepLimit() const { return this->report_step_data_->flowChangeTimeStepLimit(); }
232
235 std::tuple<std::string, double, double> flowChangeLimitingGroup() const {
236 return this->report_step_data_->flowChangeLimitingGroup();
237 }
238
242 std::size_t slave_idx,
243 bool active
244 ) const;
246 std::size_t slave_idx,
247 const std::vector<InjectionGroupTarget>& injection_targets
248 ) const;
249
252 std::size_t slave_idx,
253 const std::vector<MasterGroupNodePressure>& pressures
254 ) const;
256 std::size_t slave_idx,
257 std::size_t num_injection_targets,
258 std::size_t num_production_constraints,
259 std::size_t num_gas_lift_limits
260 ) const;
262 std::size_t slave_idx,
263 const std::vector<ReservoirCoupling::GasLiftGroupLimits<Scalar>>& gas_lift_limits
264 ) const {
265 this->report_step_data_->sendGasLiftGroupLimitsToSlave(slave_idx, gas_lift_limits);
266 }
267
271 std::size_t slave_idx,
272 std::size_t num_pressures,
273 bool is_final
274 ) const;
276 std::size_t slave_idx,
277 const std::vector<ProductionGroupConstraints>& production_constraints
278 ) const;
279 void setDeferredLogger(DeferredLogger *deferred_logger) {
280 this->logger_.setDeferredLogger(deferred_logger);
281 }
283
287 data::ReservoirCouplingGroupRates collectGroupRatesForSummary() const;
288 // These are currently only used for unit testing
289 void setSlaveActivated(int index)
290 {
291 if (this->slave_activation_status_.size() < this->numSlavesStarted()) {
292 this->slave_activation_status_.resize(this->numSlavesStarted(), 0);
293 }
294 this->slave_activation_status_[index] = 1;
295 }
296 void setSlaveActivationDate(int index, double date) { this->slave_activation_dates_[index] = date; }
297 void setSlaveGroupInjectionData(const std::string& slave_name,
298 std::vector<SlaveGroupInjectionData> injection_data)
299 {
300 this->report_step_data_->setSlaveGroupInjectionData(slave_name, std::move(injection_data));
301 }
302 void setSlaveGroupProductionData(const std::string& slave_name,
303 std::vector<SlaveGroupProductionData> production_data)
304 {
305 this->report_step_data_->setSlaveGroupProductionData(slave_name, std::move(production_data));
306 }
307 void setSlaveNextReportTimeOffset(int index, double offset);
308 void setSlaveStartDate(int index, std::time_t date) { this->slave_start_dates_[index] = date; }
313 bool slaveIsActivated(int index) const { return this->slave_activation_status_[index] != 0; }
314
322 bool slaveHasEnded(int index) const
323 {
324 return (static_cast<std::size_t>(index) < this->slave_ended_.size())
325 && (this->slave_ended_[index] != 0);
326 }
327
333 bool slaveIsCoupled(int index) const
334 {
335 return this->slaveIsActivated(index) && !this->slaveHasEnded(index);
336 }
337
346
351 bool syncAtReportSteps() const { return this->sync_at_report_steps_; }
353 const std::string& slave_name, const std::map<std::string, std::size_t>& master_group_map);
354
362
373
374private:
375 double getMasterActivationDate_() const;
376
377 const Parallel::Communication &comm_;
378 const Schedule& schedule_;
379 int argc_;
380 char **argv_;
381 // Whether the master process has activated the reservoir coupling
382 bool activated_{false};
383
384 // NOTE: MPI_Comm is just an integer handle, so we can just copy it into the vector
385 std::vector<MPI_Comm> master_slave_comm_; // MPI communicators for the slave processes
386 std::vector<std::string> slave_names_;
387
388 // The start dates are in whole seconds since the epoch. We use a double to store the value
389 // since both schedule_.getStartTime(), converted to seconds since the epoch, and
390 // schedule_.stepLength(report_step) represent whole seconds.
391 // However, note that schedule_[report_step].start_time() returns a time_point
392 // which can include milliseconds. The double values are also convenient when we need to
393 // to add fractions of seconds for sub steps to the start date.
394 std::vector<double> slave_start_dates_;
395
396 // The activation dates are in whole seconds since the epoch.
397 std::vector<double> slave_activation_dates_;
398 double activation_date_{0.0}; // The date when SLAVES is encountered in the schedule
399
400 // A mapping from a slave name to the master group name order used when slaves send
401 // potentials to the master process.
402 std::map<std::string, std::map<std::string, std::size_t>> master_group_name_order_;
403
404 // Effective group-controlled-wells count per master group, used by guide-rate
405 // distribution independently of the production control mode (see effectiveGCW()).
406 // Reset and repopulated on each master-group constraint calculation.
407 std::map<std::string, int> effective_gcw_;
408
409 // Effective group-controlled-wells count per (master group, injection phase),
410 // the injection counterpart of effective_gcw_ (see effectiveInjectionGCW()).
411 std::map<std::pair<std::string, Phase>, int> effective_injection_gcw_;
412
413 mutable ReservoirCoupling::Logger logger_;
414
415 // Whether the slave has activated. Unfortunatley, we cannot use std::vector<bool> since
416 // it is not supported to get a pointer to the underlying array of bools needed
417 // with MPI broadcast().
418 std::vector<std::uint8_t> slave_activation_status_;
419
420 // Whether the slave has reached the end of its own schedule and disconnected. Same
421 // std::uint8_t-instead-of-bool reasoning as slave_activation_status_ above: the value is
422 // derived on every rank from the broadcast next-report-date vector, so it must be a plain
423 // array of bytes.
424 std::vector<std::uint8_t> slave_ended_;
425
426 // A mapping from master group names to slave names
427 std::map<std::string, std::string> master_group_slave_names_;
428
429 // A mapping from slave names to master group names
430 // NOTE: The order of the master groups in the vector is important,
431 // as the slaves will communicate the indices of the master groups in
432 // the vector instead of the group names themselves.
433 // NOTE: This map is created by ReservoirCouplingSpawnSlaves.cpp
434 std::map<std::string, std::vector<std::string>> slave_name_to_master_groups_map_;
435
436 // Direct index-based lookup for performance optimization (O(1) instead of O(log n))
437 // This vector is populated in parallel with slave_name_to_master_groups_map_
438 // and maintains the same ordering as slave_names_ vector for consistent indexing
439 std::vector<std::vector<std::string>> slave_idx_to_master_groups_;
440
441 // Stores data that changes for a single report step or for timesteps within a report step.
442 std::unique_ptr<ReservoirCouplingMasterReportStep<Scalar>> report_step_data_{nullptr};
443
444 // Handles time stepping for the master and slaves
445 std::unique_ptr<ReservoirCouplingTimeStepper<Scalar>> time_stepper_{nullptr};
446
447 // CLI flag --rescoup-sync-at-report-steps. When true, the master syncs with
448 // its slaves at report-step boundaries (RSYNC). When false
449 // (default), it syncs at every master time step (TSYNC).
450 bool sync_at_report_steps_{false};
451};
452
453} // namespace Opm
454
455#endif // OPM_RESERVOIR_COUPLING_MASTER_HPP
Definition: DeferredLogger.hpp:57
Definition: ReservoirCoupling.hpp:42
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCoupling.hpp:55
void clearDeferredLogger()
Definition: ReservoirCoupling.hpp:47
Definition: ReservoirCouplingMaster.hpp:43
void setSlaveGroupProductionData(const std::string &slave_name, std::vector< SlaveGroupProductionData > production_data)
Definition: ReservoirCouplingMaster.hpp:302
void resizeSlaveActivationDates(int size)
Definition: ReservoirCouplingMaster.hpp:214
bool hasSlaveGroupProductionData(const std::string &group_name) const
Whether the slave of a master group has sent production data.
Definition: ReservoirCouplingMaster.hpp:147
void resizeSlaveStartDates(int size)
Definition: ReservoirCouplingMaster.hpp:215
std::map< std::string, std::vector< std::string > > & getSlaveNameToMasterGroupsMap()
Definition: ReservoirCouplingMaster.hpp:166
char * getArgv(int index) const
Definition: ReservoirCouplingMaster.hpp:117
bool slaveIsCoupled(int index) const
Whether the slave currently takes part in the coupling.
Definition: ReservoirCouplingMaster.hpp:333
bool slaveHasEnded(int index) const
Whether the slave has reached the end of its own schedule and left the coupling.
Definition: ReservoirCouplingMaster.hpp:322
int getSlaveGroupNumGroupControlledInjectors(const std::string &master_group_name, ReservoirCoupling::Phase phase) const
double getActivationDate() const
Definition: ReservoirCouplingMaster.hpp:115
char ** getArgv() const
Definition: ReservoirCouplingMaster.hpp:118
const Potentials & getSlaveGroupPotentials(const std::string &master_group_name) const
void sendGasLiftGroupLimitsToSlave(std::size_t slave_idx, const std::vector< ReservoirCoupling::GasLiftGroupLimits< Scalar > > &gas_lift_limits) const
Definition: ReservoirCouplingMaster.hpp:261
void maybeActivate(int report_step)
bool syncAtReportSteps() const
Whether the master syncs with slaves at slave report-step boundaries (true) or at every master actual...
Definition: ReservoirCouplingMaster.hpp:351
std::size_t numSlaveGroups(unsigned int index)
ReservoirCoupling::Logger & logger()
Definition: ReservoirCouplingMaster.hpp:195
void resizeNextReportDates(int size)
void maybeReceiveActivationHandshakeFromSlaves(double current_time)
void sendNextTimeStepToSlaves(double next_time_step)
Definition: ReservoirCouplingMaster.hpp:217
bool isFirstSubstepOfSyncTimestep() const
void setFirstSubstepOfSyncTimestep(bool value)
std::size_t numSlavesStarted() const
double maybeChopSubStep(double suggested_timestep, double current_time) const
bool slaveIsActivated(int index) const
Whether the slave has completed its activation handshake with the master.
Definition: ReservoirCouplingMaster.hpp:313
void setEffectiveGCW(const std::string &group_name, int value)
Definition: ReservoirCouplingMaster.hpp:85
void sendProductionConstraintsToSlave(std::size_t slave_idx, const std::vector< ProductionGroupConstraints > &production_constraints) const
std::size_t getMasterGroupCanonicalIdx(const std::string &slave_name, const std::string &master_group_name) const
Get the canonical index of the master group for a given slave name and master group name....
std::size_t numCoupledSlaves() const
Number of slaves that currently take part in the coupling.
void addSlaveCommunicator(MPI_Comm comm)
Definition: ReservoirCouplingMaster.hpp:61
void addSlaveStartDate(std::time_t date)
Definition: ReservoirCouplingMaster.hpp:66
bool activated()
Definition: ReservoirCouplingMaster.hpp:60
void initStartOfReportStep(int report_step_idx)
const Parallel::Communication & getComm() const
Definition: ReservoirCouplingMaster.hpp:119
ReservoirCoupling::Logger & logger() const
Definition: ReservoirCouplingMaster.hpp:196
void markSlaveEndedAndDisconnect(int index)
Record that a slave has ended, and close its intercommunicator.
int getSlaveIdx(const std::string &slave_name) const
void setSlaveActivated(int index)
Definition: ReservoirCouplingMaster.hpp:289
int effectiveInjectionGCW(const std::string &group_name, Phase phase) const
Effective group-controlled-wells (GCW) count for a master group in the injection guide-rate distribut...
Definition: ReservoirCouplingMaster.hpp:99
void setSlaveNextReportTimeOffset(int index, double offset)
void addSlaveActivationDate(double date)
Definition: ReservoirCouplingMaster.hpp:65
void setSlaveGroupInjectionData(const std::string &slave_name, std::vector< SlaveGroupInjectionData > injection_data)
Definition: ReservoirCouplingMaster.hpp:297
Scalar getMasterGroupGasLiftRate(const std::string &group_name, bool network) const
Lift gas rate of the slave group of a master group, as last received from the slave.
Definition: ReservoirCouplingMaster.hpp:151
void clearDeferredLogger()
Definition: ReservoirCouplingMaster.hpp:67
bool needsSlaveDataReceive() const
Check if the master needs to receive production data from the slaves.
void sendNumGroupConstraintsToSlave(std::size_t slave_idx, std::size_t num_injection_targets, std::size_t num_production_constraints, std::size_t num_gas_lift_limits) const
double flowChangeTimeStepLimit() const
Upper limit (in seconds) for the next sync step from GRUPMAST item 4, infinity if none.
Definition: ReservoirCouplingMaster.hpp:231
Scalar getMasterGroupRate(const std::string &group_name, ReservoirCoupling::Phase phase, ReservoirCoupling::RateKind kind) const
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCouplingMaster.hpp:279
void sendCoupledNetworkActiveStatusToSlave(std::size_t slave_idx, bool active) const
Send a single boolean to a slave telling it whether the master will iterate the cross-rescoup network...
void maybeSpawnSlaveProcesses(int report_step)
void sendTerminateAndDisconnect()
Send terminate signal to all active slaves and disconnect intercommunicators.
void resetEffectiveGCW()
Clear all effective-GCW entries, production and injection. Call at the start of each master-group con...
Definition: ReservoirCouplingMaster.hpp:110
const std::string & getSlaveName(int index) const
Definition: ReservoirCouplingMaster.hpp:174
void updateFlowChangeTimeStepLimit()
Update the flow-change time step limit (GRUPMAST item 4) from the flows received at the end of the sy...
Definition: ReservoirCouplingMaster.hpp:228
void sendMasterGroupNodePressuresToSlave(std::size_t slave_idx, const std::vector< MasterGroupNodePressure > &pressures) const
Send master-computed network-leaf node pressures to a slave.
ReservoirCouplingMaster(const Parallel::Communication &comm, const Schedule &schedule, int argc, char **argv)
void addSlaveName(const std::string &name)
Definition: ReservoirCouplingMaster.hpp:64
void saveGroupFlowsAtStartOfSyncStep()
Save the master group flow rates at the start of the sync step.
Definition: ReservoirCouplingMaster.hpp:224
const double * getSlaveActivationDates() const
Definition: ReservoirCouplingMaster.hpp:164
std::map< std::string, std::string > & getMasterGroupToSlaveNameMap()
Definition: ReservoirCouplingMaster.hpp:158
void sendDontTerminateSignalToSlaves()
Send "don't terminate" signal (value=0) to all active slaves.
const Schedule & schedule() const
Definition: ReservoirCouplingMaster.hpp:216
void updateMasterGroupNameOrderMap(const std::string &slave_name, const std::map< std::string, std::size_t > &master_group_map)
double getSlaveActivationDate(int index) const
Definition: ReservoirCouplingMaster.hpp:163
std::tuple< std::string, double, double > flowChangeLimitingGroup() const
The master group that sets flowChangeTimeStepLimit(): name, fractional flow change and limiting fract...
Definition: ReservoirCouplingMaster.hpp:235
void sendNumMasterGroupNodePressuresToSlave(std::size_t slave_idx, std::size_t num_pressures, bool is_final) const
Send the count of master-computed network-leaf node pressures plus the master's is_final flag for thi...
std::size_t numSlaves() const
Definition: ReservoirCouplingMaster.hpp:202
const double * getSlaveStartDates()
Definition: ReservoirCouplingMaster.hpp:176
int effectiveGCW(const std::string &group_name) const
Effective group-controlled-wells (GCW) count for a master group, used by guide-rate distribution inde...
Definition: ReservoirCouplingMaster.hpp:81
Scalar getMasterGroupGasLiftHeadroom(const std::string &group_name) const
How much more lift gas the slave group of a master group can take.
Definition: ReservoirCouplingMaster.hpp:155
void setSlaveActivationDate(int index, double date)
Definition: ReservoirCouplingMaster.hpp:296
void setNeedsSlaveDataReceive(bool value)
Set whether the master needs to receive production data from the slaves.
MPI_Comm getSlaveComm(int index) const
Definition: ReservoirCouplingMaster.hpp:165
void setEffectiveInjectionGCW(const std::string &group_name, Phase phase, int value)
Definition: ReservoirCouplingMaster.hpp:103
double getSlaveStartDate(int index) const
Definition: ReservoirCouplingMaster.hpp:175
const std::vector< std::string > & getMasterGroupNamesForSlave(std::size_t slave_idx) const
Get the master group names associated with a slave reservoir by index.
int getSlaveGroupNumGroupControlledProducers(const std::string &master_group_name) const
double getSimulationStartDate() const
Definition: ReservoirCouplingMaster.hpp:161
void sendInjectionTargetsToSlave(std::size_t slave_idx, const std::vector< InjectionGroupTarget > &injection_targets) const
void setSlaveStartDate(int index, std::time_t date)
Definition: ReservoirCouplingMaster.hpp:308
data::ReservoirCouplingGroupRates collectGroupRatesForSummary() const
Collect production/injection rates for all master groups.
bool isMasterGroup(const std::string &group_name) const
int getArgc() const
Definition: ReservoirCouplingMaster.hpp:116
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
RateKind
Selects which kind of rate to retrieve from slave group data.
Definition: ReservoirCoupling.hpp:196
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
Definition: ReservoirCoupling.hpp:215
Definition: ReservoirCoupling.hpp:315
Utility class for comparing double values representing epoch dates or elapsed time.
Definition: ReservoirCoupling.hpp:431
Definition: ReservoirCoupling.hpp:285
Definition: ReservoirCoupling.hpp:239