Skip to main content

ballistics_engine/
stability.rs

1use crate::InternalBallisticInputs as BallisticInputs;
2
3/// Calculate the gyroscopic stability coefficient (SG) for the bullet.
4///
5/// This function uses the Miller stability formula. An SG value greater than 1.5
6/// is generally considered to indicate adequate stability.
7///
8/// # Arguments
9/// * `inputs` - Ballistic input parameters
10/// * `atmo_params` - Atmospheric parameters (altitude, temp_c, pressure_hpa, density_ratio)
11///
12/// # Returns
13/// * Stability coefficient (dimensionless)
14pub fn compute_stability_coefficient(
15    inputs: &BallisticInputs,
16    atmo_params: (f64, f64, f64, f64),
17) -> f64 {
18    // Check for required parameters
19    if inputs.twist_rate == 0.0 || inputs.bullet_length == 0.0 || inputs.bullet_diameter == 0.0 {
20        return 0.0;
21    }
22
23    // Pre-calculated constants for efficiency
24    const MILLER_CONST: f64 = 30.0;
25    const TEMP_REF_K: f64 = 288.15; // 15°C
26    const PRESS_REF_HPA: f64 = 1013.25;
27
28    // Calculate intermediate values
29    // Convert twist rate from inches to meters for consistency
30    let twist_rate_m = inputs.twist_rate.abs() * 0.0254; // inches to meters
31    let twist_calibers = twist_rate_m / inputs.bullet_diameter;
32    let length_calibers = inputs.bullet_length / inputs.bullet_diameter;
33
34    // Convert units for Miller formula
35    let mass_grains = inputs.bullet_mass / 0.00006479891; // kg to grains
36    let diameter_inches = inputs.bullet_diameter / 0.0254; // meters to inches
37
38    // Miller stability formula components
39    let mass_term = MILLER_CONST * mass_grains;
40    let geom_term = twist_calibers.powi(2)
41        * diameter_inches.powi(3)
42        * length_calibers
43        * (1.0 + length_calibers.powi(2));
44
45    if geom_term == 0.0 {
46        return 0.0;
47    }
48
49    // Extract atmospheric parameters
50    let (_, temp_c, current_press_hpa, _) = atmo_params;
51    let temp_k = temp_c + 273.15;
52
53    // Atmospheric density correction factor
54    // Ratio of reference density to current density
55    let density_correction = (temp_k / TEMP_REF_K) * (PRESS_REF_HPA / current_press_hpa);
56
57    // Velocity correction factor
58    let velocity_correction = miller_velocity_correction(inputs.muzzle_velocity);
59
60    // Final stability calculation
61
62    (mass_term / geom_term) * velocity_correction * density_correction
63}
64
65/// Effective density (kg/m^3) used by the mass-based bullet-length estimate.
66///
67/// A jacketed lead-core bullet is not a solid cylinder of lead (11340 kg/m^3): the ogive/boat-tail
68/// taper and the copper jacket lower the *effective* density that relates total mass to a simple
69/// `length * frontal-area` cylinder. 7600 kg/m^3 was fit so common lead-core match bullets land at
70/// their real length / caliber ratio (see reference-bullet unit tests below). MBA-1135.
71pub const BULLET_LENGTH_RHO_EFF_KG_M3: f64 = 7600.0;
72
73/// Lower / upper bound on the estimated length-to-diameter ratio (calibers). Keeps degenerate
74/// mass/diameter combinations inside the range of real small-arms projectiles, including short
75/// handgun bullets.
76const MIN_LENGTH_CALIBERS: f64 = 1.2;
77const MAX_LENGTH_CALIBERS: f64 = 6.5;
78
79/// Gyroscopic-stability target used to synthesize a default twist when the shooter does not
80/// supply one (MBA-1135).
81///
82/// Chosen = **2.0** (see [`default_twist_inches`] docs for the reference-bullet comparison that
83/// motivated 2.0 over 1.5). A synthesized default should slightly *over*-stabilize rather than
84/// under-stabilize, and 2.0 reproduces the real factory twists of the twist-sensitive high-BC
85/// match bullets (.308/175 gr -> ~1:11", 6.5mm/140 gr -> ~1:8") most faithfully.
86pub const DEFAULT_TWIST_SG_TARGET: f64 = 2.0;
87
88const GRAINS_PER_KG: f64 = 1.0 / 0.00006479891;
89const METERS_PER_INCH: f64 = 0.0254;
90const MPS_TO_FPS: f64 = 3.28084;
91const MILLER_VEL_REF_FPS: f64 = 2800.0;
92
93/// Miller's cube-root launch-velocity correction relative to 2800 ft/s.
94///
95/// Gyroscopic stability is a launch property, so callers must pass muzzle velocity rather than
96/// re-evaluating this term from the decaying downrange airspeed.
97pub(crate) fn miller_velocity_correction(muzzle_velocity_mps: f64) -> f64 {
98    (muzzle_velocity_mps * MPS_TO_FPS / MILLER_VEL_REF_FPS).powf(1.0 / 3.0)
99}
100
101/// Estimate a bullet's length (meters) from its diameter and mass using a constant-effective-density
102/// cylinder model (MBA-1135).
103///
104/// Replaces the historical mass-blind `diameter * 4.5` fallback: two bullets of the same caliber but
105/// very different weights get very different lengths, which is what the Miller stability formula and
106/// the enhanced spin-drift / Magnus models actually need.
107///
108/// Model: `L = mass / (rho_eff * (pi/4) * d^2)` with `rho_eff = 7600 kg/m^3`, then the length/diameter
109/// ratio is clamped to `[1.2, 6.5]` calibers so pathological inputs stay physical without stretching
110/// common handgun bullets to rifle-like proportions.
111///
112/// Returns `0.0` for non-physical inputs (`mass_kg <= 0` or `diameter_m <= 0`); callers that need a
113/// non-zero length should fall back to their historical literal in that case.
114pub fn estimate_bullet_length_m(diameter_m: f64, mass_kg: f64) -> f64 {
115    if mass_kg <= 0.0 || diameter_m <= 0.0 || !mass_kg.is_finite() || !diameter_m.is_finite() {
116        return 0.0;
117    }
118
119    let frontal_area = std::f64::consts::FRAC_PI_4 * diameter_m * diameter_m;
120    let raw_length = mass_kg / (BULLET_LENGTH_RHO_EFF_KG_M3 * frontal_area);
121
122    // Clamp the length-to-diameter ratio (calibers) into a physically sensible band.
123    let length_calibers = (raw_length / diameter_m).clamp(MIN_LENGTH_CALIBERS, MAX_LENGTH_CALIBERS);
124    length_calibers * diameter_m
125}
126
127/// Synthesize a default barrel twist (inches per turn) for a bullet whose twist the shooter did not
128/// supply, by inverting the Miller stability formula for the twist that yields
129/// [`DEFAULT_TWIST_SG_TARGET`] (MBA-1135).
130///
131/// This replaces the mass-blind fixed `1:12"` fallback: heavier / longer bullets correctly demand a
132/// faster twist. The bullet length is taken from [`estimate_bullet_length_m`] (so the whole default
133/// is caliber + weight aware), the velocity term is `(v_fps / 2800)^(1/3)` at the supplied muzzle
134/// velocity, and the density correction is `1.0` (sea-level ICAO standard).
135///
136/// Inverting `Sg = 30 m_gr * vel_corr / (t_cal^2 * d_in^3 * L_cal * (1 + L_cal^2))`:
137///
138/// `t_cal = sqrt( 30 m_gr * vel_corr / (Sg_target * d_in^3 * L_cal * (1 + L_cal^2)) )`,
139/// `twist_in = t_cal * d_in`.
140///
141/// `Sg_target` was selected by comparing 1.5 vs 2.0 against common factory twists
142/// (velocities: .308/175 gr @ 2600 fps, .223/55 gr @ 3240 fps, .224/77 gr @ 2750 fps,
143/// 6.5mm/140 gr @ 2700 fps):
144///
145/// | bullet          | factory | Sg=1.5 | Sg=2.0 |
146/// |-----------------|---------|--------|--------|
147/// | .308 / 175 gr   | 1:10-12"| 1:12.9"| 1:11.2"|
148/// | .223 / 55 gr    | ~1:12"  | 1:11.7"| 1:10.1"|
149/// | .224 / 77 gr    | ~1:8"   | 1:8.4" | 1:7.2" |
150/// | 6.5mm / 140 gr  | ~1:8"   | 1:8.9" | 1:7.7" |
151///
152/// 2.0 wins: Sg=1.5 gives 1:12.9" for the 175 gr match bullet (no real .308 barrel is that slow and
153/// it under-stabilizes 175 gr), whereas 2.0's 1:11.2" / 1:8" are exactly the standard twists for the
154/// twist-sensitive high-BC bullets, and slight over-stabilization is the safe direction for a default.
155///
156/// Guards degenerate inputs (`diameter_m`, `mass_kg`, or `muzzle_velocity_mps` <= 0) by returning the
157/// historical `12.0` (1:12") fallback. Always returns a positive inches-per-turn value.
158pub fn default_twist_inches(diameter_m: f64, mass_kg: f64, muzzle_velocity_mps: f64) -> f64 {
159    const FALLBACK_TWIST_IN: f64 = 12.0;
160
161    if diameter_m <= 0.0
162        || mass_kg <= 0.0
163        || muzzle_velocity_mps <= 0.0
164        || !diameter_m.is_finite()
165        || !mass_kg.is_finite()
166        || !muzzle_velocity_mps.is_finite()
167    {
168        return FALLBACK_TWIST_IN;
169    }
170
171    let length_m = estimate_bullet_length_m(diameter_m, mass_kg);
172    if length_m <= 0.0 {
173        return FALLBACK_TWIST_IN;
174    }
175
176    let d_in = diameter_m / METERS_PER_INCH;
177    let m_gr = mass_kg * GRAINS_PER_KG;
178    let l_cal = length_m / diameter_m;
179    let velocity_correction = miller_velocity_correction(muzzle_velocity_mps);
180
181    let geom = d_in.powi(3) * l_cal * (1.0 + l_cal * l_cal);
182    let denom = DEFAULT_TWIST_SG_TARGET * geom;
183    if denom <= 0.0 {
184        return FALLBACK_TWIST_IN;
185    }
186
187    let t_cal_sq = 30.0 * m_gr * velocity_correction / denom;
188    if !t_cal_sq.is_finite() || t_cal_sq <= 0.0 {
189        return FALLBACK_TWIST_IN;
190    }
191
192    let twist_in = t_cal_sq.sqrt() * d_in;
193    if twist_in.is_finite() && twist_in > 0.0 {
194        twist_in
195    } else {
196        FALLBACK_TWIST_IN
197    }
198}
199
200/// Preserve a supplied twist (already converted to inches per turn), or synthesize the
201/// caliber/weight/velocity-aware default used by frontend argument parsers.
202#[cfg(any(target_arch = "wasm32", test))]
203pub(crate) fn resolve_twist_inches(
204    explicit_twist_inches: Option<f64>,
205    diameter_m: f64,
206    mass_kg: f64,
207    muzzle_velocity_mps: f64,
208) -> f64 {
209    explicit_twist_inches
210        .unwrap_or_else(|| default_twist_inches(diameter_m, mass_kg, muzzle_velocity_mps))
211}
212
213/// Calculate spin drift in meters using Litz approximation.
214///
215/// # Arguments
216/// * `time_s` - Time of flight in seconds
217/// * `stability` - Stability coefficient
218/// * `twist_rate` - Twist rate in inches (calibers per turn)
219/// * `is_twist_right` - True for right-hand twist, false for left-hand
220///
221/// # Returns
222/// * Spin drift in meters
223pub fn compute_spin_drift(
224    time_s: f64,
225    stability: f64,
226    twist_rate: f64,
227    is_twist_right: bool,
228) -> f64 {
229    compute_spin_drift_with_decay(time_s, stability, twist_rate, is_twist_right, None)
230}
231
232/// Calculate Litz spin drift with an optional extra retained-spin multiplier.
233///
234/// Canonical solver paths pass no multiplier because the empirical Litz time exponent already
235/// reflects real spin history. `Some` remains for source compatibility and experimental callers
236/// intentionally applying an extra scalar; canonical solvers must not use it for spin decay.
237pub fn compute_spin_drift_with_decay(
238    time_s: f64,
239    stability: f64,
240    twist_rate: f64,
241    is_twist_right: bool,
242    decay_factor: Option<f64>, // Optional spin decay factor (0-1)
243) -> f64 {
244    if stability == 0.0 || time_s <= 0.0 || twist_rate == 0.0 {
245        return 0.0;
246    }
247
248    let sign = if is_twist_right { 1.0 } else { -1.0 };
249
250    // Litz empirical spin drift: inches = 1.25 * (SG + 1.2) * TOF^1.83.
251    // Keep this summary/API path consistent with cli_api::apply_spin_drift.
252    let scaling_factor = 1.25;
253    let base_drift = sign * scaling_factor * (stability + 1.2) * time_s.powf(1.83);
254
255    // Apply spin decay if provided
256    let effective_drift = if let Some(decay) = decay_factor {
257        base_drift * decay.max(0.0).min(1.0)
258    } else {
259        base_drift
260    };
261
262    // Convert inches to meters
263    effective_drift * 0.0254
264}
265
266#[cfg(test)]
267mod tests {
268    use super::*;
269
270    fn create_test_inputs() -> BallisticInputs {
271        BallisticInputs {
272            muzzle_velocity: 823.0, // 2700 fps in m/s
273            bc_value: 0.5,
274            bullet_mass: 0.0109,      // 168 grains in kg
275            bullet_diameter: 0.00782, // 0.308 inches in meters
276            bullet_length: 0.033,     // in meters (1.3 inches)
277            twist_rate: 10.0,
278            ..Default::default()
279        }
280    }
281
282    #[test]
283    fn test_compute_stability_coefficient() {
284        let inputs = create_test_inputs();
285        let atmo_params = (0.0, 15.0, 1013.25, 1.0); // Standard conditions
286
287        let stability = compute_stability_coefficient(&inputs, atmo_params);
288
289        // Debug output
290        println!("Computed stability: {}", stability);
291
292        // Should be a reasonable stability value
293        assert!(stability > 0.0);
294        assert!(stability < 10.0); // Sanity check
295
296        // Test with typical values should give stability around 1.5-2.5
297        assert!(stability > 1.0);
298        assert!(stability < 3.0);
299    }
300
301    #[test]
302    fn test_compute_stability_coefficient_zero_cases() {
303        let mut inputs = create_test_inputs();
304        let atmo_params = (0.0, 15.0, 1013.25, 1.0);
305
306        // Test with zero twist rate
307        inputs.twist_rate = 0.0;
308        assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
309
310        // Test with zero bullet length
311        inputs = create_test_inputs();
312        inputs.bullet_length = 0.0;
313        assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
314
315        // Test with zero bullet diameter
316        inputs = create_test_inputs();
317        inputs.bullet_diameter = 0.0;
318        assert_eq!(compute_stability_coefficient(&inputs, atmo_params), 0.0);
319    }
320
321    #[test]
322    fn test_compute_stability_coefficient_atmospheric_effects() {
323        let inputs = create_test_inputs();
324
325        // Standard conditions
326        let standard_atmo = (0.0, 15.0, 1013.25, 1.0);
327        let standard_stability = compute_stability_coefficient(&inputs, standard_atmo);
328
329        // High altitude (lower pressure, lower temperature)
330        let high_alt_atmo = (3000.0, 5.0, 900.0, 1.0);
331        let high_alt_stability = compute_stability_coefficient(&inputs, high_alt_atmo);
332
333        // High altitude should have higher stability due to lower air density
334        assert!(high_alt_stability > standard_stability);
335
336        // Hot conditions (higher temperature)
337        let hot_atmo = (0.0, 35.0, 1013.25, 1.0);
338        let hot_stability = compute_stability_coefficient(&inputs, hot_atmo);
339
340        // Hot conditions should have higher stability due to lower air density
341        assert!(hot_stability > standard_stability);
342    }
343
344    #[test]
345    fn test_compute_spin_drift() {
346        let time_s = 1.5;
347        let stability = 2.0;
348        let twist_rate = 10.0;
349
350        // Test right-hand twist
351        let drift_right = compute_spin_drift(time_s, stability, twist_rate, true);
352        assert!(drift_right > 0.0); // Should drift to the right (positive)
353
354        // Test left-hand twist
355        let drift_left = compute_spin_drift(time_s, stability, twist_rate, false);
356        assert!(drift_left < 0.0); // Should drift to the left (negative)
357        assert!((drift_left + drift_right).abs() < 1e-10); // Should be equal magnitude
358
359        // Litz spin drift remains a small correction for this flight time.
360        assert!(drift_right.abs() < 0.25); // Less than 25cm for 1.5s flight
361    }
362
363    #[test]
364    fn test_compute_spin_drift_zero_cases() {
365        // Test with zero stability
366        assert_eq!(compute_spin_drift(1.5, 0.0, 10.0, true), 0.0);
367
368        // Test with zero time
369        assert_eq!(compute_spin_drift(0.0, 2.0, 10.0, true), 0.0);
370
371        // Test with negative time
372        assert_eq!(compute_spin_drift(-1.0, 2.0, 10.0, true), 0.0);
373
374        // Test with zero twist rate
375        assert_eq!(compute_spin_drift(1.5, 2.0, 0.0, true), 0.0);
376    }
377
378    // --- MBA-1135: mass-based length + Miller-inverse twist defaults ---
379
380    const GR_TO_KG: f64 = 0.00006479891;
381    const IN_TO_M: f64 = 0.0254;
382
383    fn len_in(diameter_in: f64, mass_gr: f64) -> f64 {
384        estimate_bullet_length_m(diameter_in * IN_TO_M, mass_gr * GR_TO_KG) / IN_TO_M
385    }
386
387    fn twist_in(diameter_in: f64, mass_gr: f64, v_fps: f64) -> f64 {
388        default_twist_inches(diameter_in * IN_TO_M, mass_gr * GR_TO_KG, v_fps * 0.3048)
389    }
390
391    #[test]
392    fn test_estimate_bullet_length_reference_bullets() {
393        // (diameter_in, mass_gr, expected_in, tolerance_in)
394        let cases = [
395            (0.308, 175.0, 1.24, 0.06), // .308 175 gr match  -> L/d ~4.0
396            (0.224, 77.0, 0.99, 0.06),  // .224 77 gr         -> long for caliber
397            (0.338, 300.0, 1.74, 0.06), // .338 300 gr
398            (0.224, 55.0, 0.72, 0.05),  // .224 55 gr varmint -> L/d ~3.2
399            (0.510, 750.0, 1.90, 0.08), // .510 750 gr
400        ];
401        for (d, m, expected, tol) in cases {
402            let got = len_in(d, m);
403            assert!(
404                (got - expected).abs() < tol,
405                ".{d}/{m}gr length: expected ~{expected}\", got {got:.4}\" (L/d {:.2})",
406                got / d
407            );
408        }
409    }
410
411    #[test]
412    fn test_estimate_bullet_length_preserves_handgun_geometry() {
413        // These common handgun bullets are shorter than the old 2.5-caliber rifle-oriented floor.
414        // The safety clamp must preserve the effective-density model for all of them.
415        for (diameter_in, mass_gr, expected_ratio) in [
416            (0.355, 115.0, 1.702_847_900_515), // 9 mm
417            (0.451, 230.0, 1.660_968_083_966), // .45 ACP
418            (0.355, 90.0, 1.332_663_574_316),  // .380 ACP
419        ] {
420            let diameter_m = diameter_in * IN_TO_M;
421            let mass_kg = mass_gr * GR_TO_KG;
422            let frontal_area = std::f64::consts::FRAC_PI_4 * diameter_m * diameter_m;
423            let model_ratio = mass_kg / (BULLET_LENGTH_RHO_EFF_KG_M3 * frontal_area) / diameter_m;
424            let estimated_ratio = estimate_bullet_length_m(diameter_m, mass_kg) / diameter_m;
425
426            assert!((estimated_ratio - model_ratio).abs() < 1e-12);
427            assert!((estimated_ratio - expected_ratio).abs() < 1e-12);
428        }
429    }
430
431    #[test]
432    fn test_estimate_bullet_length_degenerate_inputs() {
433        assert_eq!(estimate_bullet_length_m(0.00782, 0.0), 0.0);
434        assert_eq!(estimate_bullet_length_m(0.00782, -1.0), 0.0);
435        assert_eq!(estimate_bullet_length_m(0.0, 0.011), 0.0);
436        assert_eq!(estimate_bullet_length_m(-0.1, 0.011), 0.0);
437        assert_eq!(estimate_bullet_length_m(f64::NAN, 0.011), 0.0);
438    }
439
440    #[test]
441    fn test_estimate_bullet_length_clamps_ld_ratio() {
442        const EXPECTED_MIN: f64 = 1.2;
443        const EXPECTED_MAX: f64 = 6.5;
444
445        let diameter_m = 0.355 * IN_TO_M;
446        for raw_ratio in [1.19_f64, 1.20, 1.21, 6.49, 6.50, 6.51] {
447            let mass_kg = raw_ratio
448                * BULLET_LENGTH_RHO_EFF_KG_M3
449                * std::f64::consts::FRAC_PI_4
450                * diameter_m.powi(3);
451            let actual_ratio = estimate_bullet_length_m(diameter_m, mass_kg) / diameter_m;
452            let expected_ratio = raw_ratio.clamp(EXPECTED_MIN, EXPECTED_MAX);
453
454            assert!(
455                (actual_ratio - expected_ratio).abs() < 1e-12,
456                "raw L/d {raw_ratio}: expected {expected_ratio}, got {actual_ratio}"
457            );
458        }
459    }
460
461    #[test]
462    fn test_default_twist_reference_bullets() {
463        // With DEFAULT_TWIST_SG_TARGET = 2.0, these should land near common factory twists.
464        // (diameter_in, mass_gr, v_fps, expected_twist_in, tolerance_in)
465        let cases = [
466            (0.308, 175.0, 2600.0, 11.2, 1.2), // .308 175 gr -> ~1:10-12"
467            (0.224, 55.0, 3240.0, 10.1, 1.5),  // .223 55 gr  -> ~1:10-12"
468            (0.224, 77.0, 2750.0, 7.2, 1.2),   // .224 77 gr  -> ~1:7-8"
469            (0.264, 140.0, 2700.0, 7.7, 1.2),  // 6.5mm 140 gr -> ~1:8"
470        ];
471        for (d, m, v, expected, tol) in cases {
472            let got = twist_in(d, m, v);
473            assert!(got > 0.0, ".{d}/{m}gr twist must be positive, got {got}");
474            assert!(
475                (got - expected).abs() < tol,
476                ".{d}/{m}gr twist: expected ~1:{expected}\", got 1:{got:.2}\"",
477            );
478        }
479    }
480
481    #[test]
482    fn test_default_twist_yields_target_sg() {
483        // By construction, at sea-level standard the synthesized twist should reproduce
484        // DEFAULT_TWIST_SG_TARGET when the same estimated length is used for the Sg check.
485        let d_m = 0.308 * IN_TO_M;
486        let m_kg = 175.0 * GR_TO_KG;
487        let v_mps = 2600.0 * 0.3048;
488        let twist = default_twist_inches(d_m, m_kg, v_mps);
489        let inputs = BallisticInputs {
490            muzzle_velocity: v_mps,
491            bullet_mass: m_kg,
492            bullet_diameter: d_m,
493            bullet_length: estimate_bullet_length_m(d_m, m_kg),
494            twist_rate: twist,
495            ..Default::default()
496        };
497        let sg = compute_stability_coefficient(&inputs, (0.0, 15.0, 1013.25, 1.0));
498        assert!(
499            (sg - DEFAULT_TWIST_SG_TARGET).abs() < 0.02,
500            "expected Sg ~{DEFAULT_TWIST_SG_TARGET}, got {sg}"
501        );
502    }
503
504    #[test]
505    fn test_default_twist_degenerate_inputs_fall_back() {
506        assert_eq!(default_twist_inches(0.0, 0.011, 800.0), 12.0);
507        assert_eq!(default_twist_inches(0.00782, 0.0, 800.0), 12.0);
508        assert_eq!(default_twist_inches(0.00782, 0.011, 0.0), 12.0);
509        assert_eq!(default_twist_inches(f64::NAN, 0.011, 800.0), 12.0);
510    }
511
512    #[test]
513    fn test_resolve_twist_preserves_explicit_or_uses_miller_default() {
514        let diameter_m = 0.224 * IN_TO_M;
515        let mass_kg = 77.0 * GR_TO_KG;
516        let velocity_mps = 2750.0 * 0.3048;
517        let expected_default = default_twist_inches(diameter_m, mass_kg, velocity_mps);
518
519        assert_eq!(
520            resolve_twist_inches(Some(9.5), diameter_m, mass_kg, velocity_mps).to_bits(),
521            9.5_f64.to_bits()
522        );
523        assert_eq!(
524            resolve_twist_inches(None, diameter_m, mass_kg, velocity_mps).to_bits(),
525            expected_default.to_bits()
526        );
527        assert_ne!(expected_default.to_bits(), 12.0_f64.to_bits());
528
529        let metric_default = resolve_twist_inches(
530            None,
531            5.6896 * 0.001,
532            4.98951607 * 0.001,
533            838.2,
534        );
535        assert!((metric_default - expected_default).abs() < 1e-12);
536    }
537
538    #[test]
539    fn test_heavier_bullet_needs_faster_twist() {
540        // Same caliber + velocity: the heavier (longer) bullet must get a faster (smaller) twist.
541        let light = twist_in(0.224, 55.0, 2900.0);
542        let heavy = twist_in(0.224, 77.0, 2900.0);
543        assert!(heavy < light, "77gr twist {heavy} should be faster than 55gr {light}");
544    }
545
546    #[test]
547    fn test_print_reference_estimates() {
548        // Diagnostic dump for the MBA-1135 report: `cargo test print_reference_estimates -- --nocapture`.
549        println!("\n=== MBA-1135 estimate_bullet_length_m ===");
550        for (d, m) in [
551            (0.308, 175.0),
552            (0.224, 77.0),
553            (0.338, 300.0),
554            (0.224, 55.0),
555            (0.510, 750.0),
556            (0.264, 140.0),
557        ] {
558            let l = len_in(d, m);
559            println!(".{d}/{m}gr -> {l:.4}\" (L/d {:.3})", l / d);
560        }
561        println!("\n=== MBA-1135 default_twist_inches (SG=1.5 vs 2.0) ===");
562        for (d, m, v) in [
563            (0.308, 175.0, 2600.0),
564            (0.224, 55.0, 3240.0),
565            (0.224, 77.0, 2750.0),
566            (0.264, 140.0, 2700.0),
567        ] {
568            // Reconstruct both targets by scaling: t_cal ~ 1/sqrt(Sg) so twist(1.5)=twist(2.0)*sqrt(2.0/1.5).
569            let t20 = twist_in(d, m, v);
570            let t15 = t20 * (DEFAULT_TWIST_SG_TARGET / 1.5_f64).sqrt();
571            println!(".{d}/{m}gr @ {v}fps -> SG1.5: 1:{t15:.2}\"  SG2.0: 1:{t20:.2}\"");
572        }
573        println!();
574    }
575
576    #[test]
577    fn test_compute_spin_drift_scaling() {
578        let stability = 2.0;
579        let twist_rate = 10.0;
580
581        // Test time scaling
582        let drift_1s = compute_spin_drift(1.0, stability, twist_rate, true);
583        let drift_2s = compute_spin_drift(2.0, stability, twist_rate, true);
584
585        // Drift should increase with time (non-linearly due to 1.83 exponent)
586        assert!(drift_2s > drift_1s);
587
588        // Test stability scaling
589        let drift_low_stability = compute_spin_drift(1.5, 1.0, twist_rate, true);
590        let drift_high_stability = compute_spin_drift(1.5, 3.0, twist_rate, true);
591
592        // Higher stability should produce more drift
593        assert!(drift_high_stability > drift_low_stability);
594    }
595}