Cantera  3.1.0a1
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 
9 #include "cantera/base/global.h"
11 
12 namespace Cantera
13 {
14 
15 //! @defgroup zerodGroup Zero-Dimensional Reactor Networks
16 //!
17 //! @details See the
18 //! [Reactor Science](https://cantera.org/science/reactors/reactors.html)
19 //! section of the %Cantera website for a description of the governing equations for
20 //! specific reactor types and the methods used for solving networks of interconnected
21 //! reactors.
22 
23 class FlowDevice;
24 class WallBase;
25 class ReactorNet;
26 class ReactorSurface;
27 class Kinetics;
28 class ThermoPhase;
29 
30 enum class SensParameterType {
31  reaction,
32  enthalpy
33 };
34 
35 struct SensitivityParameter
36 {
37  size_t local; //!< local parameter index
38  size_t global; //!< global parameter index
39  double value; //!< nominal value of the parameter
40  SensParameterType type; //!< type of sensitivity parameter
41 };
42 
43 /**
44  * Base class for stirred reactors. Allows using any substance model, with
45  * arbitrary inflow, outflow, heat loss/gain, surface chemistry, and volume
46  * change.
47  * @ingroup reactorGroup
48  */
50 {
51 public:
52  explicit ReactorBase(const string& name = "(none)");
53  virtual ~ReactorBase() = default;
54  ReactorBase(const ReactorBase&) = delete;
55  ReactorBase& operator=(const ReactorBase&) = delete;
56 
57  //! String indicating the reactor model implemented. Usually
58  //! corresponds to the name of the derived class.
59  virtual string type() const {
60  return "ReactorBase";
61  }
62 
63  //! Return the name of this reactor
64  string name() const {
65  return m_name;
66  }
67 
68  //! Set the name of this reactor
69  void setName(const string& name) {
70  m_name = name;
71  }
72 
73  //! @name Methods to set up a simulation
74  //! @{
75 
76  //! Set the initial reactor volume. By default, the volume is 1.0 m^3.
77  void setInitialVolume(double vol) {
78  m_vol = vol;
79  }
80 
81  //! Specify the mixture contained in the reactor. Note that a pointer to
82  //! this substance is stored, and as the integration proceeds, the state of
83  //! the substance is modified.
84  virtual void setThermoMgr(ThermoPhase& thermo);
85 
86  //! Specify chemical kinetics governing the reactor.
87  virtual void setKineticsMgr(Kinetics& kin) {
88  throw NotImplementedError("ReactorBase::setKineticsMgr");
89  }
90 
91  //! Enable or disable changes in reactor composition due to chemical reactions.
92  virtual void setChemistry(bool cflag = true) {
93  throw NotImplementedError("ReactorBase::setChemistry");
94  }
95 
96  //! Set the energy equation on or off.
97  virtual void setEnergy(int eflag = 1) {
98  throw NotImplementedError("ReactorBase::setEnergy");
99  }
100 
101  //! Connect an inlet FlowDevice to this reactor
102  void addInlet(FlowDevice& inlet);
103 
104  //! Connect an outlet FlowDevice to this reactor
105  void addOutlet(FlowDevice& outlet);
106 
107  //! Return a reference to the *n*-th inlet FlowDevice connected to this
108  //! reactor.
109  FlowDevice& inlet(size_t n = 0);
110 
111  //! Return a reference to the *n*-th outlet FlowDevice connected to this
112  //! reactor.
113  FlowDevice& outlet(size_t n = 0);
114 
115  //! Return the number of inlet FlowDevice objects connected to this reactor.
116  size_t nInlets() {
117  return m_inlet.size();
118  }
119 
120  //! Return the number of outlet FlowDevice objects connected to this
121  //! reactor.
122  size_t nOutlets() {
123  return m_outlet.size();
124  }
125 
126  //! Return the number of Wall objects connected to this reactor.
127  size_t nWalls() {
128  return m_wall.size();
129  }
130 
131  //! Insert a Wall between this reactor and another reactor.
132  /*!
133  * `lr` = 0 if this reactor is to the left of the wall and `lr` = 1 if
134  * this reactor is to the right of the wall. This method is called
135  * automatically for both the left and right reactors by WallBase::install.
136  */
137  void addWall(WallBase& w, int lr);
138 
139  //! Return a reference to the *n*-th Wall connected to this reactor.
140  WallBase& wall(size_t n);
141 
142  virtual void addSurface(ReactorSurface* surf);
143 
144  //! Return a reference to the *n*-th ReactorSurface connected to this
145  //! reactor
146  ReactorSurface* surface(size_t n);
147 
148  //! Return the number of surfaces in a reactor
149  virtual size_t nSurfs() {
150  return m_surfaces.size();
151  }
152 
153  /**
154  * Initialize the reactor. Called automatically by ReactorNet::initialize.
155  */
156  virtual void initialize(double t0 = 0.0) {
157  throw NotImplementedError("ReactorBase::initialize");
158  }
159 
160  //! @}
161 
162  //! Set the state of the Phase object associated with this reactor to the
163  //! reactor's current state.
164  void restoreState();
165 
166  //! Set the state of the reactor to correspond to the state of the
167  //! associated ThermoPhase object. This is the inverse of restoreState().
168  //! Calling this will trigger integrator reinitialization.
169  virtual void syncState();
170 
171  //! return a reference to the contents.
173  if (!m_thermo) {
174  throw CanteraError("ReactorBase::contents",
175  "Reactor contents not defined.");
176  }
177  return *m_thermo;
178  }
179 
180  const ThermoPhase& contents() const {
181  if (!m_thermo) {
182  throw CanteraError("ReactorBase::contents",
183  "Reactor contents not defined.");
184  }
185  return *m_thermo;
186  }
187 
188  //! Return the residence time (s) of the contents of this reactor, based
189  //! on the outlet mass flow rates and the mass of the reactor contents.
190  double residenceTime();
191 
192  //! @name Solution components
193  //!
194  //! The values returned are those after the last call to ReactorNet::advance
195  //! or ReactorNet::step.
196  //! @{
197 
198  //! Returns the current volume (m^3) of the reactor.
199  double volume() const {
200  return m_vol;
201  }
202 
203  //! Returns the current density (kg/m^3) of the reactor's contents.
204  double density() const {
205  return m_state[1];
206  }
207 
208  //! Returns the current temperature (K) of the reactor's contents.
209  double temperature() const {
210  return m_state[0];
211  }
212 
213  //! Returns the current enthalpy (J/kg) of the reactor's contents.
214  double enthalpy_mass() const {
215  return m_enthalpy;
216  }
217 
218  //! Returns the current internal energy (J/kg) of the reactor's contents.
219  double intEnergy_mass() const {
220  return m_intEnergy;
221  }
222 
223  //! Returns the current pressure (Pa) of the reactor.
224  double pressure() const {
225  return m_pressure;
226  }
227 
228  //! Returns the mass (kg) of the reactor's contents.
229  double mass() const {
230  return m_vol * density();
231  }
232 
233  //! Return the vector of species mass fractions.
234  const double* massFractions() const {
235  return m_state.data() + 2;
236  }
237 
238  //! Return the mass fraction of the *k*-th species.
239  double massFraction(size_t k) const {
240  return m_state[k+2];
241  }
242 
243  //! @}
244 
245  //! The ReactorNet that this reactor belongs to.
246  ReactorNet& network();
247 
248  //! Set the ReactorNet that this reactor belongs to.
249  void setNetwork(ReactorNet* net);
250 
251 protected:
252  //! Number of homogeneous species in the mixture
253  size_t m_nsp = 0;
254 
255  ThermoPhase* m_thermo = nullptr;
256  double m_vol = 1.0; //!< Current volume of the reactor [m^3]
257  double m_enthalpy = 0.0; //!< Current specific enthalpy of the reactor [J/kg]
258  double m_intEnergy = 0.0; //!< Current internal energy of the reactor [J/kg]
259  double m_pressure = 0.0; //!< Current pressure in the reactor [Pa]
260  vector<double> m_state;
261  vector<FlowDevice*> m_inlet, m_outlet;
262 
263  vector<WallBase*> m_wall;
264  vector<ReactorSurface*> m_surfaces;
265 
266  //! Vector of length nWalls(), indicating whether this reactor is on the left (0)
267  //! or right (1) of each wall.
268  vector<int> m_lr;
269  string m_name;
270 
271  //! The ReactorNet that this reactor is part of
272  ReactorNet* m_net = nullptr;
273 };
274 }
275 
276 #endif
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:66
Base class for 'flow devices' (valves, pressure regulators, etc.) connecting reactors.
Definition: FlowDevice.h:24
Public interface for kinetics managers.
Definition: Kinetics.h:125
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:195
Base class for stirred reactors.
Definition: ReactorBase.h:50
virtual void setKineticsMgr(Kinetics &kin)
Specify chemical kinetics governing the reactor.
Definition: ReactorBase.h:87
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.
Definition: ReactorBase.h:239
size_t nWalls()
Return the number of Wall objects connected to this reactor.
Definition: ReactorBase.h:127
WallBase & wall(size_t n)
Return a reference to the n-th Wall connected to this reactor.
Definition: ReactorBase.cpp:61
double density() const
Returns the current density (kg/m^3) of the reactor's contents.
Definition: ReactorBase.h:204
void setInitialVolume(double vol)
Set the initial reactor volume. By default, the volume is 1.0 m^3.
Definition: ReactorBase.h:77
double pressure() const
Returns the current pressure (Pa) of the reactor.
Definition: ReactorBase.h:224
void addOutlet(FlowDevice &outlet)
Connect an outlet FlowDevice to this reactor.
Definition: ReactorBase.cpp:46
double m_pressure
Current pressure in the reactor [Pa].
Definition: ReactorBase.h:259
virtual string type() const
String indicating the reactor model implemented.
Definition: ReactorBase.h:59
ReactorNet * m_net
The ReactorNet that this reactor is part of.
Definition: ReactorBase.h:272
void addWall(WallBase &w, int lr)
Insert a Wall between this reactor and another reactor.
Definition: ReactorBase.cpp:51
void setNetwork(ReactorNet *net)
Set the ReactorNet that this reactor belongs to.
Definition: ReactorBase.cpp:96
void setName(const string &name)
Set the name of this reactor.
Definition: ReactorBase.h:69
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.
Definition: ReactorBase.h:209
vector< int > m_lr
Vector of length nWalls(), indicating whether this reactor is on the left (0) or right (1) of each wa...
Definition: ReactorBase.h:268
double m_vol
Current volume of the reactor [m^3].
Definition: ReactorBase.h:256
virtual void setChemistry(bool cflag=true)
Enable or disable changes in reactor composition due to chemical reactions.
Definition: ReactorBase.h:92
void addInlet(FlowDevice &inlet)
Connect an inlet FlowDevice to this reactor.
Definition: ReactorBase.cpp:41
virtual void syncState()
Set the state of the reactor to correspond to the state of the associated ThermoPhase object.
Definition: ReactorBase.cpp:30
double m_intEnergy
Current internal energy of the reactor [J/kg].
Definition: ReactorBase.h:258
const double * massFractions() const
Return the vector of species mass fractions.
Definition: ReactorBase.h:234
virtual void setEnergy(int eflag=1)
Set the energy equation on or off.
Definition: ReactorBase.h:97
size_t m_nsp
Number of homogeneous species in the mixture.
Definition: ReactorBase.h:253
double intEnergy_mass() const
Returns the current internal energy (J/kg) of the reactor's contents.
Definition: ReactorBase.h:219
double mass() const
Returns the mass (kg) of the reactor's contents.
Definition: ReactorBase.h:229
double volume() const
Returns the current volume (m^3) of the reactor.
Definition: ReactorBase.h:199
virtual void setThermoMgr(ThermoPhase &thermo)
Specify the mixture contained in the reactor.
Definition: ReactorBase.cpp:20
void restoreState()
Set the state of the Phase object associated with this reactor to the reactor's current state.
Definition: ReactorBase.cpp:79
virtual size_t nSurfs()
Return the number of surfaces in a reactor.
Definition: ReactorBase.h:149
ThermoPhase & contents()
return a reference to the contents.
Definition: ReactorBase.h:172
ReactorNet & network()
The ReactorNet that this reactor belongs to.
Definition: ReactorBase.cpp:86
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.
Definition: ReactorBase.h:122
virtual void initialize(double t0=0.0)
Initialize the reactor.
Definition: ReactorBase.h:156
size_t nInlets()
Return the number of inlet FlowDevice objects connected to this reactor.
Definition: ReactorBase.h:116
ReactorSurface * surface(size_t n)
Return a reference to the n-th ReactorSurface connected to this reactor.
Definition: ReactorBase.cpp:74
double m_enthalpy
Current specific enthalpy of the reactor [J/kg].
Definition: ReactorBase.h:257
string name() const
Return the name of this reactor.
Definition: ReactorBase.h:64
double enthalpy_mass() const
Returns the current enthalpy (J/kg) of the reactor's contents.
Definition: ReactorBase.h:214
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.
Definition: ThermoPhase.h:390
Base class for 'walls' (walls, pistons, etc.) connecting reactors.
Definition: Wall.h:22
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:564