[Thermo] add functions findIsomer/addSpeciesAlias

This commit is contained in:
Ingmar Schoegl 2019-09-24 14:01:22 -05:00 committed by Ray Speth
parent a85396ef11
commit 3ef99f966c
4 changed files with 60 additions and 0 deletions

View file

@ -703,6 +703,19 @@ public:
*/
virtual void modifySpecies(size_t k, shared_ptr<Species> spec);
//! Add a species alias (i.e. user-defined alternative species name).
//! Aliases are case-sensitive.
//! @param name original species name std::string.
//! @param alias alternate name std::string.
//! @return `true` if the alias was successfully added
//! (i.e. the original species name is found)
void addSpeciesAlias(const std::string& name, const std::string& alias);
//! Return a vector with isomers names matching a given composition map
//! @param compMap compositionMap of the species.
//! @return A vector of species names for matching species.
virtual std::vector<std::string> findIsomers(const compositionMap& compMap) const;
//! Return the Species object for the named species. Changes to this object
//! do not affect the ThermoPhase object until the #modifySpecies function
//! is called.

View file

@ -183,6 +183,8 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
void getAtoms(size_t, double*) except +translate_exception
cbool caseSensitiveSpecies()
void setCaseSensitiveSpecies(cbool)
void addSpeciesAlias(string, string) except +translate_exception
vector[string] findIsomers(Composition&) except +translate_exception
double molecularWeight(size_t) except +translate_exception
double meanMolecularWeight()

View file

@ -524,6 +524,22 @@ cdef class ThermoPhase(_SolutionBase):
if self.kinetics:
self.kinetics.invalidateCache()
def add_species_alias(self, name, alias):
"""
Add the alternate species name *alias* for an original species *name*.
"""
self.thermo.addSpeciesAlias(stringify(name), stringify(alias))
def find_isomers(self, comp):
"""
Find species/isomers matching a composition specified by *comp*.
"""
assert isinstance(comp, dict), 'Composition needs to be specified as dictionary'
iso = self.thermo.findIsomers(comp_map(comp))
return [pystr(b) for b in iso]
def n_atoms(self, species, element):
"""
Number of atoms of element *element* in species *species*. The element

View file

@ -852,6 +852,35 @@ void Phase::modifySpecies(size_t k, shared_ptr<Species> spec)
invalidateCache();
}
void Phase::addSpeciesAlias(const std::string& name, const std::string& alias)
{
if (speciesIndex(alias) != npos) {
throw CanteraError("Phase::addSpeciesAlias",
"Invalid alias '{}': species already exists", alias);
}
size_t k = speciesIndex(name);
if (k != npos) {
m_speciesIndices[alias] = k;
} else {
throw CanteraError("Phase::addSpeciesAlias",
"Unable to add alias '{}' "
"(original species '{}' not found).", alias, name);
}
}
vector<std::string> Phase::findIsomers(const compositionMap& compMap) const
{
vector<std::string> isomerNames;
for (const auto& k : m_species) {
if (k.second->composition == compMap) {
isomerNames.emplace_back(k.first);
}
}
return isomerNames;
}
shared_ptr<Species> Phase::species(const std::string& name) const
{
size_t k = speciesIndex(name);