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

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

.. _sphx_glr_examples_matlab_surf_reactor.m:

Zero-dimensional reactor with surface chemistry
===============================================


This example illustrates how to use class ``Reactor`` for zero-dimensional
simulations including both homogeneous and heterogeneous chemistry.

Requires: cantera >= 3.2.0

.. tags:: Matlab, catalysis, combustion, reactor network, plotting

.. GENERATED FROM PYTHON SOURCE LINES 9-11

Problem Definition
------------------

.. GENERATED FROM PYTHON SOURCE LINES 13-21

.. code-block:: Matlab

    tic
    help surf_reactor

    % The surface reaction mechanism describes catalytic combustion of
    % methane on platinum, and is from Deutschmann et al., 26th
    % Symp. (Intl.) on Combustion,1996, pp. 1747-1754
    surf = ct.Interface('ptcombust.yaml', 'Pt_surf');
    gas = surf.adjacent('gas');

.. GENERATED FROM PYTHON SOURCE LINES 22-23

Set the initial conditions

.. GENERATED FROM PYTHON SOURCE LINES 25-32

.. code-block:: Matlab

    t = 870.0;
    gas.TPX = {t, ct.OneAtm, 'CH4:0.01, O2:0.21, N2:0.78'};

    surf.TP = {t, surf.P};

    nsp = gas.nSpecies;
    nSurfSp = surf.nSpecies;

.. GENERATED FROM PYTHON SOURCE LINES 33-34

Create a reactor, and insert the gas

.. GENERATED FROM PYTHON SOURCE LINES 35-37

.. code-block:: Matlab

    r = ct.zeroD.IdealGasReactor(gas, 'reactor');
    r.V = 1.0e-6;

.. GENERATED FROM PYTHON SOURCE LINES 38-39

Create a reservoir to represent the environment

.. GENERATED FROM PYTHON SOURCE LINES 40-43

.. code-block:: Matlab

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

.. GENERATED FROM PYTHON SOURCE LINES 44-47

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 48-51

.. code-block:: Matlab

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

    A = 1e-4; % Wall area

.. GENERATED FROM PYTHON SOURCE LINES 52-53

Add a reacting surface, with an area matching that of the wall

.. GENERATED FROM PYTHON SOURCE LINES 54-57

.. code-block:: Matlab

    rsurf = ct.zeroD.ReactorSurface(surf, r, 'surface');
    rsurf.area = A;
    rphase = rsurf.phase  % output needs to use phase owned by reactor

.. GENERATED FROM PYTHON SOURCE LINES 58-59

Set the wall area and heat transfer coefficient.

.. GENERATED FROM PYTHON SOURCE LINES 60-62

.. code-block:: Matlab

    w.area = A;
    w.heatTransferCoeff = 1.0e1; % W/m2/K

.. GENERATED FROM PYTHON SOURCE LINES 63-64

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

.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: Matlab

    w.expansionRateCoeff = 1.0;

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

.. GENERATED FROM PYTHON SOURCE LINES 69-71

Solution
--------

.. GENERATED FROM PYTHON SOURCE LINES 73-96

.. code-block:: Matlab

    nSteps = 100;
    p0 = r.P;
    names = {'CH4', 'CO', 'CO2', 'H2O'};
    x = zeros([nSteps, 4]);
    tim = zeros(nSteps, 1);
    temp = zeros(nSteps, 1);
    pres = zeros(nSteps, 1);
    cov = zeros([nSteps nSurfSp]);
    t = 0;
    dt = 0.1;
    t0 = cputime;

    for n = 1:nSteps
        t = t + dt;
        network.advance(t);
        tim(n) = t;
        temp(n) = r.T;
        pres(n) = r.P - p0;
        cov(n, :) = rphase.X';
        x(n, :) = r.phase.moleFraction(names);
    end

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

.. GENERATED FROM PYTHON SOURCE LINES 97-98

Plot results

.. GENERATED FROM PYTHON SOURCE LINES 99-120

.. code-block:: Matlab

    clf;
    subplot(2, 2, 1);
    plot(tim, temp);
    xlabel('Time (s)');
    ylabel('Temperature (K)');
    subplot(2, 2, 2);
    plot(tim, pres);
    axis([0 5 -0.1 0.1]);
    xlabel('Time (s)');
    ylabel('Delta Pressure (Pa)');
    subplot(2, 2, 3);
    semilogy(tim, cov);
    xlabel('Time (s)');
    ylabel('Coverages');
    legend(rphase.speciesNames);
    subplot(2, 2, 4);
    plot(tim, x);
    xlabel('Time (s)');
    ylabel('Mole Fractions');
    legend(names);

    toc

.. _sphx_glr_download_examples_matlab_surf_reactor.m:

.. only:: html

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

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

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

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

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


.. only:: html

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

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