
: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/sound_speed_units.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_sound_speed_units.py>`
        to download the full example code.

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

.. _sphx_glr_examples_python_thermo_sound_speed_units.py:


Sound speeds (with units)
=========================

Compute the "equilibrium" and "frozen" sound speeds for a gas. Uses the ``pint`` library
to include customized units in the calculation.

Requires: Cantera >= 3.0.0, pint

.. tags:: Python, thermodynamics, equilibrium, units

.. GENERATED FROM PYTHON SOURCE LINES 12-70




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Temperature      Equilibrium Sound Speed     Frozen Sound Speed      Frozen Sound Speed Check
    80.00 °F               1153.91 ft/s               1153.93 ft/s               1153.94 ft/s
    280.00 °F               1345.45 ft/s               1344.87 ft/s               1344.88 ft/s
    480.00 °F               1509.88 ft/s               1509.00 ft/s               1509.01 ft/s
    680.00 °F               1653.91 ft/s               1654.06 ft/s               1654.07 ft/s
    880.00 °F               1780.29 ft/s               1784.73 ft/s               1784.75 ft/s
    1080.00 °F               1908.44 ft/s               1904.45 ft/s               1904.46 ft/s
    1280.00 °F               2015.91 ft/s               2016.04 ft/s               2016.05 ft/s
    1480.00 °F               2123.31 ft/s               2121.80 ft/s               2121.82 ft/s
    1680.00 °F               2221.98 ft/s               2222.27 ft/s               2222.29 ft/s
    1880.00 °F               2317.69 ft/s               2318.16 ft/s               2318.18 ft/s
    2080.00 °F               2408.97 ft/s               2410.11 ft/s               2410.13 ft/s
    2280.00 °F               2495.83 ft/s               2498.65 ft/s               2498.67 ft/s
    2480.00 °F               2577.97 ft/s               2584.26 ft/s               2584.28 ft/s
    2680.00 °F               2654.92 ft/s               2667.41 ft/s               2667.42 ft/s
    2880.00 °F               2726.33 ft/s               2748.59 ft/s               2748.60 ft/s
    3080.00 °F               2792.44 ft/s               2828.39 ft/s               2828.40 ft/s
    3280.00 °F               2854.41 ft/s               2907.49 ft/s               2907.51 ft/s
    3480.00 °F               2913.84 ft/s               2986.72 ft/s               2986.73 ft/s
    3680.00 °F               2974.50 ft/s               3066.99 ft/s               3067.00 ft/s
    3880.00 °F               3037.44 ft/s               3149.32 ft/s               3149.33 ft/s
    4080.00 °F               3103.87 ft/s               3234.79 ft/s               3234.80 ft/s
    4280.00 °F               3177.93 ft/s               3324.51 ft/s               3324.52 ft/s
    4480.00 °F               3257.51 ft/s               3419.62 ft/s               3419.63 ft/s
    4680.00 °F               3343.31 ft/s               3521.37 ft/s               3521.37 ft/s
    4880.00 °F               3438.69 ft/s               3631.10 ft/s               3631.11 ft/s






|

.. code-block:: Python


    import cantera.with_units as ctu
    import numpy as np

    # This sets the default output format of the units to have 2 significant digits
    # and the units are printed with a Unicode font. See:
    # https://pint.readthedocs.io/en/stable/user/formatting.html#unit-format-types
    if hasattr(ctu.cantera_units_registry, "formatter"):  # pint >= 0.24
        ctu.cantera_units_registry.formatter.default_format = ".2F~P"
    else:
        ctu.units.default_format = ".2F~P"

    def equilibrium_sound_speeds(gas, rtol=1.0e-6, max_iter=5000):
        """
        Returns a tuple containing the equilibrium and frozen sound speeds for a
        gas with an equilibrium composition.  The gas is first set to an
        equilibrium state at the temperature and pressure of the gas, since
        otherwise the equilibrium sound speed is not defined.
        """

        # set the gas to equilibrium at its current T and P
        gas.equilibrate('TP', rtol=rtol, max_iter=max_iter)

        # save properties
        s0 = gas.s
        p0 = gas.P
        r0 = gas.density

        # perturb the pressure
        p1 = p0*1.0001

        # set the gas to a state with the same entropy and composition but
        # the perturbed pressure
        gas.SP = s0, p1

        # frozen sound speed
        afrozen = np.sqrt((p1 - p0)/(gas.density - r0)).to("ft/s")

        # now equilibrate the gas holding S and P constant
        gas.equilibrate('SP', rtol=rtol, max_iter=max_iter)

        # equilibrium sound speed
        aequil = np.sqrt((p1 - p0)/(gas.density - r0)).to("ft/s")

        # check against the built-in sound speed function
        afrozen2 = gas.sound_speed.to("ft/s")

        return aequil, afrozen, afrozen2

    # test program
    if __name__ == "__main__":
        gas = ctu.Solution('gri30.yaml')
        gas.X = 'CH4:1.00, O2:2.0, N2:7.52'
        T_range = np.linspace(80, 4880, 25) * ctu.units.degF
        print("Temperature      Equilibrium Sound Speed     Frozen Sound Speed      Frozen Sound Speed Check")
        for T in T_range:
            gas.TP = T, 1.0 * ctu.units.atm
            print(T.to("degF"), *equilibrium_sound_speeds(gas), sep = "               ")


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

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


.. _sphx_glr_download_examples_python_thermo_sound_speed_units.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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