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