
: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/dotnet/SoundSpeed.cs"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_dotnet_SoundSpeed.cs>`
        to download the full example code.

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

.. _sphx_glr_examples_dotnet_SoundSpeed.cs:

C# SoundSpeed Example
=====================

Computes the “equilibrium” and “frozen” sound speeds for a gas using Cantera's .NET
API.

.. tags:: .NET, equilibrium, thermodynamics

.. GENERATED FROM PYTHON SOURCE LINES 9-76

.. code-block:: C#

    /// This file is part of Cantera. See License.txt in the top-level directory or
    /// at https://cantera.org/license.txt for license and copyright information.

    using Cantera;

    Application.DataDirectories.AddAssemblyDirectory();

    Application.AddConsoleLogging();

    var phase = Application.CreateThermoPhase("gri30.yaml");
    var species = phase.Species;

    species.SetMoleFractions(("CH4", 1.00), ("O2", 2.00), ("N2", 7.52));

    for (var n = 0; n < 27; n++)
    {
        var T = 300.0 + 100.0 * n;
        var (aEquil, aFrozen, aFrozen2) = EquilSoundSpeeds(phase, T);

        Console.WriteLine(
            $"T: {T}, aEquil: {aEquil}, aFrozen: {aFrozen}, aFrozen2: {aFrozen2}");
    }

    /// <summary>
    /// 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, because the equilibrium sound speed
    /// is otherwise undefined.
    /// </summary>
    static (double equil, double frozen, double frozen2)
        EquilSoundSpeeds(ThermoPhase gas, double T, double rTol = 1.0e-6,
                         int maxIter = 5000)
    {
        gas.SetPair(ThermoPair.TemperaturePressure, T, Consts.OneAtm);

        // Set the gas to equilibrium at its current T and P.
        gas.Equilibrate(
            ThermoPair.TemperaturePressure, tolerance: rTol, maxIterations: maxIter);

        // Save properties.
        var s0 = gas.MassEntropy;
        var p0 = gas.Pressure;
        var r0 = gas.Density;

        // Perturb the pressure.
        var p1 = p0 * 1.0001;

        // Set the gas to a state with the same entropy and composition but
        // the perturbed pressure.
        gas.SetPair(ThermoPair.EntropyPressure, s0, p1);

        // frozen sound speed
        var aFrozen = Math.Sqrt((p1 - p0)/(gas.Density - r0));

        // Now equilibrate the gas holding S and P constant.
        gas.Equilibrate(
            ThermoPair.EntropyPressure, tolerance: rTol, maxIterations: maxIter);

        // equilibrium sound speed
        var aEquil = Math.Sqrt((p1 - p0)/(gas.Density - r0));

        // Compute the frozen sound speed using the ideal gas expression as a check.
        var gamma = gas.MassCp / gas.MassCv;
        var aFrozen2 = Math.Sqrt(
            gamma * Consts.GasConstant * gas.Temperature / gas.MeanMolecularWeight);

        return (aEquil, aFrozen, aFrozen2);
    }

.. _sphx_glr_download_examples_dotnet_SoundSpeed.cs:

.. only:: html

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

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

      :download:`Download C# source code: SoundSpeed.cs <SoundSpeed.cs>`

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

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


.. only:: html

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

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