Skip to content

Commit 9fce451

Browse files
committed
Optimization in chemkin write_thermo_entry
1 parent 5fa7bf7 commit 9fce451

1 file changed

Lines changed: 50 additions & 36 deletions

File tree

rmgpy/chemkin.pyx

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ from rmgpy.reaction import Reaction
5252
from rmgpy.rmg.pdep import PDepNetwork, PDepReaction
5353
from rmgpy.species import Species
5454
from rmgpy.thermo import NASAPolynomial, NASA
55+
from rmgpy.thermo.nasa cimport NASA, NASAPolynomial
5556
from rmgpy.transport import TransportData
5657
from rmgpy.util import make_output_subdirectory
5758

@@ -1568,42 +1569,55 @@ def get_species_identifier(species):
15681569
################################################################################
15691570

15701571

1571-
def write_thermo_entry(species, element_counts=None, verbose=True):
1572+
def write_thermo_entry(species, element_counts=None, bint verbose=True):
15721573
"""
15731574
Return a string representation of the NASA model readable by Chemkin.
15741575
To use this method you must have exactly two NASA polynomials in your
15751576
model, and you must use the seven-coefficient forms for each.
15761577
"""
1577-
1578-
thermo = species.get_thermo_data()
1579-
1580-
if not isinstance(thermo, NASA):
1578+
cdef NASA thermo
1579+
cdef NASAPolynomial poly_low, poly_high
1580+
cdef dict counts
1581+
cdef list sorted_elements, elements, short_lines
1582+
cdef bint extended_syntax
1583+
cdef int count, isotope
1584+
cdef str string, line, short_line, chemkin_name, symbol, elem_1, elem_2
1585+
cdef object thermo_data
1586+
1587+
thermo_data = species.get_thermo_data()
1588+
1589+
if not isinstance(thermo_data, NASA):
15811590
raise ChemkinError('Cannot generate Chemkin string for species "{0}": '
15821591
'Thermodynamics data must be a NASA object.'.format(species))
1592+
thermo = <NASA>thermo_data
15831593

15841594
assert len(thermo.polynomials) == 2
1585-
assert thermo.polynomials[0].Tmin.value_si < thermo.polynomials[1].Tmin.value_si
1586-
assert thermo.polynomials[0].Tmax.value_si == thermo.polynomials[1].Tmin.value_si
1587-
assert thermo.polynomials[0].cm2 == 0 and thermo.polynomials[0].cm1 == 0
1588-
assert thermo.polynomials[1].cm2 == 0 and thermo.polynomials[1].cm1 == 0
1595+
poly_low = thermo.polynomials[0]
1596+
poly_high = thermo.polynomials[1]
1597+
assert poly_low.Tmin.value_si < poly_high.Tmin.value_si
1598+
assert poly_low.Tmax.value_si == poly_high.Tmin.value_si
1599+
assert poly_low.cm2 == 0 and poly_low.cm1 == 0
1600+
assert poly_high.cm2 == 0 and poly_high.cm1 == 0
15891601

15901602
# Determine the number of each type of element in the molecule
15911603
# Need to use the element's chemkin name, not the element symbol, because of isotopes.
15921604
# so we can't just use molecule[0].get_element_count().
15931605
if element_counts is None:
1594-
element_counts = {}
1595-
for atom in species.molecule[0].atoms:
1596-
element = atom.element.chemkin_name
1597-
element_counts[element] = element_counts.get(element, 0) + 1
1606+
counts = {}
1607+
for atom in species.molecule[0].vertices:
1608+
chemkin_name = atom.element.chemkin_name
1609+
counts[chemkin_name] = counts.get(chemkin_name, 0) + 1
1610+
else:
1611+
counts = element_counts
15981612

15991613
# Sort the element_counts dictionary so that it's C, H, Al, B, Cl, D, etc.
16001614
# if there's any C, else Al, B, Cl, D, H, if not. This is the "Hill" system
16011615
# done by Molecule.get_formula
1602-
if 'C' in element_counts:
1603-
sorted_elements = sorted(element_counts, key = lambda e: {'C':'0','H':'1'}.get(e, e))
1616+
if 'C' in counts:
1617+
sorted_elements = sorted(counts, key=lambda e: {'C': '0', 'H': '1'}.get(e, e))
16041618
else:
1605-
sorted_elements = sorted(element_counts)
1606-
element_counts = {e: element_counts[e] for e in sorted_elements}
1619+
sorted_elements = sorted(counts)
1620+
counts = {e: counts[e] for e in sorted_elements}
16071621

16081622
string = ''
16091623
# Write thermo comments
@@ -1618,9 +1632,9 @@ def write_thermo_entry(species, element_counts=None, verbose=True):
16181632
string += "! {0}\n".format(line)
16191633

16201634
# Compile element count string
1621-
extended_syntax = len(element_counts) > 4 # If there are more than 4 elements, use extended syntax
1635+
extended_syntax = len(counts) > 4 # If there are more than 4 elements, use extended syntax
16221636
elements = []
1623-
for key, count in element_counts.items():
1637+
for key, count in counts.items():
16241638
if isinstance(key, tuple):
16251639
symbol, isotope = key
16261640
chemkin_name = get_element(symbol, isotope=isotope).chemkin_name
@@ -1650,31 +1664,31 @@ def write_thermo_entry(species, element_counts=None, verbose=True):
16501664
string += '{ident:<16} {elem_1:<20}G{Tmin:>10.3f}{Tint:>10.3f}{Tmax:>8.2f} 1{elem_2}\n'.format(
16511665
ident=get_species_identifier(species),
16521666
elem_1=elem_1,
1653-
Tmin=thermo.polynomials[0].Tmin.value_si,
1654-
Tint=thermo.polynomials[1].Tmax.value_si,
1655-
Tmax=thermo.polynomials[0].Tmax.value_si,
1667+
Tmin=poly_low.Tmin.value_si,
1668+
Tint=poly_high.Tmax.value_si,
1669+
Tmax=poly_low.Tmax.value_si,
16561670
elem_2=elem_2,
16571671
)
16581672

16591673
# Line 2
1660-
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E}{4:< 15.8E} 2\n'.format(thermo.polynomials[1].c0,
1661-
thermo.polynomials[1].c1,
1662-
thermo.polynomials[1].c2,
1663-
thermo.polynomials[1].c3,
1664-
thermo.polynomials[1].c4)
1674+
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E}{4:< 15.8E} 2\n'.format(poly_high.c0,
1675+
poly_high.c1,
1676+
poly_high.c2,
1677+
poly_high.c3,
1678+
poly_high.c4)
16651679

16661680
# Line 3
1667-
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E}{4:< 15.8E} 3\n'.format(thermo.polynomials[1].c5,
1668-
thermo.polynomials[1].c6,
1669-
thermo.polynomials[0].c0,
1670-
thermo.polynomials[0].c1,
1671-
thermo.polynomials[0].c2)
1681+
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E}{4:< 15.8E} 3\n'.format(poly_high.c5,
1682+
poly_high.c6,
1683+
poly_low.c0,
1684+
poly_low.c1,
1685+
poly_low.c2)
16721686

16731687
# Line 4
1674-
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E} 4\n'.format(thermo.polynomials[0].c3,
1675-
thermo.polynomials[0].c4,
1676-
thermo.polynomials[0].c5,
1677-
thermo.polynomials[0].c6)
1688+
string += '{0:< 15.8E}{1:< 15.8E}{2:< 15.8E}{3:< 15.8E} 4\n'.format(poly_low.c3,
1689+
poly_low.c4,
1690+
poly_low.c5,
1691+
poly_low.c6)
16781692

16791693
return string
16801694

0 commit comments

Comments
 (0)