16#include "cantera/numerics/eigen_dense.h"
28 y[0] = m_thermo->temperature();
39 if (nm ==
"temperature") {
46 return m_thermo->speciesIndex(nm) +
m_sidx;
48 throw CanteraError(
"IdealGasMoleReactor::componentIndex",
49 "Component '{}' not found", nm);
71 return 1.5 * m_thermo->maxTemp();
80 return 0.5 * m_thermo->minTemp();
105 "Volume must be positive. Input value was {}", y[1]);
109 m_thermo->setMolesNoTruncate(y.subspan(
m_sidx,
m_nsp));
111 m_thermo->getPartialMolarIntEnergies_TV(
m_uk);
118 double& mcvdTdt = RHS[0];
119 auto dndt = RHS.subspan(
m_sidx);
123 auto imw = m_thermo->inverseMolecularWeights();
133 mcvdTdt += m_thermo->intrinsicHeating() *
m_vol;
136 for (
size_t n = 0; n <
m_nsp; n++) {
145 for (
auto outlet : m_outlet) {
146 for (
size_t n = 0; n <
m_nsp; n++) {
148 dndt[n] -=
outlet->outletSpeciesMassFlowRate(n) * imw[n];
150 double mdot =
outlet->massFlowRate();
155 for (
auto inlet : m_inlet) {
156 double mdot =
inlet->massFlowRate();
157 mcvdTdt +=
inlet->enthalpy_mass() * mdot;
158 for (
size_t n = 0; n <
m_nsp; n++) {
159 double mdot_spec =
inlet->outletSpeciesMassFlowRate(n);
161 dndt[n] +=
inlet->outletSpeciesMassFlowRate(n) * imw[n];
162 mcvdTdt -=
m_uk[n] * imw[n] * mdot_spec;
187 Eigen::SparseMatrix<double> dnk_dnj =
m_kin->netProductionRates_ddCi();
192 for (
int k = 0; k < dnk_dnj.outerSize(); k++) {
193 for (Eigen::SparseMatrix<double>::InnerIterator it(dnk_dnj, k); it; ++it) {
194 trips.emplace_back(
static_cast<int>(it.row() +
m_offset +
m_sidx),
202 double deltaTemp = m_thermo->temperature()
203 * std::sqrt(std::numeric_limits<double>::epsilon());
205 vector<double> lhsPerturbed(
m_nv, 1.0), lhsCurrent(
m_nv, 1.0);
206 vector<double> rhsPerturbed(
m_nv, 0.0), rhsCurrent(
m_nv, 0.0);
207 vector<double> yCurrent(
m_nv);
209 vector<double> yPerturbed = yCurrent;
211 yPerturbed[0] += deltaTemp;
214 double time = (
m_net !=
nullptr) ?
m_net->time() : 0.0;
215 eval(time, lhsPerturbed, rhsPerturbed);
218 eval(time, lhsCurrent, rhsCurrent);
220 for (
size_t j = 0; j <
m_nv; j++) {
221 double ydotPerturbed = rhsPerturbed[j] / lhsPerturbed[j];
222 double ydotCurrent = rhsCurrent[j] / lhsCurrent[j];
224 (ydotPerturbed - ydotCurrent) / deltaTemp);
227 Eigen::VectorXd netProductionRates = Eigen::VectorXd::Zero(
m_nsp);
228 Eigen::VectorXd internal_energy = Eigen::VectorXd::Zero(
m_nsp);
229 Eigen::VectorXd specificHeat = Eigen::VectorXd::Zero(
m_nsp);
231 m_thermo->getPartialMolarIntEnergies(
asSpan(internal_energy));
232 m_kin->getNetProductionRates(
asSpan(netProductionRates));
233 m_thermo->getPartialMolarCv_TV(
asSpan(specificHeat));
234 for (
size_t i = 0; i <
m_nsp; i++) {
235 netProductionRates[i] *=
m_vol;
238 double qdot = internal_energy.dot(netProductionRates);
240 Eigen::VectorXd uk_dnkdnj_sums = dnk_dnj.transpose() * internal_energy;
242 for (
size_t j = 0; j <
m_nsp; j++) {
244 (specificHeat[j] * qdot -
m_TotalCv * uk_dnkdnj_sums[j]) * denom);
250 bool includePressureSpecies =
254 auto imw = m_thermo->inverseMolecularWeights();
255 for (
auto outlet : m_outlet) {
256 for (
size_t n = 0; n <
m_nsp; n++) {
257 outlet->addOutletSpeciesMassFlowRateJacobian(trips,
259 includePressureSpecies);
264 includePressureSpecies);
268 for (
auto inlet : m_inlet) {
269 for (
size_t n = 0; n <
m_nsp; n++) {
270 inlet->addOutletSpeciesMassFlowRateJacobian(trips,
272 includePressureSpecies);
280 for (
size_t n = 0; n <
m_nsp; n++) {
281 inlet->addOutletSpeciesMassFlowRateJacobian(trips,
m_offset, n,
283 includePressureSpecies);
290 for (
size_t i = 0; i < m_wall.size(); i++) {
291 int f = 2 *
m_lr[i] - 1;
292 m_wall[i]->addExpansionRateJacobian(trips,
m_offset + 1, -f,
293 includePressureSpecies);
298 double expansionCoeff = -(
m_pressure + m_thermo->internalPressure())
300 m_wall[i]->addExpansionRateJacobian(trips,
m_offset, expansionCoeff,
301 includePressureSpecies);
309 double& f_species, span<double> f_energy)
311 f_species = 1.0 /
m_vol;
312 for (
size_t k = 0; k <
m_nsp; k++) {
318 SparseTriplets& trips,
size_t row,
double coeff,
bool includeSpecies)
const
322 double dPdT = (m_thermo->internalPressure() +
m_pressure)
323 / m_thermo->temperature();
326 double dPdV = -1.0 / (
m_vol * m_thermo->isothermalCompressibility());
331 trips.emplace_back(row,
m_offset, coeff * dPdT);
333 trips.emplace_back(row,
m_offset + 1, coeff * dPdV);
334 if (includeSpecies) {
339 for (
size_t k = 0; k <
m_nsp; k++) {
346 double coeff,
bool includeComposition)
const
350 if (includeComposition) {
352 vector<double> hbar(
m_nsp);
353 m_thermo->getPartialMolarEnthalpies(hbar);
354 double h_mass = m_thermo->enthalpy_mass();
355 auto mw = m_thermo->molecularWeights();
356 for (
size_t k = 0; k <
m_nsp; k++) {
358 coeff * (hbar[k] - h_mass * mw[k]) /
m_mass);
364 SparseTriplets& trips,
size_t row,
double coeff)
const
370 trips.emplace_back(row,
m_offset, coeff);
375 SparseTriplets& trips,
size_t row,
size_t k,
double coeff)
const
377 auto mw = m_thermo->molecularWeights();
378 double Yk = m_thermo->massFraction(k);
380 for (
size_t j = 0; j <
m_nsp; j++) {
381 double dYdn = -Yk * mw[j] /
m_mass;
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,...
Header file for base class WallBase.
Base class for exceptions thrown by Cantera classes.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void addTemperatureJacobian(SparseTriplets &trips, size_t row, double coeff) const override
Add terms proportional to derivatives with respect to this reactor's temperature.
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.
void addSpeciesMassFractionJacobian(SparseTriplets &trips, size_t row, size_t k, double coeff) const override
Add terms proportional to derivatives with respect to a species mass fraction.
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
void addEnthalpyJacobian(SparseTriplets &trips, size_t row, double coeff, bool includeComposition=true) const override
Add terms proportional to derivatives of this reactor's specific enthalpy.
void addPressureJacobian(SparseTriplets &trips, size_t row, double coeff, bool includeSpecies=true) const override
Add terms proportional to derivatives with respect to this reactor's pressure.
vector< double > m_uk
Species molar internal energies.
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 getJacobianElements(SparseTriplets &trips) override
Calculate an approximate Jacobian to accelerate preconditioned solvers.
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.
double m_TotalCv
Total heat capacity ( ) [J/K].
void getJacobianScalingFactors(double &f_species, span< double > f_energy) override
Get scaling factors for the Jacobian matrix terms proportional to .
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void getMoles(span< double > y)
Get moles of the system from mass fractions stored by thermo object.
const size_t m_sidx
const value for the species start index
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 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].
ReactorNet * m_net
The ReactorNet that this reactor is part of.
bool isJacobianLocalRow(size_t row) const
Check whether a global Jacobian row belongs to 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.
vector< int > m_lr
Vector of length nWalls(), indicating whether this reactor is on the left (0) or right (1) of each wa...
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.
void evalWalls(double t) override
Evaluate terms related to Walls.
bool m_jac_skip_connector_pressure_composition_dependence
Omit species terms in pressure derivatives, which can add fill-in.
bool m_jac_skip_connector_composition_dependence
Omit flow composition derivatives, which can add dense connector blocks.
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
vector< double > m_wdot
Species net molar production rates.
bool energyEnabled() const override
Returns true if solution of the energy equation is enabled.
bool m_jac_skip_flow_devices
Omit flow-device terms from the sparse Jacobian.
double m_Qdot
net heat transfer into the reactor, through walls [W]
bool m_jac_skip_walls
Omit wall terms from the sparse Jacobian.
void updateSurfaceProductionRates()
Update m_sdot to reflect current production rates of bulk phase species due to reactions on adjacent ...
vector< double > m_sdot
Total production rate of bulk phase species on surfaces [kmol/s].
double m_vdot
net rate of volume change from moving walls [m^3/s]
void initialize(double t0=0.0) override
Initialize the reactor.
const double GasConstant
Universal Gas Constant [J/kmol/K].
Namespace for the Cantera kernel.
span< double > asSpan(Eigen::DenseBase< Derived > &v)
Convenience wrapper for accessing Eigen vector/array/map data as a span.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...