24 "Error: reactor is empty.");
26 m_thermo->restoreState(m_state);
32 y[0] = m_thermo->temperature();
46 if (nm ==
"temperature") {
55 throw CanteraError(
"IdealGasMoleReactor::componentIndex",
56 "Component '{}' not found", nm);
71 if (m_thermo->type() !=
"ideal-gas") {
73 "Incompatible phase type '{}' provided", m_thermo->type());
82 return 1.5 * m_thermo->maxTemp();
91 return 0.5 * m_thermo->minTemp();
100 throw CanteraError(
"IdealGasMoleReactor::steadyConstraints",
101 "Steady state solver cannot currently be used with IdealGasMoleReactor"
102 " when reactor surfaces are present.\n"
103 "See https://github.com/Cantera/enhancements/issues/234");
120 m_thermo->setMolesNoTruncate(y +
m_sidx);
128 double& mcvdTdt = RHS[0];
129 double* dndt = RHS +
m_sidx;
133 m_thermo->restoreState(m_state);
135 m_thermo->getPartialMolarIntEnergies(&
m_uk[0]);
136 const vector<double>& imw = m_thermo->inverseMolecularWeights();
148 for (
size_t n = 0; n <
m_nsp; n++) {
157 for (
auto outlet : m_outlet) {
158 for (
size_t n = 0; n <
m_nsp; n++) {
160 dndt[n] -=
outlet->outletSpeciesMassFlowRate(n) * imw[n];
162 double mdot =
outlet->massFlowRate();
167 for (
auto inlet : m_inlet) {
168 double mdot =
inlet->massFlowRate();
169 mcvdTdt +=
inlet->enthalpy_mass() * mdot;
170 for (
size_t n = 0; n <
m_nsp; n++) {
171 double mdot_spec =
inlet->outletSpeciesMassFlowRate(n);
173 dndt[n] +=
inlet->outletSpeciesMassFlowRate(n) * imw[n];
174 mcvdTdt -=
m_uk[n] * imw[n] * mdot_spec;
180 LHS[0] =
m_mass * m_thermo->cv_mass();
190 "Reactor must be initialized first.");
196 Eigen::SparseMatrix<double> dnk_dnj =
m_kin->netProductionRates_ddCi();
198 size_t ssize = m_nv -
m_sidx;
201 if (!m_surfaces.empty()) {
202 vector<Eigen::Triplet<double>> species_trips;
203 for (
int k = 0; k < dnk_dnj.outerSize(); k++) {
204 for (Eigen::SparseMatrix<double>::InnerIterator it(dnk_dnj, k); it; ++it) {
205 species_trips.emplace_back(
static_cast<int>(it.row()),
206 static_cast<int>(it.col()), it.value());
210 dnk_dnj.resize(ssize, ssize);
211 dnk_dnj.setFromTriplets(species_trips.begin(), species_trips.end());
216 for (
int k = 0; k < dnk_dnj.outerSize(); k++) {
217 for (Eigen::SparseMatrix<double>::InnerIterator it(dnk_dnj, k); it; ++it) {
219 static_cast<int>(it.col() +
m_sidx), it.value());
225 double deltaTemp = m_thermo->temperature()
226 * std::sqrt(std::numeric_limits<double>::epsilon());
228 vector<double> lhsPerturbed(m_nv, 1.0), lhsCurrent(m_nv, 1.0);
229 vector<double> rhsPerturbed(m_nv, 0.0), rhsCurrent(m_nv, 0.0);
230 vector<double> yCurrent(m_nv);
232 vector<double> yPerturbed = yCurrent;
234 yPerturbed[0] += deltaTemp;
237 double time = (
m_net !=
nullptr) ?
m_net->time() : 0.0;
238 eval(time, lhsPerturbed.data(), rhsPerturbed.data());
241 eval(time, lhsCurrent.data(), rhsCurrent.data());
243 for (
size_t j = 0; j < m_nv; j++) {
244 double ydotPerturbed = rhsPerturbed[j] / lhsPerturbed[j];
245 double ydotCurrent = rhsCurrent[j] / lhsCurrent[j];
247 (ydotPerturbed - ydotCurrent) / deltaTemp);
250 Eigen::VectorXd netProductionRates = Eigen::VectorXd::Zero(ssize);
251 Eigen::VectorXd internal_energy = Eigen::VectorXd::Zero(ssize);
252 Eigen::VectorXd specificHeat = Eigen::VectorXd::Zero(ssize);
254 m_thermo->getPartialMolarIntEnergies(internal_energy.data());
255 m_kin->getNetProductionRates(netProductionRates.data());
256 m_thermo->getPartialMolarCp(specificHeat.data());
258 for (
size_t i = 0; i <
m_nsp; i++) {
260 netProductionRates[i] *=
m_vol;
263 double qdot = internal_energy.dot(netProductionRates);
266 double* moles = yCurrent.data() +
m_sidx;
267 for (
size_t i = 0; i < ssize; i++) {
268 NCv += moles[i] * specificHeat[i];
271 double denom = 1 / (NCv * NCv);
272 Eigen::VectorXd uk_dnkdnj_sums = dnk_dnj.transpose() * internal_energy;
274 for (
size_t j = 0; j < ssize; j++) {
276 (specificHeat[j] * qdot - NCv * uk_dnkdnj_sums[j]) * denom);
280 Eigen::SparseMatrix<double> jac(m_nv, m_nv);
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 upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
vector< size_t > steadyConstraints() const override
Get the indices of equations that are algebraic constraints when solving the steady-state problem.
void eval(double t, double *LHS, 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.
Eigen::SparseMatrix< double > jacobian() override
Calculate an approximate Jacobian to accelerate preconditioned solvers.
vector< double > m_uk
Species molar internal energies.
void getState(double *y) override
Get the the current state of the reactor.
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(double *y) override
Set the state of the reactor to correspond to the state vector y.
void initialize(double t0=0.0) override
Initialize the reactor.
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
void evalSurfaces(double *LHS, double *RHS, double *sdot) override
Evaluate terms related to surface reactions.
void getSurfaceInitialConditions(double *y) override
Get initial conditions for SurfPhase objects attached to this reactor.
void getMoles(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
void setMassFromMoles(double *y)
Set internal mass variable based on moles given.
virtual void addSurfaceJacobian(vector< Eigen::Triplet< double > > &triplets)
For each surface in the reactor, update vector of triplets with all relevant surface jacobian derivat...
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 updateSurfaceState(double *y) override
Update the state of SurfPhase objects attached to this reactor.
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.
virtual size_t nSurfs() const
Return the number of surfaces in a 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.
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
vector< double > m_wdot
Species net molar production rates.
virtual void evalWalls(double t)
Evaluate terms related to Walls.
bool energyEnabled() const override
Returns true if solution of the energy equation is enabled.
double m_Qdot
net heat transfer into the reactor, through walls [W]
vector< Eigen::Triplet< double > > m_jac_trips
Vector of triplets representing the jacobian.
vector< double > m_sdot
Production rates of gas phase species on surfaces [kmol/s].
double m_vdot
net rate of volume change from moving walls [m^3/s]
virtual size_t speciesIndex(const string &nm) const
Return the index in the solution vector for this reactor of the species named nm, in either the homog...
virtual void updateConnected(bool updatePressure)
Update the state information needed by connected reactors, flow devices, and reactor walls.
const double GasConstant
Universal Gas Constant [J/kmol/K].
Namespace for the Cantera kernel.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...