Skip to content

Commit

Permalink
[Thermo] add support for concentration string in findIsomers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar Schoegl committed Oct 30, 2019
1 parent d963030 commit d7ca08b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/cantera/thermo/Phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ class Phase
//! @return A vector of species names for matching species.
virtual std::vector<std::string> findIsomers(const compositionMap& compMap) const;

//! Return a vector with isomers names matching a given composition string
//! @param comp String containing a composition map
//! @return A vector of species names for matching species.
virtual std::vector<std::string> findIsomers(const std::string& comp) 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
1 change: 1 addition & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
void setCaseSensitiveSpecies(cbool)
void addSpeciesAlias(string, string) except +translate_exception
vector[string] findIsomers(Composition&) except +translate_exception
vector[string] findIsomers(string) except +translate_exception

double molecularWeight(size_t) except +translate_exception
double meanMolecularWeight()
Expand Down
4 changes: 4 additions & 0 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,13 +1145,17 @@ def test_alias(self):
self.assertTrue(self.gas.species_index('hydrogen') == 0)
self.gas.X = 'hydrogen:.5, O2:.5'
self.assertNear(self.gas.X[0], 0.5)
with self.assertRaisesRegex(ct.CanteraError, 'Invalid alias'):
self.gas.add_species_alias('H2', 'O2')
with self.assertRaisesRegex(ct.CanteraError, 'Unable to add alias'):
self.gas.add_species_alias('spam', 'eggs')

def test_isomers(self):
gas = ct.Solution('nDodecane_Reitz.yaml')
iso = gas.find_isomers({'C':4, 'H':9, 'O':2})
self.assertTrue(len(iso) == 2)
iso = gas.find_isomers('C:4, H:9, O:2')
self.assertTrue(len(iso) == 2)
iso = gas.find_isomers({'C':7, 'H':15})
self.assertTrue(len(iso) == 1)
iso = gas.find_isomers({'C':7, 'H':16})
Expand Down
8 changes: 6 additions & 2 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,12 @@ cdef class ThermoPhase(_SolutionBase):
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))
if isinstance(comp, dict):
iso = self.thermo.findIsomers(comp_map(comp))
elif isinstance(comp, (str, bytes)):
iso = self.thermo.findIsomers(stringify(comp))
else:
raise CanteraError('Invalid composition')

return [pystr(b) for b in iso]

Expand Down
5 changes: 5 additions & 0 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,11 @@ vector<std::string> Phase::findIsomers(const compositionMap& compMap) const
return isomerNames;
}

vector<std::string> Phase::findIsomers(const std::string& comp) const
{
return findIsomers(parseCompString(comp));
}

shared_ptr<Species> Phase::species(const std::string& name) const
{
size_t k = speciesIndex(name);
Expand Down

0 comments on commit d7ca08b

Please sign in to comment.