From f17750e4838b54e91d5e7fc33f0a950a4a04b48c Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 20 Sep 2016 21:26:45 -0400 Subject: [PATCH] Make species names case-preserving instead of case-sensitive This improves interoperability when working with mechanisms which use differing conventions for naming species using uppercase or lowercase. --- interfaces/cython/cantera/test/test_thermo.py | 6 +++--- src/oneD/StFlow.cpp | 6 ++---- src/thermo/Phase.cpp | 20 +++++++++---------- test/thermo/ThermoPhase_Test.cpp | 2 +- test/thermo/phaseConstructors.cpp | 2 +- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 5e048252e..63fd79580 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -122,7 +122,7 @@ class TestThermoPhase(utilities.CanteraTest): 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) @@ -162,7 +162,7 @@ class TestThermoPhase(utilities.CanteraTest): 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) @@ -203,7 +203,7 @@ class TestThermoPhase(utilities.CanteraTest): 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) diff --git a/src/oneD/StFlow.cpp b/src/oneD/StFlow.cpp index b32fa4b8b..2f11e3f43 100644 --- a/src/oneD/StFlow.cpp +++ b/src/oneD/StFlow.cpp @@ -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) diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 8faf377c9..44f8a0324 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -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); } } @@ -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); @@ -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); @@ -758,12 +758,11 @@ size_t Phase::addElement(const std::string& symbol, doublereal weight, } bool Phase::addSpecies(shared_ptr 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); @@ -789,7 +788,8 @@ bool Phase::addSpecies(shared_ptr 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(); @@ -854,19 +854,19 @@ void Phase::modifySpecies(size_t k, shared_ptr spec) "New species name '{}' does not match existing name '{}'", spec->name, speciesName(k)); } - const shared_ptr& old = m_species[spec->name]; + const shared_ptr& 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 Phase::species(const std::string& name) const { - return m_species.at(name); + return m_species.at(lowercase(name)); } shared_ptr Phase::species(size_t k) const diff --git a/test/thermo/ThermoPhase_Test.cpp b/test/thermo/ThermoPhase_Test.cpp index 0e93d7d2b..72022d45b 100644 --- a/test/thermo/ThermoPhase_Test.cpp +++ b/test/thermo/ThermoPhase_Test.cpp @@ -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); diff --git a/test/thermo/phaseConstructors.cpp b/test/thermo/phaseConstructors.cpp index 6502a4b83..fed866df5 100644 --- a/test/thermo/phaseConstructors.cpp +++ b/test/thermo/phaseConstructors.cpp @@ -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")); }