19#include <unordered_set>
20#include <boost/algorithm/string/join.hpp>
33 m_skipUndeclaredSpecies(false),
34 m_skipUndeclaredThirdBodies(false)
38Kinetics::~Kinetics() {}
43 throw IndexError(
"Kinetics::checkReactionIndex",
"reactions", i,
92 throw IndexError(
"Kinetics::checkSpeciesIndex",
"species", k,
m_kk-1);
106 std::map<size_t, std::vector<size_t> > participants;
107 std::vector<std::map<int, double> > net_stoich;
108 std::unordered_set<size_t> unmatched_duplicates;
111 unmatched_duplicates.insert(i);
117 unsigned long int key = 0;
119 net_stoich.emplace_back();
120 std::map<int, double>& net = net_stoich.back();
124 net[-1 -k] -= sp.second;
129 net[1+k] += sp.second;
133 vector<size_t>& related = participants[key];
134 for (
size_t m : related) {
138 unmatched_duplicates.erase(i);
139 unmatched_duplicates.erase(m);
141 }
else if (R.
type() != other.
type()) {
144 && R.
rate()->type() != other.
rate()->type())
153 }
else if (R.
type() ==
"falloff-legacy" ||
154 R.
type() ==
"chemically-activated-legacy") {
157 bool thirdBodyOk =
true;
170 }
else if (R.
type() ==
"falloff" || R.
type() ==
"chemically-activated") {
173 bool thirdBodyOk =
true;
176 if (tb1->efficiency(s) * tb2->efficiency(s) != 0.0) {
186 }
else if (R.
type() ==
"three-body") {
189 bool thirdBodyOk =
true;
202 }
else if (R.
type() ==
"three-body-legacy") {
205 bool thirdBodyOk =
true;
222 "Undeclared duplicate reactions detected:\n"
223 "Reaction {}: {}\nReaction {}: {}\n",
229 participants[key].push_back(i);
231 if (unmatched_duplicates.size()) {
232 size_t i = *unmatched_duplicates.begin();
236 "No duplicate found for declared duplicate reaction number {}"
246 std::map<int, double>& r2)
const
248 std::unordered_set<int> keys;
250 keys.insert(r.first);
253 keys.insert(r.first);
255 int k1 = r1.begin()->first;
257 doublereal ratio = 0.0;
258 if (r1[k1] && r2[k1]) {
259 ratio = r2[k1]/r1[k1];
260 bool different =
false;
262 if ((r1[k] && !r2[k]) ||
264 (r1[k] && fabs(r2[k]/r1[k] - ratio) > 1.e-8)) {
275 if (r1[k1] == 0.0 || r2[-k1] == 0.0) {
278 ratio = r2[-k1]/r1[k1];
280 if ((r1[k] && !r2[-k]) ||
281 (!r1[k] && r2[-k]) ||
282 (r1[k] && fabs(r2[-k]/r1[k] - ratio) > 1.e-8)) {
293 "To be removed after Cantera 2.6. Replacable by Reaction::checkBalance.");
299 for (
size_t n = 0; n <
nPhases(); n++) {
303 data +
m_start[n] + nsp, phase_data);
307 throw CanteraError(
"Kinetics::selectPhase",
"Phase not found.");
312 for (
size_t n =
m_start.size()-1; n !=
npos; n--) {
322 for (
size_t n = 0; n <
m_thermo.size(); n++) {
333 const std::string& ph)
const
339 for (
size_t n = 0; n <
m_thermo.size(); n++) {
354 for (
size_t n = 0; n <
m_thermo.size(); n++) {
360 throw CanteraError(
"Kinetics::speciesPhase",
"unknown species '{}'", nm);
370 throw CanteraError(
"Kinetics::speciesPhase",
"unknown species '{}'", nm);
375 for (
size_t n =
m_start.size()-1; n !=
npos; n--) {
381 "illegal species index: {}", k);
396 "To be changed after Cantera 2.6. "
397 "Return string instead of magic number; use "
398 "Kinetics::reactionTypeStr during transition.");
443 fill(deltaProp, deltaProp +
nReactions(), 0.0);
452 fill(deltaProp, deltaProp +
nReactions(), 0.0);
464 fill(cdot, cdot +
m_kk, 0.0);
477 fill(ddot, ddot +
m_kk, 0.0);
488 fill(net, net +
m_kk, 0.0);
497 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
509 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
521 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
533 Eigen::SparseMatrix<double> jac;
543 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
555 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
567 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
579 Eigen::SparseMatrix<double> jac;
589 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
597 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
605 Eigen::Map<Eigen::VectorXd> out(dwdot,
m_kk);
638 string name = KineticsFactory::factory()->canonicalize(
kineticsType());
639 if (name !=
"none") {
640 out[
"kinetics"] = name;
642 out[
"reactions"] =
"none";
653 for (
size_t i = 0; i <
m_thermo.size(); i++) {
671 if (!r->checkSpecies(*
this)) {
678 if (r->rate_units.factor() == 0) {
679 r->calculateRateCoeffUnits(*
this);
685 std::vector<size_t> rk, pk;
691 for (
const auto& sp : r->reactants) {
693 rstoich.push_back(sp.second);
696 for (
const auto& sp : r->products) {
698 pstoich.push_back(sp.second);
705 for (
const auto& sp : r->orders) {
708 auto rloc = std::find(rk.begin(), rk.end(), k);
709 if (rloc != rk.end()) {
710 rorder[rloc - rk.begin()] = sp.second;
717 rstoich.push_back(0.0);
718 rorder.push_back(sp.second);
730 m_rfn.push_back(0.0);
752 if (rNew->type() != rOld->type()) {
754 "Reaction types are different: {} != {}.",
755 rOld->type(), rNew->type());
758 if (!(rNew->usesLegacy())) {
759 if (rNew->rate()->type() != rOld->rate()->type()) {
761 "ReactionRate types are different: {} != {}.",
762 rOld->rate()->type(), rNew->rate()->type());
766 if (rNew->reactants != rOld->reactants) {
768 "Reactants are different: '{}' != '{}'.",
769 rOld->reactantString(), rNew->reactantString());
772 if (rNew->products != rOld->products) {
774 "Products are different: '{}' != '{}'.",
775 rOld->productString(), rNew->productString());
796 for (
size_t n = 0; n <
nPhases(); n++) {
799 double rxn_deltaH = 0;
800 for (
const auto& product : products) {
802 double stoich = product.second;
803 rxn_deltaH += hk[k] * stoich;
805 for (
const auto& reactant : reactants) {
807 double stoich = reactant.second;
808 rxn_deltaH -= hk[k] * stoich;
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
A map of string keys to values whose type can vary at runtime.
Base class for exceptions thrown by Cantera classes.
A reaction that is first-order in [M] at low pressure, like a third-body reaction,...
A falloff reaction that is first-order in [M] at low pressure, like a third-body reaction,...
An array index is out of range.
virtual void resizeReactions()
Finalize Kinetics object and associated objects.
vector_fp m_delta_gibbs0
Delta G^0 for all reactions.
void checkPhaseIndex(size_t m) const
Check that the specified phase index is in range Throws an exception if m is greater than nPhases()
void checkSpeciesArraySize(size_t mm) const
Check that an array size is at least nSpecies() Throws an exception if kk is less than nSpecies().
ThermoPhase & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism.
std::vector< size_t > m_start
m_start is a vector of integers specifying the beginning position for the species vector for the n'th...
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range Throws an exception if k is greater than nSpecies(...
shared_ptr< Reaction > reaction(size_t i)
Return the Reaction object for reaction i.
bool m_ready
Boolean indicating whether Kinetics object is fully configured.
virtual void getRevReactionDelta(const double *g, double *dg) const
Given an array of species properties 'g', return in array 'dg' the change in this quantity in the rev...
vector_fp m_ropnet
Net rate-of-progress for each reaction.
void selectPhase(const double *data, const ThermoPhase *phase, double *phase_data)
void getCreationRates_ddT(double *dwdot)
Calculate derivatives for species creation rates with respect to temperature at constant pressure,...
virtual Eigen::SparseMatrix< double > netRatesOfProgress_ddX()
Calculate derivatives for net rates-of-progress with respect to species mole fractions at constant te...
Eigen::SparseMatrix< double > destructionRates_ddX()
Calculate derivatives for species destruction rates with respect to species mole fractions at constan...
size_t m_kk
The number of species in all of the phases that participate in this kinetics mechanism.
ThermoPhase & speciesPhase(const std::string &nm)
This function looks up the name of a species and returns a reference to the ThermoPhase object of the...
size_t m_rxnphase
Phase Index where reactions are assumed to be taking place.
void getCreationRates_ddC(double *dwdot)
Calculate derivatives for species creation rates with respect to molar concentration at constant temp...
void getDestructionRates_ddP(double *dwdot)
Calculate derivatives for species destruction rates with respect to pressure at constant temperature,...
virtual void getReactionDelta(const double *property, double *deltaProperty) const
Change in species properties.
void checkPhaseArraySize(size_t mm) const
Check that an array size is at least nPhases() Throws an exception if mm is less than nPhases().
vector_fp m_perturb
Vector of perturbation factors for each reaction's rate of progress vector.
void getNetProductionRates_ddC(double *dwdot)
Calculate derivatives for species net production rates with respect to molar concentration at constan...
size_t nPhases() const
The number of phases participating in the reaction mechanism.
void getDestructionRates_ddT(double *dwdot)
Calculate derivatives for species destruction rates with respect to temperature at constant pressure,...
std::string productString(size_t i) const
Returns a string containing the products side of the reaction equation.
virtual void addPhase(ThermoPhase &thermo)
Add a phase to the kinetics manager object.
virtual void getFwdRatesOfProgress_ddP(double *drop)
Calculate derivatives for forward rates-of-progress with respect to pressure at constant temperature,...
Eigen::SparseMatrix< double > netProductionRates_ddX()
Calculate derivatives for species net production rates with respect to species mole fractions at cons...
vector_fp m_rkcn
Reciprocal of the equilibrium constant in concentration units.
vector_fp m_rbuf
Buffer used for storage of intermediate reaction-specific results.
Eigen::SparseMatrix< double > creationRates_ddX()
Calculate derivatives for species creation rates with respect to species mole fractions at constant t...
vector_fp m_ropf
Forward rate-of-progress for each reaction.
vector_fp m_dH
The enthalpy change for each reaction to calculate Blowers-Masel rates.
void getNetProductionRates_ddT(double *dwdot)
Calculate derivatives for species net production rates with respect to temperature at constant pressu...
vector_fp m_ropr
Reverse rate-of-progress for each reaction.
virtual Eigen::SparseMatrix< double > fwdRatesOfProgress_ddX()
Calculate derivatives for forward rates-of-progress with respect to species mole fractions at constan...
virtual void getRevRatesOfProgress_ddT(double *drop)
Calculate derivatives for reverse rates-of-progress with respect to temperature at constant pressure,...
virtual bool addReaction(shared_ptr< Reaction > r, bool resize=true)
Add a single reaction to the mechanism.
std::string kineticsSpeciesName(size_t k) const
Return the name of the kth species in the kinetics manager.
std::vector< ThermoPhase * > m_thermo
m_thermo is a vector of pointers to ThermoPhase objects that are involved with this kinetics operator
AnyMap parameters()
Return the parameters for a phase definition which are needed to reconstruct an identical object usin...
virtual void getNetProductionRates(doublereal *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
virtual void init()
Prepare the class for the addition of reactions, after all phases have been added.
void getCreationRates_ddP(double *dwdot)
Calculate derivatives for species creation rates with respect to pressure at constant temperature,...
virtual void getNetRatesOfProgress_ddT(double *drop)
Calculate derivatives for net rates-of-progress with respect to temperature at constant pressure,...
virtual void modifyReaction(size_t i, shared_ptr< Reaction > rNew)
Modify the rate expression associated with a reaction.
virtual double productStoichCoeff(size_t k, size_t i) const
Stoichiometric coefficient of species k as a product in reaction i.
std::map< std::string, size_t > m_phaseindex
Mapping of the phase name to the position of the phase within the kinetics object.
Eigen::SparseMatrix< double > m_stoichMatrix
Net stoichiometry (products - reactants)
virtual void getRevRatesOfProgress_ddP(double *drop)
Calculate derivatives for reverse rates-of-progress with respect to pressure at constant temperature,...
size_t m_mindim
number of spatial dimensions of lowest-dimensional phase.
StoichManagerN m_productStoich
Stoichiometry manager for the products for each reaction.
StoichManagerN m_revProductStoich
Stoichiometry manager for the products of reversible reactions.
virtual void getRevRatesOfProgress_ddC(double *drop)
Calculate derivatives for reverse rates-of-progress with respect to molar concentration at constant t...
virtual double reactionEnthalpy(const Composition &reactants, const Composition &products)
Calculate the reaction enthalpy of a reaction which has not necessarily been added into the Kinetics ...
size_t nReactions() const
Number of reactions in the reaction mechanism.
virtual void getDestructionRates(doublereal *ddot)
Species destruction rates [kmol/m^3/s or kmol/m^2/s].
virtual int reactionType(size_t i) const
Flag specifying the type of reaction.
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
size_t m_surfphase
Index in the list of phases of the one surface phase.
virtual void getFwdRatesOfProgress(doublereal *fwdROP)
Return the forward rates of progress of the reactions.
void getNetProductionRates_ddP(double *dwdot)
Calculate derivatives for species net production rates with respect to pressure at constant temperatu...
virtual void getNetRatesOfProgress_ddP(double *drop)
Calculate derivatives for net rates-of-progress with respect to pressure at constant temperature,...
vector_fp m_rfn
Forward rate constant for each reaction.
virtual std::string reactionTypeStr(size_t i) const
String specifying the type of reaction.
virtual std::string kineticsType() const
Identifies the Kinetics manager type.
virtual std::pair< size_t, size_t > checkDuplicates(bool throw_err=true) const
Check for unmarked duplicate reactions and unmatched marked duplicates.
void checkReactionBalance(const Reaction &R)
Check that the specified reaction is balanced (same number of atoms for each element in the reactants...
std::string reactantString(size_t i) const
Returns a string containing the reactants side of the reaction equation.
virtual void getFwdRatesOfProgress_ddT(double *drop)
Calculate derivatives for forward rates-of-progress with respect to temperature at constant pressure,...
void getDestructionRates_ddC(double *dwdot)
Calculate derivatives for species destruction rates with respect to molar concentration at constant t...
StoichManagerN m_reactantStoich
Stoichiometry manager for the reactants for each reaction.
virtual void getNetRatesOfProgress(doublereal *netROP)
Net rates of progress.
std::string reactionString(size_t i) const
Return a string representing the reaction.
Kinetics()
Default constructor.
virtual void getNetRatesOfProgress_ddC(double *drop)
Calculate derivatives for net rates-of-progress with respect to molar concentration at constant tempe...
virtual void getCreationRates(doublereal *cdot)
Species creation rates [kmol/m^3/s or kmol/m^2/s].
virtual Eigen::SparseMatrix< double > revRatesOfProgress_ddX()
Calculate derivatives for reverse rates-of-progress with respect to species mole fractions at constan...
virtual void resizeSpecies()
Resize arrays with sizes that depend on the total number of species.
std::vector< shared_ptr< Reaction > > m_reactions
Vector of Reaction objects represented by this Kinetics manager.
void checkReactionArraySize(size_t ii) const
Check that an array size is at least nReactions() Throws an exception if ii is less than nReactions()...
size_t nTotalSpecies() const
The total number of species in all phases participating in the kinetics mechanism.
virtual double reactantStoichCoeff(size_t k, size_t i) const
Stoichiometric coefficient of species k as a reactant in reaction i.
void checkReactionIndex(size_t m) const
Check that the specified reaction index is in range Throws an exception if i is greater than nReactio...
virtual void getRevRatesOfProgress(doublereal *revROP)
Return the Reverse rates of progress of the reactions.
virtual void getFwdRatesOfProgress_ddC(double *drop)
Calculate derivatives for forward rates-of-progress with respect to molar concentration at constant t...
size_t speciesPhaseIndex(size_t k) const
This function takes as an argument the kineticsSpecies index (that is, the list index in the list of ...
std::string name() const
Return the name of the phase.
size_t nSpecies() const
Returns the number of species in the phase.
size_t nDim() const
Returns the number of spatial dimensions (1, 2, or 3)
std::string speciesName(size_t k) const
Name of the species with index k.
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
Abstract base class which stores data about a reaction and its rate parameterization so that it can b...
void checkBalance(const Kinetics &kin) const
Check that the specified reaction is balanced (same number of atoms for each element in the reactants...
shared_ptr< ReactionRate > rate()
Get reaction rate pointer.
bool reversible
True if the current reaction is reversible. False otherwise.
virtual std::string type() const
The type of reaction.
Composition products
Product species and stoichiometric coefficients.
Composition reactants
Reactant species and stoichiometric coefficients.
AnyMap input
Input data used for specific models.
bool duplicate
True if the current reaction is marked as duplicate.
std::string equation() const
The chemical equation for this reaction.
void add(size_t rxn, const std::vector< size_t > &k)
Add a single reaction to the list of reactions that this stoichiometric manager object handles.
void resizeCoeffs(size_t nSpc, size_t nRxn)
Resize the sparse coefficient matrix.
const Eigen::SparseMatrix< double > & stoichCoeffs() const
Return matrix containing stoichiometric coefficients.
Base class for a phase with thermodynamic properties.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual std::string type() const
String indicating the thermodynamic model implemented.
A class for managing third-body efficiencies, including default values.
double efficiency(const std::string &k) const
Get the third-body efficiency for species k
A reaction with a non-reacting third body "M" that acts to add or remove energy from the reacting spe...
A reaction with a non-reacting third body "M" that acts to add or remove energy from the reacting spe...
This file contains definitions for utility functions and text for modules, inputfiles,...
double checkDuplicateStoich(std::map< int, double > &r1, std::map< int, double > &r2) const
Check whether r1 and r2 represent duplicate stoichiometries This function returns a ratio if two reac...
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
std::map< std::string, double > Composition
Map from string names to doubles.
void warn_deprecated(const std::string &source, const AnyBase &node, const std::string &message)
A deprecation warning for syntax in an input file.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...