From 49dbb605ab81944427766fae0950e5e45eb5859a Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 17 Aug 2012 16:44:34 +0000 Subject: [PATCH] Changed parseCompString to simplify its typical usage pattern --- include/cantera/base/stringUtils.h | 20 +++++++------ src/base/stringUtils.cpp | 48 ++++++++++-------------------- src/clib/ct.cpp | 14 ++------- src/equil/MultiPhase.cpp | 14 ++------- src/fortran/fct.cpp | 14 ++------- src/thermo/MolalityVPSSTP.cpp | 6 +--- src/thermo/Phase.cpp | 18 +++-------- src/thermo/SurfPhase.cpp | 6 +--- src/thermo/ThermoPhase.cpp | 24 ++------------- 9 files changed, 41 insertions(+), 123 deletions(-) diff --git a/include/cantera/base/stringUtils.h b/include/cantera/base/stringUtils.h index 1a1e41e4e..7ccb8d67e 100644 --- a/include/cantera/base/stringUtils.h +++ b/include/cantera/base/stringUtils.h @@ -87,25 +87,27 @@ std::string lowercase(const std::string& s); //! Parse a composition string into a map consisting of individual key:composition //! pairs. /*! - * The composition is a double. - * Example + * Elements present in *names* but not in the composition string will have + * a value of 0. Elements present in the composition string but not in *names* + * will generate an exception. The composition is a double. Example: * * Input is * - * "fire:0 ice:1 snow:2" + * "ice:1 snow:2" + * names = ["fire", "ice", "snow"] * * Output is * x["fire"] = 0 * x["ice"] = 1 * x["snow"] = 2 * - * @param ss original string consisting of multiple key:composition - * pairs on multiple lines - * @param x Output map consisting of a composition - * map, which is a string to double map + * @param ss original string consisting of multiple key:composition + * pairs on multiple lines + * @param names valid names for elements in the composition map + * @return map of names to values */ -void parseCompString(const std::string& ss, Cantera::compositionMap& x); - +compositionMap parseCompString(const std::string& ss, + const std::vector& names); //! Parse a composition string into individual key:composition //! pairs diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index 91cc7d069..493cf59de 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -184,41 +184,25 @@ std::string stripnonprint(const std::string& s) } return ss; } -//================================================================================================ -// Parse a composition string into a map consisting of individual key:composition -// pairs. -/* - * The composition is a double. - * Example - * - * Input is - * - * "fire:0 ice:1 snow:2" - * - * Output is - * x["fire"] = 0 - * x["ice"] = 1 - * x["snow"] = 2 - * - * @param ss original string consisting of multiple key:composition - * pairs on multiple lines - * @param x Output map consisting of a composition - * map, which is a string to double map - */ -void parseCompString(const std::string& ss, Cantera::compositionMap& x) + +compositionMap parseCompString(const std::string& ss, + const std::vector& names) { + compositionMap x; + for (size_t k = 0; k < names.size(); k++) { + x[names[k]] = 0.0; + } std::string s = ss; - std::string::size_type icolon, ibegin, iend; - std::string name, num, nm; + std::string num; do { - ibegin = s.find_first_not_of(", ;\n\t"); + size_t ibegin = s.find_first_not_of(", ;\n\t"); if (ibegin != std::string::npos) { s = s.substr(ibegin,s.size()); - icolon = s.find(':'); - iend = s.find_first_of(", ;\n\t"); + size_t icolon = s.find(':'); + size_t iend = s.find_first_of(", ;\n\t"); //icomma = s.find(','); if (icolon != std::string::npos) { - name = s.substr(0, icolon); + std::string name = stripws(s.substr(0, icolon)); if (iend != std::string::npos) { num = s.substr(icolon+1, iend-icolon); s = s.substr(iend+1, s.size()); @@ -226,17 +210,17 @@ void parseCompString(const std::string& ss, Cantera::compositionMap& x) num = s.substr(icolon+1, s.size()); s = ""; } - nm = stripws(name); - if (x.find(nm) == x.end()) { + if (x.find(name) == x.end()) { throw CanteraError("parseCompString", - "unknown species " + nm); + "unknown species " + name); } - x[nm] = atof(num.c_str()); + x[name] = atof(num.c_str()); } else { s = ""; } } } while (s != ""); + return x; } //================================================================================================ // Parse a composition string into individual key:composition diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 42d6085c3..ffe39b49d 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -225,12 +225,7 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - compositionMap xx; - size_t nsp = p.nSpecies(); - for (size_t n = 0; n < nsp; n++) { - xx[p.speciesName(n)] = -1; - } - parseCompString(x, xx); + compositionMap xx = parseCompString(x, p.speciesNames()); p.setMoleFractionsByName(xx); return 0; } catch (...) { @@ -259,12 +254,7 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - compositionMap yy; - size_t nsp = p.nSpecies(); - for (size_t n = 0; n < nsp; n++) { - yy[p.speciesName(n)] = -1; - } - parseCompString(y, yy); + compositionMap yy = parseCompString(y, p.speciesNames()); p.setMassFractionsByName(yy); return 0; } catch (...) { diff --git a/src/equil/MultiPhase.cpp b/src/equil/MultiPhase.cpp index 3023f0ede..537bbf552 100644 --- a/src/equil/MultiPhase.cpp +++ b/src/equil/MultiPhase.cpp @@ -532,18 +532,8 @@ void MultiPhase::setMolesByName(compositionMap& xMap) // set to zero. void MultiPhase::setMolesByName(const std::string& x) { - compositionMap xx; - - // add an entry in the map for every species, with value -1.0. - // Function parseCompString (stringUtils.cpp) uses the names - // in the map to specify the allowed species. - for (size_t k = 0; k < nSpecies(); k++) { - xx[speciesName(k)] = -1.0; - } - - // build the composition map from the string, and then set the - // moles. - parseCompString(x, xx); + // build the composition map from the string, and then set the moles. + compositionMap xx = parseCompString(x, m_snames); setMolesByName(xx); } //==================================================================================================================== diff --git a/src/fortran/fct.cpp b/src/fortran/fct.cpp index 0969527b3..b4a52e766 100644 --- a/src/fortran/fct.cpp +++ b/src/fortran/fct.cpp @@ -256,12 +256,7 @@ extern "C" { { try { ThermoPhase* p = _fph(n); - compositionMap xx; - int nsp = p->nSpecies(); - for (int nn = 0; nn < nsp; nn++) { - xx[p->speciesName(nn)] = -1; - } - parseCompString(f2string(x, lx), xx); + compositionMap xx = parseCompString(f2string(x, lx), p->speciesNames()); p->setMoleFractionsByName(xx); } catch (...) { return handleAllExceptions(-1, ERR); @@ -288,12 +283,7 @@ extern "C" { { try { ThermoPhase* p = _fph(n); - compositionMap yy; - int nsp = p->nSpecies(); - for (int nn = 0; nn < nsp; nn++) { - yy[p->speciesName(nn)] = -1; - } - parseCompString(f2string(y, leny), yy); + compositionMap yy = parseCompString(f2string(y, leny), p->speciesNames()); p->setMassFractionsByName(yy); } catch (...) { return handleAllExceptions(-1, ERR); diff --git a/src/thermo/MolalityVPSSTP.cpp b/src/thermo/MolalityVPSSTP.cpp index 54adfb043..c7a0fd1a7 100644 --- a/src/thermo/MolalityVPSSTP.cpp +++ b/src/thermo/MolalityVPSSTP.cpp @@ -404,11 +404,7 @@ void MolalityVPSSTP::setMolalitiesByName(compositionMap& mMap) */ void MolalityVPSSTP::setMolalitiesByName(const std::string& x) { - compositionMap xx; - for (size_t k = 0; k < nSpecies(); k++) { - xx[speciesName(k)] = -1.0; - } - parseCompString(x, xx); + compositionMap xx = parseCompString(x, speciesNames()); setMolalitiesByName(xx); } diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 1caa9ae1c..7b215d29a 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -361,13 +361,8 @@ void Phase::setMoleFractionsByName(compositionMap& xMap) void Phase::setMoleFractionsByName(const std::string& x) { - size_t kk = nSpecies(); - compositionMap xx; - for (size_t k = 0; k < kk; k++) { - xx[speciesName(k)] = -1.0; - } - parseCompString(x, xx); - setMoleFractionsByName(xx); + compositionMap c = parseCompString(x, speciesNames()); + setMoleFractionsByName(c); } void Phase::setMassFractions(const doublereal* const y) @@ -411,13 +406,8 @@ void Phase::setMassFractionsByName(compositionMap& yMap) void Phase::setMassFractionsByName(const std::string& y) { - size_t kk = nSpecies(); - compositionMap yy; - for (size_t k = 0; k < kk; k++) { - yy[speciesName(k)] = -1.0; - } - parseCompString(y, yy); - setMassFractionsByName(yy); + compositionMap c = parseCompString(y, speciesNames()); + setMassFractionsByName(c); } void Phase::setState_TRX(doublereal t, doublereal dens, const doublereal* x) diff --git a/src/thermo/SurfPhase.cpp b/src/thermo/SurfPhase.cpp index c52150c2b..9e0e84058 100644 --- a/src/thermo/SurfPhase.cpp +++ b/src/thermo/SurfPhase.cpp @@ -411,11 +411,7 @@ void SurfPhase:: setCoveragesByName(std::string cov) { size_t kk = nSpecies(); - compositionMap cc; - for (size_t k = 0; k < kk; k++) { - cc[speciesName(k)] = -1.0; - } - parseCompString(cov, cc); + compositionMap cc = parseCompString(cov, speciesNames()); doublereal c; vector_fp cv(kk, 0.0); bool ifound = false; diff --git a/src/thermo/ThermoPhase.cpp b/src/thermo/ThermoPhase.cpp index ee8c3422d..13dd461ac 100644 --- a/src/thermo/ThermoPhase.cpp +++ b/src/thermo/ThermoPhase.cpp @@ -188,17 +188,7 @@ void ThermoPhase::setState_TPX(doublereal t, doublereal p, compositionMap& x) //================================================================================================================= void ThermoPhase::setState_TPX(doublereal t, doublereal p, const std::string& x) { - compositionMap xx; - for (size_t k = 0; k < nSpecies(); k++) { - xx[speciesName(k)] = -1.0; - } - try { - parseCompString(x, xx); - } catch (CanteraError& err) { - err.save(); - throw CanteraError("setState_TPX", - "Unknown species in composition map: "+ x); - } + compositionMap xx = parseCompString(x, speciesNames()); setMoleFractionsByName(xx); setTemperature(t); setPressure(p); @@ -223,17 +213,7 @@ void ThermoPhase::setState_TPY(doublereal t, doublereal p, void ThermoPhase::setState_TPY(doublereal t, doublereal p, const std::string& y) { - compositionMap yy; - for (size_t k = 0; k < nSpecies(); k++) { - yy[speciesName(k)] = -1.0; - } - try { - parseCompString(y, yy); - } catch (CanteraError& err) { - err.save(); - throw CanteraError("setState_TPY", - "Unknown species in composition map: "+ y); - } + compositionMap yy = parseCompString(y, speciesNames()); setMassFractionsByName(yy); setTemperature(t); setPressure(p);