Changed parseCompString to simplify its typical usage pattern
This commit is contained in:
parent
fe72446fa0
commit
49dbb605ab
9 changed files with 41 additions and 123 deletions
|
|
@ -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<std::string>& names);
|
||||
|
||||
//! Parse a composition string into individual key:composition
|
||||
//! pairs
|
||||
|
|
|
|||
|
|
@ -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<std::string>& 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
|
||||
|
|
|
|||
|
|
@ -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 (...) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
//====================================================================================================================
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue