From 6f8b151e72280b8a934ecb6afe758c0d0a9373e0 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 3 Mar 2023 11:52:32 -0600 Subject: [PATCH] [Kinetics] Used shared pointers in Kinetics::addPhase --- include/cantera/base/Solution.h | 4 +-- include/cantera/kinetics/InterfaceKinetics.h | 3 ++ include/cantera/kinetics/Kinetics.h | 16 ++++++++++ src/kinetics/InterfaceKinetics.cpp | 7 +++++ src/kinetics/Kinetics.cpp | 32 ++++++++++++++++++++ src/kinetics/KineticsFactory.cpp | 2 +- 6 files changed, 61 insertions(+), 3 deletions(-) diff --git a/include/cantera/base/Solution.h b/include/cantera/base/Solution.h index 241887b9e6..ff9901360b 100644 --- a/include/cantera/base/Solution.h +++ b/include/cantera/base/Solution.h @@ -49,9 +49,9 @@ class Solution : public std::enable_shared_from_this virtual void setTransport(shared_ptr transport); //! Set the Transport object by name - //! @param model name of transport model + //! @param model name of transport model; if omitted, the default model is used //! @since New in Cantera 3.0 - void setTransportModel(const std::string& model); + void setTransportModel(const std::string& model=""); //! Accessor for the ThermoPhase pointer shared_ptr thermo() { diff --git a/include/cantera/kinetics/InterfaceKinetics.h b/include/cantera/kinetics/InterfaceKinetics.h index 32d83dcca3..c99d98004b 100644 --- a/include/cantera/kinetics/InterfaceKinetics.h +++ b/include/cantera/kinetics/InterfaceKinetics.h @@ -142,6 +142,9 @@ class InterfaceKinetics : public Kinetics * * @param thermo Reference to the ThermoPhase to be added. */ + virtual void addPhase(shared_ptr thermo); + + //! @see InterfaceKinetics::addPhase(shared_ptr) virtual void addPhase(ThermoPhase& thermo); virtual void init(); diff --git a/include/cantera/kinetics/Kinetics.h b/include/cantera/kinetics/Kinetics.h index 973ad16212..19c38f1cdc 100644 --- a/include/cantera/kinetics/Kinetics.h +++ b/include/cantera/kinetics/Kinetics.h @@ -219,6 +219,12 @@ class Kinetics return m_rxnphase; } + /** + * Return pointer to phase where the reactions occur. + * @since New in Cantera 3.0 + */ + shared_ptr reactionPhase() const; + /** * This method returns a reference to the nth ThermoPhase object defined * in this kinetics mechanism. It is typically used so that member @@ -1098,7 +1104,13 @@ class Kinetics * kinetics manager object as the value. * * @param thermo Reference to the ThermoPhase to be added. + * @since Changed in Cantera 3.0. Replaces version using reference. */ + virtual void addPhase(shared_ptr thermo); + + //! @see Kinetics::addPhase(shared_ptr) + //! @deprecated To be removed after Cantera 3.0. Replaced by version using shared + //! pointer. virtual void addPhase(ThermoPhase& thermo); /** @@ -1320,6 +1332,10 @@ class Kinetics */ std::vector m_thermo; + //! vector of shared pointers, @see m_thermo + //! @todo replace m_thermo with shared version after Cantera 3.0 + vector> m_sharedThermo; + /** * m_start is a vector of integers specifying the beginning position for the * species vector for the n'th phase in the kinetics class. diff --git a/src/kinetics/InterfaceKinetics.cpp b/src/kinetics/InterfaceKinetics.cpp index 7585c136d4..b3afd6325b 100644 --- a/src/kinetics/InterfaceKinetics.cpp +++ b/src/kinetics/InterfaceKinetics.cpp @@ -487,6 +487,13 @@ void InterfaceKinetics::setIOFlag(int ioFlag) } } +void InterfaceKinetics::addPhase(shared_ptr thermo) +{ + Kinetics::addPhase(thermo); + m_phaseExists.push_back(true); + m_phaseIsStable.push_back(true); +} + void InterfaceKinetics::addPhase(ThermoPhase& thermo) { Kinetics::addPhase(thermo); diff --git a/src/kinetics/Kinetics.cpp b/src/kinetics/Kinetics.cpp index 75f7e7802b..b5c874e598 100644 --- a/src/kinetics/Kinetics.cpp +++ b/src/kinetics/Kinetics.cpp @@ -93,6 +93,16 @@ size_t Kinetics::surfacePhaseIndex() const return m_surfphase; } +shared_ptr Kinetics::reactionPhase() const +{ + if (!m_sharedThermo.size()) { + // @todo remove after Cantera 3.0 + throw CanteraError("Kinetics::reactionPhase", + "Cannot access phases that were not added using smart pointers."); + } + return m_sharedThermo[m_rxnphase]; +} + void Kinetics::checkSpeciesIndex(size_t k) const { if (k >= m_kk) { @@ -561,8 +571,30 @@ Eigen::SparseMatrix Kinetics::netProductionRates_ddX() return m_stoichMatrix * netRatesOfProgress_ddX(); } +void Kinetics::addPhase(shared_ptr thermo) +{ + // the phase with lowest dimensionality is assumed to be the + // phase/interface at which reactions take place + if (thermo->nDim() <= m_mindim) { + m_mindim = thermo->nDim(); + m_rxnphase = nPhases(); + } + + // there should only be one surface phase + if (thermo->type() == kineticsType()) { + m_surfphase = nPhases(); + } + m_thermo.push_back(thermo.get()); + m_sharedThermo.push_back(thermo); + m_phaseindex[m_thermo.back()->name()] = nPhases(); + resizeSpecies(); +} + void Kinetics::addPhase(ThermoPhase& thermo) { + warn_deprecated("Kinetics::addPhase", + "To be removed after Cantera 3.0. Use version with shared pointer instead."); + // the phase with lowest dimensionality is assumed to be the // phase/interface at which reactions take place if (thermo.nDim() <= m_mindim) { diff --git a/src/kinetics/KineticsFactory.cpp b/src/kinetics/KineticsFactory.cpp index 5755de3258..295eeab7dc 100644 --- a/src/kinetics/KineticsFactory.cpp +++ b/src/kinetics/KineticsFactory.cpp @@ -89,7 +89,7 @@ shared_ptr newKinetics(const vector>& phases, shared_ptr kin(KineticsFactory::factory()->newKinetics(kinType)); for (auto& phase : phases) { - kin->addPhase(*phase); + kin->addPhase(phase); } kin->init(); addReactions(*kin, phaseNode, rootNode);