Skip to content

Commit e1f226c

Browse files
committed
Unified MEASURE input and output file formats.
As of now the only difference between the two is the presence of pdepreaction() blocks in the latter, signifying the net reactions and their pressure-dependent kinetics. This means that the entirety of the input file is contained in the output file. Accordingly, there are no longer separate readInput() and readOutput() methods, nor separate writeInput() and writeOutput() methods; instead, there are now simply readFile() and writeFile() methods to use for both cases.
1 parent 0dfce7b commit e1f226c

3 files changed

Lines changed: 48 additions & 73 deletions

File tree

rmgpy/measure/input.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from rmgpy.reaction import Reaction
4343
from rmgpy.molecule import Molecule
4444
from rmgpy.statmech import *
45-
from rmgpy.kinetics import Arrhenius
45+
from rmgpy.kinetics import Arrhenius, PDepArrhenius, Chebyshev
4646
from rmgpy.thermo import *
4747

4848
from network import Network
@@ -141,6 +141,20 @@ def reaction(reactants, products, kinetics=None, transitionState=None):
141141
network.pathReactions.append(rxn)
142142
logging.debug('Found reaction "{0}"'.format(rxn))
143143

144+
def pdepreaction(reactants, products, kinetics=None):
145+
global network
146+
try:
147+
rxn = Reaction(
148+
reactants = reactants,
149+
products = products,
150+
kinetics=kinetics,
151+
)
152+
except KeyError, e:
153+
raise InputError('A reaction was encountered with species "{0}", but that species was not found in the input file.'.format(e.args[0]))
154+
155+
network.netReactions.append(rxn)
156+
logging.debug('Found pdepreaction "{0}"'.format(rxn))
157+
144158
def collisionModel(type, parameters, bathGas):
145159
global network
146160
if type.lower() == 'single exponential down':
@@ -309,9 +323,9 @@ def getPressuresForModel(model, Pmin, Pmax, Pcount):
309323
Plist = 10.0 ** numpy.linspace(math.log10(Pmin), math.log10(Pmax), Pcount)
310324
return Plist
311325

312-
def readInput(path):
326+
def readFile(path):
313327
"""
314-
Reads a MEASURE input file from location `path` on disk. The input file
328+
Reads a MEASURE input or output file from location `path` on disk. The file
315329
format is described in the :ref:`measureusersguide`. Returns a number of
316330
quantities:
317331
@@ -381,6 +395,9 @@ def readInput(path):
381395
'reaction': reaction,
382396
'Arrhenius': Arrhenius,
383397
'TransitionState': TransitionState,
398+
'pdepreaction': pdepreaction,
399+
'Chebyshev': Chebyshev,
400+
'PDepArrhenius': PDepArrhenius,
384401
'collisionModel': collisionModel,
385402
'temperatures': temperatures,
386403
'pressures': pressures,
@@ -451,6 +468,11 @@ def readInput(path):
451468
rxn.reactants.sort()
452469
rxn.products = [speciesDict[label] for label in rxn.products]
453470
rxn.products.sort()
471+
for rxn in network.netReactions:
472+
rxn.reactants = [speciesDict[label] for label in rxn.reactants]
473+
rxn.reactants.sort()
474+
rxn.products = [speciesDict[label] for label in rxn.products]
475+
rxn.products.sort()
454476

455477
# Figure out which configurations are isomers, reactant channels, and product channels
456478
for rxn in network.pathReactions:

rmgpy/measure/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def execute(inputFile, outputFile=None, drawFile=None, logFile=None, quiet=False
127127
logHeader()
128128

129129
# Load input file
130-
from rmgpy.measure.input import readInput
131-
params = readInput(os.path.relpath(inputFile))
130+
from rmgpy.measure.input import readFile
131+
params = readFile(os.path.relpath(inputFile))
132132

133133
# Only proceed if the input network is valid
134134
if params is not None:
@@ -188,12 +188,12 @@ def execute(inputFile, outputFile=None, drawFile=None, logFile=None, quiet=False
188188
logging.info('')
189189

190190
# Save results to file
191-
from rmgpy.measure.output import writeOutput
191+
from rmgpy.measure.output import writeFile
192192
if outputFile is not None:
193193
out = os.path.relpath(outputFile)
194194
else:
195195
out = os.path.join(outputDirectory, 'output.py')
196-
writeOutput(out, network, Tlist, Plist, Elist, method, model)
196+
writeFile(out, network, Tlist, Plist, Elist, method, model, Tmin, Tmax, Pmin, Pmax)
197197

198198
# Log end timestamp
199199
logging.info('')

rmgpy/measure/output.py

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def writeReaction(f, rxn):
173173
f.write('reaction(\n')
174174
f.write(' reactants=[{0}],\n'.format(', '.join([('"{0}"'.format(spec)) for spec in rxn.reactants])))
175175
f.write(' products=[{0}],\n'.format(', '.join([('"{0}"'.format(spec)) for spec in rxn.products])))
176-
f.write(' reversible={0!r},\n'.format(rxn.reversible))
177176
if rxn.kinetics is not None:
178177
f.write(' kinetics={0!r},\n'.format(rxn.kinetics))
179178
if rxn.transitionState is not None:
@@ -205,7 +204,6 @@ def writePDepReaction(f, rxn):
205204
f.write('pdepreaction(\n')
206205
f.write(' reactants=[{0}],\n'.format(', '.join([('"{0}"'.format(spec)) for spec in rxn.reactants])))
207206
f.write(' products=[{0}],\n'.format(', '.join([('"{0}"'.format(spec)) for spec in rxn.products])))
208-
f.write(' reversible={0!r},\n'.format(rxn.reversible))
209207
if rxn.kinetics is not None:
210208
if isinstance(rxn.kinetics, Chebyshev):
211209
f.write(' kinetics=Chebyshev(\n')
@@ -232,67 +230,10 @@ def writePDepReaction(f, rxn):
232230

233231
################################################################################
234232

235-
def writeOutput(path, network, Tlist, Plist, Elist, method, model):
236-
"""
237-
Write a MEASURE output file to `path` on disk. The parameters needed mirror
238-
those returned by :meth:`readInput()`:
239-
240-
* The :class:`Network` object `network` representing the unimolecular
241-
reaction network
242-
243-
* The list of temperatures `Tlist` in K to be used in the master equation
244-
calculation
245-
246-
* The list of pressures `Plist` in Pa to be used in the master equation
247-
calculation
248-
249-
* A tuple `Elist` containing the maximum energy grain size in J/mol and the
250-
minimum number of energy grains to use in the master equation calculation;
251-
whichever of these results in more energy grains
252-
253-
* The approximate `method` to use to estimate the phenomenological rate
254-
coefficients :math:`k(T,P)`
255-
256-
* The interpolation `model` to fit the estimated :math:`k(T,P)` values to
257-
258-
If successful, the file created on disk will contain all of the species
259-
and net reaction data, including all phenomenological rate coefficients
260-
:math:`k(T,P)`.
261-
"""
262-
263-
logging.info('Saving output to "{0}"...'.format(path))
264-
265-
f = open(os.path.relpath(path), 'w')
266-
267-
f.write('################################################################################\n')
268-
f.write('#\n')
269-
f.write('# MEASURE output file for {0}\n'.format(network))
270-
f.write('#\n')
271-
f.write('# Generated on {0}\n'.format(time.asctime()))
272-
f.write('#\n')
273-
f.write('################################################################################\n\n')
274-
275-
# Write metadata
276-
writeNetworkMetadata(f, network)
277-
278-
# Write each species to file
279-
writeNetworkSpecies(f, network)
280-
f.write('################################################################################\n\n')
281-
# Write each configuration to file
282-
writeNetworkConfigurations(f, network)
283-
f.write('################################################################################\n\n')
284-
285-
# Write each net reaction to file
286-
writeNetworkNetReactions(f, network)
287-
288-
f.close()
289-
290-
################################################################################
291-
292-
def writeInput(path, network, Tlist, Plist, Elist, method, model):
233+
def writeFile(path, network, Tlist, Plist, Elist, method, model, Tmin, Tmax, Pmin, Pmax):
293234
"""
294235
Write a MEASURE input file to `path` on disk. The parameters needed mirror
295-
those returned by :meth:`readInput()`:
236+
those returned by :meth:`readFile()`:
296237
297238
* The :class:`Network` object `network` representing the unimolecular
298239
reaction network
@@ -320,7 +261,7 @@ def writeInput(path, network, Tlist, Plist, Elist, method, model):
320261

321262
f.write('################################################################################\n')
322263
f.write('#\n')
323-
f.write('# MEASURE input file for {0}\n'.format(network))
264+
f.write('# MEASURE file for {0}\n'.format(network))
324265
f.write('#\n')
325266
f.write('# Generated on {0}\n'.format(time.asctime()))
326267
f.write('#\n')
@@ -353,14 +294,26 @@ def writeInput(path, network, Tlist, Plist, Elist, method, model):
353294
f.write(' },\n')
354295
f.write(')\n\n')
355296

356-
f.write('temperatures(([{0}],"K"))\n'.format(', '.join([('{0:g}'.format(T)) for T in Tlist])))
357-
f.write('pressures(([{0}],"bar"))\n'.format(', '.join([('{0:g}'.format(P/1e5)) for P in Plist])))
358-
dE, count = Elist
297+
if Tmin is not None and Tmax is not None:
298+
f.write('temperatures(Tmin=({0},"K"), Tmax=({1},"K"), count={2})\n'.format(Tmin, Tmax, len(Tlist)))
299+
else:
300+
f.write('temperatures(([{0}],"K"))\n'.format(', '.join([('{0:g}'.format(T)) for T in Tlist])))
301+
if Pmin is not None and Pmax is not None:
302+
f.write('pressures(Pmin=({0},"bar"), Pmax=({1},"bar"), count={2})\n'.format(Pmin/1e5, Pmax/1e5, len(Plist)))
303+
else:
304+
f.write('pressures(([{0}],"bar"))\n'.format(', '.join([('{0:g}'.format(P/1e5)) for P in Plist])))
305+
dE = Elist[1] - Elist[0]; count = len(Elist)
359306
f.write('energies(dE=({0:g},"kJ/mol"), count={1:d})\n'.format(dE/1000.0, count))
360307
f.write('method("{0}")\n'.format(method))
361308
if model[0].lower() == 'chebyshev':
362309
f.write('interpolationModel("chebyshev", {0:d}, {1:d})\n'.format(model[1], model[2]))
363310
else:
364311
f.write('interpolationModel("{0}")\n'.format(model[0].lower()))
365-
312+
f.write('\n')
313+
314+
if len(network.netReactions) > 0:
315+
f.write('################################################################################\n\n')
316+
# Write each net reaction to file
317+
writeNetworkNetReactions(f, network)
318+
366319
f.close()

0 commit comments

Comments
 (0)