Cantera  4.0.0a1
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
31void ConstPressureMoleReactor::getState(double* y)
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 + m_sidx);
39}
40
41void ConstPressureMoleReactor::updateState(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 + m_sidx);
47 m_thermo->setMolesNoTruncate(y + m_sidx);
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, double* LHS, double* RHS)
58{
59 double* dndt = RHS + m_sidx; // kmol per s
60
61 evalWalls(time);
62 updateSurfaceProductionRates();
63
64 const vector<double>& imw = m_thermo->inverseMolecularWeights();
65
66 if (m_chem) {
67 m_kin->getNetProductionRates(&m_wdot[0]); // "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, double* LHS, double* RHS)
110{
111 eval(t, LHS, RHS);
112 RHS[1] = m_mass - m_initialMass;
113}
114
115vector<size_t> ConstPressureMoleReactor::initializeSteady()
116{
117 m_initialMass = m_mass;
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
134string ConstPressureMoleReactor::componentName(size_t k) {
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
145double ConstPressureMoleReactor::upperBound(size_t k) const {
146 // Component is either enthalpy or moles of a bulk or surface species
147 return BigNumber;
148}
149
150double ConstPressureMoleReactor::lowerBound(size_t k) const {
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
160void ConstPressureMoleReactor::resetBadValues(double* y) {
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.
An array index is out of range.
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:175
const double BigNumber
largest number to compare to inf.
Definition ct_defs.h:162
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...