Skip to content

Commit

Permalink
[Thermo] add functions findIsomer/addSpeciesAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and Ingmar Schoegl committed Oct 30, 2019
1 parent 1e01054 commit 16c3b93
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/cantera/thermo/Phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,19 @@ class Phase
*/
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.
Expand Down
2 changes: 2 additions & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 16c3b93

Please sign in to comment.