Cantera  2.3.0
IdealGasReactor.cpp
Go to the documentation of this file.
1 //! @file IdealGasReactor.cpp A zero-dimensional reactor
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 
8 #include "cantera/zeroD/Wall.h"
9 
10 using namespace std;
11 
12 namespace Cantera
13 {
14 
15 void IdealGasReactor::setThermoMgr(ThermoPhase& thermo)
16 {
17  //! @TODO: Add a method to ThermoPhase that indicates whether a given
18  //! subclass is compatible with this reactor model
19  if (thermo.type() != "IdealGas") {
20  throw CanteraError("IdealGasReactor::setThermoMgr",
21  "Incompatible phase type provided");
22  }
23  Reactor::setThermoMgr(thermo);
24 }
25 
26 void IdealGasReactor::getInitialConditions(double t0, size_t leny, double* y)
27 {
28  warn_deprecated("IdealGasReactor::getInitialConditions",
29  "Use getState instead. To be removed after Cantera 2.3.");
30  getState(y);
31 }
32 
33 void IdealGasReactor::getState(double* y)
34 {
35  if (m_thermo == 0) {
36  throw CanteraError("getState",
37  "Error: reactor is empty.");
38  }
39  m_thermo->restoreState(m_state);
40 
41  // set the first component to the total mass
42  m_mass = m_thermo->density() * m_vol;
43  y[0] = m_mass;
44 
45  // set the second component to the total volume
46  y[1] = m_vol;
47 
48  // Set the third component to the temperature
49  y[2] = m_thermo->temperature();
50 
51  // set components y+3 ... y+K+2 to the mass fractions of each species
52  m_thermo->getMassFractions(y+3);
53 
54  // set the remaining components to the surface species
55  // coverages on the walls
56  getSurfaceInitialConditions(y + m_nsp + 3);
57 }
58 
59 void IdealGasReactor::initialize(doublereal t0)
60 {
61  Reactor::initialize(t0);
62  m_uk.resize(m_nsp, 0.0);
63 }
64 
65 void IdealGasReactor::updateState(doublereal* y)
66 {
67  // The components of y are [0] the total mass, [1] the total volume,
68  // [2] the temperature, [3...K+3] are the mass fractions of each species,
69  // and [K+3...] are the coverages of surface species on each wall.
70  m_mass = y[0];
71  m_vol = y[1];
72  m_thermo->setMassFractions_NoNorm(y+3);
73  m_thermo->setState_TR(y[2], m_mass / m_vol);
74  updateSurfaceState(y + m_nsp + 3);
75 
76  // save parameters needed by other connected reactors
77  m_enthalpy = m_thermo->enthalpy_mass();
78  m_pressure = m_thermo->pressure();
79  m_intEnergy = m_thermo->intEnergy_mass();
80  m_thermo->saveState(m_state);
81 }
82 
83 void IdealGasReactor::evalEqs(doublereal time, doublereal* y,
84  doublereal* ydot, doublereal* params)
85 {
86  double dmdt = 0.0; // dm/dt (gas phase)
87  double mcvdTdt = 0.0; // m * c_v * dT/dt
88  double* dYdt = ydot + 3;
89 
90  m_thermo->restoreState(m_state);
91  applySensitivity(params);
92  m_thermo->getPartialMolarIntEnergies(&m_uk[0]);
93  const vector_fp& mw = m_thermo->molecularWeights();
94  const doublereal* Y = m_thermo->massFractions();
95 
96  if (m_chem) {
97  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
98  }
99 
100  evalWalls(time);
101  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 3);
102  dmdt += mdot_surf;
103 
104  // compression work and external heat transfer
105  mcvdTdt += - m_pressure * m_vdot - m_Q;
106 
107  for (size_t n = 0; n < m_nsp; n++) {
108  // heat release from gas phase and surface reactions
109  mcvdTdt -= m_wdot[n] * m_uk[n] * m_vol;
110  mcvdTdt -= m_sdot[n] * m_uk[n];
111  // production in gas phase and from surfaces
112  dYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n] / m_mass;
113  // dilution by net surface mass flux
114  dYdt[n] -= Y[n] * mdot_surf / m_mass;
115  }
116 
117  // add terms for outlets
118  for (size_t i = 0; i < m_outlet.size(); i++) {
119  double mdot_out = m_outlet[i]->massFlowRate(time);
120  dmdt -= mdot_out; // mass flow out of system
121  mcvdTdt -= mdot_out * m_pressure * m_vol / m_mass; // flow work
122  }
123 
124  // add terms for inlets
125  for (size_t i = 0; i < m_inlet.size(); i++) {
126  double mdot_in = m_inlet[i]->massFlowRate(time);
127  dmdt += mdot_in; // mass flow into system
128  mcvdTdt += m_inlet[i]->enthalpy_mass() * mdot_in;
129  for (size_t n = 0; n < m_nsp; n++) {
130  double mdot_spec = m_inlet[i]->outletSpeciesMassFlowRate(n);
131  // flow of species into system and dilution by other species
132  dYdt[n] += (mdot_spec - mdot_in * Y[n]) / m_mass;
133 
134  // In combination with h_in*mdot_in, flow work plus thermal
135  // energy carried with the species
136  mcvdTdt -= m_uk[n] / mw[n] * mdot_spec;
137  }
138  }
139 
140  ydot[0] = dmdt;
141  ydot[1] = m_vdot;
142  if (m_energy) {
143  ydot[2] = mcvdTdt / (m_mass * m_thermo->cv_mass());
144  } else {
145  ydot[2] = 0;
146  }
147 
148  resetSensitivity(params);
149 }
150 
151 size_t IdealGasReactor::componentIndex(const string& nm) const
152 {
153  size_t k = speciesIndex(nm);
154  if (k != npos) {
155  return k + 3;
156  } else if (nm == "m" || nm == "mass") {
157  if (nm == "m") {
158  warn_deprecated("IdealGasReactor::componentIndex(\"m\")",
159  "Using the name 'm' for mass is deprecated, and will be "
160  "disabled after Cantera 2.3. Use 'mass' instead.");
161  }
162  return 0;
163  } else if (nm == "V" || nm == "volume") {
164  if (nm == "V") {
165  warn_deprecated("IdealGasReactor::componentIndex(\"V\")",
166  "Using the name 'V' for volume is deprecated, and will be "
167  "disabled after Cantera 2.3. Use 'volume' instead.");
168  }
169  return 1;
170  } else if (nm == "T" || nm == "temperature") {
171  if (nm == "T") {
172  warn_deprecated("IdealGasReactor::componentIndex(\"T\")",
173  "Using the name 'T' for temperature is deprecated, and will be "
174  "disabled after Cantera 2.3. Use 'temperature' instead.");
175  }
176  return 2;
177  } else {
178  return npos;
179  }
180 }
181 
182 std::string IdealGasReactor::componentName(size_t k) {
183  if (k == 2) {
184  return "temperature";
185  } else {
186  return Reactor::componentName(k);
187  }
188 }
189 
190 
191 }
Header file for class Wall.
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
STL namespace.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
virtual std::string type() const
String indicating the thermodynamic model implemented.
Definition: ThermoPhase.h:141
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
Namespace for the Cantera kernel.
Definition: application.cpp:29