ChemKinLib Basic Tutorial

Welcome to ChemKinLib! Here is a quick tutorial on how to use select classes and functions.

Example: Irreversible Reactions

In the following example, we go through the steps to compute the reaction rates for this set of reactions:


$ H + O_2 \rightarrow O + OH$
$ O + H_2 \rightarrow H + OH$
$ H_2 + OH \rightarrow H_2O + H$
$ O + H_2O \rightarrow OH + OH$
$ HO_2 + H \rightarrow H_2 + O_2$
$ HO_2 + H \rightarrow OH + OH$
$ HO_2 + O \rightarrow O_2 + OH$
$ HO_2 + OH \rightarrow H_2O + O_2$
$ H_2O_2 + H \rightarrow H_2O + OH$
$ H_2O_2 + H \rightarrow HO_2 + H_2$
$ H_2O_2 + O \rightarrow OH + HO_2$

(Sanity check: all of these reactions are elementary and irreversible.)

Notes:

  • We have included some example .xml files in our "data directory" (we will use one of them in this particular example). Thus, we will import DATA_DIRECTORY from config.py.

  • For computing reaction rates of reversible reactions, much of the code remains the same. For an example, please see script rev_rxns.py.


In [1]:
# Import modules
import numpy
import os
from chemkinlib.utils import Parser
from chemkinlib.utils import visualizer
from chemkinlib.reactions import ReactionSystems
from chemkinlib.config import DATA_DIRECTORY

Here, we define the input .xml file (found in the data directory) and initiate a reaction parser object in order to process the input .xml file (create a list of reaction objects and collect relevant NASA polynomial coefficients).


In [2]:
xml_filename = os.path.join(DATA_DIRECTORY, "rxnset_long.xml")
parser = Parser.ReactionParser(xml_filename)

Now, we set the reaction conditions by inputing the concentrations and temperature of the reaction.


In [3]:
concentration = ({'H':1, 'H2':1, 'H2O':0,
                  'H2O2':1, 'HO2':1, 'O':1,
                  'O2':1, 'OH':1}) # molar concentrations
temperature = 1000 # Kelvin

With the parser (that has the list of correctly classified reactions and relevant NASA polynomial coefficients) and the reaction conditions defined by the user, we set up the reaction system.


In [4]:
rxnsys = ReactionSystems.ReactionSystem(parser.reaction_list,
                                        parser.NASA_poly_coefs,
                                        temperature,
                                        concentration)

With the reaction system, we can compute the change in specie concentrations over time (20 time steps with a step size of 1e-15). We display the concentrations of all the species.


In [5]:
# Compute the concentration change with timestep
dt = 1e-15
for t in range(20):
    print("The concentration after {} timestep is:".format(t))
    print(list(rxnsys.step(dt)[1]))


The concentration after 0 timestep is:
[0.8755471252780187, 0.98920942127814959, 1.164315871484559, 1.0225054103622886, 0.050369635956676997, 1.0471415761281715, 0.88743500842478007, 0.96347595108735573]
The concentration after 1 timestep is:
[0.77479330020738346, 0.97999747322334629, 1.293202635913105, 1.0387229328677954, 0.099767424713190861, 1.0903223914126201, 0.79136433460757871, 0.93182950705498024]
The concentration after 2 timestep is:
[0.6921443066657863, 0.97205910843888688, 1.395937883709192, 1.050123339781821, 0.14746771495670602, 1.1295592003411774, 0.70868119206489288, 0.90402725404153739]
The concentration after 3 timestep is:
[0.62357786910340296, 0.96516066949213242, 1.4790213040785041, 1.057775215810707, 0.19311185660114297, 1.165033715725778, 0.63701205638227187, 0.87930731280606045]
The concentration after 4 timestep is:
[0.56613694554564886, 0.95912220087493216, 1.5471052480963339, 1.0624707595405316, 0.23655714085637256, 1.1970014330828782, 0.57451053844239597, 0.85709573356090651]
The concentration after 5 timestep is:
[0.51760520657548592, 0.95380386945099938, 1.6035826704180498, 1.0648076840569092, 0.27778767886616024, 1.2257435069148013, 0.51971737027486231, 0.83695201344273162]
The concentration after 6 timestep is:
[0.4762930926200854, 0.94909585371159633, 1.6509691062555862, 1.0652439886894018, 0.31686087741384278, 1.2515415576187066, 0.47146297846372165, 0.81853254522705887]
The concentration after 7 timestep is:
[0.44089315540079421, 0.94491090091577401, 1.6911569806569779, 1.0641354596650194, 0.35387488829562042, 1.2746649131038588, 0.42879823590296123, 0.80156546605899348]
The concentration after 8 timestep is:
[0.41038003770031595, 0.94117885989929462, 1.7255886215500813, 1.0617618844564287, 0.38894867423713458, 1.2953646587583068, 0.3909443034343989, 0.78583295996403868]
The concentration after 9 timestep is:
[0.38393993049150382, 0.93784265439997905, 1.7553762537365338, 1.058345718301259, 0.42220979427796146, 1.3138714142200969, 0.35725567853180895, 0.77115855604085626]
The concentration after 10 timestep is:
[0.36091992794583205, 0.93485530158220553, 1.7813866702380241, 1.0540655979508828, 0.45378697638634069, 1.3303951221289054, 0.32719255439392014, 0.75739784937388832]
The concentration after 11 timestep is:
[0.34079108105295775, 0.93217768943223578, 1.8043019156853872, 1.0490662720745108, 0.48380568862744006, 1.3451258849153924, 0.30029985456639668, 0.74443161364567834]
The concentration after 12 timestep is:
[0.32312105096559407, 0.92977690718182249, 1.8246633949215629, 1.0434659978063838, 0.5123856003526589, 1.3582353059131529, 0.27619112792289019, 0.73216061493593365]
The concentration after 13 timestep is:
[0.30755359883992428, 0.92762498094016976, 1.842904347731366, 1.0373621182274742, 0.53963923719366758, 1.3698780296495126, 0.2545360322493429, 0.7205016551685417]
The concentration after 14 timestep is:
[0.29379301625817156, 0.92569790812134223, 1.8593740399423784, 1.0308353157957098, 0.56567138782851711, 1.3801933129778827, 0.23505050160099819, 0.70938451747499909]
The concentration after 15 timestep is:
[0.28159217433584433, 0.92397491372005625, 1.8743559785264294, 1.0239528897887222, 0.59057897950438143, 1.3893065375773836, 0.21748894454284451, 0.69874958200433712]
The concentration after 16 timestep is:
[0.27074325617763412, 0.92243787248315834, 1.8880817632421207, 1.0167713059302266, 0.61445124005899976, 1.3973306197825088, 0.20163799604890895, 0.68854594627644172]
The concentration after 17 timestep is:
[0.26107050185218544, 0.92107085603650385, 1.900741716676013, 1.0093381974288056, 0.63737002872550108, 1.4043672996708527, 0.18731147005708679, 0.67872992955305056]
The concentration after 18 timestep is:
[0.25242447872056334, 0.91985977481541226, 1.9124931112011894, 1.0016939484072873, 0.6594102597276974, 1.4105083058888188, 0.17434624866978354, 0.66926387256924724]
The concentration after 19 timestep is:
[0.24467751922389006, 0.91879209244467652, 1.9234665862801421, 0.99387295649328244, 0.68064036984083676, 1.41583640054383, 0.16259890851895309, 0.6601151666543883]

Note that the concentrations displayed are in the order of species defined by the reaction system object.


In [12]:
print(rxnsys.involved_species.keys())


dict_keys(['H', 'O', 'OH', 'H2', 'H2O', 'O2', 'HO2', 'H2O2'])

Finally, in the following lines, we compute the reaction rates for all the species in the reaction and display them.


In [6]:
# Compute and sort reaction rates (by species)
rxnrates_dict = rxnsys.sort_reaction_rates()

# Display reaction rates by species
for specie, rxnrate in rxnrates_dict.items():
    print("d[{0}]/dt : \t {1:e}".format(specie, rxnrate))


d[H]/dt : 	 -7.335299e+12
d[O]/dt : 	 -9.998210e+11
d[OH]/dt : 	 1.062245e+13
d[H2]/dt : 	 -7.899184e+12
d[H2O]/dt : 	 2.084634e+13
d[O2]/dt : 	 4.947175e+12
d[HO2]/dt : 	 -1.118186e+13
d[H2O2]/dt : 	 -8.999801e+12

Special Feature: Visualization

  • We also included an option to visualize the reaction (changes in concentration) using Graphviz, so please refer to scripts rev_rxns.py and irrev_rxns.py for examples.