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#endif
31
32namespace Opm {
33
34#ifndef RESERVOIR_COUPLING_ENABLED
35// Forward declarations for non-MPI builds
36template <class Scalar> class ReservoirCouplingMaster;
37template <class Scalar> class ReservoirCouplingSlave;
38#endif
39
40namespace ReservoirCoupling {
41
52template <class Scalar>
53class Proxy {
54public:
55 Proxy() = default;
56
57 // Copyable and movable (just holds non-owning pointers)
58 Proxy(const Proxy&) = default;
59 Proxy& operator=(const Proxy&) = default;
60 Proxy(Proxy&&) noexcept = default;
61 Proxy& operator=(Proxy&&) noexcept = default;
62
63#ifdef RESERVOIR_COUPLING_ENABLED
64 // === Mode Queries ===
65
67 bool isEnabled() const noexcept { return master_ || slave_; }
68
70 bool isMaster() const noexcept { return master_ != nullptr; }
71
73 bool isSlave() const noexcept { return slave_ != nullptr; }
74
75 // === Setters (called during init, after construction) ===
76
78 void setMaster(ReservoirCouplingMaster<Scalar>* master) {
79 master_ = master;
80 slave_ = nullptr;
81 }
82
84 void setSlave(ReservoirCouplingSlave<Scalar>* slave) {
85 slave_ = slave;
86 master_ = nullptr;
87 }
88
89 // === Pointer Access ===
90
92 ReservoirCouplingMaster<Scalar>* masterPtr() noexcept { return master_; }
93 const ReservoirCouplingMaster<Scalar>* masterPtr() const noexcept { return master_; }
94
96 ReservoirCouplingSlave<Scalar>* slavePtr() noexcept { return slave_; }
97 const ReservoirCouplingSlave<Scalar>* slavePtr() const noexcept { return slave_; }
98
99 // === Reference Access (caller must ensure correct mode) ===
100
102 ReservoirCouplingMaster<Scalar>& master() { return *master_; }
103 const ReservoirCouplingMaster<Scalar>& master() const { return *master_; }
104
106 ReservoirCouplingSlave<Scalar>& slave() { return *slave_; }
107 const ReservoirCouplingSlave<Scalar>& slave() const { return *slave_; }
108
109private:
110 ReservoirCouplingMaster<Scalar>* master_ = nullptr;
111 ReservoirCouplingSlave<Scalar>* slave_ = nullptr;
112
113#else // !RESERVOIR_COUPLING_ENABLED
114
115 // === Mode Queries (always false in non-MPI builds) ===
116
117 bool isEnabled() const noexcept { return false; }
118 bool isMaster() const noexcept { return false; }
119 bool isSlave() const noexcept { return false; }
120
121 // Stub implementations for non-MPI builds.
122 // These should never be called since isMaster()/isSlave() always return false.
123
125 throw std::logic_error("ReservoirCoupling::Proxy::setMaster() called in non-MPI build");
126 }
127
129 throw std::logic_error("ReservoirCoupling::Proxy::setSlave() called in non-MPI build");
130 }
131
132 ReservoirCouplingMaster<Scalar>* masterPtr() noexcept { return nullptr; }
133 const ReservoirCouplingMaster<Scalar>* masterPtr() const noexcept { return nullptr; }
134
135 ReservoirCouplingSlave<Scalar>* slavePtr() noexcept { return nullptr; }
136 const ReservoirCouplingSlave<Scalar>* slavePtr() const noexcept { return nullptr; }
137
139 throw std::logic_error("ReservoirCoupling::Proxy::master() called in non-MPI build");
140 }
142 throw std::logic_error("ReservoirCoupling::Proxy::master() called in non-MPI build");
143 }
144
146 throw std::logic_error("ReservoirCoupling::Proxy::slave() called in non-MPI build");
147 }
149 throw std::logic_error("ReservoirCoupling::Proxy::slave() called in non-MPI build");
150 }
151
152#endif // RESERVOIR_COUPLING_ENABLED
153};
154
155} // namespace ReservoirCoupling
156} // namespace Opm
157
158#endif // OPM_RESCOUP_PROXY_HPP
Thin proxy for reservoir coupling master/slave pointers.
Definition: RescoupProxy.hpp:53
ReservoirCouplingMaster< Scalar > * masterPtr() noexcept
Definition: RescoupProxy.hpp:132
Proxy(Proxy &&) noexcept=default
Proxy & operator=(const Proxy &)=default
void setMaster(ReservoirCouplingMaster< Scalar > *)
Definition: RescoupProxy.hpp:124
const ReservoirCouplingMaster< Scalar > & master() const
Definition: RescoupProxy.hpp:141
Proxy(const Proxy &)=default
void setSlave(ReservoirCouplingSlave< Scalar > *)
Definition: RescoupProxy.hpp:128
ReservoirCouplingSlave< Scalar > * slavePtr() noexcept
Definition: RescoupProxy.hpp:135
ReservoirCouplingSlave< Scalar > & slave()
Definition: RescoupProxy.hpp:145
bool isEnabled() const noexcept
Definition: RescoupProxy.hpp:117
ReservoirCouplingMaster< Scalar > & master()
Definition: RescoupProxy.hpp:138
bool isMaster() const noexcept
Definition: RescoupProxy.hpp:118
const ReservoirCouplingSlave< Scalar > & slave() const
Definition: RescoupProxy.hpp:148
const ReservoirCouplingSlave< Scalar > * slavePtr() const noexcept
Definition: RescoupProxy.hpp:136
const ReservoirCouplingMaster< Scalar > * masterPtr() const noexcept
Definition: RescoupProxy.hpp:133
bool isSlave() const noexcept
Definition: RescoupProxy.hpp:119
Definition: ReservoirCouplingMaster.hpp:38
Definition: ReservoirCouplingSlave.hpp:40
Definition: blackoilbioeffectsmodules.hh:43