<jupyter_start><jupyter_text>A Cantera Simulation with Reaction Sensitivity: Comparison with Native RMG Sensitivity Analysis and CHEMKIN Sensitivity AnalysisNote that this requires Cantera with SUNDIALS installed for sensitivity support.  If you are using Anaconda,Cantera version >= 2.3.0 is required Let's check what version of Cantera you have installed<jupyter_code>import cantera
print cantera.__version__
from rmgpy.chemkin import *
from rmgpy.tools.canteraModel import *
from rmgpy.tools.plot import parseCSVData
from rmgpy.species import Species
from IPython.display import display, Image<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>speciesList, reactionList = loadChemkinFile('data/minimal_model/chem_annotated.inp',
                                            'data/minimal_model/species_dictionary.txt',
                                           'data/minimal_model/tran.dat')<jupyter_output><empty_output><jupyter_text>Set the reaction conditions<jupyter_code># Find the species: ethane and methane
user_ethane = Species().fromSMILES('CC')
user_methane = Species().fromSMILES('C')
speciesDict = getRMGSpeciesFromUserSpecies([user_ethane, user_methane], speciesList)
ethane = speciesDict[user_ethane]
methane = speciesDict[user_methane]
sensitiveSpecies = [ethane, methane]

#reactorTypeList = ['IdealGasReactor']
reactorTypeList = ['IdealGasConstPressureTemperatureReactor']
molFracList=[{ethane: 1}]
Tlist = ([1300],'K')#,1500,2000],'K')
Plist = ([1],'atm')
reactionTimeList = ([0.5], 'ms')
# Create cantera object, loading in the species and reactions
job = Cantera(speciesList=speciesList, reactionList=reactionList, outputDirectory='temp', sensitiveSpecies=sensitiveSpecies)
# 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(reactorTypeList, reactionTimeList, molFracList, Tlist, Plist)
# Simulate and plot
alldata = job.simulate()
job.plot(alldata)
# Show the plots in the ipython notebook
for i, condition in enumerate(job.conditions):
    print 'Cantera Simulation: Condition {0} Mole Fractions'.format(i+1)
    display(Image(filename="temp/{0}_mole_fractions.png".format(i+1)))
    
    print 'Cantera Simulation: Condition {0} Ethane Reaction Sensitivity'.format(i+1)
    display(Image(filename="temp/{0}_ethane(1)_sensitivity.png".format(i+1)))
# Let's compare against the same simulation in RMG
# Create an input file

input = '''
database(
    thermoLibraries = ['primaryThermoLibrary'],
    reactionLibraries = [],
    seedMechanisms = [],
    kineticsDepositories = 'default',
    kineticsFamilies = 'default',
    kineticsEstimator = 'rate rules',
)

species(
    label = "ethane",
    structure = SMILES("CC"))

species(
    label = "methane",
    structure = SMILES("C"))

simpleReactor(
    temperature = (1300,"K"), 
    pressure = (1,"atm"),
    initialMoleFractions={
        "ethane": 1
    },
    terminationTime = (0.5,"ms"),
    sensitivity=['ethane','methane']
)

model(
    toleranceMoveToCore = 0.04,
)

options(
    saveSimulationProfiles = True,
)

'''
f = open('temp/temp_input.py', 'wb')
f.write(input)
f.close()
from rmgpy.tools.sensitivity import runSensitivity
runSensitivity('temp/temp_input.py', 'data/minimal_model/chem_annotated.inp', 'data/minimal_model/species_dictionary.txt')
print 'RMG Native Simulation: Species Mole Fractions'
display(Image(filename="temp/solver/simulation_1_19.png"))

print 'RMG Native Simulation: Ethane Reaction Sensitivity'
display(Image(filename="temp/solver/sensitivity_1_SPC_1_reactions.png"))

# Let's also compare against the same simulation and sensitivity analysis that was conducted in CHEMKIN
# and saved as a .csv file
time, dataList = parseCSVData('data/minimal_model/chemkin_mole_fractions.csv')
SimulationPlot(xVar=time,yVar=dataList).plot('temp/chemkin_mole_fractions.png')
print 'CHEMKIN Simulation: Mole Fractions'
display(Image(filename="temp/chemkin_mole_fractions.png"))
time, dataList = parseCSVData('data/minimal_model/chemkin_sensitivity_ethane.csv')
ReactionSensitivityPlot(xVar=time,yVar=dataList).barplot('temp/chemkin_ethane_sensitivity.png')
print 'CHEMKIN Simulation: Ethane Reaction Sensitivity'
display(Image(filename="temp/chemkin_ethane_sensitivity.png"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10689582896667366, 'fraction_numerical': 0.009222385244183609, 'mean_word_length': 5.817142857142857, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '595784', 'license': 'MIT', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1463, 'n_words': 355, 'path': 'ipython/canteraSensitivityComparison.ipynb', 'repo_name': 'cainja/RMG-Py', 'size': '4771'}