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

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

.. _sphx_glr_examples_matlab_reactor2.m:

Adiabatic, constant volume 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 constant volume.

Requires: cantera >= 3.2.0

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

.. GENERATED FROM PYTHON SOURCE LINES 0-9

.. code-block:: Matlab

    function reactor2(g)
        tic
        help reactor2

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

.. GENERATED FROM PYTHON SOURCE LINES 20-21

Set the initial conditions

.. GENERATED FROM PYTHON SOURCE LINES 22-23

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

        gas.TPX = {1001.0, ct.OneAtm, 'H2:2,O2:1,N2:4'};

.. GENERATED FROM PYTHON SOURCE LINES 24-25

Create a reactor, and insert the gas

.. GENERATED FROM PYTHON SOURCE LINES 26-27

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

        r = ct.zeroD.IdealGasReactor(gas);

.. GENERATED FROM PYTHON SOURCE LINES 28-29

Create a reactor network and insert the reactor

.. GENERATED FROM PYTHON SOURCE LINES 30-49

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

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

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

        for n = 1:100
            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 50-51

Plot results

.. GENERATED FROM PYTHON SOURCE LINES 51-70

.. 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_reactor2.m:

.. only:: html

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

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

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

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

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


.. only:: html

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

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