Isolate lowercase fallback in speciesIndex
This commit is contained in:
parent
717635d3e6
commit
5e692e893a
4 changed files with 16 additions and 40 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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(<int>kk))
|
||||
s._assign(self.thermo.species(stringify(k)))
|
||||
elif isinstance(k, (int, float)):
|
||||
s._assign(self.thermo.species(<int>k))
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -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<std::string, size_t>::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) {
|
||||
size_t loc = speciesIndex(sp.first);
|
||||
if (loc != npos) {
|
||||
mf[loc] = sp.second;
|
||||
} else {
|
||||
throw CanteraError("Phase::setMassFractionsByName",
|
||||
"Unknown species '{}'", sp.first);
|
||||
} else {
|
||||
mf[loc] = sp.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
setMassFractions(&mf[0]);
|
||||
|
|
@ -846,29 +838,9 @@ void Phase::modifySpecies(size_t k, shared_ptr<Species> spec)
|
|||
|
||||
shared_ptr<Species> Phase::species(const std::string& name) const
|
||||
{
|
||||
std::map<std::string, shared_ptr<Species> >::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<Species> 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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue