A Cantera Simulation Using RMG-Py


In [ ]:
from rmgpy.chemkin import *
from rmgpy.tools.canteraModel import *
from IPython.display import display, Image

Load the species and reaction from the RMG-generated chemkin file chem_annotated.inp and species_dictionary.txt file found in your chemkin folder after running a job.


In [ ]:
speciesList, reactionList = loadChemkinFile('data/minimal_model/chem_annotated.inp',
                                            'data/minimal_model/species_dictionary.txt',
                                           'data/minimal_model/tran.dat')

Set a few conditions for how to react the system


In [ ]:
# Find the species: ethane
speciesDict = getRMGSpeciesFromSMILES(['CC'], speciesList)
ethane = speciesDict['CC']

reactorType = 'IdealGasReactor'
molFracList=[{ethane: 1}]
Tlist = ([1300,1500,2000],'K')
Plist = ([1],'atm')
reactionTime = (0.5, 'ms')

In [ ]:
# Create cantera object, loading in the species and reactions
job = Cantera(speciesList=speciesList, reactionList=reactionList, outputDirectory='temp')
# The cantera file must be created from an associated chemkin file

# We can either load the Model from the initialized set of rmg species and reactions
job.loadModel()

# Or load it from a chemkin file by uncommenting the following line:
#job.loadChemkinModel('data/minimal_model/chem_annotated.inp',transportFile='data/minimal_model/tran.dat')

# Generate the conditions based on the settings we declared earlier
job.generateConditions('IdealGasReactor', reactionTime, molFracList, Tlist, Plist)
# Simulate and plot
alldata = job.simulate()
job.plot(alldata)

In [ ]:
# We can view the cantera model Solution's species and reactions
ctSpecies = job.model.species()
ctReactions = job.model.reactions()

# We can view a cantera species or reaction object from this
ct_ethane = ctSpecies[4]
ct_rxn = ctReactions[0]

print ct_ethane
print ct_rxn

In [ ]:
# We can also do things like modifying the cantera species thermo and reaction kinetics through modifying the
# RMG objects first, then using the `modifyReactionKinetics` or `modifySpeciesThermo` functions

# Alter the RMG objects in place, lets pick ethane and the first reaction
rmg_ethane = speciesDict['CC']
rmg_ethane.thermo.changeBaseEnthalpy(2*4184)  # Change base enthalpy by 2 kcal/mol

rmg_rxn = reactionList[0]
rmg_rxn.kinetics.changeRate(4)  # Change A factor by multiplying by a factor of 4

# Take a look at the state of the cantera model before and after
print 'Cantera Model: Before'
ctSpecies = job.model.species()
ctReactions = job.model.reactions()
print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)
print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)

# Now use the altered RMG objects to modify the kinetics and thermo
job.modifyReactionKinetics(0, rmg_rxn)
job.modifySpeciesThermo(4, rmg_ethane)

# If we modify thermo, the cantera model must be refreshed.  If only kinetics are modified, this does not need to be done.
job.refreshModel()

print ''
print 'Cantera Model: After'
ctSpecies = job.model.species()
ctReactions = job.model.reactions()
print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)
print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)

In [ ]:
# Show the plots in the ipython notebook
for i, condition in enumerate(job.conditions):
    print 'Condition {0}'.format(i+1)
    display(Image(filename="temp/{0}_mole_fractions.png".format(i+1)))