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

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.52 ft/s               1344.87 ft/s               1344.88 ft/s
480.00 °F               1509.69 ft/s               1509.00 ft/s               1509.01 ft/s
680.00 °F               1654.24 ft/s               1654.06 ft/s               1654.07 ft/s
880.00 °F               1780.51 ft/s               1784.73 ft/s               1784.75 ft/s
1080.00 °F               1908.36 ft/s               1904.45 ft/s               1904.46 ft/s
1280.00 °F               2015.90 ft/s               2016.04 ft/s               2016.05 ft/s
1480.00 °F               2123.36 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

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 = "               ")

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

Gallery generated by Sphinx-Gallery