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

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

.. _sphx_glr_examples_matlab_flamespeed.m:

Freely-propagating flame
========================


MATLAB demo program to compute flame speeds using GRI-Mech. This example is equivalent
to the C++ :doc:`flamespeed.cpp <../cxx/flamespeed>` example.

Requires: cantera >= 3.2.0

.. tags:: Matlab, combustion, 1D flow, premixed flame, flame speed, saving output

.. GENERATED FROM PYTHON SOURCE LINES 9-11

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

.. GENERATED FROM PYTHON SOURCE LINES 13-17

.. code-block:: Matlab

    tic
    help flamespeed

    runtime = cputime;  % record the starting time

.. GENERATED FROM PYTHON SOURCE LINES 18-19

**Set parameter values**

.. GENERATED FROM PYTHON SOURCE LINES 21-31

.. code-block:: Matlab

    phi = 1.;  % equivalence ratio
    Tin = 300.0;  % burner temperature (K)
    pressure = ct.OneAtm;  % pressure

    uIn = 0.3;  % initial guess (m/s)
    lz = 0.1;  % length of simulated flow domain
    nz = 6;  % initial number of grid points

    logLevel = 1;
    refineGrid = true;  % turn on grid refinement

.. GENERATED FROM PYTHON SOURCE LINES 32-36

**Create the gas object**

This object will be used to evaluate all thermodynamic, kinetic,
and transport properties

.. GENERATED FROM PYTHON SOURCE LINES 38-52

.. code-block:: Matlab

    gas = ct.Solution('gri30.yaml', 'gri30', 'mixture-averaged');
    nsp = gas.nSpecies;

    gas.setEquivalenceRatio(phi, 'CH4', 'O2:0.21,N2:0.79');
    gas.TP = {Tin, pressure};
    rhoIn = gas.massDensity;
    Xin = gas.X;
    Yin = gas.Y;

    gas.equilibrate('HP');
    rhoOut = gas.massDensity;
    Yout = gas.Y;
    Tad = gas.T;
    disp(sprintf("phi = %1.1f, Tad = %1.1f\n", phi, Tad));

.. GENERATED FROM PYTHON SOURCE LINES 53-54

**Create domains required for a flame simulation**

.. GENERATED FROM PYTHON SOURCE LINES 56-71

.. code-block:: Matlab

    % Create the flow domain
    flame = ct.oneD.FreeFlow(gas);
    flame.setupUniformGrid(nz, lz, 0.);
    flame.setSteadyTolerances(1e-5, 1e-11);
    flame.P = pressure;

    % Create the inlet
    inlt = ct.oneD.Inlet(gas);
    inlt.T = Tin;
    inlt.X = Xin;
    inlt.massFlux = uIn * rhoIn;

    % Create the outlet
    outlt = ct.oneD.Outlet(gas);
    uOut = uIn * rhoIn / rhoOut;

.. GENERATED FROM PYTHON SOURCE LINES 72-73

**Create the stack and insert the domains**

.. GENERATED FROM PYTHON SOURCE LINES 75-95

.. code-block:: Matlab

    stack = ct.oneD.Sim1D({inlt, flame, outlt});

    % Supply initial guess

    locs = [0.0, 0.3, 0.7, 1.0];
    flame.setProfile('velocity', locs, [uIn, uIn, uOut, uOut]);
    flame.setProfile('T', locs, [Tin, Tin, Tad, Tad]);

    names = gas.speciesNames;
    for i = 1:gas.nSpecies
        flame.setProfile(names{i}, locs, [Yin(i), Yin(i), Yout(i), Yout(i)]);
    end

    stack.show();

    ratio = 10.0;
    slope = 0.08;
    curve = 0.1;

    flame.setRefineCriteria(ratio,slope,curve);

.. GENERATED FROM PYTHON SOURCE LINES 96-97

Display the initial guess and save to a container file

.. GENERATED FROM PYTHON SOURCE LINES 99-109

.. code-block:: Matlab

    fprintf("Profile used for initial guess:\n\n")
    flame.info

    if ct.usesHDF5()
        % Cantera is compiled with native HDF5 support
        fileName = 'flamespeed.h5';
    else
        fileName = 'flamespeed.yaml';
    end
    stack.save(fileName, 'initial-guess', 'Initial guess', true);

.. GENERATED FROM PYTHON SOURCE LINES 110-114

Solution
--------

Solve freely propagating flame

.. GENERATED FROM PYTHON SOURCE LINES 116-120

.. code-block:: Matlab

    % Linearly interpolate to find location where this temperature would
    % exist. The temperature at this location will then be fixed for
    % remainder of calculation.
    stack.setFixedTemperature(0.5 * (Tin + Tad));

.. GENERATED FROM PYTHON SOURCE LINES 121-122

**Mixture-averaged Diffusion**

.. GENERATED FROM PYTHON SOURCE LINES 124-129

.. code-block:: Matlab

    flame.energyEnabled = true;
    stack.solve(logLevel, refineGrid);
    uVec = flame.values('velocity');
    disp(sprintf("Flame speed with mixture-averaged transport: %1.4f m/s\n", uVec(1)));
    stack.save(fileName, "mix", "Solution with mixture-averaged transport", true);

.. GENERATED FROM PYTHON SOURCE LINES 130-131

**Multicomponent Diffusion**

.. GENERATED FROM PYTHON SOURCE LINES 133-139

.. code-block:: Matlab

    % now switch to multicomponent transport
    flame.transportModel = 'multicomponent';
    stack.solve(logLevel, refineGrid);
    uVec = flame.values('velocity');
    disp(sprintf("Flame speed with multicomponent transport: %1.4f m/s\n", uVec(1)));
    stack.save(fileName, "multi", "Solution with multicomponent transport", true);

.. GENERATED FROM PYTHON SOURCE LINES 140-141

**Multicomponent Diffusion with Soret Effect**

.. GENERATED FROM PYTHON SOURCE LINES 143-149

.. code-block:: Matlab

    % now enable Soret diffusion
    flame.soretEnabled = true;
    stack.solve(logLevel, refineGrid);
    uVec = flame.values('velocity');
    disp(sprintf("Flame speed with multicomponent transport + Soret: %1.4f m/s\n", uVec(1)));
    stack.save(fileName, "soret", "Solution with multicomponent transport and Soret", true);

.. GENERATED FROM PYTHON SOURCE LINES 150-151

**Display solution profile**

.. GENERATED FROM PYTHON SOURCE LINES 152-154

.. code-block:: Matlab

    fprintf("Profile after final solution step:\n\n")
    flame.info

.. GENERATED FROM PYTHON SOURCE LINES 155-156

Show elapsed time

.. GENERATED FROM PYTHON SOURCE LINES 156-159

.. code-block:: Matlab

    elapsed = cputime - runtime;
    e = sprintf('Elapsed CPU time: %10.4g', elapsed);
    disp(e);
    toc

.. _sphx_glr_download_examples_matlab_flamespeed.m:

.. only:: html

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

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

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

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

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


.. only:: html

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

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