Cantera  2.3.0
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 http://www.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 Wall;
16 class ReactorNet;
17 class ReactorSurface;
18 
19 const int ReservoirType = 1;
20 const int ReactorType = 2;
21 const int FlowReactorType = 3;
22 const int ConstPressureReactorType = 4;
23 const int IdealGasReactorType = 5;
24 const int IdealGasConstPressureReactorType = 6;
25 
26 enum class SensParameterType {
27  reaction,
28  enthalpy
29 };
30 
31 struct SensitivityParameter
32 {
33  size_t local; //!< local parameter index
34  size_t global; //!< global parameter index
35  double value; //!< nominal value of the parameter
36  SensParameterType type; //!< type of sensitivity parameter
37 };
38 
39 /**
40  * Base class for stirred reactors. Allows using any substance model, with
41  * arbitrary inflow, outflow, heat loss/gain, surface chemistry, and volume
42  * change.
43  */
45 {
46 public:
47  explicit ReactorBase(const std::string& name = "(none)");
48  virtual ~ReactorBase() {}
49 
50  //! Return a constant indicating the type of this Reactor
51  virtual int type() const {
52  return 0;
53  }
54 
55  //! Return the name of this reactor
56  std::string name() const {
57  return m_name;
58  }
59 
60  //! Set the name of this reactor
61  void setName(const std::string& name) {
62  m_name = name;
63  }
64 
65  //! @name Methods to set up a simulation.
66  //@{
67 
68  //! Set the initial reactor volume. By default, the volume is 1.0 m^3.
69  void setInitialVolume(doublereal vol) {
70  m_vol = vol;
71  }
72 
73  //! Specify the mixture contained in the reactor. Note that a pointer to
74  //! this substance is stored, and as the integration proceeds, the state of
75  //! the substance is modified.
76  virtual void setThermoMgr(thermo_t& thermo);
77 
78  //! Connect an inlet FlowDevice to this reactor
79  void addInlet(FlowDevice& inlet);
80 
81  //! Connect an outlet FlowDevice to this reactor
83 
84  //! Return a reference to the *n*-th inlet FlowDevice connected to this
85  //! reactor.
86  FlowDevice& inlet(size_t n = 0);
87 
88  //! Return a reference to the *n*-th outlet FlowDevice connected to this
89  //! reactor.
90  FlowDevice& outlet(size_t n = 0);
91 
92  //! Return the number of inlet FlowDevice objects connected to this reactor.
93  size_t nInlets() {
94  return m_inlet.size();
95  }
96 
97  //! Return the number of outlet FlowDevice objects connected to this
98  //! reactor.
99  size_t nOutlets() {
100  return m_outlet.size();
101  }
102 
103  //! Return the number of Wall objects connected to this reactor.
104  size_t nWalls() {
105  return m_wall.size();
106  }
107 
108  //! Insert a Wall between this reactor and another reactor.
109  /*!
110  * `lr` = 0 if this reactor is to the left of the wall and `lr` = 1 if
111  * this reactor is to the right of the wall. This method is called
112  * automatically for both the left and right reactors by Wall::install.
113  */
114  void addWall(Wall& w, int lr);
115 
116  //! Return a reference to the *n*-th Wall connected to this reactor.
117  Wall& wall(size_t n);
118 
119  void addSurface(ReactorSurface* surf);
120 
121  //! Return a reference to the *n*-th ReactorSurface connected to this
122  //! reactor
123  ReactorSurface* surface(size_t n);
124 
125  /**
126  * Initialize the reactor. Called automatically by ReactorNet::initialize.
127  */
128  virtual void initialize(doublereal t0 = 0.0) {
129  throw NotImplementedError("ReactorBase::initialize");
130  }
131 
132  //@}
133 
134  //! Set the state of the Phase object associated with this reactor to the
135  //! reactor's current state.
136  void restoreState() {
137  if (!m_thermo) {
138  throw CanteraError("ReactorBase::restoreState", "No phase defined.");
139  }
140  m_thermo->restoreState(m_state);
141  }
142 
143  //! Set the state of the reactor to correspond to the state of the
144  //! associated ThermoPhase object. This is the inverse of restoreState().
145  //! Calling this will trigger integrator reinitialization.
146  virtual void syncState();
147 
148  //! return a reference to the contents.
150  if (!m_thermo) {
151  throw CanteraError("ReactorBase::contents",
152  "Reactor contents not defined.");
153  }
154  return *m_thermo;
155  }
156 
157  const thermo_t& contents() const {
158  if (!m_thermo) {
159  throw CanteraError("ReactorBase::contents",
160  "Reactor contents not defined.");
161  }
162  return *m_thermo;
163  }
164 
165  //! Return the residence time (s) of the contents of this reactor, based
166  //! on the outlet mass flow rates and the mass of the reactor contents.
167  doublereal residenceTime();
168 
169  /**
170  * @name Solution components.
171  * The values returned are those after the last call to ReactorNet::advance
172  * or ReactorNet::step.
173  */
174  //@{
175 
176  //! Returns the current volume (m^3) of the reactor.
177  doublereal volume() const {
178  return m_vol;
179  }
180 
181  //! Returns the current density (kg/m^3) of the reactor's contents.
182  doublereal density() const {
183  return m_state[1];
184  }
185 
186  //! Returns the current temperature (K) of the reactor's contents.
187  doublereal temperature() const {
188  return m_state[0];
189  }
190 
191  //! Returns the current enthalpy (J/kg) of the reactor's contents.
192  doublereal enthalpy_mass() const {
193  return m_enthalpy;
194  }
195 
196  //! Returns the current internal energy (J/kg) of the reactor's contents.
197  doublereal intEnergy_mass() const {
198  return m_intEnergy;
199  }
200 
201  //! Returns the current pressure (Pa) of the reactor.
202  doublereal pressure() const {
203  return m_pressure;
204  }
205 
206  //! Returns the mass (kg) of the reactor's contents.
207  doublereal mass() const {
208  return m_vol * density();
209  }
210 
211  //! Return the vector of species mass fractions.
212  const doublereal* massFractions() const {
213  return m_state.data() + 2;
214  }
215 
216  //! Return the mass fraction of the *k*-th species.
217  doublereal massFraction(size_t k) const {
218  return m_state[k+2];
219  }
220 
221  //@}
222 
223  //! The ReactorNet that this reactor belongs to.
224  ReactorNet& network();
225 
226  //! Set the ReactorNet that this reactor belongs to.
227  void setNetwork(ReactorNet* net);
228 
229 protected:
230  //! Number of homogeneous species in the mixture
231  size_t m_nsp;
232 
233  thermo_t* m_thermo;
234  doublereal m_vol;
235  doublereal m_enthalpy;
236  doublereal m_intEnergy;
237  doublereal m_pressure;
238  vector_fp m_state;
239  std::vector<FlowDevice*> m_inlet, m_outlet;
240  std::vector<Wall*> m_wall;
241  std::vector<ReactorSurface*> m_surfaces;
242  vector_int m_lr;
243  std::string m_name;
244 
245  //! The ReactorNet that this reactor is part of
247 };
248 }
249 
250 #endif
virtual int type() const
Return a constant indicating the type of this Reactor.
Definition: ReactorBase.h:51
doublereal volume() const
Returns the current volume (m^3) of the reactor.
Definition: ReactorBase.h:177
A class representing a network of connected reactors.
Definition: ReactorNet.h:23
const doublereal * massFractions() const
Return the vector of species mass fractions.
Definition: ReactorBase.h:212
virtual void initialize(doublereal t0=0.0)
Initialize the reactor.
Definition: ReactorBase.h:128
void restoreState(const vector_fp &state)
Restore a state saved on a previous call to saveState.
Definition: Phase.cpp:309
Base class for &#39;flow devices&#39; (valves, pressure regulators, etc.) connecting reactors.
Definition: FlowDevice.h:27
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:193
doublereal intEnergy_mass() const
Returns the current internal energy (J/kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:197
ReactorNet * m_net
The ReactorNet that this reactor is part of.
Definition: ReactorBase.h:246
ReactorNet & network()
The ReactorNet that this reactor belongs to.
Definition: ReactorBase.cpp:85
size_t nInlets()
Return the number of inlet FlowDevice objects connected to this reactor.
Definition: ReactorBase.h:93
void addOutlet(FlowDevice &outlet)
Connect an outlet FlowDevice to this reactor.
Definition: ReactorBase.cpp:52
void addInlet(FlowDevice &inlet)
Connect an inlet FlowDevice to this reactor.
Definition: ReactorBase.cpp:47
doublereal density() const
Returns the current density (kg/m^3) of the reactor&#39;s contents.
Definition: ReactorBase.h:182
doublereal mass() const
Returns the mass (kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:207
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:159
size_t nWalls()
Return the number of Wall objects connected to this reactor.
Definition: ReactorBase.h:104
Wall & wall(size_t n)
Return a reference to the n-th Wall connected to this reactor.
Definition: ReactorBase.cpp:67
Represents a wall between between two ReactorBase objects.
Definition: Wall.h:25
virtual void syncState()
Set the state of the reactor to correspond to the state of the associated ThermoPhase object...
Definition: ReactorBase.cpp:36
void setInitialVolume(doublereal vol)
Set the initial reactor volume. By default, the volume is 1.0 m^3.
Definition: ReactorBase.h:69
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
Base class for stirred reactors.
Definition: ReactorBase.h:44
void restoreState()
Set the state of the Phase object associated with this reactor to the reactor&#39;s current state...
Definition: ReactorBase.h:136
void addWall(Wall &w, int lr)
Insert a Wall between this reactor and another reactor.
Definition: ReactorBase.cpp:57
virtual void setThermoMgr(thermo_t &thermo)
Specify the mixture contained in the reactor.
Definition: ReactorBase.cpp:26
doublereal massFraction(size_t k) const
Return the mass fraction of the k-th species.
Definition: ReactorBase.h:217
thermo_t & contents()
return a reference to the contents.
Definition: ReactorBase.h:149
FlowDevice & inlet(size_t n=0)
Return a reference to the n-th inlet FlowDevice connected to this reactor.
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...
ReactorSurface * surface(size_t n)
Return a reference to the n-th ReactorSurface connected to this reactor.
Definition: ReactorBase.cpp:80
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:157
doublereal pressure() const
Returns the current pressure (Pa) of the reactor.
Definition: ReactorBase.h:202
size_t m_nsp
Number of homogeneous species in the mixture.
Definition: ReactorBase.h:231
size_t nOutlets()
Return the number of outlet FlowDevice objects connected to this reactor.
Definition: ReactorBase.h:99
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
doublereal temperature() const
Returns the current temperature (K) of the reactor&#39;s contents.
Definition: ReactorBase.h:187
Namespace for the Cantera kernel.
Definition: application.cpp:29
Header file for class ThermoPhase, the base class for phases with thermodynamic properties, and the text for the Module thermoprops (see Thermodynamic Properties and class ThermoPhase).
void setName(const std::string &name)
Set the name of this reactor.
Definition: ReactorBase.h:61
std::string name() const
Return the name of this reactor.
Definition: ReactorBase.h:56
doublereal enthalpy_mass() const
Returns the current enthalpy (J/kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:192