From 4856d1328b9339459725d63a1e76671e15d91052 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 31 Jul 2017 23:27:10 -0400 Subject: [PATCH] [Thermo] Make IonsFromNeutralVPSSTP::neutralMoleculePhase_ a shared_ptr Also make it non-public and add getters and setters --- .../cantera/thermo/IonsFromNeutralVPSSTP.h | 39 ++---------- include/cantera/thermo/PDSS_IonsFromNeutral.h | 5 +- src/thermo/IonsFromNeutralVPSSTP.cpp | 62 ++++++++----------- src/thermo/PDSS_IonsFromNeutral.cpp | 2 +- src/transport/LiquidTranInteraction.cpp | 2 +- 5 files changed, 34 insertions(+), 76 deletions(-) diff --git a/include/cantera/thermo/IonsFromNeutralVPSSTP.h b/include/cantera/thermo/IonsFromNeutralVPSSTP.h index fb9c0eb42..31522f9a0 100644 --- a/include/cantera/thermo/IonsFromNeutralVPSSTP.h +++ b/include/cantera/thermo/IonsFromNeutralVPSSTP.h @@ -85,17 +85,9 @@ public: * to set up the object * @param id ID of the phase in the input file. Defaults to the * empty string. - * @param neutralPhase The object takes a neutralPhase ThermoPhase object - * as input. It can either take a pointer to an existing object in the - * parameter list, in which case it does not own the object, or it can - * construct a neutral Phase as a slave object, in which case, it does - * own the slave object, for purposes of who gets to destroy the object. - * If this parameter is zero, then a slave neutral phase object is - * created and used. */ IonsFromNeutralVPSSTP(const std::string& inputFile, - const std::string& id = "", - ThermoPhase* neutralPhase = 0); + const std::string& id = ""); //! Construct and initialize an IonsFromNeutralVPSSTP object //! directly from an XML database @@ -103,18 +95,8 @@ public: * @param phaseRoot XML phase node containing the description of the phase * @param id id attribute containing the name of the phase. * (default is the empty string) - * @param neutralPhase The object takes a neutralPhase ThermoPhase object - * as input. It can either take a pointer to an existing object in the - * parameter list, in which case it does not own the object, or it can - * construct a neutral Phase as a slave object, in which case, it does - * own the slave object, for purposes of who gets to destroy the object. - * If this parameter is zero, then a slave neutral phase object is - * created and used. */ - IonsFromNeutralVPSSTP(XML_Node& phaseRoot, const std::string& id = "", - ThermoPhase* neutralPhase = 0); - - virtual ~IonsFromNeutralVPSSTP(); + IonsFromNeutralVPSSTP(XML_Node& phaseRoot, const std::string& id = ""); // @} //! @name Utilities @@ -289,6 +271,9 @@ public: //@} + void setNeutralMoleculePhase(shared_ptr neutral); + shared_ptr getNeutralMoleculePhase(); + virtual void initThermo(); virtual void initThermoXML(XML_Node& phaseNode, const std::string& id); virtual void setParametersFromXML(const XML_Node& thermoNode); @@ -420,13 +405,8 @@ protected: //! neutralMoleculePhase ThermoPhase. These have neutral charges. std::vector passThroughList_; -public: //! This is a pointer to the neutral Molecule Phase - /*! - * If the variable, IOwnNThermoPhase_ is true, then we own - * the pointer. If not, then this is considered a shallow pointer. - */ - ThermoPhase* neutralMoleculePhase_; + shared_ptr neutralMoleculePhase_; private: GibbsExcessVPSSTP* geThermo; @@ -437,13 +417,6 @@ private: mutable vector_fp dX_NeutralMolecule_; mutable vector_fp m_work; // length m_kk - //! If true then we own the underlying neutral Molecule Phase - /*! - * If this is false, then the neutral molecule phase is considered - * as a shallow pointer. - */ - bool IOwnNThermoPhase_; - //! Temporary mole fraction vector mutable vector_fp moleFractionsTmp_; diff --git a/include/cantera/thermo/PDSS_IonsFromNeutral.h b/include/cantera/thermo/PDSS_IonsFromNeutral.h index aca44108b..8181c80dd 100644 --- a/include/cantera/thermo/PDSS_IonsFromNeutral.h +++ b/include/cantera/thermo/PDSS_IonsFromNeutral.h @@ -96,10 +96,7 @@ public: protected: //! Pointer to the Neutral Molecule ThermoPhase object - /*! - * This is a shallow pointer. - */ - const ThermoPhase* neutralMoleculePhase_; + shared_ptr neutralMoleculePhase_; std::map neutralSpeciesMultipliers_; diff --git a/src/thermo/IonsFromNeutralVPSSTP.cpp b/src/thermo/IonsFromNeutralVPSSTP.cpp index fba0f4244..54fa9639f 100644 --- a/src/thermo/IonsFromNeutralVPSSTP.cpp +++ b/src/thermo/IonsFromNeutralVPSSTP.cpp @@ -32,50 +32,30 @@ IonsFromNeutralVPSSTP::IonsFromNeutralVPSSTP() : indexSpecialSpecies_(npos), indexSecondSpecialSpecies_(npos), neutralMoleculePhase_(0), - geThermo(0), - IOwnNThermoPhase_(true) + geThermo(0) { } IonsFromNeutralVPSSTP::IonsFromNeutralVPSSTP(const std::string& inputFile, - const std::string& id_, - ThermoPhase* neutralPhase) : + const std::string& id_) : ionSolnType_(cIonSolnType_SINGLEANION), numNeutralMoleculeSpecies_(0), indexSpecialSpecies_(npos), - indexSecondSpecialSpecies_(npos), - neutralMoleculePhase_(neutralPhase), - IOwnNThermoPhase_(true) + indexSecondSpecialSpecies_(npos) { - if (neutralPhase) { - IOwnNThermoPhase_ = false; - } initThermoFile(inputFile, id_); } IonsFromNeutralVPSSTP::IonsFromNeutralVPSSTP(XML_Node& phaseRoot, - const std::string& id_, ThermoPhase* neutralPhase) : + const std::string& id_) : ionSolnType_(cIonSolnType_SINGLEANION), numNeutralMoleculeSpecies_(0), indexSpecialSpecies_(npos), - indexSecondSpecialSpecies_(npos), - neutralMoleculePhase_(neutralPhase), - IOwnNThermoPhase_(true) + indexSecondSpecialSpecies_(npos) { - if (neutralPhase) { - IOwnNThermoPhase_ = false; - } importPhase(phaseRoot, this); } -IonsFromNeutralVPSSTP::~IonsFromNeutralVPSSTP() -{ - if (IOwnNThermoPhase_) { - delete neutralMoleculePhase_; - neutralMoleculePhase_ = 0; - } -} - // ------------ Molar Thermodynamic Properties ---------------------- doublereal IonsFromNeutralVPSSTP::enthalpy_mole() const @@ -474,20 +454,15 @@ void IonsFromNeutralVPSSTP::initThermo() { initLengths(); GibbsExcessVPSSTP::initThermo(); - geThermo = dynamic_cast(neutralMoleculePhase_); } -void IonsFromNeutralVPSSTP::initLengths() +void IonsFromNeutralVPSSTP::setNeutralMoleculePhase(shared_ptr neutral) { + neutralMoleculePhase_ = neutral; + geThermo = dynamic_cast(neutralMoleculePhase_.get()); numNeutralMoleculeSpecies_ = neutralMoleculePhase_->nSpecies(); - moleFractions_.resize(m_kk); fm_neutralMolec_ions_.resize(numNeutralMoleculeSpecies_ * m_kk); - fm_invert_ionForNeutral.resize(m_kk); NeutralMolecMoleFractions_.resize(numNeutralMoleculeSpecies_); - cationList_.resize(m_kk); - anionList_.resize(m_kk); - passThroughList_.resize(m_kk); - moleFractionsTmp_.resize(m_kk); muNeutralMolecule_.resize(numNeutralMoleculeSpecies_); lnActCoeff_NeutralMolecule_.resize(numNeutralMoleculeSpecies_); dlnActCoeffdT_NeutralMolecule_.resize(numNeutralMoleculeSpecies_); @@ -497,6 +472,22 @@ void IonsFromNeutralVPSSTP::initLengths() y_.resize(numNeutralMoleculeSpecies_, 0.0); dlnActCoeff_NeutralMolecule_.resize(numNeutralMoleculeSpecies_, 0.0); dX_NeutralMolecule_.resize(numNeutralMoleculeSpecies_, 0.0); +} + +shared_ptr IonsFromNeutralVPSSTP::getNeutralMoleculePhase() +{ + return neutralMoleculePhase_; +} + +void IonsFromNeutralVPSSTP::initLengths() +{ + moleFractions_.resize(m_kk); + fm_neutralMolec_ions_.resize(numNeutralMoleculeSpecies_ * m_kk); + fm_invert_ionForNeutral.resize(m_kk); + cationList_.resize(m_kk); + anionList_.resize(m_kk); + passThroughList_.resize(m_kk); + moleFractionsTmp_.resize(m_kk); m_work.resize(m_kk); } @@ -549,10 +540,7 @@ void IonsFromNeutralVPSSTP::setParametersFromXML(const XML_Node& thermoNode) "neut_ptr = 0"); } - // Create the neutralMolecule ThermoPhase if we haven't already - if (!neutralMoleculePhase_) { - neutralMoleculePhase_ = newPhase(*neut_ptr); - } + setNeutralMoleculePhase(shared_ptr(newPhase(*neut_ptr))); } void IonsFromNeutralVPSSTP::initThermoXML(XML_Node& phaseNode, const std::string& id_) diff --git a/src/thermo/PDSS_IonsFromNeutral.cpp b/src/thermo/PDSS_IonsFromNeutral.cpp index 46dcd39f8..489cc1c07 100644 --- a/src/thermo/PDSS_IonsFromNeutral.cpp +++ b/src/thermo/PDSS_IonsFromNeutral.cpp @@ -27,7 +27,7 @@ PDSS_IonsFromNeutral::PDSS_IonsFromNeutral() void PDSS_IonsFromNeutral::setParent(VPStandardStateTP* phase, size_t k) { - neutralMoleculePhase_ = dynamic_cast(*phase).neutralMoleculePhase_; + neutralMoleculePhase_ = dynamic_cast(*phase).getNeutralMoleculePhase(); } void PDSS_IonsFromNeutral::setParametersFromXML(const XML_Node& speciesNode) diff --git a/src/transport/LiquidTranInteraction.cpp b/src/transport/LiquidTranInteraction.cpp index 4fa0c9dea..f89a9ba99 100644 --- a/src/transport/LiquidTranInteraction.cpp +++ b/src/transport/LiquidTranInteraction.cpp @@ -517,7 +517,7 @@ void LTI_StefanMaxwell_PPN::getMatrixTransProp(DenseMatrix& mat, doublereal* spe } m_ionCondMix = m_ionCondMixModel->getMixTransProp(m_ionCondSpecies); - MargulesVPSSTP* marg_thermo = dynamic_cast(ions_thermo->neutralMoleculePhase_); + MargulesVPSSTP* marg_thermo = dynamic_cast(ions_thermo->getNeutralMoleculePhase().get()); doublereal vol = m_thermo->molarVolume(); size_t k = 0;