Cantera  2.5.1
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 https://cantera.org/license.txt for license and copyright information.
5 
6 #include "cantera/zeroD/IdealGasConstPressureReactor.h"
9 
10 using namespace std;
11 
12 namespace Cantera
13 {
14 
15 void IdealGasConstPressureReactor::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("IdealGasConstPressureReactor::setThermoMgr",
21  "Incompatible phase type provided");
22  }
23  Reactor::setThermoMgr(thermo);
24 }
25 
26 void IdealGasConstPressureReactor::getState(double* y)
27 {
28  if (m_thermo == 0) {
29  throw CanteraError("IdealGasConstPressureReactor::getState",
30  "Error: reactor is empty.");
31  }
32  m_thermo->restoreState(m_state);
33 
34  // set the first component to the total mass
35  y[0] = m_thermo->density() * m_vol;
36 
37  // set the second component to the temperature
38  y[1] = m_thermo->temperature();
39 
40  // set components y+2 ... y+K+1 to the mass fractions Y_k of each species
41  m_thermo->getMassFractions(y+2);
42 
43  // set the remaining components to the surface species
44  // coverages on the walls
45  getSurfaceInitialConditions(y + m_nsp + 2);
46 }
47 
48 void IdealGasConstPressureReactor::initialize(doublereal t0)
49 {
50  ConstPressureReactor::initialize(t0);
51  m_hk.resize(m_nsp, 0.0);
52 }
53 
54 void IdealGasConstPressureReactor::updateState(doublereal* y)
55 {
56  // The components of y are [0] the total mass, [1] the temperature,
57  // [2...K+2) are the mass fractions of each species, and [K+2...] are the
58  // coverages of surface species on each wall.
59  m_mass = y[0];
60  m_thermo->setMassFractions_NoNorm(y+2);
61  m_thermo->setState_TP(y[1], m_pressure);
62  m_vol = m_mass / m_thermo->density();
63  updateSurfaceState(y + m_nsp + 2);
64  updateConnected(false);
65 }
66 
67 void IdealGasConstPressureReactor::evalEqs(doublereal time, doublereal* y,
68  doublereal* ydot, doublereal* params)
69 {
70  double dmdt = 0.0; // dm/dt (gas phase)
71  double mcpdTdt = 0.0; // m * c_p * dT/dt
72  double* dYdt = ydot + 2;
73 
74  applySensitivity(params);
75  evalWalls(time);
76 
77  m_thermo->restoreState(m_state);
78  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 2);
79  dmdt += mdot_surf;
80 
81  m_thermo->getPartialMolarEnthalpies(&m_hk[0]);
82  const vector_fp& mw = m_thermo->molecularWeights();
83  const doublereal* Y = m_thermo->massFractions();
84 
85  if (m_chem) {
86  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
87  }
88 
89  // external heat transfer
90  mcpdTdt -= m_Q;
91 
92  for (size_t n = 0; n < m_nsp; n++) {
93  // heat release from gas phase and surface reactions
94  mcpdTdt -= m_wdot[n] * m_hk[n] * m_vol;
95  mcpdTdt -= m_sdot[n] * m_hk[n];
96  // production in gas phase and from surfaces
97  dYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n] / m_mass;
98  // dilution by net surface mass flux
99  dYdt[n] -= Y[n] * mdot_surf / m_mass;
100  }
101 
102  // add terms for outlets
103  for (auto outlet : m_outlet) {
104  dmdt -= outlet->massFlowRate(); // mass flow out of system
105  }
106 
107  // add terms for inlets
108  for (auto inlet : m_inlet) {
109  double mdot = inlet->massFlowRate();
110  dmdt += mdot; // mass flow into system
111  mcpdTdt += inlet->enthalpy_mass() * mdot;
112  for (size_t n = 0; n < m_nsp; n++) {
113  double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
114  // flow of species into system and dilution by other species
115  dYdt[n] += (mdot_spec - mdot * Y[n]) / m_mass;
116  mcpdTdt -= m_hk[n] / mw[n] * mdot_spec;
117  }
118  }
119 
120  ydot[0] = dmdt;
121  if (m_energy) {
122  ydot[1] = mcpdTdt / (m_mass * m_thermo->cp_mass());
123  } else {
124  ydot[1] = 0.0;
125  }
126 
127  resetSensitivity(params);
128 }
129 
130 size_t IdealGasConstPressureReactor::componentIndex(const string& nm) const
131 {
132  size_t k = speciesIndex(nm);
133  if (k != npos) {
134  return k + 2;
135  } else if (nm == "mass") {
136  return 0;
137  } else if (nm == "temperature") {
138  return 1;
139  } else {
140  return npos;
141  }
142 }
143 
144 std::string IdealGasConstPressureReactor::componentName(size_t k) {
145  if (k == 1) {
146  return "temperature";
147  } else {
148  return ConstPressureReactor::componentName(k);
149  }
150 }
151 
152 }
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
virtual std::string type() const
String indicating the thermodynamic model implemented.
Definition: ThermoPhase.h:113
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