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/Schedule/Schedule.hpp>
28#include <opm/common/OpmLog/OpmLog.hpp>
29
30#include <mpi.h>
31
32#include <filesystem>
33#include <vector>
34
35namespace Opm {
36
37template <class Scalar>
39public:
47
49 const Parallel::Communication &comm,
50 const Schedule &schedule,
51 int argc, char **argv
52 );
53
54 bool activated() { return this->activated_; }
55 void addSlaveCommunicator(MPI_Comm comm) {
56 this->master_slave_comm_.push_back(comm);
57 }
58 void addSlaveName(const std::string &name) { this->slave_names_.push_back(name); }
59 void addSlaveActivationDate(double date) { this->slave_activation_dates_.push_back(date); }
60 void addSlaveStartDate(std::time_t date) { this->slave_start_dates_.push_back(date); }
62
74 int effectiveGCW(const std::string& group_name) const {
75 const auto it = this->effective_gcw_.find(group_name);
76 return (it == this->effective_gcw_.end()) ? 1 : it->second;
77 }
78 void setEffectiveGCW(const std::string& group_name, int value) {
79 this->effective_gcw_[group_name] = value;
80 }
81
85 void resetEffectiveGCW() { this->effective_gcw_.clear(); }
86
87 double getActivationDate() const { return this->activation_date_; }
88 int getArgc() const { return this->argc_; }
89 char *getArgv(int index) const { return this->argv_[index]; }
90 char **getArgv() const { return this->argv_; }
91 const Parallel::Communication &getComm() const { return this->comm_; }
92
105 const std::vector<std::string>& getMasterGroupNamesForSlave(std::size_t slave_idx) const;
106
114 const std::string &slave_name, const std::string &master_group_name) const;
115
117 const std::string &group_name, ReservoirCoupling::Phase phase, ReservoirCoupling::RateKind kind) const;
118 std::map<std::string, std::string>& getMasterGroupToSlaveNameMap() {
119 return this->master_group_slave_names_;
120 }
121 double getSimulationStartDate() const { return this->schedule_.getStartTime(); }
122 double getSlaveActivationDate(int index) const { return this->slave_activation_dates_[index]; }
123 const double *getSlaveActivationDates() const { return this->slave_activation_dates_.data(); }
124 MPI_Comm getSlaveComm(int index) const { return this->master_slave_comm_[index]; }
125 std::map<std::string, std::vector<std::string>> &getSlaveNameToMasterGroupsMap() {
126 return this->slave_name_to_master_groups_map_;
127 }
128 const Potentials& getSlaveGroupPotentials(const std::string &master_group_name);
129 int getSlaveIdx(const std::string &slave_name) const;
130 const std::string &getSlaveName(int index) const { return this->slave_names_[index]; }
131 double getSlaveStartDate(int index) const { return this->slave_start_dates_[index]; }
132 const double *getSlaveStartDates() { return this->slave_start_dates_.data(); }
133 void initStartOfReportStep(int report_step_idx);
136 bool isMasterGroup(const std::string &group_name) const;
137
145
149 void setNeedsSlaveDataReceive(bool value);
150
151 ReservoirCoupling::Logger& logger() { return this->logger_; }
152 ReservoirCoupling::Logger& logger() const { return this->logger_; }
153 void maybeActivate(int report_step);
155 double maybeChopSubStep(double suggested_timestep, double current_time) const;
156 void maybeSpawnSlaveProcesses(int report_step);
157 std::size_t numSlaveGroups(unsigned int index);
158 std::size_t numSlaves() const { return this->numSlavesStarted(); }
159 std::size_t numSlavesStarted() const;
160 std::size_t numActivatedSlaves() const;
165 void resizeNextReportDates(int size);
166 void resizeSlaveActivationDates(int size) { this->slave_activation_dates_.resize(size); }
167 void resizeSlaveStartDates(int size) { this->slave_start_dates_.resize(size); }
168 const Schedule& schedule() const { return this->schedule_; }
169 void sendNextTimeStepToSlaves(double next_time_step) {
170 this->time_stepper_->sendNextTimeStepToSlaves(next_time_step);
171 }
172
176 std::size_t slave_idx,
177 bool active
178 ) const;
180 std::size_t slave_idx,
181 const std::vector<InjectionGroupTarget>& injection_targets
182 ) const;
183
186 std::size_t slave_idx,
187 const std::vector<MasterGroupNodePressure>& pressures
188 ) const;
190 std::size_t slave_idx,
191 std::size_t num_injection_targets,
192 std::size_t num_production_constraints
193 ) const;
194
198 std::size_t slave_idx,
199 std::size_t num_pressures,
200 bool is_final
201 ) const;
203 std::size_t slave_idx,
204 const std::vector<ProductionGroupConstraints>& production_constraints
205 ) const;
206 void setDeferredLogger(DeferredLogger *deferred_logger) {
207 this->logger_.setDeferredLogger(deferred_logger);
208 }
210
214 data::ReservoirCouplingGroupRates collectGroupRatesForSummary() const;
215 // These are currently only used for unit testing
216 void setSlaveActivationDate(int index, double date) { this->slave_activation_dates_[index] = date; }
217 void setSlaveNextReportTimeOffset(int index, double offset);
218 void setSlaveStartDate(int index, std::time_t date) { this->slave_start_dates_[index] = date; }
219 bool slaveIsActivated(int index) const { return this->slave_activation_status_[index] != 0; }
220
225 bool syncAtReportSteps() const { return this->sync_at_report_steps_; }
227 const std::string& slave_name, const std::map<std::string, std::size_t>& master_group_map);
228
236
247
248private:
249 double getMasterActivationDate_() const;
250
251 const Parallel::Communication &comm_;
252 const Schedule& schedule_;
253 int argc_;
254 char **argv_;
255 // Whether the master process has activated the reservoir coupling
256 bool activated_{false};
257
258 // NOTE: MPI_Comm is just an integer handle, so we can just copy it into the vector
259 std::vector<MPI_Comm> master_slave_comm_; // MPI communicators for the slave processes
260 std::vector<std::string> slave_names_;
261
262 // The start dates are in whole seconds since the epoch. We use a double to store the value
263 // since both schedule_.getStartTime() and schedule_.stepLength(report_step) returns
264 // a double value representing whole seconds.
265 // However, note that schedule_[report_step].start_time() returns a time_point
266 // which can include milliseconds. The double values are also convenient when we need to
267 // to add fractions of seconds for sub steps to the start date.
268 std::vector<double> slave_start_dates_;
269
270 // The activation dates are in whole seconds since the epoch.
271 std::vector<double> slave_activation_dates_;
272 double activation_date_{0.0}; // The date when SLAVES is encountered in the schedule
273
274 // A mapping from a slave name to the master group name order used when slaves send
275 // potentials to the master process.
276 std::map<std::string, std::map<std::string, std::size_t>> master_group_name_order_;
277
278 // Effective group-controlled-wells count per master group, used by guide-rate
279 // distribution independently of the production control mode (see effectiveGCW()).
280 // Reset and repopulated on each master-group constraint calculation.
281 std::map<std::string, int> effective_gcw_;
282
283 mutable ReservoirCoupling::Logger logger_;
284
285 // Whether the slave has activated. Unfortunatley, we cannot use std::vector<bool> since
286 // it is not supported to get a pointer to the underlying array of bools needed
287 // with MPI broadcast().
288 std::vector<std::uint8_t> slave_activation_status_;
289
290 // A mapping from master group names to slave names
291 std::map<std::string, std::string> master_group_slave_names_;
292
293 // A mapping from slave names to master group names
294 // NOTE: The order of the master groups in the vector is important,
295 // as the slaves will communicate the indices of the master groups in
296 // the vector instead of the group names themselves.
297 // NOTE: This map is created by ReservoirCouplingSpawnSlaves.cpp
298 std::map<std::string, std::vector<std::string>> slave_name_to_master_groups_map_;
299
300 // Direct index-based lookup for performance optimization (O(1) instead of O(log n))
301 // This vector is populated in parallel with slave_name_to_master_groups_map_
302 // and maintains the same ordering as slave_names_ vector for consistent indexing
303 std::vector<std::vector<std::string>> slave_idx_to_master_groups_;
304
305 // Stores data that changes for a single report step or for timesteps within a report step.
306 std::unique_ptr<ReservoirCouplingMasterReportStep<Scalar>> report_step_data_{nullptr};
307
308 // Handles time stepping for the master and slaves
309 std::unique_ptr<ReservoirCouplingTimeStepper<Scalar>> time_stepper_{nullptr};
310
311 // CLI flag --rescoup-sync-at-report-steps. When true, the master syncs with
312 // its slaves at report-step boundaries (RSYNC). When false
313 // (default), it syncs at every master time step (TSYNC).
314 bool sync_at_report_steps_{false};
315};
316
317} // namespace Opm
318
319#endif // OPM_RESERVOIR_COUPLING_MASTER_HPP
Definition: DeferredLogger.hpp:57
Definition: ReservoirCoupling.hpp:42
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCoupling.hpp:54
void clearDeferredLogger()
Definition: ReservoirCoupling.hpp:47
Definition: ReservoirCouplingMaster.hpp:38
void resizeSlaveActivationDates(int size)
Definition: ReservoirCouplingMaster.hpp:166
void resizeSlaveStartDates(int size)
Definition: ReservoirCouplingMaster.hpp:167
std::map< std::string, std::vector< std::string > > & getSlaveNameToMasterGroupsMap()
Definition: ReservoirCouplingMaster.hpp:125
char * getArgv(int index) const
Definition: ReservoirCouplingMaster.hpp:89
double getActivationDate() const
Definition: ReservoirCouplingMaster.hpp:87
char ** getArgv() const
Definition: ReservoirCouplingMaster.hpp:90
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:225
std::size_t numSlaveGroups(unsigned int index)
ReservoirCoupling::Logger & logger()
Definition: ReservoirCouplingMaster.hpp:151
void resizeNextReportDates(int size)
void maybeReceiveActivationHandshakeFromSlaves(double current_time)
void sendNextTimeStepToSlaves(double next_time_step)
Definition: ReservoirCouplingMaster.hpp:169
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
Definition: ReservoirCouplingMaster.hpp:219
void setEffectiveGCW(const std::string &group_name, int value)
Definition: ReservoirCouplingMaster.hpp:78
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....
void addSlaveCommunicator(MPI_Comm comm)
Definition: ReservoirCouplingMaster.hpp:55
void addSlaveStartDate(std::time_t date)
Definition: ReservoirCouplingMaster.hpp:60
bool activated()
Definition: ReservoirCouplingMaster.hpp:54
void initStartOfReportStep(int report_step_idx)
const Parallel::Communication & getComm() const
Definition: ReservoirCouplingMaster.hpp:91
ReservoirCoupling::Logger & logger() const
Definition: ReservoirCouplingMaster.hpp:152
int getSlaveIdx(const std::string &slave_name) const
std::size_t numActivatedSlaves() const
void setSlaveNextReportTimeOffset(int index, double offset)
void addSlaveActivationDate(double date)
Definition: ReservoirCouplingMaster.hpp:59
void clearDeferredLogger()
Definition: ReservoirCouplingMaster.hpp:61
bool needsSlaveDataReceive() const
Check if the master needs to receive production data from the slaves.
Scalar getMasterGroupRate(const std::string &group_name, ReservoirCoupling::Phase phase, ReservoirCoupling::RateKind kind) const
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCouplingMaster.hpp:206
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. Call at the start of each master-group constraint calculation before...
Definition: ReservoirCouplingMaster.hpp:85
const std::string & getSlaveName(int index) const
Definition: ReservoirCouplingMaster.hpp:130
void sendMasterGroupNodePressuresToSlave(std::size_t slave_idx, const std::vector< MasterGroupNodePressure > &pressures) const
Send master-computed network-leaf node pressures to a slave.
const Potentials & getSlaveGroupPotentials(const std::string &master_group_name)
ReservoirCouplingMaster(const Parallel::Communication &comm, const Schedule &schedule, int argc, char **argv)
void addSlaveName(const std::string &name)
Definition: ReservoirCouplingMaster.hpp:58
const double * getSlaveActivationDates() const
Definition: ReservoirCouplingMaster.hpp:123
void sendNumGroupConstraintsToSlave(std::size_t slave_idx, std::size_t num_injection_targets, std::size_t num_production_constraints) const
std::map< std::string, std::string > & getMasterGroupToSlaveNameMap()
Definition: ReservoirCouplingMaster.hpp:118
void sendDontTerminateSignalToSlaves()
Send "don't terminate" signal (value=0) to all active slaves.
const Schedule & schedule() const
Definition: ReservoirCouplingMaster.hpp:168
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:122
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:158
const double * getSlaveStartDates()
Definition: ReservoirCouplingMaster.hpp:132
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:74
void setSlaveActivationDate(int index, double date)
Definition: ReservoirCouplingMaster.hpp:216
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:124
double getSlaveStartDate(int index) const
Definition: ReservoirCouplingMaster.hpp:131
const std::vector< std::string > & getMasterGroupNamesForSlave(std::size_t slave_idx) const
Get the master group names associated with a slave reservoir by index.
double getSimulationStartDate() const
Definition: ReservoirCouplingMaster.hpp:121
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:218
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:88
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
RateKind
Selects which kind of rate to retrieve from slave group data.
Definition: ReservoirCoupling.hpp:171
MessageTag
Definition: ReservoirCoupling.hpp:134
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:159
Definition: blackoilbioeffectsmodules.hh:45
Definition: ReservoirCoupling.hpp:238
Master-computed network-leaf node pressure for a single master group.
Definition: ReservoirCoupling.hpp:281
Definition: ReservoirCoupling.hpp:190
Definition: ReservoirCoupling.hpp:248
Utility class for comparing double values representing epoch dates or elapsed time.
Definition: ReservoirCoupling.hpp:322
Definition: ReservoirCoupling.hpp:214