
:html_theme.sidebar_secondary.remove:

.. py:currentmodule:: cantera


.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/python/kinetics/extract_submechanism.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_python_kinetics_extract_submechanism.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_python_kinetics_extract_submechanism.py:


Extracting a sub-mechanism
==========================

An example demonstrating how to use Species and Reaction objects to
programmatically extract a reaction submechanism. In this example, the CO/H2
oxidation reactions are extracted from the GRI 3.0 mechanism.

To test the submechanism, a premixed CO/H2 flame is simulated using the original
mechanism and the submechanism, which demonstrates that the submechanism
contains all of the important species and reactions.

Requires: cantera >= 2.6.0, matplotlib >= 2.0

.. tags:: Python, kinetics, combustion, 1D flow, editing mechanisms, plotting

.. GENERATED FROM PYTHON SOURCE LINES 17-26

.. code-block:: Python


    from timeit import default_timer
    import cantera as ct
    import matplotlib.pyplot as plt

    input_file = 'gri30.yaml'
    all_species = ct.Species.list_from_file(input_file)
    species = []








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Filter species.

.. GENERATED FROM PYTHON SOURCE LINES 28-45

.. code-block:: Python

    for S in all_species:
        comp = S.composition
        if 'C' in comp and 'H' in comp:
            # Exclude all hydrocarbon species
            continue
        if 'N' in comp and comp != {'N': 2}:
            # Exclude all nitrogen compounds except for N2
            continue
        if 'Ar' in comp:
            # Exclude Argon
            continue

        species.append(S)

    species_names = {S.name for S in species}
    print('Species: {0}'.format(', '.join(S.name for S in species)))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Species: H2, H, O, O2, OH, H2O, HO2, H2O2, C, CO, CO2, N2




.. GENERATED FROM PYTHON SOURCE LINES 46-47

Filter reactions, keeping only those that only involve the selected species.

.. GENERATED FROM PYTHON SOURCE LINES 47-69

.. code-block:: Python

    ref_phase = ct.Solution(thermo='ideal-gas', kinetics='gas', species=all_species)
    all_reactions = ct.Reaction.list_from_file(input_file, ref_phase)
    reactions = []

    print('\nReactions:')
    for R in all_reactions:
        if not all(reactant in species_names for reactant in R.reactants):
            continue

        if not all(product in species_names for product in R.products):
            continue

        reactions.append(R)
        print(R.equation)
    print('\n')

    gas1 = ct.Solution(input_file)
    gas2 = ct.Solution(name="gri30-CO-H2-submech",
                       thermo="ideal-gas", kinetics="gas",
                       transport_model="mixture-averaged",
                       species=species, reactions=reactions)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Reactions:
    2 O + M <=> O2 + M
    H + O + M <=> OH + M
    H2 + O <=> H + OH
    HO2 + O <=> O2 + OH
    H2O2 + O <=> HO2 + OH
    CO + O (+M) <=> CO2 (+M)
    CO + O2 <=> CO2 + O
    H + O2 + M <=> HO2 + M
    H + O2 + O2 <=> HO2 + O2
    H + O2 + H2O <=> HO2 + H2O
    H + O2 + N2 <=> HO2 + N2
    H + O2 + AR <=> HO2 + AR
    H + O2 <=> O + OH
    2 H + M <=> H2 + M
    2 H + H2 <=> H2 + H2
    2 H + H2O <=> H2 + H2O
    2 H + CO2 <=> H2 + CO2
    H + OH + M <=> H2O + M
    H + HO2 <=> H2O + O
    H + HO2 <=> H2 + O2
    H + HO2 <=> 2 OH
    H + H2O2 <=> H2 + HO2
    H + H2O2 <=> H2O + OH
    H2 + OH <=> H + H2O
    2 OH (+M) <=> H2O2 (+M)
    2 OH <=> H2O + O
    HO2 + OH <=> H2O + O2
    H2O2 + OH <=> H2O + HO2
    H2O2 + OH <=> H2O + HO2
    C + OH <=> CO + H
    CO + OH <=> CO2 + H
    2 HO2 <=> H2O2 + O2
    2 HO2 <=> H2O2 + O2
    CO + HO2 <=> CO2 + OH
    C + O2 <=> CO + O
    HO2 + OH <=> H2O + O2






.. GENERATED FROM PYTHON SOURCE LINES 70-71

Save the resulting mechanism for later use.

.. GENERATED FROM PYTHON SOURCE LINES 71-74

.. code-block:: Python

    gas2.update_user_header({"description": "CO-H2 submechanism extracted from GRI-Mech 3.0"})
    gas2.write_yaml("gri30-CO-H2-submech.yaml", header=True)








.. GENERATED FROM PYTHON SOURCE LINES 75-76

Simulate a flame with each mechanism.

.. GENERATED FROM PYTHON SOURCE LINES 76-97

.. code-block:: Python

    def solve_flame(gas):
        gas.TPX = 373, 0.05*ct.one_atm, 'H2:0.4, CO:0.6, O2:1, N2:3.76'

        # Create the flame simulation object
        sim = ct.CounterflowPremixedFlame(gas=gas, width=0.2)

        sim.reactants.mdot = 0.12  # kg/m^2/s
        sim.products.mdot = 0.06  # kg/m^2/s

        sim.set_refine_criteria(ratio=3, slope=0.1, curve=0.2)
        sim.solve(0, auto=True)
        return sim

    t1 = default_timer()
    sim1 = solve_flame(gas1)
    t2 = default_timer()
    print('Solved with GRI 3.0 in {0:.2f} seconds'.format(t2-t1))
    sim2 = solve_flame(gas2)
    t3 = default_timer()
    print('Solved with CO/H2 submechanism in {0:.2f} seconds'.format(t3-t2))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Solved with GRI 3.0 in 20.81 seconds
    Solved with CO/H2 submechanism in 0.93 seconds




.. GENERATED FROM PYTHON SOURCE LINES 98-99

Plot the results to show that the submechanism gives the same results as the original.

.. GENERATED FROM PYTHON SOURCE LINES 99-111

.. code-block:: Python


    plt.plot(sim1.grid, sim1.heat_release_rate,
             lw=2, label='GRI 3.0')

    plt.plot(sim2.grid, sim2.heat_release_rate,
             'r--', lw=2, label='CO/H2 submechanism')

    plt.ylabel('Heat release rate ($W/m^3$)')
    plt.xlabel('position (m)')
    plt.legend(loc='upper right')
    plt.tight_layout()
    plt.show()



.. image-sg:: /examples/python/kinetics/images/sphx_glr_extract_submechanism_001.png
   :alt: extract submechanism
   :srcset: /examples/python/kinetics/images/sphx_glr_extract_submechanism_001.png, /examples/python/kinetics/images/sphx_glr_extract_submechanism_001_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 22.282 seconds)


.. _sphx_glr_download_examples_python_kinetics_extract_submechanism.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: extract_submechanism.ipynb <extract_submechanism.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: extract_submechanism.py <extract_submechanism.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: extract_submechanism.zip <extract_submechanism.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
