Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IdealGasReactor.cpp
Go to the documentation of this file.
1 /**
2  * @file IdealGasReactor.cpp A zero-dimensional reactor
3  */
4 
7 #include "cantera/zeroD/Wall.h"
8 
9 using namespace std;
10 
11 namespace Cantera
12 {
13 
14 void IdealGasReactor::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.eosType() != cIdealGas) {
19  throw CanteraError("IdealGasReactor::setThermoMgr",
20  "Incompatible phase type provided");
21  }
22  Reactor::setThermoMgr(thermo);
23 }
24 
25 void IdealGasReactor::getInitialConditions(double t0, size_t leny, double* y)
26 {
27  if (m_thermo == 0) {
28  cout << "Error: reactor is empty." << endl;
29  return;
30  }
31  m_thermo->restoreState(m_state);
32 
33  // set the first component to the total mass
34  m_mass = m_thermo->density() * m_vol;
35  y[0] = m_mass;
36 
37  // set the second component to the total volume
38  y[1] = m_vol;
39 
40  // Set the third component to the temperature
41  y[2] = m_thermo->temperature();
42 
43  // set components y+3 ... y+K+2 to the mass fractions of each species
44  m_thermo->getMassFractions(y+3);
45 
46  // set the remaining components to the surface species
47  // coverages on the walls
48  getSurfaceInitialConditions(y + m_nsp + 3);
49 }
50 
51 void IdealGasReactor::initialize(doublereal t0)
52 {
53  Reactor::initialize(t0);
54  m_uk.resize(m_nsp, 0.0);
55 }
56 
57 void IdealGasReactor::updateState(doublereal* y)
58 {
59  for (size_t i = 0; i < m_nv; i++) {
60  AssertFinite(y[i], "IdealGasReactor::updateState",
61  "y[" + int2str(i) + "] is not finite");
62  }
63 
64  // The components of y are [0] the total mass, [1] the total volume,
65  // [2] the temperature, [3...K+3] are the mass fractions of each species,
66  // and [K+3...] are the coverages of surface species on each wall.
67  m_mass = y[0];
68  m_vol = y[1];
69  m_thermo->setMassFractions_NoNorm(y+3);
70  m_thermo->setState_TR(y[2], m_mass / m_vol);
71  updateSurfaceState(y + m_nsp + 3);
72 
73  // save parameters needed by other connected reactors
74  m_enthalpy = m_thermo->enthalpy_mass();
75  m_pressure = m_thermo->pressure();
76  m_intEnergy = m_thermo->intEnergy_mass();
77  m_thermo->saveState(m_state);
78 }
79 
80 void IdealGasReactor::evalEqs(doublereal time, doublereal* y,
81  doublereal* ydot, doublereal* params)
82 {
83  double dmdt = 0.0; // dm/dt (gas phase)
84  double mcvdTdt = 0.0; // m * c_v * dT/dt
85  double* dYdt = ydot + 3;
86 
87  m_thermo->restoreState(m_state);
88  applySensitivity(params);
89  m_thermo->getPartialMolarIntEnergies(&m_uk[0]);
90  const vector_fp& mw = m_thermo->molecularWeights();
91  const doublereal* Y = m_thermo->massFractions();
92 
93  if (m_chem) {
94  m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
95  }
96 
97  evalWalls(time);
98  double mdot_surf = evalSurfaces(time, ydot + m_nsp + 3);
99  dmdt += mdot_surf;
100 
101  // compression work and external heat transfer
102  mcvdTdt += - m_pressure * m_vdot - m_Q;
103 
104  for (size_t n = 0; n < m_nsp; n++) {
105  // heat release from gas phase and surface reations
106  mcvdTdt -= m_wdot[n] * m_uk[n] * m_vol;
107  mcvdTdt -= m_sdot[n] * m_uk[n];
108  // production in gas phase and from surfaces
109  dYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n] / m_mass;
110  // dilution by net surface mass flux
111  dYdt[n] -= Y[n] * mdot_surf / m_mass;
112  }
113 
114  // add terms for outlets
115  for (size_t i = 0; i < m_outlet.size(); i++) {
116  double mdot_out = m_outlet[i]->massFlowRate(time);
117  dmdt -= mdot_out; // mass flow out of system
118  mcvdTdt -= mdot_out * m_pressure * m_vol / m_mass; // flow work
119  }
120 
121  // add terms for inlets
122  for (size_t i = 0; i < m_inlet.size(); i++) {
123  double mdot_in = m_inlet[i]->massFlowRate(time);
124  dmdt += mdot_in; // mass flow into system
125  mcvdTdt += m_inlet[i]->enthalpy_mass() * mdot_in;
126  for (size_t n = 0; n < m_nsp; n++) {
127  double mdot_spec = m_inlet[i]->outletSpeciesMassFlowRate(n);
128  // flow of species into system and dilution by other species
129  dYdt[n] += (mdot_spec - mdot_in * Y[n]) / m_mass;
130 
131  // In combintion with h_in*mdot_in, flow work plus thermal
132  // energy carried with the species
133  mcvdTdt -= m_uk[n] / mw[n] * mdot_spec;
134  }
135  }
136 
137  ydot[0] = dmdt;
138  ydot[1] = m_vdot;
139  if (m_energy) {
140  ydot[2] = mcvdTdt / (m_mass * m_thermo->cv_mass());
141  } else {
142  ydot[2] = 0;
143  }
144 
145  for (size_t i = 0; i < m_nv; i++) {
146  AssertFinite(ydot[i], "IdealGasReactor::evalEqs",
147  "ydot[" + int2str(i) + "] is not finite");
148  }
149 
150  resetSensitivity(params);
151 }
152 
153 size_t IdealGasReactor::componentIndex(const string& nm) const
154 {
155  size_t k = speciesIndex(nm);
156  if (k != npos) {
157  return k + 3;
158  } else if (nm == "m" || nm == "mass") {
159  return 0;
160  } else if (nm == "V" || nm == "volume") {
161  return 1;
162  } else if (nm == "T" || nm == "temperature") {
163  return 2;
164  } else {
165  return npos;
166  }
167 }
168 
169 }
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:39
Header file for class Wall.
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
#define AssertFinite(expr, procedure, message)
Throw an exception if the specified exception is not a finite number.
Definition: ctexceptions.h:308
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
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
virtual int eosType() const
Equation of state type flag.
Definition: ThermoPhase.h:142
const int cIdealGas
Equation of state types:
Definition: mix_defs.h:37