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