Cantera  2.3.0
IdealGasConstPressureReactor.cpp
1 //! @file ConstPressureReactor.cpp A constant pressure 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 
6 #include "cantera/zeroD/IdealGasConstPressureReactor.h"
8 
9 using namespace std;
10 
11 namespace Cantera
12 {
13 
14 void IdealGasConstPressureReactor::setThermoMgr(ThermoPhase& thermo)
15 {
16  //! @TODO: Add a method to ThermoPhase that indicates whether a given
17  //! subclass is compatible with this reactor model
18  if (thermo.type() != "IdealGas") {
19  throw CanteraError("IdealGasReactor::setThermoMgr",
20  "Incompatible phase type provided");
21  }
22  Reactor::setThermoMgr(thermo);
23 }
24 
25 
26 void IdealGasConstPressureReactor::getInitialConditions(double t0, size_t leny,
27  double* y)
28 {
29  warn_deprecated("IdealGasConstPressureReactor::getInitialConditions",
30  "Use getState instead. To be removed after Cantera 2.3.");
31  getState(y);
32 }
33 
34 void IdealGasConstPressureReactor::getState(double* y)
35 {
36  if (m_thermo == 0) {
37  throw CanteraError("getState",
38  "Error: reactor is empty.");
39  }
40  m_thermo->restoreState(m_state);
41 
42  // set the first component to the total mass
43  y[0] = m_thermo->density() * m_vol;
44 
45  // set the second component to the temperature
46  y[1] = m_thermo->temperature();
47 
48  // set components y+2 ... y+K+1 to the mass fractions Y_k of each species
49  m_thermo->getMassFractions(y+2);
50 
51  // set the remaining components to the surface species
52  // coverages on the walls
53  getSurfaceInitialConditions(y + m_nsp + 2);
54 }
55 
56 void IdealGasConstPressureReactor::initialize(doublereal t0)
57 {
58  ConstPressureReactor::initialize(t0);
59  m_hk.resize(m_nsp, 0.0);
60 }
61 
62 void IdealGasConstPressureReactor::updateState(doublereal* y)
63 {
64  // The components of y are [0] the total mass, [1] the temperature,
65  // [2...K+2) are the mass fractions of each species, and [K+2...] are the
66  // coverages of surface species on each wall.
67  m_mass = y[0];
68  m_thermo->setMassFractions_NoNorm(y+2);
69  m_thermo->setState_TP(y[1], m_pressure);
70  m_vol = m_mass / m_thermo->density();
71  updateSurfaceState(y + m_nsp + 2);
72 
73  // save parameters needed by other connected reactors
74  m_enthalpy = m_thermo->enthalpy_mass();
75  m_intEnergy = m_thermo->intEnergy_mass();
76  m_thermo->saveState(m_state);
77 }
78 
79 void IdealGasConstPressureReactor::evalEqs(doublereal time, doublereal* y,
80  doublereal* ydot, doublereal* params)
81 {
82  double dmdt = 0.0; // dm/dt (gas phase)
83  double mcpdTdt = 0.0; // m * c_p * dT/dt
84  double* dYdt = ydot + 2;
85 
86  m_thermo->restoreState(m_state);
87  applySensitivity(params);
88  evalWalls(time);
89  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 2);
90  dmdt += mdot_surf;
91 
92  m_thermo->getPartialMolarEnthalpies(&m_hk[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  // external heat transfer
101  mcpdTdt -= m_Q;
102 
103  for (size_t n = 0; n < m_nsp; n++) {
104  // heat release from gas phase and surface reactions
105  mcpdTdt -= m_wdot[n] * m_hk[n] * m_vol;
106  mcpdTdt -= m_sdot[n] * m_hk[n];
107  // production in gas phase and from surfaces
108  dYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n] / m_mass;
109  // dilution by net surface mass flux
110  dYdt[n] -= Y[n] * mdot_surf / m_mass;
111  }
112 
113  // add terms for outlets
114  for (size_t i = 0; i < m_outlet.size(); i++) {
115  dmdt -= m_outlet[i]->massFlowRate(time); // mass flow out of system
116  }
117 
118  // add terms for inlets
119  for (size_t i = 0; i < m_inlet.size(); i++) {
120  double mdot_in = m_inlet[i]->massFlowRate(time);
121  dmdt += mdot_in; // mass flow into system
122  mcpdTdt += m_inlet[i]->enthalpy_mass() * mdot_in;
123  for (size_t n = 0; n < m_nsp; n++) {
124  double mdot_spec = m_inlet[i]->outletSpeciesMassFlowRate(n);
125  // flow of species into system and dilution by other species
126  dYdt[n] += (mdot_spec - mdot_in * Y[n]) / m_mass;
127  mcpdTdt -= m_hk[n] / mw[n] * mdot_spec;
128  }
129  }
130 
131  ydot[0] = dmdt;
132  if (m_energy) {
133  ydot[1] = mcpdTdt / (m_mass * m_thermo->cp_mass());
134  } else {
135  ydot[1] = 0.0;
136  }
137 
138  resetSensitivity(params);
139 }
140 
141 size_t IdealGasConstPressureReactor::componentIndex(const string& nm) const
142 {
143  size_t k = speciesIndex(nm);
144  if (k != npos) {
145  return k + 2;
146  } else if (nm == "m" || nm == "mass") {
147  if (nm == "m") {
148  warn_deprecated("IdealGasConstPressureReactor::componentIndex(\"m\")",
149  "Using the name 'm' for mass is deprecated, and will be "
150  "disabled after Cantera 2.3. Use 'mass' instead.");
151  }
152  return 0;
153  } else if (nm == "T" || nm == "temperature") {
154  if (nm == "T") {
155  warn_deprecated("IdealGasConstPressureReactor::componentIndex(\"T\")",
156  "Using the name 'T' for temperature is deprecated, and will be "
157  "disabled after Cantera 2.3. Use 'temperature' instead.");
158  }
159  return 1;
160  } else {
161  return npos;
162  }
163 }
164 
165 std::string IdealGasConstPressureReactor::componentName(size_t k) {
166  if (k == 1) {
167  return "temperature";
168  } else {
169  return ConstPressureReactor::componentName(k);
170  }
171 }
172 
173 }
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