From 43952b30fcc0a9cee089146f7b3622aaca20066c Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 3 Mar 2015 19:36:26 -0500 Subject: [PATCH] [Thermo] Use shared_ptr in ThermoPhase --- include/cantera/thermo/Phase.h | 6 ++--- include/cantera/thermo/ThermoPhase.h | 2 +- src/thermo/Phase.cpp | 31 +++++++++++++------------ src/thermo/ThermoFactory.cpp | 8 +++---- src/thermo/ThermoPhase.cpp | 7 +++--- src/transport/GasTransport.cpp | 4 ++-- test/thermo/phaseConstructors.cpp | 26 ++++++++++----------- test/thermo/thermoParameterizations.cpp | 30 ++++++++++++++++-------- test/transport/transportFromScratch.cpp | 20 ++++++++-------- 9 files changed, 72 insertions(+), 62 deletions(-) diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index 8575ff662..8a8009e49 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -761,7 +761,7 @@ public: //! Add a Species to this Phase. Returns `true` if the species was //! successfully added, or `false` if the species was ignored. //! @see ignoreUndefinedElements addUndefinedElements throwUndefinedElements - virtual bool addSpecies(const Species& spec); + virtual bool addSpecies(shared_ptr& spec); void addSpecies(const std::string& name, const doublereal* comp, doublereal charge = 0.0, doublereal size = 1.0); @@ -779,7 +779,7 @@ public: doublereal size = 1.0); //! Return the Species object for the named species. - const Species& species(const std::string& name) const; + shared_ptr species(const std::string& name) const; //! Set behavior when adding a species containing undefined elements to just //! skip the species. @@ -843,7 +843,7 @@ protected: vector_fp m_speciesCharge; //!< Vector of species charges. length m_kk. - std::map m_species; + std::map > m_species; //! Flag determining behavior when adding species with an undefined element UndefElement::behavior m_undefinedElementBehavior; diff --git a/include/cantera/thermo/ThermoPhase.h b/include/cantera/thermo/ThermoPhase.h index 7d0824951..cb538b115 100644 --- a/include/cantera/thermo/ThermoPhase.h +++ b/include/cantera/thermo/ThermoPhase.h @@ -1310,7 +1310,7 @@ public: //@{ using Phase::addSpecies; - virtual bool addSpecies(const Species& spec); + virtual bool addSpecies(shared_ptr& spec); //! Store a reference pointer to the XML tree containing the species data //! for this phase. diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index b15257542..390306a4e 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -839,11 +839,11 @@ size_t Phase::addUniqueElementAfterFreeze(const std::string& symbol, return addElement(symbol, weight, atomicNumber, entropy298, elem_type); } -bool Phase::addSpecies(const Species& spec) { - m_species[spec.name] = spec; +bool Phase::addSpecies(shared_ptr& spec) { + m_species[spec->name] = spec; vector_fp comp(nElements()); - for (map::const_iterator iter = spec.composition.begin(); - iter != spec.composition.end(); + for (map::const_iterator iter = spec->composition.begin(); + iter != spec->composition.end(); iter++) { size_t m = elementIndex(iter->first); if (m == npos) { // Element doesn't exist in this phase @@ -860,33 +860,33 @@ bool Phase::addSpecies(const Species& spec) { case UndefElement::error: default: throw CanteraError("Phase::addSpecies", - "Species '" + spec.name + "' contains an " + "Species '" + spec->name + "' contains an " "undefined element '" + iter->first + "'."); } } comp[m] = iter->second; } - m_speciesNames.push_back(spec.name); - m_speciesCharge.push_back(spec.charge); - m_speciesSize.push_back(spec.size); + m_speciesNames.push_back(spec->name); + m_speciesCharge.push_back(spec->charge); + m_speciesSize.push_back(spec->size); size_t ne = nElements(); double wt = 0.0; const vector_fp& aw = atomicWeights(); - if (spec.charge != 0.0) { + if (spec->charge != 0.0) { size_t eindex = elementIndex("E"); if (eindex != npos) { doublereal ecomp = comp[eindex]; - if (fabs(spec.charge + ecomp) > 0.001) { + if (fabs(spec->charge + ecomp) > 0.001) { if (ecomp != 0.0) { throw CanteraError("Phase::addSpecies", "Input charge and element E compositions differ " - "for species " + spec.name); + "for species " + spec->name); } else { // Just fix up the element E composition based on the input // species charge - comp[eindex] = -spec.charge; + comp[eindex] = -spec->charge; } } } else { @@ -894,7 +894,7 @@ bool Phase::addSpecies(const Species& spec) { ne = nElements(); eindex = elementIndex("E"); comp.resize(ne); - comp[ne - 1] = - spec.charge; + comp[ne - 1] = - spec->charge; } } for (size_t m = 0; m < ne; m++) { @@ -933,7 +933,8 @@ void Phase::addSpecies(const std::string& name_, const doublereal* comp, cmap[elementName(i)] = comp[i]; } } - Phase::addSpecies(Species(name_, cmap, 0, charge_, size_)); + shared_ptr sp(new Species(name_, cmap, 0, charge_, size_)); + Phase::addSpecies(sp); } void Phase::addUniqueSpecies(const std::string& name_, const doublereal* comp, @@ -965,7 +966,7 @@ void Phase::addUniqueSpecies(const std::string& name_, const doublereal* comp, addSpecies(name_, comp, charge_, size_); } -const Species& Phase::species(const std::string& name) const +shared_ptr Phase::species(const std::string& name) const { return getValue(m_species, name); } diff --git a/src/thermo/ThermoFactory.cpp b/src/thermo/ThermoFactory.cpp index 8a7685aeb..4af76eda7 100644 --- a/src/thermo/ThermoFactory.cpp +++ b/src/thermo/ThermoFactory.cpp @@ -621,7 +621,7 @@ bool installSpecies(size_t k, const XML_Node& s, thermo_t& th, vp_ptr->createInstallPDSS(k, s, phaseNode_ptr); } else { SpeciesThermoInterpType* st = newSpeciesThermoInterpType(s); - Species sp(s["name"], comp_map, st, chrg, sz); + shared_ptr sp(new Species(s["name"], comp_map, st, chrg, sz)); // Read gas-phase transport data, if provided if (s.hasChild("transport") && @@ -646,10 +646,10 @@ bool installSpecies(size_t k, const XML_Node& s, thermo_t& th, getOptionalFloat(tr, "acentric_factor", acentric); GasTransportData* gastran = new GasTransportData; - gastran->setCustomaryUnits(sp.name, geometry, diam, welldepth, + gastran->setCustomaryUnits(sp->name, geometry, diam, welldepth, dipole, polar, rot, acentric); - sp.transport.reset(gastran); - gastran->validate(sp); + sp->transport.reset(gastran); + gastran->validate(*sp); } th.addSpecies(sp); } diff --git a/src/thermo/ThermoPhase.cpp b/src/thermo/ThermoPhase.cpp index 3a63adaa6..2fd4911e6 100644 --- a/src/thermo/ThermoPhase.cpp +++ b/src/thermo/ThermoPhase.cpp @@ -694,13 +694,12 @@ void ThermoPhase::installSlavePhases(Cantera::XML_Node* phaseNode) { } -bool ThermoPhase::addSpecies(const Species& spec) +bool ThermoPhase::addSpecies(shared_ptr& 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); + spec->thermo->validate(spec->name); + m_spthermo->install_STIT(m_kk-1, spec->thermo); } return added; } diff --git a/src/transport/GasTransport.cpp b/src/transport/GasTransport.cpp index e72639392..74f4e9bf9 100644 --- a/src/transport/GasTransport.cpp +++ b/src/transport/GasTransport.cpp @@ -464,9 +464,9 @@ void GasTransport::setupMM() void GasTransport::getTransportData() { for (size_t k = 0; k < m_thermo->nSpecies(); k++) { - const Species& s = m_thermo->species(m_thermo->speciesName(k)); + shared_ptr s = m_thermo->species(m_thermo->speciesName(k)); const GasTransportData& sptran = - dynamic_cast(*s.transport.get()); + dynamic_cast(*s->transport.get()); if (sptran.geometry == "atom") { m_crot[k] = 0.0; } else if (sptran.geometry == "linear") { diff --git a/test/thermo/phaseConstructors.cpp b/test/thermo/phaseConstructors.cpp index 1fef8588e..86bac4be4 100644 --- a/test/thermo/phaseConstructors.cpp +++ b/test/thermo/phaseConstructors.cpp @@ -105,23 +105,23 @@ class ConstructFromScratch : public testing::Test { public: ConstructFromScratch() - : sH2O("H2O", parseCompString("H:2 O:1"), - new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs)) - , sH2("H2", parseCompString("H:2"), - new NasaPoly2(200, 3500, 101325, h2_nasa_coeffs)) - , sO2("O2", parseCompString("O:2"), - new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs)) - , sOH("OH", parseCompString("H:1 O:1"), - new NasaPoly2(200, 3500, 101325, oh_nasa_coeffs)) - , sCO("CO", parseCompString("C:1 O:1"), - new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs)) - , sCO2("CO2", parseCompString("C:1 O:2"), - new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs)) + : sH2O(new Species("H2O", parseCompString("H:2 O:1"), + new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs))) + , sH2(new Species("H2", parseCompString("H:2"), + new NasaPoly2(200, 3500, 101325, h2_nasa_coeffs))) + , sO2(new Species("O2", parseCompString("O:2"), + new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs))) + , sOH(new Species("OH", parseCompString("H:1 O:1"), + new NasaPoly2(200, 3500, 101325, oh_nasa_coeffs))) + , sCO(new Species("CO", parseCompString("C:1 O:1"), + new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs))) + , sCO2(new Species("CO2", parseCompString("C:1 O:2"), + new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs))) { } IdealGasPhase p; - Species sH2O, sH2, sO2, sOH, sCO, sCO2; + shared_ptr sH2O, sH2, sO2, sOH, sCO, sCO2; }; TEST_F(ConstructFromScratch, AddElements) diff --git a/test/thermo/thermoParameterizations.cpp b/test/thermo/thermoParameterizations.cpp index 263a8b704..3b20877cd 100644 --- a/test/thermo/thermoParameterizations.cpp +++ b/test/thermo/thermoParameterizations.cpp @@ -35,9 +35,12 @@ TEST_F(SpeciesThermoInterpTypeTest, install_const_cp) SpeciesThermoInterpType* stit_o2 = new ConstCpPoly(200, 5000, 101325, c_o2); SpeciesThermoInterpType* stit_h2 = new ConstCpPoly(200, 5000, 101325, c_h2); SpeciesThermoInterpType* stit_h2o = new ConstCpPoly(200, 5000, 101325, c_h2o); - p.addSpecies(Species("O2", parseCompString("O:2"), stit_o2)); - p.addSpecies(Species("H2", parseCompString("H:2"), stit_h2)); - p.addSpecies(Species("H2O", parseCompString("H:2 O:1"), stit_h2o)); + shared_ptr sO2(new Species("O2", parseCompString("O:2"), stit_o2)); + shared_ptr sH2(new Species("H2", parseCompString("H:2"), stit_h2)); + shared_ptr sH2O(new Species("H2O", parseCompString("H:2 O:1"), stit_h2o)); + p.addSpecies(sO2); + p.addSpecies(sH2); + p.addSpecies(sH2O); p.initThermo(); p2.setState_TPX(298.15, 101325, "H2:0.2, O2:0.7, H2O:0.1"); p.setState_TPX(298.15, 101325, "H2:0.2, O2:0.7, H2O:0.1"); @@ -53,9 +56,11 @@ TEST_F(SpeciesThermoInterpTypeTest, DISABLED_install_bad_pref) // pressure consistency. SpeciesThermoInterpType* stit_o2 = new ConstCpPoly(200, 5000, 101325, c_o2); SpeciesThermoInterpType* stit_h2 = new ConstCpPoly(200, 5000, 100000, c_h2); - p.addSpecies(Species("O2", parseCompString("O:2"), stit_o2)); + shared_ptr sO2(new Species("O2", parseCompString("O:2"), stit_o2)); + shared_ptr sH2(new Species("H2", parseCompString("H:2"), stit_h2)); + p.addSpecies(sO2); // Pref does not match - ASSERT_THROW(p.addSpecies(Species("H2", parseCompString("H:2"), stit_h2)), CanteraError); + ASSERT_THROW(p.addSpecies(sH2), CanteraError); delete stit_h2; } @@ -66,9 +71,12 @@ TEST_F(SpeciesThermoInterpTypeTest, install_nasa) SpeciesThermoInterpType* stit_o2 = new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs); SpeciesThermoInterpType* stit_h2 = new NasaPoly2(200, 3500, 101325, h2_nasa_coeffs); SpeciesThermoInterpType* stit_h2o = new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs); - p.addSpecies(Species("O2", parseCompString("O:2"), stit_o2)); - p.addSpecies(Species("H2", parseCompString("H:2"), stit_h2)); - p.addSpecies(Species("H2O", parseCompString("H:2 O:1"), stit_h2o)); + shared_ptr sO2(new Species("O2", parseCompString("O:2"), stit_o2)); + shared_ptr sH2(new Species("H2", parseCompString("H:2"), stit_h2)); + shared_ptr sH2O(new Species("H2O", parseCompString("H:2 O:1"), stit_h2o)); + p.addSpecies(sO2); + p.addSpecies(sH2); + p.addSpecies(sH2O); p.initThermo(); p2.setState_TPX(900, 101325, "H2:0.2, O2:0.7, H2O:0.1"); p.setState_TPX(900, 101325, "H2:0.2, O2:0.7, H2O:0.1"); @@ -84,8 +92,10 @@ TEST_F(SpeciesThermoInterpTypeTest, install_shomate) IdealGasPhase p2("../data/simplephases.cti", "shomate1"); SpeciesThermoInterpType* stit_co = new ShomatePoly2(200, 6000, 101325, co_shomate_coeffs); SpeciesThermoInterpType* stit_co2 = new ShomatePoly2(200, 6000, 101325, co2_shomate_coeffs); - p.addSpecies(Species("CO", parseCompString("C:1 O:1"), stit_co)); - p.addSpecies(Species("CO2", parseCompString("C:1 O:2"), stit_co2)); + shared_ptr sCO(new Species("CO", parseCompString("C:1 O:1"), stit_co)); + shared_ptr sCO2(new Species("CO2", parseCompString("C:1 O:2"), stit_co2)); + p.addSpecies(sCO); + p.addSpecies(sCO2); p.initThermo(); p2.setState_TPX(900, 101325, "CO:0.2, CO2:0.8"); p.setState_TPX(900, 101325, "CO:0.2, CO2:0.8"); diff --git a/test/transport/transportFromScratch.cpp b/test/transport/transportFromScratch.cpp index bebadfd94..ac01d74d1 100644 --- a/test/transport/transportFromScratch.cpp +++ b/test/transport/transportFromScratch.cpp @@ -18,12 +18,12 @@ class TransportFromScratch : public testing::Test { public: TransportFromScratch() - : sH2("H2", parseCompString("H:2"), - new NasaPoly2(200, 3500, 101325, h2_nasa_coeffs)) - , sO2("O2", parseCompString("O:2"), - new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs)) - , sH2O("H2O", parseCompString("H:2 O:1"), - new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs)) + : sH2(new Species("H2", parseCompString("H:2"), + new NasaPoly2(200, 3500, 101325, h2_nasa_coeffs))) + , sO2(new Species("O2", parseCompString("O:2"), + new NasaPoly2(200, 3500, 101325, o2_nasa_coeffs))) + , sH2O(new Species("H2O", parseCompString("H:2 O:1"), + new NasaPoly2(200, 3500, 101325, h2o_nasa_coeffs))) , tH2(new GasTransportData()) , tO2(new GasTransportData()) , tH2O(new GasTransportData()) @@ -32,9 +32,9 @@ public: tO2->setCustomaryUnits("O2", "linear", 3.46, 107.40, 0.0, 1.60, 3.80); tH2O->setCustomaryUnits("H2O", "nonlinear", 2.60, 572.4, 1.84, 0.0, 4.00); - sH2.transport = tH2; - sO2.transport = tO2; - sH2O.transport = tH2O; + sH2->transport = tH2; + sO2->transport = tO2; + sH2O->transport = tH2O; std::string phase_def = "ideal_gas(name='test', elements='O H'," "species='gri30: H2 O2 H2O')"; @@ -54,7 +54,7 @@ public: test->setState_TPX(400, 5e5, "H2:0.5, O2:0.3, H2O:0.2"); } - Species sH2, sO2, sH2O; + shared_ptr sH2, sO2, sH2O; shared_ptr tH2, tO2, tH2O; shared_ptr ref; shared_ptr test;