
: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/python/thermo/isentropic.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_python_thermo_isentropic.py>`
        to download the full example code.

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

.. _sphx_glr_examples_python_thermo_isentropic.py:


Converging-Diverging Nozzle
===========================

Calculate the area ratio vs. Mach number curve for a mixture accelerating to supersonic
speed through a converging--diverging nozzle, assuming isentropic, adiabatic flow.

The calculations are done for a 10:1 hydrogen/nitrogen mixture with stagnation
temperature of 1200 K and a stagnation pressure of 10 atm.

Requires: cantera >= 2.5.0, matplotlib >= 2.0

.. tags:: Python, thermodynamics, compressible flow, plotting

.. GENERATED FROM PYTHON SOURCE LINES 15-21

.. code-block:: Python


    import cantera as ct
    import math
    import numpy as np
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 22-23

Set initial conditions and get stagnation state parameters:

.. GENERATED FROM PYTHON SOURCE LINES 23-31

.. code-block:: Python

    gas = ct.Solution('h2o2.yaml')
    gas.TPX = 1200.0, 10.0*ct.one_atm, 'H2:1, N2:0.1'

    s0 = gas.s
    h0 = gas.h
    p0 = gas.P
    T0 = gas.T








.. GENERATED FROM PYTHON SOURCE LINES 32-45

To calculate the state at different points, we can take pressure as the independent
variable and set the state of the gas based on that pressure and the entropy, which is
constant (:math:`s = s_0`) by the isentropic assumption. Assuming an adiabatic flow,
the total enthalpy is constant, which gives us an equation that can be solved for the
velocity:

.. math:: h_0 = h + \frac{v^2}{2}

Finally, conservation of mass requires:

.. math:: \rho v A = \dot{m} = \textrm{constant}

which gives us an equation that can be solved for the area.

.. GENERATED FROM PYTHON SOURCE LINES 45-61

.. code-block:: Python

    mdot = 1  # arbitrary
    n_points = 200
    data = np.zeros((n_points, 4))

    for i, p in enumerate(np.linspace(0.01 * p0, 0.99*p0, n_points)):
        # set the state using (s0, p)
        gas.SP = s0, p

        v = np.sqrt(2.0*(h0 - gas.h))  # h + V^2/2 = h0
        area = mdot / (gas.density * v)  # rho*v*A = constant
        Ma = v/gas.sound_speed
        data[i, :] = [area, Ma, gas.T/T0, p/p0]

    # Normalize by the minimum area (nozzle throat)
    data[:, 0] /= min(data[:, 0])








.. GENERATED FROM PYTHON SOURCE LINES 62-63

Plot the results:

.. GENERATED FROM PYTHON SOURCE LINES 63-73

.. code-block:: Python

    fig, ax = plt.subplots()
    h1 = ax.plot(data[:, 1], data[:, 3], 'C1', label='$P/P_0$')
    h2 = ax.plot(data[:, 1], data[:, 2], 'C2', label='$T/T_0$')
    ax.set(xlabel='Mach Number', ylabel='Temperature / Pressure Ratio',
           ylim=(0, 1.05))
    ax2 = ax.twinx()
    h3 = ax2.plot(data[:, 1], data[:, 0], label='$A/A^*$')
    ax2.set(ylabel='Area Ratio', ylim=(0,None))
    ax.legend(handles=[h1[0], h2[0], h3[0]], loc='upper center')
    plt.show()



.. image-sg:: /examples/python/thermo/images/sphx_glr_isentropic_001.png
   :alt: isentropic
   :srcset: /examples/python/thermo/images/sphx_glr_isentropic_001.png, /examples/python/thermo/images/sphx_glr_isentropic_001_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.058 seconds)


.. _sphx_glr_download_examples_python_thermo_isentropic.py:

.. only:: html

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

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: isentropic.ipynb <isentropic.ipynb>`

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

      :download:`Download Python source code: isentropic.py <isentropic.py>`

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

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


.. only:: html

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

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