use crate::InternalBallisticInputs as BallisticInputs;
pub fn compute_stability_coefficient(
inputs: &BallisticInputs,
atmo_params: (f64, f64, f64, f64),
) -> f64 {
if inputs.twist_rate == 0.0 || inputs.bullet_length == 0.0 || inputs.bullet_diameter == 0.0 {
return 0.0;
}
const MILLER_CONST: f64 = 30.0;
const VEL_REF_FPS: f64 = 2800.0;
const TEMP_REF_K: f64 = 288.15; const PRESS_REF_HPA: f64 = 1013.25;
let twist_rate_m = inputs.twist_rate.abs() * 0.0254; let twist_calibers = twist_rate_m / inputs.bullet_diameter;
let length_calibers = inputs.bullet_length / inputs.bullet_diameter;
let mass_grains = inputs.bullet_mass / 0.00006479891; let diameter_inches = inputs.bullet_diameter / 0.0254; let velocity_fps = inputs.muzzle_velocity * 3.28084;
let mass_term = MILLER_CONST * mass_grains;
let geom_term = twist_calibers.powi(2)
* diameter_inches.powi(3)
* length_calibers
* (1.0 + length_calibers.powi(2));
if geom_term == 0.0 {
return 0.0;
}
let (_, temp_c, current_press_hpa, _) = atmo_params;
let temp_k = temp_c + 273.15;
let density_correction = (temp_k / TEMP_REF_K) * (PRESS_REF_HPA / current_press_hpa);
let velocity_correction = (velocity_fps / VEL_REF_FPS).powf(1.0 / 3.0);
(mass_term / geom_term) * velocity_correction * density_correction
}
pub const BULLET_LENGTH_RHO_EFF_KG_M3: f64 = 7600.0;
const MIN_LENGTH_CALIBERS: f64 = 2.5;
const MAX_LENGTH_CALIBERS: f64 = 6.5;
pub const DEFAULT_TWIST_SG_TARGET: f64 = 2.0;
const GRAINS_PER_KG: f64 = 1.0 / 0.00006479891;
const METERS_PER_INCH: f64 = 0.0254;
const MPS_TO_FPS: f64 = 3.28084;
const MILLER_VEL_REF_FPS: f64 = 2800.0;
pub fn estimate_bullet_length_m(diameter_m: f64, mass_kg: f64) -> f64 {
if mass_kg <= 0.0 || diameter_m <= 0.0 || !mass_kg.is_finite() || !diameter_m.is_finite() {
return 0.0;
}
let frontal_area = std::f64::consts::FRAC_PI_4 * diameter_m * diameter_m;
let raw_length = mass_kg / (BULLET_LENGTH_RHO_EFF_KG_M3 * frontal_area);
let length_calibers = (raw_length / diameter_m).clamp(MIN_LENGTH_CALIBERS, MAX_LENGTH_CALIBERS);
length_calibers * diameter_m
}
pub fn default_twist_inches(diameter_m: f64, mass_kg: f64, muzzle_velocity_mps: f64) -> f64 {
const FALLBACK_TWIST_IN: f64 = 12.0;
if diameter_m <= 0.0
|| mass_kg <= 0.0
|| muzzle_velocity_mps <= 0.0
|| !diameter_m.is_finite()
|| !mass_kg.is_finite()
|| !muzzle_velocity_mps.is_finite()
{
return FALLBACK_TWIST_IN;
}
let length_m = estimate_bullet_length_m(diameter_m, mass_kg);
if length_m <= 0.0 {
return FALLBACK_TWIST_IN;
}
let d_in = diameter_m / METERS_PER_INCH;
let m_gr = mass_kg * GRAINS_PER_KG;
let l_cal = length_m / diameter_m;
let v_fps = muzzle_velocity_mps * MPS_TO_FPS;
let velocity_correction = (v_fps / MILLER_VEL_REF_FPS).powf(1.0 / 3.0);
let geom = d_in.powi(3) * l_cal * (1.0 + l_cal * l_cal);
let denom = DEFAULT_TWIST_SG_TARGET * geom;
if denom <= 0.0 {
return FALLBACK_TWIST_IN;
}
let t_cal_sq = 30.0 * m_gr * velocity_correction / denom;
if !t_cal_sq.is_finite() || t_cal_sq <= 0.0 {
return FALLBACK_TWIST_IN;
}
let twist_in = t_cal_sq.sqrt() * d_in;
if twist_in.is_finite() && twist_in > 0.0 {
twist_in
} else {
FALLBACK_TWIST_IN
}
}
pub fn compute_spin_drift(
time_s: f64,
stability: f64,
twist_rate: f64,
is_twist_right: bool,
) -> f64 {
compute_spin_drift_with_decay(time_s, stability, twist_rate, is_twist_right, None)
}
pub fn compute_spin_drift_with_decay(
time_s: f64,
stability: f64,
twist_rate: f64,
is_twist_right: bool,
decay_factor: Option<f64>, ) -> f64 {
if stability == 0.0 || time_s <= 0.0 || twist_rate == 0.0 {
return 0.0;
}
let sign = if is_twist_right { 1.0 } else { -1.0 };
let scaling_factor = 1.25;
let base_drift = sign * scaling_factor * (stability + 1.2) * time_s.powf(1.83);
let effective_drift = if let Some(decay) = decay_factor {
base_drift * decay.max(0.0).min(1.0)
} else {
base_drift
};
effective_drift * 0.0254
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_inputs() -> BallisticInputs {
BallisticInputs {
muzzle_velocity: 823.0, bc_value: 0.5,
bullet_mass: 0.0109, bullet_diameter: 0.00782, bullet_length: 0.033, twist_rate: 10.0,
..Default::default()
}
}
#[test]
fn test_compute_stability_coefficient() {
let inputs = create_test_inputs();
let atmo_params = (0.0, 15.0, 1013.25, 1.0);
let stability = compute_stability_coefficient(&inputs, atmo_params);
println!("Computed stability: {}", stability);
assert!(stability > 0.0);
assert!(stability < 10.0);
assert!(stability > 1.0);
assert!(stability < 3.0);
}
#[test]
fn test_compute_stability_coefficient_zero_cases() {
let mut inputs = create_test_inputs();
let atmo_params = (0.0, 15.0, 1013.25, 1.0);
inputs.twist_rate = 0.0;
assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
inputs = create_test_inputs();
inputs.bullet_length = 0.0;
assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
inputs = create_test_inputs();
inputs.bullet_diameter = 0.0;
assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
}
#[test]
fn test_compute_stability_coefficient_atmospheric_effects() {
let inputs = create_test_inputs();
let standard_atmo = (0.0, 15.0, 1013.25, 1.0);
let standard_stability = compute_stability_coefficient(&inputs, standard_atmo);
let high_alt_atmo = (3000.0, 5.0, 900.0, 1.0);
let high_alt_stability = compute_stability_coefficient(&inputs, high_alt_atmo);
assert!(high_alt_stability > standard_stability);
let hot_atmo = (0.0, 35.0, 1013.25, 1.0);
let hot_stability = compute_stability_coefficient(&inputs, hot_atmo);
assert!(hot_stability > standard_stability);
}
#[test]
fn test_compute_spin_drift() {
let time_s = 1.5;
let stability = 2.0;
let twist_rate = 10.0;
let drift_right = compute_spin_drift(time_s, stability, twist_rate, true);
assert!(drift_right > 0.0);
let drift_left = compute_spin_drift(time_s, stability, twist_rate, false);
assert!(drift_left < 0.0); assert!((drift_left + drift_right).abs() < 1e-10);
assert!(drift_right.abs() < 0.25); }
#[test]
fn test_compute_spin_drift_zero_cases() {
assert_eq!(compute_spin_drift(1.5, 0.0, 10.0, true), 0.0);
assert_eq!(compute_spin_drift(0.0, 2.0, 10.0, true), 0.0);
assert_eq!(compute_spin_drift(-1.0, 2.0, 10.0, true), 0.0);
assert_eq!(compute_spin_drift(1.5, 2.0, 0.0, true), 0.0);
}
const GR_TO_KG: f64 = 0.00006479891;
const IN_TO_M: f64 = 0.0254;
fn len_in(diameter_in: f64, mass_gr: f64) -> f64 {
estimate_bullet_length_m(diameter_in * IN_TO_M, mass_gr * GR_TO_KG) / IN_TO_M
}
fn twist_in(diameter_in: f64, mass_gr: f64, v_fps: f64) -> f64 {
default_twist_inches(diameter_in * IN_TO_M, mass_gr * GR_TO_KG, v_fps * 0.3048)
}
#[test]
fn test_estimate_bullet_length_reference_bullets() {
let cases = [
(0.308, 175.0, 1.24, 0.06), (0.224, 77.0, 0.99, 0.06), (0.338, 300.0, 1.74, 0.06), (0.224, 55.0, 0.72, 0.05), (0.510, 750.0, 1.90, 0.08), ];
for (d, m, expected, tol) in cases {
let got = len_in(d, m);
assert!(
(got - expected).abs() < tol,
".{d}/{m}gr length: expected ~{expected}\", got {got:.4}\" (L/d {:.2})",
got / d
);
}
}
#[test]
fn test_estimate_bullet_length_degenerate_inputs() {
assert_eq!(estimate_bullet_length_m(0.00782, 0.0), 0.0);
assert_eq!(estimate_bullet_length_m(0.00782, -1.0), 0.0);
assert_eq!(estimate_bullet_length_m(0.0, 0.011), 0.0);
assert_eq!(estimate_bullet_length_m(-0.1, 0.011), 0.0);
assert_eq!(estimate_bullet_length_m(f64::NAN, 0.011), 0.0);
}
#[test]
fn test_estimate_bullet_length_clamps_ld_ratio() {
let d = 0.172 * IN_TO_M;
let long = estimate_bullet_length_m(d, 0.05); assert!((long / d - 6.5).abs() < 1e-9, "expected clamp at 6.5 cal, got {}", long / d);
let short = estimate_bullet_length_m(0.510 * IN_TO_M, 0.001);
assert!(
(short / (0.510 * IN_TO_M) - 2.5).abs() < 1e-9,
"expected clamp at 2.5 cal, got {}",
short / (0.510 * IN_TO_M)
);
}
#[test]
fn test_default_twist_reference_bullets() {
let cases = [
(0.308, 175.0, 2600.0, 11.2, 1.2), (0.224, 55.0, 3240.0, 10.1, 1.5), (0.224, 77.0, 2750.0, 7.2, 1.2), (0.264, 140.0, 2700.0, 7.7, 1.2), ];
for (d, m, v, expected, tol) in cases {
let got = twist_in(d, m, v);
assert!(got > 0.0, ".{d}/{m}gr twist must be positive, got {got}");
assert!(
(got - expected).abs() < tol,
".{d}/{m}gr twist: expected ~1:{expected}\", got 1:{got:.2}\"",
);
}
}
#[test]
fn test_default_twist_yields_target_sg() {
let d_m = 0.308 * IN_TO_M;
let m_kg = 175.0 * GR_TO_KG;
let v_mps = 2600.0 * 0.3048;
let twist = default_twist_inches(d_m, m_kg, v_mps);
let inputs = BallisticInputs {
muzzle_velocity: v_mps,
bullet_mass: m_kg,
bullet_diameter: d_m,
bullet_length: estimate_bullet_length_m(d_m, m_kg),
twist_rate: twist,
..Default::default()
};
let sg = compute_stability_coefficient(&inputs, (0.0, 15.0, 1013.25, 1.0));
assert!(
(sg - DEFAULT_TWIST_SG_TARGET).abs() < 0.02,
"expected Sg ~{DEFAULT_TWIST_SG_TARGET}, got {sg}"
);
}
#[test]
fn test_default_twist_degenerate_inputs_fall_back() {
assert_eq!(default_twist_inches(0.0, 0.011, 800.0), 12.0);
assert_eq!(default_twist_inches(0.00782, 0.0, 800.0), 12.0);
assert_eq!(default_twist_inches(0.00782, 0.011, 0.0), 12.0);
assert_eq!(default_twist_inches(f64::NAN, 0.011, 800.0), 12.0);
}
#[test]
fn test_heavier_bullet_needs_faster_twist() {
let light = twist_in(0.224, 55.0, 2900.0);
let heavy = twist_in(0.224, 77.0, 2900.0);
assert!(heavy < light, "77gr twist {heavy} should be faster than 55gr {light}");
}
#[test]
fn test_print_reference_estimates() {
println!("\n=== MBA-1135 estimate_bullet_length_m ===");
for (d, m) in [
(0.308, 175.0),
(0.224, 77.0),
(0.338, 300.0),
(0.224, 55.0),
(0.510, 750.0),
(0.264, 140.0),
] {
let l = len_in(d, m);
println!(".{d}/{m}gr -> {l:.4}\" (L/d {:.3})", l / d);
}
println!("\n=== MBA-1135 default_twist_inches (SG=1.5 vs 2.0) ===");
for (d, m, v) in [
(0.308, 175.0, 2600.0),
(0.224, 55.0, 3240.0),
(0.224, 77.0, 2750.0),
(0.264, 140.0, 2700.0),
] {
let t20 = twist_in(d, m, v);
let t15 = t20 * (DEFAULT_TWIST_SG_TARGET / 1.5_f64).sqrt();
println!(".{d}/{m}gr @ {v}fps -> SG1.5: 1:{t15:.2}\" SG2.0: 1:{t20:.2}\"");
}
println!();
}
#[test]
fn test_compute_spin_drift_scaling() {
let stability = 2.0;
let twist_rate = 10.0;
let drift_1s = compute_spin_drift(1.0, stability, twist_rate, true);
let drift_2s = compute_spin_drift(2.0, stability, twist_rate, true);
assert!(drift_2s > drift_1s);
let drift_low_stability = compute_spin_drift(1.5, 1.0, twist_rate, true);
let drift_high_stability = compute_spin_drift(1.5, 3.0, twist_rate, true);
assert!(drift_high_stability > drift_low_stability);
}
}