RescoupProxy.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 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_RESCOUP_PROXY_HPP
21#define OPM_RESCOUP_PROXY_HPP
22
24#ifdef RESERVOIR_COUPLING_ENABLED
28#else
29#include <stdexcept>
30#include <string>
31#endif
32
33namespace Opm {
34
35#ifndef RESERVOIR_COUPLING_ENABLED
36// Forward declarations for non-MPI builds
37template <class Scalar> class ReservoirCouplingMaster;
38template <class Scalar> class ReservoirCouplingSlave;
39#endif
40
41namespace ReservoirCoupling {
42
53template <class Scalar>
54class Proxy {
55public:
56 Proxy() = default;
57
58 // Copyable and movable (just holds non-owning pointers)
59 Proxy(const Proxy&) = default;
60 Proxy& operator=(const Proxy&) = default;
61 Proxy(Proxy&&) noexcept = default;
62 Proxy& operator=(Proxy&&) noexcept = default;
63
64#ifdef RESERVOIR_COUPLING_ENABLED
65 // === Mode Queries ===
66
68 bool isEnabled() const noexcept { return master_ || slave_; }
69
71 bool isMaster() const noexcept { return master_ != nullptr; }
72
74 bool isSlave() const noexcept { return slave_ != nullptr; }
75
76 // === Setters (called during init, after construction) ===
77
79 void setMaster(ReservoirCouplingMaster<Scalar>* master) {
80 master_ = master;
81 slave_ = nullptr;
82 }
83
85 void setSlave(ReservoirCouplingSlave<Scalar>* slave) {
86 slave_ = slave;
87 master_ = nullptr;
88 }
89
90 // === Pointer Access ===
91
93 ReservoirCouplingMaster<Scalar>* masterPtr() noexcept { return master_; }
94 const ReservoirCouplingMaster<Scalar>* masterPtr() const noexcept { return master_; }
95
97 ReservoirCouplingSlave<Scalar>* slavePtr() noexcept { return slave_; }
98 const ReservoirCouplingSlave<Scalar>* slavePtr() const noexcept { return slave_; }
99
100 // === Reference Access (caller must ensure correct mode) ===
101
103 ReservoirCouplingMaster<Scalar>& master() { return *master_; }
104 const ReservoirCouplingMaster<Scalar>& master() const { return *master_; }
105
107 ReservoirCouplingSlave<Scalar>& slave() { return *slave_; }
108 const ReservoirCouplingSlave<Scalar>& slave() const { return *slave_; }
109
110 // === Facade Methods (safe to call regardless of mode) ===
111
115 bool isMasterGroup(const std::string& group_name) const {
116 return master_ && master_->isMasterGroup(group_name);
117 }
118
119private:
120 ReservoirCouplingMaster<Scalar>* master_ = nullptr;
121 ReservoirCouplingSlave<Scalar>* slave_ = nullptr;
122
123#else // !RESERVOIR_COUPLING_ENABLED
124
125 // === Mode Queries (always false in non-MPI builds) ===
126
127 bool isEnabled() const noexcept { return false; }
128 bool isMaster() const noexcept { return false; }
129 bool isSlave() const noexcept { return false; }
130
131 // Stub implementations for non-MPI builds.
132 // These should never be called since isMaster()/isSlave() always return false.
133
135 throw std::logic_error("ReservoirCoupling::Proxy::setMaster() called in non-MPI build");
136 }
137
139 throw std::logic_error("ReservoirCoupling::Proxy::setSlave() called in non-MPI build");
140 }
141
142 ReservoirCouplingMaster<Scalar>* masterPtr() noexcept { return nullptr; }
143 const ReservoirCouplingMaster<Scalar>* masterPtr() const noexcept { return nullptr; }
144
145 ReservoirCouplingSlave<Scalar>* slavePtr() noexcept { return nullptr; }
146 const ReservoirCouplingSlave<Scalar>* slavePtr() const noexcept { return nullptr; }
147
149 throw std::logic_error("ReservoirCoupling::Proxy::master() called in non-MPI build");
150 }
152 throw std::logic_error("ReservoirCoupling::Proxy::master() called in non-MPI build");
153 }
154
156 throw std::logic_error("ReservoirCoupling::Proxy::slave() called in non-MPI build");
157 }
159 throw std::logic_error("ReservoirCoupling::Proxy::slave() called in non-MPI build");
160 }
161
162 // === Facade Methods (always return false/no-op in non-MPI builds) ===
163
164 bool isMasterGroup(const std::string& /*group_name*/) const noexcept {
165 return false;
166 }
167#endif // !RESERVOIR_COUPLING_ENABLED
168};
169
170} // namespace ReservoirCoupling
171} // namespace Opm
172
173#endif // OPM_RESCOUP_PROXY_HPP
Thin proxy for reservoir coupling master/slave pointers.
Definition: RescoupProxy.hpp:54
ReservoirCouplingMaster< Scalar > * masterPtr() noexcept
Definition: RescoupProxy.hpp:142
Proxy(Proxy &&) noexcept=default
Proxy & operator=(const Proxy &)=default
bool isMasterGroup(const std::string &) const noexcept
Definition: RescoupProxy.hpp:164
void setMaster(ReservoirCouplingMaster< Scalar > *)
Definition: RescoupProxy.hpp:134
const ReservoirCouplingMaster< Scalar > & master() const
Definition: RescoupProxy.hpp:151
Proxy(const Proxy &)=default
void setSlave(ReservoirCouplingSlave< Scalar > *)
Definition: RescoupProxy.hpp:138
ReservoirCouplingSlave< Scalar > * slavePtr() noexcept
Definition: RescoupProxy.hpp:145
ReservoirCouplingSlave< Scalar > & slave()
Definition: RescoupProxy.hpp:155
bool isEnabled() const noexcept
Definition: RescoupProxy.hpp:127
ReservoirCouplingMaster< Scalar > & master()
Definition: RescoupProxy.hpp:148
bool isMaster() const noexcept
Definition: RescoupProxy.hpp:128
const ReservoirCouplingSlave< Scalar > & slave() const
Definition: RescoupProxy.hpp:158
const ReservoirCouplingSlave< Scalar > * slavePtr() const noexcept
Definition: RescoupProxy.hpp:146
const ReservoirCouplingMaster< Scalar > * masterPtr() const noexcept
Definition: RescoupProxy.hpp:143
bool isSlave() const noexcept
Definition: RescoupProxy.hpp:129
Definition: ReservoirCouplingMaster.hpp:38
Definition: ReservoirCouplingSlave.hpp:40
Definition: blackoilbioeffectsmodules.hh:43