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