Skip to content

Commit 615a0d6

Browse files
authored
Merge pull request #2532 to fix rmgpy.rmg.input.save_input_file
Fix writing of RMG input files by rmgpy.rmg.input.save_input_file A few fixes, so that scripts can use RMG to (load, modify, and) save input files. (At least for many more situations than they formerly could, even if we're not at 100% yet.)
2 parents 0c4fa99 + 6b6f576 commit 615a0d6

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

rmgpy/rmg/input.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3
1+
22

33
###############################################################################
44
# #
@@ -44,7 +44,7 @@
4444
from rmgpy.molecule import Molecule
4545
from rmgpy.molecule.fragment import Fragment
4646
from rmgpy.molecule.group import Group
47-
from rmgpy.quantity import Energy, Quantity, RateCoefficient, SurfaceConcentration
47+
from rmgpy.quantity import Energy, Quantity, RateCoefficient, SurfaceConcentration, Concentration
4848
from rmgpy.rmg.model import CoreEdgeReactionModel
4949
from rmgpy.rmg.reactionmechanismsimulator_reactors import (
5050
ConstantTLiquidSurfaceReactor,
@@ -1643,6 +1643,8 @@ def read_thermo_input_file(path, rmg0):
16431643
rmg.reaction_model = CoreEdgeReactionModel()
16441644
rmg.initial_species = []
16451645
rmg.reaction_systems = []
1646+
if rmg.output_directory is None:
1647+
rmg.output_directory = os.path.dirname(full_path)
16461648
species_dict = {}
16471649

16481650
global_context = {'__builtins__': None}
@@ -1699,12 +1701,16 @@ def save_input_file(path, rmg):
16991701
f.write(' kineticsEstimator = {0!r},\n'.format(rmg.kinetics_estimator))
17001702
f.write(')\n\n')
17011703

1702-
if rmg.surfaceSiteDenisty or rmg.binding_energies:
1704+
if rmg.surface_site_density or rmg.binding_energies:
17031705
f.write('catalystProperties(\n')
1704-
if rmg.surfaceSiteDenisty:
1705-
f.write(' surface_site_density = {0!r},'.format(rmg.surface_site_density))
1706+
if rmg.surface_site_density:
1707+
f.write(' surfaceSiteDensity = ({0:g}, "{1!s}"),'.format(rmg.surface_site_density.value, rmg.surface_site_density.units) + '\n')
17061708
if rmg.binding_energies:
1707-
f.write(' binding_energies = {0!r},'.format(rmg.binding_energies))
1709+
f.write(' bindingEnergies = {\n')
1710+
for spc, be in rmg.binding_energies.items():
1711+
f.write(' "{0!s}": ({1:g}, "{2!s}"),\n'.format(spc, be.value, be.units))
1712+
f.write(' },\n')
1713+
17081714
f.write(')\n\n')
17091715

17101716
# Species
@@ -1718,33 +1724,72 @@ def save_input_file(path, rmg):
17181724
f.write('"""),\n')
17191725
f.write(')\n\n')
17201726

1727+
def format_temperature(system):
1728+
"""Get temperature string format for reaction system, whether single value or range"""
1729+
if system.T is not None:
1730+
return '({0:g},"{1!s}")'.format(system.T.value, system.T.units)
1731+
1732+
return f'[({system.Trange[0].value:g}, "{system.Trange[0].units}"), ({system.Trange[1].value:g}, "{system.Trange[1].units}")],'
1733+
1734+
def format_pressure(system):
1735+
"""Get pressure string format for reaction system, whether single value or range"""
1736+
if system.P is not None:
1737+
return '({0:g},"{1!s}")'.format(system.P.value, system.P.units)
1738+
1739+
return f'[({system.Prange[0].value:g}, "{system.Prange[0].units}"), ({system.Prange[1].value:g}, "{system.Prange[1].units}")],'
1740+
1741+
def format_initial_mole_fractions(system):
1742+
"""Get initial mole fractions string format for reaction system"""
1743+
mole_fractions = ''
1744+
for spcs, molfrac in system.initial_mole_fractions.items():
1745+
if isinstance(molfrac, list):
1746+
mole_fractions += ' "{0!s}": [{1:g}, {2:g}],\n'.format(spcs.label, molfrac[0], molfrac[1])
1747+
else:
1748+
mole_fractions += ' "{0!s}": {1:g},\n'.format(spcs.label, molfrac)
1749+
return mole_fractions
1750+
1751+
17211752
# Reaction systems
17221753
for system in rmg.reaction_systems:
17231754
if rmg.solvent:
17241755
f.write('liquidReactor(\n')
1725-
f.write(' temperature = ({0:g},"{1!s}"),\n'.format(system.T.value, system.T.units))
1756+
f.write(' temperature = ' + format_temperature(system) + '\n')
17261757
f.write(' initialConcentrations={\n')
17271758
for spcs, conc in system.initial_concentrations.items():
1759+
# conc may have been converted to SI, so we need to convert back
1760+
if type(conc) == float:
1761+
conc = Quantity(conc, Concentration.units)
17281762
f.write(' "{0!s}": ({1:g},"{2!s}"),\n'.format(spcs.label, conc.value, conc.units))
1763+
elif isinstance(system, SurfaceReactor):
1764+
f.write('surfaceReactor(\n')
1765+
f.write(' temperature = ' + format_temperature(system) + '\n')
1766+
f.write(' initialPressure = ({0:g},"{1!s}"),\n'.format(system.P_initial.value, system.P_initial.units))
1767+
f.write(' initialGasMoleFractions={\n')
1768+
for spcs, molfrac in system.initial_gas_mole_fractions.items():
1769+
f.write(' "{0!s}": {1:g},\n'.format(spcs.label, molfrac))
1770+
f.write(' },\n')
1771+
f.write(' initialSurfaceCoverages={\n')
1772+
for spcs, cov in system.initial_surface_coverages.items():
1773+
f.write(' "{0!s}": {1:g},\n'.format(spcs.label, cov))
17291774
else:
17301775
f.write('simpleReactor(\n')
1731-
f.write(' temperature = ({0:g},"{1!s}"),\n'.format(system.T.value, system.T.units))
1732-
# Convert the pressure from SI pascal units to bar here
1733-
# Do something more fancy later for converting to user's desired units for both T and P..
1734-
f.write(' pressure = ({0:g},"{1!s}"),\n'.format(system.P.value, system.P.units))
1776+
f.write(' temperature = ' + format_temperature(system) + '\n')
1777+
f.write(' pressure = ' + format_pressure(system) + '\n')
17351778
f.write(' initialMoleFractions={\n')
1736-
for spcs, molfrac in system.initial_mole_fractions.items():
1737-
f.write(' "{0!s}": {1:g},\n'.format(spcs.label, molfrac))
1779+
f.write(format_initial_mole_fractions(system))
17381780
f.write(' },\n')
17391781

17401782
# Termination criteria
17411783
conversions = ''
17421784
for term in system.termination:
17431785
if isinstance(term, TerminationTime):
17441786
f.write(' terminationTime = ({0:g},"{1!s}"),\n'.format(term.time.value, term.time.units))
1745-
1746-
else:
1787+
elif isinstance(term, TerminationRateRatio):
1788+
f.write(' terminationRateRatio = {0:g},\n'.format(term.ratio))
1789+
elif isinstance(term, TerminationConversion):
17471790
conversions += ' "{0:s}": {1:g},\n'.format(term.species.label, term.conversion)
1791+
else:
1792+
raise NotImplementedError('Termination criteria of type {0} not supported'.format(type(term)))
17481793
if conversions:
17491794
f.write(' terminationConversion = {\n')
17501795
f.write(conversions)
@@ -1787,9 +1832,9 @@ def save_input_file(path, rmg):
17871832
if rmg.pressure_dependence:
17881833
f.write('pressureDependence(\n')
17891834
f.write(' method = {0!r},\n'.format(rmg.pressure_dependence.method))
1790-
f.write(' maximumGrainSize = ({0:g},"{1!s}"),\n'.format(rmg.pressure_dependence.grain_size.value,
1791-
rmg.pressure_dependence.grain_size.units))
1792-
f.write(' minimumNumberOfGrains = {0},\n'.format(rmg.pressure_dependence.grain_count))
1835+
f.write(' maximumGrainSize = ({0:g},"{1!s}"),\n'.format(rmg.pressure_dependence.maximum_grain_size.value,
1836+
rmg.pressure_dependence.maximum_grain_size.units))
1837+
f.write(' minimumNumberOfGrains = {0},\n'.format(rmg.pressure_dependence.minimum_grain_count))
17931838
f.write(' temperatures = ({0:g},{1:g},"{2!s}",{3:d}),\n'.format(
17941839
rmg.pressure_dependence.Tmin.value,
17951840
rmg.pressure_dependence.Tmax.value,

0 commit comments

Comments
 (0)