Welcome to ChemKinLib! Here is a quick tutorial on how to use select classes and functions.
In the following example, we go through the steps to compute the reaction rates for this set of reactions:
(Sanity check: all of these reactions are elementary and irreversible.)
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]))
Note that the concentrations displayed are in the order of species defined by the reaction system object.
In [12]:
print(rxnsys.involved_species.keys())
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))