Cantera  4.0.0a1
Loading...
Searching...
No Matches
ReactorDelegator.h
Go to the documentation of this file.
1//! @file ReactorDelegator.h
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#ifndef CT_REACTORDELEGATOR_H
7#define CT_REACTORDELEGATOR_H
8
9#include "Reactor.h"
13
14namespace Cantera
15{
16
17//! An abstract base class for providing access to protected capabilities
18//! Reactor objects from delegate methods, which would normally only be able to
19//! access public Reactor members.
20//!
21//! Actual implementations of these methods are found in the templated
22//! ReactorDelegator class. The purpose of this base class is so these methods
23//! can be accessed by casting a Reactor* to a ReactorAccessor* without needing
24//! to know the specific kind of Reactor at compilation time.
26{
27public:
28 //! Set the number of equations represented by this reactor
29 virtual void setNEq(size_t n) = 0;
30
31 //! Get the net rate of volume change (for example, from moving walls) [m^3/s]
32 virtual double expansionRate() const = 0;
33
34 //! Set the net rate of volume change (for example, from moving walls) [m^3/s]
35 virtual void setExpansionRate(double v) = 0;
36
37 //! Get the net heat transfer rate (for example, through walls) into the
38 //! reactor [W]. This value is initialized and calculated as part of
39 //! Reactor::evalWalls().
40 virtual double heatRate() const = 0;
41
42 //! Set the net heat transfer rate (for example, through walls) into the
43 //! reactor [W]. For a value set using this method to affect the calculations done
44 //! by Reactor::eval, this method should be called in either a "replace" or "after"
45 //! delegate for Reactor::evalWalls().
46 virtual void setHeatRate(double q) = 0;
47
48 //! @copydoc ReactorBase::surfaceProductionRates
49 virtual span<double> surfaceProductionRates() = 0;
50};
51
52//! Delegate methods of the Reactor class to external functions
53//! @ingroup reactorGroup
54template <class R>
55class ReactorDelegator : public Delegator, public R, public ReactorAccessor
56{
57public:
58 template <class... Args>
59 ReactorDelegator(Args&&... args)
60 : R(std::forward<Args>(args)...)
61 {
62 install("initialize", m_initialize, [this](double t0) { R::initialize(t0); });
63 install("getState", m_getState,
64 [this](span<double> y) {
65 R::getState(y);
66 });
67 install("updateState", m_updateState,
68 [this](span<double> y) {
69 R::updateState(y);
70 });
71 install("updateConnected", m_updateConnected,
72 [this](bool updatePressure) { R::updateConnected(updatePressure); });
73 install("eval", m_eval,
74 [this](double t, span<double> LHS, span<double> RHS) {
75 R::eval(t, LHS, RHS);
76 }
77 );
78 if constexpr (std::is_base_of<Reactor, R>::value) {
79 install("evalWalls", m_evalWalls, [this](double t) { R::evalWalls(t); });
80 }
81 install("componentName", m_componentName,
82 [this](size_t k) { return R::componentName(k); });
83 install("componentIndex", m_componentIndex,
84 [this](const string& nm) { return R::componentIndex(nm); });
85 }
86
87 // Overrides of Reactor methods
88
89 string type() const override {
90 return fmt::format("Extensible{}", R::type());
91 }
92
93 void initialize(double t0) override {
94 m_initialize(t0);
95 }
96
97 void getState(span<double> y) override {
98 checkArraySize("ReactorDelegator::getState", y.size(), R::m_nv);
99 m_getState(y);
100 }
101
102 void updateState(span<const double> y) override {
103 checkArraySize("ReactorDelegator::updateState", y.size(), R::m_nv);
104 m_updateState(stripConst(y));
105 }
106
107 void updateConnected(bool updatePressure) override {
108 m_updateConnected(updatePressure);
109 }
110
111 void eval(double t, span<double> LHS, span<double> RHS) override {
112 checkArraySize("ReactorDelegator::eval[LHS]", LHS.size(), R::m_nv);
113 checkArraySize("ReactorDelegator::eval[RHS]", RHS.size(), R::m_nv);
114 m_eval(t, LHS, RHS);
115 }
116
117 void evalWalls(double t) override {
118 if constexpr (std::is_base_of<Reactor, R>::value) {
119 m_evalWalls(t);
120 } else {
122 }
123 }
124
125 string componentName(size_t k) override {
126 return m_componentName(k);
127 }
128
129 size_t componentIndex(const string& nm) const override {
130 return m_componentIndex(nm);
131 }
132
133 // Public access to protected Reactor variables needed by derived classes
134
135 void setNEq(size_t n) override {
136 R::m_nv = n;
137 }
138
139 double expansionRate() const override {
140 if constexpr (std::is_base_of<Reactor, R>::value) {
141 return R::m_vdot;
142 } else {
143 throw NotImplementedError("ReactorDelegator::expansionRate",
144 "Expansion rate is undefined for reactors of type '{}'.", type());
145 }
146 }
147
148 void setExpansionRate(double v) override {
149 if constexpr (std::is_base_of<Reactor, R>::value) {
150 R::m_vdot = v;
151 } else {
152 throw NotImplementedError("ReactorDelegator::setExpansionRate",
153 "Expansion rate is undefined for reactors of type '{}'.", type());
154 }
155 }
156
157 double heatRate() const override {
158 if constexpr (std::is_base_of<Reactor, R>::value) {
159 return R::m_Qdot;
160 } else {
161 throw NotImplementedError("ReactorDelegator::heatRate",
162 "Heat rate is undefined for reactors of type '{}'.", type());
163 }
164 }
165
166 void setHeatRate(double q) override {
167 if constexpr (std::is_base_of<Reactor, R>::value) {
168 R::m_Qdot = q;
169 } else {
170 throw NotImplementedError("ReactorDelegator::setHeatRate",
171 "Heat rate is undefined for reactors of type '{}'.", type());
172 }
173 }
174
175 span<double> surfaceProductionRates() override {
176 return R::m_sdot;
177 }
178
179private:
180 function<void(double)> m_initialize;
181 function<void(span<double>)> m_getState;
182 function<void(span<double>)> m_updateState;
183 function<void(bool)> m_updateConnected;
184 function<void(double, span<double>, span<double>)> m_eval;
185 function<void(double)> m_evalWalls;
186 function<string(size_t)> m_componentName;
187 function<size_t(const string&)> m_componentIndex;
188};
189
190}
191#endif
Header file for class ReactorSurface.
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
Delegate member functions of a C++ class to externally-specified functions.
Definition Delegator.h:91
void install(const string &name, function< void()> &target, const function< void()> &func)
Install a function with the signature void() as being delegatable.
Definition Delegator.h:289
span< double > stripConst(const span< const double > &s)
Helper to remove const for cases where the delegated function signature uses span<const double>.
Definition Delegator.h:488
An error indicating that an unimplemented function has been called.
An abstract base class for providing access to protected capabilities Reactor objects from delegate m...
virtual void setNEq(size_t n)=0
Set the number of equations represented by this reactor.
virtual span< double > surfaceProductionRates()=0
Production rates on surfaces.
virtual void setHeatRate(double q)=0
Set the net heat transfer rate (for example, through walls) into the reactor [W].
virtual void setExpansionRate(double v)=0
Set the net rate of volume change (for example, from moving walls) [m^3/s].
virtual double heatRate() const =0
Get the net heat transfer rate (for example, through walls) into the reactor [W].
virtual double expansionRate() const =0
Get the net rate of volume change (for example, from moving walls) [m^3/s].
virtual void evalWalls(double t)
Evaluate contributions from walls connected to this reactor.
Delegate methods of the Reactor class to external functions.
void setNEq(size_t n) override
Set the number of equations represented by this reactor.
double heatRate() const override
Get the net heat transfer rate (for example, through walls) into the reactor [W].
double expansionRate() const override
Get the net rate of volume change (for example, from moving walls) [m^3/s].
span< double > surfaceProductionRates() override
Production rates on surfaces.
void setExpansionRate(double v) override
Set the net rate of volume change (for example, from moving walls) [m^3/s].
void setHeatRate(double q) override
Set the net heat transfer rate (for example, through walls) into the reactor [W].
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.