Cantera  3.1.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:
62 ReactorDelegator(shared_ptr<Solution> contents, const string& name="(none)")
63 : R(contents, name)
64 {
65 install("initialize", m_initialize, [this](double t0) { R::initialize(t0); });
66 install("syncState", m_syncState, [this]() { R::syncState(); });
67 install("getState", m_getState,
68 [this](std::array<size_t, 1> sizes, double* y) { R::getState(y); });
69 install("updateState", m_updateState,
70 [this](std::array<size_t, 1> sizes, double* y) { R::updateState(y); });
71 install("updateSurfaceState", m_updateSurfaceState,
72 [this](std::array<size_t, 1> sizes, double* y) { R::updateSurfaceState(y); });
73 install("getSurfaceInitialConditions", m_getSurfaceInitialConditions,
74 [this](std::array<size_t, 1> sizes, double* y) {
75 R::getSurfaceInitialConditions(y);
76 }
77 );
78 install("updateConnected", m_updateConnected,
79 [this](bool updatePressure) { R::updateConnected(updatePressure); });
80 install("eval", m_eval,
81 [this](std::array<size_t, 2> sizes, double t, double* LHS, double* RHS) {
82 R::eval(t, LHS, RHS);
83 }
84 );
85 install("evalWalls", m_evalWalls, [this](double t) { R::evalWalls(t); });
86 install("evalSurfaces", m_evalSurfaces,
87 [this](std::array<size_t, 3> sizes, double* LHS, double* RHS, double* sdot) {
88 R::evalSurfaces(LHS, RHS, sdot);
89 }
90 );
91 install("componentName", m_componentName,
92 [this](size_t k) { return R::componentName(k); });
93 install("componentIndex", m_componentIndex,
94 [this](const string& nm) { return R::componentIndex(nm); });
95 install("speciesIndex", m_speciesIndex,
96 [this](const string& nm) { return R::speciesIndex(nm); });
97 }
98
99 // Overrides of Reactor methods
100
101 string type() const override {
102 return fmt::format("Extensible{}", R::type());
103 }
104
105 void initialize(double t0) override {
106 m_initialize(t0);
107 }
108
109 void syncState() override {
110 m_syncState();
111 }
112
113 void getState(double* y) override {
114 std::array<size_t, 1> sizes{R::neq()};
115 m_getState(sizes, y);
116 }
117
118 void updateState(double* y) override {
119 std::array<size_t, 1> sizes{R::neq()};
120 m_updateState(sizes, y);
121 }
122
123 void updateSurfaceState(double* y) override {
124 std::array<size_t, 1> sizes{R::m_nv_surf};
125 m_updateSurfaceState(sizes, y);
126 }
127
128 void getSurfaceInitialConditions(double* y) override {
129 std::array<size_t, 1> sizes{R::m_nv_surf};
130 m_getSurfaceInitialConditions(sizes, y);
131 }
132
133 void updateConnected(bool updatePressure) override {
134 m_updateConnected(updatePressure);
135 }
136
137 void eval(double t, double* LHS, double* RHS) override {
138 std::array<size_t, 2> sizes{R::neq(), R::neq()};
139 m_eval(sizes, t, LHS, RHS);
140 }
141
142 void evalWalls(double t) override {
143 m_evalWalls(t);
144 }
145
146 void evalSurfaces(double* LHS, double* RHS, double* sdot) override {
147 std::array<size_t, 3> sizes{R::m_nv_surf, R::m_nv_surf, R::m_nsp};
148 m_evalSurfaces(sizes, LHS, RHS, sdot);
149 }
150
151 string componentName(size_t k) override {
152 return m_componentName(k);
153 }
154
155 size_t componentIndex(const string& nm) const override {
156 return m_componentIndex(nm);
157 }
158
159 size_t speciesIndex(const string& nm) const override {
160 return m_speciesIndex(nm);
161 }
162
163 // Public access to protected Reactor variables needed by derived classes
164
165 void setNEq(size_t n) override {
166 R::m_nv = n;
167 }
168
169 double expansionRate() const override {
170 return R::m_vdot;
171 }
172
173 void setExpansionRate(double v) override {
174 R::m_vdot = v;
175 }
176
177 double heatRate() const override {
178 return R::m_Qdot;
179 }
180
181 void setHeatRate(double q) override {
182 R::m_Qdot = q;
183 }
184
185 void restoreThermoState() override {
186 R::m_thermo->restoreState(R::m_state);
187 }
188
189 void restoreSurfaceState(size_t n) override {
190 R::m_surfaces.at(n)->syncState();
191 }
192
193private:
194 function<void(double)> m_initialize;
195 function<void()> m_syncState;
196 function<void(std::array<size_t, 1>, double*)> m_getState;
197 function<void(std::array<size_t, 1>, double*)> m_updateState;
198 function<void(std::array<size_t, 1>, double*)> m_updateSurfaceState;
199 function<void(std::array<size_t, 1>, double*)> m_getSurfaceInitialConditions;
200 function<void(bool)> m_updateConnected;
201 function<void(std::array<size_t, 2>, double, double*, double*)> m_eval;
202 function<void(double)> m_evalWalls;
203 function<void(std::array<size_t, 3>, double*, double*, double*)> m_evalSurfaces;
204 function<string(size_t)> m_componentName;
205 function<size_t(const string&)> m_componentIndex;
206 function<size_t(const string&)> m_speciesIndex;
207};
208
209}
210#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:595