diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index af5f9c147..a37104633 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -165,6 +165,7 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera": # species properties size_t nSpecies() + shared_ptr[CxxSpecies] species(string) except +translate_exception shared_ptr[CxxSpecies] species(size_t) except +translate_exception size_t speciesIndex(string) except +translate_exception string speciesName(size_t) except +translate_exception diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index d2fddd4fb..8ce4d846e 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1257,6 +1257,10 @@ class TestMisc(utilities.CanteraTest): gas = ct.Solution('h2o2.xml') self.assertFalse(gas.case_sensitive_species_names) self.assertTrue(gas.species_index('h2') == 0) + gas.X = 'h2:.5, o2:.5' + self.assertNear(gas.X[0], 0.5) + gas.Y = 'h2:.5, o2:.5' + self.assertNear(gas.Y[0], 0.5) gas.case_sensitive_species_names = True self.assertTrue(gas.case_sensitive_species_names) diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index f54d6b332..4980b69ec 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -490,8 +490,7 @@ cdef class ThermoPhase(_SolutionBase): s = Species(init=False) if isinstance(k, (str, bytes)): - kk = self.thermo.speciesIndex(stringify(k)) - s._assign(self.thermo.species(kk)) + s._assign(self.thermo.species(stringify(k))) elif isinstance(k, (int, float)): s._assign(self.thermo.species(k)) else: diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 62fe85cd9..a457c7ccb 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -195,9 +195,8 @@ size_t Phase::findSpeciesLower(const std::string& name) const size_t Phase::speciesIndex(const std::string& nameStr) const { size_t loc = npos; - std::map::const_iterator it; - it = m_speciesIndices.find(nameStr); + auto it = m_speciesIndices.find(nameStr); if (it != m_speciesIndices.end()) { return it->second; } else if (!m_caseSensitiveSpecies) { @@ -369,19 +368,12 @@ 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; - } catch (std::out_of_range&) { - size_t loc = npos; - if (!m_caseSensitiveSpecies) { - loc = findSpeciesLower(sp.first); - } - if (loc == npos) { - throw CanteraError("Phase::setMassFractionsByName", - "Unknown species '{}'", sp.first); - } else { - mf[loc] = sp.second; - } + size_t loc = speciesIndex(sp.first); + if (loc != npos) { + mf[loc] = sp.second; + } else { + throw CanteraError("Phase::setMassFractionsByName", + "Unknown species '{}'", sp.first); } } setMassFractions(&mf[0]); @@ -846,29 +838,9 @@ void Phase::modifySpecies(size_t k, shared_ptr spec) shared_ptr Phase::species(const std::string& name) const { - std::map >::const_iterator it; - - it = m_species.find(name); - if (it != m_species.end()) { - return it->second; - } else if (!m_caseSensitiveSpecies) { - std::string nLower = toLowerCopy(name); - bool found = false; - shared_ptr sp; - for (const auto& k : m_species) { - if (toLowerCopy(k.first) == nLower) { - if (!found) { - found = true; - sp = k.second; - } else { - throw CanteraError("Phase::species", - "Lowercase species name '{}' is not unique. " - "Set Phase::caseSensitiveSpecies to true to " - "enforce case sensitive species names", nLower); - } - } - } - return sp; + size_t k = speciesIndex(name); + if (k != npos) { + return m_species.at(speciesName(k)); } else { throw CanteraError("Phase::setMassFractionsByName", "Unknown species '{}'", name);