Cantera  2.4.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::getState(double* y)
17 {
18  if (m_thermo == 0) {
19  throw CanteraError("getState",
20  "Error: reactor is empty.");
21  }
22  m_thermo->restoreState(m_state);
23 
24  // set the first component to the total mass
25  y[0] = m_thermo->density() * m_vol;
26 
27  // set the second component to the total enthalpy
28  y[1] = m_thermo->enthalpy_mass() * m_thermo->density() * m_vol;
29 
30  // set components y+2 ... y+K+1 to the mass fractions Y_k of each species
31  m_thermo->getMassFractions(y+2);
32 
33  // set the remaining components to the surface species
34  // coverages on the walls
35  getSurfaceInitialConditions(y + m_nsp + 2);
36 }
37 
38 void ConstPressureReactor::initialize(doublereal t0)
39 {
40  Reactor::initialize(t0);
41  m_nv -= 1; // Constant pressure reactor has one fewer state variable
42 }
43 
44 void ConstPressureReactor::updateState(doublereal* y)
45 {
46  // The components of y are [0] the total mass, [1] the total enthalpy,
47  // [2...K+2) are the mass fractions of each species, and [K+2...] are the
48  // coverages of surface species on each wall.
49  m_mass = y[0];
50  m_thermo->setMassFractions_NoNorm(y+2);
51  if (m_energy) {
52  m_thermo->setState_HP(y[1]/m_mass, m_pressure);
53  } else {
54  m_thermo->setPressure(m_pressure);
55  }
56  m_vol = m_mass / m_thermo->density();
57  updateSurfaceState(y + m_nsp + 2);
58 
59  // save parameters needed by other connected reactors
60  m_enthalpy = m_thermo->enthalpy_mass();
61  m_intEnergy = m_thermo->intEnergy_mass();
62  m_thermo->saveState(m_state);
63 }
64 
65 void ConstPressureReactor::evalEqs(doublereal time, doublereal* y,
66  doublereal* ydot, doublereal* params)
67 {
68  double dmdt = 0.0; // dm/dt (gas phase)
69  double* dYdt = ydot + 2;
70  m_thermo->restoreState(m_state);
71  applySensitivity(params);
72  evalWalls(time);
73  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 2);
74  dmdt += mdot_surf;
75 
76  const vector_fp& mw = m_thermo->molecularWeights();
77  const doublereal* Y = m_thermo->massFractions();
78 
79  if (m_chem) {
80  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
81  }
82 
83  for (size_t k = 0; k < m_nsp; k++) {
84  // production in gas phase and from surfaces
85  dYdt[k] = (m_wdot[k] * m_vol + m_sdot[k]) * mw[k] / m_mass;
86  // dilution by net surface mass flux
87  dYdt[k] -= Y[k] * mdot_surf / m_mass;
88  }
89 
90  // external heat transfer
91  double dHdt = - m_Q;
92 
93  // add terms for outlets
94  for (size_t i = 0; i < m_outlet.size(); i++) {
95  double mdot_out = m_outlet[i]->massFlowRate(time); // mass flow out of system
96  dmdt -= mdot_out;
97  dHdt -= mdot_out * m_enthalpy;
98  }
99 
100  // add terms for inlets
101  for (size_t i = 0; i < m_inlet.size(); i++) {
102  double mdot_in = m_inlet[i]->massFlowRate(time);
103  dmdt += mdot_in; // mass flow into system
104  for (size_t n = 0; n < m_nsp; n++) {
105  double mdot_spec = m_inlet[i]->outletSpeciesMassFlowRate(n);
106  // flow of species into system and dilution by other species
107  dYdt[n] += (mdot_spec - mdot_in * Y[n]) / m_mass;
108  }
109  dHdt += mdot_in * m_inlet[i]->enthalpy_mass();
110  }
111 
112  ydot[0] = dmdt;
113  if (m_energy) {
114  ydot[1] = dHdt;
115  } else {
116  ydot[1] = 0.0;
117  }
118 
119  // reset sensitivity parameters
120  resetSensitivity(params);
121 }
122 
123 size_t ConstPressureReactor::componentIndex(const string& nm) const
124 {
125  size_t k = speciesIndex(nm);
126  if (k != npos) {
127  return k + 2;
128  } else if (nm == "mass") {
129  return 0;
130  } else if (nm == "enthalpy") {
131  return 1;
132  } else {
133  return npos;
134  }
135 }
136 
137 std::string ConstPressureReactor::componentName(size_t k) {
138  if (k == 0) {
139  return "mass";
140  } else if (k == 1) {
141  return "enthalpy";
142  } else if (k >= 2 && k < neq()) {
143  k -= 2;
144  if (k < m_thermo->nSpecies()) {
145  return m_thermo->speciesName(k);
146  } else {
147  k -= m_thermo->nSpecies();
148  }
149  for (auto& S : m_surfaces) {
150  ThermoPhase* th = S->thermo();
151  if (k < th->nSpecies()) {
152  return th->speciesName(k);
153  } else {
154  k -= th->nSpecies();
155  }
156  }
157  }
158  throw CanteraError("ConstPressureReactor::componentName",
159  "Index is out of bounds.");
160 }
161 
162 }
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
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:266
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:191
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: AnyMap.cpp:8