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
|
# species properties
|
||||||
size_t nSpecies()
|
size_t nSpecies()
|
||||||
|
shared_ptr[CxxSpecies] species(string) except +translate_exception
|
||||||
shared_ptr[CxxSpecies] species(size_t) except +translate_exception
|
shared_ptr[CxxSpecies] species(size_t) except +translate_exception
|
||||||
size_t speciesIndex(string) except +translate_exception
|
size_t speciesIndex(string) except +translate_exception
|
||||||
string speciesName(size_t) except +translate_exception
|
string speciesName(size_t) except +translate_exception
|
||||||
|
|
|
||||||
|
|
@ -1257,6 +1257,10 @@ class TestMisc(utilities.CanteraTest):
|
||||||
gas = ct.Solution('h2o2.xml')
|
gas = ct.Solution('h2o2.xml')
|
||||||
self.assertFalse(gas.case_sensitive_species_names)
|
self.assertFalse(gas.case_sensitive_species_names)
|
||||||
self.assertTrue(gas.species_index('h2') == 0)
|
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
|
gas.case_sensitive_species_names = True
|
||||||
self.assertTrue(gas.case_sensitive_species_names)
|
self.assertTrue(gas.case_sensitive_species_names)
|
||||||
|
|
|
||||||
|
|
@ -490,8 +490,7 @@ cdef class ThermoPhase(_SolutionBase):
|
||||||
|
|
||||||
s = Species(init=False)
|
s = Species(init=False)
|
||||||
if isinstance(k, (str, bytes)):
|
if isinstance(k, (str, bytes)):
|
||||||
kk = self.thermo.speciesIndex(stringify(k))
|
s._assign(self.thermo.species(stringify(k)))
|
||||||
s._assign(self.thermo.species(<int>kk))
|
|
||||||
elif isinstance(k, (int, float)):
|
elif isinstance(k, (int, float)):
|
||||||
s._assign(self.thermo.species(<int>k))
|
s._assign(self.thermo.species(<int>k))
|
||||||
else:
|
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 Phase::speciesIndex(const std::string& nameStr) const
|
||||||
{
|
{
|
||||||
size_t loc = npos;
|
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()) {
|
if (it != m_speciesIndices.end()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
} else if (!m_caseSensitiveSpecies) {
|
} else if (!m_caseSensitiveSpecies) {
|
||||||
|
|
@ -369,19 +368,12 @@ void Phase::setMassFractionsByName(const compositionMap& yMap)
|
||||||
{
|
{
|
||||||
vector_fp mf(m_kk, 0.0);
|
vector_fp mf(m_kk, 0.0);
|
||||||
for (const auto& sp : yMap) {
|
for (const auto& sp : yMap) {
|
||||||
try {
|
size_t loc = speciesIndex(sp.first);
|
||||||
mf[m_speciesIndices.at(sp.first)] = sp.second;
|
if (loc != npos) {
|
||||||
} catch (std::out_of_range&) {
|
mf[loc] = sp.second;
|
||||||
size_t loc = npos;
|
} else {
|
||||||
if (!m_caseSensitiveSpecies) {
|
throw CanteraError("Phase::setMassFractionsByName",
|
||||||
loc = findSpeciesLower(sp.first);
|
"Unknown species '{}'", sp.first);
|
||||||
}
|
|
||||||
if (loc == npos) {
|
|
||||||
throw CanteraError("Phase::setMassFractionsByName",
|
|
||||||
"Unknown species '{}'", sp.first);
|
|
||||||
} else {
|
|
||||||
mf[loc] = sp.second;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setMassFractions(&mf[0]);
|
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
|
shared_ptr<Species> Phase::species(const std::string& name) const
|
||||||
{
|
{
|
||||||
std::map<std::string, shared_ptr<Species> >::const_iterator it;
|
size_t k = speciesIndex(name);
|
||||||
|
if (k != npos) {
|
||||||
it = m_species.find(name);
|
return m_species.at(speciesName(k));
|
||||||
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;
|
|
||||||
} else {
|
} else {
|
||||||
throw CanteraError("Phase::setMassFractionsByName",
|
throw CanteraError("Phase::setMassFractionsByName",
|
||||||
"Unknown species '{}'", name);
|
"Unknown species '{}'", name);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue