def execute(inputFile, options): """ Generate a reaction model for the set of reaction systems specified in an input file at location `inputFile`. Output and temporary files will be placed in the directories `outputDir` and `scratchDir`, respectively; if either of these are empty strings, the corresponding files will be placed in the same directory as the input file. The `libraryDir` parameter can be used to specify additional directories to search for RMG databases. The `verbose` parameter is an integer specifying the amount of log text seen at the console; the levels correspond to those of the :data:`logging` module. """ # Set directories settings.outputDirectory = options.outputDirectory settings.scratchDirectory = options.scratchDirectory settings.libraryDirectory = options.libraryDirectory # Save initialization time settings.initializationTime = time.time() # Set up log (uses stdout and a file) initializeLog(options.verbose) # Log start timestamp logging.info('RMG execution initiated at ' + time.asctime() + '\n') # Print out RMG header printRMGHeader() # Make output subdirectories plotDir = os.path.join(settings.outputDirectory, 'plot') if os.path.exists(plotDir): for f in os.listdir(plotDir): os.remove(plotDir + '/' + f) os.rmdir(plotDir) os.mkdir(plotDir) specDir = os.path.join(settings.outputDirectory, 'species') if os.path.exists(specDir): for f in os.listdir(specDir): os.remove(specDir + '/' + f) os.rmdir(specDir) os.mkdir(specDir) # Read input file reactionModel, coreSpecies, reactionSystems = io.readInputFile(inputFile) # Initialize reaction model if options.restart: import cPickle logging.info('Loading previous restart file...') f = open(os.path.join(settings.outputDirectory,'restart.pkl'), 'rb') species.speciesList = cPickle.load(f) species.speciesCounter = cPickle.load(f) reaction.reactionList = cPickle.load(f) reactionModel = cPickle.load(f) reactionSystems = cPickle.load(f) f.close() options.restart = False # have already restarted else: reactionModel.initialize(coreSpecies) # RMG execution statistics coreSpeciesCount = [] coreReactionCount = [] edgeSpeciesCount = [] edgeReactionCount = [] execTime = [] restartSize = [] memoryUse = [] # Main RMG loop done = False while not done: done = True speciesToAdd = [] for index, reactionSystem in enumerate(reactionSystems): # Conduct simulation logging.info('Conducting simulation of reaction system %s...' % (index+1)) t, y, dydt, valid, spec = reactionSystem.simulate(reactionModel) # Postprocess results logging.info('Saving simulation results for reaction system %s...' % (index+1)) reactionSystem.postprocess(reactionModel, t, y, dydt, str(index+1)) # If simulation is invalid, note which species should be added to # the core if not valid: speciesToAdd.append(spec) done = False # Add the notes species to the core speciesToAdd = list(set(speciesToAdd)) for spec in speciesToAdd: reactionModel.enlarge(spec) # Save the restart file import cPickle logging.info('Saving restart file...') f = open(os.path.join(settings.outputDirectory,'restart.pkl'), 'wb') cPickle.dump(species.speciesList, f) cPickle.dump(species.speciesCounter, f) cPickle.dump(reaction.reactionList, f) cPickle.dump(reactionModel, f) cPickle.dump(reactionSystems, f) f.close() if not done: logging.info('Updating RMG execution statistics...') # Update RMG execution statistics coreSpeciesCount.append(len(reactionModel.core.species)) coreReactionCount.append(len(reactionModel.core.reactions)) edgeSpeciesCount.append(len(reactionModel.edge.species)) edgeReactionCount.append(len(reactionModel.edge.reactions)) execTime.append(time.time() - settings.initializationTime) from guppy import hpy hp = hpy() memoryUse.append(hp.heap().size / 1.0e6) logging.debug('Execution time: %s s' % (execTime[-1])) logging.debug('Memory used: %s MB' % (memoryUse[-1])) restartSize.append(os.path.getsize(os.path.join(settings.outputDirectory,'restart.pkl')) / 1.0e6) generateExecutionPlots(execTime, coreSpeciesCount, coreReactionCount, edgeSpeciesCount, edgeReactionCount, memoryUse, restartSize) logging.info('') # Write output file logging.info('MODEL GENERATION COMPLETED') logging.info('') logging.info('The final model core has %s species and %s reactions' % (len(reactionModel.core.species), len(reactionModel.core.reactions))) logging.info('The final model edge has %s species and %s reactions' % (len(reactionModel.edge.species), len(reactionModel.edge.reactions))) io.writeOutputFile(os.path.join(settings.outputDirectory,'output.xml'), reactionModel, reactionSystems) # Log end timestamp logging.info('RMG execution terminated at ' + time.asctime())
logging.info('') # Consider stopping gracefully if the next iteration might take us # past the wall time if settings.wallTime > 0 and len(execTime) > 1: t = execTime[-1] dt = execTime[-1] - execTime[-2] if t + 2 * dt > settings.wallTime: logging.info('MODEL GENERATION TERMINATED') logging.info('') logging.info('There is not enough time to complete the next iteration before the wall time is reached.') logging.info('The output model may be incomplete.') logging.info('') logging.info('The current model core has %s species and %s reactions' % (len(reactionModel.core.species), len(reactionModel.core.reactions))) logging.info('The current model edge has %s species and %s reactions' % (len(reactionModel.edge.species), len(reactionModel.edge.reactions))) io.writeOutputFile(os.path.join(settings.outputDirectory,'output.xml'), reactionModel, reactionSystems) return # Write output file logging.info('MODEL GENERATION COMPLETED') logging.info('') logging.info('The final model core has %s species and %s reactions' % (len(reactionModel.core.species), len(reactionModel.core.reactions))) logging.info('The final model edge has %s species and %s reactions' % (len(reactionModel.edge.species), len(reactionModel.edge.reactions))) io.writeOutputFile(os.path.join(settings.outputDirectory,'output.xml'), reactionModel, reactionSystems) # Log end timestamp logging.info('') logging.info('RMG execution terminated at ' + time.asctime()) ################################################################################