opm-simulators
Loading...
Searching...
No Matches
ReservoirCouplingMaster.hpp
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
23#include <opm/simulators/utils/ParallelCommunication.hpp>
24#include <opm/simulators/flow/rescoup/ReservoirCoupling.hpp>
25#include <opm/simulators/flow/rescoup/ReservoirCouplingMasterReportStep.hpp>
26#include <opm/simulators/flow/rescoup/ReservoirCouplingTimeStepper.hpp>
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>
38class ReservoirCouplingMaster {
39public:
40 using MessageTag = ReservoirCoupling::MessageTag;
41 using Seconds = ReservoirCoupling::Seconds;
43 using SlaveGroupProductionData = ReservoirCoupling::SlaveGroupProductionData<Scalar>;
44 using InjectionGroupTarget = ReservoirCoupling::InjectionGroupTarget<Scalar>;
45 using ProductionGroupConstraints = ReservoirCoupling::ProductionGroupConstraints<Scalar>;
46
47 ReservoirCouplingMaster(
48 const Parallel::Communication &comm,
49 const Schedule &schedule,
50 int argc, char **argv
51 );
52
53 bool activated() { return this->activated_; }
54 void addSlaveCommunicator(MPI_Comm comm) {
55 this->master_slave_comm_.push_back(comm);
56 }
57 void addSlaveName(const std::string &name) { this->slave_names_.push_back(name); }
58 void addSlaveActivationDate(double date) { this->slave_activation_dates_.push_back(date); }
59 void addSlaveStartDate(std::time_t date) { this->slave_start_dates_.push_back(date); }
60 void clearDeferredLogger() { logger_.clearDeferredLogger(); }
61 double getActivationDate() const { return this->activation_date_; }
62 int getArgc() const { return this->argc_; }
63 char *getArgv(int index) const { return this->argv_[index]; }
64 char **getArgv() const { return this->argv_; }
65 const Parallel::Communication &getComm() const { return this->comm_; }
78 const std::vector<std::string>& getMasterGroupNamesForSlave(std::size_t slave_idx) const;
86 const std::string &slave_name, const std::string &master_group_name) const;
87 Scalar getMasterGroupRate(
88 const std::string &group_name, ReservoirCoupling::Phase phase, ReservoirCoupling::RateKind kind) const;
89 std::map<std::string, std::string>& getMasterGroupToSlaveNameMap() {
90 return this->master_group_slave_names_;
91 }
92 double getSimulationStartDate() const { return this->schedule_.getStartTime(); }
93 double getSlaveActivationDate(int index) const { return this->slave_activation_dates_[index]; }
94 const double *getSlaveActivationDates() const { return this->slave_activation_dates_.data(); }
95 MPI_Comm getSlaveComm(int index) const { return this->master_slave_comm_[index]; }
96 std::map<std::string, std::vector<std::string>> &getSlaveNameToMasterGroupsMap() {
97 return this->slave_name_to_master_groups_map_;
98 }
99 const Potentials& getSlaveGroupPotentials(const std::string &master_group_name);
100 int getSlaveIdx(const std::string &slave_name) const;
101 const std::string &getSlaveName(int index) const { return this->slave_names_[index]; }
102 double getSlaveStartDate(int index) const { return this->slave_start_dates_[index]; }
103 const double *getSlaveStartDates() { return this->slave_start_dates_.data(); }
104 void initStartOfReportStep(int report_step_idx);
105 void initTimeStepping();
106 bool isFirstSubstepOfSyncTimestep() const;
107 bool isMasterGroup(const std::string &group_name) const;
108 ReservoirCoupling::Logger& logger() { return this->logger_; }
109 ReservoirCoupling::Logger& logger() const { return this->logger_; }
110 void maybeActivate(int report_step);
111 void maybeReceiveActivationHandshakeFromSlaves(double current_time);
112 double maybeChopSubStep(double suggested_timestep, double current_time) const;
113 void maybeSpawnSlaveProcesses(int report_step);
114 std::size_t numSlaveGroups(unsigned int index);
115 std::size_t numSlaves() const { return this->numSlavesStarted(); }
116 std::size_t numSlavesStarted() const;
117 std::size_t numActivatedSlaves() const;
118 void rebuildSlaveIdxToMasterGroupsVector();
119 void receiveNextReportDateFromSlaves();
120 void receiveProductionDataFromSlaves();
121 void receiveInjectionDataFromSlaves();
122 void resizeNextReportDates(int size);
123 void resizeSlaveActivationDates(int size) { this->slave_activation_dates_.resize(size); }
124 void resizeSlaveStartDates(int size) { this->slave_start_dates_.resize(size); }
125 const Schedule& schedule() const { return this->schedule_; }
126 void sendNextTimeStepToSlaves(double next_time_step) {
127 this->time_stepper_->sendNextTimeStepToSlaves(next_time_step);
128 }
129 void sendInjectionTargetsToSlave(
130 std::size_t slave_idx,
131 const std::vector<InjectionGroupTarget>& injection_targets
132 ) const;
133 void sendNumGroupConstraintsToSlave(
134 std::size_t slave_idx,
135 std::size_t num_injection_targets,
136 std::size_t num_production_constraints
137 ) const;
138 void sendProductionConstraintsToSlave(
139 std::size_t slave_idx,
140 const std::vector<ProductionGroupConstraints>& production_constraints
141 ) const;
142 void setDeferredLogger(DeferredLogger *deferred_logger) {
143 this->logger_.setDeferredLogger(deferred_logger);
144 }
145 void setFirstSubstepOfSyncTimestep(bool value);
146 // These are currently only used for unit testing
147 void setSlaveActivationDate(int index, double date) { this->slave_activation_dates_[index] = date; }
148 void setSlaveNextReportTimeOffset(int index, double offset);
149 void setSlaveStartDate(int index, std::time_t date) { this->slave_start_dates_[index] = date; }
150 bool slaveIsActivated(int index) const { return this->slave_activation_status_[index] != 0; }
151 void updateMasterGroupNameOrderMap(
152 const std::string& slave_name, const std::map<std::string, std::size_t>& master_group_map);
153
161
172
173private:
174 double getMasterActivationDate_() const;
175
176 const Parallel::Communication &comm_;
177 const Schedule& schedule_;
178 int argc_;
179 char **argv_;
180 // Whether the master process has activated the reservoir coupling
181 bool activated_{false};
182 // NOTE: MPI_Comm is just an integer handle, so we can just copy it into the vector
183 std::vector<MPI_Comm> master_slave_comm_; // MPI communicators for the slave processes
184 std::vector<std::string> slave_names_;
185 // The start dates are in whole seconds since the epoch. We use a double to store the value
186 // since both schedule_.getStartTime() and schedule_.stepLength(report_step) returns
187 // a double value representing whole seconds.
188 // However, note that schedule_[report_step].start_time() returns a time_point
189 // which can include milliseconds. The double values are also convenient when we need to
190 // to add fractions of seconds for sub steps to the start date.
191 std::vector<double> slave_start_dates_;
192 // The activation dates are in whole seconds since the epoch.
193 std::vector<double> slave_activation_dates_;
194 double activation_date_{0.0}; // The date when SLAVES is encountered in the schedule
195 // A mapping from a slave name to the master group name order used when slaves send
196 // potentials to the master process.
197 std::map<std::string, std::map<std::string, std::size_t>> master_group_name_order_;
198 mutable ReservoirCoupling::Logger logger_;
199 // Whether the slave has activated. Unfortunatley, we cannot use std::vector<bool> since
200 // it is not supported to get a pointer to the underlying array of bools needed
201 // with MPI broadcast().
202 std::vector<std::uint8_t> slave_activation_status_;
203 // A mapping from master group names to slave names
204 std::map<std::string, std::string> master_group_slave_names_;
205 // A mapping from slave names to master group names
206 // NOTE: The order of the master groups in the vector is important,
207 // as the slaves will communicate the indices of the master groups in
208 // the vector instead of the group names themselves.
209 // NOTE: This map is created by ReservoirCouplingSpawnSlaves.cpp
210 std::map<std::string, std::vector<std::string>> slave_name_to_master_groups_map_;
211 // Direct index-based lookup for performance optimization (O(1) instead of O(log n))
212 // This vector is populated in parallel with slave_name_to_master_groups_map_
213 // and maintains the same ordering as slave_names_ vector for consistent indexing
214 std::vector<std::vector<std::string>> slave_idx_to_master_groups_;
215 // Stores data that changes for a single report step or for timesteps within a report step.
216 std::unique_ptr<ReservoirCouplingMasterReportStep<Scalar>> report_step_data_{nullptr};
217 // Handles time stepping for the master and slaves
218 std::unique_ptr<ReservoirCouplingTimeStepper<Scalar>> time_stepper_{nullptr};
219};
220
221} // namespace Opm
222
223#endif // OPM_RESERVOIR_COUPLING_MASTER_HPP
Definition DeferredLogger.hpp:57
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.
Definition ReservoirCouplingMaster.cpp:91
void sendTerminateAndDisconnect()
Send terminate signal to all active slaves and disconnect intercommunicators.
Definition ReservoirCouplingMaster.cpp:432
void sendDontTerminateSignalToSlaves()
Send "don't terminate" signal (value=0) to all active slaves.
Definition ReservoirCouplingMaster.cpp:376
const std::vector< std::string > & getMasterGroupNamesForSlave(std::size_t slave_idx) const
Get the master group names associated with a slave reservoir by index.
Definition ReservoirCouplingMaster.cpp:72
Definition ReservoirCoupling.hpp:42
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:45
Definition ReservoirCoupling.hpp:235
Definition ReservoirCoupling.hpp:187
Definition ReservoirCoupling.hpp:245
Utility class for comparing double values representing epoch dates or elapsed time.
Definition ReservoirCoupling.hpp:305
Definition ReservoirCoupling.hpp:211