Skip to content

Commit

Permalink
Allow colons in species names in CTI phase definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Nov 25, 2016
1 parent 05ad05c commit 3300611
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
50 changes: 28 additions & 22 deletions interfaces/cython/cantera/ctml_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,29 +1767,8 @@ def __init__(self,
# dictionary of species names
self._spmap = {}

# for each species string, check whether or not the species
# are imported or defined locally. If imported, the string
# contains a colon (:)
for sp in self._species:
icolon = sp.find(':')
if icolon > 0:
#datasrc, spnames = sp.split(':')
datasrc = sp[:icolon].strip()
spnames = sp[icolon+1:]
self._sp.append((datasrc+'.xml', spnames))
else:
spnames = sp
self._sp.append(('', spnames))

for s in spnames.split():
self._spmap[s] = self._dim

self._rxns = reactions

# check that species have been declared
if len(self._spmap) == 0:
raise CTI_Error('No species declared for phase '+self._name)

# and that only one species is declared if it is a pure phase
if self.is_pure() and len(self._spmap) > 1:
raise CTI_Error('Stoichiometric phases must declare exactly one species, \n'+
Expand Down Expand Up @@ -1819,7 +1798,6 @@ def conc_dim(self):
rate coefficients."""
return (1, -self._dim)


def buildrxns(self, p):

if isinstance(self._rxns, str):
Expand Down Expand Up @@ -1865,6 +1843,34 @@ def buildrxns(self, p):


def build(self, p):
# for each species string, check whether or not the species
# are imported or defined locally. If imported, the string
# contains a colon (:)
for sp in self._species:
foundColon = False
allLocal = True
for token in sp.split():
if ':' in sp:
foundColon = True
if token not in _speciesnames:
allLocal = False

if foundColon and not allLocal:
icolon = sp.find(':')
datasrc = sp[:icolon].strip()
spnames = sp[icolon+1:]
self._sp.append((datasrc+'.xml', spnames))
else:
spnames = sp
self._sp.append(('', spnames))

for s in spnames.split():
self._spmap[s] = self._dim

# check that species have been declared
if len(self._spmap) == 0:
raise CTI_Error('No species declared for phase '+self._name)

p.addComment(' phase '+self._name+' ')
ph = p.addChild('phase')
ph['id'] = self._name
Expand Down
22 changes: 11 additions & 11 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ def test_pathologicalSpeciesNames(self):
outName=pjoin(self.test_work_dir, 'species-names.cti'), quiet=True)
gas = ct.Solution('species-names.cti')

self.assertEqual(gas.n_species, 6)
self.assertEqual(gas.n_species, 7)
self.assertEqual(gas.species_name(0), '(Parens)')
self.assertEqual(gas.species_name(1), '@#$%^-2')
self.assertEqual(gas.species_name(2), '[xy2]*{.}')
self.assertEqual(gas.species_name(3), 'plus+')
self.assertEqual(gas.species_name(4), 'eq=uals')
self.assertEqual(gas.species_name(5), 'plus')
self.assertEqual(gas.species_name(3), '[xy2]*{.}')
self.assertEqual(gas.species_name(4), 'plus+')
self.assertEqual(gas.species_name(5), 'eq=uals')
self.assertEqual(gas.species_name(6), 'plus')

self.assertEqual(gas.n_reactions, 6)
nu = gas.product_stoich_coeffs() - gas.reactant_stoich_coeffs()
self.assertEqual(list(nu[:,0]), [-1, -1, 2, 0, 0, 0])
self.assertEqual(list(nu[:,1]), [-2, 3, -1, 0, 0, 0])
self.assertEqual(list(nu[:,2]), [-1, 0, 0, 1, 0, 0])
self.assertEqual(list(nu[:,3]), [3, 0, 0, -2, -1, 0])
self.assertEqual(list(nu[:,4]), [2, 0, 0, -1, 0, -1])
self.assertEqual(list(nu[:,5]), [1, 0, 0, 1, -1, -1])
self.assertEqual(list(nu[:,0]), [-1, -1, 0, 2, 0, 0, 0])
self.assertEqual(list(nu[:,1]), [-2, 3, 0, -1, 0, 0, 0])
self.assertEqual(list(nu[:,2]), [-1, 0, 0, 0, 1, 0, 0])
self.assertEqual(list(nu[:,3]), [3, 0, 0, 0, -2, -1, 0])
self.assertEqual(list(nu[:,4]), [2, 0, 0, 0, -1, 0, -1])
self.assertEqual(list(nu[:,5]), [1, 0, 0, 0, 1, -1, -1])

def test_unterminatedSections(self):
with self.assertRaises(ck2cti.InputParseError):
Expand Down
6 changes: 5 additions & 1 deletion test/data/species-names.inp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Elements
H C
End
SPEC
(Parens) @#$%^-2 [xy2]*{.} plus+ eq=uals plus
(Parens) @#$%^-2 co:lons [xy2]*{.} plus+ eq=uals plus
end

thermo
Expand All @@ -15,6 +15,10 @@ thermo
7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2
-9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3
-4.84743026E-08 1.66693956E-11-1.02466476E+04-4.64130376E+00 4
co:lons C 1H 4 G 200.000 3500.000 1000.000 1
7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2
-9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3
-4.84743026E-08 1.66693956E-11-1.02466476E+04-4.64130376E+00 4
[xy2]*{.} C 1H 4 G 200.000 3500.000 1000.000 1
7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2
-9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3
Expand Down

0 comments on commit 3300611

Please sign in to comment.