Cantera  3.0.0
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 //! Set the state of the thermo object to correspond to the state of the reactor
49 virtual void restoreThermoState() = 0;
50
51 //! Set the state of the thermo object for surface *n* to correspond to the
52 //! state of that surface
53 virtual void restoreSurfaceState(size_t n) = 0;
54};
55
56//! Delegate methods of the Reactor class to external functions
57//! @ingroup reactorGroup
58template <class R>
59class ReactorDelegator : public Delegator, public R, public ReactorAccessor
60{
61public:
63 install("initialize", m_initialize, [this](double t0) { R::initialize(t0); });
64 install("syncState", m_syncState, [this]() { R::syncState(); });
65 install("getState", m_getState,
66 [this](std::array<size_t, 1> sizes, double* y) { R::getState(y); });
67 install("updateState", m_updateState,
68 [this](std::array<size_t, 1> sizes, double* y) { R::updateState(y); });
69 install("updateSurfaceState", m_updateSurfaceState,
70 [this](std::array<size_t, 1> sizes, double* y) { R::updateSurfaceState(y); });
71 install("getSurfaceInitialConditions", m_getSurfaceInitialConditions,
72 [this](std::array<size_t, 1> sizes, double* y) {
73 R::getSurfaceInitialConditions(y);
74 }
75 );
76 install("updateConnected", m_updateConnected,
77 [this](bool updatePressure) { R::updateConnected(updatePressure); });
78 install("eval", m_eval,
79 [this](std::array<size_t, 2> sizes, double t, double* LHS, double* RHS) {
80 R::eval(t, LHS, RHS);
81 }
82 );
83 install("evalWalls", m_evalWalls, [this](double t) { R::evalWalls(t); });
84 install("evalSurfaces", m_evalSurfaces,
85 [this](std::array<size_t, 3> sizes, double* LHS, double* RHS, double* sdot) {
86 R::evalSurfaces(LHS, RHS, sdot);
87 }
88 );
89 install("componentName", m_componentName,
90 [this](size_t k) { return R::componentName(k); });
91 install("componentIndex", m_componentIndex,
92 [this](const string& nm) { return R::componentIndex(nm); });
93 install("speciesIndex", m_speciesIndex,
94 [this](const string& nm) { return R::speciesIndex(nm); });
95 }
96
97 // Overrides of Reactor methods
98
99 void initialize(double t0) override {
100 m_initialize(t0);
101 }
102
103 void syncState() override {
104 m_syncState();
105 }
106
107 void getState(double* y) override {
108 std::array<size_t, 1> sizes{R::neq()};
109 m_getState(sizes, y);
110 }
111
112 void updateState(double* y) override {
113 std::array<size_t, 1> sizes{R::neq()};
114 m_updateState(sizes, y);
115 }
116
117 void updateSurfaceState(double* y) override {
118 std::array<size_t, 1> sizes{R::m_nv_surf};
119 m_updateSurfaceState(sizes, y);
120 }
121
122 void getSurfaceInitialConditions(double* y) override {
123 std::array<size_t, 1> sizes{R::m_nv_surf};
124 m_getSurfaceInitialConditions(sizes, y);
125 }
126
127 void updateConnected(bool updatePressure) override {
128 m_updateConnected(updatePressure);
129 }
130
131 void eval(double t, double* LHS, double* RHS) override {
132 std::array<size_t, 2> sizes{R::neq(), R::neq()};
133 m_eval(sizes, t, LHS, RHS);
134 }
135
136 void evalWalls(double t) override {
137 m_evalWalls(t);
138 }
139
140 void evalSurfaces(double* LHS, double* RHS, double* sdot) override {
141 std::array<size_t, 3> sizes{R::m_nv_surf, R::m_nv_surf, R::m_nsp};
142 m_evalSurfaces(sizes, LHS, RHS, sdot);
143 }
144
145 string componentName(size_t k) override {
146 return m_componentName(k);
147 }
148
149 size_t componentIndex(const string& nm) const override {
150 return m_componentIndex(nm);
151 }
152
153 size_t speciesIndex(const string& nm) const override {
154 return m_speciesIndex(nm);
155 }
156
157 // Public access to protected Reactor variables needed by derived classes
158
159 void setNEq(size_t n) override {
160 R::m_nv = n;
161 }
162
163 double expansionRate() const override {
164 return R::m_vdot;
165 }
166
167 void setExpansionRate(double v) override {
168 R::m_vdot = v;
169 }
170
171 double heatRate() const override {
172 return R::m_Qdot;
173 }
174
175 void setHeatRate(double q) override {
176 R::m_Qdot = q;
177 }
178
179 void restoreThermoState() override {
180 R::m_thermo->restoreState(R::m_state);
181 }
182
183 void restoreSurfaceState(size_t n) override {
184 R::m_surfaces.at(n)->syncState();
185 }
186
187private:
188 function<void(double)> m_initialize;
189 function<void()> m_syncState;
190 function<void(std::array<size_t, 1>, double*)> m_getState;
191 function<void(std::array<size_t, 1>, double*)> m_updateState;
192 function<void(std::array<size_t, 1>, double*)> m_updateSurfaceState;
193 function<void(std::array<size_t, 1>, double*)> m_getSurfaceInitialConditions;
194 function<void(bool)> m_updateConnected;
195 function<void(std::array<size_t, 2>, double, double*, double*)> m_eval;
196 function<void(double)> m_evalWalls;
197 function<void(std::array<size_t, 3>, double*, double*, double*)> m_evalSurfaces;
198 function<string(size_t)> m_componentName;
199 function<size_t(const string&)> m_componentIndex;
200 function<size_t(const string&)> m_speciesIndex;
201};
202
203}
204#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:104
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:301
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 void restoreThermoState()=0
Set the state of the thermo object to correspond to the state of the reactor.
virtual void restoreSurfaceState(size_t n)=0
Set the state of the thermo object for surface n to correspond to the state of that surface.
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].
Delegate methods of the Reactor class to external functions.
void setNEq(size_t n) override
Set the number of equations represented by this reactor.
void restoreSurfaceState(size_t n) override
Set the state of the thermo object for surface n to correspond to the state of that surface.
double heatRate() const override
Get the net heat transfer rate (for example, through walls) into the reactor [W].
void restoreThermoState() override
Set the state of the thermo object to correspond to the state of the reactor.
double expansionRate() const override
Get the net rate of volume change (for example, from moving walls) [m^3/s].
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:564