Skip to content

Commit

Permalink
[ck2cti] Allow elements/isotopes with custom atomic weights
Browse files Browse the repository at this point in the history
Resolves #344
  • Loading branch information
speth committed Jun 23, 2016
1 parent b7e1a5f commit 7d18120
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
24 changes: 23 additions & 1 deletion interfaces/cython/cantera/ck2cti.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ def __init__(self):
self.warning_as_error = True

self.elements = []
self.element_weights = {} # for custom elements only
self.speciesList = []
self.speciesDict = {}
self.reactions = []
Expand Down Expand Up @@ -939,6 +940,16 @@ def getRateConstantUnits(self, length_dims, length_units, quantity_dims,
units = '1' + units
return units

def addElement(self, element_string):
if '/' in element_string:
name, weight, _ = element_string.split('/')
weight = fortFloat(weight)
name = name.capitalize()
self.elements.append(name)
self.element_weights[name] = weight
else:
self.elements.append(element_string.capitalize())

def readThermoEntry(self, lines, TintDefault):
"""
Read a thermodynamics entry for one species in a Chemkin-format file
Expand Down Expand Up @@ -1469,12 +1480,14 @@ def readline():
break

line, comment = readline()
# Normalize custom atomic weights
line = re.sub(r'\s*/\s*([0-9\.EeDd+-]+)\s*/', r'/\1/ ', line)
tokens.extend(line.split())

for token in tokens:
if token.upper() == 'END':
break
self.elements.append(token.capitalize())
self.addElement(token)

elif tokens[0].upper().startswith('SPEC'):
# List of species identifiers
Expand Down Expand Up @@ -1875,6 +1888,15 @@ def writeCTI(self, header=None, name='gas', transportModel='Mix',
lines.append(' initial_state=state(temperature=300.0, pressure=OneAtm))')
lines.append('')

# Write data on custom elements
if self.element_weights:
lines.append(delimiterLine)
lines.append('# Element data')
lines.append(delimiterLine)
lines.append('')
for name,weight in self.element_weights.items():
lines.append('element(symbol={0!r}, atomic_mass={1})'.format(name, weight))

# Write the individual species data
lines.append(delimiterLine)
lines.append('# Species data')
Expand Down
9 changes: 9 additions & 0 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ def test_reaction_comments2(self):
self.assertIn('An end of line comment', text)
self.assertIn('A comment after the last reaction', text)

def test_custom_element(self):
convertMech('../data/custom-elements.inp',
outName='custom-elements.cti', quiet=True)
gas = ct.Solution('custom-elements.cti')
self.assertEqual(gas.n_elements, 4)
self.assertNear(gas.atomic_weight(2), 13.003)
self.assertEqual(gas.n_atoms('ethane', 'C'), 2)
self.assertEqual(gas.n_atoms('CC', 'C'), 1)
self.assertEqual(gas.n_atoms('CC', 'Ci'), 1)

class CtmlConverterTest(utilities.CanteraTest):
def test_sofc(self):
Expand Down
19 changes: 19 additions & 0 deletions test/data/custom-elements.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ELEMENTS
H C CI / 13.003 /AR
END

SPECIES
ethane CC
END

THERM ALL
300.000 1000.000 5000.000
ethane H 6 C 2 G100.000 5000.000 954.51 1
4.58979653E+00 1.41508345E-02-4.75965676E-06 8.60302658E-10-6.21723639E-14 2
-1.27217512E+04-3.61719603E+00 3.78034545E+00-3.24275729E-03 5.52385248E-05 3
-6.38587529E-08 2.28639901E-11-1.16203413E+04 5.21029844E+00 4
CC H 6 C 1 CI1 G100.000 5000.000 954.51 1
4.58979542E+00 1.41508364E-02-4.75965787E-06 8.60302924E-10-6.21723861E-14 2
-1.27217507E+04-2.92404261E+00 3.78034578E+00-3.24276131E-03 5.52385395E-05 3
-6.38587729E-08 2.28639990E-11-1.16203414E+04 5.90344446E+00 4
END

0 comments on commit 7d18120

Please sign in to comment.