
: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/matlab/periodic_cstr.m"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_matlab_periodic_cstr.m>`
        to download the full example code.

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

.. _sphx_glr_examples_matlab_periodic_cstr.m:

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

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

.. GENERATED FROM PYTHON SOURCE LINES 0-3

.. code-block:: Matlab

    function periodic_cstr
        tic
        help periodic_cstr

.. GENERATED FROM PYTHON SOURCE LINES 31-32

Create the gas mixture

.. GENERATED FROM PYTHON SOURCE LINES 33-40

.. code-block:: Matlab
   :dedent: 1

        gas = ct.Solution('h2o2.yaml', 'ohmech');

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

        gas.TPX = {300, p, 'H2:2, O2:1'};

.. GENERATED FROM PYTHON SOURCE LINES 41-45

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 46-46

.. code-block:: Matlab
   :dedent: 1

        upstream = ct.zeroD.Reservoir(gas);
.. GENERATED FROM PYTHON SOURCE LINES 47-49

Now set the gas to the initial temperature of the reactor, and create
the reactor object.

.. GENERATED FROM PYTHON SOURCE LINES 50-51

.. code-block:: Matlab
   :dedent: 1

        gas.TP = {t, p};
        cstr = ct.zeroD.IdealGasReactor(gas);
.. GENERATED FROM PYTHON SOURCE LINES 52-54

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

.. GENERATED FROM PYTHON SOURCE LINES 55-55

.. code-block:: Matlab
   :dedent: 1

        cstr.V = 10.0 * 1.0e-6;
.. GENERATED FROM PYTHON SOURCE LINES 56-59

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 60-60

.. code-block:: Matlab
   :dedent: 1

        env = ct.zeroD.Reservoir(gas);
.. GENERATED FROM PYTHON SOURCE LINES 61-66

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 67-69

.. code-block:: Matlab
   :dedent: 1

        w = ct.zeroD.Wall(cstr, env);
        w.area = 1.0;
        w.heatTransferCoeff = 0.02;
.. GENERATED FROM PYTHON SOURCE LINES 70-72

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-77

.. code-block:: Matlab
   :dedent: 1

        sccm = 1.25;
        vdot = sccm * 1.0e-6/60.0 * ((ct.OneAtm / gas.P) * (gas.T / 273.15)); % m^3/s
        mdot = gas.massDensity * vdot; % kg/s
        mfc = ct.zeroD.MassFlowController(upstream, cstr);
        mfc.massFlowRate = mdot;
.. GENERATED FROM PYTHON SOURCE LINES 78-79

Now create a downstream reservoir to exhaust into.

.. GENERATED FROM PYTHON SOURCE LINES 80-80

.. code-block:: Matlab
   :dedent: 1

        downstream = ct.zeroD.Reservoir(gas);
.. GENERATED FROM PYTHON SOURCE LINES 81-84

Connect the reactor to the downstream reservoir with a valve, and
set the coefficient sufficiently large to keep the reactor pressure
close to the downstream pressure of 60 Torr.

.. GENERATED FROM PYTHON SOURCE LINES 85-86

.. code-block:: Matlab
   :dedent: 1

        v = ct.zeroD.Valve(cstr, downstream);
        v.valveCoeff = 1.0e-9;
.. GENERATED FROM PYTHON SOURCE LINES 87-88

Create the network

.. GENERATED FROM PYTHON SOURCE LINES 89-89

.. code-block:: Matlab
   :dedent: 1

        network = ct.zeroD.ReactorNet({cstr});
.. GENERATED FROM PYTHON SOURCE LINES 90-91

Now integrate in time

.. GENERATED FROM PYTHON SOURCE LINES 92-111

.. code-block:: Matlab
   :dedent: 1

        tme = 0.0;
        dt = 0.1;

        n = 0;

        while tme < 300.0
            n = n + 1;
            tme = tme + dt;
            network.advance(tme);
            tm(n) = tme;
            y(1, n) = cstr.massFraction('H2');
            y(2, n) = cstr.massFraction('O2');
            y(3, n) = cstr.massFraction('H2O');
        end

        for i = 2:length(tm)
            if abs(y(3, i-1) - y(3, i)) > (y(3, i-1) * 5)
                disp(tm(i))
            end
        end
.. GENERATED FROM PYTHON SOURCE LINES 112-113

Plot results

.. GENERATED FROM PYTHON SOURCE LINES 113-122

.. code-block:: Matlab

        clf
        figure(1)
        plot(tm, y)
        legend('H2', 'O2', 'H2O')
        title('Mass Fractions')
        ylabel('Mass Fractions')
        xlabel('Time (s)')

        toc
    end

.. _sphx_glr_download_examples_matlab_periodic_cstr.m:

.. only:: html

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

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

      :download:`Download Matlab source code: periodic_cstr.m <periodic_cstr.m>`

    .. 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>`_
