Skip to main content

ballistics_engine/
fast_trajectory.rs

1//! Fast trajectory solver for longer ranges.
2//!
3//! This is a Rust implementation of the fast fixed-step trajectory solver
4//! that provides significant performance improvements for long-range calculations.
5
6use crate::{
7    atmosphere::get_local_atmosphere,
8    constants::{G_ACCEL_MPS2, MPS_TO_FPS},
9    drag::get_drag_coefficient,
10    wind::WindSock,
11    BCSegmentData, DragModel, InternalBallisticInputs as BallisticInputs,
12};
13use nalgebra::Vector3;
14
15/// Fast solution container matching Python implementation
16#[derive(Debug, Clone)]
17pub struct FastSolution {
18    /// Time points
19    pub t: Vec<f64>,
20    /// State vectors at each time point [6 x n_points]
21    pub y: Vec<Vec<f64>>,
22    /// Event times [target_hit, max_ord, ground_hit]
23    pub t_events: [Vec<f64>; 3],
24    /// Whether integration succeeded
25    pub success: bool,
26}
27
28impl FastSolution {
29    /// Interpolate solution at time t
30    pub fn sol(&self, t_query: &[f64]) -> Vec<Vec<f64>> {
31        let mut result = vec![vec![0.0; t_query.len()]; 6];
32
33        for (i, &tq) in t_query.iter().enumerate() {
34            // Find the right interval using binary search
35            // Use unwrap_or to safely handle NaN values by treating them as greater
36            let idx = match self
37                .t
38                .binary_search_by(|&t| t.partial_cmp(&tq).unwrap_or(std::cmp::Ordering::Greater))
39            {
40                Ok(idx) => idx,
41                Err(idx) => idx,
42            };
43
44            if idx == 0 {
45                // Before first point
46                for j in 0..6 {
47                    result[j][i] = self.y[j][0];
48                }
49            } else if idx >= self.t.len() {
50                // After last point
51                for j in 0..6 {
52                    result[j][i] = self.y[j][self.t.len() - 1];
53                }
54            } else {
55                // Linear interpolation
56                let t0 = self.t[idx - 1];
57                let t1 = self.t[idx];
58                let span = t1 - t0;
59
60                for j in 0..6 {
61                    let y0 = self.y[j][idx - 1];
62                    let y1 = self.y[j][idx];
63                    result[j][i] = if span.abs() < f64::EPSILON {
64                        y1
65                    } else {
66                        let frac = (tq - t0) / span;
67                        y0 + frac * (y1 - y0)
68                    };
69                }
70            }
71        }
72
73        result
74    }
75
76    /// Convert from row-major to column-major format for compatibility
77    pub fn from_trajectory_data(
78        times: Vec<f64>,
79        states: Vec<[f64; 6]>,
80        t_events: [Vec<f64>; 3],
81    ) -> Self {
82        let n_points = times.len();
83        let mut y = vec![vec![0.0; n_points]; 6];
84
85        for (i, state) in states.iter().enumerate() {
86            for j in 0..6 {
87                y[j][i] = state[j];
88            }
89        }
90
91        FastSolution {
92            t: times,
93            y,
94            t_events,
95            success: true,
96        }
97    }
98
99    /// A clearly-failed solution carrying only the launch state, with `success = false`.
100    /// Used when inputs are too degenerate to integrate (e.g. non-physical atmosphere),
101    /// so callers see `success = false` instead of a stub trajectory reported as success.
102    fn degenerate(initial_state: &[f64; 6]) -> Self {
103        let mut y = vec![Vec::new(); 6];
104        for (j, slot) in y.iter_mut().enumerate() {
105            slot.push(initial_state[j]);
106        }
107        FastSolution {
108            t: vec![0.0],
109            y,
110            t_events: [Vec::new(), Vec::new(), Vec::new()],
111            success: false,
112        }
113    }
114}
115
116/// True if `atmo_params` can yield a finite, positive air density. `atmo_params` has TWO
117/// modes that `compute_derivatives` distinguishes:
118///   * **Standard**: `(base_alt_m, base_temp_c, base_pressure_hPa, base_density_ratio)` —
119///     positive station pressure. (Slot 3 is a density RATIO, not humidity, despite the
120///     `humidity` field it lands in via `build_inputs`.)
121///   * **Direct**: `(air_density, speed_of_sound, 0.0, 0.0)` — slots 2 and 3 are `0.0`
122///     sentinels, with a real density (< 2.0 kg/m³) and speed of sound (> 200 m/s).
123///
124/// A pressure <= 0 that ISN'T the direct-mode sentinel (or any non-finite value) — often a
125/// unit mistake like inHg where hPa is expected — gives zero/NaN density and would silently
126/// truncate the integration to a single point, so it's rejected. (Earlier this guard also
127/// rejected legitimate direct-mode input; the direct-mode allowance below fixes that.)
128fn atmo_is_physical(atmo_params: (f64, f64, f64, f64)) -> bool {
129    let (a, b, c, d) = atmo_params;
130    if !(a.is_finite() && b.is_finite() && c.is_finite() && d.is_finite()) {
131        return false;
132    }
133    // Direct-atmosphere mode: (density, speed_of_sound, 0, 0). Constants mirror
134    // derivatives.rs (MAX_REALISTIC_DENSITY = 2.0 kg/m³, MIN_REALISTIC_SPEED_OF_SOUND = 200 m/s).
135    let direct_mode = c == 0.0 && d == 0.0 && a > 0.0 && a < 2.0 && b > 200.0;
136    // Standard mode: positive station pressure (hPa).
137    direct_mode || c > 0.0
138}
139
140/// Fast trajectory integration parameters
141pub struct FastIntegrationParams {
142    pub horiz: f64,
143    pub vert: f64,
144    pub initial_state: [f64; 6],
145    pub t_span: (f64, f64),
146    /// Dual-mode atmosphere tuple — see [`atmo_is_physical`]. Standard mode is
147    /// `(base_alt_m, base_temp_c, base_pressure_hPa, base_density_ratio)` (slot 3 is a density
148    /// RATIO, not humidity); direct mode is `(air_density, speed_of_sound, 0.0, 0.0)`.
149    pub atmo_params: (f64, f64, f64, f64),
150}
151
152/// Aerodynamic-jump vertical launch-angle offset (radians) for the fast-integrate path.
153///
154/// Bryan Litz's crosswind estimator (`Y = 0.01*Sg - 0.0024*L + 0.032` MOA/mph) fed by the
155/// engine's Miller Sg. The fast path receives a prebuilt initial velocity (no muzzle angle),
156/// so the caller rotates that velocity by this offset. Returns 0 when the feature is off or
157/// the inputs are degenerate. Crosswind is taken from `wind_speed`/`wind_angle`
158/// (BallisticInputs convention: 0 = headwind, +90deg = from the right). MBA-959, EXPERIMENTAL.
159pub fn aerodynamic_jump_launch_offset_rad(
160    inputs: &BallisticInputs,
161    atmo_params: (f64, f64, f64, f64),
162) -> f64 {
163    if !inputs.enable_aerodynamic_jump {
164        return 0.0;
165    }
166    let diameter = inputs.bullet_diameter;
167    if !(inputs.twist_rate.is_finite() && inputs.twist_rate != 0.0)
168        || !(diameter.is_finite() && diameter > 0.0)
169        || !(inputs.bullet_length.is_finite() && inputs.bullet_length > 0.0)
170        || !inputs.muzzle_velocity.is_finite()
171    {
172        return 0.0;
173    }
174    let sg = crate::stability::compute_stability_coefficient(inputs, atmo_params);
175    if !(sg.is_finite() && sg > 0.0) {
176        return 0.0;
177    }
178    let length_cal = inputs.bullet_length / diameter;
179    const MS_TO_MPH: f64 = 2.236_936_292_054_4;
180    let crosswind_from_right_mph = inputs.wind_speed * inputs.wind_angle.sin() * MS_TO_MPH;
181    let vertical_moa = crate::aerodynamic_jump::litz_crosswind_jump_moa(
182        sg,
183        length_cal,
184        crosswind_from_right_mph,
185        inputs.is_twist_right,
186    );
187    if !vertical_moa.is_finite() {
188        return 0.0;
189    }
190    const MOA_PER_RAD: f64 = 3437.7467707849;
191    vertical_moa / MOA_PER_RAD
192}
193
194/// Rotate a McCoy-frame state's velocity (indices 3..6) by `theta_rad` in the vertical
195/// (downrange–vertical) plane, preserving speed and horizontal heading. Positive = up.
196fn rotate_launch_velocity(state: &mut [f64; 6], theta_rad: f64) {
197    let (vx, vy, vz) = (state[3], state[4], state[5]);
198    let speed = (vx * vx + vy * vy + vz * vz).sqrt();
199    if speed <= 0.0 {
200        return;
201    }
202    let h = (vx * vx + vz * vz).sqrt(); // horizontal (downrange+lateral) speed
203    let new_elev = vy.atan2(h) + theta_rad;
204    state[4] = speed * new_elev.sin();
205    let new_h = speed * new_elev.cos();
206    let scale = if h > 1e-12 { new_h / h } else { 0.0 };
207    state[3] = vx * scale;
208    state[5] = vz * scale;
209}
210
211/// Fast fixed-step integration for longer trajectories
212pub fn fast_integrate(
213    inputs: &BallisticInputs,
214    wind_sock: &WindSock,
215    params: FastIntegrationParams,
216) -> FastSolution {
217    // Degenerate atmosphere -> non-physical air density would silently stub the run.
218    if !atmo_is_physical(params.atmo_params) {
219        return FastSolution::degenerate(&params.initial_state);
220    }
221    let mut effective_inputs = inputs.clone();
222    if params.atmo_params.2 > 0.0 {
223        effective_inputs.altitude = params.atmo_params.0;
224        effective_inputs.temperature = params.atmo_params.1;
225        effective_inputs.pressure = params.atmo_params.2;
226        effective_inputs.humidity = params.atmo_params.3;
227    }
228    let inputs = &effective_inputs;
229    // Extract parameters
230    let _mass_kg = inputs.bullet_mass; // SI (kg)
231    let bc = inputs.bc_value;
232    let drag_model = &inputs.bc_type;
233
234    // Check for BC segments
235    let has_bc_segments =
236        inputs.bc_segments.is_some() && !inputs.bc_segments.as_ref().unwrap().is_empty();
237    let has_bc_segments_data =
238        inputs.bc_segments_data.is_some() && !inputs.bc_segments_data.as_ref().unwrap().is_empty();
239
240    // Time step - adjust based on distance
241    let dt = if params.horiz > 200.0 {
242        0.001
243    } else if params.horiz > 100.0 {
244        0.0005
245    } else {
246        0.0001
247    };
248
249    // Maximum integration time. This bounds BOTH the step-array pre-allocation (n_steps) AND the
250    // integration loop itself (the loop runs for at most n_steps-1 iterations); the
251    // hit_target / hit_ground early-breaks below terminate the loop sooner for real shots. Estimate
252    // the flight time from the HORIZONTAL velocity with a 4x margin: the previous 2x estimate using
253    // the FULL muzzle speed truncated long-range trajectories once drag slowed the bullet (real
254    // time of flight to the target far exceeds horiz/v0), so Monte Carlo reported impact metrics
255    // at a too-short downrange. NOTE: the 4x factor is a heuristic, NOT a proven upper bound — it
256    // can still be exceeded by extreme high-drag / high-launch-angle shots, which would truncate
257    // the loop before impact.
258    // MBA-959: aerodynamic jump perturbs the prebuilt launch velocity vertically (this path is
259    // handed an initial_state, not a muzzle angle). A no-op returning the original when disabled.
260    let mut initial_state = params.initial_state;
261    let aj_offset = aerodynamic_jump_launch_offset_rad(inputs, params.atmo_params);
262    if aj_offset != 0.0 {
263        rotate_launch_velocity(&mut initial_state, aj_offset);
264    }
265    let vx = initial_state[3]; // horizontal (downrange) velocity
266    let t_max = if vx > 1e-6 && params.horiz > 0.0 {
267        (4.0 * params.horiz / vx).min(params.t_span.1)
268    } else {
269        params.t_span.1
270    };
271
272    // Initialize arrays
273    let n_steps = ((t_max / dt) as usize) + 1;
274    let mut times = Vec::with_capacity(n_steps);
275    let mut states = Vec::with_capacity(n_steps);
276
277    // Initial state (with the aerodynamic-jump launch perturbation applied above)
278    times.push(0.0);
279    states.push(initial_state);
280
281    // Base drag density = the muzzle (shooter-altitude) density. atmo_params.3 is base_ratio
282    // = air_density/1.225 at the shooter altitude (the MC caller computes it via
283    // calculate_atmosphere). Previously this called get_local_atmosphere with query alt 0.0
284    // while base_alt = shooter altitude, which re-scaled that ratio DOWN to sea level —
285    // discarding the correct altitude density and inflating drag for every elevated MC run.
286    // Guard a missing/absent ratio (base_ratio <= 0, e.g. legacy or uninitialized atmo_params):
287    // fall back to the standard sea-level density rather than 0, so a zero ratio cannot collapse
288    // density_scale to 0 and silently disable drag entirely. (atmo_params.0 is base_alt here, not
289    // a density, so it is not a usable fallback.)
290    let base_density = if params.atmo_params.3 > 0.0 {
291        params.atmo_params.3 * 1.225
292    } else {
293        1.225 // standard sea-level air density (kg/m^3)
294    };
295
296    // Hoist invariants out of compute_derivatives (called 4x per step). Both the drag-model name
297    // and the projectile shape depend only on inputs, not on state/mach, so computing them per
298    // call wasted an allocation + heuristic every k1..k4. Mirrors cli_api.rs exactly.
299
300    // Drag-model name as a borrowed &'static str. DragModel's Display goes via Debug, which
301    // heap-allocates a String on every call; this match is bit-identical (Display == Debug ==
302    // variant name) with no per-step allocation.
303    let drag_model_str: &str = match drag_model {
304        DragModel::G1 => "G1",
305        DragModel::G2 => "G2",
306        DragModel::G5 => "G5",
307        DragModel::G6 => "G6",
308        DragModel::G7 => "G7",
309        DragModel::G8 => "G8",
310        DragModel::GI => "GI",
311        DragModel::GS => "GS",
312    };
313
314    // SI fallbacks for caliber/weight (SI-only MC callers may leave the imperial fields 0).
315    let caliber_in = if inputs.caliber_inches > 0.0 {
316        inputs.caliber_inches
317    } else {
318        inputs.bullet_diameter / 0.0254
319    };
320    let weight_gr = if inputs.weight_grains > 0.0 {
321        inputs.weight_grains
322    } else {
323        inputs.bullet_mass / 0.00006479891
324    };
325
326    // Projectile shape for transonic corrections (MBA-949: shared resolver — bullet_model name
327    // first, then the caliber/weight/drag-model heuristic).
328    let projectile_shape = crate::transonic_drag::resolve_projectile_shape(
329        inputs.bullet_model.as_deref(),
330        caliber_in,
331        weight_gr,
332        drag_model_str,
333    );
334
335    // Coriolis omega (Earth rotation), hoisted (invariant over the flight). MBA-957:
336    // fast_integrate — the Monte Carlo / Python-binding path — previously applied NO Coriolis.
337    // Mirror the validated cli_api solver exactly: McCoy frame X=downrange, Y=up, Z=lateral;
338    // azimuth 0 = North; omega.Z is NEGATIVE (Omega.East = -Omega cos(lat) sin(az)); applied as
339    // the physical -2 Omega x v inside compute_derivatives.
340    let omega_vector = if inputs.enable_coriolis && inputs.latitude.is_some() {
341        let omega_earth = 7.2921159e-5_f64; // rad/s
342        let lat = inputs.latitude.unwrap().to_radians();
343        let az = inputs.shot_azimuth; // compass bearing (0=N), NOT the aiming offset
344        Some(Vector3::new(
345            omega_earth * lat.cos() * az.cos(),  // X: downrange
346            omega_earth * lat.sin(),             // Y: vertical
347            -omega_earth * lat.cos() * az.sin(), // Z: lateral (corrected sign)
348        ))
349    } else {
350        None
351    };
352
353    // Integration loop
354    let mut hit_target = false;
355    let mut hit_ground = false;
356    let mut max_ord_time = None;
357    let mut max_ord_y = 0.0;
358    let ground_threshold = inputs.ground_threshold;
359
360    // RK4 integration
361    for i in 0..n_steps - 1 {
362        let t = i as f64 * dt;
363        let state = states[i];
364
365        let pos = Vector3::new(state[0], state[1], state[2]);
366        let _vel = Vector3::new(state[3], state[4], state[5]);
367
368        // Check termination conditions (X is downrange, McCoy)
369        if pos.x >= params.horiz {
370            hit_target = true;
371            break;
372        }
373
374        if pos.y <= ground_threshold {
375            hit_ground = true;
376            break;
377        }
378
379        // Track maximum ordinate
380        if pos.y > max_ord_y {
381            max_ord_y = pos.y;
382            max_ord_time = Some(t);
383        }
384
385        // RK4 step
386        let k1 = compute_derivatives(
387            &state,
388            inputs,
389            wind_sock,
390            base_density,
391            drag_model,
392            projectile_shape,
393            bc,
394            has_bc_segments,
395            has_bc_segments_data,
396            omega_vector,
397        );
398
399        let mut state2 = state;
400        for j in 0..6 {
401            state2[j] = state[j] + 0.5 * dt * k1[j];
402        }
403        let k2 = compute_derivatives(
404            &state2,
405            inputs,
406            wind_sock,
407            base_density,
408            drag_model,
409            projectile_shape,
410            bc,
411            has_bc_segments,
412            has_bc_segments_data,
413            omega_vector,
414        );
415
416        let mut state3 = state;
417        for j in 0..6 {
418            state3[j] = state[j] + 0.5 * dt * k2[j];
419        }
420        let k3 = compute_derivatives(
421            &state3,
422            inputs,
423            wind_sock,
424            base_density,
425            drag_model,
426            projectile_shape,
427            bc,
428            has_bc_segments,
429            has_bc_segments_data,
430            omega_vector,
431        );
432
433        let mut state4 = state;
434        for j in 0..6 {
435            state4[j] = state[j] + dt * k3[j];
436        }
437        let k4 = compute_derivatives(
438            &state4,
439            inputs,
440            wind_sock,
441            base_density,
442            drag_model,
443            projectile_shape,
444            bc,
445            has_bc_segments,
446            has_bc_segments_data,
447            omega_vector,
448        );
449
450        // Update state
451        let mut new_state = state;
452        for j in 0..6 {
453            new_state[j] = state[j] + dt * (k1[j] + 2.0 * k2[j] + 2.0 * k3[j] + k4[j]) / 6.0;
454        }
455
456        times.push(t + dt);
457        states.push(new_state);
458    }
459
460    // Create event arrays
461    let t_events = [
462        if hit_target {
463            vec![*times.last().unwrap()]
464        } else {
465            vec![]
466        },
467        if let Some(t) = max_ord_time {
468            vec![t]
469        } else {
470            vec![]
471        },
472        if hit_ground {
473            vec![*times.last().unwrap()]
474        } else {
475            vec![]
476        },
477    ];
478
479    FastSolution::from_trajectory_data(times, states, t_events)
480}
481
482/// Compute derivatives for the state vector
483fn compute_derivatives(
484    state: &[f64; 6],
485    inputs: &BallisticInputs,
486    wind_sock: &WindSock,
487    base_density: f64,
488    drag_model: &DragModel,
489    projectile_shape: crate::transonic_drag::ProjectileShape,
490    bc: f64,
491    has_bc_segments: bool,
492    has_bc_segments_data: bool,
493    omega: Option<Vector3<f64>>,
494) -> [f64; 6] {
495    let pos = Vector3::new(state[0], state[1], state[2]);
496    let vel = Vector3::new(state[3], state[4], state[5]);
497
498    // Get wind vector (based on downrange distance, which is X coordinate, McCoy)
499    let wind_vector = wind_sock.vector_for_range_stateless(pos.x);
500
501    // Velocity relative to air
502    let vel_adjusted = vel - wind_vector;
503    let v_mag = vel_adjusted.norm();
504
505    // Gravity acceleration vector, rotated into the shot-aligned frame by shooting_angle
506    // (uphill/downhill inclined fire), matching cli_api::TrajectorySolver::gravity_acceleration.
507    let theta = inputs.shooting_angle;
508    let accel_gravity = Vector3::new(
509        -G_ACCEL_MPS2 * theta.sin(),
510        -G_ACCEL_MPS2 * theta.cos(),
511        0.0,
512    );
513
514    // Calculate acceleration
515    let mut accel = if v_mag < 1e-6 {
516        accel_gravity
517    } else {
518        // Calculate drag
519        let v_fps = v_mag * MPS_TO_FPS;
520
521        // Calculate speed of sound from altitude using standard lapse rate
522        // atmo_params: (base_alt, base_temp_c, base_press_hpa, base_ratio)
523        let altitude = inputs.altitude + pos.y;
524        let (_, speed_of_sound) = get_local_atmosphere(
525            altitude,
526            inputs.altitude, // base_alt approximation
527            inputs.temperature,
528            inputs.pressure,
529            if inputs.humidity > 0.0 {
530                inputs.humidity
531            } else {
532                1.0
533            },
534        );
535        let mach = v_mag / speed_of_sound;
536
537        // Get BC value (potentially from segments)
538        let bc_current = if has_bc_segments_data && inputs.bc_segments_data.is_some() {
539            get_bc_from_velocity_segments(v_fps, inputs.bc_segments_data.as_ref().unwrap())
540        } else if has_bc_segments && inputs.bc_segments.is_some() {
541            crate::derivatives::interpolated_bc(
542                mach,
543                inputs.bc_segments.as_ref().unwrap(),
544                Some(inputs),
545            )
546        } else {
547            bc
548        };
549        // Guard bc_value == 0 (allowed on FFI/WASM/public MC surfaces): the division below
550        // would be Inf -> NaN. Mirrors cli_api's effective_bc.max(1e-6); inert for valid BCs.
551        let bc_current = bc_current.max(1e-6);
552
553        // Apply the transonic drag-rise correction once (mirrors derivatives.rs / cli_api) so
554        // the Monte Carlo / fast path doesn't under-predict drag near Mach 1. The projectile
555        // shape is invariant for the whole integration, so it is hoisted into fast_integrate and
556        // passed in rather than recomputed per call. wave_drag=false: the G1/G7 tables already
557        // embed the rise.
558        // MBA-940: a user-supplied custom drag table is the final Cd, used as-is (no G-model
559        // lookup, transonic, or form-factor correction — the curve already encodes the true drag).
560        // Its Cd is the projectile's ACTUAL drag coefficient, so the retardation denominator
561        // must be the sectional density (lb/in²), not a BC: Cd_own / SD == Cd_ref / BC
562        // (see BallisticInputs::custom_drag_denominator).
563        let (drag_factor, retard_denom) = if let Some(ref table) = inputs.custom_drag_table {
564            (
565                table.interpolate(mach),
566                inputs.custom_drag_denominator(bc_current),
567            )
568        } else {
569            let base_cd = get_drag_coefficient(mach, drag_model);
570            let cd =
571                crate::transonic_drag::transonic_correction(mach, base_cd, projectile_shape, false);
572            // MBA-948: honor use_form_factor in the fast path too (was derivatives.rs-only). No-op
573            // when the flag is false (apply_form_factor_to_drag short-circuits), as it is on every
574            // current consumer surface.
575            let cd = crate::form_factor::apply_form_factor_to_drag(
576                cd,
577                inputs.bullet_model.as_deref(),
578                &inputs.bc_type,
579                inputs.use_form_factor,
580            );
581            (cd, bc_current)
582        };
583
584        // Calculate drag acceleration using proper ballistics formula
585        let cd_to_retard = crate::constants::CD_TO_RETARD;
586        let standard_factor = drag_factor * cd_to_retard;
587        let density_scale = base_density / 1.225;
588
589        // Drag acceleration in ft/s^2
590        let a_drag_ft_s2 = (v_fps * v_fps) * standard_factor * density_scale / retard_denom;
591
592        // Convert to m/s^2 and apply to velocity vector
593        let a_drag_m_s2 = a_drag_ft_s2 * 0.3048; // ft/s^2 to m/s^2
594        let accel_drag = -a_drag_m_s2 * (vel_adjusted / v_mag);
595
596        // Total acceleration
597        accel_drag + accel_gravity
598    };
599
600    // Coriolis (Earth rotation), MBA-957. omega already carries the corrected lateral sign; use
601    // the ground-frame velocity and the physical -2 Omega x v, exactly as the validated cli_api.
602    if let Some(omega) = omega {
603        accel += -2.0 * omega.cross(&vel);
604    }
605
606    // Return derivatives [vx, vy, vz, ax, ay, az]
607    [vel.x, vel.y, vel.z, accel.x, accel.y, accel.z]
608}
609
610/// Get BC from velocity-based segments
611fn get_bc_from_velocity_segments(velocity_fps: f64, segments: &[BCSegmentData]) -> f64 {
612    for segment in segments {
613        if velocity_fps >= segment.velocity_min && velocity_fps <= segment.velocity_max {
614            return segment.bc_value;
615        }
616    }
617
618    // If no matching segment, use the BC from the closest segment
619    if let Some(first) = segments.first() {
620        if velocity_fps < first.velocity_min {
621            return first.bc_value;
622        }
623    }
624
625    if let Some(last) = segments.last() {
626        if velocity_fps > last.velocity_max {
627            return last.bc_value;
628        }
629    }
630
631    // Fallback (shouldn't reach here if segments are properly defined)
632    0.5
633}
634
635/// Fast integration with explicit wind segments using RK45
636/// MBA-155: Upstreamed from ballistics_rust
637pub fn fast_integrate_with_segments(
638    inputs: &BallisticInputs,
639    wind_segments: Vec<crate::wind::WindSegment>,
640    params: FastIntegrationParams,
641) -> FastSolution {
642    // Use the RK45 implementation from trajectory_integration module
643    use crate::trajectory_integration::{integrate_trajectory, TrajectoryParams};
644
645    // Degenerate atmosphere -> non-physical air density would silently stub the run.
646    if !atmo_is_physical(params.atmo_params) {
647        return FastSolution::degenerate(&params.initial_state);
648    }
649
650    // Extract parameters
651    let mass_kg = inputs.bullet_mass; // SI (kg)
652    let bc = inputs.bc_value;
653    let drag_model = inputs.bc_type;
654
655    // Coriolis omega — gated on enable_coriolis (+ a latitude), INDEPENDENT of
656    // spin-drift/Magnus. A caller can now request Coriolis-only (enable_coriolis=true
657    // with enable_advanced_effects=false) instead of being forced to enable all three.
658    let omega_vector = if inputs.enable_coriolis && inputs.latitude.is_some() {
659        // Calculate omega based on latitude and shot azimuth
660        // The Earth's rotation vector must be projected into the shooter's
661        // local frame which depends on azimuth (shooting direction).
662        // azimuth_angle: 0 = North, pi/2 = East
663        let omega_earth = 7.2921159e-5; // rad/s
664        let lat_rad = inputs.latitude.unwrap_or(0.0).to_radians();
665        let azimuth = inputs.shot_azimuth; // compass bearing (0=N), NOT the aiming offset
666        Some(Vector3::new(
667            omega_earth * lat_rad.cos() * azimuth.cos(), // X: downrange component
668            omega_earth * lat_rad.sin(),                 // Y: vertical component
669            -omega_earth * lat_rad.cos() * azimuth.sin(), // Z: lateral (MBA-957: corrected sign)
670        ))
671    } else {
672        None
673    };
674
675    // Set up trajectory parameters
676    let traj_params = TrajectoryParams {
677        mass_kg,
678        bc,
679        drag_model,
680        wind_segments,
681        atmos_params: params.atmo_params,
682        omega_vector,
683        enable_spin_drift: inputs.enable_advanced_effects,
684        enable_magnus: inputs.enable_magnus,
685        enable_coriolis: inputs.enable_coriolis,
686        target_distance_m: params.horiz,
687        enable_wind_shear: inputs.enable_wind_shear,
688        wind_shear_model: inputs.wind_shear_model.clone(),
689        shooter_altitude_m: inputs.altitude,
690        is_twist_right: inputs.is_twist_right,
691        shooting_angle: inputs.shooting_angle,
692        // MBA-717: carry the real bullet geometry so spin-drift / Magnus use it.
693        bullet_diameter: inputs.bullet_diameter,
694        bullet_length: inputs.bullet_length,
695        twist_rate: inputs.twist_rate,
696        custom_drag_table: inputs.custom_drag_table.clone(),
697        bc_segments: inputs.bc_segments.clone(),
698        use_bc_segments: inputs.use_bc_segments,
699        // MBA-954: keep the historical -1000.0 here (behavior-preserving for this binding path);
700        // threading inputs.ground_threshold would change the default ground plane for existing
701        // callers. Direct TrajectoryParams constructors can now configure it.
702        ground_threshold: -1000.0,
703    };
704
705    // Use RK45 adaptive integration
706    let trajectory = integrate_trajectory(
707        params.initial_state,
708        params.t_span,
709        traj_params,
710        "RK45", // Use RK45 implementation
711        1e-6,   // tolerance
712        0.01,   // max_step
713    );
714
715    // Convert trajectory to FastSolution format
716    let n_points = trajectory.len();
717    let mut times = Vec::with_capacity(n_points);
718    let mut states = Vec::with_capacity(n_points);
719
720    let mut target_hit_time: Option<f64> = None;
721    let mut ground_hit_time: Option<f64> = None;
722    let mut max_ord_time = None;
723    let mut max_ord_y = 0.0;
724
725    for (t, state_vec) in trajectory {
726        // Convert Vector6 to array
727        let state = [
728            state_vec[0],
729            state_vec[1],
730            state_vec[2],
731            state_vec[3],
732            state_vec[4],
733            state_vec[5],
734        ];
735
736        // Check termination conditions
737        // McCoy: state[0]=downrange, state[1]=vertical, state[2]=lateral
738
739        // Record FIRST time target is hit
740        if target_hit_time.is_none() && state[0] >= params.horiz {
741            target_hit_time = Some(t);
742        }
743
744        // Record ground hit
745        if ground_hit_time.is_none() && state[1] <= inputs.ground_threshold {
746            ground_hit_time = Some(t);
747        }
748
749        // Track maximum ordinate
750        if state[1] > max_ord_y {
751            max_ord_y = state[1];
752            max_ord_time = Some(t);
753        }
754
755        times.push(t);
756        states.push(state);
757    }
758
759    // Create event arrays
760    let t_events = [
761        if let Some(t) = target_hit_time {
762            vec![t]
763        } else {
764            vec![]
765        },
766        if let Some(t) = max_ord_time {
767            vec![t]
768        } else {
769            vec![]
770        },
771        if let Some(t) = ground_hit_time {
772            vec![t]
773        } else {
774            vec![]
775        },
776    ];
777
778    FastSolution::from_trajectory_data(times, states, t_events)
779}
780
781#[cfg(test)]
782mod tests {
783    use super::*;
784
785    #[test]
786    fn test_fast_solution_interpolation() {
787        let times = vec![0.0, 1.0, 2.0];
788        let states = vec![
789            [0.0, 0.0, 0.0, 100.0, 50.0, 0.0],
790            [100.0, 45.0, 0.0, 99.0, 40.0, 0.0],
791            [198.0, 80.0, 0.0, 98.0, 30.0, 0.0],
792        ];
793
794        let solution = FastSolution::from_trajectory_data(times, states, [vec![], vec![], vec![]]);
795
796        // Test interpolation at t=1.5
797        let result = solution.sol(&[1.5]);
798
799        assert!((result[0][0] - 149.0).abs() < 1e-10); // x position
800        assert!((result[1][0] - 62.5).abs() < 1e-10); // y position
801        assert!((result[3][0] - 98.5).abs() < 1e-10); // vx velocity
802    }
803
804    #[test]
805    fn test_bc_from_velocity_segments() {
806        let segments = vec![
807            BCSegmentData {
808                velocity_min: 0.0,
809                velocity_max: 1000.0,
810                bc_value: 0.5,
811            },
812            BCSegmentData {
813                velocity_min: 1000.0,
814                velocity_max: 2000.0,
815                bc_value: 0.52,
816            },
817            BCSegmentData {
818                velocity_min: 2000.0,
819                velocity_max: 3000.0,
820                bc_value: 0.55,
821            },
822        ];
823
824        assert_eq!(get_bc_from_velocity_segments(500.0, &segments), 0.5);
825        assert_eq!(get_bc_from_velocity_segments(1500.0, &segments), 0.52);
826        assert_eq!(get_bc_from_velocity_segments(2500.0, &segments), 0.55);
827
828        // Test edge cases
829        assert_eq!(get_bc_from_velocity_segments(-100.0, &segments), 0.5); // Below min
830        assert_eq!(get_bc_from_velocity_segments(3500.0, &segments), 0.55); // Above max
831    }
832
833    #[test]
834    fn test_fast_solution_interpolation_edge_cases() {
835        let times = vec![0.0, 1.0, 2.0, 3.0];
836        let states = vec![
837            [0.0, 0.0, 0.0, 800.0, 50.0, 0.0],
838            [800.0, 40.0, 100.0, 750.0, 30.0, 0.0],
839            [1550.0, 60.0, 200.0, 700.0, 10.0, 0.0],
840            [2250.0, 50.0, 300.0, 650.0, -10.0, 0.0],
841        ];
842
843        let solution = FastSolution::from_trajectory_data(times, states, [vec![], vec![], vec![]]);
844
845        // Test interpolation before first point
846        let result_before = solution.sol(&[-0.5]);
847        assert!((result_before[0][0] - 0.0).abs() < 1e-10); // Should clamp to first
848
849        // Test interpolation after last point
850        let result_after = solution.sol(&[5.0]);
851        assert!((result_after[0][0] - 2250.0).abs() < 1e-10); // Should clamp to last
852
853        // Test interpolation at exact points
854        let result_exact = solution.sol(&[1.0]);
855        assert!((result_exact[0][0] - 800.0).abs() < 1e-10);
856
857        // Test multiple query points
858        let result_multi = solution.sol(&[0.5, 1.5, 2.5]);
859        assert_eq!(result_multi[0].len(), 3);
860    }
861
862    #[test]
863    fn test_fast_solution_from_trajectory_data() {
864        let times = vec![0.0, 0.5, 1.0];
865        let states = vec![
866            [0.0, 1.0, 2.0, 3.0, 4.0, 5.0],
867            [10.0, 11.0, 12.0, 13.0, 14.0, 15.0],
868            [20.0, 21.0, 22.0, 23.0, 24.0, 25.0],
869        ];
870        let t_events = [vec![1.0], vec![0.5], vec![]];
871
872        let solution = FastSolution::from_trajectory_data(times.clone(), states, t_events);
873
874        // Check that data is stored correctly
875        assert_eq!(solution.t, times);
876        assert_eq!(solution.y.len(), 6); // 6 state components
877        assert_eq!(solution.y[0].len(), 3); // 3 time points
878        assert!(solution.success);
879
880        // Verify column-major storage
881        assert_eq!(solution.y[0][0], 0.0); // x at t=0
882        assert_eq!(solution.y[1][0], 1.0); // y at t=0
883        assert_eq!(solution.y[0][2], 20.0); // x at t=1.0
884    }
885
886    #[test]
887    fn test_bc_segments_boundary_conditions() {
888        // Test with single segment
889        let single_segment = vec![BCSegmentData {
890            velocity_min: 1000.0,
891            velocity_max: 2000.0,
892            bc_value: 0.5,
893        }];
894
895        assert_eq!(get_bc_from_velocity_segments(500.0, &single_segment), 0.5); // Below
896        assert_eq!(get_bc_from_velocity_segments(1500.0, &single_segment), 0.5); // In range
897        assert_eq!(get_bc_from_velocity_segments(2500.0, &single_segment), 0.5); // Above
898
899        // Test with exact boundary values
900        // Note: When velocity matches boundary, first matching segment wins
901        let segments = vec![
902            BCSegmentData {
903                velocity_min: 0.0,
904                velocity_max: 999.0, // Exclusive upper bound to avoid overlap
905                bc_value: 0.45,
906            },
907            BCSegmentData {
908                velocity_min: 1000.0,
909                velocity_max: 2000.0,
910                bc_value: 0.50,
911            },
912        ];
913
914        assert_eq!(get_bc_from_velocity_segments(1000.0, &segments), 0.50); // At second segment start
915        assert_eq!(get_bc_from_velocity_segments(0.0, &segments), 0.45); // At min
916        assert_eq!(get_bc_from_velocity_segments(999.0, &segments), 0.45); // At first segment max
917    }
918
919    #[test]
920    fn test_bc_segments_empty_fallback() {
921        let empty_segments: Vec<BCSegmentData> = vec![];
922
923        // With empty segments, should return fallback value
924        let result = get_bc_from_velocity_segments(1500.0, &empty_segments);
925        assert_eq!(result, 0.5); // Fallback value
926    }
927
928    #[test]
929    fn test_fast_integration_params() {
930        // Verify FastIntegrationParams struct can be constructed
931        let params = FastIntegrationParams {
932            horiz: 1000.0,
933            vert: 0.0,
934            initial_state: [0.0, 0.0, 0.0, 800.0, 50.0, 0.0], // McCoy: vx=downrange
935            t_span: (0.0, 5.0),
936            atmo_params: (0.0, 59.0, 29.92, 0.0),
937        };
938
939        assert_eq!(params.horiz, 1000.0);
940        assert_eq!(params.t_span.0, 0.0);
941        assert_eq!(params.t_span.1, 5.0);
942        assert_eq!(params.initial_state[3], 800.0); // vx (downrange, McCoy)
943    }
944
945    #[test]
946    fn test_fast_solution_event_arrays() {
947        let times = vec![0.0, 1.0, 2.0];
948        let states = vec![
949            [0.0, 0.0, 0.0, 800.0, 50.0, 0.0],
950            [800.0, 40.0, 500.0, 750.0, 30.0, 0.0],
951            [1500.0, 20.0, 1000.0, 700.0, 10.0, 0.0],
952        ];
953
954        // Create solution with events
955        let t_events = [
956            vec![2.0], // target_hit at t=2
957            vec![0.5], // max_ord at t=0.5
958            vec![],    // no ground_hit
959        ];
960
961        let solution = FastSolution::from_trajectory_data(times, states, t_events);
962
963        assert_eq!(solution.t_events[0], vec![2.0]); // Target hit
964        assert_eq!(solution.t_events[1], vec![0.5]); // Max ordinate
965        assert!(solution.t_events[2].is_empty()); // No ground hit
966    }
967
968    #[test]
969    fn fast_path_coriolis_uses_shot_direction() {
970        // Regression: fast_integrate_with_segments (the path the Python binding uses)
971        // built its Coriolis omega from azimuth_angle (the aiming offset, always ~0)
972        // instead of shot_azimuth, so east and west shots came out identical. After the
973        // fix they must differ with the correct Eotvos sign (east lifted, higher).
974        use std::f64::consts::FRAC_PI_2;
975        // Returns (final_downrange, final_vertical) for a shot fired along `shot_az`.
976        fn final_xy(shot_az: f64) -> (f64, f64) {
977            let mut inputs = BallisticInputs::default();
978            inputs.muzzle_velocity = 800.0;
979            inputs.bc_value = 0.5;
980            inputs.bc_type = DragModel::G7;
981            inputs.enable_advanced_effects = true; // gates the omega vector
982            inputs.enable_coriolis = true;
983            inputs.latitude = Some(45.0);
984            inputs.shot_azimuth = shot_az;
985            let v = 800.0_f64;
986            let elev = 0.02_f64;
987            let params = FastIntegrationParams {
988                horiz: 1000.0,
989                vert: 0.0,
990                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
991                t_span: (0.0, 5.0),
992                atmo_params: (0.0, 59.0, 29.92, 0.0),
993            };
994            let sol = fast_integrate_with_segments(&inputs, vec![], params);
995            let n = sol.y[0].len();
996            (sol.y[0][n - 1], sol.y[1][n - 1])
997        }
998        let (ex, ey) = final_xy(FRAC_PI_2); // east
999        let (wx, wy) = final_xy(3.0 * FRAC_PI_2); // west
1000                                                  // Both shots cover essentially the same downrange (Coriolis barely affects x),
1001                                                  // so comparing the final vertical is apples-to-apples.
1002        assert!(
1003            (ex - wx).abs() < 0.5,
1004            "east/west downrange should be ~equal (ex={ex:.4}, wx={wx:.4})"
1005        );
1006        // Pre-fix the fast path was North-locked, making these byte-identical. The Eotvos
1007        // term now lifts the east shot above the west shot.
1008        assert!(
1009            ey > wy,
1010            "fast-path east ({ey:.6}) must be higher than west ({wy:.6}) (Eotvos)"
1011        );
1012        assert!(
1013            (ey - wy) > 1e-5,
1014            "fast-path E-W vertical separation ({:.8} m) should be non-zero (the pre-fix bug was exact equality)",
1015            ey - wy
1016        );
1017    }
1018
1019    #[test]
1020    fn fast_path_coriolis_independent_of_advanced_effects() {
1021        // Coriolis is now gated on enable_coriolis (+ latitude), NOT enable_advanced_effects.
1022        // So a caller can request Coriolis-only without being forced to enable spin/Magnus.
1023        use std::f64::consts::FRAC_PI_2;
1024        fn final_y(coriolis: bool, shot_az: f64) -> f64 {
1025            let mut inputs = BallisticInputs::default();
1026            inputs.muzzle_velocity = 800.0;
1027            inputs.bc_value = 0.5;
1028            inputs.bc_type = DragModel::G7;
1029            inputs.enable_coriolis = coriolis;
1030            inputs.enable_advanced_effects = false; // explicitly OFF — Coriolis must still work
1031            inputs.latitude = Some(45.0);
1032            inputs.shot_azimuth = shot_az;
1033            let v = 800.0_f64;
1034            let elev = 0.02_f64;
1035            let params = FastIntegrationParams {
1036                horiz: 1000.0,
1037                vert: 0.0,
1038                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
1039                t_span: (0.0, 5.0),
1040                atmo_params: (0.0, 59.0, 29.92, 0.0),
1041            };
1042            let sol = fast_integrate_with_segments(&inputs, vec![], params);
1043            let n = sol.y[0].len();
1044            sol.y[1][n - 1]
1045        }
1046        // enable_coriolis=true with advanced effects OFF: directional Coriolis still applies.
1047        let e = final_y(true, FRAC_PI_2);
1048        let w = final_y(true, 3.0 * FRAC_PI_2);
1049        assert!(
1050            e > w && (e - w) > 1e-5,
1051            "Coriolis-only (no advanced effects) must still be directional: E={e} W={w}"
1052        );
1053        // enable_coriolis=false: no Coriolis at all, east == west.
1054        let e2 = final_y(false, FRAC_PI_2);
1055        let w2 = final_y(false, 3.0 * FRAC_PI_2);
1056        assert!(
1057            (e2 - w2).abs() < 1e-9,
1058            "with enable_coriolis=false, east/west must be identical: E={e2} W={w2}"
1059        );
1060    }
1061
1062    #[test]
1063    fn fast_path_rejects_degenerate_atmosphere() {
1064        let mut inputs = BallisticInputs::default();
1065        inputs.muzzle_velocity = 800.0;
1066        inputs.bc_value = 0.5;
1067        inputs.bc_type = DragModel::G7;
1068        let v = 800.0_f64;
1069        let e = 0.02_f64;
1070        let mk = |atmo: (f64, f64, f64, f64)| FastIntegrationParams {
1071            horiz: 500.0,
1072            vert: 0.0,
1073            initial_state: [0.0, 0.0, 0.0, v * e.cos(), v * e.sin(), 0.0],
1074            t_span: (0.0, 5.0),
1075            atmo_params: atmo,
1076        };
1077        // pressure <= 0 -> fail loudly (success=false) instead of a 1-point stub as success.
1078        let zero_p = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, 0.0, 50.0)));
1079        assert!(
1080            !zero_p.success,
1081            "pressure=0 atmosphere must yield success=false"
1082        );
1083        // non-finite pressure -> also rejected.
1084        let nan_p = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, f64::NAN, 50.0)));
1085        assert!(!nan_p.success, "NaN pressure must yield success=false");
1086        // realistic atmosphere -> success.
1087        let good = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, 1013.25, 50.0)));
1088        assert!(good.success, "realistic atmosphere must yield success=true");
1089        // Direct-atmosphere mode (density, speed_of_sound, 0, 0) is legitimate and must NOT
1090        // be rejected by the guard (regression: 0.21.2 rejected it via the pressure<=0 check).
1091        let direct = fast_integrate_with_segments(&inputs, vec![], mk((1.225, 340.0, 0.0, 0.0)));
1092        assert!(
1093            direct.success,
1094            "direct-atmosphere mode (pressure=0 sentinel) must yield success=true"
1095        );
1096    }
1097
1098    #[test]
1099    fn fast_path_carries_real_bullet_geometry() {
1100        // MBA-717: build_inputs hardcoded diameter=.308 / length=1.24in / twist=10 because
1101        // TrajectoryParams didn't carry them. They're now plumbed through, so the BallisticInputs
1102        // the derivatives see reflect the real bullet (caliber gates the Magnus block at
1103        // bullet_diameter > 0.0). Guard against regressing back to the hardcoded values: a
1104        // zero-diameter input must reach the derivatives as zero (Magnus skipped), whereas the
1105        // old code would have forced .308 regardless. We assert the run still completes and the
1106        // two geometries don't crash — the data path is exercised end-to-end.
1107        let run = |diameter: f64, twist: f64| {
1108            let mut inputs = BallisticInputs::default();
1109            inputs.muzzle_velocity = 800.0;
1110            inputs.bc_value = 0.5;
1111            inputs.bc_type = DragModel::G7;
1112            inputs.bullet_diameter = diameter;
1113            inputs.bullet_length = 0.0318;
1114            inputs.twist_rate = twist;
1115            inputs.enable_advanced_effects = true;
1116            inputs.enable_magnus = true;
1117            let v = 800.0_f64;
1118            let elev = 0.02_f64;
1119            let params = FastIntegrationParams {
1120                horiz: 1000.0,
1121                vert: 0.0,
1122                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
1123                t_span: (0.0, 5.0),
1124                atmo_params: (0.0, 15.0, 1013.25, 50.0),
1125            };
1126            fast_integrate_with_segments(&inputs, vec![], params)
1127        };
1128        // Both a real .224 and a real .338 produce a valid trajectory (geometry is honored, not
1129        // overridden by a hardcoded .308). Regression sentinel for the plumbing.
1130        assert!(run(0.00569, 7.0).success, ".224 geometry must solve");
1131        assert!(run(0.00858, 10.0).success, ".338 geometry must solve");
1132    }
1133}