13#include <boost/math/tools/roots.hpp>
17namespace bmt = boost::math::tools;
22MoleReactor::MoleReactor(shared_ptr<Solution> sol,
const string& name)
27MoleReactor::MoleReactor(shared_ptr<Solution> sol,
bool clone,
const string& name)
28 : Reactor(sol, clone, name)
36 auto Y = m_thermo->massFractions();
37 auto imw = m_thermo->inverseMolecularWeights();
38 for (
size_t i = 0; i <
m_nsp; i++) {
39 y[i] =
m_mass * imw[i] * Y[i];
45 auto mw = m_thermo->molecularWeights();
48 for (
size_t i = 0; i <
m_nsp; i++) {
57 y[0] = m_thermo->intEnergy_mass() *
m_mass;
66 vector<double> y(
neq());
69 double totalMoles = std::accumulate(y.begin() + sidx,
70 y.begin() + sidx +
m_nsp, 0.0);
71 if (totalMoles <= 0.0) {
74 std::fill(atol.begin() + sidx, atol.begin() + sidx +
m_nsp,
75 baseAtol * totalMoles);
85 m_thermo->setMolesNoTruncate(y.subspan(
m_sidx,
m_nsp));
89 auto u_err = [
this, U](
double T) {
91 return m_thermo->intEnergy_mass() *
m_mass - U;
93 double T = m_thermo->temperature();
94 boost::uintmax_t maxiter = 100;
95 pair<double, double> TT;
97 TT = bmt::bracket_and_solve_root(
98 u_err, T, 1.2,
true, bmt::eps_tolerance<double>(48), maxiter);
99 }
catch (std::exception&) {
103 TT = bmt::bisect(u_err, m_thermo->minTemp(), m_thermo->maxTemp(),
104 bmt::eps_tolerance<double>(48), maxiter);
105 }
catch (std::exception& err2) {
109 "{}\nat U = {}, rho = {}", err2.what(), U,
m_mass /
m_vol);
112 if (fabs(TT.first - TT.second) > 1e-7*TT.first) {
113 throw CanteraError(
"MoleReactor::updateState",
"root finding failed");
124 auto dndt = RHS.subspan(
m_sidx);
129 auto imw = m_thermo->inverseMolecularWeights();
143 RHS[0] += m_thermo->intrinsicHeating() *
m_vol;
148 for (
size_t k = 0; k <
m_nsp; k++) {
154 for (
auto outlet : m_outlet) {
156 for (
size_t n = 0; n <
m_nsp; n++) {
157 dndt[n] -=
outlet->outletSpeciesMassFlowRate(n) * imw[n];
160 double mdot =
outlet->massFlowRate();
167 for (
auto inlet : m_inlet) {
168 double mdot =
inlet->massFlowRate();
169 for (
size_t n = 0; n <
m_nsp; n++) {
171 dndt[n] +=
inlet->outletSpeciesMassFlowRate(n) * imw[n];
174 RHS[0] += mdot *
inlet->enthalpy_mass();
182 if (nm ==
"int_energy") {
185 if (nm ==
"volume") {
189 return m_thermo->speciesIndex(nm) +
m_sidx;
192 "Component '{}' not found", nm);
202 return m_thermo->speciesName(k -
m_sidx);
204 throw IndexError(
"MoleReactor::componentName",
"component", k,
m_nv);
217 }
else if (k >= 2 && k <
m_nv) {
220 throw CanteraError(
"MoleReactor::lowerBound",
"Index {} is out of bounds.", k);
226 y[k] = std::max(y[k], 0.0);
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,...
Base class for exceptions thrown by Cantera classes.
An array index is out of range.
MoleReactor is meant to serve the same purpose as the reactor class but with a state vector composed ...
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.
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
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 updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
void updateDefaultTolerances(span< double > atol, double baseAtol) override
Update default absolute tolerances based on this reactor's state variables.
void setMassFromMoles(span< const double > y)
Set internal mass variable based on moles given.
void getState(span< double > y) override
Get the current state of the reactor.
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
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 speciesOffset() const
Offset of the first species in the local state vector.
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.
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
vector< double > m_wdot
Species net molar production rates.
double m_Qdot
net heat transfer into the reactor, through walls [W]
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]
Namespace for the Cantera kernel.
const double Tiny
Small number to compare differences of mole fractions against.
const double BigNumber
largest number to compare to inf.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...