Cantera  3.2.0a4
Loading...
Searching...
No Matches
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"
12
13namespace Cantera
14{
15
17{
18 if (m_thermo == 0) {
19 throw CanteraError("IdealGasConstPressureReactor::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 temperature
28 y[1] = m_thermo->temperature();
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
36}
37
39{
40 //! @todo: Add a method to ThermoPhase that indicates whether a given
41 //! subclass is compatible with this reactor model
42 if (m_thermo->type() != "ideal-gas") {
43 throw CanteraError("IdealGasConstPressureReactor::initialize",
44 "Incompatible phase type '{}' provided", m_thermo->type());
46 m_hk.resize(m_nsp, 0.0);
47}
48
50{
51 // The components of y are [0] the total mass, [1] the temperature,
52 // [2...K+2) are the mass fractions of each species, and [K+2...] are the
53 // coverages of surface species on each wall.
54 m_mass = y[0];
55 m_thermo->setMassFractions_NoNorm(y+2);
56 m_thermo->setState_TP(y[1], m_pressure);
57 m_vol = m_mass / m_thermo->density();
58 updateConnected(false);
60}
61
62void IdealGasConstPressureReactor::eval(double time, double* LHS, double* RHS)
63{
64 double& dmdt = RHS[0]; // dm/dt (gas phase)
65 double& mcpdTdt = RHS[1]; // m * c_p * dT/dt
66 double* mdYdt = RHS + 2; // mass * dY/dt
67
68 dmdt = 0.0;
69 mcpdTdt = 0.0;
70
71 evalWalls(time);
72
73 m_thermo->restoreState(m_state);
74 const vector<double>& mw = m_thermo->molecularWeights();
75 const double* Y = m_thermo->massFractions();
76
77 evalSurfaces(LHS + m_nsp + 2, RHS + m_nsp + 2, m_sdot.data());
78 double mdot_surf = dot(m_sdot.begin(), m_sdot.end(), mw.begin());
79 dmdt += mdot_surf;
80
81 m_thermo->getPartialMolarEnthalpies(&m_hk[0]);
82
83 if (m_chem) {
84 m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
85 }
86
87 // external heat transfer
88 mcpdTdt += m_Qdot;
89
90 for (size_t n = 0; n < m_nsp; n++) {
91 // heat release from gas phase and surface reactions
92 mcpdTdt -= m_wdot[n] * m_hk[n] * m_vol;
93 mcpdTdt -= m_sdot[n] * m_hk[n];
94 // production in gas phase and from surfaces
95 mdYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n];
96 // dilution by net surface mass flux
97 mdYdt[n] -= Y[n] * mdot_surf;
98 //Assign left-hand side of dYdt ODE as total mass
99 LHS[n+2] = 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 mdYdt[n] += mdot_spec - mdot * Y[n];
116 mcpdTdt -= m_hk[n] / mw[n] * mdot_spec;
117 }
118 }
119
120 if (m_energy) {
121 LHS[1] = m_mass * m_thermo->cp_mass();
122 } else {
123 RHS[1] = 0.0;
124 }
125}
126
128{
129 if (nSurfs() != 0) {
130 throw CanteraError("IdealGasConstPressureReactor::steadyConstraints",
131 "Steady state solver cannot currently be used with "
132 " IdealGasConstPressureReactor when reactor surfaces are present.\n"
133 "See https://github.com/Cantera/enhancements/issues/234");
134 }
135 if (energyEnabled()) {
136 return {0}; // mass
137 } else {
138 return {0, 1}; // mass and temperature
139 }
140}
141
143{
144 size_t k = speciesIndex(nm);
145 if (k != npos) {
146 return k + 2;
147 } else if (nm == "mass") {
148 return 0;
149 } else if (nm == "temperature") {
150 return 1;
151 } else {
152 return npos;
153 }
154}
155
157 if (k == 1) {
158 return "temperature";
159 } else {
161 }
162}
163
165{
166 if (k == 1) {
167 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
168 return 1.5 * m_thermo->maxTemp();
169 } else {
171 }
172}
173
175{
176 if (k == 1) {
177 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
178 return 0.5 * m_thermo->minTemp();
179 } else {
181 }
182}
183
184}
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Base class for exceptions thrown by Cantera classes.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
string componentName(size_t k) override
Return the name of the solution component with index i.
void initialize(double t0=0.0) override
Initialize the reactor.
double outletSpeciesMassFlowRate(size_t k)
Mass flow rate (kg/s) of outlet species k.
double enthalpy_mass()
specific enthalpy
double massFlowRate()
Mass flow rate (kg/s).
Definition FlowDevice.h:36
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
vector< size_t > steadyConstraints() const override
Get the indices of equations that are algebraic constraints when solving the steady-state problem.
void eval(double t, double *LHS, double *RHS) override
Evaluate the reactor governing equations.
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
void getState(double *y) override
Get the the current state of the reactor.
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
string componentName(size_t k) override
Return the name of the solution component with index i.
void updateState(double *y) override
Set the state of the reactor to correspond to the state vector y.
void initialize(double t0=0.0) override
Initialize the reactor.
vector< double > m_hk
Species molar enthalpies.
virtual void getNetProductionRates(double *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition Kinetics.cpp:428
void restoreState(const vector< double > &state)
Restore a state saved on a previous call to saveState.
Definition Phase.cpp:286
virtual void setMassFractions_NoNorm(const double *const y)
Set the mass fractions to the specified values without normalizing.
Definition Phase.cpp:376
double temperature() const
Temperature (K).
Definition Phase.h:587
const double * massFractions() const
Return a const pointer to the mass fraction array.
Definition Phase.h:467
const vector< double > & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition Phase.cpp:423
virtual double density() const
Density (kg/m^3).
Definition Phase.h:612
void getMassFractions(double *const y) const
Get the species mass fractions.
Definition Phase.cpp:499
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
double m_pressure
Current pressure in the reactor [Pa].
virtual size_t nSurfs() const
Return the number of surfaces in a reactor.
FlowDevice & inlet(size_t n=0)
Return a reference to the n-th inlet FlowDevice connected to this reactor.
double m_vol
Current volume of the reactor [m^3].
double m_mass
Current mass of the reactor [kg].
size_t m_nsp
Number of homogeneous species in the mixture.
virtual void evalSurfaces(double *LHS, double *RHS, double *sdot)
Evaluate terms related to surface reactions.
Definition Reactor.cpp:309
virtual void updateSurfaceState(double *y)
Update the state of SurfPhase objects attached to this reactor.
Definition Reactor.cpp:193
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:293
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:304
virtual void evalWalls(double t)
Evaluate terms related to Walls.
Definition Reactor.cpp:297
bool energyEnabled() const
Returns true if solution of the energy equation is enabled.
Definition Reactor.h:95
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:297
vector< double > m_sdot
Production rates of gas phase species on surfaces [kmol/s].
Definition Reactor.h:302
virtual void getSurfaceInitialConditions(double *y)
Get initial conditions for SurfPhase objects attached to this reactor.
Definition Reactor.cpp:99
virtual size_t speciesIndex(const string &nm) const
Return the index in the solution vector for this reactor of the species named nm, in either the homog...
Definition Reactor.cpp:464
virtual void updateConnected(bool updatePressure)
Update the state information needed by connected reactors, flow devices, and reactor walls.
Definition Reactor.cpp:202
virtual void getPartialMolarEnthalpies(double *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual void setState_TP(double t, double p)
Set the temperature (K) and pressure (Pa)
virtual double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid.
string type() const override
String indicating the thermodynamic model implemented.
virtual double maxTemp(size_t k=npos) const
Maximum temperature for which the thermodynamic data for the species are valid.
double cp_mass() const
Specific heat at constant pressure. Units: J/kg/K.
double dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition utilities.h:82
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...