From 1f231d1dd01d344cb30e67821f188eeef2a783cd Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 15 Apr 2016 17:05:10 -0400 Subject: [PATCH] [Kinetics] Eliminate need for 'finalize' method All reaction-sized arrays are now allocated as reactions are added, which means that the finalize() method is unnecessary and reactions can be continuously added, even after the Kinetics object has been used for rate calculations. --- include/cantera/kinetics/BulkKinetics.h | 2 - include/cantera/kinetics/EdgeKinetics.h | 6 +- include/cantera/kinetics/GasKinetics.h | 2 - include/cantera/kinetics/InterfaceKinetics.h | 6 +- include/cantera/kinetics/Kinetics.h | 6 +- interfaces/cython/cantera/_cantera.pxd | 1 - interfaces/cython/cantera/base.pyx | 1 - src/kinetics/BulkKinetics.cpp | 10 --- src/kinetics/GasKinetics.cpp | 16 +---- src/kinetics/InterfaceKinetics.cpp | 76 +++----------------- src/kinetics/Kinetics.cpp | 8 +-- src/kinetics/importKinetics.cpp | 4 -- test/kinetics/kineticsFromScratch.cpp | 7 -- 13 files changed, 28 insertions(+), 117 deletions(-) diff --git a/include/cantera/kinetics/BulkKinetics.h b/include/cantera/kinetics/BulkKinetics.h index 925e256f5..43d6e476c 100644 --- a/include/cantera/kinetics/BulkKinetics.h +++ b/include/cantera/kinetics/BulkKinetics.h @@ -36,8 +36,6 @@ public: virtual bool addReaction(shared_ptr r); virtual void init(); - virtual void finalize(); - virtual bool ready() const; virtual void setMultiplier(size_t i, double f); virtual void invalidateCache(); diff --git a/include/cantera/kinetics/EdgeKinetics.h b/include/cantera/kinetics/EdgeKinetics.h index a892bd006..164890b07 100644 --- a/include/cantera/kinetics/EdgeKinetics.h +++ b/include/cantera/kinetics/EdgeKinetics.h @@ -21,7 +21,9 @@ class EdgeKinetics : public InterfaceKinetics { public: //! Constructor - EdgeKinetics() : InterfaceKinetics() {} + EdgeKinetics() : InterfaceKinetics() { + m_nDim = 1; + } EdgeKinetics(const EdgeKinetics& right) : InterfaceKinetics(right) { @@ -44,8 +46,6 @@ public: virtual int type() const { return cEdgeKinetics; } - - virtual void finalize(); }; } diff --git a/include/cantera/kinetics/GasKinetics.h b/include/cantera/kinetics/GasKinetics.h index a0ee54a06..dfde9bb4c 100644 --- a/include/cantera/kinetics/GasKinetics.h +++ b/include/cantera/kinetics/GasKinetics.h @@ -53,8 +53,6 @@ public: virtual void init(); virtual bool addReaction(shared_ptr r); virtual void modifyReaction(size_t i, shared_ptr rNew); - virtual void finalize(); - virtual bool ready() const; virtual void invalidateCache(); //@} diff --git a/include/cantera/kinetics/InterfaceKinetics.h b/include/cantera/kinetics/InterfaceKinetics.h index af03ca21c..d50eda2a5 100644 --- a/include/cantera/kinetics/InterfaceKinetics.h +++ b/include/cantera/kinetics/InterfaceKinetics.h @@ -200,8 +200,6 @@ public: virtual void init(); virtual bool addReaction(shared_ptr r); virtual void modifyReaction(size_t i, shared_ptr rNew); - virtual void finalize(); - virtual bool ready() const; //! @} //! Internal routine that updates the Rates of Progress of the reactions @@ -674,6 +672,10 @@ protected: void applyStickingCorrection(double* kf); int m_ioFlag; + + //! Number of dimensions of reacting phase (2 for InterfaceKinetics, 1 for + //! EdgeKinetics) + size_t m_nDim; }; } diff --git a/include/cantera/kinetics/Kinetics.h b/include/cantera/kinetics/Kinetics.h index 1d4389395..419b9bf4a 100644 --- a/include/cantera/kinetics/Kinetics.h +++ b/include/cantera/kinetics/Kinetics.h @@ -730,6 +730,7 @@ public: * The base class method does nothing, but derived classes may use this to * perform any initialization (allocating arrays, etc.) that must be done * after the reactions are entered. + * @deprecated No longer needed. To be removed after Cantera 2.3. */ virtual void finalize(); @@ -810,9 +811,12 @@ public: /** * Returns true if the kinetics manager has been properly initialized and * finalized. + * @deprecated Object is always ready. To be removed after Cantera 2.3. */ virtual bool ready() const { - return false; + warn_deprecated("Kinetics::ready", + "Object is always ready. To be removed after Cantera 2.3."); + return true; } //! Check for duplicate reactions. diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index ff355740e..455f4220b 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -361,7 +361,6 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera": void init() except + void skipUndeclaredThirdBodies(cbool) void addReaction(shared_ptr[CxxReaction]) except + - void finalize() except + void modifyReaction(int, shared_ptr[CxxReaction]) except + void invalidateCache() except + diff --git a/interfaces/cython/cantera/base.pyx b/interfaces/cython/cantera/base.pyx index d709c9fe9..4a066fdb5 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -102,7 +102,6 @@ cdef class _SolutionBase: self.kinetics.skipUndeclaredThirdBodies(True) for reaction in reactions: self.kinetics.addReaction(reaction._reaction) - self.kinetics.finalize() def __getitem__(self, selection): diff --git a/src/kinetics/BulkKinetics.cpp b/src/kinetics/BulkKinetics.cpp index c2079e7b9..fdc4c9ce7 100644 --- a/src/kinetics/BulkKinetics.cpp +++ b/src/kinetics/BulkKinetics.cpp @@ -144,16 +144,6 @@ void BulkKinetics::init() m_grt.resize(m_kk); } -void BulkKinetics::finalize() -{ - m_finalized = true; -} - -bool BulkKinetics::ready() const -{ - return m_finalized; -} - void BulkKinetics::setMultiplier(size_t i, double f) { Kinetics::setMultiplier(i, f); m_ROP_ok = false; diff --git a/src/kinetics/GasKinetics.cpp b/src/kinetics/GasKinetics.cpp index 1d0346be4..553c8d671 100644 --- a/src/kinetics/GasKinetics.cpp +++ b/src/kinetics/GasKinetics.cpp @@ -294,9 +294,11 @@ void GasKinetics::addFalloffReaction(FalloffReaction& r) } m_falloff_concm.install(nfall, efficiencies, r.third_body.default_efficiency); + concm_falloff_values.resize(m_falloff_concm.workSize()); // install the falloff function calculator for this reaction m_falloffn.install(nfall, r.reaction_type, r.falloff); + falloff_work.resize(m_falloffn.workSize()); } void GasKinetics::addThreeBodyReaction(ThreeBodyReaction& r) @@ -315,6 +317,7 @@ void GasKinetics::addThreeBodyReaction(ThreeBodyReaction& r) } m_3b_concm.install(nReactions()-1, efficiencies, r.third_body.default_efficiency); + concm_3b_values.resize(m_3b_concm.workSize()); } void GasKinetics::addPlogReaction(PlogReaction& r) @@ -389,19 +392,6 @@ void GasKinetics::init() m_logp_ref = log(thermo().refPressure()) - log(GasConstant); } -void GasKinetics::finalize() -{ - BulkKinetics::finalize(); - falloff_work.resize(m_falloffn.workSize()); - concm_3b_values.resize(m_3b_concm.workSize()); - concm_falloff_values.resize(m_falloff_concm.workSize()); -} - -bool GasKinetics::ready() const -{ - return m_finalized; -} - void GasKinetics::invalidateCache() { BulkKinetics::invalidateCache(); diff --git a/src/kinetics/InterfaceKinetics.cpp b/src/kinetics/InterfaceKinetics.cpp index 82839eebb..d2654d4a6 100644 --- a/src/kinetics/InterfaceKinetics.cpp +++ b/src/kinetics/InterfaceKinetics.cpp @@ -5,7 +5,6 @@ // Copyright 2002 California Institute of Technology #include "cantera/kinetics/InterfaceKinetics.h" -#include "cantera/kinetics/EdgeKinetics.h" #include "cantera/kinetics/RateCoeffMgr.h" #include "cantera/kinetics/ImplicitSurfChem.h" #include "cantera/thermo/SurfPhase.h" @@ -31,7 +30,8 @@ InterfaceKinetics::InterfaceKinetics(thermo_t* thermo) : m_has_electrochem_rxns(false), m_has_exchange_current_density_formulation(false), m_phaseExistsCheck(false), - m_ioFlag(0) + m_ioFlag(0), + m_nDim(2) { if (thermo != 0) { addPhase(*thermo); @@ -708,6 +708,12 @@ bool InterfaceKinetics::addReaction(shared_ptr r_base) size_t p = speciesPhaseIndex(k); m_rxnPhaseIsProduct[i][p] = true; } + + deltaElectricEnergy_.push_back(0.0); + m_deltaG0.push_back(0.0); + m_deltaG.push_back(0.0); + m_ProdStanConcReac.push_back(0.0); + return true; } @@ -828,38 +834,25 @@ void InterfaceKinetics::init() } m_actConc.resize(m_kk); m_conc.resize(m_kk); + m_StandardConc.resize(m_kk, 0.0); m_mu0.resize(m_kk); m_mu.resize(m_kk); m_mu0_Kc.resize(m_kk); m_grt.resize(m_kk); m_pot.resize(m_kk, 0.0); m_phi.resize(nPhases(), 0.0); -} -void InterfaceKinetics::finalize() -{ - Kinetics::finalize(); - deltaElectricEnergy_.resize(nReactions()); size_t ks = reactionPhaseIndex(); if (ks == npos) throw CanteraError("InterfaceKinetics::finalize", "no surface phase is present."); // Check to see that the interface routine has a dimension of 2 m_surf = (SurfPhase*)&thermo(ks); - if (m_surf->nDim() != 2) { + if (m_surf->nDim() != m_nDim) { throw CanteraError("InterfaceKinetics::finalize", "expected interface dimension = 2, but got dimension = {}", m_surf->nDim()); } - m_StandardConc.resize(m_kk, 0.0); - m_deltaG0.resize(nReactions(), 0.0); - m_deltaG.resize(nReactions(), 0.0); - m_ProdStanConcReac.resize(nReactions(), 0.0); - - if (m_thermo.size() != m_phaseExists.size()) { - throw CanteraError("InterfaceKinetics::finalize", "internal error"); - } - m_finalized = true; } doublereal InterfaceKinetics::electrochem_beta(size_t irxn) const @@ -872,11 +865,6 @@ doublereal InterfaceKinetics::electrochem_beta(size_t irxn) const return 0.0; } -bool InterfaceKinetics::ready() const -{ - return m_finalized; -} - void InterfaceKinetics::advanceCoverages(doublereal tstep) { if (m_integrator == 0) { @@ -1013,48 +1001,4 @@ void InterfaceKinetics::applyStickingCorrection(double* kf) } } - -void EdgeKinetics::finalize() -{ - // Note we can't call the Interface::finalize() routine because we need to - // check for a dimension of 1 below. Therefore, we have to malloc room in - // arrays that would normally be handled by the - // InterfaceKinetics::finalize() call. - Kinetics::finalize(); - - size_t safe_reaction_size = std::max(nReactions(), 1); - deltaElectricEnergy_.resize(safe_reaction_size); - size_t ks = reactionPhaseIndex(); - if (ks == npos) throw CanteraError("EdgeKinetics::finalize", - "no surface phase is present."); - - // Check to see edge phase has a dimension of 1 - m_surf = (SurfPhase*)&thermo(ks); - if (m_surf->nDim() != 1) { - throw CanteraError("EdgeKinetics::finalize", - "expected interface dimension = 1, but got dimension = {}", - m_surf->nDim()); - } - m_StandardConc.resize(m_kk, 0.0); - m_deltaG0.resize(safe_reaction_size, 0.0); - m_deltaG.resize(safe_reaction_size, 0.0); - - m_ProdStanConcReac.resize(safe_reaction_size, 0.0); - - if (m_thermo.size() != m_phaseExists.size()) { - throw CanteraError("InterfaceKinetics::finalize", "internal error"); - } - - // Guarantee that these arrays can be converted to double* even in the - // special case where there are no reactions defined. - if (!nReactions()) { - m_perturb.resize(1, 1.0); - m_ropf.resize(1, 0.0); - m_ropr.resize(1, 0.0); - m_ropnet.resize(1, 0.0); - m_rkcn.resize(1, 0.0); - } - m_finalized = true; -} - } diff --git a/src/kinetics/Kinetics.cpp b/src/kinetics/Kinetics.cpp index 0b2e78f9e..18a8075c0 100644 --- a/src/kinetics/Kinetics.cpp +++ b/src/kinetics/Kinetics.cpp @@ -513,15 +513,13 @@ void Kinetics::addPhase(thermo_t& thermo) } m_thermo.push_back(&thermo); m_phaseindex[m_thermo.back()->id()] = nPhases(); + m_kk += thermo.nSpecies(); } void Kinetics::finalize() { - m_kk = 0; - for (size_t n = 0; n < nPhases(); n++) { - size_t nsp = m_thermo[n]->nSpecies(); - m_kk += nsp; - } + warn_deprecated("Kinetics::finalize", + "No longer needed. To be removed after Cantera 2.3."); } bool Kinetics::addReaction(shared_ptr r) diff --git a/src/kinetics/importKinetics.cpp b/src/kinetics/importKinetics.cpp index 58f6b398d..bd030e338 100644 --- a/src/kinetics/importKinetics.cpp +++ b/src/kinetics/importKinetics.cpp @@ -36,7 +36,6 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin, // purely additive. vector rarrays = p.getChildren("reactionArray"); if (rarrays.empty()) { - kin.finalize(); return false; } for (size_t n = 0; n < rarrays.size(); n++) { @@ -122,9 +121,6 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin, kin.checkDuplicates(); } - // Finalize the installation of the kinetics, now that we know the true - // number of reactions in the mechanism, itot. - kin.finalize(); return true; } diff --git a/test/kinetics/kineticsFromScratch.cpp b/test/kinetics/kineticsFromScratch.cpp index 81a956f72..d91d4a15b 100644 --- a/test/kinetics/kineticsFromScratch.cpp +++ b/test/kinetics/kineticsFromScratch.cpp @@ -57,7 +57,6 @@ TEST_F(KineticsFromScratch, add_elementary_reaction) auto R = make_shared(reac, prod, rate); kin.addReaction(R); - kin.finalize(); check_rates(0); } @@ -74,7 +73,6 @@ TEST_F(KineticsFromScratch, add_three_body_reaction) auto R = make_shared(reac, prod, rate, tbody); kin.addReaction(R); - kin.finalize(); check_rates(1); } @@ -123,7 +121,6 @@ TEST_F(KineticsFromScratch, add_falloff_reaction) auto R = make_shared(reac, prod, low_rate, high_rate, tbody); R->falloff = newFalloff(TROE_FALLOFF, falloff_params); kin.addReaction(R); - kin.finalize(); check_rates(2); } @@ -146,7 +143,6 @@ TEST_F(KineticsFromScratch, add_plog_reaction) auto R = make_shared(reac, prod, Plog(rates)); kin.addReaction(R); - kin.finalize(); check_rates(3); } @@ -194,7 +190,6 @@ TEST_F(KineticsFromScratch, add_chebyshev_reaction) auto R = make_shared(reac, prod, rate); kin.addReaction(R); - kin.finalize(); check_rates(4); } @@ -369,7 +364,6 @@ TEST_F(InterfaceKineticsFromScratch, add_surface_reaction) auto R = make_shared(reac, prod, rate); kin.addReaction(R); - kin.finalize(); check_rates(3); } @@ -384,6 +378,5 @@ TEST_F(InterfaceKineticsFromScratch, add_sticking_reaction) auto R = make_shared(reac, prod, rate, true); kin.addReaction(R); - kin.finalize(); check_rates(0); }