
: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/reactor1.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_reactor1.m>`
        to download the full example code.

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

.. _sphx_glr_examples_matlab_reactor1.m:

Adiabatic, constant pressure reactor
====================================


This example illustrates how to use class ``Reactor`` for zero-dimensional
kinetics simulations. Here the parameters are set so that the reactor is
adiabatic and very close to constant pressure.

Requires: cantera >= 3.2.0

.. tags:: Matlab, combustion, reactor network, ignition delay, plotting

.. GENERATED FROM PYTHON SOURCE LINES 0-10

.. code-block:: Matlab

    function reactor1(g)
        tic
        help reactor1

        if nargin == 1
            gas = g;
        else
            gas = ct.Solution('gri30.yaml', 'gri30', 'none');
        end

        P = 101325.0;
.. GENERATED FROM PYTHON SOURCE LINES 21-22

Set the initial conditions

.. GENERATED FROM PYTHON SOURCE LINES 23-30

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

        gas.TP = {1001.0, P};
        nsp = gas.nSpecies;
        xx = zeros(nsp, 1);
        xx(1) = 0.285;
        xx(4) = 0.142;
        xx(48) = 0.573;
        gas.X = xx;

.. GENERATED FROM PYTHON SOURCE LINES 31-32

Create a reactor, and insert the gas

.. GENERATED FROM PYTHON SOURCE LINES 33-34

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

        r = ct.zeroD.IdealGasReactor(gas);

.. GENERATED FROM PYTHON SOURCE LINES 35-36

Create a reservoir to represent the environment

.. GENERATED FROM PYTHON SOURCE LINES 37-40

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

        a = ct.Solution('air.yaml', 'air', 'none');
        a.TP = {a.T, P};
        env = ct.zeroD.Reservoir(a);

.. GENERATED FROM PYTHON SOURCE LINES 41-44

Define a wall between the reactor and the environment and
make it flexible, so that the pressure in the reactor is held
at the environment pressure.

.. GENERATED FROM PYTHON SOURCE LINES 45-46

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

        w = ct.zeroD.Wall(r, env);

.. GENERATED FROM PYTHON SOURCE LINES 47-48

Set expansion parameter. dV/dt = KA(P_1 - P_2)

.. GENERATED FROM PYTHON SOURCE LINES 49-50

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

        w.expansionRateCoeff = 1.0e6;

.. GENERATED FROM PYTHON SOURCE LINES 51-52

Set wall area

.. GENERATED FROM PYTHON SOURCE LINES 53-54

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

        w.area = 1.0;

.. GENERATED FROM PYTHON SOURCE LINES 55-56

Create a reactor network and insert the reactor:

.. GENERATED FROM PYTHON SOURCE LINES 57-76

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

        network = ct.zeroD.ReactorNet({r});

        nSteps = 100;
        tim(nSteps) = 0;
        temp(nSteps) = 0;
        x(nSteps, 3) = 0;
        t = 0.0;
        dt = 1.0e-5;
        t0 = cputime;

        for n = 1:nSteps
            t = t + dt;
            network.advance(t);
            tim(n) = network.time;
            temp(n) = r.T;
            x(n, 1:3) = r.phase.moleFraction({'OH', 'H', 'H2'});
        end

        disp(['CPU time = ' num2str(cputime - t0)]);

.. GENERATED FROM PYTHON SOURCE LINES 77-78

Plot results

.. GENERATED FROM PYTHON SOURCE LINES 78-97

.. code-block:: Matlab

        clf;
        subplot(2, 2, 1);
        plot(tim, temp);
        xlabel('Time (s)');
        ylabel('Temperature (K)');
        subplot(2, 2, 2)
        plot(tim, x(:, 1));
        xlabel('Time (s)');
        ylabel('OH Mole Fraction (K)');
        subplot(2, 2, 3)
        plot(tim, x(:, 2));
        xlabel('Time (s)');
        ylabel('H Mole Fraction (K)');
        subplot(2, 2, 4)
        plot(tim, x(:, 3));
        xlabel('Time (s)');
        ylabel('H2 Mole Fraction (K)');

        toc
    end

.. _sphx_glr_download_examples_matlab_reactor1.m:

.. only:: html

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

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

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

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

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


.. only:: html

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

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