.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/chemistry/loadmechanism.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_chemistry_loadmechanism.py: .. _ref_load_mechanism: ================================ Preprocess a gas-phase mechanism ================================ Before you can run a Chemkin simulation, you must load and preprocess the reaction mechanism data. The mechanism data consists of a reaction mechanism input file, a thermodynamic data file, and an optional transport data file. This example shows how to instantiate and preprocess a chemistry set, which is always the first task in running any PyChemkin simulation. PyChemkin allows several chemistry sets to coexist in the same Python project. However, only one chemistry set can be active at a time. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python :dedent: 1 .. GENERATED FROM PYTHON SOURCE LINES 43-45 Import PyChemkin packages and start the logger ============================================== .. GENERATED FROM PYTHON SOURCE LINES 45-61 .. code-block:: Python from pathlib import Path # import PyChemkin packages import ansys.chemkin.core as ck from ansys.chemkin.core import Color from ansys.chemkin.core.logger import logger # check the working directory current_dir = str(Path.cwd()) logger.debug("working directory: " + current_dir) # set PyChemkin verbose mode ck.set_verbose(True) .. GENERATED FROM PYTHON SOURCE LINES 62-67 Create a chemistry set ====================== The first mechanism to load is the GRI 3.0 mechanism for methane combustion. This mechanism and its associated data files come with the standard Ansys Chemkin installation in the ``/reaction/data`` directory. .. GENERATED FROM PYTHON SOURCE LINES 67-85 .. code-block:: Python # set mechanism directory (the default Chemkin mechanism data directory) data_dir = Path(ck.ansys_dir) / "reaction" / "data" mechanism_dir = data_dir # set mechanism input files # including the full file path is recommended # the gas-phase reaction mechanism file (GRI 3.0) chemfile = str(mechanism_dir / "grimech30_chem.inp") # the thermodynamic data file thermfile = str(mechanism_dir / "grimech30_thermo.dat") # the transport data file tranfile = str(mechanism_dir / "grimech30_transport.dat") # create a chemistry set based on the GRI 3.0 methane combustion mechanism MyGasMech = ck.Chemistry(chem=chemfile, therm=thermfile, tran=tranfile, label="GRI 3.0") .. GENERATED FROM PYTHON SOURCE LINES 86-88 Preprocess the chemistry set ============================ .. GENERATED FROM PYTHON SOURCE LINES 88-109 .. code-block:: Python # preprocess the mechanism files ierror = MyGasMech.preprocess() # display the preprocess status print() if ierror != 0: # When a non-zero value is returned from the process, check the text output files, # "chem.out," "tran.out," and "summary.out," for potential error messages about # the mechanism data. print(f"Preprocessing error encountered. Code = {ierror:d}.") print(f"See the summary file {MyGasMech.summaryfile} for details.") exit() else: Color.ckprint("OK", ["Preprocessing succeeded.", "!!!"]) print("Mechanism information:") print(f"Number of elements = {MyGasMech.number_elements:d}.") print(f"Number of gas species = {MyGasMech.number_species:d}.") print(f"Number of gas reactions = {MyGasMech.number_gas_reactions:d}.") .. GENERATED FROM PYTHON SOURCE LINES 110-112 Display the basic mechanism information ======================================= .. GENERATED FROM PYTHON SOURCE LINES 112-136 .. code-block:: Python print(f"\nElement and species information of mechanism {MyGasMech.label}") print("=" * 50) # extract element symbols as a list elelist = MyGasMech.element_symbols # get atomic masses as numpy 1D double array awt = MyGasMech.atomic_weight # print element information for k in range(len(elelist)): print(f"Element number {k + 1:3d}: {elelist[k]:16}. Mass = {awt[k]:f}.") print("=" * 50) # extract gas species symbols as a list specieslist = MyGasMech.species_symbols # get species molecular masses as numpy 1D double array wt = MyGasMech.species_molar_weight # print gas species information for k in range(len(specieslist)): print(f"Species number {k + 1:3d}: {specieslist[k]:16}. Mass = {wt[k]:f}.") print("=" * 50) .. GENERATED FROM PYTHON SOURCE LINES 137-154 Create a second chemistry set ============================= The second mechanism to load into the project is the C2-NOx mechanism for the combustion of C1-C2 hydrocarbons. This mechanism differs from the GRI mechanism in the sense that it is self-contained, that is, the thermodynamic and transport data of all species is included in the ``C2_NOx_SRK.inp`` mechanism input file, which sits in the same reaction data directory as the GRI mechanism input files. Use the same steps that were used to instantiate the first chemistry set to process any set of reaction mechanism files. Here, use a slightly different procedure to instantiate the second chemistry set. After you have created it, specify the reaction mechanism files one by one. The reaction mechanism file in this case contains all the necessary thermodynamic and transport data. Thus, there is no need to specify thermodynamic and transport data files. However, an additional step is required to instruct the preprocessor to include the transport data. .. GENERATED FROM PYTHON SOURCE LINES 154-171 .. code-block:: Python # set the second mechanism directory (the default Chemkin mechanism data directory) mechanism_dir = data_dir # create a chemistry set based on C2_NOx using an alternative method My2ndMech = ck.Chemistry(label="C2 NOx") # set mechanism input files individually # this mechanism file contains all the necessary thermodynamic and transport data # thus, there is no need to specify thermodynamic and transport data files My2ndMech.chemfile = str(mechanism_dir / "C2_NOx_SRK.inp") # instruct the preprocessor to include the transport properties # only when the mechanism file contains all the transport data My2ndMech.preprocess_transportdata() .. GENERATED FROM PYTHON SOURCE LINES 172-174 Preprocess the second chemistry set =================================== .. GENERATED FROM PYTHON SOURCE LINES 174-195 .. code-block:: Python # preprocess the second mechanism file ierror = My2ndMech.preprocess() # display the preprocess status print() if ierror != 0: # When a non-zero value is returned from the process, check the text output files, # "chem.out," "tran.out," and "summary.out," for potential error messages about # the mechanism data. print(f"Preprocessing error encountered. Code = {ierror:d}.") print(f"See the summary file {My2ndMech.summaryfile} for details.") exit() else: Color.ckprint("OK", ["Preprocessing succeeded.", "!!!"]) print("Mechanism information:") print(f"Number of elements = {My2ndMech.mm:d}.") print(f"Number of gas species = {My2ndMech.kk:d}.") print(f"Number of gas reactions = {My2ndMech.ii_gas:d}.") .. GENERATED FROM PYTHON SOURCE LINES 196-198 Display the basic mechanism information ======================================= .. GENERATED FROM PYTHON SOURCE LINES 198-221 .. code-block:: Python print(f"\nElement and species information of mechanism {My2ndMech.label}") print("=" * 50) # extract element symbols as a list elelist = My2ndMech.element_symbols # get atomic masses as numpy 1D double array awt = My2ndMech.awt # print element information for k in range(len(elelist)): print(f"Element # {k + 1:3d}: {elelist[k]:16}. Mass = {awt[k]:f}.") print("=" * 50) # extract gas species symbols as a list specieslist = My2ndMech.species_symbols # get species molecular masses as numpy 1D double array wt = My2ndMech.wt # print gas species information for k in range(len(specieslist)): print(f"Species # {k + 1:3d}: {specieslist[k]:16}. Mass = {wt[k]:f}.") print("=" * 50) .. _sphx_glr_download_examples_chemistry_loadmechanism.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: loadmechanism.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: loadmechanism.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: loadmechanism.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_