From 5fff5ce99f18c4dc4fd3bf42a45a705b79d2bfd1 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 4 Oct 2013 16:22:35 +0000 Subject: [PATCH] [Thermo] Slightly simplify adding species Eliminate the freezeSpecies and init methods of class Phase, instead adjusting array sizes as new species are added. --- include/cantera/thermo/Phase.h | 20 ----------- src/thermo/FixedChemPotSSTP.cpp | 1 - src/thermo/Phase.cpp | 62 ++++++++++----------------------- src/thermo/ThermoFactory.cpp | 6 ++-- 4 files changed, 21 insertions(+), 68 deletions(-) diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index b8d41e1d5..4e86780a3 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -682,15 +682,6 @@ public: doublereal size = 1.0); //!@} end group adding species and elements - //! Call when finished adding species. - //! Prepare to use them for calculation of mixture properties. - virtual void freezeSpecies(); - - //! True if freezeSpecies has been called. - bool speciesFrozen() { - return m_speciesFrozen; - } - virtual bool ready() const; //! Return the State Mole Fraction Number @@ -699,12 +690,6 @@ public: } protected: - //! @internal Initialize. - //! Make a local copy of the vector of molecular weights, and resize the - //! composition arrays to the appropriate size. - //! @param mw Vector of molecular weights of the species. - void init(const vector_fp& mw); - //! Set the molecular weight of a single species to a given value //! @param k id of the species //! @param mw Molecular Weight (kg kmol-1) @@ -765,11 +750,6 @@ private: //! this int is incremented. int m_stateNum; - //! Boolean indicating whether the number of species has been frozen. - //! During the construction of the phase, this is false. After construction - //! of the the phase, this is true. - bool m_speciesFrozen; - //! If this is true, then no elements may be added to the object. bool m_elementsFrozen; diff --git a/src/thermo/FixedChemPotSSTP.cpp b/src/thermo/FixedChemPotSSTP.cpp index 9f6076f77..2365ee570 100644 --- a/src/thermo/FixedChemPotSSTP.cpp +++ b/src/thermo/FixedChemPotSSTP.cpp @@ -99,7 +99,6 @@ FixedChemPotSSTP::FixedChemPotSSTP(const std::string& Ename, doublereal val) : c[2] = 0.0; c[3] = 0.0; m_spthermo->install(pname, 0, SIMPLE, c, 0.0, 1.0E30, OneAtm); - freezeSpecies(); initThermo(); m_p0 = OneAtm; m_tlast = 298.15; diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 43c79d546..a8a669ebc 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -25,7 +25,6 @@ Phase::Phase() : m_dens(0.001), m_mmw(0.0), m_stateNum(-1), - m_speciesFrozen(false), m_elementsFrozen(false), m_mm(0), m_elem_type(0) @@ -42,7 +41,6 @@ Phase::Phase(const Phase& right) : m_dens(0.001), m_mmw(0.0), m_stateNum(-1), - m_speciesFrozen(false) , m_elementsFrozen(false), m_mm(0), m_elem_type(0) @@ -70,7 +68,6 @@ Phase& Phase::operator=(const Phase& right) m_rmolwts = right.m_rmolwts; m_stateNum = -1; - m_speciesFrozen = right.m_speciesFrozen; m_speciesNames = right.m_speciesNames; m_speciesComp = right.m_speciesComp; m_speciesCharge = right.m_speciesCharge; @@ -877,8 +874,26 @@ void Phase::addSpecies(const std::string& name_, const doublereal* comp, m_speciesComp.push_back(compNew[m]); wt += compNew[m] * aw[m]; } + + // Some surface phases may define species representing empty sites + // that have zero molecular weight. Give them a very small molecular + // weight to avoid dividing by zero. + wt = std::max(wt, Tiny); m_molwts.push_back(wt); + m_rmolwts.push_back(1.0/wt); m_kk++; + + // Ensure that the Phase has a valid mass fraction vector that sums to + // one. We will assume that species 0 has a mass fraction of 1.0 and mass + // fraction of all other species is 0.0. + if (m_kk == 1) { + m_y.push_back(1.0); + m_ym.push_back(m_rmolwts[0]); + m_mmw = 1.0 / m_ym[0]; + } else { + m_y.push_back(0.0); + m_ym.push_back(0.0); + } } void Phase::addUniqueSpecies(const std::string& name_, const doublereal* comp, @@ -910,48 +925,9 @@ void Phase::addUniqueSpecies(const std::string& name_, const doublereal* comp, addSpecies(name_, comp, charge_, size_); } -void Phase::freezeSpecies() -{ - m_speciesFrozen = true; - init(molecularWeights()); -} - -void Phase::init(const vector_fp& mw) -{ - m_kk = mw.size(); - m_rmolwts.resize(m_kk); - m_y.resize(m_kk, 0.0); - m_ym.resize(m_kk, 0.0); - copy(mw.begin(), mw.end(), m_molwts.begin()); - for (size_t k = 0; k < m_kk; k++) { - if (m_molwts[k] < 0.0) { - throw CanteraError("Phase::init", - "negative molecular weight for species number " - + int2str(k)); - } - - // Some surface phases may define species representing empty sites - // that have zero molecular weight. Give them a very small molecular - // weight to avoid dividing by zero. - if (m_molwts[k] < Tiny) { - m_molwts[k] = Tiny; - } - m_rmolwts[k] = 1.0/m_molwts[k]; - } - - // Now that we have resized the State object, let's fill it with a valid - // mass fraction vector that sums to one. The Phase object should never - // have a mass fraction vector that doesn't sum to one. We will assume that - // species 0 has a mass fraction of 1.0 and mass fraction of all other - // species is 0.0. - m_y[0] = 1.0; - m_ym[0] = m_y[0] * m_rmolwts[0]; - m_mmw = 1.0 / m_ym[0]; -} - bool Phase::ready() const { - return (m_kk > 0 && m_elementsFrozen && m_speciesFrozen); + return (m_kk > 0 && m_elementsFrozen); } } // namespace Cantera diff --git a/src/thermo/ThermoFactory.cpp b/src/thermo/ThermoFactory.cpp index 4cc46ea3d..a62d02a3e 100644 --- a/src/thermo/ThermoFactory.cpp +++ b/src/thermo/ThermoFactory.cpp @@ -602,10 +602,8 @@ bool importPhase(XML_Node& phase, ThermoPhase* th, th->installSlavePhases(&phase); } - // done adding species. - th->freezeSpecies(); - - // Perform any required subclass-specific initialization. + // Done adding species. Perform any required subclass-specific + // initialization. th->initThermo(); // Perform any required subclass-specific initialization