ReservoirCouplingSlave.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_SLAVE_HPP
21#define OPM_RESERVOIR_COUPLING_SLAVE_HPP
22
25#include <opm/input/eclipse/Schedule/Schedule.hpp>
28#include <opm/common/OpmLog/OpmLog.hpp>
29
30#include <mpi.h>
31
32#include <vector>
33
34namespace Opm {
35
36template <class Scalar>
37class ReservoirCouplingSlaveReportStep;
38
39template <class Scalar>
41public:
49
51 const Parallel::Communication &comm, const Schedule &schedule, const SimulatorTimer &timer
52 );
53 bool activated() const { return activated_; }
55 const Parallel::Communication& getComm() const { return comm_; }
56 MPI_Comm getMasterComm() const { return slave_master_comm_; }
57 const std::string& getSlaveName() const { return slave_name_; }
58 const std::map<std::string, std::string>& getSlaveToMasterGroupNameMap() const {
59 return slave_to_master_group_map_; }
62 bool hasMasterInjectionTarget(const std::string& gname, const Phase phase) const;
63
66 bool hasMasterProductionLimits(const std::string& gname) const;
67
70 bool hasMasterProductionTarget(const std::string& gname) const;
73 bool isSlaveGroup(const std::string& group_name) const;
74 ReservoirCoupling::Logger& logger() { return this->logger_; }
75 ReservoirCoupling::Logger& logger() const { return this->logger_; }
78 std::pair<Scalar, Group::InjectionCMode> masterInjectionTarget(
79 const std::string& gname, const Phase phase) const;
80
83 const MasterProductionLimits& masterProductionLimits(const std::string& gname) const;
84
87 std::pair<Scalar, Group::ProductionCMode> masterProductionTarget(const std::string& gname) const;
88 void maybeActivate(int report_step);
89 std::size_t numSlaveGroups() const { return this->slave_group_order_.size(); }
91 std::pair<std::size_t, std::size_t> receiveNumGroupConstraintsFromMaster() const;
94 void receiveInjectionGroupTargetsFromMaster(std::size_t num_targets);
95
98 void receiveProductionGroupConstraintsFromMaster(std::size_t num_targets);
99
101 void sendInjectionDataToMaster(const std::vector<SlaveGroupInjectionData> &injection_data) const;
103 void sendProductionDataToMaster(const std::vector<SlaveGroupProductionData> &production_data) const;
104 void setDeferredLogger(DeferredLogger *deferred_logger) {
105 this->logger_.setDeferredLogger(deferred_logger);
106 }
108 const std::string& slaveGroupIdxToGroupName(std::size_t group_idx) const {
109 return this->slave_group_order_.at(group_idx);
110 }
111 bool terminated() const { return this->terminated_; }
112
122
133
134private:
135 void checkGrupSlavGroupNames_();
136 std::pair<double, bool> getGrupSlavActivationDateAndCheckHistoryMatchingMode_() const;
137 bool historyMatchingMode_() const { return this->history_matching_mode_; }
138 std::size_t numMasterGroups_() const { return this->slave_to_master_group_map_.size(); }
139 void receiveMasterGroupNamesFromMasterProcess_();
140 void receiveSlaveNameFromMasterProcess_();
141 void saveMasterGroupNamesAsMapAndEstablishOrder_(const std::vector<char>& group_names);
142 void sendActivationDateToMasterProcess_();
143 void sendActivationHandshakeToMasterProcess_() const;
144 void sendSimulationStartDateToMasterProcess_() const;
145
146 const Parallel::Communication &comm_;
147 const Schedule& schedule_;
148 const SimulatorTimer &timer_;
149 // MPI parent communicator for a slave process
150 MPI_Comm slave_master_comm_{MPI_COMM_NULL};
151 std::map<std::string, std::string> slave_to_master_group_map_;
152 bool activated_{false};
153 // True if the slave was terminated by the master process
154 bool terminated_{false};
155 // True if no GRUPMAST keyword in the master schedule and no GRUPSLAV keyword in the slave schedule
156 bool history_matching_mode_{false};
157 std::string slave_name_; // This is the slave name as defined in the master process
158 mutable ReservoirCoupling::Logger logger_;
159 // Order of the slave groups. A mapping from slave group index to slave group name.
160 // The indices are determined by the order the master process sends us the group names, see
161 // receiveMasterGroupNamesFromMasterProcess_()
162 // Later, the master process will send us group name indices, and not the group names themselves,
163 // so we use this mapping to recover the slave group names from the indices.
164 std::map<std::size_t, std::string> slave_group_order_;
165 // Stores data that changes for a single report step or for timesteps within a report step.
166 std::unique_ptr<ReservoirCouplingSlaveReportStep<Scalar>> report_step_data_{nullptr};
167};
168
169} // namespace Opm
170
171#endif // OPM_RESERVOIR_COUPLING_SLAVE_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: ReservoirCouplingSlave.hpp:40
const std::map< std::string, std::string > & getSlaveToMasterGroupNameMap() const
Definition: ReservoirCouplingSlave.hpp:58
ReservoirCouplingSlave(const Parallel::Communication &comm, const Schedule &schedule, const SimulatorTimer &timer)
const std::string & getSlaveName() const
Definition: ReservoirCouplingSlave.hpp:57
void setFirstSubstepOfSyncTimestep(bool value)
void sendProductionDataToMaster(const std::vector< SlaveGroupProductionData > &production_data) const
std::size_t numSlaveGroups() const
Definition: ReservoirCouplingSlave.hpp:89
bool isFirstSubstepOfSyncTimestep() const
std::pair< Scalar, Group::ProductionCMode > masterProductionTarget(const std::string &gname) const
Get the master-imposed production target and control mode for a group.
const Parallel::Communication & getComm() const
Definition: ReservoirCouplingSlave.hpp:55
bool isSlaveGroup(const std::string &group_name) const
std::pair< std::size_t, std::size_t > receiveNumGroupConstraintsFromMaster() const
const MasterProductionLimits & masterProductionLimits(const std::string &gname) const
Get the master-imposed per-rate-type production limits for a group.
void receiveInjectionGroupTargetsFromMaster(std::size_t num_targets)
Receive injection group targets from master and store them locally.
void receiveProductionGroupConstraintsFromMaster(std::size_t num_targets)
Receive production group constraints from master and store them locally.
void setDeferredLogger(DeferredLogger *deferred_logger)
Definition: ReservoirCouplingSlave.hpp:104
MPI_Comm getMasterComm() const
Definition: ReservoirCouplingSlave.hpp:56
void clearDeferredLogger()
Definition: ReservoirCouplingSlave.hpp:54
bool hasMasterInjectionTarget(const std::string &gname, const Phase phase) const
Check if a master-imposed injection target exists for a group and phase.
void sendNextReportDateToMasterProcess() const
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.
ReservoirCoupling::Logger & logger()
Definition: ReservoirCouplingSlave.hpp:74
bool maybeReceiveTerminateSignalFromMaster()
Blocking receive for terminate/continue signal from master.
const std::string & slaveGroupIdxToGroupName(std::size_t group_idx) const
Definition: ReservoirCouplingSlave.hpp:108
bool terminated() const
Definition: ReservoirCouplingSlave.hpp:111
void sendInjectionDataToMaster(const std::vector< SlaveGroupInjectionData > &injection_data) const
bool hasMasterProductionTarget(const std::string &gname) const
Check if a master-imposed production target exists for a group.
bool activated() const
Definition: ReservoirCouplingSlave.hpp:53
ReservoirCoupling::Logger & logger() const
Definition: ReservoirCouplingSlave.hpp:75
void maybeActivate(int report_step)
bool hasMasterProductionLimits(const std::string &gname) const
Check if master-imposed per-rate-type production limits exist for a group.
void receiveTerminateAndDisconnect()
Receive terminate signal from master and disconnect the intercommunicator.
typename ReservoirCoupling::MasterProductionLimits< Scalar > MasterProductionLimits
Definition: ReservoirCouplingSlave.hpp:47
Definition: SimulatorTimer.hpp:39
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
MessageTag
Definition: ReservoirCoupling.hpp:134
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:156
Definition: blackoilbioeffectsmodules.hh:45
Definition: ReservoirCoupling.hpp:235
Per-rate-type production limits received from master hierarchy. A value of -1 means no limit defined ...
Definition: ReservoirCoupling.hpp:263
Definition: ReservoirCoupling.hpp:187
Definition: ReservoirCoupling.hpp:245
Definition: ReservoirCoupling.hpp:229
Definition: ReservoirCoupling.hpp:211