Cantera
Loading...
Searching...
No Matches
ConstPressureMoleReactor.cpp
Go to the documentation of this file.
1//! @file ConstPressureMoleReactor.cpp A constant pressure
2//! zero-dimensional reactor with moles as the state
3
4// This file is part of Cantera. See License.txt in the top-level directory or
5// at https://cantera.org/license.txt for license and copyright information.
6
14
15namespace Cantera
16{
17
18ConstPressureMoleReactor::ConstPressureMoleReactor(shared_ptr<Solution> sol,
19 const string& name)
20 : ConstPressureMoleReactor(sol, true, name)
21{
22}
23
24ConstPressureMoleReactor::ConstPressureMoleReactor(shared_ptr<Solution> sol, bool clone,
25 const string& name)
26 : MoleReactor(sol, clone, name)
27{
28 m_nv = 1 + m_nsp; // enthalpy and moles of each species
29}
30
32{
33 // set mass to be used in getMoles function
34 m_mass = m_thermo->density() * m_vol;
35 // set the first array element to enthalpy
36 y[0] = m_thermo->enthalpy_mass() * m_thermo->density() * m_vol;
37 // get moles of species in remaining state
38 getMoles(y.subspan(m_sidx));
39}
40
41void ConstPressureMoleReactor::updateState(span<const double> y)
42{
43 // the components of y are: [0] the enthalpy, [1...K+1) are the
44 // moles of each species, and [K+1...] are the moles of surface
45 // species on each wall.
46 setMassFromMoles(y.subspan(m_sidx));
47 m_thermo->setMolesNoTruncate(y.subspan(m_sidx, m_nsp));
48 if (m_energy) {
49 m_thermo->setState_HP(y[0] / m_mass, m_pressure);
50 } else {
51 m_thermo->setPressure(m_pressure);
52 }
53 m_vol = m_mass / m_thermo->density();
54 updateConnected(false);
55}
56
57void ConstPressureMoleReactor::eval(double time, span<double> LHS, span<double> RHS)
58{
59 auto dndt = RHS.subspan(m_sidx); // kmol per s
60
61 evalWalls(time);
63
64 auto imw = m_thermo->inverseMolecularWeights();
65
66 if (m_chem) {
67 m_kin->getNetProductionRates(m_wdot); // "omega dot"
68 }
69
70 // external heat transfer
71 double dHdt = m_Qdot;
72
73 if (m_energy) {
74 dHdt += m_thermo->intrinsicHeating() * m_vol;
75 }
76
77 for (size_t n = 0; n < m_nsp; n++) {
78 // production in gas phase and from surfaces
79 dndt[n] = m_wdot[n] * m_vol + m_sdot[n];
80 }
81
82 // add terms for outlets
83 for (auto outlet : m_outlet) {
84 // determine enthalpy contribution
85 dHdt -= outlet->massFlowRate() * m_enthalpy;
86 // flow of species into system and dilution by other species
87 for (size_t n = 0; n < m_nsp; n++) {
88 dndt[n] -= outlet->outletSpeciesMassFlowRate(n) * imw[n];
89 }
90 }
91
92 // add terms for inlets
93 for (auto inlet : m_inlet) {
94 // enthalpy contribution from inlets
95 dHdt += inlet->enthalpy_mass() * inlet->massFlowRate();
96 // flow of species into system and dilution by other species
97 for (size_t n = 0; n < m_nsp; n++) {
98 dndt[n] += inlet->outletSpeciesMassFlowRate(n) * imw[n];
99 }
100 }
101
102 if (m_energy) {
103 RHS[0] = dHdt;
104 } else {
105 RHS[0] = 0.0;
106 }
107}
108
109void ConstPressureMoleReactor::evalSteady(double t, span<double> LHS, span<double> RHS)
110{
111 eval(t, LHS, RHS);
112 RHS[1] = m_mass - m_initialMass;
113}
114
116{
118 return {1};
119}
120
121size_t ConstPressureMoleReactor::componentIndex(const string& nm) const
122{
123 if (nm == "enthalpy") {
124 return 0;
125 }
126 try {
127 return m_thermo->speciesIndex(nm) + m_sidx;
128 } catch (const CanteraError&) {
129 throw CanteraError("ConstPressureMoleReactor::componentIndex",
130 "Component '{}' not found", nm);
131 }
132}
133
135 if (k == 0) {
136 return "enthalpy";
137 } else if (k >= m_sidx && k < neq()) {
138 return m_thermo->speciesName(k - m_sidx);
139 } else {
140 throw IndexError("ConstPressureMoleReactor::componentName",
141 "component", k, m_nv);
142 }
143}
144
146 // Component is either enthalpy or moles of a bulk or surface species
147 return BigNumber;
148}
149
151 if (k == 0) {
152 return -BigNumber; // enthalpy
153 } else if (k >= 1 && k < m_nv) {
154 return -Tiny; // moles of bulk or surface species
155 } else {
156 throw CanteraError("ConstPressureMoleReactor::lowerBound", "Index {} is out of bounds.", k);
157 }
158}
159
161 for (size_t k = m_sidx; k < m_nv; k++) {
162 y[k] = std::max(y[k], 0.0);
163 }
164}
165
166}
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Header file for class ReactorSurface.
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
Header file for base class WallBase.
Base class for exceptions thrown by Cantera classes.
ConstPressureMoleReactor is a class for constant-pressure reactors which use a state of moles.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void resetBadValues(span< double > y) override
Reset physically or mathematically problematic values, such as negative species concentrations.
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.
double m_initialMass
Initial mass [kg]; used for steady-state calculations.
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(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.
An array index is out of range.
void getMoles(span< double > y)
Get moles of the system from mass fractions stored by thermo object.
void setMassFromMoles(span< const double > y)
Set internal mass variable based on moles given.
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].
size_t neq()
Number of equations (state variables) for this reactor.
size_t m_nv
Number of state variables for this 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.
double m_enthalpy
Current specific enthalpy of the reactor [J/kg].
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:209
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:166
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:170
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:169
void updateSurfaceProductionRates()
Update m_sdot to reflect current production rates of bulk phase species due to reactions on adjacent ...
Definition Reactor.cpp:306
vector< double > m_sdot
Total production rate of bulk phase species on surfaces [kmol/s].
Definition Reactor.h:174
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const double Tiny
Small number to compare differences of mole fractions against.
Definition ct_defs.h:176
const double BigNumber
largest number to compare to inf.
Definition ct_defs.h:163
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...