#[derive(Debug, Clone)]
struct AtmosphereLayer {
base_altitude: f64,
base_temperature: f64,
base_pressure: f64,
lapse_rate: f64,
}
const G_ACCEL_MPS2: f64 = 9.80665;
const R_AIR: f64 = 287.0531; const GAMMA: f64 = 1.4; const R_DRY: f64 = 287.05; const R_VAPOR: f64 = 461.495;
const R: f64 = 8.314472; const M_A: f64 = 28.96546e-3; const M_V: f64 = 18.01528e-3;
const ICAO_LAYERS: &[AtmosphereLayer] = &[
AtmosphereLayer {
base_altitude: 0.0,
base_temperature: 288.15, base_pressure: 101325.0, lapse_rate: -0.0065, },
AtmosphereLayer {
base_altitude: 11000.0,
base_temperature: 216.65, base_pressure: 22632.1, lapse_rate: 0.0, },
AtmosphereLayer {
base_altitude: 20000.0,
base_temperature: 216.65, base_pressure: 5474.89, lapse_rate: 0.001, },
AtmosphereLayer {
base_altitude: 32000.0,
base_temperature: 228.65, base_pressure: 868.02, lapse_rate: 0.0028, },
AtmosphereLayer {
base_altitude: 47000.0,
base_temperature: 270.65, base_pressure: 110.91, lapse_rate: 0.0, },
AtmosphereLayer {
base_altitude: 51000.0,
base_temperature: 270.65, base_pressure: 66.94, lapse_rate: -0.0028, },
AtmosphereLayer {
base_altitude: 71000.0,
base_temperature: 214.65, base_pressure: 3.96, lapse_rate: -0.002, },
];
fn calculate_icao_standard_atmosphere(altitude_m: f64) -> (f64, f64) {
let altitude = altitude_m.clamp(0.0, 84000.0);
let layer = ICAO_LAYERS
.iter()
.rev()
.find(|layer| altitude >= layer.base_altitude)
.unwrap_or(&ICAO_LAYERS[0]);
let height_diff = altitude - layer.base_altitude;
let temperature = layer.base_temperature + layer.lapse_rate * height_diff;
let pressure = if layer.lapse_rate.abs() < 1e-10 {
layer.base_pressure * (-G_ACCEL_MPS2 * height_diff / (R_AIR * layer.base_temperature)).exp()
} else {
let temp_ratio = temperature / layer.base_temperature;
layer.base_pressure * temp_ratio.powf(-G_ACCEL_MPS2 / (layer.lapse_rate * R_AIR))
};
(temperature, pressure)
}
pub fn calculate_atmosphere(
altitude_m: f64,
temp_override_c: Option<f64>,
press_override_hpa: Option<f64>,
humidity_percent: f64,
) -> (f64, f64) {
let (temp_k, pressure_pa) = if temp_override_c.is_some() && press_override_hpa.is_some() {
(
temp_override_c.unwrap() + 273.15,
press_override_hpa.unwrap() * 100.0,
)
} else {
let (std_temp_k, std_pressure_pa) = calculate_icao_standard_atmosphere(altitude_m);
let final_temp_k = if let Some(temp_c) = temp_override_c {
temp_c + 273.15
} else {
std_temp_k
};
let final_pressure_pa = if let Some(press_hpa) = press_override_hpa {
press_hpa * 100.0
} else {
std_pressure_pa
};
(final_temp_k, final_pressure_pa)
};
let humidity_clamped = humidity_percent.clamp(0.0, 100.0);
let temp_c = temp_k - 273.15;
let es_hpa = if temp_c >= 0.0 {
6.1121 * (18.678 - temp_c / 234.5) * (temp_c / (257.14 + temp_c)).exp()
} else {
6.1115 * (23.036 - temp_c / 333.7) * (temp_c / (279.82 + temp_c)).exp()
};
let vapor_pressure_pa = humidity_clamped / 100.0 * es_hpa * 100.0;
let dry_pressure_pa = (pressure_pa - vapor_pressure_pa).max(0.0);
let density = dry_pressure_pa / (R_DRY * temp_k) + vapor_pressure_pa / (R_VAPOR * temp_k);
let mole_fraction_vapor = vapor_pressure_pa / pressure_pa;
let temp_c_abs = temp_k;
let gamma_moist = GAMMA * (1.0 - mole_fraction_vapor * 0.11);
let r_moist = R_AIR * (1.0 + 0.6078 * mole_fraction_vapor);
let speed_of_sound_base = (gamma_moist * r_moist * temp_c_abs).sqrt();
let humidity_correction = 1.0 + 0.0001 * humidity_clamped * (temp_c / 20.0);
let speed_of_sound = speed_of_sound_base * humidity_correction;
(density, speed_of_sound)
}
pub fn calculate_air_density_cimp(temp_c: f64, pressure_hpa: f64, humidity_percent: f64) -> f64 {
let t_k = temp_c + 273.15;
let p_sv = enhanced_saturation_vapor_pressure(t_k);
let f = enhanced_enhancement_factor(pressure_hpa, temp_c);
let p_v = humidity_percent.clamp(0.0, 100.0) / 100.0 * f * p_sv;
let x_v = p_v / pressure_hpa;
let z = enhanced_compressibility_factor(pressure_hpa, t_k, x_v);
let density = ((pressure_hpa * M_A) / (z * R * t_k)) * (1.0 - x_v * (1.0 - M_V / M_A));
density * 100.0
}
#[inline(always)]
fn enhanced_saturation_vapor_pressure(t_k: f64) -> f64 {
const A: [f64; 6] = [
-7.85951783,
1.84408259,
-11.7866497,
22.6807411,
-15.9618719,
1.80122502,
];
let t_k_safe = t_k.max(173.15);
let tau = 1.0 - t_k_safe / 647.096; let ln_p_ratio = (647.096 / t_k_safe)
* (A[0] * tau
+ A[1] * tau.powf(1.5)
+ A[2] * tau.powf(3.0)
+ A[3] * tau.powf(3.5)
+ A[4] * tau.powf(4.0)
+ A[5] * tau.powf(7.5));
220640.0 * ln_p_ratio.exp() }
#[inline(always)]
fn enhanced_enhancement_factor(p: f64, t: f64) -> f64 {
const ALPHA: f64 = 1.00062;
const BETA: f64 = 3.14e-8;
const GAMMA: f64 = 5.6e-7;
const DELTA: f64 = 1.2e-10;
ALPHA + BETA * p + GAMMA * t * t + DELTA * p * t
}
#[inline(always)]
fn enhanced_compressibility_factor(p: f64, t_k: f64, x_v: f64) -> f64 {
const A0: f64 = 1.58123e-6;
const A1: f64 = -2.9331e-8;
const A2: f64 = 1.1043e-10;
const B0: f64 = 5.707e-6;
const B1: f64 = -2.051e-8;
const C0: f64 = 1.9898e-4;
const C1: f64 = -2.376e-6;
const D: f64 = 1.83e-11;
const E: f64 = -0.765e-8;
const F0: f64 = 2.1e-12;
const F1: f64 = -1.1e-14;
let t_k_safe = t_k.max(173.15); let t = t_k_safe - 273.15;
let p_t = p / t_k_safe;
let z_second_order =
1.0 - p_t * (A0 + A1 * t + A2 * t * t + (B0 + B1 * t) * x_v + (C0 + C1 * t) * x_v * x_v);
let z_third_order = p_t * p_t * (D + E * x_v * x_v);
let z_fourth_order = p_t * p_t * p_t * (F0 + F1 * x_v * x_v * x_v);
z_second_order + z_third_order + z_fourth_order
}
pub fn get_local_atmosphere(
altitude_m: f64,
base_alt: f64,
base_temp_c: f64,
base_press_hpa: f64,
base_ratio: f64,
) -> (f64, f64) {
let altitude_m_rounded = altitude_m.round();
let height_diff = altitude_m_rounded - base_alt;
let lapse_rate = determine_local_lapse_rate(altitude_m_rounded);
let temp_c = base_temp_c + lapse_rate * height_diff;
let temp_k = temp_c + 273.15;
let base_temp_k = base_temp_c + 273.15;
let pressure_hpa = if lapse_rate.abs() < 1e-10 {
base_press_hpa * (-G_ACCEL_MPS2 * height_diff / (R_AIR * base_temp_k)).exp()
} else {
let temp_ratio = temp_k / base_temp_k;
base_press_hpa * temp_ratio.powf(-G_ACCEL_MPS2 / (lapse_rate * R_AIR))
};
let density_ratio = base_ratio * (base_temp_k * pressure_hpa) / (base_press_hpa * temp_k);
let density = density_ratio * 1.225;
let speed_of_sound = (temp_k * 401.874).sqrt();
(density, speed_of_sound)
}
#[inline(always)]
fn determine_local_lapse_rate(altitude_m: f64) -> f64 {
let layer = ICAO_LAYERS
.iter()
.rev()
.find(|layer| altitude_m >= layer.base_altitude)
.unwrap_or(&ICAO_LAYERS[0]);
layer.lapse_rate
}
#[inline(always)]
pub fn get_direct_atmosphere(density: f64, speed_of_sound: f64) -> (f64, f64) {
(density, speed_of_sound)
}
pub fn calculate_air_density_cipm(temp_c: f64, pressure_hpa: f64, humidity_percent: f64) -> f64 {
calculate_air_density_cimp(temp_c, pressure_hpa, humidity_percent)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_icao_standard_atmosphere() {
let (temp, press) = calculate_icao_standard_atmosphere(0.0);
assert!((temp - 288.15).abs() < 0.01);
assert!((press - 101325.0).abs() < 1.0);
let (temp_11km, press_11km) = calculate_icao_standard_atmosphere(11000.0);
assert!((temp_11km - 216.65).abs() < 0.01);
assert!(press_11km < 101325.0);
let (temp_25km, _) = calculate_icao_standard_atmosphere(25000.0);
assert!(temp_25km > 216.65); }
#[test]
fn test_enhanced_atmosphere_sea_level() {
let (density, speed) = calculate_atmosphere(0.0, None, None, 0.0);
assert!((density - 1.225).abs() < 0.01);
assert!((speed - 340.0).abs() < 1.0);
}
#[test]
fn test_enhanced_atmosphere_with_humidity() {
let (density_dry, speed_dry) = calculate_atmosphere(0.0, None, None, 0.0);
let (density_humid, speed_humid) = calculate_atmosphere(0.0, None, None, 80.0);
assert!(density_humid < density_dry);
assert!(speed_humid > speed_dry);
}
#[test]
fn test_enhanced_atmosphere_stratosphere() {
let (density_20km, speed_20km) = calculate_atmosphere(20000.0, None, None, 0.0);
let (density_30km, speed_30km) = calculate_atmosphere(30000.0, None, None, 0.0);
assert!(density_30km < density_20km);
assert!(speed_30km > speed_20km);
}
#[test]
fn test_enhanced_cimp_density() {
let density = calculate_air_density_cimp(15.0, 1013.25, 0.0);
assert!((density - 1.225).abs() < 0.01);
let density_humid = calculate_air_density_cimp(15.0, 1013.25, 50.0);
assert!(density_humid < density);
}
#[test]
fn test_variable_lapse_rates() {
let lapse_tropo = determine_local_lapse_rate(5000.0);
let lapse_strato = determine_local_lapse_rate(25000.0);
assert!((lapse_tropo - (-0.0065)).abs() < 0.0001);
assert!(lapse_strato > 0.0); }
}