[Thermo] Use shared_ptr to STIT objects in Species and SpeciesThermo

Exclusive ownership of the SpeciesThermoInterpType objects is no longer assumed.
This commit is contained in:
Ray Speth 2015-02-27 18:33:22 -05:00
parent 35dbe37bf7
commit 5195e4f9b8
15 changed files with 41 additions and 82 deletions

View file

@ -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<SpeciesThermoInterpType> 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(); //<! Delete owned SpeciesThermoInterpType objects.
protected:
typedef std::map<int, std::vector<SpeciesThermoInterpType*> > STIT_map;
typedef std::map<int, std::vector<shared_ptr<SpeciesThermoInterpType> > > STIT_map;
typedef std::map<int, std::vector<double> > 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;

View file

@ -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<SpeciesThermoInterpType> stit_ptr) {
throw CanteraError("install_STIT", "not implemented");
}

View file

@ -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<TransportData> transport;
protected:
//! Thermodynamic data for the species
SpeciesThermoInterpType* thermo_;
shared_ptr<SpeciesThermoInterpType> thermo;
};
}

View file

@ -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<SpeciesThermoInterpType> stit) = 0;
//! Compute the reference-state properties for all species.
/*!

View file

@ -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<SpeciesThermoInterpType> stit_ptr) {
throw CanteraError("install_STIT", "not implemented");
}

View file

@ -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<SpeciesThermoInterpType> stit(
newSpeciesThermoInterpType("const_cp", 0.1, 1e30, OneAtm, c));
m_spthermo->install_STIT(0, stit);
initThermo();
m_p0 = OneAtm;

View file

@ -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<SpeciesThermoInterpType> 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<SpeciesThermoInterpType> 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<SpeciesThermoInterpType> 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<SpeciesThermoInterpType> 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<SpeciesThermoInterpType> 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<SpeciesThermoInterpType*>& species = iter->second;
const std::vector<shared_ptr<SpeciesThermoInterpType> >& 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<int, size_t>& 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<int, size_t>& 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;
}

View file

@ -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<SpeciesThermoInterpType> 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])));

View file

@ -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<SpeciesThermoInterpType> stit_ptr) {
throw CanteraError("install_STIT", "not implemented");
}

View file

@ -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<SpeciesThermoInterpType> stit_ptr) {
throw CanteraError("install_STIT", "not implemented");
}

View file

@ -11,7 +11,6 @@ namespace Cantera {
Species::Species()
: charge(std::numeric_limits<double>::quiet_NaN())
, size(std::numeric_limits<double>::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);
}
}
}

View file

@ -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<SpeciesThermoInterpType> stit(newSpeciesThermoInterpType(speciesNode));
stit->validate(speciesNode["name"]);
spthermo.install_STIT(k, stit);
}

View file

@ -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;
}

View file

@ -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<SpeciesThermoInterpType> stit(newSpeciesThermoInterpType(s));
stit->validate(s["name"]);
m_spthermo->install_STIT(k, stit);
if (m_p0 < 0.0) {

View file

@ -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<SpeciesThermoInterpType> stit(newSpeciesThermoInterpType(speciesNode));
stit->validate(speciesNode["name"]);
m_spthermo->install_STIT(k, stit);