Cantera  2.5.1
Kinetics.h
Go to the documentation of this file.
1 /**
2  * @file Kinetics.h
3  * Base class for kinetics managers and also contains the kineticsmgr
4  * module documentation (see \ref kineticsmgr and class
5  * \link Cantera::Kinetics Kinetics\endlink).
6  */
7 
8 // This file is part of Cantera. See License.txt in the top-level directory or
9 // at https://cantera.org/license.txt for license and copyright information.
10 
11 #ifndef CT_KINETICS_H
12 #define CT_KINETICS_H
13 
15 #include "StoichManager.h"
17 #include "cantera/base/global.h"
18 
19 namespace Cantera
20 {
21 
22 /**
23  * @defgroup chemkinetics Chemical Kinetics
24  */
25 
26 /// @defgroup kineticsmgr Kinetics Managers
27 /// @section kinmodman Models and Managers
28 ///
29 /// A kinetics manager is a C++ class that implements a kinetics model; a
30 /// kinetics model is a set of mathematical equation describing how various
31 /// kinetic quantities are to be computed -- reaction rates, species production
32 /// rates, etc. Many different kinetics models might be defined to handle
33 /// different types of kinetic processes. For example, one kinetics model might
34 /// use expressions valid for elementary reactions in ideal gas mixtures. It
35 /// might, for example, require the reaction orders to be integral and equal to
36 /// the forward stoichiometric coefficients, require that each reaction be
37 /// reversible with a reverse rate satisfying detailed balance, include
38 /// pressure-dependent unimolecular reactions, etc. Another kinetics model might
39 /// be designed for heterogeneous chemistry at interfaces, and might allow
40 /// empirical reaction orders, coverage-dependent activation energies,
41 /// irreversible reactions, and include effects of potential differences across
42 /// the interface on reaction rates.
43 ///
44 /// A kinetics manager implements a kinetics model. Since the model equations
45 /// may be complex and expensive to evaluate, a kinetics manager may adopt
46 /// various strategies to 'manage' the computation and evaluate the expressions
47 /// efficiently. For example, if there are rate coefficients or other quantities
48 /// that depend only on temperature, a manager class may choose to store these
49 /// quantities internally, and re-evaluate them only when the temperature has
50 /// actually changed. Or a manager designed for use with reaction mechanisms
51 /// with a few repeated activation energies might precompute the terms \f$
52 /// exp(-E/RT) \f$, instead of evaluating the exponential repeatedly for each
53 /// reaction. There are many other possible 'management styles', each of which
54 /// might be better suited to some reaction mechanisms than others.
55 ///
56 /// But however a manager structures the internal computation, the tasks the
57 /// manager class must perform are, for the most part, the same. It must be able
58 /// to compute reaction rates, species production rates, equilibrium constants,
59 /// etc. Therefore, all kinetics manager classes should have a common set of
60 /// public methods, but differ in how they implement these methods.
61 ///
62 /// A kinetics manager computes reaction rates of progress, species production
63 /// rates, equilibrium constants, and similar quantities for a reaction
64 /// mechanism. All kinetics manager classes derive from class Kinetics, which
65 /// defines a common public interface for all kinetics managers. Each derived
66 /// class overloads the virtual methods of Kinetics to implement a particular
67 /// kinetics model.
68 ///
69 /// For example, class GasKinetics implements reaction rate expressions
70 /// appropriate for homogeneous reactions in ideal gas mixtures, and class
71 /// InterfaceKinetics implements expressions appropriate for heterogeneous
72 /// mechanisms at interfaces, including how to handle reactions involving
73 /// charged species of phases with different electric potentials --- something
74 /// that class GasKinetics doesn't deal with at all.
75 ///
76 /// Many of the methods of class Kinetics write into arrays the values of some
77 /// quantity for each species, for example the net production rate. These
78 /// methods always write the results into flat arrays, ordered by phase in the
79 /// order the phase was added, and within a phase in the order the species were
80 /// added to the phase (which is the same ordering as in the input file).
81 /// Example: suppose a heterogeneous mechanism involves three phases -- a bulk
82 /// phase 'a', another bulk phase 'b', and the surface phase 'a:b' at the a/b
83 /// interface. Phase 'a' contains 12 species, phase 'b' contains 3, and at the
84 /// interface there are 5 adsorbed species defined in phase 'a:b'. Then methods
85 /// like getNetProductionRates(doublereal* net) will write and output array of
86 /// length 20, beginning at the location pointed to by 'net'. The first 12
87 /// values will be the net production rates for all 12 species of phase 'a'
88 /// (even if some do not participate in the reactions), the next 3 will be for
89 /// phase 'b', and finally the net production rates for the surface species will
90 /// occupy the last 5 locations.
91 /// @ingroup chemkinetics
92 
93 
94 //! Public interface for kinetics managers.
95 /*!
96  * This class serves as a base class to derive 'kinetics managers', which are
97  * classes that manage homogeneous chemistry within one phase, or heterogeneous
98  * chemistry at one interface. The virtual methods of this class are meant to be
99  * overloaded in subclasses. The non-virtual methods perform generic functions
100  * and are implemented in Kinetics. They should not be overloaded. Only those
101  * methods required by a subclass need to be overloaded; the rest will throw
102  * exceptions if called.
103  *
104  * When the nomenclature "kinetics species index" is used below, this means that
105  * the species index ranges over all species in all phases handled by the
106  * kinetics manager.
107  *
108  * @ingroup kineticsmgr
109  */
110 class Kinetics
111 {
112 public:
113  /**
114  * @name Constructors and General Information about Mechanism
115  */
116  //@{
117 
118  /// Default constructor.
119  Kinetics();
120 
121  virtual ~Kinetics();
122 
123  //! Kinetics objects are not copyable or assignable
124  Kinetics(const Kinetics&) = delete;
125  Kinetics& operator=(const Kinetics&)= delete;
126 
127  //! Identifies the Kinetics manager type.
128  //! Each class derived from Kinetics should override this method to return
129  //! a meaningful identifier.
130  virtual std::string kineticsType() const {
131  return "Kinetics";
132  }
133 
134  //! Number of reactions in the reaction mechanism.
135  size_t nReactions() const {
136  return m_reactions.size();
137  }
138 
139  //! Check that the specified reaction index is in range
140  //! Throws an exception if i is greater than nReactions()
141  void checkReactionIndex(size_t m) const;
142 
143  //! Check that an array size is at least nReactions()
144  //! Throws an exception if ii is less than nReactions(). Used before calls
145  //! which take an array pointer.
146  void checkReactionArraySize(size_t ii) const;
147 
148  //! Check that the specified species index is in range
149  //! Throws an exception if k is greater than nSpecies()-1
150  void checkSpeciesIndex(size_t k) const;
151 
152  //! Check that an array size is at least nSpecies()
153  //! Throws an exception if kk is less than nSpecies(). Used before calls
154  //! which take an array pointer.
155  void checkSpeciesArraySize(size_t mm) const;
156 
157  //@}
158  //! @name Information/Lookup Functions about Phases and Species
159  //@{
160 
161  /**
162  * The number of phases participating in the reaction mechanism. For a
163  * homogeneous reaction mechanism, this will always return 1, but for a
164  * heterogeneous mechanism it will return the total number of phases in the
165  * mechanism.
166  */
167  size_t nPhases() const {
168  return m_thermo.size();
169  }
170 
171  //! Check that the specified phase index is in range
172  //! Throws an exception if m is greater than nPhases()
173  void checkPhaseIndex(size_t m) const;
174 
175  //! Check that an array size is at least nPhases()
176  //! Throws an exception if mm is less than nPhases(). Used before calls
177  //! which take an array pointer.
178  void checkPhaseArraySize(size_t mm) const;
179 
180  /**
181  * Return the phase index of a phase in the list of phases defined within
182  * the object.
183  *
184  * @param ph std::string name of the phase
185  *
186  * If a -1 is returned, then the phase is not defined in the Kinetics
187  * object.
188  */
189  size_t phaseIndex(const std::string& ph) const {
190  if (m_phaseindex.find(ph) == m_phaseindex.end()) {
191  return npos;
192  } else {
193  return m_phaseindex.at(ph) - 1;
194  }
195  }
196 
197  /**
198  * This returns the integer index of the phase which has ThermoPhase type
199  * cSurf. For heterogeneous mechanisms, this identifies the one surface
200  * phase. For homogeneous mechanisms, this returns -1.
201  */
202  size_t surfacePhaseIndex() const {
203  return m_surfphase;
204  }
205 
206  /**
207  * Phase where the reactions occur. For heterogeneous mechanisms, one of
208  * the phases in the list of phases represents the 2D interface or 1D edge
209  * at which the reactions take place. This method returns the index of the
210  * phase with the smallest spatial dimension (1, 2, or 3) among the list
211  * of phases. If there is more than one, the index of the first one is
212  * returned. For homogeneous mechanisms, the value 0 is returned.
213  */
214  size_t reactionPhaseIndex() const {
215  return m_rxnphase;
216  }
217 
218  /**
219  * This method returns a reference to the nth ThermoPhase object defined
220  * in this kinetics mechanism. It is typically used so that member
221  * functions of the ThermoPhase object may be called. For homogeneous
222  * mechanisms, there is only one object, and this method can be called
223  * without an argument to access it.
224  *
225  * @param n Index of the ThermoPhase being sought.
226  */
227  thermo_t& thermo(size_t n=0) {
228  return *m_thermo[n];
229  }
230  const thermo_t& thermo(size_t n=0) const {
231  return *m_thermo[n];
232  }
233 
234  /**
235  * The total number of species in all phases participating in the kinetics
236  * mechanism. This is useful to dimension arrays for use in calls to
237  * methods that return the species production rates, for example.
238  */
239  size_t nTotalSpecies() const {
240  return m_kk;
241  }
242 
243  /**
244  * The location of species k of phase n in species arrays. Kinetics manager
245  * classes return species production rates in flat arrays, with the species
246  * of each phases following one another, in the order the phases were added.
247  * This method is useful to find the value for a particular species of a
248  * particular phase in arrays returned from methods like getCreationRates
249  * that return an array of species-specific quantities.
250  *
251  * Example: suppose a heterogeneous mechanism involves three phases. The
252  * first contains 12 species, the second 26, and the third 3. Then species
253  * arrays must have size at least 41, and positions 0 - 11 are the values
254  * for the species in the first phase, positions 12 - 37 are the values for
255  * the species in the second phase, etc. Then kineticsSpeciesIndex(7, 0) =
256  * 7, kineticsSpeciesIndex(4, 1) = 16, and kineticsSpeciesIndex(2, 2) = 40.
257  *
258  * @param k species index
259  * @param n phase index for the species
260  */
261  size_t kineticsSpeciesIndex(size_t k, size_t n) const {
262  return m_start[n] + k;
263  }
264 
265  //! Return the name of the kth species in the kinetics manager.
266  /*!
267  * k is an integer from 0 to ktot - 1, where ktot is the number of
268  * species in the kinetics manager, which is the sum of the number of
269  * species in all phases participating in the kinetics manager. If k is
270  * out of bounds, the string "<unknown>" is returned.
271  *
272  * @param k species index
273  */
274  std::string kineticsSpeciesName(size_t k) const;
275 
276  /**
277  * This routine will look up a species number based on the input
278  * std::string nm. The lookup of species will occur for all phases
279  * listed in the kinetics object.
280  *
281  * return
282  * - If a match is found, the position in the species list is returned.
283  * - If no match is found, the value -1 is returned.
284  *
285  * @param nm Input string name of the species
286  */
287  size_t kineticsSpeciesIndex(const std::string& nm) const;
288 
289  /**
290  * This routine will look up a species number based on the input
291  * std::string nm. The lookup of species will occur in the specified
292  * phase of the object, or all phases if ph is "<any>".
293  *
294  * return
295  * - If a match is found, the position in the species list is returned.
296  * - If no match is found, the value npos (-1) is returned.
297  *
298  * @param nm Input string name of the species
299  * @param ph Input string name of the phase.
300  */
301  size_t kineticsSpeciesIndex(const std::string& nm,
302  const std::string& ph) const;
303 
304  /**
305  * This function looks up the name of a species and returns a
306  * reference to the ThermoPhase object of the phase where the species
307  * resides. Will throw an error if the species doesn't match.
308  *
309  * @param nm String containing the name of the species.
310  */
311  thermo_t& speciesPhase(const std::string& nm);
312  const thermo_t& speciesPhase(const std::string& nm) const;
313 
314  /**
315  * This function takes as an argument the kineticsSpecies index
316  * (i.e., the list index in the list of species in the kinetics
317  * manager) and returns the species' owning ThermoPhase object.
318  *
319  * @param k Species index
320  */
321  thermo_t& speciesPhase(size_t k) {
322  return thermo(speciesPhaseIndex(k));
323  }
324 
325  /**
326  * This function takes as an argument the kineticsSpecies index (i.e., the
327  * list index in the list of species in the kinetics manager) and returns
328  * the index of the phase owning the species.
329  *
330  * @param k Species index
331  */
332  size_t speciesPhaseIndex(size_t k) const;
333 
334  //! @}
335  //! @name Reaction Rates Of Progress
336  //! @{
337 
338  //! Return the forward rates of progress of the reactions
339  /*!
340  * Forward rates of progress. Return the forward rates of
341  * progress in array fwdROP, which must be dimensioned at
342  * least as large as the total number of reactions.
343  *
344  * @param fwdROP Output vector containing forward rates
345  * of progress of the reactions. Length: nReactions().
346  */
347  virtual void getFwdRatesOfProgress(doublereal* fwdROP);
348 
349  //! Return the Reverse rates of progress of the reactions
350  /*!
351  * Return the reverse rates of progress in array revROP, which must be
352  * dimensioned at least as large as the total number of reactions.
353  *
354  * @param revROP Output vector containing reverse rates
355  * of progress of the reactions. Length: nReactions().
356  */
357  virtual void getRevRatesOfProgress(doublereal* revROP);
358 
359  /**
360  * Net rates of progress. Return the net (forward - reverse) rates of
361  * progress in array netROP, which must be dimensioned at least as large
362  * as the total number of reactions.
363  *
364  * @param netROP Output vector of the net ROP. Length: nReactions().
365  */
366  virtual void getNetRatesOfProgress(doublereal* netROP);
367 
368  //! Return a vector of Equilibrium constants.
369  /*!
370  * Return the equilibrium constants of the reactions in concentration
371  * units in array kc, which must be dimensioned at least as large as the
372  * total number of reactions.
373  *
374  * \f[
375  * Kc_i = exp [ \Delta G_{ss,i} ] prod(Cs_k) exp(\sum_k \nu_{k,i} F \phi_n) ]
376  * \f]
377  *
378  * @param kc Output vector containing the equilibrium constants.
379  * Length: nReactions().
380  */
381  virtual void getEquilibriumConstants(doublereal* kc) {
382  throw NotImplementedError("Kinetics::getEquilibriumConstants");
383  }
384 
385  /**
386  * Change in species properties. Given an array of molar species property
387  * values \f$ z_k, k = 1, \dots, K \f$, return the array of reaction values
388  * \f[
389  * \Delta Z_i = \sum_k \nu_{k,i} z_k, i = 1, \dots, I.
390  * \f]
391  * For example, if this method is called with the array of standard-state
392  * molar Gibbs free energies for the species, then the values returned in
393  * array \c deltaProperty would be the standard-state Gibbs free energies of
394  * reaction for each reaction.
395  *
396  * @param property Input vector of property value. Length: m_kk.
397  * @param deltaProperty Output vector of deltaRxn. Length: nReactions().
398  */
399  virtual void getReactionDelta(const doublereal* property,
400  doublereal* deltaProperty);
401 
402  /**
403  * Given an array of species properties 'g', return in array 'dg' the
404  * change in this quantity in the reversible reactions. Array 'g' must
405  * have a length at least as great as the number of species, and array
406  * 'dg' must have a length as great as the total number of reactions.
407  * This method only computes 'dg' for the reversible reactions, and the
408  * entries of 'dg' for the irreversible reactions are unaltered. This is
409  * primarily designed for use in calculating reverse rate coefficients
410  * from thermochemistry for reversible reactions.
411  */
412  virtual void getRevReactionDelta(const doublereal* g, doublereal* dg);
413 
414  //! Return the vector of values for the reaction Gibbs free energy change.
415  /*!
416  * (virtual from Kinetics.h)
417  * These values depend upon the concentration of the solution.
418  *
419  * units = J kmol-1
420  *
421  * @param deltaG Output vector of deltaG's for reactions Length:
422  * nReactions().
423  */
424  virtual void getDeltaGibbs(doublereal* deltaG) {
425  throw NotImplementedError("Kinetics::getDeltaGibbs");
426  }
427 
428  //! Return the vector of values for the reaction electrochemical free
429  //! energy change.
430  /*!
431  * These values depend upon the concentration of the solution and the
432  * voltage of the phases
433  *
434  * units = J kmol-1
435  *
436  * @param deltaM Output vector of deltaM's for reactions Length:
437  * nReactions().
438  */
439  virtual void getDeltaElectrochemPotentials(doublereal* deltaM) {
440  throw NotImplementedError("Kinetics::getDeltaElectrochemPotentials");
441  }
442 
443  /**
444  * Return the vector of values for the reactions change in enthalpy.
445  * These values depend upon the concentration of the solution.
446  *
447  * units = J kmol-1
448  *
449  * @param deltaH Output vector of deltaH's for reactions Length:
450  * nReactions().
451  */
452  virtual void getDeltaEnthalpy(doublereal* deltaH) {
453  throw NotImplementedError("Kinetics::getDeltaEnthalpy");
454  }
455 
456  /**
457  * Return the vector of values for the reactions change in entropy. These
458  * values depend upon the concentration of the solution.
459  *
460  * units = J kmol-1 Kelvin-1
461  *
462  * @param deltaS Output vector of deltaS's for reactions Length:
463  * nReactions().
464  */
465  virtual void getDeltaEntropy(doublereal* deltaS) {
466  throw NotImplementedError("Kinetics::getDeltaEntropy");
467  }
468 
469  /**
470  * Return the vector of values for the reaction standard state Gibbs free
471  * energy change. These values don't depend upon the concentration of the
472  * solution.
473  *
474  * units = J kmol-1
475  *
476  * @param deltaG Output vector of ss deltaG's for reactions Length:
477  * nReactions().
478  */
479  virtual void getDeltaSSGibbs(doublereal* deltaG) {
480  throw NotImplementedError("Kinetics::getDeltaSSGibbs");
481  }
482 
483  /**
484  * Return the vector of values for the change in the standard state
485  * enthalpies of reaction. These values don't depend upon the concentration
486  * of the solution.
487  *
488  * units = J kmol-1
489  *
490  * @param deltaH Output vector of ss deltaH's for reactions Length:
491  * nReactions().
492  */
493  virtual void getDeltaSSEnthalpy(doublereal* deltaH) {
494  throw NotImplementedError("Kinetics::getDeltaSSEnthalpy");
495  }
496 
497  /**
498  * Return the vector of values for the change in the standard state
499  * entropies for each reaction. These values don't depend upon the
500  * concentration of the solution.
501  *
502  * units = J kmol-1 Kelvin-1
503  *
504  * @param deltaS Output vector of ss deltaS's for reactions Length:
505  * nReactions().
506  */
507  virtual void getDeltaSSEntropy(doublereal* deltaS) {
508  throw NotImplementedError("Kinetics::getDeltaSSEntropy");
509  }
510 
511  //! @}
512  //! @name Species Production Rates
513  //! @{
514 
515  /**
516  * Species creation rates [kmol/m^3/s or kmol/m^2/s]. Return the species
517  * creation rates in array cdot, which must be dimensioned at least as
518  * large as the total number of species in all phases. @see nTotalSpecies.
519  *
520  * @param cdot Output vector of creation rates. Length: m_kk.
521  */
522  virtual void getCreationRates(doublereal* cdot);
523 
524  /**
525  * Species destruction rates [kmol/m^3/s or kmol/m^2/s]. Return the species
526  * destruction rates in array ddot, which must be dimensioned at least as
527  * large as the total number of species. @see nTotalSpecies.
528  *
529  * @param ddot Output vector of destruction rates. Length: m_kk.
530  */
531  virtual void getDestructionRates(doublereal* ddot);
532 
533  /**
534  * Species net production rates [kmol/m^3/s or kmol/m^2/s]. Return the
535  * species net production rates (creation - destruction) in array wdot,
536  * which must be dimensioned at least as large as the total number of
537  * species. @see nTotalSpecies.
538  *
539  * @param wdot Output vector of net production rates. Length: m_kk.
540  */
541  virtual void getNetProductionRates(doublereal* wdot);
542 
543  //! @}
544  //! @name Reaction Mechanism Informational Query Routines
545  //! @{
546 
547  /**
548  * Stoichiometric coefficient of species k as a reactant in reaction i.
549  *
550  * @param k kinetic species index
551  * @param i reaction index
552  */
553  virtual double reactantStoichCoeff(size_t k, size_t i) const;
554  /**
555  * Stoichiometric coefficient of species k as a product in reaction i.
556  *
557  * @param k kinetic species index
558  * @param i reaction index
559  */
560  virtual double productStoichCoeff(size_t k, size_t i) const;
561 
562  //! Reactant order of species k in reaction i.
563  /*!
564  * This is the nominal order of the activity concentration in
565  * determining the forward rate of progress of the reaction
566  *
567  * @param k kinetic species index
568  * @param i reaction index
569  */
570  virtual doublereal reactantOrder(size_t k, size_t i) const {
571  throw NotImplementedError("Kinetics::reactantOrder");
572  }
573 
574  //! product Order of species k in reaction i.
575  /*!
576  * This is the nominal order of the activity concentration of species k in
577  * determining the reverse rate of progress of the reaction i
578  *
579  * For irreversible reactions, this will all be zero.
580  *
581  * @param k kinetic species index
582  * @param i reaction index
583  */
584  virtual doublereal productOrder(int k, int i) const {
585  throw NotImplementedError("Kinetics::productOrder");
586  }
587 
588  //! Get the vector of activity concentrations used in the kinetics object
589  /*!
590  * @param[out] conc Vector of activity concentrations. Length is equal
591  * to the number of species in the kinetics object
592  */
593  virtual void getActivityConcentrations(doublereal* const conc) {
594  throw NotImplementedError("Kinetics::getActivityConcentrations");
595  }
596 
597  /**
598  * Flag specifying the type of reaction. The legal values and their meaning
599  * are specific to the particular kinetics manager.
600  *
601  * @param i reaction index
602  */
603  virtual int reactionType(size_t i) const {
604  return m_reactions[i]->reaction_type;
605  }
606 
607  /**
608  * True if reaction i has been declared to be reversible. If isReversible(i)
609  * is false, then the reverse rate of progress for reaction i is always
610  * zero.
611  *
612  * @param i reaction index
613  */
614  virtual bool isReversible(size_t i) {
615  throw NotImplementedError("Kinetics::isReversible");
616  }
617 
618  /**
619  * Return a string representing the reaction.
620  *
621  * @param i reaction index
622  */
623  std::string reactionString(size_t i) const {
624  return m_reactions[i]->equation();
625  }
626 
627  //! Returns a string containing the reactants side of the reaction equation.
628  std::string reactantString(size_t i) const {
629  return m_reactions[i]->reactantString();
630  }
631 
632  //! Returns a string containing the products side of the reaction equation.
633  std::string productString(size_t i) const {
634  return m_reactions[i]->productString();
635  }
636 
637  /**
638  * Return the forward rate constants
639  *
640  * The computed values include all temperature-dependent, pressure-dependent,
641  * and third body contributions. Length is the number of reactions. Units are
642  * a combination of kmol, m^3 and s, that depend on the rate expression for
643  * the reaction.
644  *
645  * @param kfwd Output vector containing the forward reaction rate
646  * constants. Length: nReactions().
647  */
648  virtual void getFwdRateConstants(doublereal* kfwd) {
649  throw NotImplementedError("Kinetics::getFwdRateConstants");
650  }
651 
652  /**
653  * Return the reverse rate constants.
654  *
655  * The computed values include all temperature-dependent, pressure-dependent,
656  * and third body contributions. Length is the number of reactions. Units are
657  * a combination of kmol, m^3 and s, that depend on the rate expression for
658  * the reaction. Note, this routine will return rate constants for
659  * irreversible reactions if the default for `doIrreversible` is overridden.
660  *
661  * @param krev Output vector of reverse rate constants
662  * @param doIrreversible boolean indicating whether irreversible reactions
663  * should be included.
664  */
665  virtual void getRevRateConstants(doublereal* krev,
666  bool doIrreversible = false) {
667  throw NotImplementedError("Kinetics::getFwdRateConstants");
668  }
669 
670  //! @}
671  //! @name Reaction Mechanism Construction
672  //! @{
673 
674  //! Add a phase to the kinetics manager object.
675  /*!
676  * This must be done before the function init() is called or before any
677  * reactions are input. The following fields are updated:
678  *
679  * - #m_start -> vector of integers, containing the starting position of
680  * the species for each phase in the kinetics mechanism.
681  * - #m_surfphase -> index of the surface phase.
682  * - #m_thermo -> vector of pointers to ThermoPhase phases that
683  * participate in the kinetics mechanism.
684  * - #m_phaseindex -> map containing the std::string id of each
685  * ThermoPhase phase as a key and the index of the phase within the
686  * kinetics manager object as the value.
687  *
688  * @param thermo Reference to the ThermoPhase to be added.
689  */
690  virtual void addPhase(thermo_t& thermo);
691 
692  /**
693  * Prepare the class for the addition of reactions, after all phases have
694  * been added. This method is called automatically when the first reaction
695  * is added. It needs to be called directly only in the degenerate case
696  * where there are no reactions. The base class method does nothing, but
697  * derived classes may use this to perform any initialization (allocating
698  * arrays, etc.) that requires knowing the phases.
699  */
700  virtual void init() {}
701 
702  /**
703  * Resize arrays with sizes that depend on the total number of species.
704  * Automatically called before adding each Reaction and Phase.
705  */
706  virtual void resizeSpecies();
707 
708  /**
709  * Add a single reaction to the mechanism. Derived classes should call the
710  * base class method in addition to handling their own specialized behavior.
711  *
712  * @param r Pointer to the Reaction object to be added.
713  * @return `true` if the reaction is added or `false` if it was skipped
714  */
715  virtual bool addReaction(shared_ptr<Reaction> r);
716 
717  /**
718  * Modify the rate expression associated with a reaction. The
719  * stoichiometric equation, type of the reaction, reaction orders, third
720  * body efficiencies, reversibility, etc. must be unchanged.
721  *
722  * @param i Index of the reaction to be modified
723  * @param rNew Reaction with the new rate expressions
724  */
725  virtual void modifyReaction(size_t i, shared_ptr<Reaction> rNew);
726 
727  /**
728  * Return the Reaction object for reaction *i*. Changes to this object do
729  * not affect the Kinetics object until the #modifyReaction function is
730  * called.
731  */
732  shared_ptr<Reaction> reaction(size_t i);
733 
734  shared_ptr<const Reaction> reaction(size_t i) const;
735 
736  //! Determine behavior when adding a new reaction that contains species not
737  //! defined in any of the phases associated with this kinetics manager. If
738  //! set to true, the reaction will silently be ignored. If false, (the
739  //! default) an exception will be raised.
740  void skipUndeclaredSpecies(bool skip) {
742  }
743  bool skipUndeclaredSpecies() const {
745  }
746 
747  //! Determine behavior when adding a new reaction that contains third-body
748  //! efficiencies for species not defined in any of the phases associated
749  //! with this kinetics manager. If set to true, the given third-body
750  //! efficiency will be ignored. If false, (the default) an exception will be
751  //! raised.
752  void skipUndeclaredThirdBodies(bool skip) {
754  }
755  bool skipUndeclaredThirdBodies() const {
757  }
758 
759  //@}
760  //! @name Altering Reaction Rates
761  /*!
762  * These methods alter reaction rates. They are designed primarily for
763  * carrying out sensitivity analysis, but may be used for any purpose
764  * requiring dynamic alteration of rate constants. For each reaction, a
765  * real-valued multiplier may be defined that multiplies the reaction rate
766  * coefficient. The multiplier may be set to zero to completely remove a
767  * reaction from the mechanism.
768  */
769  //@{
770 
771  //! The current value of the multiplier for reaction i.
772  /*!
773  * @param i index of the reaction
774  */
775  doublereal multiplier(size_t i) const {
776  return m_perturb[i];
777  }
778 
779  //! Set the multiplier for reaction i to f.
780  /*!
781  * @param i index of the reaction
782  * @param f value of the multiplier.
783  */
784  virtual void setMultiplier(size_t i, doublereal f) {
785  m_perturb[i] = f;
786  }
787 
788  virtual void invalidateCache() {};
789 
790  //@}
791 
792  //! Check for unmarked duplicate reactions and unmatched marked duplicates
793  /**
794  * If `throw_err` is true, then an exception will be thrown if either any
795  * unmarked duplicate reactions are found, or if any marked duplicate
796  * reactions do not have a matching duplicate reaction. If `throw_err` is
797  * false, the indices of the first pair of duplicate reactions found will be
798  * returned, or the index of the unmatched duplicate will be returned as
799  * both elements of the pair. If no unmarked duplicates or unmatched marked
800  * duplicate reactions are found, returns `(npos, npos)`.
801  */
802  virtual std::pair<size_t, size_t> checkDuplicates(bool throw_err=true) const;
803 
804  /*!
805  * Takes as input an array of properties for all species in the mechanism
806  * and copies those values belonging to a particular phase to the output
807  * array.
808  * @param data Input data array.
809  * @param phase Pointer to one of the phase objects participating in this
810  * reaction mechanism
811  * @param phase_data Output array where the values for the the specified
812  * phase are to be written.
813  */
814  void selectPhase(const doublereal* data, const thermo_t* phase,
815  doublereal* phase_data);
816 
817  //! Set root Solution holding all phase information
818  virtual void setRoot(std::shared_ptr<Solution> root) {
819  m_root = root;
820  }
821 
822 protected:
823  //! Cache for saved calculations within each Kinetics object.
825 
826  // Update internal rate-of-progress variables #m_ropf and #m_ropr.
827  virtual void updateROP() {
828  throw NotImplementedError("Kinetics::updateROP");
829  }
830 
831  //! Check whether `r1` and `r2` represent duplicate stoichiometries
832  //! This function returns a ratio if two reactions are duplicates of
833  //! one another, and 0.0 otherwise.
834  /*!
835  * `r1` and `r2` are maps of species key to stoichiometric coefficient, one
836  * for each reaction, where the species key is `1+k` for reactants and
837  * `-1-k` for products and `k` is the species index.
838  *
839  * @return 0.0 if the stoichiometries are not multiples of one another
840  * Otherwise, it returns the ratio of the stoichiometric coefficients.
841  *
842  * @ingroup kineticsmgr
843  */
844  double checkDuplicateStoich(std::map<int, double>& r1,
845  std::map<int, double>& r2) const;
846 
847  //! Check that the specified reaction is balanced (same number of atoms for
848  //! each element in the reactants and products). Raises an exception if the
849  //! reaction is not balanced.
850  void checkReactionBalance(const Reaction& R);
851 
852  //! @name Stoichiometry management
853  /*!
854  * These objects and functions handle turning reaction extents into species
855  * production rates and also handle turning thermo properties into reaction
856  * thermo properties.
857  */
858  //@{
859 
860  //! Stoichiometry manager for the reactants for each reaction
861  StoichManagerN m_reactantStoich;
862 
863  //! Stoichiometry manager for the products of reversible reactions
864  StoichManagerN m_revProductStoich;
865 
866  //! Stoichiometry manager for the products of irreversible reactions
867  StoichManagerN m_irrevProductStoich;
868  //@}
869 
870  //! The number of species in all of the phases
871  //! that participate in this kinetics mechanism.
872  size_t m_kk;
873 
874  /// Vector of perturbation factors for each reaction's rate of
875  /// progress vector. It is initialized to one.
877 
878  //! Vector of Reaction objects represented by this Kinetics manager
879  std::vector<shared_ptr<Reaction> > m_reactions;
880 
881  //! m_thermo is a vector of pointers to ThermoPhase objects that are
882  //! involved with this kinetics operator
883  /*!
884  * For homogeneous kinetics applications, this vector will only have one
885  * entry. For interfacial reactions, this vector will consist of multiple
886  * entries; some of them will be surface phases, and the other ones will be
887  * bulk phases. The order that the objects are listed determines the order
888  * in which the species comprising each phase are listed in the source term
889  * vector, originating from the reaction mechanism.
890  *
891  * Note that this kinetics object doesn't own these ThermoPhase objects
892  * and is not responsible for creating or deleting them.
893  */
894  std::vector<thermo_t*> m_thermo;
895 
896  /**
897  * m_start is a vector of integers specifying the beginning position for the
898  * species vector for the n'th phase in the kinetics class.
899  */
900  std::vector<size_t> m_start;
901 
902  /**
903  * Mapping of the phase name to the position of the phase within the
904  * kinetics object. Positions start with the value of 1. The member
905  * function, phaseIndex() decrements by one before returning the index
906  * value, so that missing phases return -1.
907  */
908  std::map<std::string, size_t> m_phaseindex;
909 
910  //! Index in the list of phases of the one surface phase.
911  size_t m_surfphase;
912 
913  //! Phase Index where reactions are assumed to be taking place
914  /*!
915  * We calculate this by assuming that the phase with the lowest
916  * dimensionality is the phase where reactions are taking place.
917  */
918  size_t m_rxnphase;
919 
920  //! number of spatial dimensions of lowest-dimensional phase.
921  size_t m_mindim;
922 
923  //! Forward rate constant for each reaction
925 
926  //! Reciprocal of the equilibrium constant in concentration units
928 
929  //! Forward rate-of-progress for each reaction
931 
932  //! Reverse rate-of-progress for each reaction
934 
935  //! Net rate-of-progress for each reaction
937 
938  //! @see skipUndeclaredSpecies()
940 
941  //! @see skipUndeclaredThirdBodies()
943 
944  //! reference to Solution
945  std::weak_ptr<Solution> m_root;
946 };
947 
948 }
949 
950 #endif
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Public interface for kinetics managers.
Definition: Kinetics.h:111
size_t reactionPhaseIndex() const
Phase where the reactions occur.
Definition: Kinetics.h:214
virtual void getEquilibriumConstants(doublereal *kc)
Return a vector of Equilibrium constants.
Definition: Kinetics.h:381
void checkPhaseIndex(size_t m) const
Check that the specified phase index is in range Throws an exception if m is greater than nPhases()
Definition: Kinetics.cpp:50
void checkSpeciesArraySize(size_t mm) const
Check that an array size is at least nSpecies() Throws an exception if kk is less than nSpecies().
Definition: Kinetics.cpp:71
virtual void getDeltaGibbs(doublereal *deltaG)
Return the vector of values for the reaction Gibbs free energy change.
Definition: Kinetics.h:424
Kinetics(const Kinetics &)=delete
Kinetics objects are not copyable or assignable.
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...
Definition: Kinetics.h:900
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range Throws an exception if k is greater than nSpecies(...
Definition: Kinetics.cpp:64
virtual doublereal reactantOrder(size_t k, size_t i) const
Reactant order of species k in reaction i.
Definition: Kinetics.h:570
ValueCache m_cache
Cache for saved calculations within each Kinetics object.
Definition: Kinetics.h:824
virtual bool isReversible(size_t i)
True if reaction i has been declared to be reversible.
Definition: Kinetics.h:614
virtual void getDeltaEntropy(doublereal *deltaS)
Return the vector of values for the reactions change in entropy.
Definition: Kinetics.h:465
virtual void getActivityConcentrations(doublereal *const conc)
Get the vector of activity concentrations used in the kinetics object.
Definition: Kinetics.h:593
shared_ptr< Reaction > reaction(size_t i)
Return the Reaction object for reaction i.
Definition: Kinetics.cpp:613
virtual void getDeltaSSEntropy(doublereal *deltaS)
Return the vector of values for the change in the standard state entropies for each reaction.
Definition: Kinetics.h:507
virtual void getDeltaElectrochemPotentials(doublereal *deltaM)
Return the vector of values for the reaction electrochemical free energy change.
Definition: Kinetics.h:439
thermo_t & speciesPhase(size_t k)
This function takes as an argument the kineticsSpecies index (i.e., the list index in the list of spe...
Definition: Kinetics.h:321
virtual void getRevRateConstants(doublereal *krev, bool doIrreversible=false)
Return the reverse rate constants.
Definition: Kinetics.h:665
vector_fp m_ropnet
Net rate-of-progress for each reaction.
Definition: Kinetics.h:936
virtual void getDeltaEnthalpy(doublereal *deltaH)
Return the vector of values for the reactions change in enthalpy.
Definition: Kinetics.h:452
thermo_t & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism.
Definition: Kinetics.h:227
size_t m_kk
The number of species in all of the phases that participate in this kinetics mechanism.
Definition: Kinetics.h:872
virtual void getFwdRateConstants(doublereal *kfwd)
Return the forward rate constants.
Definition: Kinetics.h:648
size_t m_rxnphase
Phase Index where reactions are assumed to be taking place.
Definition: Kinetics.h:918
size_t phaseIndex(const std::string &ph) const
Return the phase index of a phase in the list of phases defined within the object.
Definition: Kinetics.h:189
StoichManagerN m_irrevProductStoich
Stoichiometry manager for the products of irreversible reactions.
Definition: Kinetics.h:867
doublereal multiplier(size_t i) const
The current value of the multiplier for reaction i.
Definition: Kinetics.h:775
virtual int reactionType(size_t i) const
Flag specifying the type of reaction.
Definition: Kinetics.h:603
void checkPhaseArraySize(size_t mm) const
Check that an array size is at least nPhases() Throws an exception if mm is less than nPhases().
Definition: Kinetics.cpp:57
vector_fp m_perturb
Vector of perturbation factors for each reaction's rate of progress vector.
Definition: Kinetics.h:876
virtual void getReactionDelta(const doublereal *property, doublereal *deltaProperty)
Change in species properties.
Definition: Kinetics.cpp:388
size_t nPhases() const
The number of phases participating in the reaction mechanism.
Definition: Kinetics.h:167
std::string productString(size_t i) const
Returns a string containing the products side of the reaction equation.
Definition: Kinetics.h:633
vector_fp m_rkcn
Reciprocal of the equilibrium constant in concentration units.
Definition: Kinetics.h:927
vector_fp m_ropf
Forward rate-of-progress for each reaction.
Definition: Kinetics.h:930
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...
Definition: Kinetics.cpp:398
vector_fp m_ropr
Reverse rate-of-progress for each reaction.
Definition: Kinetics.h:933
virtual void setMultiplier(size_t i, doublereal f)
Set the multiplier for reaction i to f.
Definition: Kinetics.h:784
virtual bool addReaction(shared_ptr< Reaction > r)
Add a single reaction to the mechanism.
Definition: Kinetics.cpp:476
void skipUndeclaredSpecies(bool skip)
Determine behavior when adding a new reaction that contains species not defined in any of the phases ...
Definition: Kinetics.h:740
std::string kineticsSpeciesName(size_t k) const
Return the name of the kth species in the kinetics manager.
Definition: Kinetics.cpp:284
virtual void setRoot(std::shared_ptr< Solution > root)
Set root Solution holding all phase information.
Definition: Kinetics.h:818
virtual void getNetProductionRates(doublereal *wdot)
Species net production rates [kmol/m^3/s or kmol/m^2/s].
Definition: Kinetics.cpp:433
virtual void init()
Prepare the class for the addition of reactions, after all phases have been added.
Definition: Kinetics.h:700
std::vector< thermo_t * > m_thermo
m_thermo is a vector of pointers to ThermoPhase objects that are involved with this kinetics operator
Definition: Kinetics.h:894
bool m_skipUndeclaredThirdBodies
Definition: Kinetics.h:942
virtual void modifyReaction(size_t i, shared_ptr< Reaction > rNew)
Modify the rate expression associated with a reaction.
Definition: Kinetics.cpp:588
void skipUndeclaredThirdBodies(bool skip)
Determine behavior when adding a new reaction that contains third-body efficiencies for species not d...
Definition: Kinetics.h:752
virtual double productStoichCoeff(size_t k, size_t i) const
Stoichiometric coefficient of species k as a product in reaction i.
Definition: Kinetics.cpp:364
std::map< std::string, size_t > m_phaseindex
Mapping of the phase name to the position of the phase within the kinetics object.
Definition: Kinetics.h:908
bool m_skipUndeclaredSpecies
Definition: Kinetics.h:939
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...
Definition: Kinetics.cpp:326
void selectPhase(const doublereal *data, const thermo_t *phase, doublereal *phase_data)
Definition: Kinetics.cpp:270
size_t m_mindim
number of spatial dimensions of lowest-dimensional phase.
Definition: Kinetics.h:921
std::weak_ptr< Solution > m_root
reference to Solution
Definition: Kinetics.h:945
StoichManagerN m_revProductStoich
Stoichiometry manager for the products of reversible reactions.
Definition: Kinetics.h:864
size_t nReactions() const
Number of reactions in the reaction mechanism.
Definition: Kinetics.h:135
virtual void getDestructionRates(doublereal *ddot)
Species destruction rates [kmol/m^3/s or kmol/m^2/s].
Definition: Kinetics.cpp:422
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
Definition: Kinetics.h:261
size_t m_surfphase
Index in the list of phases of the one surface phase.
Definition: Kinetics.h:911
virtual void getFwdRatesOfProgress(doublereal *fwdROP)
Return the forward rates of progress of the reactions.
Definition: Kinetics.cpp:370
vector_fp m_rfn
Forward rate constant for each reaction.
Definition: Kinetics.h:924
virtual std::string kineticsType() const
Identifies the Kinetics manager type.
Definition: Kinetics.h:130
virtual doublereal productOrder(int k, int i) const
product Order of species k in reaction i.
Definition: Kinetics.h:584
virtual void getDeltaSSGibbs(doublereal *deltaG)
Return the vector of values for the reaction standard state Gibbs free energy change.
Definition: Kinetics.h:479
virtual std::pair< size_t, size_t > checkDuplicates(bool throw_err=true) const
Check for unmarked duplicate reactions and unmatched marked duplicates.
Definition: Kinetics.cpp:78
void checkReactionBalance(const Reaction &R)
Check that the specified reaction is balanced (same number of atoms for each element in the reactants...
Definition: Kinetics.cpp:228
std::string reactantString(size_t i) const
Returns a string containing the reactants side of the reaction equation.
Definition: Kinetics.h:628
StoichManagerN m_reactantStoich
Stoichiometry manager for the reactants for each reaction.
Definition: Kinetics.h:861
virtual void getNetRatesOfProgress(doublereal *netROP)
Net rates of progress.
Definition: Kinetics.cpp:382
std::string reactionString(size_t i) const
Return a string representing the reaction.
Definition: Kinetics.h:623
Kinetics()
Default constructor.
Definition: Kinetics.cpp:21
virtual void getCreationRates(doublereal *cdot)
Species creation rates [kmol/m^3/s or kmol/m^2/s].
Definition: Kinetics.cpp:407
size_t surfacePhaseIndex() const
This returns the integer index of the phase which has ThermoPhase type cSurf.
Definition: Kinetics.h:202
virtual void resizeSpecies()
Resize arrays with sizes that depend on the total number of species.
Definition: Kinetics.cpp:464
std::vector< shared_ptr< Reaction > > m_reactions
Vector of Reaction objects represented by this Kinetics manager.
Definition: Kinetics.h:879
void checkReactionArraySize(size_t ii) const
Check that an array size is at least nReactions() Throws an exception if ii is less than nReactions()...
Definition: Kinetics.cpp:42
size_t nTotalSpecies() const
The total number of species in all phases participating in the kinetics mechanism.
Definition: Kinetics.h:239
virtual double reactantStoichCoeff(size_t k, size_t i) const
Stoichiometric coefficient of species k as a reactant in reaction i.
Definition: Kinetics.cpp:358
void checkReactionIndex(size_t m) const
Check that the specified reaction index is in range Throws an exception if i is greater than nReactio...
Definition: Kinetics.cpp:34
virtual void addPhase(thermo_t &thermo)
Add a phase to the kinetics manager object.
Definition: Kinetics.cpp:445
virtual void getDeltaSSEnthalpy(doublereal *deltaH)
Return the vector of values for the change in the standard state enthalpies of reaction.
Definition: Kinetics.h:493
virtual void getRevRatesOfProgress(doublereal *revROP)
Return the Reverse rates of progress of the reactions.
Definition: Kinetics.cpp:376
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...
Definition: Kinetics.cpp:347
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:187
Intermediate class which stores data about a reaction and its rate parameterization so that it can be...
Definition: Reaction.h:24
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
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...
Definition: Kinetics.cpp:184
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:188
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
ThermoPhase thermo_t
typedef for the ThermoPhase class
Definition: ThermoPhase.h:1908