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        let drag_factor = if let Some(ref table) = inputs.custom_drag_table {
561            table.interpolate(mach)
562        } else {
563            let base_cd = get_drag_coefficient(mach, drag_model);
564            let cd =
565                crate::transonic_drag::transonic_correction(mach, base_cd, projectile_shape, false);
566            // MBA-948: honor use_form_factor in the fast path too (was derivatives.rs-only). No-op
567            // when the flag is false (apply_form_factor_to_drag short-circuits), as it is on every
568            // current consumer surface.
569            crate::form_factor::apply_form_factor_to_drag(
570                cd,
571                inputs.bullet_model.as_deref(),
572                &inputs.bc_type,
573                inputs.use_form_factor,
574            )
575        };
576
577        // Calculate drag acceleration using proper ballistics formula
578        let cd_to_retard = crate::constants::CD_TO_RETARD;
579        let standard_factor = drag_factor * cd_to_retard;
580        let density_scale = base_density / 1.225;
581
582        // Drag acceleration in ft/s^2
583        let a_drag_ft_s2 = (v_fps * v_fps) * standard_factor * density_scale / bc_current;
584
585        // Convert to m/s^2 and apply to velocity vector
586        let a_drag_m_s2 = a_drag_ft_s2 * 0.3048; // ft/s^2 to m/s^2
587        let accel_drag = -a_drag_m_s2 * (vel_adjusted / v_mag);
588
589        // Total acceleration
590        accel_drag + accel_gravity
591    };
592
593    // Coriolis (Earth rotation), MBA-957. omega already carries the corrected lateral sign; use
594    // the ground-frame velocity and the physical -2 Omega x v, exactly as the validated cli_api.
595    if let Some(omega) = omega {
596        accel += -2.0 * omega.cross(&vel);
597    }
598
599    // Return derivatives [vx, vy, vz, ax, ay, az]
600    [vel.x, vel.y, vel.z, accel.x, accel.y, accel.z]
601}
602
603/// Get BC from velocity-based segments
604fn get_bc_from_velocity_segments(velocity_fps: f64, segments: &[BCSegmentData]) -> f64 {
605    for segment in segments {
606        if velocity_fps >= segment.velocity_min && velocity_fps <= segment.velocity_max {
607            return segment.bc_value;
608        }
609    }
610
611    // If no matching segment, use the BC from the closest segment
612    if let Some(first) = segments.first() {
613        if velocity_fps < first.velocity_min {
614            return first.bc_value;
615        }
616    }
617
618    if let Some(last) = segments.last() {
619        if velocity_fps > last.velocity_max {
620            return last.bc_value;
621        }
622    }
623
624    // Fallback (shouldn't reach here if segments are properly defined)
625    0.5
626}
627
628/// Fast integration with explicit wind segments using RK45
629/// MBA-155: Upstreamed from ballistics_rust
630pub fn fast_integrate_with_segments(
631    inputs: &BallisticInputs,
632    wind_segments: Vec<crate::wind::WindSegment>,
633    params: FastIntegrationParams,
634) -> FastSolution {
635    // Use the RK45 implementation from trajectory_integration module
636    use crate::trajectory_integration::{integrate_trajectory, TrajectoryParams};
637
638    // Degenerate atmosphere -> non-physical air density would silently stub the run.
639    if !atmo_is_physical(params.atmo_params) {
640        return FastSolution::degenerate(&params.initial_state);
641    }
642
643    // Extract parameters
644    let mass_kg = inputs.bullet_mass; // SI (kg)
645    let bc = inputs.bc_value;
646    let drag_model = inputs.bc_type;
647
648    // Coriolis omega — gated on enable_coriolis (+ a latitude), INDEPENDENT of
649    // spin-drift/Magnus. A caller can now request Coriolis-only (enable_coriolis=true
650    // with enable_advanced_effects=false) instead of being forced to enable all three.
651    let omega_vector = if inputs.enable_coriolis && inputs.latitude.is_some() {
652        // Calculate omega based on latitude and shot azimuth
653        // The Earth's rotation vector must be projected into the shooter's
654        // local frame which depends on azimuth (shooting direction).
655        // azimuth_angle: 0 = North, pi/2 = East
656        let omega_earth = 7.2921159e-5; // rad/s
657        let lat_rad = inputs.latitude.unwrap_or(0.0).to_radians();
658        let azimuth = inputs.shot_azimuth; // compass bearing (0=N), NOT the aiming offset
659        Some(Vector3::new(
660            omega_earth * lat_rad.cos() * azimuth.cos(), // X: downrange component
661            omega_earth * lat_rad.sin(),                 // Y: vertical component
662            -omega_earth * lat_rad.cos() * azimuth.sin(), // Z: lateral (MBA-957: corrected sign)
663        ))
664    } else {
665        None
666    };
667
668    // Set up trajectory parameters
669    let traj_params = TrajectoryParams {
670        mass_kg,
671        bc,
672        drag_model,
673        wind_segments,
674        atmos_params: params.atmo_params,
675        omega_vector,
676        enable_spin_drift: inputs.enable_advanced_effects,
677        enable_magnus: inputs.enable_magnus,
678        enable_coriolis: inputs.enable_coriolis,
679        target_distance_m: params.horiz,
680        enable_wind_shear: inputs.enable_wind_shear,
681        wind_shear_model: inputs.wind_shear_model.clone(),
682        shooter_altitude_m: inputs.altitude,
683        is_twist_right: inputs.is_twist_right,
684        shooting_angle: inputs.shooting_angle,
685        // MBA-717: carry the real bullet geometry so spin-drift / Magnus use it.
686        bullet_diameter: inputs.bullet_diameter,
687        bullet_length: inputs.bullet_length,
688        twist_rate: inputs.twist_rate,
689        custom_drag_table: inputs.custom_drag_table.clone(),
690        bc_segments: inputs.bc_segments.clone(),
691        use_bc_segments: inputs.use_bc_segments,
692        // MBA-954: keep the historical -1000.0 here (behavior-preserving for this binding path);
693        // threading inputs.ground_threshold would change the default ground plane for existing
694        // callers. Direct TrajectoryParams constructors can now configure it.
695        ground_threshold: -1000.0,
696    };
697
698    // Use RK45 adaptive integration
699    let trajectory = integrate_trajectory(
700        params.initial_state,
701        params.t_span,
702        traj_params,
703        "RK45", // Use RK45 implementation
704        1e-6,   // tolerance
705        0.01,   // max_step
706    );
707
708    // Convert trajectory to FastSolution format
709    let n_points = trajectory.len();
710    let mut times = Vec::with_capacity(n_points);
711    let mut states = Vec::with_capacity(n_points);
712
713    let mut target_hit_time: Option<f64> = None;
714    let mut ground_hit_time: Option<f64> = None;
715    let mut max_ord_time = None;
716    let mut max_ord_y = 0.0;
717
718    for (t, state_vec) in trajectory {
719        // Convert Vector6 to array
720        let state = [
721            state_vec[0],
722            state_vec[1],
723            state_vec[2],
724            state_vec[3],
725            state_vec[4],
726            state_vec[5],
727        ];
728
729        // Check termination conditions
730        // McCoy: state[0]=downrange, state[1]=vertical, state[2]=lateral
731
732        // Record FIRST time target is hit
733        if target_hit_time.is_none() && state[0] >= params.horiz {
734            target_hit_time = Some(t);
735        }
736
737        // Record ground hit
738        if ground_hit_time.is_none() && state[1] <= inputs.ground_threshold {
739            ground_hit_time = Some(t);
740        }
741
742        // Track maximum ordinate
743        if state[1] > max_ord_y {
744            max_ord_y = state[1];
745            max_ord_time = Some(t);
746        }
747
748        times.push(t);
749        states.push(state);
750    }
751
752    // Create event arrays
753    let t_events = [
754        if let Some(t) = target_hit_time {
755            vec![t]
756        } else {
757            vec![]
758        },
759        if let Some(t) = max_ord_time {
760            vec![t]
761        } else {
762            vec![]
763        },
764        if let Some(t) = ground_hit_time {
765            vec![t]
766        } else {
767            vec![]
768        },
769    ];
770
771    FastSolution::from_trajectory_data(times, states, t_events)
772}
773
774#[cfg(test)]
775mod tests {
776    use super::*;
777
778    #[test]
779    fn test_fast_solution_interpolation() {
780        let times = vec![0.0, 1.0, 2.0];
781        let states = vec![
782            [0.0, 0.0, 0.0, 100.0, 50.0, 0.0],
783            [100.0, 45.0, 0.0, 99.0, 40.0, 0.0],
784            [198.0, 80.0, 0.0, 98.0, 30.0, 0.0],
785        ];
786
787        let solution = FastSolution::from_trajectory_data(times, states, [vec![], vec![], vec![]]);
788
789        // Test interpolation at t=1.5
790        let result = solution.sol(&[1.5]);
791
792        assert!((result[0][0] - 149.0).abs() < 1e-10); // x position
793        assert!((result[1][0] - 62.5).abs() < 1e-10); // y position
794        assert!((result[3][0] - 98.5).abs() < 1e-10); // vx velocity
795    }
796
797    #[test]
798    fn test_bc_from_velocity_segments() {
799        let segments = vec![
800            BCSegmentData {
801                velocity_min: 0.0,
802                velocity_max: 1000.0,
803                bc_value: 0.5,
804            },
805            BCSegmentData {
806                velocity_min: 1000.0,
807                velocity_max: 2000.0,
808                bc_value: 0.52,
809            },
810            BCSegmentData {
811                velocity_min: 2000.0,
812                velocity_max: 3000.0,
813                bc_value: 0.55,
814            },
815        ];
816
817        assert_eq!(get_bc_from_velocity_segments(500.0, &segments), 0.5);
818        assert_eq!(get_bc_from_velocity_segments(1500.0, &segments), 0.52);
819        assert_eq!(get_bc_from_velocity_segments(2500.0, &segments), 0.55);
820
821        // Test edge cases
822        assert_eq!(get_bc_from_velocity_segments(-100.0, &segments), 0.5); // Below min
823        assert_eq!(get_bc_from_velocity_segments(3500.0, &segments), 0.55); // Above max
824    }
825
826    #[test]
827    fn test_fast_solution_interpolation_edge_cases() {
828        let times = vec![0.0, 1.0, 2.0, 3.0];
829        let states = vec![
830            [0.0, 0.0, 0.0, 800.0, 50.0, 0.0],
831            [800.0, 40.0, 100.0, 750.0, 30.0, 0.0],
832            [1550.0, 60.0, 200.0, 700.0, 10.0, 0.0],
833            [2250.0, 50.0, 300.0, 650.0, -10.0, 0.0],
834        ];
835
836        let solution = FastSolution::from_trajectory_data(times, states, [vec![], vec![], vec![]]);
837
838        // Test interpolation before first point
839        let result_before = solution.sol(&[-0.5]);
840        assert!((result_before[0][0] - 0.0).abs() < 1e-10); // Should clamp to first
841
842        // Test interpolation after last point
843        let result_after = solution.sol(&[5.0]);
844        assert!((result_after[0][0] - 2250.0).abs() < 1e-10); // Should clamp to last
845
846        // Test interpolation at exact points
847        let result_exact = solution.sol(&[1.0]);
848        assert!((result_exact[0][0] - 800.0).abs() < 1e-10);
849
850        // Test multiple query points
851        let result_multi = solution.sol(&[0.5, 1.5, 2.5]);
852        assert_eq!(result_multi[0].len(), 3);
853    }
854
855    #[test]
856    fn test_fast_solution_from_trajectory_data() {
857        let times = vec![0.0, 0.5, 1.0];
858        let states = vec![
859            [0.0, 1.0, 2.0, 3.0, 4.0, 5.0],
860            [10.0, 11.0, 12.0, 13.0, 14.0, 15.0],
861            [20.0, 21.0, 22.0, 23.0, 24.0, 25.0],
862        ];
863        let t_events = [vec![1.0], vec![0.5], vec![]];
864
865        let solution = FastSolution::from_trajectory_data(times.clone(), states, t_events);
866
867        // Check that data is stored correctly
868        assert_eq!(solution.t, times);
869        assert_eq!(solution.y.len(), 6); // 6 state components
870        assert_eq!(solution.y[0].len(), 3); // 3 time points
871        assert!(solution.success);
872
873        // Verify column-major storage
874        assert_eq!(solution.y[0][0], 0.0); // x at t=0
875        assert_eq!(solution.y[1][0], 1.0); // y at t=0
876        assert_eq!(solution.y[0][2], 20.0); // x at t=1.0
877    }
878
879    #[test]
880    fn test_bc_segments_boundary_conditions() {
881        // Test with single segment
882        let single_segment = vec![BCSegmentData {
883            velocity_min: 1000.0,
884            velocity_max: 2000.0,
885            bc_value: 0.5,
886        }];
887
888        assert_eq!(get_bc_from_velocity_segments(500.0, &single_segment), 0.5); // Below
889        assert_eq!(get_bc_from_velocity_segments(1500.0, &single_segment), 0.5); // In range
890        assert_eq!(get_bc_from_velocity_segments(2500.0, &single_segment), 0.5); // Above
891
892        // Test with exact boundary values
893        // Note: When velocity matches boundary, first matching segment wins
894        let segments = vec![
895            BCSegmentData {
896                velocity_min: 0.0,
897                velocity_max: 999.0, // Exclusive upper bound to avoid overlap
898                bc_value: 0.45,
899            },
900            BCSegmentData {
901                velocity_min: 1000.0,
902                velocity_max: 2000.0,
903                bc_value: 0.50,
904            },
905        ];
906
907        assert_eq!(get_bc_from_velocity_segments(1000.0, &segments), 0.50); // At second segment start
908        assert_eq!(get_bc_from_velocity_segments(0.0, &segments), 0.45); // At min
909        assert_eq!(get_bc_from_velocity_segments(999.0, &segments), 0.45); // At first segment max
910    }
911
912    #[test]
913    fn test_bc_segments_empty_fallback() {
914        let empty_segments: Vec<BCSegmentData> = vec![];
915
916        // With empty segments, should return fallback value
917        let result = get_bc_from_velocity_segments(1500.0, &empty_segments);
918        assert_eq!(result, 0.5); // Fallback value
919    }
920
921    #[test]
922    fn test_fast_integration_params() {
923        // Verify FastIntegrationParams struct can be constructed
924        let params = FastIntegrationParams {
925            horiz: 1000.0,
926            vert: 0.0,
927            initial_state: [0.0, 0.0, 0.0, 800.0, 50.0, 0.0], // McCoy: vx=downrange
928            t_span: (0.0, 5.0),
929            atmo_params: (0.0, 59.0, 29.92, 0.0),
930        };
931
932        assert_eq!(params.horiz, 1000.0);
933        assert_eq!(params.t_span.0, 0.0);
934        assert_eq!(params.t_span.1, 5.0);
935        assert_eq!(params.initial_state[3], 800.0); // vx (downrange, McCoy)
936    }
937
938    #[test]
939    fn test_fast_solution_event_arrays() {
940        let times = vec![0.0, 1.0, 2.0];
941        let states = vec![
942            [0.0, 0.0, 0.0, 800.0, 50.0, 0.0],
943            [800.0, 40.0, 500.0, 750.0, 30.0, 0.0],
944            [1500.0, 20.0, 1000.0, 700.0, 10.0, 0.0],
945        ];
946
947        // Create solution with events
948        let t_events = [
949            vec![2.0], // target_hit at t=2
950            vec![0.5], // max_ord at t=0.5
951            vec![],    // no ground_hit
952        ];
953
954        let solution = FastSolution::from_trajectory_data(times, states, t_events);
955
956        assert_eq!(solution.t_events[0], vec![2.0]); // Target hit
957        assert_eq!(solution.t_events[1], vec![0.5]); // Max ordinate
958        assert!(solution.t_events[2].is_empty()); // No ground hit
959    }
960
961    #[test]
962    fn fast_path_coriolis_uses_shot_direction() {
963        // Regression: fast_integrate_with_segments (the path the Python binding uses)
964        // built its Coriolis omega from azimuth_angle (the aiming offset, always ~0)
965        // instead of shot_azimuth, so east and west shots came out identical. After the
966        // fix they must differ with the correct Eotvos sign (east lifted, higher).
967        use std::f64::consts::FRAC_PI_2;
968        // Returns (final_downrange, final_vertical) for a shot fired along `shot_az`.
969        fn final_xy(shot_az: f64) -> (f64, f64) {
970            let mut inputs = BallisticInputs::default();
971            inputs.muzzle_velocity = 800.0;
972            inputs.bc_value = 0.5;
973            inputs.bc_type = DragModel::G7;
974            inputs.enable_advanced_effects = true; // gates the omega vector
975            inputs.enable_coriolis = true;
976            inputs.latitude = Some(45.0);
977            inputs.shot_azimuth = shot_az;
978            let v = 800.0_f64;
979            let elev = 0.02_f64;
980            let params = FastIntegrationParams {
981                horiz: 1000.0,
982                vert: 0.0,
983                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
984                t_span: (0.0, 5.0),
985                atmo_params: (0.0, 59.0, 29.92, 0.0),
986            };
987            let sol = fast_integrate_with_segments(&inputs, vec![], params);
988            let n = sol.y[0].len();
989            (sol.y[0][n - 1], sol.y[1][n - 1])
990        }
991        let (ex, ey) = final_xy(FRAC_PI_2); // east
992        let (wx, wy) = final_xy(3.0 * FRAC_PI_2); // west
993                                                  // Both shots cover essentially the same downrange (Coriolis barely affects x),
994                                                  // so comparing the final vertical is apples-to-apples.
995        assert!(
996            (ex - wx).abs() < 0.5,
997            "east/west downrange should be ~equal (ex={ex:.4}, wx={wx:.4})"
998        );
999        // Pre-fix the fast path was North-locked, making these byte-identical. The Eotvos
1000        // term now lifts the east shot above the west shot.
1001        assert!(
1002            ey > wy,
1003            "fast-path east ({ey:.6}) must be higher than west ({wy:.6}) (Eotvos)"
1004        );
1005        assert!(
1006            (ey - wy) > 1e-5,
1007            "fast-path E-W vertical separation ({:.8} m) should be non-zero (the pre-fix bug was exact equality)",
1008            ey - wy
1009        );
1010    }
1011
1012    #[test]
1013    fn fast_path_coriolis_independent_of_advanced_effects() {
1014        // Coriolis is now gated on enable_coriolis (+ latitude), NOT enable_advanced_effects.
1015        // So a caller can request Coriolis-only without being forced to enable spin/Magnus.
1016        use std::f64::consts::FRAC_PI_2;
1017        fn final_y(coriolis: bool, shot_az: f64) -> f64 {
1018            let mut inputs = BallisticInputs::default();
1019            inputs.muzzle_velocity = 800.0;
1020            inputs.bc_value = 0.5;
1021            inputs.bc_type = DragModel::G7;
1022            inputs.enable_coriolis = coriolis;
1023            inputs.enable_advanced_effects = false; // explicitly OFF — Coriolis must still work
1024            inputs.latitude = Some(45.0);
1025            inputs.shot_azimuth = shot_az;
1026            let v = 800.0_f64;
1027            let elev = 0.02_f64;
1028            let params = FastIntegrationParams {
1029                horiz: 1000.0,
1030                vert: 0.0,
1031                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
1032                t_span: (0.0, 5.0),
1033                atmo_params: (0.0, 59.0, 29.92, 0.0),
1034            };
1035            let sol = fast_integrate_with_segments(&inputs, vec![], params);
1036            let n = sol.y[0].len();
1037            sol.y[1][n - 1]
1038        }
1039        // enable_coriolis=true with advanced effects OFF: directional Coriolis still applies.
1040        let e = final_y(true, FRAC_PI_2);
1041        let w = final_y(true, 3.0 * FRAC_PI_2);
1042        assert!(
1043            e > w && (e - w) > 1e-5,
1044            "Coriolis-only (no advanced effects) must still be directional: E={e} W={w}"
1045        );
1046        // enable_coriolis=false: no Coriolis at all, east == west.
1047        let e2 = final_y(false, FRAC_PI_2);
1048        let w2 = final_y(false, 3.0 * FRAC_PI_2);
1049        assert!(
1050            (e2 - w2).abs() < 1e-9,
1051            "with enable_coriolis=false, east/west must be identical: E={e2} W={w2}"
1052        );
1053    }
1054
1055    #[test]
1056    fn fast_path_rejects_degenerate_atmosphere() {
1057        let mut inputs = BallisticInputs::default();
1058        inputs.muzzle_velocity = 800.0;
1059        inputs.bc_value = 0.5;
1060        inputs.bc_type = DragModel::G7;
1061        let v = 800.0_f64;
1062        let e = 0.02_f64;
1063        let mk = |atmo: (f64, f64, f64, f64)| FastIntegrationParams {
1064            horiz: 500.0,
1065            vert: 0.0,
1066            initial_state: [0.0, 0.0, 0.0, v * e.cos(), v * e.sin(), 0.0],
1067            t_span: (0.0, 5.0),
1068            atmo_params: atmo,
1069        };
1070        // pressure <= 0 -> fail loudly (success=false) instead of a 1-point stub as success.
1071        let zero_p = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, 0.0, 50.0)));
1072        assert!(
1073            !zero_p.success,
1074            "pressure=0 atmosphere must yield success=false"
1075        );
1076        // non-finite pressure -> also rejected.
1077        let nan_p = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, f64::NAN, 50.0)));
1078        assert!(!nan_p.success, "NaN pressure must yield success=false");
1079        // realistic atmosphere -> success.
1080        let good = fast_integrate_with_segments(&inputs, vec![], mk((0.0, 15.0, 1013.25, 50.0)));
1081        assert!(good.success, "realistic atmosphere must yield success=true");
1082        // Direct-atmosphere mode (density, speed_of_sound, 0, 0) is legitimate and must NOT
1083        // be rejected by the guard (regression: 0.21.2 rejected it via the pressure<=0 check).
1084        let direct = fast_integrate_with_segments(&inputs, vec![], mk((1.225, 340.0, 0.0, 0.0)));
1085        assert!(
1086            direct.success,
1087            "direct-atmosphere mode (pressure=0 sentinel) must yield success=true"
1088        );
1089    }
1090
1091    #[test]
1092    fn fast_path_carries_real_bullet_geometry() {
1093        // MBA-717: build_inputs hardcoded diameter=.308 / length=1.24in / twist=10 because
1094        // TrajectoryParams didn't carry them. They're now plumbed through, so the BallisticInputs
1095        // the derivatives see reflect the real bullet (caliber gates the Magnus block at
1096        // bullet_diameter > 0.0). Guard against regressing back to the hardcoded values: a
1097        // zero-diameter input must reach the derivatives as zero (Magnus skipped), whereas the
1098        // old code would have forced .308 regardless. We assert the run still completes and the
1099        // two geometries don't crash — the data path is exercised end-to-end.
1100        let run = |diameter: f64, twist: f64| {
1101            let mut inputs = BallisticInputs::default();
1102            inputs.muzzle_velocity = 800.0;
1103            inputs.bc_value = 0.5;
1104            inputs.bc_type = DragModel::G7;
1105            inputs.bullet_diameter = diameter;
1106            inputs.bullet_length = 0.0318;
1107            inputs.twist_rate = twist;
1108            inputs.enable_advanced_effects = true;
1109            inputs.enable_magnus = true;
1110            let v = 800.0_f64;
1111            let elev = 0.02_f64;
1112            let params = FastIntegrationParams {
1113                horiz: 1000.0,
1114                vert: 0.0,
1115                initial_state: [0.0, 0.0, 0.0, v * elev.cos(), v * elev.sin(), 0.0],
1116                t_span: (0.0, 5.0),
1117                atmo_params: (0.0, 15.0, 1013.25, 50.0),
1118            };
1119            fast_integrate_with_segments(&inputs, vec![], params)
1120        };
1121        // Both a real .224 and a real .338 produce a valid trajectory (geometry is honored, not
1122        // overridden by a hardcoded .308). Regression sentinel for the plumbing.
1123        assert!(run(0.00569, 7.0).success, ".224 geometry must solve");
1124        assert!(run(0.00858, 10.0).success, ".338 geometry must solve");
1125    }
1126}