Skip to content

Commit

Permalink
Make species names case-preserving instead of case-sensitive
Browse files Browse the repository at this point in the history
This improves interoperability when working with mechanisms which use differing
conventions for naming species using uppercase or lowercase.
  • Loading branch information
speth committed Oct 13, 2016
1 parent deee889 commit f17750e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
6 changes: 3 additions & 3 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_setComposition_singleton(self):
self.assertEqual(list(X[0,:,0]), list(Y))

def test_setCompositionString(self):
self.phase.X = 'H2:1.0, O2:1.0'
self.phase.X = 'h2:1.0, o2:1.0'
X = self.phase.X
self.assertNear(X[0], 0.5)
self.assertNear(X[3], 0.5)
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_setCompositionDict(self):
self.assertNear(Y[3], 0.75)

def test_getCompositionDict(self):
self.phase.X = 'OH:1e-9, O2:0.4, AR:0.6'
self.phase.X = 'oh:1e-9, O2:0.4, AR:0.6'
self.assertEqual(len(self.phase.mole_fraction_dict(1e-7)), 2)
self.assertEqual(len(self.phase.mole_fraction_dict()), 3)

Expand Down Expand Up @@ -203,7 +203,7 @@ def test_setCompositionDict_bad2(self):
self.phase.Y = {'H2':1.0, 'O2':'xx'}

def test_setCompositionSlice(self):
self.phase['H2', 'O2'].X = 0.1, 0.9
self.phase['h2', 'o2'].X = 0.1, 0.9
X = self.phase.X
self.assertNear(X[0], 0.1)
self.assertNear(X[3], 0.9)
Expand Down
6 changes: 2 additions & 4 deletions src/oneD/StFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ StFlow::StFlow(IdealGasPhase* ph, size_t nsp, size_t points) :

// Find indices for radiating species
m_kRadiating.resize(2, npos);
size_t kr = m_thermo->speciesIndex("CO2");
m_kRadiating[0] = (kr != npos) ? kr : m_thermo->speciesIndex("co2");
kr = m_thermo->speciesIndex("H2O");
m_kRadiating[1] = (kr != npos) ? kr : m_thermo->speciesIndex("h2o");
m_kRadiating[0] = m_thermo->speciesIndex("CO2");
m_kRadiating[1] = m_thermo->speciesIndex("H2O");
}

void StFlow::resize(size_t ncomponents, size_t points)
Expand Down
20 changes: 10 additions & 10 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ size_t Phase::speciesIndex(const std::string& nameStr) const
{
if (nameStr.find(':') != npos) {
std::string pn;
std::string sn = parseSpeciesName(nameStr, pn);
std::string sn = lowercase(parseSpeciesName(nameStr, pn));
if (pn == "" || pn == m_name || pn == m_id) {
return getValue(m_speciesIndices, sn, npos);
} else {
return npos;
}
} else {
return getValue(m_speciesIndices, nameStr, npos);
return getValue(m_speciesIndices, lowercase(nameStr), npos);
}
}

Expand Down Expand Up @@ -369,7 +369,7 @@ void Phase::setMoleFractionsByName(const compositionMap& xMap)
vector_fp mf(m_kk, 0.0);
for (const auto& sp : xMap) {
try {
mf[m_speciesIndices.at(sp.first)] = sp.second;
mf[m_speciesIndices.at(lowercase(sp.first))] = sp.second;
} catch (std::out_of_range&) {
throw CanteraError("Phase::setMoleFractionsByName",
"Unknown species '{}'", sp.first);
Expand Down Expand Up @@ -413,7 +413,7 @@ void Phase::setMassFractionsByName(const compositionMap& yMap)
vector_fp mf(m_kk, 0.0);
for (const auto& sp : yMap) {
try {
mf[m_speciesIndices.at(sp.first)] = sp.second;
mf[m_speciesIndices.at(lowercase(sp.first))] = sp.second;
} catch (std::out_of_range&) {
throw CanteraError("Phase::setMassFractionsByName",
"Unknown species '{}'", sp.first);
Expand Down Expand Up @@ -758,12 +758,11 @@ size_t Phase::addElement(const std::string& symbol, doublereal weight,
}

bool Phase::addSpecies(shared_ptr<Species> spec) {
if (m_species.find(spec->name) != m_species.end()) {
if (m_species.find(lowercase(spec->name)) != m_species.end()) {
throw CanteraError("Phase::addSpecies",
"Phase '{}' already contains a species named '{}'.",
m_name, spec->name);
}
m_species[spec->name] = spec;
vector_fp comp(nElements());
for (const auto& elem : spec->composition) {
size_t m = elementIndex(elem.first);
Expand All @@ -789,7 +788,8 @@ bool Phase::addSpecies(shared_ptr<Species> spec) {
}

m_speciesNames.push_back(spec->name);
m_speciesIndices[spec->name] = m_kk;
m_species[lowercase(spec->name)] = spec;
m_speciesIndices[lowercase(spec->name)] = m_kk;
m_speciesCharge.push_back(spec->charge);
m_speciesSize.push_back(spec->size);
size_t ne = nElements();
Expand Down Expand Up @@ -854,19 +854,19 @@ void Phase::modifySpecies(size_t k, shared_ptr<Species> spec)
"New species name '{}' does not match existing name '{}'",
spec->name, speciesName(k));
}
const shared_ptr<Species>& old = m_species[spec->name];
const shared_ptr<Species>& old = m_species[lowercase(spec->name)];
if (spec->composition != old->composition) {
throw CanteraError("Phase::modifySpecies",
"New composition for '{}' does not match existing composition",
spec->name);
}
m_species[spec->name] = spec;
m_species[lowercase(spec->name)] = spec;
invalidateCache();
}

shared_ptr<Species> Phase::species(const std::string& name) const
{
return m_species.at(name);
return m_species.at(lowercase(name));
}

shared_ptr<Species> Phase::species(size_t k) const
Expand Down
2 changes: 1 addition & 1 deletion test/thermo/ThermoPhase_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST_F(TestThermoMethods, getMoleFractionsByName)
EXPECT_DOUBLE_EQ(X["H2"], 0.3);
EXPECT_DOUBLE_EQ(X["AR"], 0.5);

thermo->setMoleFractionsByName("OH:1e-9, O2:0.2, H2:0.3, AR:0.5");
thermo->setMoleFractionsByName("OH:1e-9, O2:0.2, h2:0.3, AR:0.5");
X = thermo->getMoleFractionsByName();
EXPECT_EQ(X.size(), (size_t) 4);

Expand Down
2 changes: 1 addition & 1 deletion test/thermo/phaseConstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ TEST_F(ConstructFromScratch, addUndefinedElements)
ASSERT_EQ((size_t) 4, p.nSpecies());
ASSERT_EQ((size_t) 3, p.nElements());
ASSERT_EQ((size_t) 1, p.nAtoms(p.speciesIndex("CO2"), p.elementIndex("C")));
ASSERT_EQ((size_t) 2, p.nAtoms(p.speciesIndex("CO2"), p.elementIndex("O")));
ASSERT_EQ((size_t) 2, p.nAtoms(p.speciesIndex("co2"), p.elementIndex("O")));
p.setMassFractionsByName("H2:0.5, CO2:0.5");
ASSERT_DOUBLE_EQ(0.5, p.massFraction("CO2"));
}
Expand Down

0 comments on commit f17750e

Please sign in to comment.