Cantera  2.3.0
ConstPressureReactor.cpp
Go to the documentation of this file.
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 
8 #include "cantera/zeroD/Wall.h"
10 
11 using namespace std;
12 
13 namespace Cantera
14 {
15 
16 void ConstPressureReactor::getInitialConditions(double t0, size_t leny,
17  double* y)
18 {
19  warn_deprecated("ConstPressureReactor::getInitialConditions",
20  "Use getState instead. To be removed after Cantera 2.3.");
21  getState(y);
22 }
23 
24 void ConstPressureReactor::getState(double* y)
25 {
26  if (m_thermo == 0) {
27  throw CanteraError("getState",
28  "Error: reactor is empty.");
29  }
30  m_thermo->restoreState(m_state);
31 
32  // set the first component to the total mass
33  y[0] = m_thermo->density() * m_vol;
34 
35  // set the second component to the total enthalpy
36  y[1] = m_thermo->enthalpy_mass() * m_thermo->density() * m_vol;
37 
38  // set components y+2 ... y+K+1 to the mass fractions Y_k of each species
39  m_thermo->getMassFractions(y+2);
40 
41  // set the remaining components to the surface species
42  // coverages on the walls
43  getSurfaceInitialConditions(y + m_nsp + 2);
44 }
45 
46 void ConstPressureReactor::initialize(doublereal t0)
47 {
48  Reactor::initialize(t0);
49  m_nv -= 1; // Constant pressure reactor has one fewer state variable
50 }
51 
52 void ConstPressureReactor::updateState(doublereal* y)
53 {
54  // The components of y are [0] the total mass, [1] the total enthalpy,
55  // [2...K+2) are the mass fractions of each species, and [K+2...] are the
56  // coverages of surface species on each wall.
57  m_mass = y[0];
58  m_thermo->setMassFractions_NoNorm(y+2);
59  if (m_energy) {
60  m_thermo->setState_HP(y[1]/m_mass, m_pressure);
61  } else {
62  m_thermo->setPressure(m_pressure);
63  }
64  m_vol = m_mass / m_thermo->density();
65  updateSurfaceState(y + m_nsp + 2);
66 
67  // save parameters needed by other connected reactors
68  m_enthalpy = m_thermo->enthalpy_mass();
69  m_intEnergy = m_thermo->intEnergy_mass();
70  m_thermo->saveState(m_state);
71 }
72 
73 void ConstPressureReactor::evalEqs(doublereal time, doublereal* y,
74  doublereal* ydot, doublereal* params)
75 {
76  double dmdt = 0.0; // dm/dt (gas phase)
77  double* dYdt = ydot + 2;
78  m_thermo->restoreState(m_state);
79  applySensitivity(params);
80  evalWalls(time);
81  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 2);
82  dmdt += mdot_surf;
83 
84  const vector_fp& mw = m_thermo->molecularWeights();
85  const doublereal* Y = m_thermo->massFractions();
86 
87  if (m_chem) {
88  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
89  }
90 
91  for (size_t k = 0; k < m_nsp; k++) {
92  // production in gas phase and from surfaces
93  dYdt[k] = (m_wdot[k] * m_vol + m_sdot[k]) * mw[k] / m_mass;
94  // dilution by net surface mass flux
95  dYdt[k] -= Y[k] * mdot_surf / m_mass;
96  }
97 
98  // external heat transfer
99  double dHdt = - m_Q;
100 
101  // add terms for outlets
102  for (size_t i = 0; i < m_outlet.size(); i++) {
103  double mdot_out = m_outlet[i]->massFlowRate(time); // mass flow out of system
104  dmdt -= mdot_out;
105  dHdt -= mdot_out * m_enthalpy;
106  }
107 
108  // add terms for inlets
109  for (size_t i = 0; i < m_inlet.size(); i++) {
110  double mdot_in = m_inlet[i]->massFlowRate(time);
111  dmdt += mdot_in; // mass flow into system
112  for (size_t n = 0; n < m_nsp; n++) {
113  double mdot_spec = m_inlet[i]->outletSpeciesMassFlowRate(n);
114  // flow of species into system and dilution by other species
115  dYdt[n] += (mdot_spec - mdot_in * Y[n]) / m_mass;
116  }
117  dHdt += mdot_in * m_inlet[i]->enthalpy_mass();
118  }
119 
120  ydot[0] = dmdt;
121  if (m_energy) {
122  ydot[1] = dHdt;
123  } else {
124  ydot[1] = 0.0;
125  }
126 
127  // reset sensitivity parameters
128  resetSensitivity(params);
129 }
130 
131 size_t ConstPressureReactor::componentIndex(const string& nm) const
132 {
133  size_t k = speciesIndex(nm);
134  if (k != npos) {
135  return k + 2;
136  } else if (nm == "m" || nm == "mass") {
137  if (nm == "m") {
138  warn_deprecated("ConstPressureReactor::componentIndex(\"m\")",
139  "Using the name 'm' for mass is deprecated, and will be "
140  "disabled after Cantera 2.3. Use 'mass' instead.");
141  }
142  return 0;
143  } else if (nm == "H" || nm == "enthalpy") {
144  if (nm == "H") {
145  warn_deprecated("ConstPressureReactor::componentIndex(\"H\")",
146  "Using the name 'H' for enthalpy is deprecated, and will be "
147  "disabled after Cantera 2.3. Use 'enthalpy' instead.");
148  }
149  return 1;
150  } else {
151  return npos;
152  }
153 }
154 
155 std::string ConstPressureReactor::componentName(size_t k) {
156  if (k == 0) {
157  return "mass";
158  } else if (k == 1) {
159  return "enthalpy";
160  } else if (k >= 2 && k < neq()) {
161  k -= 2;
162  if (k < m_thermo->nSpecies()) {
163  return m_thermo->speciesName(k);
164  } else {
165  k -= m_thermo->nSpecies();
166  }
167  for (auto& S : m_surfaces) {
168  ThermoPhase* th = S->thermo();
169  if (k < th->nSpecies()) {
170  return th->speciesName(k);
171  } else {
172  k -= th->nSpecies();
173  }
174  }
175  }
176  throw CanteraError("ConstPressureReactor::componentName",
177  "Index is out of bounds.");
178 }
179 
180 }
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase, assuming an ideal solution model (see Thermodynamic Properties and class SurfPhase).
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
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:262
STL namespace.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:267
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
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