Cantera  4.0.0a1
Loading...
Searching...
No Matches
IdealGasConstPressureMoleReactor.cpp
Go to the documentation of this file.
1//! @file IdealGasConstPressureMoleReactor.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
15#include "cantera/numerics/eigen_dense.h"
16#include <limits>
17
18namespace Cantera
19{
20
22{
23 // get mass for calculations
24 m_mass = m_thermo->density() * m_vol;
25 // set the first component to the temperature
26 y[0] = m_thermo->temperature();
27 // get moles of species in remaining state
28 getMoles(y.subspan(m_sidx));
29}
30
32{
34 m_hk.resize(m_nsp, 0.0);
35}
36
38{
39 // the components of y are: [0] the temperature, [1...K+1) are the
40 // moles of each species, and [K+1...] are the moles of surface
41 // species on each wall.
42 setMassFromMoles(y.subspan(m_sidx));
43 m_thermo->setMolesNoTruncate(y.subspan(m_sidx, m_nsp));
44 m_thermo->setState_TP(y[0], m_pressure);
45 m_vol = m_mass / m_thermo->density();
47 m_TotalCp = m_mass * m_thermo->cp_mass();
48 updateConnected(false);
49}
50
51void IdealGasConstPressureMoleReactor::eval(double time, span<double> LHS,
52 span<double> RHS)
53{
54 double& mcpdTdt = RHS[0]; // m * c_p * dT/dt
55 auto dndt = RHS.subspan(m_sidx); // kmol per s
56
57 evalWalls(time);
59 auto imw = m_thermo->inverseMolecularWeights();
60
61 if (m_chem) {
62 m_kin->getNetProductionRates(m_wdot); // "omega dot"
63 }
64
65 // external heat transfer
66 mcpdTdt += m_Qdot;
67
68 if (m_energy) {
69 mcpdTdt += m_thermo->intrinsicHeating() * m_vol;
70 }
71
72 for (size_t n = 0; n < m_nsp; n++) {
73 // heat release from gas phase and surface reactions
74 mcpdTdt -= m_wdot[n] * m_hk[n] * m_vol;
75 mcpdTdt -= m_sdot[n] * m_hk[n];
76 // production in gas phase and from surfaces
77 dndt[n] = m_wdot[n] * m_vol + m_sdot[n];
78 }
79
80 // add terms for outlets
81 for (auto outlet : m_outlet) {
82 for (size_t n = 0; n < m_nsp; n++) {
83 // flow of species out of system
84 dndt[n] -= outlet->outletSpeciesMassFlowRate(n) * imw[n];
85 }
86 }
87
88 // add terms for inlets
89 for (auto inlet : m_inlet) {
90 double mdot = inlet->massFlowRate();
91 mcpdTdt += inlet->enthalpy_mass() * mdot;
92 for (size_t n = 0; n < m_nsp; n++) {
93 double mdot_spec = inlet->outletSpeciesMassFlowRate(n);
94 // flow of species into system and dilution by other species
95 dndt[n] += inlet->outletSpeciesMassFlowRate(n) * imw[n];
96 mcpdTdt -= m_hk[n] * imw[n] * mdot_spec;
97 }
98 }
99
100 if (m_energy) {
101 LHS[0] = m_TotalCp;
102 } else {
103 RHS[0] = 0.0;
104 }
105}
106
108 vector<Eigen::Triplet<double>>& trips)
109{
110 // dnk_dnj represents d(dot(n_k)) / d (n_j) but is first assigned as
111 // d (dot(omega)) / d c_j, it is later transformed appropriately.
112 Eigen::SparseMatrix<double> dnk_dnj = m_kin->netProductionRates_ddCi();
113
114 // add species to species derivatives elements to the jacobian
115 // calculate ROP derivatives, excluding the terms -n_i / (V * N) dc_i/dn_j
116 // as it substantially reduces matrix sparsity
117 size_t offset = static_cast<int>(m_offset + m_sidx);
118 // double molarVol = m_thermo->molarVolume();
119 for (int k = 0; k < dnk_dnj.outerSize(); k++) {
120 for (Eigen::SparseMatrix<double>::InnerIterator it(dnk_dnj, k); it; ++it) {
121 trips.emplace_back(it.row() + offset, it.col() + offset, it.value());
122 }
123 }
124
125 // Temperature Derivatives
126 if (m_energy) {
127 // getting perturbed state for finite difference
128 double deltaTemp = m_thermo->temperature()
129 * std::sqrt(std::numeric_limits<double>::epsilon());
130 // get current state
131 vector<double> yCurrent(m_nv);
132 getState(yCurrent);
133 // finite difference temperature derivatives
134 vector<double> lhsPerturbed(m_nv, 1.0), lhsCurrent(m_nv, 1.0);
135 vector<double> rhsPerturbed(m_nv, 0.0), rhsCurrent(m_nv, 0.0);
136 vector<double> yPerturbed = yCurrent;
137 // perturb temperature
138 yPerturbed[0] += deltaTemp;
139 // getting perturbed state
140 updateState(yPerturbed);
141 double time = (m_net != nullptr) ? m_net->time() : 0.0;
142 eval(time, lhsPerturbed, rhsPerturbed);
143 // reset and get original state
144 updateState(yCurrent);
145 eval(time, lhsCurrent, rhsCurrent);
146 // d ydot_j/dT
147 for (size_t j = 0; j < m_nv; j++) {
148 double ydotPerturbed = rhsPerturbed[j] / lhsPerturbed[j];
149 double ydotCurrent = rhsCurrent[j] / lhsCurrent[j];
150 trips.emplace_back(static_cast<int>(j + m_offset), static_cast<int>(m_offset),
151 (ydotPerturbed - ydotCurrent) / deltaTemp);
152 }
153 // d T_dot/dnj
154 // allocate vectors for whole system
155 Eigen::VectorXd netProductionRates = Eigen::VectorXd::Zero(m_nsp);
156 Eigen::VectorXd enthalpy = Eigen::VectorXd::Zero(m_nsp);
157 Eigen::VectorXd specificHeat = Eigen::VectorXd::Zero(m_nsp);
158 // gas phase
159 m_thermo->getPartialMolarCp(asSpan(specificHeat));
160 m_thermo->getPartialMolarEnthalpies(asSpan(enthalpy));
161 m_kin->getNetProductionRates(asSpan(netProductionRates));
162 // scale production rates by the volume for gas species
163 for (size_t i = 0; i < m_nsp; i++) {
164 netProductionRates[i] *= m_vol;
165 }
166 double qdot = enthalpy.dot(netProductionRates);
167 double denom = 1 / (m_TotalCp * m_TotalCp);
168 Eigen::VectorXd hk_dnkdnj_sums = dnk_dnj.transpose() * enthalpy;
169 // Add derivatives to jac by spanning columns
170 for (size_t j = 0; j < m_nsp; j++) {
171 trips.emplace_back(m_offset, static_cast<int>(j + m_offset + m_sidx),
172 (specificHeat[j] * qdot - m_TotalCp * hk_dnkdnj_sums[j]) * denom);
173 }
174 }
175}
176
178 double& f_species, span<double> f_energy)
179{
180 f_species = 1.0 / m_vol;
181 for (size_t k = 0; k < m_nsp; k++) {
182 f_energy[k] = - m_hk[k] / m_TotalCp;
183 }
184}
185
187{
188 if (nm == "temperature") {
189 return 0;
190 }
191 try {
192 return m_thermo->speciesIndex(nm) + m_sidx;
193 } catch (const CanteraError&) {
194 throw CanteraError("IdealGasConstPressureReactor::componentIndex",
195 "Component '{}' not found", nm);
196 }
197}
198
200 if (k == 0) {
201 return "temperature";
202 } else if (k >= m_sidx && k < neq()) {
203 return m_thermo->speciesName(k - m_sidx);
204 }
205 throw IndexError("IdealGasConstPressureMoleReactor::componentName",
206 "components", k, m_nv);
207}
208
210 if (k == 0) {
211 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
212 return 1.5 * m_thermo->maxTemp();
213 } else {
214 return BigNumber; // moles of a bulk or surface species
215 }
216}
217
219 if (k == 0) {
220 //@todo: Revise pending resolution of https://github.com/Cantera/enhancements/issues/229
221 return 0.5 * m_thermo->minTemp();
222 } else {
224 }
225}
226
227}
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 class ThermoPhase, the base class for phases with thermodynamic properties,...
Base class for exceptions thrown by Cantera classes.
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
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.
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
void getJacobianElements(vector< Eigen::Triplet< double > > &trips) override
Calculate an approximate Jacobian to accelerate preconditioned solvers.
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.
void updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
vector< double > m_hk
Species molar enthalpies.
void getState(span< double > y) override
Get the current state of the reactor.
void getJacobianScalingFactors(double &f_species, span< double > f_energy) override
Get scaling factors for the Jacobian matrix terms proportional to .
An array index is out of range.
virtual void getNetProductionRates(span< double > wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition Kinetics.cpp:447
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.
span< const double > inverseMolecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition Phase.cpp:416
virtual void setMolesNoTruncate(span< const double > N)
Set the state of the object with moles in [kmol].
Definition Phase.cpp:550
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
double temperature() const
Temperature (K).
Definition Phase.h:586
string speciesName(size_t k) const
Name of the species with index k.
Definition Phase.cpp:143
virtual double density() const
Density (kg/m^3).
Definition Phase.h:611
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].
ReactorNet * m_net
The ReactorNet that this reactor is part of.
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].
size_t m_offset
Offset into global ReactorNet state vector.
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.
double time()
Current value of the simulation time [s], for reactor networks that are solved in the time domain.
void evalWalls(double t) override
Evaluate terms related to Walls.
Definition Reactor.cpp:194
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
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
vector< double > m_sdot
Total production rate of bulk phase species on surfaces [kmol/s].
Definition Reactor.h:155
void initialize(double t0=0.0) override
Initialize the reactor.
Definition Reactor.cpp:63
virtual void getPartialMolarEnthalpies(span< double > hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual void setState_TP(double t, double p)
Set the temperature (K) and pressure (Pa)
virtual double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid.
virtual void getPartialMolarCp(span< double > cpbar) const
Return an array of partial molar heat capacities 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.
virtual double intrinsicHeating()
Intrinsic volumetric heating rate [W/m³].
double cp_mass() const
Specific heat at constant pressure and composition [J/kg/K].
Eigen::SparseMatrix< double > netProductionRates_ddCi()
Calculate derivatives for species net production rates with respect to species concentration at const...
Definition Kinetics.cpp:609
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
span< double > asSpan(Eigen::DenseBase< Derived > &v)
Convenience wrapper for accessing Eigen vector/array/map data as a span.
Definition eigen_dense.h:46
offset
Offsets of solution components in the 1D solution array.
Definition Flow1D.h:25
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...