From 6c3fc200b6ad27b8886649853d823ef2935ffcab Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 15 Oct 2014 02:03:01 +0000 Subject: [PATCH] Clean up implementation of GeneralSpeciesThermo --- include/cantera/thermo/GeneralSpeciesThermo.h | 40 +++--- src/thermo/GeneralSpeciesThermo.cpp | 118 ++++++------------ 2 files changed, 55 insertions(+), 103 deletions(-) diff --git a/include/cantera/thermo/GeneralSpeciesThermo.h b/include/cantera/thermo/GeneralSpeciesThermo.h index 3411c9cf1..2fb330d77 100644 --- a/include/cantera/thermo/GeneralSpeciesThermo.h +++ b/include/cantera/thermo/GeneralSpeciesThermo.h @@ -132,40 +132,34 @@ private: * @return pointer to the SpeciesThermoInterpType object. */ SpeciesThermoInterpType* provideSTIT(size_t k); - const SpeciesThermoInterpType* provideSTIT(size_t k) const; -protected: - /** - * This is the main unknown in the object. It is - * a list of pointers to type SpeciesThermoInterpType. - * Note, this object owns the objects, so they are deleted - * in the destructor of this object. - * Note, that in some instances, m_sp[k] = 0, e.g., no - * SpeciesThermoInterpType is installed for one or more - * species. These cases must be handled by the calling - * routine. - */ - std::map > m_sp; + void clear(); // > m_tpoly; +protected: + 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; + + //! Temperature polynomials for each thermo parameterization + mutable tpoly_map m_tpoly; std::map > m_speciesLoc; //! Maximum value of the lowest temperature - doublereal m_tlow_max; + doublereal m_tlow_max; //! Minimum value of the highest temperature - doublereal m_thigh_min; + doublereal m_thigh_min; //! reference pressure (Pa) - doublereal m_p0; - - /** - * Internal variable indicating the length of the - * number of species in the phase. - */ - size_t m_kk; + doublereal m_p0; //! Make the class VPSSMgr a friend because we need to access //! the function provideSTIT() diff --git a/src/thermo/GeneralSpeciesThermo.cpp b/src/thermo/GeneralSpeciesThermo.cpp index e8571ce03..95fabcd81 100644 --- a/src/thermo/GeneralSpeciesThermo.cpp +++ b/src/thermo/GeneralSpeciesThermo.cpp @@ -13,11 +13,7 @@ #include "cantera/thermo/ConstCpPoly.h" #include "cantera/thermo/Mu0Poly.h" #include "cantera/thermo/AdsorbateThermo.h" -#include "cantera/thermo/SpeciesThermoFactory.h" #include "cantera/thermo/StatMech.h" -#include - -using namespace std; namespace Cantera { @@ -25,30 +21,21 @@ GeneralSpeciesThermo::GeneralSpeciesThermo() : SpeciesThermo(), m_tlow_max(0.0), m_thigh_min(1.0E30), - m_p0(OneAtm), - m_kk(0) + m_p0(OneAtm) { - m_tlow_max = 0.0; - m_thigh_min = 1.0E30; } GeneralSpeciesThermo::GeneralSpeciesThermo(const GeneralSpeciesThermo& b) : m_tpoly(b.m_tpoly), m_tlow_max(b.m_tlow_max), m_thigh_min(b.m_thigh_min), - m_kk(b.m_kk) + m_p0(b.m_p0) { - // Delete current SpeciesThermoInterpType objects - std::map >::const_iterator iter; - for (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(); - + clear(); // Copy SpeciesThermoInterpTypes from 'b' - for (iter = b.m_sp.begin(); iter != b.m_sp.end(); iter++) { + 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()); @@ -62,18 +49,11 @@ GeneralSpeciesThermo::operator=(const GeneralSpeciesThermo& b) if (&b == this) { return *this; } - - // Delete current SpeciesThermoInterpType objects - std::map >::const_iterator iter; - for (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(); - + clear(); // Copy SpeciesThermoInterpType objects from 'b' - for (iter = b.m_sp.begin(); iter != b.m_sp.end(); iter++) { + 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()); @@ -83,7 +63,6 @@ GeneralSpeciesThermo::operator=(const GeneralSpeciesThermo& b) m_tpoly = b.m_tpoly; m_tlow_max = b.m_tlow_max; m_thigh_min = b.m_thigh_min; - m_kk = b.m_kk; m_p0 = b.m_p0; return *this; @@ -91,14 +70,7 @@ GeneralSpeciesThermo::operator=(const GeneralSpeciesThermo& b) GeneralSpeciesThermo::~GeneralSpeciesThermo() { - for (std::map >::iterator iter=m_sp.begin(); - iter != m_sp.end(); - iter++) { - std::vector& sp = iter->second; - for (size_t k = 0; k < sp.size(); k++) { - delete sp[k]; - } - } + clear(); } SpeciesThermo* @@ -107,6 +79,18 @@ 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, @@ -115,25 +99,15 @@ void GeneralSpeciesThermo::install(const std::string& name, doublereal maxTemp_, doublereal refPressure_) { - /* - * Resize the arrays if necessary, filling the empty - * slots with the zero pointer. - */ - if (minTemp_ <= 0.0) { - throw CanteraError("Error in GeneralSpeciesThermo.cpp", - " Cannot take 0 tmin as input. \n\n"); - } - - if (index >= m_kk) { - m_kk = index+1; + throw CanteraError("GeneralSpeciesThermo::install", + "T_min must be positive"); } /* * Create the necessary object */ - - SpeciesThermoInterpType* sp; + SpeciesThermoInterpType* sp; switch (type) { case NASA1: sp = new NasaPoly1(index, minTemp_, maxTemp_, refPressure_, c); @@ -162,9 +136,8 @@ void GeneralSpeciesThermo::install(const std::string& name, sp = new Adsorbate(index, minTemp_, maxTemp_, refPressure_, c); break; default: - throw UnknownSpeciesThermoModel( - "GeneralSpeciesThermo::install", - "unknown species type", int2str(type)); + throw CanteraError("GeneralSpeciesThermo::install", + "unknown species type: " + int2str(type)); } install_STIT(sp); @@ -172,33 +145,24 @@ void GeneralSpeciesThermo::install(const std::string& name, void GeneralSpeciesThermo::install_STIT(SpeciesThermoInterpType* stit_ptr) { - /* - * Resize the arrays if necessary, filling the empty - * slots with the zero pointer. - */ if (!stit_ptr) { throw CanteraError("GeneralSpeciesThermo::install_STIT", "zero pointer"); } size_t index = stit_ptr->speciesIndex(); - if (index >= m_kk) { - m_kk = index+1; - } AssertThrow(m_speciesLoc.find(index) == m_speciesLoc.end(), "Index position isn't null, duplication of assignment: " + int2str(index)); int type = stit_ptr->reportType(); - m_speciesLoc[index] = make_pair(type, m_sp[type].size()); + m_speciesLoc[index] = std::make_pair(type, m_sp[type].size()); m_sp[type].push_back(stit_ptr); if (m_sp[type].size() == 1) { m_tpoly[type].resize(stit_ptr->temperaturePolySize()); } - /* - * Calculate max and min - */ - m_tlow_max = max(stit_ptr->minTemp(), m_tlow_max); - m_thigh_min = min(stit_ptr->maxTemp(), m_thigh_min); + // Calculate max and min T + m_tlow_max = std::max(stit_ptr->minTemp(), m_tlow_max); + m_thigh_min = std::min(stit_ptr->maxTemp(), m_thigh_min); markInstalled(index); } @@ -221,10 +185,10 @@ void GeneralSpeciesThermo::update_one(size_t k, doublereal t, doublereal* cp_R, void GeneralSpeciesThermo::update(doublereal t, doublereal* cp_R, doublereal* h_RT, doublereal* s_R) const { - map >::const_iterator iter = m_sp.begin(); - map >::iterator jter = m_tpoly.begin(); + STIT_map::const_iterator iter = m_sp.begin(); + tpoly_map::iterator jter = m_tpoly.begin(); for (; iter != m_sp.end(); iter++, jter++) { - const 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++) { @@ -262,9 +226,7 @@ void GeneralSpeciesThermo::reportParams(size_t index, int& type, doublereal GeneralSpeciesThermo::minTemp(size_t k) const { - if (k == npos) { - return m_tlow_max; - } else { + if (k != npos) { const SpeciesThermoInterpType* sp = provideSTIT(k); if (sp) { return sp->minTemp(); @@ -275,9 +237,7 @@ doublereal GeneralSpeciesThermo::minTemp(size_t k) const doublereal GeneralSpeciesThermo::maxTemp(size_t k) const { - if (k == npos) { - return m_thigh_min; - } else { + if (k != npos) { const SpeciesThermoInterpType* sp = provideSTIT(k); if (sp) { return sp->maxTemp(); @@ -288,9 +248,7 @@ doublereal GeneralSpeciesThermo::maxTemp(size_t k) const doublereal GeneralSpeciesThermo::refPressure(size_t k) const { - if (k == npos) { - return m_p0; - } else { + if (k != npos) { const SpeciesThermoInterpType* sp = provideSTIT(k); if (sp) { return sp->refPressure();