# 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

"""
Explanation: 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:
<br>
<center> $ H + O_2 \rightarrow O + OH$</center>
<center> $ O + H_2 \rightarrow H + OH$</center>
<center> $ H_2 + OH \rightarrow H_2O + H$</center>
<center> $ O + H_2O \rightarrow OH + OH$</center>
<center> $ HO_2 + H \rightarrow H_2 + O_2$</center>
<center> $ HO_2 + H \rightarrow OH + OH$</center>
<center> $ HO_2 + O \rightarrow O_2 + OH$</center>
<center> $ HO_2 + OH \rightarrow H_2O + O_2$</center>
<center> $ H_2O_2 + H \rightarrow H_2O + OH$</center>
<center> $ H_2O_2 + H \rightarrow HO_2 + H_2$</center>
<center> $ H_2O_2 + O \rightarrow OH + HO_2$</center>
(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.
End of explanation
"""


xml_filename = os.path.join(DATA_DIRECTORY, "rxnset_long.xml")
parser = Parser.ReactionParser(xml_filename)

"""
Explanation: 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).
End of explanation
"""


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

"""
Explanation: Now, we set the reaction conditions by inputing the concentrations and temperature of the reaction.
End of explanation
"""


rxnsys = ReactionSystems.ReactionSystem(parser.reaction_list,
                                        parser.NASA_poly_coefs,
                                        temperature,
                                        concentration)

"""
Explanation: 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.
End of explanation
"""


# 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]))

"""
Explanation: 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.
End of explanation
"""


print(rxnsys.involved_species.keys())

"""
Explanation: Note that the concentrations displayed are in the order of species defined by the reaction system object.
End of explanation
"""


# 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))

"""
Explanation: Finally, in the following lines, we compute the reaction rates for all the species in the reaction and display them.
End of explanation
"""


