Cantera  3.2.0a5
Loading...
Searching...
No Matches
IdealGasReactor.cpp
Go to the documentation of this file.
1//! @file IdealGasReactor.cpp A 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
12
13namespace Cantera
14{
15
17{
18 if (m_thermo == 0) {
19 throw CanteraError("IdealGasReactor::getState",
20 "Error: reactor is empty.");
21 }
22 m_thermo->restoreState(m_state);
23
24 // set the first component to the total mass
25 m_mass = m_thermo->density() * m_vol;
26 y[0] = m_mass;
27
28 // set the second component to the total volume
29 y[1] = m_vol;
30
31 // Set the third component to the temperature
32 y[2] = m_thermo->temperature();
33
34 // set components y+3 ... y+K+2 to the mass fractions of each species
35 m_thermo->getMassFractions(y+3);
36
37 // set the remaining components to the surface species
38 // coverages on the walls
40}
41
43{
44 //! @todo: Add a method to ThermoPhase that indicates whether a given
45 //! subclass is compatible with this reactor model
46 if (m_thermo->type() != "ideal-gas") {
47 throw CanteraError("IdealGasReactor::initialize",
48 "Incompatible phase type '{}' provided", m_thermo->type());
49 }
51 m_uk.resize(m_nsp, 0.0);
52}
53
55{
56 // The components of y are [0] the total mass, [1] the total volume,
57 // [2] the temperature, [3...K+3] are the mass fractions of each species,
58 // and [K+3...] are the coverages of surface species on each wall.
59 m_mass = y[0];
60 m_vol = y[1];
61 m_thermo->setMassFractions_NoNorm(y+3);
62 m_thermo->setState_TD(y[2], m_mass / m_vol);
63 updateConnected(true);
65}
66
67void IdealGasReactor::eval(double time, double* LHS, double* RHS)
68{
69 double& dmdt = RHS[0]; // dm/dt (gas phase)
70 double& mcvdTdt = RHS[2]; // m * c_v * dT/dt
71 double* mdYdt = RHS + 3; // mass * dY/dt
72
73 evalWalls(time);
74 m_thermo->restoreState(m_state);
75 m_thermo->getPartialMolarIntEnergies(&m_uk[0]);
76 const vector<double>& mw = m_thermo->molecularWeights();
77 const double* Y = m_thermo->massFractions();
78
79 if (m_chem) {
80 m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
81 }
82
83 evalSurfaces(LHS + m_nsp + 3, RHS + m_nsp + 3, m_sdot.data());
84 double mdot_surf = dot(m_sdot.begin(), m_sdot.end(), mw.begin());
85 dmdt += mdot_surf;
86
87 // compression work and external heat transfer
88 mcvdTdt += - m_pressure * m_vdot + m_Qdot;
89
90 for (size_t n = 0; n < m_nsp; n++) {
91 // heat release from gas phase and surface reactions
92 mcvdTdt -= m_wdot[n] * m_uk[n] * m_vol;
93 mcvdTdt -= m_sdot[n] * m_uk[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+3] = m_mass;
100 }
101
102 // add terms for outlets
103 for (auto outlet : m_outlet) {
104 double mdot = outlet->massFlowRate();
105 dmdt -= mdot; // mass flow out of system
106 mcvdTdt -= mdot * m_pressure * m_vol / m_mass; // flow work
107 }
108
109 // add terms for inlets
110 for (auto inlet : m_inlet) {
111 double mdot = inlet->massFlowRate();
112 dmdt += mdot; // mass flow into system
113 mcvdTdt += inlet->enthalpy_mass() * mdot;
114 for (size_t n = 0; n < m_nsp; n++) {
115 double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
116 // flow of species into system and dilution by other species
117 mdYdt[n] += mdot_spec - mdot * Y[n];
118
119 // In combination with h_in*mdot_in, flow work plus thermal
120 // energy carried with the species
121 mcvdTdt -= m_uk[n] / mw[n] * mdot_spec;
122 }
123 }
124
125 RHS[1] = m_vdot;
126 if (m_energy) {
127 LHS[2] = m_mass * m_thermo->cv_mass();
128 } else {
129 RHS[2] = 0;
130 }
131}
132
134{
135 if (nSurfs() != 0) {
136 throw CanteraError("IdealGasReactor::steadyConstraints",
137 "Steady state solver cannot currently be used with IdealGasReactor"
138 " when reactor surfaces are present.\n"
139 "See https://github.com/Cantera/enhancements/issues/234");
140 }
141 if (energyEnabled()) {
142 return {1}; // volume
143 } else {
144 return {1, 2}; // volume and temperature
145 }
146}
147
148size_t IdealGasReactor::componentIndex(const string& nm) const
149{
150 if (nm == "mass") {
151 return 0;
152 }
153 if (nm == "volume") {
154 return 1;
155 }
156 if (nm == "temperature") {
157 return 2;
158 }
159 try {
160 return speciesIndex(nm) + 3;
161 } catch (const CanteraError&) {
162 throw CanteraError("IdealGasReactor::componentIndex",
163 "Component '{}' not found", nm);
164 }
165}
166
168 if (k == 2) {
169 return "temperature";
170 } else {
171 return Reactor::componentName(k);
172 }
173}
174
175double IdealGasReactor::upperBound(size_t k) const {
176 if (k == 2) {
177 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
178 return 1.5 * m_thermo->maxTemp();
179 } else {
180 return Reactor::upperBound(k);
181 }
182}
183
184double IdealGasReactor::lowerBound(size_t k) const {
185 if (k == 2) {
186 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
187 return 0.5 * m_thermo->minTemp();
188 } else {
189 return Reactor::lowerBound(k);
190 }
191}
192
193}
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,...
Header file for base class WallBase.
Base class for exceptions thrown by Cantera classes.
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.
vector< double > m_uk
Species molar internal energies.
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.
virtual void getNetProductionRates(double *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition Kinetics.cpp:489
void restoreState(const vector< double > &state)
Restore a state saved on a previous call to saveState.
Definition Phase.cpp:323
virtual void setMassFractions_NoNorm(const double *const y)
Set the mass fractions to the specified values without normalizing.
Definition Phase.cpp:413
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition Phase.cpp:435
double temperature() const
Temperature (K).
Definition Phase.h:629
const double * massFractions() const
Return a const pointer to the mass fraction array.
Definition Phase.h:509
const vector< double > & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition Phase.cpp:460
virtual double density() const
Density (kg/m^3).
Definition Phase.h:654
void getMassFractions(double *const y) const
Get the species mass fractions.
Definition Phase.cpp:536
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 double lowerBound(size_t k) const
Get the lower bound on the k-th component of the local state vector.
Definition Reactor.cpp:534
virtual string componentName(size_t k)
Return the name of the solution component with index i.
Definition Reactor.cpp:494
virtual void evalSurfaces(double *LHS, double *RHS, double *sdot)
Evaluate terms related to surface reactions.
Definition Reactor.cpp:297
virtual void updateSurfaceState(double *y)
Update the state of SurfPhase objects attached to this reactor.
Definition Reactor.cpp:181
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:286
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:297
virtual void evalWalls(double t)
Evaluate terms related to Walls.
Definition Reactor.cpp:285
bool energyEnabled() const override
Returns true if solution of the energy equation is enabled.
Definition Reactor.h:88
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:290
virtual double upperBound(size_t k) const
Get the upper bound on the k-th component of the local state vector.
Definition Reactor.cpp:520
vector< double > m_sdot
Production rates of gas phase species on surfaces [kmol/s].
Definition Reactor.h:295
double m_vdot
net rate of volume change from moving walls [m^3/s]
Definition Reactor.h:288
virtual void getSurfaceInitialConditions(double *y)
Get initial conditions for SurfPhase objects attached to this reactor.
Definition Reactor.cpp:87
void initialize(double t0=0.0) override
Initialize the reactor.
Definition Reactor.cpp:96
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:452
virtual void updateConnected(bool updatePressure)
Update the state information needed by connected reactors, flow devices, and reactor walls.
Definition Reactor.cpp:190
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 void getPartialMolarIntEnergies(double *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
virtual double maxTemp(size_t k=npos) const
Maximum temperature for which the thermodynamic data for the species are valid.
double cv_mass() const
Specific heat at constant volume. 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
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...