Cantera  2.5.1
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 https://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("ConstPressureReactor::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  updateConnected(false);
59 }
60 
61 void ConstPressureReactor::evalEqs(doublereal time, doublereal* y,
62  doublereal* ydot, doublereal* params)
63 {
64  double dmdt = 0.0; // dm/dt (gas phase)
65  double* dYdt = ydot + 2;
66 
67  evalWalls(time);
68  applySensitivity(params);
69 
70  m_thermo->restoreState(m_state);
71  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 2);
72  dmdt += mdot_surf;
73 
74  const vector_fp& mw = m_thermo->molecularWeights();
75  const doublereal* Y = m_thermo->massFractions();
76 
77  if (m_chem) {
78  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
79  }
80 
81  for (size_t k = 0; k < m_nsp; k++) {
82  // production in gas phase and from surfaces
83  dYdt[k] = (m_wdot[k] * m_vol + m_sdot[k]) * mw[k] / m_mass;
84  // dilution by net surface mass flux
85  dYdt[k] -= Y[k] * mdot_surf / m_mass;
86  }
87 
88  // external heat transfer
89  double dHdt = - m_Q;
90 
91  // add terms for outlets
92  for (auto outlet : m_outlet) {
93  double mdot = outlet->massFlowRate();
94  dmdt -= mdot;
95  dHdt -= mdot * m_enthalpy;
96  }
97 
98  // add terms for inlets
99  for (auto inlet : m_inlet) {
100  double mdot = inlet->massFlowRate();
101  dmdt += mdot; // mass flow into system
102  for (size_t n = 0; n < m_nsp; n++) {
103  double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
104  // flow of species into system and dilution by other species
105  dYdt[n] += (mdot_spec - mdot * Y[n]) / m_mass;
106  }
107  dHdt += mdot * inlet->enthalpy_mass();
108  }
109 
110  ydot[0] = dmdt;
111  if (m_energy) {
112  ydot[1] = dHdt;
113  } else {
114  ydot[1] = 0.0;
115  }
116 
117  // reset sensitivity parameters
118  resetSensitivity(params);
119 }
120 
121 size_t ConstPressureReactor::componentIndex(const string& nm) const
122 {
123  size_t k = speciesIndex(nm);
124  if (k != npos) {
125  return k + 2;
126  } else if (nm == "mass") {
127  return 0;
128  } else if (nm == "enthalpy") {
129  return 1;
130  } else {
131  return npos;
132  }
133 }
134 
135 std::string ConstPressureReactor::componentName(size_t k) {
136  if (k == 0) {
137  return "mass";
138  } else if (k == 1) {
139  return "enthalpy";
140  } else if (k >= 2 && k < neq()) {
141  k -= 2;
142  if (k < m_thermo->nSpecies()) {
143  return m_thermo->speciesName(k);
144  } else {
145  k -= m_thermo->nSpecies();
146  }
147  for (auto& S : m_surfaces) {
148  ThermoPhase* th = S->thermo();
149  if (k < th->nSpecies()) {
150  return th->speciesName(k);
151  } else {
152  k -= th->nSpecies();
153  }
154  }
155  }
156  throw CanteraError("ConstPressureReactor::componentName",
157  "Index is out of bounds.");
158 }
159 
160 }
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
Header file for base class WallBase.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:285
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:229
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:188
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:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264