Cantera  3.3.0a1
Loading...
Searching...
No Matches
ReactorBase.h
Go to the documentation of this file.
1//! @file ReactorBase.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_REACTORBASE_H
7#define CT_REACTORBASE_H
8
11
12namespace Cantera
13{
14
15//! @defgroup zerodGroup Zero-Dimensional Reactor Networks
16//!
17//! @details See the [Reactor Science](../reference/reactors/index.html) section of the
18//! %Cantera website for a description of the governing equations for specific reactor
19//! types and the methods used for solving networks of interconnected reactors.
20
21class FlowDevice;
22class WallBase;
23class ReactorNet;
24class ReactorSurface;
25class Kinetics;
26class ThermoPhase;
27class Solution;
28
29enum class SensParameterType {
30 reaction,
31 enthalpy
32};
33
35{
36 size_t local; //!< local parameter index
37 size_t global; //!< global parameter index
38 double value; //!< nominal value of the parameter
39 SensParameterType type; //!< type of sensitivity parameter
40};
41
42/**
43 * Base class for reactor objects. Allows using any substance model, with arbitrary
44 * inflow, outflow, heat loss/gain, surface chemistry, and volume change, whenever
45 * defined.
46 * @ingroup reactorGroup
47 */
49{
50public:
51 //! Instantiate a ReactorBase object with Solution contents.
52 //! @param sol Solution object to be set.
53 //! @param name Name of the reactor.
54 //! @since New in %Cantera 3.1.
55 ReactorBase(shared_ptr<Solution> sol, const string& name="(none)");
56
57 //! Instantiate a ReactorBase object with Solution contents.
58 //! @param sol Solution object representing the contents of this reactor
59 //! @param clone Determines whether to clone `sol` so that the internal state of
60 //! this reactor is independent of the original Solution object and any Solution
61 //! objects used by other reactors in the network.
62 //! @param name Name of the reactor.
63 //! @since Added the `clone` argument in %Cantera 3.2. If not specified, the default
64 //! behavior in %Cantera 3.2 is not to clone the Solution object. This will
65 //! change after %Cantera 3.2 to default to `true`.
66 ReactorBase(shared_ptr<Solution> sol, bool clone, const string& name="(none)");
67
68 virtual ~ReactorBase();
69 ReactorBase(const ReactorBase&) = delete;
70 ReactorBase& operator=(const ReactorBase&) = delete;
71
72 //! String indicating the reactor model implemented. Usually
73 //! corresponds to the name of the derived class.
74 virtual string type() const {
75 return "ReactorBase";
76 }
77
78 //! Return the name of this reactor
79 string name() const {
80 return m_name;
81 }
82
83 //! Set the name of this reactor
84 void setName(const string& name) {
85 m_name = name;
86 }
87
88 //! Set the default name of a reactor. Returns `false` if it was previously set.
89 bool setDefaultName(map<string, int>& counts);
90
91 //! Access the Solution object used to represent the contents of this reactor.
92 //! @since New in %Cantera 3.2
93 shared_ptr<Solution> phase() { return m_solution; }
94
95 //! Access the Solution object used to represent the contents of this reactor.
96 //! @since New in %Cantera 3.2
97 shared_ptr<const Solution> phase() const { return m_solution; }
98
99 //! @name Methods to set up a simulation
100 //! @{
101
102 //! Set the initial reactor volume.
103 virtual void setInitialVolume(double vol) {
104 throw NotImplementedError("ReactorBase::setInitialVolume",
105 "Volume is undefined for reactors of type '{}'.", type());
106 }
107
108 //! Returns an area associated with a reactor [m²].
109 //! Examples: surface area of ReactorSurface or cross section area of FlowReactor.
110 virtual double area() const {
111 throw NotImplementedError("ReactorBase::area",
112 "Area is undefined for reactors of type '{}'.", type());
113 }
114
115 //! Set an area associated with a reactor [m²].
116 //! Examples: surface area of ReactorSurface or cross section area of FlowReactor.
117 virtual void setArea(double a) {
118 throw NotImplementedError("ReactorBase::setArea",
119 "Area is undefined for reactors of type '{}'.", type());
120 }
121
122 //! Returns `true` if changes in the reactor composition due to chemical reactions
123 //! are enabled.
124 //! @since New in %Cantera 3.2.
125 virtual bool chemistryEnabled() const {
126 throw NotImplementedError("ReactorBase::chemistryEnabled",
127 "Not implemented for reactor type '{}'.", type());
128 }
129
130 //! Enable or disable changes in reactor composition due to chemical reactions.
131 //! @since New in %Cantera 3.2.
132 virtual void setChemistryEnabled(bool cflag = true) {
133 throw NotImplementedError("ReactorBase::setChemistryEnabled",
134 "Not implemented for reactor type '{}'.", type());
135 }
136
137 //! Returns `true` if solution of the energy equation is enabled.
138 //! @since New in %Cantera 3.2.
139 virtual bool energyEnabled() const {
140 throw NotImplementedError("ReactorBase::energyEnabled",
141 "Not implemented for reactor type '{}'.", type());
142 }
143
144 //! Set the energy equation on or off.
145 //! @since New in %Cantera 3.2.
146 virtual void setEnergyEnabled(bool eflag = true) {
147 throw NotImplementedError("ReactorBase::setEnergyEnabled",
148 "Not implemented for reactor type '{}'.", type());
149 }
150
151 //! Connect an inlet FlowDevice to this reactor
152 virtual void addInlet(FlowDevice& inlet);
153
154 //! Connect an outlet FlowDevice to this reactor
155 virtual void addOutlet(FlowDevice& outlet);
156
157 //! Return a reference to the *n*-th inlet FlowDevice connected to this reactor.
158 FlowDevice& inlet(size_t n = 0);
159
160 //! Return a reference to the *n*-th outlet FlowDevice connected to this reactor.
161 FlowDevice& outlet(size_t n = 0);
162
163 //! Return the number of inlet FlowDevice objects connected to this reactor.
164 size_t nInlets() {
165 return m_inlet.size();
166 }
167
168 //! Return the number of outlet FlowDevice objects connected to this reactor.
169 size_t nOutlets() {
170 return m_outlet.size();
171 }
172
173 //! Return the number of Wall objects connected to this reactor.
174 size_t nWalls() {
175 return m_wall.size();
176 }
177
178 //! Insert a Wall between this reactor and another reactor.
179 /*!
180 * `lr` = 0 if this reactor is to the left of the wall and `lr` = 1 if
181 * this reactor is to the right of the wall. This method is called
182 * automatically for both the left and right reactors by WallBase::install.
183 */
184 virtual void addWall(WallBase& w, int lr);
185
186 //! Return a reference to the *n*-th Wall connected to this reactor.
187 WallBase& wall(size_t n);
188
189 //! Add a ReactorSurface object to a Reactor object.
190 //! @attention This method should generally not be called directly by users.
191 //! Reactor and ReactorSurface objects should be connected by providing adjacent
192 //! reactors to the newReactorSurface factory function.
193 virtual void addSurface(ReactorSurface* surf);
194
195 //! Return a reference to the *n*-th ReactorSurface connected to this reactor.
196 ReactorSurface* surface(size_t n);
197
198 //! Return the number of surfaces in a reactor
199 virtual size_t nSurfs() const {
200 return m_surfaces.size();
201 }
202
203 /**
204 * Initialize the reactor. Called automatically by ReactorNet::initialize.
205 */
206 virtual void initialize(double t0 = 0.0) {
207 throw NotImplementedError("ReactorBase::initialize");
208 }
209
210 //! @}
211
212 //! Set the state of the Phase object associated with this reactor to the
213 //! reactor's current state.
214 virtual void restoreState();
215
216 //! Set the state of the reactor to the associated ThermoPhase object.
217 //! This method is the inverse of restoreState() and will trigger integrator
218 //! reinitialization.
219 virtual void syncState();
220
221 //! Return the residence time (s) of the contents of this reactor, based
222 //! on the outlet mass flow rates and the mass of the reactor contents.
223 double residenceTime();
224
225 //! @name Solution components
226 //!
227 //! The values returned are those after the last call to ReactorNet::advance
228 //! or ReactorNet::step.
229 //! @{
230
231 //! Returns the current volume (m^3) of the reactor.
232 double volume() const {
233 return m_vol;
234 }
235
236 //! Returns the current density (kg/m^3) of the reactor's contents.
237 double density() const {
238 if (m_state.empty()) {
239 throw CanteraError("ReactorBase::density",
240 "Reactor state empty and/or contents not defined.");
241 }
242 return m_state[1];
243 }
244
245 //! Returns the current temperature (K) of the reactor's contents.
246 double temperature() const {
247 if (m_state.empty()) {
248 throw CanteraError("ReactorBase::temperature",
249 "Reactor state empty and/or contents not defined.");
250 }
251 return m_state[0];
252 }
253
254 //! Returns the current enthalpy (J/kg) of the reactor's contents.
255 double enthalpy_mass() const {
256 return m_enthalpy;
257 }
258
259 //! Returns the current pressure (Pa) of the reactor.
260 double pressure() const {
261 return m_pressure;
262 }
263
264 //! Returns the mass (kg) of the reactor's contents.
265 double mass() const {
266 return m_mass;
267 }
268
269 //! Return the vector of species mass fractions.
270 const double* massFractions() const {
271 if (m_state.empty()) {
272 throw CanteraError("ReactorBase::massFractions",
273 "Reactor state empty and/or contents not defined.");
274 }
275 return m_state.data() + 2;
276 }
277
278 //! Return the mass fraction of the *k*-th species.
279 double massFraction(size_t k) const {
280 if (m_state.empty()) {
281 throw CanteraError("ReactorBase::massFraction",
282 "Reactor state empty and/or contents not defined.");
283 }
284 return m_state[k+2];
285 }
286
287 //! @}
288
289 //! The ReactorNet that this reactor belongs to.
291
292 //! Set the ReactorNet that this reactor belongs to.
293 void setNetwork(ReactorNet* net);
294
295 //! Add a sensitivity parameter associated with the reaction number *rxn*
296 virtual void addSensitivityReaction(size_t rxn) {
297 throw NotImplementedError("ReactorBase::addSensitivityReaction");
298 }
299
300 //! Number of sensitivity parameters associated with this reactor.
301 virtual size_t nSensParams() const {
302 return m_sensParams.size();
303 }
304
305protected:
306 explicit ReactorBase(const string& name="(none)");
307
308 //! Number of homogeneous species in the mixture
309 size_t m_nsp = 0;
310
311 ThermoPhase* m_thermo = nullptr;
312 double m_vol = 0.0; //!< Current volume of the reactor [m^3]
313 double m_mass = 0.0; //!< Current mass of the reactor [kg]
314 double m_enthalpy = 0.0; //!< Current specific enthalpy of the reactor [J/kg]
315 double m_pressure = 0.0; //!< Current pressure in the reactor [Pa]
316 vector<double> m_state;
317 vector<FlowDevice*> m_inlet, m_outlet;
318
319 vector<WallBase*> m_wall;
320 vector<ReactorSurface*> m_surfaces;
321
322 //! Vector of length nWalls(), indicating whether this reactor is on the left (0)
323 //! or right (1) of each wall.
324 vector<int> m_lr;
325 string m_name; //!< Reactor name.
326 bool m_defaultNameSet = false; //!< `true` if default name has been previously set.
327
328 //! The ReactorNet that this reactor is part of
329 ReactorNet* m_net = nullptr;
330
331 //! Composite thermo/kinetics/transport handler
332 shared_ptr<Solution> m_solution;
333
334 // Data associated each sensitivity parameter
335 vector<SensitivityParameter> m_sensParams;
336};
337}
338
339#endif
Base class for exceptions thrown by Cantera classes.
Base class for 'flow devices' (valves, pressure regulators, etc.) connecting reactors.
Definition FlowDevice.h:25
An error indicating that an unimplemented function has been called.
Base class for reactor objects.
Definition ReactorBase.h:49
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
double massFraction(size_t k) const
Return the mass fraction of the k-th species.
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
bool m_defaultNameSet
true if default name has been previously set.
size_t nWalls()
Return the number of Wall objects connected to this reactor.
virtual void setArea(double a)
Set an area associated with a reactor [m²].
WallBase & wall(size_t n)
Return a reference to the n-th Wall connected to this reactor.
double density() const
Returns the current density (kg/m^3) of the reactor's contents.
double pressure() const
Returns the current pressure (Pa) of the reactor.
virtual void addOutlet(FlowDevice &outlet)
Connect an outlet FlowDevice to this reactor.
double m_pressure
Current pressure in the reactor [Pa].
virtual string type() const
String indicating the reactor model implemented.
Definition ReactorBase.h:74
ReactorNet * m_net
The ReactorNet that this reactor is part of.
virtual void addWall(WallBase &w, int lr)
Insert a Wall between this reactor and another reactor.
void setNetwork(ReactorNet *net)
Set the ReactorNet that this reactor belongs to.
bool setDefaultName(map< string, int > &counts)
Set the default name of a reactor. Returns false if it was previously set.
virtual void addSensitivityReaction(size_t rxn)
Add a sensitivity parameter associated with the reaction number rxn
virtual double area() const
Returns an area associated with a reactor [m²].
void setName(const string &name)
Set the name of this reactor.
Definition ReactorBase.h:84
virtual size_t nSurfs() const
Return the number of surfaces in a reactor.
FlowDevice & inlet(size_t n=0)
Return a reference to the n-th inlet FlowDevice connected to this reactor.
double temperature() const
Returns the current temperature (K) of the reactor's contents.
virtual bool chemistryEnabled() const
Returns true if changes in the reactor composition due to chemical reactions are enabled.
vector< int > m_lr
Vector of length nWalls(), indicating whether this reactor is on the left (0) or right (1) of each wa...
double m_vol
Current volume of the reactor [m^3].
virtual size_t nSensParams() const
Number of sensitivity parameters associated with this reactor.
virtual void addInlet(FlowDevice &inlet)
Connect an inlet FlowDevice to this reactor.
virtual void syncState()
Set the state of the reactor to the associated ThermoPhase object.
double m_mass
Current mass of the reactor [kg].
virtual void setEnergyEnabled(bool eflag=true)
Set the energy equation on or off.
const double * massFractions() const
Return the vector of species mass fractions.
size_t m_nsp
Number of homogeneous species in the mixture.
string m_name
Reactor name.
double mass() const
Returns the mass (kg) of the reactor's contents.
virtual void setInitialVolume(double vol)
Set the initial reactor volume.
double volume() const
Returns the current volume (m^3) of the reactor.
virtual void restoreState()
Set the state of the Phase object associated with this reactor to the reactor's current state.
ReactorNet & network()
The ReactorNet that this reactor belongs to.
double residenceTime()
Return the residence time (s) of the contents of this reactor, based on the outlet mass flow rates an...
size_t nOutlets()
Return the number of outlet FlowDevice objects connected to this reactor.
virtual void setChemistryEnabled(bool cflag=true)
Enable or disable changes in reactor composition due to chemical reactions.
virtual void initialize(double t0=0.0)
Initialize the reactor.
size_t nInlets()
Return the number of inlet FlowDevice objects connected to this reactor.
ReactorSurface * surface(size_t n)
Return a reference to the n-th ReactorSurface connected to this reactor.
double m_enthalpy
Current specific enthalpy of the reactor [J/kg].
virtual bool energyEnabled() const
Returns true if solution of the energy equation is enabled.
shared_ptr< Solution > phase()
Access the Solution object used to represent the contents of this reactor.
Definition ReactorBase.h:93
shared_ptr< const Solution > phase() const
Access the Solution object used to represent the contents of this reactor.
Definition ReactorBase.h:97
string name() const
Return the name of this reactor.
Definition ReactorBase.h:79
double enthalpy_mass() const
Returns the current enthalpy (J/kg) of the reactor's contents.
virtual void addSurface(ReactorSurface *surf)
Add a ReactorSurface object to a Reactor object.
A class representing a network of connected reactors.
Definition ReactorNet.h:30
A surface where reactions can occur that is in contact with the bulk fluid of a Reactor.
Base class for a phase with thermodynamic properties.
Base class for 'walls' (walls, pistons, etc.) connecting reactors.
Definition Wall.h:23
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
size_t global
global parameter index
Definition ReactorBase.h:37
SensParameterType type
type of sensitivity parameter
Definition ReactorBase.h:39
size_t local
local parameter index
Definition ReactorBase.h:36
double value
nominal value of the parameter
Definition ReactorBase.h:38