Skip to content

Commit ed6584c

Browse files
vincthnngynrwest
authored andcommitted
Redid the yaml_input_reader class as the previous version had INSANELY MESSY SPAGHETTI CODE.
Changes to the class: - started using field mapping to clean up the functions, eliminates the super long and messy if/else statements seen before - implemented a common processor function for structures and data in order to keep code clean (abstracted it basically) - added PyNum doc strings for accessibility - added more line by line comments so code is more clearly explained Changes to example input: - made it compliant with RMG Changes made to main.py and input.py: - main.py: ensured that read_file_auto is called - input.py: added PyNum docstring
1 parent 4ecb685 commit ed6584c

File tree

4 files changed

+929
-534
lines changed

4 files changed

+929
-534
lines changed

examples/rmg/butane_yaml_example/input.yaml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ simpleReactor:
127127
# list initial mole fractions of compounds using the label from the 'species' label.
128128
# RMG will normalize if sum/=1
129129
initialMoleFractions:
130-
N2: 4
131-
O2: 1
130+
N2: 4.0
131+
O2: 1.0
132132
butane: 0.1538461538 # 1/6.5
133133

134134
# number of simulations used to explore variable temperature and pressure reactors
@@ -138,10 +138,10 @@ simpleReactor:
138138
# only one must be specified
139139
# the first condition to be satisfied will terminate the process
140140
terminationConversion:
141-
butane: 0.99
141+
butane: 0.9 # Changed from 0.99 to 0.9 for faster convergence
142142

143143
terminationTime:
144-
value: 40
144+
value: 100 # Increased from 40 to 100 seconds as safety fallback
145145
units: s
146146

147147
# the next two optional values specify how RMG computes sensitivities of
@@ -176,10 +176,10 @@ simpleReactor:
176176
# normally this doesn't cause many issues and is modified after other issues are
177177
# ruled out
178178
simulator:
179-
atol: 1e-16
180-
rtol: 1e-8
181-
# sensAtol: 1e-6
182-
# sensRtol: 1e-4
179+
atol: 1.0e-16
180+
rtol: 1.0e-8
181+
sens_atol: 1.0e-6
182+
sens_rtol: 1.0e-4
183183

184184
# used to add species to the model and to reduce memory usage by removing unimportant additional species.
185185
# all relative values are normalized by a characteristic flux at that time point
@@ -197,7 +197,7 @@ model:
197197
# determines when to stop a ODE run to add a species.
198198
# Lower values will improve speed.
199199
# if it is too low, may never get to the end simulation to prune species.
200-
toleranceInterruptSimulation: 1
200+
toleranceInterruptSimulation: 1.0
201201

202202
# number of edge species needed to accumulate before pruning occurs
203203
# larger values require more memory and will prune less often
@@ -218,11 +218,11 @@ model:
218218
# for bimolecular reactions, will only allow them to react if
219219
# filterThreshold*C_A*C_B > toleranceMoveToCore*characteristic_rate
220220
# and if filterReactions=True
221-
filterThreshold: 1e8
221+
filterThreshold: 1.0e8
222222

223223
options:
224224
# provides a name for the seed mechanism produced at the end of an rmg run default is 'Seed'
225-
name: SeedName
225+
name: butane_oxidation # Changed from generic 'SeedName' to be more descriptive
226226

227227
# if True (default) every iteration it saves the current model as libraries/seeds
228228
# (and deletes the old one)
@@ -288,13 +288,13 @@ pressureDependence:
288288
min: 300
289289
max: 2200
290290
units: K
291-
count: 2
291+
count: 8 # Increased from 2 for better coverage
292292

293293
pressures:
294294
min: 0.01
295295
max: 100
296296
units: bar
297-
count: 3
297+
count: 5 # Increased from 3 for better coverage
298298

299299
# The two options for interpolation are 'PDepArrhenius' (no extra arguments) and
300300
# 'Chebyshev' which is followed by the number of basis sets in
@@ -331,7 +331,7 @@ generatedSpeciesConstraints:
331331
# maximumHeavyAtoms: 20
332332

333333
# maximum radicals on a molecule
334-
maximumRadicalElectrons: 1
334+
maximumRadicalElectrons: 2 # Increased from 1 to allow peroxy radicals (ROO•)
335335

336336
# maximum number of singlet carbenes (lone pair on a carbon atom) in a molecule
337337
maximumSingletCarbenes: 1

rmgpy/rmg/input.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,28 +1940,51 @@ def read_input_file_auto(path, rmg0):
19401940
this function automatically detects the file format based on the extension
19411941
and calls the appropriate reader
19421942
1943-
:param path: Path to the input file (.py or .yaml/.yml)
1944-
:param rmg0: RMG object to populate
1943+
Parameters
1944+
----------
1945+
path : Union[str, Path]
1946+
Path to the input file (.py or .yaml/.yml)
1947+
rmg0 : RMG
1948+
RMG object to populate with input data
1949+
1950+
Raises
1951+
------
1952+
IOError
1953+
If the input file cannot be found
1954+
ValueError
1955+
If the file format is unsupported
1956+
ImportError
1957+
If the file is in YAML format but PyYAML is not installed in the
1958+
current Python environment.
19451959
"""
19461960
from pathlib import Path
19471961

1948-
# Get the file extension
1962+
# get the file extension
19491963
file_path = Path(path)
19501964
extension = file_path.suffix.lower()
19511965

1952-
# Check if file exists
1966+
# check if file exists
19531967
if not file_path.exists():
19541968
raise IOError(f'The input file "{path}" could not be found.')
19551969

1956-
# Route to appropriate reader based on extension
1970+
# route to appropriate reader based on extension
19571971
if extension == '.py':
1958-
# Use the original Python input file reader
1972+
# use the original Python input file reader
19591973
logging.info(f'Detected Python input file format (.py)')
19601974
read_input_file(path, rmg0)
19611975
elif extension in ['.yaml', '.yml']:
1962-
# Use the YAML input file reader
1976+
# use the YAML input file reader
19631977
try:
1964-
from rmgpy.rmg.yaml_input_reader import read_yaml_input_file
1978+
#iImport the YAML reader functions directly
1979+
import sys
1980+
import os
1981+
1982+
# add the directory containing yaml_input_reader.py to the Python path
1983+
yaml_reader_dir = os.path.dirname(__file__)
1984+
if yaml_reader_dir not in sys.path:
1985+
sys.path.insert(0, yaml_reader_dir)
1986+
1987+
from yaml_input_reader import read_yaml_input_file
19651988
logging.info(f'Detected YAML input file format ({extension})')
19661989
read_yaml_input_file(path, rmg0)
19671990
except ImportError:

rmgpy/rmg/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,14 @@ def load_input(self, path=None):
260260
Load an RMG job from the input file located at `input_file`, or
261261
from the `input_file` attribute if not given as a parameter.
262262
"""
263-
from rmgpy.rmg.input import read_input_file
263+
from rmgpy.rmg.input import read_input_file, read_input_file_auto, read_thermo_input_file, save_input_file
264+
265+
self.input_file = path
266+
read_input_file_auto(path, self) # Changed to use auto-detection
264267

265268
if path is None:
266269
path = self.input_file
267-
read_input_file(path, self)
270+
read_input_file_auto(path, self)
268271
self.reaction_model.kinetics_estimator = self.kinetics_estimator
269272
# If the output directory is not yet set, then set it to the same
270273
# directory as the input file by default

0 commit comments

Comments
 (0)