17#include <boost/math/tools/roots.hpp>
20namespace bmt = boost::math::tools;
25Reactor::Reactor(shared_ptr<Solution> sol,
const string& name)
37 for (
auto S : m_surfaces) {
38 S->kinetics()->setDerivativeSettings(settings);
56 "Error: reactor is empty.");
81 for (
auto& S : m_surfaces) {
82 S->getCoverages(y + loc);
83 loc += S->thermo()->nSpecies();
89 if (!m_thermo || (m_chem && !
m_kin)) {
90 throw CanteraError(
"Reactor::initialize",
"Reactor contents not set"
91 " for reactor '" + m_name +
"'.");
98 for (
size_t n = 0; n < m_wall.size(); n++) {
106 for (
auto& S : m_surfaces) {
107 m_nv_surf += S->thermo()->nSpecies();
108 size_t nt = S->kinetics()->nTotalSpecies();
109 maxnt = std::max(maxnt, nt);
112 m_work.resize(maxnt);
117 size_t ns = m_sensParams.size();
118 for (
auto& S : m_surfaces) {
119 ns += S->nSensParams();
142 auto u_err = [
this, U](
double T) {
148 boost::uintmax_t maxiter = 100;
149 pair<double, double> TT;
151 TT = bmt::bracket_and_solve_root(
152 u_err, T, 1.2,
true, bmt::eps_tolerance<double>(48), maxiter);
153 }
catch (std::exception&) {
158 bmt::eps_tolerance<double>(48), maxiter);
159 }
catch (std::exception& err2) {
163 "{}\nat U = {}, rho = {}", err2.what(), U,
m_mass /
m_vol);
166 if (fabs(TT.first - TT.second) > 1e-7*TT.first) {
167 throw CanteraError(
"Reactor::updateState",
"root finding failed");
181 for (
auto& S : m_surfaces) {
182 S->setCoverages(y+loc);
183 loc += S->thermo()->nSpecies();
190 if (updatePressure) {
198 if (
m_net !=
nullptr) {
201 for (
size_t i = 0; i < m_outlet.size(); i++) {
202 m_outlet[i]->setSimTime(time);
203 m_outlet[i]->updateMassFlowRate(time);
205 for (
size_t i = 0; i < m_inlet.size(); i++) {
206 m_inlet[i]->setSimTime(time);
207 m_inlet[i]->updateMassFlowRate(time);
209 for (
size_t i = 0; i < m_wall.size(); i++) {
210 m_wall[i]->setSimTime(time);
216 double& dmdt = RHS[0];
217 double* mdYdt = RHS + 3;
236 for (
size_t k = 0; k <
m_nsp; k++) {
240 mdYdt[k] -= Y[k] * mdot_surf;
255 for (
auto outlet : m_outlet) {
264 for (
auto inlet : m_inlet) {
267 for (
size_t n = 0; n <
m_nsp; n++) {
270 mdYdt[n] += (mdot_spec - mdot * Y[n]);
283 for (
size_t i = 0; i < m_wall.size(); i++) {
284 int f = 2 *
m_lr[i] - 1;
285 m_vdot -= f * m_wall[i]->expansionRate();
286 m_Qdot += f * m_wall[i]->heatRate();
292 fill(sdot, sdot +
m_nsp, 0.0);
295 for (
auto S : m_surfaces) {
304 for (
size_t k = 1; k < nk; k++) {
305 RHS[loc + k] = m_work[k] * rs0 * surf->
size(k);
312 double wallarea = S->area();
313 for (
size_t k = 0; k <
m_nsp; k++) {
314 sdot[k] += m_work[bulkloc + k] * wallarea;
323 "Reactor must be initialized first.");
328 Eigen::ArrayXd yCurrent(m_nv);
332 Eigen::ArrayXd yPerturbed = yCurrent;
333 Eigen::ArrayXd lhsPerturbed(m_nv), lhsCurrent(m_nv);
334 Eigen::ArrayXd rhsPerturbed(m_nv), rhsCurrent(m_nv);
338 eval(time, lhsCurrent.data(), rhsCurrent.data());
340 double rel_perturb = std::sqrt(std::numeric_limits<double>::epsilon());
343 for (
size_t j = 0; j < m_nv; j++) {
344 yPerturbed = yCurrent;
345 double delta_y = std::max(std::abs(yCurrent[j]), 1000 * atol) * rel_perturb;
346 yPerturbed[j] += delta_y;
351 eval(time, lhsPerturbed.data(), rhsPerturbed.data());
354 for (
size_t i = 0; i < m_nv; i++) {
355 double ydotPerturbed = rhsPerturbed[i] / lhsPerturbed[i];
356 double ydotCurrent = rhsCurrent[i] / lhsCurrent[i];
357 if (ydotCurrent != ydotPerturbed) {
359 static_cast<int>(i),
static_cast<int>(j),
360 (ydotPerturbed - ydotCurrent) / delta_y);
366 Eigen::SparseMatrix<double> jac(m_nv, m_nv);
374 fill(sdot, sdot +
m_nsp, 0.0);
377 for (
auto S : m_surfaces) {
386 for (
size_t k = 1; k < nk; k++) {
387 RHS[loc + k] = m_work[k] * rs0 * surf->
size(k);
394 double wallarea = S->area();
395 for (
size_t k = 0; k <
m_nsp; k++) {
396 sdot[k] += m_work[bulkloc + k] * wallarea;
405 "Reaction number out of range ({})", rxn);
410 m_sensParams.emplace_back(
417 throw CanteraError(
"Reactor::addSensitivitySpeciesEnthalpy",
418 "Species index out of range ({})", k);
424 m_sensParams.emplace_back(
426 SensParameterType::enthalpy});
439 for (
auto& S : m_surfaces) {
456 }
else if (nm ==
"mass") {
458 }
else if (nm ==
"volume") {
460 }
else if (nm ==
"int_energy") {
474 }
else if (k >= 3 && k <
neq()) {
476 if (k < m_thermo->nSpecies()) {
481 for (
auto& S : m_surfaces) {
483 if (k < th->nSpecies()) {
490 throw CanteraError(
"Reactor::componentName",
"Index is out of bounds.");
498 for (
auto& p : m_sensParams) {
499 if (p.type == SensParameterType::reaction) {
502 }
else if (p.type == SensParameterType::enthalpy) {
506 for (
auto& S : m_surfaces) {
507 S->setSensitivityParameters(params);
511 m_kin->invalidateCache();
520 for (
auto& p : m_sensParams) {
521 if (p.type == SensParameterType::reaction) {
523 }
else if (p.type == SensParameterType::enthalpy) {
527 for (
auto& S : m_surfaces) {
528 S->resetSensitivityParameters();
532 m_kin->invalidateCache();
540 "Error: reactor is empty.");
546 [](
double val){return val>0;})) {
557 std::fill(limits, limits + m_nv, -1.0);
566 throw CanteraError(
"Reactor::setAdvanceLimit",
"No component named '{}'", nm);
571 "Error: reactor is empty.");
576 "Cannot set limit on a reactor that is not "
577 "assigned to a ReactorNet object.");
581 }
else if (k > m_nv) {
583 "Index out of bounds.");
590 [](
double val){return val>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,...
Header file for base class WallBase.
A map of string keys to values whose type can vary at runtime.
Base class for exceptions thrown by Cantera classes.
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).
Public interface for kinetics managers.
double multiplier(size_t i) const
The current value of the multiplier for reaction i.
shared_ptr< Reaction > reaction(size_t i)
Return the Reaction object for reaction i.
size_t nReactions() const
Number of reactions in the reaction mechanism.
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
virtual void setMultiplier(size_t i, double f)
Set the multiplier for reaction i to f.
virtual void getNetProductionRates(double *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
void restoreState(const vector< double > &state)
Restore a state saved on a previous call to saveState.
size_t nSpecies() const
Returns the number of species in the phase.
virtual void setMassFractions_NoNorm(const double *const y)
Set the mass fractions to the specified values without normalizing.
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
double temperature() const
Temperature (K).
void saveState(vector< double > &state) const
Save the current internal state of the phase.
string speciesName(size_t k) const
Name of the species with index k.
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
const double * massFractions() const
Return a const pointer to the mass fraction array.
const vector< double > & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
size_t speciesIndex(const string &name) const
Returns the index of a species named 'name' within the Phase object.
virtual double density() const
Density (kg/m^3).
void getMassFractions(double *const y) const
Get the species mass fractions.
virtual double pressure() const
Return the thermodynamic pressure (Pa).
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
double m_pressure
Current pressure in the reactor [Pa].
ReactorNet * m_net
The ReactorNet that this reactor is part of.
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].
virtual void syncState()
Set the state of the reactor to correspond to the state of the associated ThermoPhase object.
double m_intEnergy
Current internal energy of the reactor [J/kg].
size_t m_nsp
Number of homogeneous species in the mixture.
virtual void setThermoMgr(ThermoPhase &thermo)
Specify the mixture contained in the reactor.
ReactorNet & network()
The ReactorNet that this reactor belongs to.
double m_enthalpy
Current specific enthalpy of the reactor [J/kg].
string name() const
Return the name of this reactor.
void initialize()
Initialize the reactor network.
double time()
Current value of the simulation time [s], for reactor networks that are solved in the time domain.
size_t registerSensitivityParameter(const string &name, double value, double scale)
Used by Reactor and Wall objects to register the addition of sensitivity parameters so that the React...
double distance()
Current position [m] along the length of the reactor network, for reactors that are solved as a funct...
double atol()
Absolute integration tolerance.
virtual string componentName(size_t k)
Return the name of the solution component with index i.
void setKineticsMgr(Kinetics &kin) override
virtual void evalSurfaces(double *LHS, double *RHS, double *sdot)
Evaluate terms related to surface reactions.
virtual size_t componentIndex(const string &nm) const
Return the index in the solution vector for this reactor of the component named nm.
virtual void updateSurfaceState(double *y)
Update the state of SurfPhase objects attached to this reactor.
virtual void applySensitivity(double *params)
Set reaction rate multipliers based on the sensitivity variables in params.
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
vector< double > m_wdot
Species net molar production rates.
virtual void eval(double t, double *LHS, double *RHS)
Evaluate the reactor governing equations.
virtual void evalWalls(double t)
Evaluate terms related to Walls.
Eigen::SparseMatrix< double > finiteDifferenceJacobian()
Calculate the reactor-specific Jacobian using a finite difference method.
size_t neq()
Number of equations (state variables) for this reactor.
virtual void addSensitivitySpeciesEnthalpy(size_t k)
Add a sensitivity parameter associated with the enthalpy formation of species k (in the homogeneous p...
double m_Qdot
net heat transfer into the reactor, through walls [W]
vector< double > m_advancelimits
!< Number of variables associated with reactor surfaces
virtual void addSensitivityReaction(size_t rxn)
Add a sensitivity parameter associated with the reaction number rxn (in the homogeneous phase).
virtual size_t nSensParams() const
Number of sensitivity parameters associated with this reactor (including walls)
virtual void setDerivativeSettings(AnyMap &settings)
Use this to set the kinetics objects derivative settings.
virtual void updateState(double *y)
Set the state of the reactor to correspond to the state vector y.
void setChemistry(bool cflag=true) override
Enable or disable changes in reactor composition due to chemical reactions.
void setAdvanceLimits(const double *limits)
Set absolute step size limits during advance.
vector< Eigen::Triplet< double > > m_jac_trips
Vector of triplets representing the jacobian.
void setAdvanceLimit(const string &nm, const double limit)
Set individual step size limit for component name nm
virtual void resetSensitivity(double *params)
Reset the reaction rate multipliers.
vector< double > m_sdot
Production rates of gas phase species on surfaces [kmol/s].
virtual void getState(double *y)
Get the the current state of the reactor.
bool hasAdvanceLimits() const
Check whether Reactor object uses advance limits.
double m_vdot
net rate of volume change from moving walls [m^3/s]
void syncState() override
Set the state of the reactor to correspond to the state of the associated ThermoPhase object.
virtual void getSurfaceInitialConditions(double *y)
Get initial conditions for SurfPhase objects attached to this reactor.
void initialize(double t0=0.0) override
Initialize the reactor.
bool getAdvanceLimits(double *limits) const
Retrieve absolute step size limits during advance.
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 bool timeIsIndependent() const
Indicates whether the governing equations for this reactor are functions of time or a spatial variabl...
virtual void updateConnected(bool updatePressure)
Update the state information needed by connected reactors, flow devices, and reactor walls.
A simple thermodynamic model for a surface phase, assuming an ideal solution model.
double size(size_t k) const
Returns the number of sites occupied by one molecule of species k.
double siteDensity() const
Returns the site density.
Base class for a phase with thermodynamic properties.
virtual double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid.
virtual double maxTemp(size_t k=npos) const
Maximum temperature for which the thermodynamic data for the species are valid.
virtual void modifyOneHf298SS(const size_t k, const double Hf298New)
Modify the value of the 298 K Heat of Formation of one species in the phase (J kmol-1)
void invalidateCache() override
Invalidate any cached values which are normally updated only when a change in state is detected.
virtual void resetHf298(const size_t k=npos)
Restore the original heat of formation of one or more species.
double intEnergy_mass() const
Specific internal energy. Units: J/kg.
double Hf298SS(const size_t k) const
Report the 298 K Heat of Formation of the standard state of one species (J kmol-1)
double enthalpy_mass() const
Specific enthalpy. Units: J/kg.
Base class for 'walls' (walls, pistons, etc.) connecting reactors.
virtual void initialize()
Called just before the start of integration.
virtual void setDerivativeSettings(const AnyMap &settings)
Set/modify derivative settings.
double dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
const double GasConstant
Universal Gas Constant [J/kmol/K].
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
offset
Offsets of solution components in the 1D solution array.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...