diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index 0d5d4008ae..7f5c37fd75 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -716,6 +716,11 @@ class Phase //! @return A vector of species names for matching species. virtual std::vector 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 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. diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 407eddc5bf..58d8b2650a 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -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() diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 66ca27472b..fa2b50f5de 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1145,6 +1145,8 @@ 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') @@ -1152,6 +1154,8 @@ 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}) diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index 7d333e2944..01defe4e97 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -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] diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index af50f1634b..c0fd0e1bb7 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -881,6 +881,11 @@ vector Phase::findIsomers(const compositionMap& compMap) const return isomerNames; } +vector Phase::findIsomers(const std::string& comp) const +{ + return findIsomers(parseCompString(comp)); +} + shared_ptr Phase::species(const std::string& name) const { size_t k = speciesIndex(name);