
: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/reactors/periodic_cstr.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_reactors_periodic_cstr.py>`
        to download the full example code.

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

.. _sphx_glr_examples_python_reactors_periodic_cstr.py:


Continuously stirred tank reactor with periodic behavior
========================================================

This example illustrates a continuously stirred tank reactor (CSTR) with steady
inputs but periodic interior state.

A stoichiometric hydrogen/oxygen mixture is introduced and reacts to produce
water.  But since water has a large efficiency as a third body in the chain
termination reaction

.. math::

       \mathrm{ H + O_2 + M \rightleftharpoons HO_2 + M }

as soon as a significant amount of water is produced the reaction stops. After
enough time has passed that the water is exhausted from the reactor, the mixture
explodes again and the process repeats. This explanation can be verified by
decreasing the rate for reaction 7 in file ``h2o2.yaml`` and re-running the
example.

*Acknowledgments*: The idea for this example and an estimate of the conditions
needed to see the oscillations came from Bob Kee, Colorado School of Mines

Requires: cantera >= 3.2.0, matplotlib >= 2.0

.. tags:: Python, combustion, reactor network, well-stirred reactor, plotting

.. GENERATED FROM PYTHON SOURCE LINES 29-33

.. code-block:: Python


    import cantera as ct
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 34-35

Create the gas mixture and set initial conditions

.. GENERATED FROM PYTHON SOURCE LINES 35-43

.. code-block:: Python

    gas = ct.Solution('h2o2.yaml')

    # pressure = 60 Torr, T = 770 K
    p = 60.0*133.3
    t = 770.0

    gas.TPX = t, p, 'H2:2, O2:1'








.. GENERATED FROM PYTHON SOURCE LINES 44-47

Create an upstream reservoir that will supply the reactor. The temperature,
pressure, and composition of the upstream reservoir are set to those of the
``gas`` object at the time the reservoir is created.

.. GENERATED FROM PYTHON SOURCE LINES 47-49

.. code-block:: Python

    upstream = ct.Reservoir(gas, clone=True)








.. GENERATED FROM PYTHON SOURCE LINES 50-54

Now create the reactor object with the same initial state.

Set its volume to 10 cm³. In this problem, the reactor volume is fixed, so
the initial volume is the volume at all later times.

.. GENERATED FROM PYTHON SOURCE LINES 54-57

.. code-block:: Python

    cstr = ct.IdealGasReactor(gas, clone=True)
    cstr.volume = 10.0*1.0e-6








.. GENERATED FROM PYTHON SOURCE LINES 58-61

We need to have heat loss to see the oscillations. Create a reservoir to
represent the environment, and initialize its temperature to the reactor
temperature.

.. GENERATED FROM PYTHON SOURCE LINES 61-63

.. code-block:: Python

    env = ct.Reservoir(gas, clone=True)








.. GENERATED FROM PYTHON SOURCE LINES 64-68

Create a heat-conducting wall between the reactor and the environment. Set its
area, and its overall heat transfer coefficient. Larger ``U`` causes the reactor
to be closer to isothermal. If ``U`` is too small, the gas ignites, and the
temperature spikes and stays high.

.. GENERATED FROM PYTHON SOURCE LINES 68-70

.. code-block:: Python

    w = ct.Wall(cstr, env, A=1.0, U=0.02)








.. GENERATED FROM PYTHON SOURCE LINES 71-73

Connect the upstream reservoir to the reactor with a mass flow controller
(constant mdot). Set the mass flow rate to 1.25 sccm.

.. GENERATED FROM PYTHON SOURCE LINES 73-78

.. code-block:: Python

    sccm = 1.25
    vdot = sccm * 1.0e-6 / 60.0 * ((ct.one_atm / gas.P) * (gas.T / 273.15))  # m^3/s
    mdot = gas.density * vdot  # kg/s
    mfc = ct.MassFlowController(upstream, cstr, mdot=mdot)








.. GENERATED FROM PYTHON SOURCE LINES 79-82

Now create a downstream reservoir to exhaust into and connect the reactor to this
reservoir with a valve. Set the coefficient sufficiently large to keep the reactor
pressure close to the downstream pressure of 60 Torr.

.. GENERATED FROM PYTHON SOURCE LINES 82-85

.. code-block:: Python

    downstream = ct.Reservoir(gas, clone=True)
    v = ct.Valve(cstr, downstream, K=1.0e-9)








.. GENERATED FROM PYTHON SOURCE LINES 86-87

Create the network and integrate in time:

.. GENERATED FROM PYTHON SOURCE LINES 87-108

.. code-block:: Python

    network = ct.ReactorNet([cstr])
    t = 0.0
    dt = 0.1

    states = ct.SolutionArray(gas, extra=['t'])
    while t < 300.0:
        t += dt
        network.advance(t)
        states.append(cstr.phase.state, t=t)

    aliases = {'H2': 'H$_2$', 'O2': 'O$_2$', 'H2O': 'H$_2$O'}
    for name, alias in aliases.items():
        gas.add_species_alias(name, alias)

    fig, ax = plt.subplots()
    for spc in aliases.values():
        ax.plot(states.t, states(spc).Y, label=spc)
    plt.legend(loc='upper right')
    plt.xlabel('time [s]')
    plt.ylabel('mass fraction')
    plt.show()



.. image-sg:: /examples/python/reactors/images/sphx_glr_periodic_cstr_001.png
   :alt: periodic cstr
   :srcset: /examples/python/reactors/images/sphx_glr_periodic_cstr_001.png, /examples/python/reactors/images/sphx_glr_periodic_cstr_001_2_00x.png 2.00x
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_examples_python_reactors_periodic_cstr.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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