Cantera  2.0
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 
17 const int ReservoirType = 1;
18 const int ReactorType = 2;
19 const int FlowReactorType = 3;
20 const int ConstPressureReactorType = 4;
21 
22 /**
23  * Base class for stirred reactors.
24  * Allows using any substance model, with arbitrary
25  * inflow, outflow, heat loss/gain, surface chemistry, and
26  * volume change.
27  */
29 {
30 
31 public:
32 
33  ReactorBase(std::string name = "(none)");
34  virtual ~ReactorBase() {}
35 
36  //-----------------------------------------------------
37 
38  virtual int type() const {
39  return 0;
40  }
41  std::string name() const {
42  return m_name;
43  }
44  void setName(std::string name) {
45  m_name = name;
46  }
47 
48  /** @name Methods to set up a simulation. */
49  //@{
50 
51 
52  /**
53  * Set the initial reactor volume. By default, the volume is
54  * 1.0 m^3.
55  */
56  void setInitialVolume(doublereal vol) {
57  m_vol = vol;
58  m_vol0 = vol;
59  }
60 
61  /**
62  * Set initial time. Default = 0.0 s. Restarts integration
63  * from this time using the current mixture state as the
64  * initial condition.
65  */
66  void setInitialTime(doublereal time) {
67  m_time = time;
68  m_init = false;
69  }
70 
71  /**
72  * Specify the mixture contained in the reactor. Note that
73  * a pointer to this substance is stored, and as the integration
74  * proceeds, the state of the substance is modified.
75  */
76  void setThermoMgr(thermo_t& thermo);
77 
78  void addInlet(FlowDevice& inlet);
79  void addOutlet(FlowDevice& outlet);
80  FlowDevice& inlet(size_t n = 0);
81  FlowDevice& outlet(size_t n = 0);
82 
83  size_t nInlets() {
84  return m_inlet.size();
85  }
86  size_t nOutlets() {
87  return m_outlet.size();
88  }
89  size_t nWalls() {
90  return m_wall.size();
91  }
92 
93  void addWall(Wall& w, int lr);
94  Wall& wall(size_t n);
95 
96  /**
97  * Initialize the reactor. Must be called after specifying the
98  * (and if necessary the inlet mixture) and before
99  * calling advance.
100  */
101  virtual void initialize(doublereal t0 = 0.0) {
102  tilt();
103  }
104 
105  /**
106  * Advance the state of the reactor in time.
107  * @param time Time to advance to (s).
108  * Note that this method
109  * changes the state of the mixture object.
110  */
111  virtual void advance(doublereal time) {
112  tilt();
113  }
114  virtual double step(doublereal time) {
115  tilt();
116  return 0.0;
117  }
118  virtual void start() {}
119 
120  //@}
121 
122  void resetState();
123 
124  /// return a reference to the contents.
126  return *m_thermo;
127  }
128 
129  const thermo_t& contents() const {
130  return *m_thermo;
131  }
132 
133  doublereal residenceTime();
134 
135 
136  /**
137  * @name Solution components.
138  * The values returned are those after the last call to advance
139  * or step.
140  */
141  //@{
142 
143  /// the current time (s).
144  doublereal time() const {
145  return m_time;
146  }
147 
148 
149  //! Returns the current volume of the reactor
150  /*!
151  * @return Return the volume in m**3
152  */
153  doublereal volume() const {
154  return m_vol;
155  }
156  doublereal density() const {
157  return m_state[1];
158  }
159  doublereal temperature() const {
160  return m_state[0];
161  }
162  doublereal enthalpy_mass() const {
163  return m_enthalpy;
164  }
165  doublereal intEnergy_mass() const {
166  return m_intEnergy;
167  }
168  doublereal pressure() const {
169  return m_pressure;
170  }
171  doublereal mass() const {
172  return m_vol * density();
173  }
174  const doublereal* massFractions() const {
175  return DATA_PTR(m_state) + 2;
176  }
177  doublereal massFraction(size_t k) const {
178  return m_state[k+2];
179  }
180 
181  //@}
182 
183  int error(std::string msg) const {
184  writelog("Error: "+msg);
185  return 1;
186  }
187 
188 protected:
189 
190  //! Number of homogeneous species in the mixture
191  size_t m_nsp;
192 
193  thermo_t* m_thermo;
194  doublereal m_time;
195  doublereal m_vol, m_vol0;
196  bool m_init;
197  size_t m_nInlets, m_nOutlets;
198  bool m_open;
199  doublereal m_enthalpy;
200  doublereal m_intEnergy;
201  doublereal m_pressure;
202  vector_fp m_state;
203  std::vector<FlowDevice*> m_inlet, m_outlet;
204  std::vector<Wall*> m_wall;
205  vector_int m_lr;
206  size_t m_nwalls;
207  std::string m_name;
208  double m_rho0;
209 
210 private:
211 
212  void tilt(std::string method="") const {
213  throw CanteraError("ReactorBase::"+method,
214  "ReactorBase method called!");
215  }
216 };
217 }
218 
219 #endif
220