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
16void IdealGasReactor::getState(span<double> y)
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.subspan(3, m_nsp));
30}
31
33{
35 m_uk.resize(m_nsp, 0.0);
36}
37
38void IdealGasReactor::updateState(span<const double> y)
39{
40 // The components of y are [0] the total mass, [1] the total volume,
41 // [2] the temperature, [3...K+3] are the mass fractions of each species,
42 // and [K+3...] are the coverages of surface species on each wall.
43 m_mass = y[0];
44 m_vol = y[1];
45 m_thermo->setMassFractions_NoNorm(y.subspan(3, m_nsp));
46 m_thermo->setState_TD(y[2], m_mass / m_vol);
47 updateConnected(true);
48}
49
50void IdealGasReactor::eval(double time, span<double> LHS, span<double> RHS)
51{
52 double& dmdt = RHS[0]; // dm/dt (gas phase)
53 double& mcvdTdt = RHS[2]; // m * c_v * dT/dt
54 auto mdYdt = RHS.subspan(3); // mass * dY/dt
55
56 evalWalls(time);
59 auto mw = m_thermo->molecularWeights();
60 auto Y = m_thermo->massFractions();
61
62 if (m_chem) {
63 m_kin->getNetProductionRates(m_wdot); // "omega dot"
64 }
65
66 double mdot_surf = dot(m_sdot.begin(), m_sdot.end(), mw.begin());
67 dmdt += mdot_surf;
68
69 // compression work and external heat transfer
70 mcvdTdt += - (m_pressure + m_thermo->internalPressure()) * m_vdot + m_Qdot;
71
72 if (m_energy) {
73 mcvdTdt += m_thermo->intrinsicHeating() * m_vol;
74 }
75
76 for (size_t n = 0; n < m_nsp; n++) {
77 // heat release from gas phase and surface reactions
78 mcvdTdt -= m_wdot[n] * m_uk[n] * m_vol;
79 mcvdTdt -= m_sdot[n] * m_uk[n];
80 // production in gas phase and from surfaces
81 mdYdt[n] = (m_wdot[n] * m_vol + m_sdot[n]) * mw[n];
82 // dilution by net surface mass flux
83 mdYdt[n] -= Y[n] * mdot_surf;
84 //Assign left-hand side of dYdt ODE as total mass
85 LHS[n+3] = m_mass;
86 }
87
88 // add terms for outlets
89 for (auto outlet : m_outlet) {
90 double mdot = outlet->massFlowRate();
91 dmdt -= mdot; // mass flow out of system
92 mcvdTdt -= mdot * m_pressure * m_vol / m_mass; // flow work
93 }
94
95 // add terms for inlets
96 for (auto inlet : m_inlet) {
97 double mdot = inlet->massFlowRate();
98 dmdt += mdot; // mass flow into system
99 mcvdTdt += inlet->enthalpy_mass() * mdot;
100 for (size_t n = 0; n < m_nsp; n++) {
101 double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
102 // flow of species into system and dilution by other species
103 mdYdt[n] += mdot_spec - mdot * Y[n];
104
105 // In combination with h_in*mdot_in, flow work plus thermal
106 // energy carried with the species
107 mcvdTdt -= m_uk[n] / mw[n] * mdot_spec;
108 }
109 }
110
111 RHS[1] = m_vdot;
112 if (m_energy) {
113 LHS[2] = m_mass * m_thermo->cv_mass();
114 } else {
115 RHS[2] = 0;
116 }
117}
118
119void IdealGasReactor::evalSteady(double t, span<double> LHS, span<double> RHS)
120{
121 eval(t, LHS, RHS);
122 RHS[1] = m_vol - m_initialVolume;
123 if (!energyEnabled()) {
124 RHS[2] = m_thermo->temperature() - m_initialTemperature;
125 }
126}
127
129{
130 m_initialTemperature = m_thermo->temperature();
132 if (energyEnabled()) {
133 return {1}; // volume
134 } else {
135 return {1, 2}; // volume and temperature
136 }
137}
138
139size_t IdealGasReactor::componentIndex(const string& nm) const
140{
141 if (nm == "mass") {
142 return 0;
143 }
144 if (nm == "volume") {
145 return 1;
146 }
147 if (nm == "temperature") {
148 return 2;
149 }
150 try {
151 return m_thermo->speciesIndex(nm) + 3;
152 } catch (const CanteraError&) {
153 throw CanteraError("IdealGasReactor::componentIndex",
154 "Component '{}' not found", nm);
155 }
156}
157
159 if (k == 2) {
160 return "temperature";
161 } else {
162 return Reactor::componentName(k);
163 }
164}
165
166double IdealGasReactor::upperBound(size_t k) const {
167 if (k == 2) {
168 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
169 return 1.5 * m_thermo->maxTemp();
170 } else {
171 return Reactor::upperBound(k);
172 }
173}
174
175double IdealGasReactor::lowerBound(size_t k) const {
176 if (k == 2) {
177 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
178 return 0.5 * m_thermo->minTemp();
179 } else {
180 return Reactor::lowerBound(k);
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,...
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, span< double > LHS, span< double > RHS) override
Evaluate the reactor governing equations.
void evalSteady(double t, span< double > LHS, span< double > RHS) override
Evaluate the governing equations with modifications for the steady-state solver.
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 at constant T,V.
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 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.
void updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
void getState(span< double > y) override
Get the current state of the reactor.
virtual void getNetProductionRates(span< double > wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition Kinetics.cpp:447
void getMassFractions(span< double > y) const
Get the species mass fractions.
Definition Phase.cpp:479
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
span< const double > molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition Phase.cpp:401
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition Phase.cpp:375
double temperature() const
Temperature (K).
Definition Phase.h:585
span< const double > massFractions() const
Return a view of the mass fraction array.
Definition Phase.h:465
virtual double density() const
Density (kg/m^3).
Definition Phase.h:610
virtual void setMassFractions_NoNorm(span< const double > y)
Set the mass fractions to the specified values without normalizing.
Definition Phase.cpp:352
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:194
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
Definition Reactor.cpp:335
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:291
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
Definition Reactor.cpp:349
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:322
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:63
virtual double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid.
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 internalPressure() const
Return the internal pressure [Pa].
virtual double intrinsicHeating()
Intrinsic volumetric heating rate [W/m³].
virtual void getPartialMolarIntEnergies_TV(span< double > utilde) const
Return an array of partial molar internal energies at constant temperature and volume [J/kmol].
double dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition utilities.h:96
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...