From 799ef815183f04cc5b69da6bfb8a12fe9676cf38 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 16 Aug 2019 10:42:30 -0500 Subject: [PATCH] [Base] link managers back to SolutionBase --- include/cantera/base/Base.h | 7 ++++++- include/cantera/kinetics/Kinetics.h | 10 ++++++++++ include/cantera/thermo/ThermoPhase.h | 10 ++++++++++ include/cantera/transport/TransportBase.h | 10 ++++++++++ interfaces/cython/cantera/_cantera.pxd | 3 +++ interfaces/cython/cantera/base.pyx | 11 ++++++++--- src/base/Base.cpp | 13 +++++++++++++ 7 files changed, 60 insertions(+), 4 deletions(-) diff --git a/include/cantera/base/Base.h b/include/cantera/base/Base.h index 4c3acea5a..9c0846422 100644 --- a/include/cantera/base/Base.h +++ b/include/cantera/base/Base.h @@ -16,7 +16,8 @@ class Kinetics; class Transport; //! A container class holding managers for all pieces defining a phase -class SolutionBase { +class SolutionBase : public std::enable_shared_from_this +{ public: SolutionBase(); SolutionBase(const std::string& infile, const std::string& phasename); @@ -24,6 +25,10 @@ public: SolutionBase(const SolutionBase&) = delete; SolutionBase& operator=(const SolutionBase&) = delete; + static shared_ptr create() { + return shared_ptr( new SolutionBase ); + } + //! String indicating the type of the SolutionBase object. Corresponds //! to the type of phase originally instantiated std::string type() const { diff --git a/include/cantera/kinetics/Kinetics.h b/include/cantera/kinetics/Kinetics.h index 17b232d4b..4cacaa8e4 100644 --- a/include/cantera/kinetics/Kinetics.h +++ b/include/cantera/kinetics/Kinetics.h @@ -19,6 +19,8 @@ namespace Cantera { +class SolutionBase; + /** * @defgroup chemkinetics Chemical Kinetics */ @@ -814,6 +816,11 @@ public: void selectPhase(const doublereal* data, const thermo_t* phase, doublereal* phase_data); + //! Set root SolutionBase holding all phase information + virtual void setRoot(std::shared_ptr root) { + m_root = root; + } + protected: //! Cache for saved calculations within each Kinetics object. ValueCache m_cache; @@ -935,6 +942,9 @@ protected: //! @see skipUndeclaredThirdBodies() bool m_skipUndeclaredThirdBodies; + + //! reference to SolutionBase + std::weak_ptr m_root; }; } diff --git a/include/cantera/thermo/ThermoPhase.h b/include/cantera/thermo/ThermoPhase.h index fdac8cf29..2b21ce6e1 100644 --- a/include/cantera/thermo/ThermoPhase.h +++ b/include/cantera/thermo/ThermoPhase.h @@ -40,6 +40,8 @@ const int cSS_CONVENTION_VPSS = 1; const int cSS_CONVENTION_SLAVE = 2; //@} +class SolutionBase; + //! Base class for a phase with thermodynamic properties. /*! * Class ThermoPhase is the base class for the family of classes that represent @@ -1616,6 +1618,11 @@ public: //@} + //! Set root SolutionBase holding all phase information + virtual void setRoot(std::shared_ptr root) { + m_root = root; + } + protected: //! Fills `names` and `data` with the column names and species thermo //! properties to be included in the output of the reportCSV method. @@ -1660,6 +1667,9 @@ protected: //! last value of the temperature processed by reference state mutable doublereal m_tlast; + + //! reference to SolutionBase + std::weak_ptr m_root; }; //! typedef for the ThermoPhase class diff --git a/include/cantera/transport/TransportBase.h b/include/cantera/transport/TransportBase.h index 9447fd677..171aff9f3 100644 --- a/include/cantera/transport/TransportBase.h +++ b/include/cantera/transport/TransportBase.h @@ -74,6 +74,8 @@ const VelocityBasis VB_SPECIES_2 = 2; const VelocityBasis VB_SPECIES_3 = 3; //@} +class SolutionBase; + //! Base class for transport property managers. /*! * All classes that compute transport properties for a single phase derive from @@ -654,6 +656,11 @@ public: */ virtual void setThermo(thermo_t& thermo); + //! Set root SolutionBase holding all phase information + virtual void setRoot(std::shared_ptr root) { + m_root = root; + } + protected: //! Enable the transport object for use. /*! @@ -680,6 +687,9 @@ protected: //! Velocity basis from which diffusion velocities are computed. //! Defaults to the mass averaged basis = -2 int m_velocityBasis; + + //! reference to SolutionBase + std::weak_ptr m_root; }; } diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index ff5ac7219..ff576b20e 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -132,6 +132,8 @@ cdef extern from "cantera/base/Base.h" namespace "Cantera": void setKinetics(shared_ptr[CxxKinetics]) void setTransport(shared_ptr[CxxTransport]) + cdef shared_ptr[CxxSolutionBase] CxxNewSolutionBase "Cantera::SolutionBase::create" () + cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera": cdef cppclass CxxThermoPhase "Cantera::ThermoPhase": @@ -942,6 +944,7 @@ cdef class GasTransportData: cdef class _SolutionBase: cdef CxxSolutionBase* base + cdef shared_ptr[CxxSolutionBase] _base cdef shared_ptr[CxxThermoPhase] _thermo cdef CxxThermoPhase* thermo cdef shared_ptr[CxxKinetics] _kinetics diff --git a/interfaces/cython/cantera/base.pyx b/interfaces/cython/cantera/base.pyx index 802b8c13f..4bb8dcb57 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -18,6 +18,7 @@ cdef class _SolutionBase: self.thermo = other.thermo self.kinetics = other.kinetics self.transport = other.transport + self._base = other._base self._thermo = other._thermo self._kinetics = other._kinetics self._transport = other._transport @@ -26,9 +27,14 @@ cdef class _SolutionBase: self._selected_species = other._selected_species.copy() return - # Assign type and unique_name during __init__ - self.base = new CxxSolutionBase() + # Assign base and set managers to NULL + self._base = CxxNewSolutionBase() + self.base = self._base.get() + self.thermo = NULL + self.kinetics = NULL + self.transport = NULL + # Parse inputs if infile.endswith('.yml') or infile.endswith('.yaml') or yaml: self._init_yaml(infile, phaseid, phases, yaml) elif infile or source: @@ -41,7 +47,6 @@ cdef class _SolutionBase: # Initialization of transport is deferred to Transport.__init__ self.base.setThermoPhase(self._thermo) self.base.setKinetics(self._kinetics) - self.transport = NULL self._selected_species = np.ndarray(0, dtype=np.integer) diff --git a/src/base/Base.cpp b/src/base/Base.cpp index 367b5659d..82fe246e0 100644 --- a/src/base/Base.cpp +++ b/src/base/Base.cpp @@ -4,6 +4,9 @@ // at https://cantera.org/license.txt for license and copyright information. #include "cantera/base/Base.h" +#include "cantera/thermo/ThermoPhase.h" +#include "cantera/kinetics/Kinetics.h" +#include "cantera/transport/TransportBase.h" namespace Cantera { @@ -24,15 +27,25 @@ SolutionBase::SolutionBase(const std::string& infile, throw NotImplementedError("SolutionBase constructor from file"); } + void SolutionBase::setThermoPhase(shared_ptr thermo) { m_thermo = thermo; + if (m_thermo) { + m_thermo->setRoot(shared_from_this()); + } } void SolutionBase::setKinetics(shared_ptr kinetics) { m_kinetics = kinetics; + if (m_kinetics) { + m_kinetics->setRoot(shared_from_this()); + } } void SolutionBase::setTransport(shared_ptr transport) { m_transport = transport; + if (m_transport) { + m_transport->setRoot(shared_from_this()); + } } }