From 5195e4f9b8947e7700c1d92d89408a21c0a43b78 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 27 Feb 2015 18:33:22 -0500 Subject: [PATCH] [Thermo] Use shared_ptr to STIT objects in Species and SpeciesThermo Exclusive ownership of the SpeciesThermoInterpType objects is no longer assumed. --- include/cantera/thermo/GeneralSpeciesThermo.h | 12 ++---- include/cantera/thermo/SimpleThermo.h | 3 +- include/cantera/thermo/Species.h | 6 +-- include/cantera/thermo/SpeciesThermo.h | 5 ++- include/cantera/thermo/SpeciesThermoMgr.h | 3 +- src/thermo/FixedChemPotSSTP.cpp | 5 ++- src/thermo/GeneralSpeciesThermo.cpp | 43 ++++++------------- src/thermo/LatticeSolidPhase.cpp | 2 +- src/thermo/NasaThermo.h | 2 +- src/thermo/ShomateThermo.h | 3 +- src/thermo/Species.cpp | 28 +++--------- src/thermo/SpeciesThermoFactory.cpp | 2 +- src/thermo/ThermoPhase.cpp | 5 +-- src/thermo/VPSSMgr.cpp | 2 +- src/thermo/VPSSMgr_IdealGas.cpp | 2 +- 15 files changed, 41 insertions(+), 82 deletions(-) diff --git a/include/cantera/thermo/GeneralSpeciesThermo.h b/include/cantera/thermo/GeneralSpeciesThermo.h index c4f365c2a..44b04daa9 100644 --- a/include/cantera/thermo/GeneralSpeciesThermo.h +++ b/include/cantera/thermo/GeneralSpeciesThermo.h @@ -43,9 +43,6 @@ public: */ GeneralSpeciesThermo& operator=(const GeneralSpeciesThermo& b); - //! Destructor - virtual ~GeneralSpeciesThermo(); - virtual SpeciesThermo* duplMyselfAsSpeciesThermo() const ; //! Install a new species thermodynamic property @@ -81,7 +78,8 @@ public: doublereal minTemp, doublereal maxTemp, doublereal refPressure); - virtual void install_STIT(size_t index, SpeciesThermoInterpType* stit_ptr); + virtual void install_STIT(size_t index, + shared_ptr stit_ptr); //! Install a PDSS object to handle the reference state thermodynamics //! calculation @@ -134,16 +132,12 @@ private: SpeciesThermoInterpType* provideSTIT(size_t k); const SpeciesThermoInterpType* provideSTIT(size_t k) const; - void clear(); // > STIT_map; + typedef std::map > > STIT_map; typedef std::map > tpoly_map; /** * This is the main unknown in the object. It contains pointers to * SpeciesThermoInterpType objects, sorted by the parameterization type. - * This object owns the SpeciesThermoInterpType objects, so they are deleted - * in the destructor of this object. */ STIT_map m_sp; diff --git a/include/cantera/thermo/SimpleThermo.h b/include/cantera/thermo/SimpleThermo.h index df906abf8..325773de0 100644 --- a/include/cantera/thermo/SimpleThermo.h +++ b/include/cantera/thermo/SimpleThermo.h @@ -181,7 +181,8 @@ public: markInstalled(index); } - virtual void install_STIT(size_t index, SpeciesThermoInterpType* stit_ptr) { + virtual void install_STIT(size_t index, + shared_ptr stit_ptr) { throw CanteraError("install_STIT", "not implemented"); } diff --git a/include/cantera/thermo/Species.h b/include/cantera/thermo/Species.h index f3ad4a118..5c5fb08c8 100644 --- a/include/cantera/thermo/Species.h +++ b/include/cantera/thermo/Species.h @@ -35,9 +35,6 @@ public: Species& operator=(const Species& other); ~Species(); - //! Access the thermodynamic parameterization for the species - SpeciesThermoInterpType& thermo(); - //! The name of the species std::string name; @@ -53,9 +50,8 @@ public: shared_ptr transport; -protected: //! Thermodynamic data for the species - SpeciesThermoInterpType* thermo_; + shared_ptr thermo; }; } diff --git a/include/cantera/thermo/SpeciesThermo.h b/include/cantera/thermo/SpeciesThermo.h index 8f081431e..a2bdffc6e 100644 --- a/include/cantera/thermo/SpeciesThermo.h +++ b/include/cantera/thermo/SpeciesThermo.h @@ -10,6 +10,7 @@ #define CT_SPECIESTHERMO_H #include "cantera/base/ct_defs.h" +#include "cantera/base/smart_ptr.h" namespace Cantera { @@ -201,11 +202,11 @@ public: //! parameterization for one species. /*! * @param index Index of the species being installed - * @param stit_ptr Pointer to the SpeciesThermoInterpType object + * @param stit Pointer to the SpeciesThermoInterpType object * This will set up the thermo for one species */ virtual void install_STIT(size_t index, - SpeciesThermoInterpType* stit_ptr) = 0; + shared_ptr stit) = 0; //! Compute the reference-state properties for all species. /*! diff --git a/include/cantera/thermo/SpeciesThermoMgr.h b/include/cantera/thermo/SpeciesThermoMgr.h index 4365094d1..22df41900 100644 --- a/include/cantera/thermo/SpeciesThermoMgr.h +++ b/include/cantera/thermo/SpeciesThermoMgr.h @@ -84,7 +84,8 @@ public: doublereal minTemp, doublereal maxTemp, doublereal refPressure); - virtual void install_STIT(size_t index, SpeciesThermoInterpType* stit_ptr) { + virtual void install_STIT(size_t index, + shared_ptr stit_ptr) { throw CanteraError("install_STIT", "not implemented"); } diff --git a/src/thermo/FixedChemPotSSTP.cpp b/src/thermo/FixedChemPotSSTP.cpp index d7e283f1b..b4d5a0671 100644 --- a/src/thermo/FixedChemPotSSTP.cpp +++ b/src/thermo/FixedChemPotSSTP.cpp @@ -15,6 +15,7 @@ #include "cantera/thermo/FixedChemPotSSTP.h" #include "cantera/thermo/ThermoFactory.h" #include "cantera/thermo/SpeciesThermoFactory.h" +#include "cantera/thermo/SpeciesThermoInterpType.h" #include "cantera/base/ctml.h" #include "cantera/base/stringUtils.h" @@ -92,8 +93,8 @@ FixedChemPotSSTP::FixedChemPotSSTP(const std::string& Ename, doublereal val) : c[1] = val; c[2] = 0.0; c[3] = 0.0; - SpeciesThermoInterpType* stit = - newSpeciesThermoInterpType("const_cp", 0.1, 1e30, OneAtm, c); + shared_ptr stit( + newSpeciesThermoInterpType("const_cp", 0.1, 1e30, OneAtm, c)); m_spthermo->install_STIT(0, stit); initThermo(); m_p0 = OneAtm; diff --git a/src/thermo/GeneralSpeciesThermo.cpp b/src/thermo/GeneralSpeciesThermo.cpp index b3c8e52b4..c0ac3236b 100644 --- a/src/thermo/GeneralSpeciesThermo.cpp +++ b/src/thermo/GeneralSpeciesThermo.cpp @@ -26,14 +26,14 @@ GeneralSpeciesThermo::GeneralSpeciesThermo(const GeneralSpeciesThermo& b) : m_thigh_min(b.m_thigh_min), m_p0(b.m_p0) { - clear(); + m_sp.clear(); // Copy SpeciesThermoInterpTypes from 'b' for (STIT_map::const_iterator iter = b.m_sp.begin(); iter != b.m_sp.end(); iter++) { for (size_t k = 0; k < iter->second.size(); k++) { - const SpeciesThermoInterpType* spec = iter->second[k]; - m_sp[iter->first].push_back(spec->duplMyselfAsSpeciesThermoInterpType()); + shared_ptr spec(iter->second[k]->duplMyselfAsSpeciesThermoInterpType()); + m_sp[iter->first].push_back(spec); } } } @@ -46,14 +46,14 @@ GeneralSpeciesThermo::operator=(const GeneralSpeciesThermo& b) } SpeciesThermo::operator=(b); - clear(); + m_sp.clear(); // Copy SpeciesThermoInterpType objects from 'b' for (STIT_map::const_iterator iter = b.m_sp.begin(); iter != b.m_sp.end(); iter++) { for (size_t k = 0; k < iter->second.size(); k++) { - const SpeciesThermoInterpType* spec = iter->second[k]; - m_sp[iter->first].push_back(spec->duplMyselfAsSpeciesThermoInterpType()); + shared_ptr spec(iter->second[k]->duplMyselfAsSpeciesThermoInterpType()); + m_sp[iter->first].push_back(spec); } } @@ -66,29 +66,12 @@ GeneralSpeciesThermo::operator=(const GeneralSpeciesThermo& b) return *this; } -GeneralSpeciesThermo::~GeneralSpeciesThermo() -{ - clear(); -} - SpeciesThermo* GeneralSpeciesThermo::duplMyselfAsSpeciesThermo() const { return new GeneralSpeciesThermo(*this); } -void GeneralSpeciesThermo::clear() -{ - for (STIT_map::const_iterator iter = m_sp.begin(); - iter != m_sp.end(); - iter++) { - for (size_t k = 0; k < iter->second.size(); k++) { - delete iter->second[k]; - } - } - m_sp.clear(); -} - void GeneralSpeciesThermo::install(const std::string& name, size_t index, int type, @@ -108,14 +91,14 @@ void GeneralSpeciesThermo::install(const std::string& name, /* * Create the necessary object */ - SpeciesThermoInterpType* sp = newSpeciesThermoInterpType(type, - minTemp_, maxTemp_, refPressure_, c); + shared_ptr sp(newSpeciesThermoInterpType(type, + minTemp_, maxTemp_, refPressure_, c)); sp->validate(name); install_STIT(index, sp); } void GeneralSpeciesThermo::install_STIT(size_t index, - SpeciesThermoInterpType* stit_ptr) + shared_ptr stit_ptr) { if (!stit_ptr) { throw CanteraError("GeneralSpeciesThermo::install_STIT", @@ -140,7 +123,7 @@ void GeneralSpeciesThermo::install_STIT(size_t index, void GeneralSpeciesThermo::installPDSShandler(size_t k, PDSS* PDSS_ptr, VPSSMgr* vpssmgr_ptr) { - STITbyPDSS* stit_ptr = new STITbyPDSS(k, vpssmgr_ptr, PDSS_ptr); + shared_ptr stit_ptr(new STITbyPDSS(k, vpssmgr_ptr, PDSS_ptr)); install_STIT(k, stit_ptr); } @@ -159,7 +142,7 @@ void GeneralSpeciesThermo::update(doublereal t, doublereal* cp_R, STIT_map::const_iterator iter = m_sp.begin(); tpoly_map::iterator jter = m_tpoly.begin(); for (; iter != m_sp.end(); iter++, jter++) { - const std::vector& species = iter->second; + const std::vector >& species = iter->second; double* tpoly = &jter->second[0]; species[0]->updateTemperaturePoly(t, tpoly); for (size_t k = 0; k < species.size(); k++) { @@ -232,7 +215,7 @@ SpeciesThermoInterpType* GeneralSpeciesThermo::provideSTIT(size_t k) { try { const std::pair& loc = getValue(m_speciesLoc, k); - return getValue(m_sp, loc.first)[loc.second]; + return getValue(m_sp, loc.first)[loc.second].get(); } catch (std::out_of_range&) { return 0; } @@ -242,7 +225,7 @@ const SpeciesThermoInterpType* GeneralSpeciesThermo::provideSTIT(size_t k) const { try { const std::pair& loc = getValue(m_speciesLoc, k); - return getValue(m_sp, loc.first)[loc.second]; + return getValue(m_sp, loc.first)[loc.second].get(); } catch (std::out_of_range&) { return 0; } diff --git a/src/thermo/LatticeSolidPhase.cpp b/src/thermo/LatticeSolidPhase.cpp index 187eb112b..321565a4f 100644 --- a/src/thermo/LatticeSolidPhase.cpp +++ b/src/thermo/LatticeSolidPhase.cpp @@ -361,7 +361,7 @@ void LatticeSolidPhase::installSlavePhases(Cantera::XML_Node* phaseNode) } addUniqueSpecies(lp->speciesName(k), &ecomp[0], lp->charge(k), lp->size(k)); - SpeciesThermoInterpType* stit = newSpeciesThermoInterpType(*spNode[k]); + shared_ptr stit(newSpeciesThermoInterpType(*spNode[k])); stit->validate(spNode[k]->attrib("name")); m_spthermo->install_STIT(kk, stit); m_speciesData.push_back(new XML_Node(*(spNode[k]))); diff --git a/src/thermo/NasaThermo.h b/src/thermo/NasaThermo.h index 4ad79569e..25852df43 100644 --- a/src/thermo/NasaThermo.h +++ b/src/thermo/NasaThermo.h @@ -82,7 +82,7 @@ public: doublereal min_temp, doublereal max_temp, doublereal ref_pressure); - virtual void install_STIT(size_t index, SpeciesThermoInterpType* stit_ptr) { + virtual void install_STIT(size_t index, shared_ptr stit_ptr) { throw CanteraError("install_STIT", "not implemented"); } diff --git a/src/thermo/ShomateThermo.h b/src/thermo/ShomateThermo.h index f7f911194..3e2a055a7 100644 --- a/src/thermo/ShomateThermo.h +++ b/src/thermo/ShomateThermo.h @@ -204,7 +204,8 @@ public: markInstalled(index); } - virtual void install_STIT(size_t index, SpeciesThermoInterpType* stit_ptr) { + virtual void install_STIT(size_t index, + shared_ptr stit_ptr) { throw CanteraError("install_STIT", "not implemented"); } diff --git a/src/thermo/Species.cpp b/src/thermo/Species.cpp index 54e0482c3..df1b38ae4 100644 --- a/src/thermo/Species.cpp +++ b/src/thermo/Species.cpp @@ -11,7 +11,6 @@ namespace Cantera { Species::Species() : charge(std::numeric_limits::quiet_NaN()) , size(std::numeric_limits::quiet_NaN()) - , thermo_(0) { } @@ -21,13 +20,12 @@ Species::Species(const std::string& name_, const compositionMap& comp_, , composition(comp_) , charge(charge_) , size(size_) - , thermo_(therm) + , thermo(therm) { } Species::~Species() { - delete thermo_; } Species::Species(const Species& other) @@ -37,10 +35,8 @@ Species::Species(const Species& other) , size(other.size) , transport(other.transport) { - if (other.thermo_) { - thermo_ = other.thermo_->duplMyselfAsSpeciesThermoInterpType(); - } else { - thermo_ = 0; + if (other.thermo) { + thermo.reset(other.thermo->duplMyselfAsSpeciesThermoInterpType()); } } @@ -54,24 +50,10 @@ Species& Species::operator=(const Species& other) charge = other.charge; size = other.size; transport = other.transport; - delete thermo_; - if (other.thermo_) { - thermo_ = other.thermo_->duplMyselfAsSpeciesThermoInterpType(); - } else { - thermo_ = 0; + if (other.thermo) { + thermo.reset(other.thermo->duplMyselfAsSpeciesThermoInterpType()); } return *this; } - -SpeciesThermoInterpType& Species::thermo() -{ - if (thermo_) { - return *thermo_; - } else { - throw CanteraError("Species::thermo", - "No thermo for species " + name); - } -} - } diff --git a/src/thermo/SpeciesThermoFactory.cpp b/src/thermo/SpeciesThermoFactory.cpp index d66d7759e..0e8cc9262 100644 --- a/src/thermo/SpeciesThermoFactory.cpp +++ b/src/thermo/SpeciesThermoFactory.cpp @@ -582,7 +582,7 @@ void SpeciesThermoFactory::installThermoForSpecies (size_t k, const XML_Node& speciesNode, ThermoPhase* th_ptr, SpeciesThermo& spthermo, const XML_Node* phaseNode_ptr) const { - SpeciesThermoInterpType* stit = newSpeciesThermoInterpType(speciesNode); + shared_ptr stit(newSpeciesThermoInterpType(speciesNode)); stit->validate(speciesNode["name"]); spthermo.install_STIT(k, stit); } diff --git a/src/thermo/ThermoPhase.cpp b/src/thermo/ThermoPhase.cpp index 1ab8f8fa0..3a63adaa6 100644 --- a/src/thermo/ThermoPhase.cpp +++ b/src/thermo/ThermoPhase.cpp @@ -699,9 +699,8 @@ bool ThermoPhase::addSpecies(const Species& spec) bool added = Phase::addSpecies(spec); if (added) { Species& s = m_species[spec.name]; - s.thermo().validate(spec.name); - m_spthermo->install_STIT(m_kk-1, - s.thermo().duplMyselfAsSpeciesThermoInterpType()); + s.thermo->validate(spec.name); + m_spthermo->install_STIT(m_kk-1, s.thermo); } return added; } diff --git a/src/thermo/VPSSMgr.cpp b/src/thermo/VPSSMgr.cpp index a774ef601..21300d3a1 100644 --- a/src/thermo/VPSSMgr.cpp +++ b/src/thermo/VPSSMgr.cpp @@ -394,7 +394,7 @@ void VPSSMgr::initThermoXML(XML_Node& phaseNode, const std::string& id) void VPSSMgr::installSTSpecies(size_t k, const XML_Node& s, const XML_Node* phaseNode_ptr) { - SpeciesThermoInterpType* stit = newSpeciesThermoInterpType(s); + shared_ptr stit(newSpeciesThermoInterpType(s)); stit->validate(s["name"]); m_spthermo->install_STIT(k, stit); if (m_p0 < 0.0) { diff --git a/src/thermo/VPSSMgr_IdealGas.cpp b/src/thermo/VPSSMgr_IdealGas.cpp index 6425954a5..dbe2206d4 100644 --- a/src/thermo/VPSSMgr_IdealGas.cpp +++ b/src/thermo/VPSSMgr_IdealGas.cpp @@ -103,7 +103,7 @@ VPSSMgr_IdealGas::createInstallPDSS(size_t k, const XML_Node& speciesNode, m_Vss.resize(k+1, 0.0); } - SpeciesThermoInterpType* stit = newSpeciesThermoInterpType(speciesNode); + shared_ptr stit(newSpeciesThermoInterpType(speciesNode)); stit->validate(speciesNode["name"]); m_spthermo->install_STIT(k, stit);