15 #include <unordered_set>
21 Kinetics::Kinetics() :
27 m_skipUndeclaredSpecies(false),
28 m_skipUndeclaredThirdBodies(false)
32 Kinetics::~Kinetics() {}
37 throw IndexError(
"Kinetics::checkReactionIndex",
"reactions", i,
67 throw IndexError(
"Kinetics::checkSpeciesIndex",
"species", k,
m_kk-1);
81 std::map<size_t, std::vector<size_t> > participants;
82 std::vector<std::map<int, double> > net_stoich;
83 std::unordered_set<size_t> unmatched_duplicates;
86 unmatched_duplicates.insert(i);
92 unsigned long int key = 0;
94 net_stoich.emplace_back();
95 std::map<int, double>& net = net_stoich.back();
99 net[-1 -k] -= sp.second;
104 net[1+k] += sp.second;
108 vector<size_t>& related = participants[key];
109 for (
size_t m : related) {
113 unmatched_duplicates.erase(i);
114 unmatched_duplicates.erase(m);
128 bool thirdBodyOk =
true;
144 bool thirdBodyOk =
true;
161 "Undeclared duplicate reactions detected:\n"
162 "Reaction {}: {}\nReaction {}: {}\n",
168 participants[key].push_back(i);
170 if (unmatched_duplicates.size()) {
171 size_t i = *unmatched_duplicates.begin();
175 "No duplicate found for declared duplicate reaction number {}"
185 std::map<int, double>& r2)
const
187 std::unordered_set<int> keys;
189 keys.insert(r.first);
192 keys.insert(r.first);
194 int k1 = r1.begin()->first;
196 doublereal ratio = 0.0;
197 if (r1[k1] && r2[k1]) {
198 ratio = r2[k1]/r1[k1];
199 bool different =
false;
201 if ((r1[k] && !r2[k]) ||
203 (r1[k] && fabs(r2[k]/r1[k] - ratio) > 1.e-8)) {
214 if (r1[k1] == 0.0 || r2[-k1] == 0.0) {
217 ratio = r2[-k1]/r1[k1];
219 if ((r1[k] && !r2[-k]) ||
220 (!r1[k] && r2[-k]) ||
221 (r1[k] && fabs(r2[-k]/r1[k] - ratio) > 1.e-8)) {
235 double stoich = sp.second;
236 for (
size_t m = 0; m < ph.
nElements(); m++) {
244 double stoich = sp.second;
245 for (
size_t m = 0; m < ph.
nElements(); m++) {
252 for (
const auto& el : balr) {
253 const string& elem = el.first;
254 double elemsum = balr[elem] + balp[elem];
255 double elemdiff = fabs(balp[elem] - balr[elem]);
256 if (elemsum > 0.0 && elemdiff/elemsum > 1e-4) {
258 msg += fmt::format(
" {} {} {}\n",
259 elem, balr[elem], balp[elem]);
264 "The following reaction is unbalanced: {}\n"
265 " Element Reactants Products\n{}",
271 doublereal* phase_data)
273 for (
size_t n = 0; n <
nPhases(); n++) {
277 data +
m_start[n] + nsp, phase_data);
281 throw CanteraError(
"Kinetics::selectPhase",
"Phase not found.");
286 for (
size_t n =
m_start.size()-1; n !=
npos; n--) {
296 for (
size_t n = 0; n <
m_thermo.size(); n++) {
307 const std::string& ph)
const
313 for (
size_t n = 0; n <
m_thermo.size(); n++) {
328 for (
size_t n = 0; n <
m_thermo.size(); n++) {
334 throw CanteraError(
"Kinetics::speciesPhase",
"unknown species '{}'", nm);
344 throw CanteraError(
"Kinetics::speciesPhase",
"unknown species '{}'", nm);
349 for (
size_t n =
m_start.size()-1; n !=
npos; n--) {
355 "illegal species index: {}", k);
390 fill(deltaProp, deltaProp +
nReactions(), 0.0);
400 fill(deltaProp, deltaProp +
nReactions(), 0.0);
412 fill(cdot, cdot +
m_kk, 0.0);
426 fill(ddot, ddot +
m_kk, 0.0);
437 fill(net, net +
m_kk, 0.0);
469 for (
size_t i = 0; i <
m_thermo.size(); i++) {
488 if (r->reversible && !r->orders.empty()) {
490 "Reaction orders may only be given for irreversible reactions");
494 for (
const auto& sp : r->reactants) {
500 "Reaction '{}' contains the undeclared species '{}'",
501 r->equation(), sp.first);
505 for (
const auto& sp : r->products) {
511 "Reaction '{}' contains the undeclared species '{}'",
512 r->equation(), sp.first);
516 for (
const auto& sp : r->orders) {
522 "Reaction '{}' has a reaction order specified for the "
523 "undeclared species '{}'",
524 r->equation(), sp.first);
533 std::vector<size_t> rk, pk;
539 for (
const auto& sp : r->reactants) {
541 rstoich.push_back(sp.second);
544 for (
const auto& sp : r->products) {
546 pstoich.push_back(sp.second);
553 for (
const auto& sp : r->orders) {
556 auto rloc = std::find(rk.begin(), rk.end(), k);
557 if (rloc != rk.end()) {
558 rorder[rloc - rk.begin()] = sp.second;
565 rstoich.push_back(0.0);
566 rorder.push_back(sp.second);
579 m_rfn.push_back(0.0);
592 if (rNew->reaction_type != rOld->reaction_type) {
594 "Reaction types are different: {} != {}.",
595 rOld->reaction_type, rNew->reaction_type);
598 if (rNew->reactants != rOld->reactants) {
600 "Reactants are different: '{}' != '{}'.",
601 rOld->reactantString(), rNew->reactantString());
604 if (rNew->products != rOld->products) {
606 "Products are different: '{}' != '{}'.",
607 rOld->productString(), rNew->productString());
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Base class for exceptions thrown by Cantera classes.
A reaction that is first-order in [M] at low pressure, like a third-body reaction,...
An array index is out of range.
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().
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.
vector_fp m_ropnet
Net rate-of-progress for each reaction.
thermo_t & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism.
size_t m_kk
The number of species in all of the phases that participate in this kinetics mechanism.
size_t m_rxnphase
Phase Index where reactions are assumed to be taking place.
StoichManagerN m_irrevProductStoich
Stoichiometry manager for the products of irreversible reactions.
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.
virtual void getReactionDelta(const doublereal *property, doublereal *deltaProperty)
Change in species properties.
size_t nPhases() const
The number of phases participating in the reaction mechanism.
vector_fp m_rkcn
Reciprocal of the equilibrium constant in concentration units.
vector_fp m_ropf
Forward rate-of-progress for each reaction.
virtual void getRevReactionDelta(const doublereal *g, doublereal *dg)
Given an array of species properties 'g', return in array 'dg' the change in this quantity in the rev...
vector_fp m_ropr
Reverse rate-of-progress for each reaction.
virtual bool addReaction(shared_ptr< Reaction > r)
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.
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.
std::vector< thermo_t * > m_thermo
m_thermo is a vector of pointers to ThermoPhase objects that are involved with this kinetics operator
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.
bool m_skipUndeclaredSpecies
thermo_t & speciesPhase(const std::string &nm)
This function looks up the name of a species and returns a reference to the ThermoPhase object of the...
void selectPhase(const doublereal *data, const thermo_t *phase, doublereal *phase_data)
size_t m_mindim
number of spatial dimensions of lowest-dimensional phase.
StoichManagerN m_revProductStoich
Stoichiometry manager for the products of reversible reactions.
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].
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.
vector_fp m_rfn
Forward rate constant for each 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...
StoichManagerN m_reactantStoich
Stoichiometry manager for the reactants for each reaction.
virtual void getNetRatesOfProgress(doublereal *netROP)
Net rates of progress.
virtual void getCreationRates(doublereal *cdot)
Species creation rates [kmol/m^3/s or kmol/m^2/s].
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 addPhase(thermo_t &thermo)
Add a phase to the kinetics manager object.
virtual void getRevRatesOfProgress(doublereal *revROP)
Return the Reverse rates of progress of the reactions.
size_t speciesPhaseIndex(size_t k) const
This function takes as an argument the kineticsSpecies index (i.e., the list index in the list of spe...
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.
doublereal nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
size_t nElements() const
Number of elements.
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
std::string elementName(size_t m) const
Name of the element with index m.
Intermediate class which stores data about a reaction and its rate parameterization so that it can be...
bool reversible
True if the current reaction is reversible. False otherwise.
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.
int reaction_type
Type of the reaction.
std::string equation() const
The chemical equation for this reaction.
Base class for a phase with thermodynamic properties.
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...
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...
const size_t npos
index returned by functions to indicate "no position"
std::map< std::string, double > Composition
Map from string names to doubles.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Namespace for the Cantera kernel.
const U & getValue(const std::map< T, U > &m, const T &key, const U &default_val)
Const accessor for a value in a std::map.
const int THREE_BODY_RXN
A gas-phase reaction that requires a third-body collision partner.
const int CHEMACT_RXN
A chemical activation reaction.
const int FALLOFF_RXN
The general form for a gas-phase association or dissociation reaction, with a pressure-dependent rate...
Contains declarations for string manipulation functions within Cantera.