13#include <boost/math/tools/roots.hpp>
17namespace bmt = boost::math::tools;
22MoleReactor::MoleReactor(shared_ptr<Solution> sol,
const string& name)
23 : MoleReactor(sol, true, name)
27MoleReactor::MoleReactor(shared_ptr<Solution> sol,
bool clone,
const string& name)
28 : Reactor(sol, clone, name)
33void MoleReactor::getMoles(span<double> y)
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];
43void MoleReactor::setMassFromMoles(span<const double> y)
45 auto mw = m_thermo->molecularWeights();
48 for (
size_t i = 0; i < m_nsp; i++) {
49 m_mass += y[i] * mw[i];
53void MoleReactor::getState(span<double> y)
56 m_mass = m_thermo->density() * m_vol;
57 y[0] = m_thermo->intEnergy_mass() * m_mass;
61 getMoles(y.subspan(m_sidx));
64void MoleReactor::updateDefaultTolerances(span<double> atol,
double baseAtol)
66 vector<double> y(neq());
68 size_t sidx = speciesOffset();
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);
78void MoleReactor::updateState(span<const double> y)
83 setMassFromMoles(y.subspan(m_sidx));
85 m_thermo->setMolesNoTruncate(y.subspan(m_sidx, m_nsp));
89 auto u_err = [
this, U](
double T) {
90 m_thermo->setState_TD(T, m_mass / m_vol);
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) {
107 m_thermo->setState_TD(T, m_mass / m_vol);
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");
115 m_thermo->setState_TD(TT.second, m_mass / m_vol);
117 m_thermo->setDensity(m_mass / m_vol);
119 updateConnected(
true);
122void MoleReactor::eval(
double time, span<double> LHS, span<double> RHS)
124 auto dndt = RHS.subspan(m_sidx);
127 updateSurfaceProductionRates();
129 auto imw = m_thermo->inverseMolecularWeights();
134 m_kin->getNetProductionRates(m_wdot);
142 RHS[0] = - m_thermo->pressure() * m_vdot + m_Qdot;
143 RHS[0] += m_thermo->intrinsicHeating() * m_vol;
148 for (
size_t k = 0; k < m_nsp; k++) {
150 dndt[k] = m_wdot[k] * m_vol + m_sdot[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();
162 RHS[0] -= mdot * m_enthalpy;
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();
180size_t MoleReactor::componentIndex(
const string& nm)
const
182 if (nm ==
"int_energy") {
185 if (nm ==
"volume") {
189 return m_thermo->speciesIndex(nm) + m_sidx;
192 "Component '{}' not found", nm);
196string MoleReactor::componentName(
size_t k) {
201 }
else if (k >= m_sidx && k < neq()) {
202 return m_thermo->speciesName(k - m_sidx);
204 throw IndexError(
"MoleReactor::componentName",
"component", k, m_nv);
207double MoleReactor::upperBound(
size_t k)
const {
212double MoleReactor::lowerBound(
size_t k)
const {
217 }
else if (k >= 2 && k < m_nv) {
220 throw CanteraError(
"MoleReactor::lowerBound",
"Index {} is out of bounds.", k);
224void MoleReactor::resetBadValues(span<double> y) {
225 for (
size_t k = m_sidx; k < m_nv; 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.
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...