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