Cantera  2.4.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  ReactorBase(const ReactorBase&) = delete;
50  ReactorBase& operator=(const ReactorBase&) = delete;
51 
52  //! Return a constant indicating the type of this Reactor
53  virtual int type() const {
54  return 0;
55  }
56 
57  //! Return the name of this reactor
58  std::string name() const {
59  return m_name;
60  }
61 
62  //! Set the name of this reactor
63  void setName(const std::string& name) {
64  m_name = name;
65  }
66 
67  //! @name Methods to set up a simulation.
68  //@{
69 
70  //! Set the initial reactor volume. By default, the volume is 1.0 m^3.
71  void setInitialVolume(doublereal vol) {
72  m_vol = vol;
73  }
74 
75  //! Specify the mixture contained in the reactor. Note that a pointer to
76  //! this substance is stored, and as the integration proceeds, the state of
77  //! the substance is modified.
78  virtual void setThermoMgr(thermo_t& thermo);
79 
80  //! Connect an inlet FlowDevice to this reactor
81  void addInlet(FlowDevice& inlet);
82 
83  //! Connect an outlet FlowDevice to this reactor
85 
86  //! Return a reference to the *n*-th inlet FlowDevice connected to this
87  //! reactor.
88  FlowDevice& inlet(size_t n = 0);
89 
90  //! Return a reference to the *n*-th outlet FlowDevice connected to this
91  //! reactor.
92  FlowDevice& outlet(size_t n = 0);
93 
94  //! Return the number of inlet FlowDevice objects connected to this reactor.
95  size_t nInlets() {
96  return m_inlet.size();
97  }
98 
99  //! Return the number of outlet FlowDevice objects connected to this
100  //! reactor.
101  size_t nOutlets() {
102  return m_outlet.size();
103  }
104 
105  //! Return the number of Wall objects connected to this reactor.
106  size_t nWalls() {
107  return m_wall.size();
108  }
109 
110  //! Insert a Wall between this reactor and another reactor.
111  /*!
112  * `lr` = 0 if this reactor is to the left of the wall and `lr` = 1 if
113  * this reactor is to the right of the wall. This method is called
114  * automatically for both the left and right reactors by Wall::install.
115  */
116  void addWall(Wall& w, int lr);
117 
118  //! Return a reference to the *n*-th Wall connected to this reactor.
119  Wall& wall(size_t n);
120 
121  void addSurface(ReactorSurface* surf);
122 
123  //! Return a reference to the *n*-th ReactorSurface connected to this
124  //! reactor
125  ReactorSurface* surface(size_t n);
126 
127  /**
128  * Initialize the reactor. Called automatically by ReactorNet::initialize.
129  */
130  virtual void initialize(doublereal t0 = 0.0) {
131  throw NotImplementedError("ReactorBase::initialize");
132  }
133 
134  //@}
135 
136  //! Set the state of the Phase object associated with this reactor to the
137  //! reactor's current state.
138  void restoreState() {
139  if (!m_thermo) {
140  throw CanteraError("ReactorBase::restoreState", "No phase defined.");
141  }
142  m_thermo->restoreState(m_state);
143  }
144 
145  //! Set the state of the reactor to correspond to the state of the
146  //! associated ThermoPhase object. This is the inverse of restoreState().
147  //! Calling this will trigger integrator reinitialization.
148  virtual void syncState();
149 
150  //! return a reference to the contents.
152  if (!m_thermo) {
153  throw CanteraError("ReactorBase::contents",
154  "Reactor contents not defined.");
155  }
156  return *m_thermo;
157  }
158 
159  const thermo_t& contents() const {
160  if (!m_thermo) {
161  throw CanteraError("ReactorBase::contents",
162  "Reactor contents not defined.");
163  }
164  return *m_thermo;
165  }
166 
167  //! Return the residence time (s) of the contents of this reactor, based
168  //! on the outlet mass flow rates and the mass of the reactor contents.
169  doublereal residenceTime();
170 
171  /**
172  * @name Solution components.
173  * The values returned are those after the last call to ReactorNet::advance
174  * or ReactorNet::step.
175  */
176  //@{
177 
178  //! Returns the current volume (m^3) of the reactor.
179  doublereal volume() const {
180  return m_vol;
181  }
182 
183  //! Returns the current density (kg/m^3) of the reactor's contents.
184  doublereal density() const {
185  return m_state[1];
186  }
187 
188  //! Returns the current temperature (K) of the reactor's contents.
189  doublereal temperature() const {
190  return m_state[0];
191  }
192 
193  //! Returns the current enthalpy (J/kg) of the reactor's contents.
194  doublereal enthalpy_mass() const {
195  return m_enthalpy;
196  }
197 
198  //! Returns the current internal energy (J/kg) of the reactor's contents.
199  doublereal intEnergy_mass() const {
200  return m_intEnergy;
201  }
202 
203  //! Returns the current pressure (Pa) of the reactor.
204  doublereal pressure() const {
205  return m_pressure;
206  }
207 
208  //! Returns the mass (kg) of the reactor's contents.
209  doublereal mass() const {
210  return m_vol * density();
211  }
212 
213  //! Return the vector of species mass fractions.
214  const doublereal* massFractions() const {
215  return m_state.data() + 2;
216  }
217 
218  //! Return the mass fraction of the *k*-th species.
219  doublereal massFraction(size_t k) const {
220  return m_state[k+2];
221  }
222 
223  //@}
224 
225  //! The ReactorNet that this reactor belongs to.
226  ReactorNet& network();
227 
228  //! Set the ReactorNet that this reactor belongs to.
229  void setNetwork(ReactorNet* net);
230 
231 protected:
232  //! Number of homogeneous species in the mixture
233  size_t m_nsp;
234 
235  thermo_t* m_thermo;
236  doublereal m_vol;
237  doublereal m_enthalpy;
238  doublereal m_intEnergy;
239  doublereal m_pressure;
240  vector_fp m_state;
241  std::vector<FlowDevice*> m_inlet, m_outlet;
242  std::vector<Wall*> m_wall;
243  std::vector<ReactorSurface*> m_surfaces;
244  vector_int m_lr;
245  std::string m_name;
246 
247  //! The ReactorNet that this reactor is part of
249 };
250 }
251 
252 #endif
virtual int type() const
Return a constant indicating the type of this Reactor.
Definition: ReactorBase.h:53
doublereal volume() const
Returns the current volume (m^3) of the reactor.
Definition: ReactorBase.h:179
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:214
virtual void initialize(doublereal t0=0.0)
Initialize the reactor.
Definition: ReactorBase.h:130
void restoreState(const vector_fp &state)
Restore a state saved on a previous call to saveState.
Definition: Phase.cpp:233
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:187
doublereal intEnergy_mass() const
Returns the current internal energy (J/kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:199
ReactorNet * m_net
The ReactorNet that this reactor is part of.
Definition: ReactorBase.h:248
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:95
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:184
doublereal mass() const
Returns the mass (kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:209
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:106
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:71
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:138
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:219
thermo_t & contents()
return a reference to the contents.
Definition: ReactorBase.h:151
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:204
size_t m_nsp
Number of homogeneous species in the mixture.
Definition: ReactorBase.h:233
size_t nOutlets()
Return the number of outlet FlowDevice objects connected to this reactor.
Definition: ReactorBase.h:101
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:189
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
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:63
std::string name() const
Return the name of this reactor.
Definition: ReactorBase.h:58
doublereal enthalpy_mass() const
Returns the current enthalpy (J/kg) of the reactor&#39;s contents.
Definition: ReactorBase.h:194