Cantera  4.0.0a1
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 // set the first component to the total mass
19 m_mass = m_thermo->density() * m_vol;
20 y[0] = m_mass;
21
22 // set the second component to the total volume
23 y[1] = m_vol;
24
25 // Set the third component to the temperature
26 y[2] = m_thermo->temperature();
27
28 // set components y+3 ... y+K+2 to the mass fractions of each species
29 m_thermo->getMassFractions(y+3);
30}
31
33{
34 //! @todo: Add a method to ThermoPhase that indicates whether a given
35 //! subclass is compatible with this reactor model
36 if (m_thermo->type() != "ideal-gas" && m_thermo->type() != "plasma") {
37 throw CanteraError("IdealGasReactor::initialize",
38 "Incompatible phase type '{}' provided", m_thermo->type());
39 }
41 m_uk.resize(m_nsp, 0.0);
42}
43
45{
46 // The components of y are [0] the total mass, [1] the total volume,
47 // [2] the temperature, [3...K+3] are the mass fractions of each species,
48 // and [K+3...] are the coverages of surface species on each wall.
49 m_mass = y[0];
50 m_vol = y[1];
51 m_thermo->setMassFractions_NoNorm(y+3);
52 m_thermo->setState_TD(y[2], m_mass / m_vol);
53 updateConnected(true);
54}
55
56void IdealGasReactor::eval(double time, double* LHS, double* RHS)
57{
58 double& dmdt = RHS[0]; // dm/dt (gas phase)
59 double& mcvdTdt = RHS[2]; // m * c_v * dT/dt
60 double* mdYdt = RHS + 3; // mass * dY/dt
61
62 evalWalls(time);
64 m_thermo->getPartialMolarIntEnergies(&m_uk[0]);
65 const vector<double>& mw = m_thermo->molecularWeights();
66 const double* Y = m_thermo->massFractions();
67
68 if (m_chem) {
69 m_kin->getNetProductionRates(&m_wdot[0]); // "omega dot"
70 }
71
72 double mdot_surf = dot(m_sdot.begin(), m_sdot.end(), mw.begin());
73 dmdt += mdot_surf;
74
75 // compression work and external heat transfer
76 mcvdTdt += - m_pressure * m_vdot + m_Qdot;
77
78 if (m_energy) {
79 mcvdTdt += m_thermo->intrinsicHeating() * m_vol;
80 }
81
82 for (size_t n = 0; n < m_nsp; n++) {
83 // heat release from gas phase and surface reactions
84 mcvdTdt -= m_wdot[n] * m_uk[n] * m_vol;
85 mcvdTdt -= m_sdot[n] * m_uk[n];
86 // production in gas phase and from surfaces
87 mdYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n];
88 // dilution by net surface mass flux
89 mdYdt[n] -= Y[n] * mdot_surf;
90 //Assign left-hand side of dYdt ODE as total mass
91 LHS[n+3] = m_mass;
92 }
93
94 // add terms for outlets
95 for (auto outlet : m_outlet) {
96 double mdot = outlet->massFlowRate();
97 dmdt -= mdot; // mass flow out of system
98 mcvdTdt -= mdot * m_pressure * m_vol / m_mass; // flow work
99 }
100
101 // add terms for inlets
102 for (auto inlet : m_inlet) {
103 double mdot = inlet->massFlowRate();
104 dmdt += mdot; // mass flow into system
105 mcvdTdt += inlet->enthalpy_mass() * mdot;
106 for (size_t n = 0; n < m_nsp; n++) {
107 double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
108 // flow of species into system and dilution by other species
109 mdYdt[n] += mdot_spec - mdot * Y[n];
110
111 // In combination with h_in*mdot_in, flow work plus thermal
112 // energy carried with the species
113 mcvdTdt -= m_uk[n] / mw[n] * mdot_spec;
114 }
115 }
116
117 RHS[1] = m_vdot;
118 if (m_energy) {
119 LHS[2] = m_mass * m_thermo->cv_mass();
120 } else {
121 RHS[2] = 0;
122 }
123}
124
125void IdealGasReactor::evalSteady(double t, double* LHS, double* RHS)
126{
127 eval(t, LHS, RHS);
128 RHS[1] = m_vol - m_initialVolume;
129 if (!energyEnabled()) {
130 RHS[2] = m_thermo->temperature() - m_initialTemperature;
131 }
132}
133
135{
136 m_initialTemperature = m_thermo->temperature();
138 if (energyEnabled()) {
139 return {1}; // volume
140 } else {
141 return {1, 2}; // volume and temperature
142 }
143}
144
145size_t IdealGasReactor::componentIndex(const string& nm) const
146{
147 if (nm == "mass") {
148 return 0;
149 }
150 if (nm == "volume") {
151 return 1;
152 }
153 if (nm == "temperature") {
154 return 2;
155 }
156 try {
157 return m_thermo->speciesIndex(nm) + 3;
158 } catch (const CanteraError&) {
159 throw CanteraError("IdealGasReactor::componentIndex",
160 "Component '{}' not found", nm);
161 }
162}
163
165 if (k == 2) {
166 return "temperature";
167 } else {
168 return Reactor::componentName(k);
169 }
170}
171
172double IdealGasReactor::upperBound(size_t k) const {
173 if (k == 2) {
174 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
175 return 1.5 * m_thermo->maxTemp();
176 } else {
177 return Reactor::upperBound(k);
178 }
179}
180
181double IdealGasReactor::lowerBound(size_t k) const {
182 if (k == 2) {
183 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
184 return 0.5 * m_thermo->minTemp();
185 } else {
186 return Reactor::lowerBound(k);
187 }
188}
189
190}
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.
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 evalSteady(double t, double *LHS, double *RHS) override
Evaluate the governing equations with modifications for the steady-state solver.
vector< double > m_uk
Species molar internal energies.
void getState(double *y) override
Get the current state of the reactor.
vector< size_t > initializeSteady() override
Initialize the reactor before solving a steady-state problem.
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.
double m_initialTemperature
Initial temperature [K]; used for steady-state calculations.
double m_initialVolume
Initial volume [m³]; used for steady-state calculations.
virtual void getNetProductionRates(double *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition Kinetics.cpp:436
virtual void setMassFractions_NoNorm(const double *const y)
Set the mass fractions to the specified values without normalizing.
Definition Phase.cpp:373
size_t speciesIndex(const string &name, bool raise=true) const
Returns the index of a species named 'name' within the Phase object.
Definition Phase.cpp:127
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition Phase.cpp:395
double temperature() const
Temperature (K).
Definition Phase.h:598
const double * massFractions() const
Return a const pointer to the mass fraction array.
Definition Phase.h:478
const vector< double > & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition Phase.cpp:420
virtual double density() const
Density (kg/m^3).
Definition Phase.h:623
void getMassFractions(double *const y) const
Get the species mass fractions.
Definition Phase.cpp:496
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].
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 updateConnected(bool updatePressure)
Update state information needed by connected reactors, flow devices, and walls.
void evalWalls(double t) override
Evaluate terms related to Walls.
Definition Reactor.cpp:193
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
Definition Reactor.cpp:334
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:147
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:151
bool energyEnabled() const override
Returns true if solution of the energy equation is enabled.
Definition Reactor.h:79
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:150
void updateSurfaceProductionRates()
Update m_sdot to reflect current production rates of bulk phase species due to reactions on adjacent ...
Definition Reactor.cpp:290
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
Definition Reactor.cpp:348
vector< double > m_sdot
Total production rate of bulk phase species on surfaces [kmol/s].
Definition Reactor.h:155
string componentName(size_t k) override
Return the name of the solution component with index i.
Definition Reactor.cpp:321
double m_vdot
net rate of volume change from moving walls [m^3/s]
Definition Reactor.h:149
void initialize(double t0=0.0) override
Initialize the reactor.
Definition Reactor.cpp:62
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 and composition [J/kg/K].
virtual double intrinsicHeating()
Intrinsic volumetric heating rate [W/m³].
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...