Last active
June 14, 2023 02:32
-
-
Save EnisBerk/73928510698ca54197fad08c628d7f8f to your computer and use it in GitHub Desktop.
Utilities to calculate the speed of sound relative to temperature, humidity, and pressure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Original Code from: Dr Richard Lord - http://resource.npl.co.uk/acoustics/techguides/speedair/ | |
Based on the approximate formula found in | |
Cramer, Owen. "The variation of the specific heat ratio and the speed of sound | |
in air with temperature, pressure, humidity, and CO2 concentration." | |
The Journal of the Acoustical Society of America 93.5 (1993): 2510-2516. | |
Saturation vapour pressure found in Richard S. Davis, "Equation for the | |
Determination of the Density of Moist Air (1981/91)", Metrologia, | |
29, p. 67-70, 1992,and a mole fraction of carbon dioxide of 0.0004. | |
The mole fraction is simply an expression of the number of moles | |
of a compound divided by the total number of moles of all the | |
compounds present in the gas. | |
I also used some info from http://www.sengpielaudio.com/calculator-airpressure.htm | |
""" | |
import math | |
import numpy as np | |
def log10(x): | |
return math.log(x) / math.log(10) | |
def roundto(x, dp): | |
return round(x * 10**dp) / 10**dp | |
def sqr(x): | |
return x**2 | |
def calculate_speed_of_sound(temperature, pressure, relative_humidity): | |
# calculate speed of sound in humid air | |
# only pressure 75 to 102 | |
# only temperature 0 to 30 | |
# only relative humidity 0 to 100 | |
# temperature in degrees Celsius | |
# pressure in kilopascals | |
# relative humidity in percent | |
# returns speed of sound in meters per second | |
# constants | |
# Check that sensible numbers were entered | |
if not (75 <= pressure <= 102): | |
print("Pressure must be between 75 and 102 kPa") | |
return | |
pressure_pa = pressure * 1000.0 # Convert to Pa | |
# tested only for 0 to 40 degrees Celsius | |
# if not (0 <= temperature <= 40): | |
# print("Temperature must be between 0 and 30 degrees Celsius") | |
# return | |
# Check that sensible numbers were entered | |
if relative_humidity > 100 or relative_humidity < 0: | |
print("Relative humidity must be between 0 and 100%") | |
return | |
temperature_kelvin = 273.15 + temperature # Measured ambient temp | |
enhancement_factor = 3.141593 * 10**-8 * pressure_pa + 1.00062 + sqr( | |
temperature) * 5.6 * 10**-7 | |
psv1 = sqr(temperature_kelvin | |
) * 1.2378847 * 10**-5 - 1.9121316 * 10**-2 * temperature_kelvin | |
psv2 = 33.93711047 - 6.3431645 * 10**3 / temperature_kelvin | |
psv = math.exp(psv1) * math.exp(psv2) | |
water_vapor_concentration = relative_humidity * enhancement_factor * psv / pressure_pa | |
mole_fraction_water_vapor = water_vapor_concentration / 100.0 | |
mole_fraction_carbon_dioxide = 400.0 * 10**-6 | |
c1 = 0.603055 * temperature + 331.5024 - sqr( | |
temperature) * 5.28 * 10**-4 + ( | |
0.1495874 * temperature + 51.471935 - | |
sqr(temperature) * 7.82 * 10**-4) * mole_fraction_water_vapor | |
c2 = (-1.82 * 10**-7 + 3.73 * 10**-8 * temperature - | |
sqr(temperature) * 2.93 * 10**-10) * pressure_pa + ( | |
-85.20931 - 0.228525 * temperature + | |
sqr(temperature) * 5.91 * 10**-5) * mole_fraction_carbon_dioxide | |
c3 = sqr(mole_fraction_water_vapor) * 2.835149 - sqr( | |
pressure_pa | |
) * 2.15 * 10**-13 + sqr( | |
mole_fraction_carbon_dioxide | |
) * 29.179762 + 4.86 * 10**-4 * mole_fraction_water_vapor * pressure_pa * mole_fraction_carbon_dioxide | |
speed_of_sound = c1 + c2 - c3 | |
return speed_of_sound | |
# Call the function to calculate the speed of sound | |
tempreature, pressure, relative_humidity = 25, 101.325, 100 | |
calculate_speed_of_sound(tempreature, pressure, relative_humidity) | |
# calculate average change in speed of sound per temperature change | |
def average_change_by_temp(pressure = 101.325, relative_humidity = 0): | |
prev=0 | |
# kPa (standard pressure) | |
relative_humidity = 0 # dry air | |
total=[] | |
for i in range(0,30): | |
new=(calculate_speed_of_sound(i, pressure, relative_humidity)) | |
# print(new,new-prev) | |
total.append((new-prev)) | |
prev=new | |
print(sum(total[1:])/(len(total)-1)) | |
# calculate average change in speed of sound per humidity change | |
def average_change_by_humidity(pressure = 101.325, temperature = 25): | |
prev=0 | |
# kPa (standard pressure) | |
temperature = 25 # dry air | |
total=[] | |
for i in range(0,100): | |
new=(calculate_speed_of_sound(temperature, pressure, i)) | |
# print(new,new-prev) | |
total.append((new-prev)) | |
prev=new | |
print(sum(total[1:])/(len(total)-1)) | |
print('Average change in speed of sound per degree Celsius') | |
print('(standard pressure), dry air') | |
average_change_by_temp(pressure = 101.325, relative_humidity = 0) | |
print('(standard pressure), wet air') | |
average_change_by_temp(pressure = 101.325, relative_humidity = 100) | |
print('low pressure, wet air') | |
average_change_by_temp(pressure = 75, relative_humidity = 100) | |
print('low pressure, dry air') | |
average_change_by_temp(pressure = 75, relative_humidity = 0) | |
print('\nAverage change in speed of sound per humidity') | |
average_change_by_humidity(pressure = 101.325, temperature = 25) | |
average_change_by_humidity(pressure = 101.325, temperature = 0) | |
average_change_by_humidity(pressure = 75, temperature = 25) | |
average_change_by_humidity(pressure = 75, temperature = 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment