Skip to content

Commit

Permalink
Merge pull request #1735 from ReactionMechanismGenerator/ipython
Browse files Browse the repository at this point in the history
Update IPython notebooks
  • Loading branch information
amarkpayne authored Oct 26, 2019
2 parents 7147277 + 8a057a4 commit f70d761
Show file tree
Hide file tree
Showing 19 changed files with 1,774 additions and 2,512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"outputs": [],
"source": [
"import cantera\n",
"print cantera.__version__ # Check Cantera version"
"print(cantera.__version__) # Check Cantera version"
]
},
{
Expand All @@ -31,10 +31,10 @@
"\n",
"from IPython.display import display, Image\n",
"\n",
"from rmgpy.chemkin import loadChemkinFile\n",
"from rmgpy.chemkin import load_chemkin_file\n",
"from rmgpy.species import Species\n",
"from rmgpy.tools.canteraModel import Cantera, getRMGSpeciesFromUserSpecies\n",
"from rmgpy.tools.plot import SimulationPlot, ReactionSensitivityPlot, parseCSVData\n",
"from rmgpy.tools.canteraModel import Cantera, get_rmg_species_from_user_species\n",
"from rmgpy.tools.plot import SimulationPlot, ReactionSensitivityPlot, parse_csv_data\n",
"from rmgpy.tools.simulate import run_simulation"
]
},
Expand All @@ -51,9 +51,9 @@
"metadata": {},
"outputs": [],
"source": [
"speciesList, reactionList = loadChemkinFile('./data/ethane_model/chem_annotated.inp',\n",
" './data/ethane_model/species_dictionary.txt',\n",
" './data/ethane_model/tran.dat')"
"species_list, reaction_list = load_chemkin_file('./data/ethane_model/chem_annotated.inp',\n",
" './data/ethane_model/species_dictionary.txt',\n",
" './data/ethane_model/tran.dat')"
]
},
{
Expand All @@ -70,19 +70,19 @@
"outputs": [],
"source": [
"# Find the species: ethane and methane\n",
"user_ethane = Species().fromSMILES('CC')\n",
"user_methane = Species().fromSMILES('C')\n",
"speciesDict = getRMGSpeciesFromUserSpecies([user_ethane, user_methane], speciesList)\n",
"ethane = speciesDict[user_ethane]\n",
"methane = speciesDict[user_methane]\n",
"sensitiveSpecies = [ethane, methane]\n",
"user_ethane = Species().from_smiles('CC')\n",
"user_methane = Species().from_smiles('C')\n",
"species_dict = get_rmg_species_from_user_species([user_ethane, user_methane], species_list)\n",
"ethane = species_dict[user_ethane]\n",
"methane = species_dict[user_methane]\n",
"sensitive_species = [ethane, methane]\n",
"\n",
"#reactorTypeList = ['IdealGasReactor']\n",
"reactorTypeList = ['IdealGasConstPressureTemperatureReactor']\n",
"molFracList=[{ethane: 1}]\n",
"#reactor_type_list = ['IdealGasReactor']\n",
"reactor_type_list = ['IdealGasConstPressureTemperatureReactor']\n",
"mol_frac_list = [{ethane: 1}]\n",
"Tlist = ([1300], 'K')\n",
"Plist = ([1], 'bar')\n",
"reactionTimeList = ([0.5], 'ms')"
"reaction_time_list = ([0.5], 'ms')"
]
},
{
Expand All @@ -92,17 +92,17 @@
"outputs": [],
"source": [
"# Create cantera object, loading in the species and reactions\n",
"job = Cantera(speciesList=speciesList, reactionList=reactionList, outputDirectory='temp', sensitiveSpecies=sensitiveSpecies)\n",
"job = Cantera(species_list=species_list, reaction_list=reaction_list, output_directory='temp', sensitive_species=sensitive_species)\n",
"# The cantera file must be created from an associated chemkin file\n",
"\n",
"# We can either load the Model from the initialized set of rmg species and reactions\n",
"job.loadModel()\n",
"job.load_model()\n",
"\n",
"# Or load it from a chemkin file by uncommenting the following line:\n",
"#job.loadChemkinModel('data/minimal_model/chem_annotated.inp',transportFile='data/minimal_model/tran.dat')\n",
"#job.load_chemkin_model('data/ethane_model/chem_annotated.inp',transport_file='data/ethane_model/tran.dat')\n",
"\n",
"# Generate the conditions based on the settings we declared earlier\n",
"job.generateConditions(reactorTypeList, reactionTimeList, molFracList, Tlist, Plist)"
"job.generate_conditions(reactor_type_list, reaction_time_list, mol_frac_list, Tlist, Plist)"
]
},
{
Expand All @@ -117,10 +117,10 @@
"\n",
"# Show the plots in the ipython notebook\n",
"for i, condition in enumerate(job.conditions):\n",
" print 'Cantera Simulation: Condition {0} Species Mole Fractions'.format(i+1)\n",
" print('Cantera Simulation: Condition {0} Species Mole Fractions'.format(i+1))\n",
" display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))\n",
" \n",
" print 'Cantera Simulation: Condition {0} Ethane Reaction Sensitivity'.format(i+1)\n",
" print('Cantera Simulation: Condition {0} Ethane Reaction Sensitivity'.format(i+1))\n",
" display(Image(filename=\"temp/{0}_ethane(1)_sensitivity.png\".format(i+1)))"
]
},
Expand Down Expand Up @@ -149,10 +149,10 @@
"metadata": {},
"outputs": [],
"source": [
"print 'RMG Native Simulation: Species Mole Fractions'\n",
"print('RMG Native Simulation: Species Mole Fractions')\n",
"display(Image(filename=\"./temp/solver/simulation_1_27.png\"))\n",
"\n",
"print 'RMG Native Simulation: Ethane Reaction Sensitivity'\n",
"print('RMG Native Simulation: Ethane Reaction Sensitivity')\n",
"display(Image(filename=\"./temp/solver/sensitivity_1_SPC_1_reactions.png\"))"
]
},
Expand All @@ -164,14 +164,14 @@
"source": [
"# Let's also compare against the same simulation and sensitivity analysis that was conducted in CHEMKIN\n",
"# and saved as a .csv file\n",
"time, dataList = parseCSVData('./data/ethane_model/chemkin_mole_fractions.csv')\n",
"SimulationPlot(xVar=time, yVar=dataList, numSpecies=10).plot('./temp/chemkin_mole_fractions.png')\n",
"print 'CHEMKIN Simulation: Species Mole Fractions'\n",
"time, data_list = parse_csv_data('./data/ethane_model/chemkin_mole_fractions.csv')\n",
"SimulationPlot(x_var=time, y_var=data_list, num_species=10).plot('./temp/chemkin_mole_fractions.png')\n",
"print('CHEMKIN Simulation: Species Mole Fractions')\n",
"display(Image(filename=\"./temp/chemkin_mole_fractions.png\"))\n",
"\n",
"time, dataList = parseCSVData('./data/ethane_model/chemkin_sensitivity_ethane.csv')\n",
"ReactionSensitivityPlot(xVar=time, yVar=dataList, numReactions=10).barplot('./temp/chemkin_sensitivity_ethane.png')\n",
"print 'CHEMKIN Simulation: Ethane Reaction Sensitivity'\n",
"time, data_list = parse_csv_data('./data/ethane_model/chemkin_sensitivity_ethane.csv')\n",
"ReactionSensitivityPlot(x_var=time, y_var=data_list, num_reactions=10).barplot('./temp/chemkin_sensitivity_ethane.png')\n",
"print('CHEMKIN Simulation: Ethane Reaction Sensitivity')\n",
"display(Image(filename=\"./temp/chemkin_sensitivity_ethane.png\"))"
]
},
Expand All @@ -185,21 +185,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python [conda env:rmg_env]",
"language": "python",
"name": "python2"
"name": "conda-env-rmg_env-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"source": [
"from IPython.display import display, Image\n",
"\n",
"from rmgpy.chemkin import loadChemkinFile\n",
"from rmgpy.tools.canteraModel import Cantera, getRMGSpeciesFromUserSpecies\n",
"from rmgpy.chemkin import load_chemkin_file\n",
"from rmgpy.tools.canteraModel import Cantera, get_rmg_species_from_user_species\n",
"from rmgpy.species import Species"
]
},
Expand All @@ -33,9 +33,9 @@
"metadata": {},
"outputs": [],
"source": [
"speciesList, reactionList = loadChemkinFile('data/ethane_model/chem_annotated.inp',\n",
" 'data/ethane_model/species_dictionary.txt',\n",
" 'data/ethane_model/tran.dat')"
"species_list, reaction_list = load_chemkin_file('data/ethane_model/chem_annotated.inp',\n",
" 'data/ethane_model/species_dictionary.txt',\n",
" 'data/ethane_model/tran.dat')"
]
},
{
Expand All @@ -52,15 +52,15 @@
"outputs": [],
"source": [
"# Find the species: ethane\n",
"user_ethane=Species().fromSMILES('CC')\n",
"speciesDict = getRMGSpeciesFromUserSpecies([user_ethane], speciesList)\n",
"ethane = speciesDict[user_ethane]\n",
"user_ethane=Species().from_smiles('CC')\n",
"species_dict = get_rmg_species_from_user_species([user_ethane], species_list)\n",
"ethane = species_dict[user_ethane]\n",
"\n",
"reactorTypeList = ['IdealGasReactor']\n",
"molFracList=[{ethane: 1}]\n",
"Tlist = ([1300,1500,2000], 'K')\n",
"reactor_type_list = ['IdealGasReactor']\n",
"mol_frac_list=[{ethane: 1}]\n",
"Tlist = ([1300, 1500, 2000], 'K')\n",
"Plist = ([1], 'bar')\n",
"reactionTimeList = ([0.5], 'ms')"
"reaction_time_list = ([0.5], 'ms')"
]
},
{
Expand All @@ -70,17 +70,17 @@
"outputs": [],
"source": [
"# Create cantera object, loading in the species and reactions\n",
"job = Cantera(speciesList=speciesList, reactionList=reactionList, outputDirectory='temp')\n",
"job = Cantera(species_list=species_list, reaction_list=reaction_list, output_directory='temp')\n",
"# The cantera file must be created from an associated chemkin file\n",
"\n",
"# We can either load the Model from the initialized set of rmg species and reactions\n",
"job.loadModel()\n",
"job.load_model()\n",
"\n",
"# Or load it from a chemkin file by uncommenting the following line:\n",
"#job.loadChemkinModel('data/minimal_model/chem_annotated.inp',transportFile='data/minimal_model/tran.dat')\n",
"#job.load_chemkin_model('data/ethane_model/chem_annotated.inp',transport_file='data/ethane_model/tran.dat')\n",
"\n",
"# Generate the conditions based on the settings we declared earlier\n",
"job.generateConditions(reactorTypeList, reactionTimeList, molFracList, Tlist, Plist)"
"job.generate_conditions(reactor_type_list, reaction_time_list, mol_frac_list, Tlist, Plist)"
]
},
{
Expand All @@ -95,7 +95,7 @@
"\n",
"# Show the plots in the ipython notebook\n",
"for i, condition in enumerate(job.conditions):\n",
" print 'Condition {0}'.format(i+1)\n",
" print('Condition {0}'.format(i+1))\n",
" display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))"
]
},
Expand All @@ -106,15 +106,15 @@
"outputs": [],
"source": [
"# We can get the cantera model Solution's species and reactions\n",
"ctSpecies = job.model.species()\n",
"ctReactions = job.model.reactions()\n",
"ct_species = job.model.species()\n",
"ct_reactions = job.model.reactions()\n",
"\n",
"# We can view a cantera species or reaction object from this\n",
"ct_ethane = ctSpecies[4]\n",
"ct_rxn = ctReactions[0]\n",
"ct_ethane = ct_species[4]\n",
"ct_rxn = ct_reactions[0]\n",
"\n",
"print ct_ethane\n",
"print ct_rxn"
"print(ct_ethane)\n",
"print(ct_rxn)"
]
},
{
Expand All @@ -127,32 +127,32 @@
"# RMG objects first, then using the `modifyReactionKinetics` or `modifySpeciesThermo` functions\n",
"\n",
"# Alter the RMG objects in place, lets pick ethane and the first reaction\n",
"rmg_ethane = speciesDict[user_ethane]\n",
"rmg_ethane.thermo.changeBaseEnthalpy(2*4184) # Change base enthalpy by 2 kcal/mol\n",
"rmg_ethane = species_dict[user_ethane]\n",
"rmg_ethane.thermo.change_base_enthalpy(2*4184) # Change base enthalpy by 2 kcal/mol\n",
"\n",
"rmg_rxn = reactionList[0]\n",
"rmg_rxn.kinetics.changeRate(4) # Change A factor by multiplying by a factor of 4\n",
"rmg_rxn = reaction_list[0]\n",
"rmg_rxn.kinetics.change_rate(4) # Change A factor by multiplying by a factor of 4\n",
"\n",
"# Take a look at the state of the cantera model before and after\n",
"print 'Cantera Model: Before'\n",
"ctSpecies = job.model.species()\n",
"ctReactions = job.model.reactions()\n",
"print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)\n",
"print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)\n",
"print('Cantera Model: Before')\n",
"ct_species = job.model.species()\n",
"ct_reactions = job.model.reactions()\n",
"print('Ethane Thermo = {} kcal/mol'.format(ct_species[4].thermo.h(300)/1000/4184))\n",
"print('Reaction 1 Kinetics = {}'.format(ct_reactions[0].rate))\n",
"\n",
"# Now use the altered RMG objects to modify the kinetics and thermo\n",
"job.modifyReactionKinetics(0, rmg_rxn)\n",
"job.modifySpeciesThermo(4, rmg_ethane, useChemkinIdentifier = True)\n",
"job.modify_reaction_kinetics(0, rmg_rxn)\n",
"job.modify_species_thermo(4, rmg_ethane, use_chemkin_identifier = True)\n",
"\n",
"# If we modify thermo, the cantera model must be refreshed. If only kinetics are modified, this does not need to be done.\n",
"job.refreshModel()\n",
"job.refresh_model()\n",
"\n",
"print ''\n",
"print 'Cantera Model: After'\n",
"ctSpecies = job.model.species()\n",
"ctReactions = job.model.reactions()\n",
"print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)\n",
"print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)"
"print('')\n",
"print('Cantera Model: After')\n",
"ct_species = job.model.species()\n",
"ct_reactions = job.model.reactions()\n",
"print('Ethane Thermo = {} kcal/mol'.format(ct_species[4].thermo.h(300)/1000/4184))\n",
"print('Reaction 1 Kinetics = {}'.format(ct_reactions[0].rate))"
]
},
{
Expand All @@ -167,7 +167,7 @@
"\n",
"# Show the plots in the ipython notebook\n",
"for i, condition in enumerate(job.conditions):\n",
" print 'Condition {0}'.format(i+1)\n",
" print('Condition {0}'.format(i+1))\n",
" display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))"
]
},
Expand All @@ -181,21 +181,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python [conda env:rmg_env]",
"language": "python",
"name": "python2"
"name": "conda-env-rmg_env-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit f70d761

Please sign in to comment.