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:
45 const Parallel::Communication &comm,
46 const Schedule &schedule,
47 int argc, char **argv
48 );
49
50 bool activated() { return this->activated_; }
51 void addSlaveCommunicator(MPI_Comm comm) {
52 this->master_slave_comm_.push_back(comm);
53 }
54 void addSlaveName(const std::string &name) { this->slave_names_.push_back(name); }
55 void addSlaveActivationDate(double date) { this->slave_activation_dates_.push_back(date); }
56 void addSlaveStartDate(std::time_t date) { this->slave_start_dates_.push_back(date); }
58 double getActivationDate() const { return this->activation_date_; }
59 int getArgc() const { return this->argc_; }
60 char *getArgv(int index) const { return this->argv_[index]; }
61 char **getArgv() const { return this->argv_; }
62 const Parallel::Communication &getComm() const { return this->comm_; }
63 ReservoirCoupling::Logger& getLogger() { return this->logger_; }
76 const std::vector<std::string>& getMasterGroupNamesForSlave(std::size_t slave_idx) const;
84 const std::string &slave_name, const std::string &master_group_name) const;
85 std::map<std::string, std::string>& getMasterGroupToSlaveNameMap() {
86 return this->master_group_slave_names_;
87 }
88 double getSlaveActivationDate(int index) const { return this->slave_activation_dates_[index]; }
89 const double *getSlaveActivationDates() const { return this->slave_activation_dates_.data(); }
90 std::map<std::string, std::vector<std::string>> &getSlaveNameToMasterGroupsMap() {
91 return this->slave_name_to_master_groups_map_;
92 }
93 double getSimulationStartDate() const { return this->schedule_.getStartTime(); }
94 MPI_Comm getSlaveComm(int index) const { return this->master_slave_comm_[index]; }
95 const Potentials& getSlaveGroupPotentials(const std::string &master_group_name);
96 const std::string &getSlaveName(int index) const { return this->slave_names_[index]; }
97 double getSlaveStartDate(int index) const { return this->slave_start_dates_[index]; }
98 const double *getSlaveStartDates() { return this->slave_start_dates_.data(); }
99 void initStartOfReportStep(int report_step_idx);
101 bool isMasterGroup(const std::string &group_name) const;
102 void maybeActivate(int report_step);
104 double maybeChopSubStep(double suggested_timestep, double current_time) const;
105 void maybeSpawnSlaveProcesses(int report_step);
106 std::size_t numSlaveGroups(unsigned int index);
107 std::size_t numSlavesStarted() const;
112 void resizeNextReportDates(int size);
113 void resizeSlaveActivationDates(int size) { this->slave_activation_dates_.resize(size); }
114 void resizeSlaveStartDates(int size) { this->slave_start_dates_.resize(size); }
115 const Schedule& schedule() const { return this->schedule_; }
116 void sendNextTimeStepToSlaves(double next_time_step) {
117 this->time_stepper_->sendNextTimeStepToSlaves(next_time_step);
118 }
119 void setDeferredLogger(DeferredLogger *deferred_logger) {
120 this->logger_.setDeferredLogger(deferred_logger);
121 }
122 // These are currently only used for unit testing
123 void setSlaveActivationDate(int index, double date) { this->slave_activation_dates_[index] = date; }
124 void setSlaveNextReportTimeOffset(int index, double offset);
125 void setSlaveStartDate(int index, std::time_t date) { this->slave_start_dates_[index] = date; }
126 bool slaveIsActivated(int index) const { return this->slave_activation_status_[index] != 0; }
128 const std::string& slave_name, const std::map<std::string, std::size_t>& master_group_map);
129
130private:
131 double getMasterActivationDate_() const;
132
133 const Parallel::Communication &comm_;
134 const Schedule& schedule_;
135 int argc_;
136 char **argv_;
137 // Whether the master process has activated the reservoir coupling
138 bool activated_{false};
139 // NOTE: MPI_Comm is just an integer handle, so we can just copy it into the vector
140 std::vector<MPI_Comm> master_slave_comm_; // MPI communicators for the slave processes
141 std::vector<std::string> slave_names_;
142 // The start dates are in whole seconds since the epoch. We use a double to store the value
143 // since both schedule_.getStartTime() and schedule_.stepLength(report_step) returns
144 // a double value representing whole seconds.
145 // However, note that schedule_[report_step].start_time() returns a time_point
146 // which can include milliseconds. The double values are also convenient when we need to
147 // to add fractions of seconds for sub steps to the start date.
148 std::vector<double> slave_start_dates_;
149 // The activation dates are in whole seconds since the epoch.
150 std::vector<double> slave_activation_dates_;
151 double activation_date_{0.0}; // The date when SLAVES is encountered in the schedule
152 // A mapping from a slave name to the master group name order used when slaves send
153 // potentials to the master process.
154 std::map<std::string, std::map<std::string, std::size_t>> master_group_name_order_;
155 ReservoirCoupling::Logger logger_;
156 // Whether the slave has activated. Unfortunatley, we cannot use std::vector<bool> since
157 // it is not supported to get a pointer to the underlying array of bools needed
158 // with MPI broadcast().
159 std::vector<std::uint8_t> slave_activation_status_;
160 // A mapping from master group names to slave names
161 std::map<std::string, std::string> master_group_slave_names_;
162 // A mapping from slave names to master group names
163 // NOTE: The order of the master groups in the vector is important,
164 // as the slaves will communicate the indices of the master groups in
165 // the vector instead of the group names themselves.
166 // NOTE: This map is created by ReservoirCouplingSpawnSlaves.cpp
167 std::map<std::string, std::vector<std::string>> slave_name_to_master_groups_map_;
168 // Direct index-based lookup for performance optimization (O(1) instead of O(log n))
169 // This vector is populated in parallel with slave_name_to_master_groups_map_
170 // and maintains the same ordering as slave_names_ vector for consistent indexing
171 std::vector<std::vector<std::string>> slave_idx_to_master_groups_;
172 // Stores data that changes for a single report step or for timesteps within a report step.
173 std::unique_ptr<ReservoirCouplingMasterReportStep<Scalar>> report_step_data_{nullptr};
174 // Handles time stepping for the master and slaves
175 std::unique_ptr<ReservoirCouplingTimeStepper<Scalar>> time_stepper_{nullptr};
176};
177
178} // namespace Opm
179
180#endif // OPM_RESERVOIR_COUPLING_MASTER_HPP
Definition: DeferredLogger.hpp:57
Definition: ReservoirCoupling.hpp:36
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCoupling.hpp:42
void clearDeferredLogger()
Definition: ReservoirCoupling.hpp:39
Definition: ReservoirCouplingMaster.hpp:38
void resizeSlaveActivationDates(int size)
Definition: ReservoirCouplingMaster.hpp:113
void resizeSlaveStartDates(int size)
Definition: ReservoirCouplingMaster.hpp:114
std::map< std::string, std::vector< std::string > > & getSlaveNameToMasterGroupsMap()
Definition: ReservoirCouplingMaster.hpp:90
char * getArgv(int index) const
Definition: ReservoirCouplingMaster.hpp:60
double getActivationDate() const
Definition: ReservoirCouplingMaster.hpp:58
char ** getArgv() const
Definition: ReservoirCouplingMaster.hpp:61
void maybeActivate(int report_step)
std::size_t numSlaveGroups(unsigned int index)
void resizeNextReportDates(int size)
void maybeReceiveActivationHandshakeFromSlaves(double current_time)
void sendNextTimeStepToSlaves(double next_time_step)
Definition: ReservoirCouplingMaster.hpp:116
std::size_t numSlavesStarted() const
double maybeChopSubStep(double suggested_timestep, double current_time) const
bool slaveIsActivated(int index) const
Definition: ReservoirCouplingMaster.hpp:126
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:51
void addSlaveStartDate(std::time_t date)
Definition: ReservoirCouplingMaster.hpp:56
bool activated()
Definition: ReservoirCouplingMaster.hpp:50
void initStartOfReportStep(int report_step_idx)
const Parallel::Communication & getComm() const
Definition: ReservoirCouplingMaster.hpp:62
void setSlaveNextReportTimeOffset(int index, double offset)
void addSlaveActivationDate(double date)
Definition: ReservoirCouplingMaster.hpp:55
void clearDeferredLogger()
Definition: ReservoirCouplingMaster.hpp:57
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCouplingMaster.hpp:119
void maybeSpawnSlaveProcesses(int report_step)
const std::string & getSlaveName(int index) const
Definition: ReservoirCouplingMaster.hpp:96
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:54
const double * getSlaveActivationDates() const
Definition: ReservoirCouplingMaster.hpp:89
std::map< std::string, std::string > & getMasterGroupToSlaveNameMap()
Definition: ReservoirCouplingMaster.hpp:85
const Schedule & schedule() const
Definition: ReservoirCouplingMaster.hpp:115
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:88
const double * getSlaveStartDates()
Definition: ReservoirCouplingMaster.hpp:98
void setSlaveActivationDate(int index, double date)
Definition: ReservoirCouplingMaster.hpp:123
ReservoirCoupling::Logger & getLogger()
Definition: ReservoirCouplingMaster.hpp:63
MPI_Comm getSlaveComm(int index) const
Definition: ReservoirCouplingMaster.hpp:94
double getSlaveStartDate(int index) const
Definition: ReservoirCouplingMaster.hpp:97
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:93
void setSlaveStartDate(int index, std::time_t date)
Definition: ReservoirCouplingMaster.hpp:125
bool isMasterGroup(const std::string &group_name) const
int getArgc() const
Definition: ReservoirCouplingMaster.hpp:59
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
MessageTag
Definition: ReservoirCoupling.hpp:121
Definition: blackoilbioeffectsmodules.hh:43
Definition: ReservoirCoupling.hpp:160
Utility class for comparing double values representing epoch dates or elapsed time.
Definition: ReservoirCoupling.hpp:226
Definition: ReservoirCoupling.hpp:184