Skip to main content

ballistics_engine/
trajectory_integration.rs

1//! Advanced trajectory integration methods (RK4, RK45)
2//!
3//! This module provides production-grade numerical integration for ballistic trajectories:
4//! - RK4: 4th-order Runge-Kutta (fixed step)
5//! - RK45: Dormand-Prince adaptive method (same as scipy.integrate.solve_ivp)
6//!
7//! MBA-155: Upstreamed from ballistics_rust for shared use
8
9use nalgebra::{Vector3, Vector6};
10use std::collections::HashMap;
11
12use crate::derivatives::compute_derivatives;
13use crate::wind::WindSegment;
14use crate::BallisticInputs;
15use crate::DragModel;
16
17fn wind_vector_for_range(range_m: f64, wind_segments: &[WindSegment]) -> Vector3<f64> {
18    if range_m.is_nan() {
19        return Vector3::zeros();
20    }
21    for seg in wind_segments {
22        if range_m < seg.2 {
23            let wind_speed_mps = seg.0 * 0.2777778; // km/h to m/s
24            let wind_angle_rad = seg.1.to_radians();
25            return Vector3::new(
26                -wind_speed_mps * wind_angle_rad.cos(),
27                0.0,
28                -wind_speed_mps * wind_angle_rad.sin(),
29            );
30        }
31    }
32    Vector3::zeros()
33}
34
35/// RK4 integration step
36fn rk4_step(
37    state: &Vector6<f64>,
38    t: f64,
39    dt: f64,
40    params: &TrajectoryParams,
41    inputs: &BallisticInputs,
42) -> Vector6<f64> {
43    // RK4 integration
44    let k1 = compute_derivatives_vec(state, t, params, inputs);
45    let k2 = compute_derivatives_vec(&(state + dt * 0.5 * k1), t + dt * 0.5, params, inputs);
46    let k3 = compute_derivatives_vec(&(state + dt * 0.5 * k2), t + dt * 0.5, params, inputs);
47    let k4 = compute_derivatives_vec(&(state + dt * k3), t + dt, params, inputs);
48
49    state + (dt / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4)
50}
51
52/// Adaptive RK45 integration step (Dormand-Prince method)
53fn rk45_step(
54    state: &Vector6<f64>,
55    t: f64,
56    dt: f64,
57    params: &TrajectoryParams,
58    inputs: &BallisticInputs,
59    tol: f64,
60) -> (Vector6<f64>, f64, f64) {
61    // Dormand-Prince coefficients (same as scipy.integrate.solve_ivp RK45)
62    const A21: f64 = 1.0 / 5.0;
63    const A31: f64 = 3.0 / 40.0;
64    const A32: f64 = 9.0 / 40.0;
65    const A41: f64 = 44.0 / 45.0;
66    const A42: f64 = -56.0 / 15.0;
67    const A43: f64 = 32.0 / 9.0;
68    const A51: f64 = 19372.0 / 6561.0;
69    const A52: f64 = -25360.0 / 2187.0;
70    const A53: f64 = 64448.0 / 6561.0;
71    const A54: f64 = -212.0 / 729.0;
72    const A61: f64 = 9017.0 / 3168.0;
73    const A62: f64 = -355.0 / 33.0;
74    const A63: f64 = 46732.0 / 5247.0;
75    const A64: f64 = 49.0 / 176.0;
76    const A65: f64 = -5103.0 / 18656.0;
77    const A71: f64 = 35.0 / 384.0;
78    const A73: f64 = 500.0 / 1113.0;
79    const A74: f64 = 125.0 / 192.0;
80    const A75: f64 = -2187.0 / 6784.0;
81    const A76: f64 = 11.0 / 84.0;
82
83    // 5th order coefficients
84    const B1: f64 = 35.0 / 384.0;
85    const B3: f64 = 500.0 / 1113.0;
86    const B4: f64 = 125.0 / 192.0;
87    const B5: f64 = -2187.0 / 6784.0;
88    const B6: f64 = 11.0 / 84.0;
89
90    // 4th order coefficients (for error estimation)
91    const B1_ERR: f64 = 5179.0 / 57600.0;
92    const B3_ERR: f64 = 7571.0 / 16695.0;
93    const B4_ERR: f64 = 393.0 / 640.0;
94    const B5_ERR: f64 = -92097.0 / 339200.0;
95    const B6_ERR: f64 = 187.0 / 2100.0;
96    const B7_ERR: f64 = 1.0 / 40.0;
97
98    // Compute stages
99    let k1 = compute_derivatives_vec(state, t, params, inputs);
100    let k2 = compute_derivatives_vec(&(state + dt * A21 * k1), t + dt * 0.2, params, inputs);
101    let k3 = compute_derivatives_vec(
102        &(state + dt * (A31 * k1 + A32 * k2)),
103        t + dt * 0.3,
104        params,
105        inputs,
106    );
107    let k4 = compute_derivatives_vec(
108        &(state + dt * (A41 * k1 + A42 * k2 + A43 * k3)),
109        t + dt * 0.8,
110        params,
111        inputs,
112    );
113    let k5 = compute_derivatives_vec(
114        &(state + dt * (A51 * k1 + A52 * k2 + A53 * k3 + A54 * k4)),
115        t + dt * 8.0 / 9.0,
116        params,
117        inputs,
118    );
119    let k6 = compute_derivatives_vec(
120        &(state + dt * (A61 * k1 + A62 * k2 + A63 * k3 + A64 * k4 + A65 * k5)),
121        t + dt,
122        params,
123        inputs,
124    );
125    let k7 = compute_derivatives_vec(
126        &(state + dt * (A71 * k1 + A73 * k3 + A74 * k4 + A75 * k5 + A76 * k6)),
127        t + dt,
128        params,
129        inputs,
130    );
131
132    // 5th order solution
133    let y_new = state + dt * (B1 * k1 + B3 * k3 + B4 * k4 + B5 * k5 + B6 * k6);
134
135    // 4th order solution for error estimate
136    let y_err = state
137        + dt * (B1_ERR * k1 + B3_ERR * k3 + B4_ERR * k4 + B5_ERR * k5 + B6_ERR * k6 + B7_ERR * k7);
138
139    // Error estimate
140    let error = (y_new - y_err).norm() / (1.0 + state.norm());
141
142    // Adaptive step size
143    let safety = 0.9;
144    let dt_new = if error < tol {
145        dt * safety * (tol / error).powf(0.2).min(2.0)
146    } else {
147        dt * safety * (tol / error).powf(0.25).max(0.1)
148    };
149
150    (y_new, dt_new, error)
151}
152
153/// Parameters for trajectory computation
154pub struct TrajectoryParams {
155    pub mass_kg: f64,
156    pub bc: f64,
157    pub drag_model: DragModel,
158    pub wind_segments: Vec<WindSegment>,
159    /// Dual-mode atmosphere tuple consumed by `compute_derivatives`:
160    /// **Standard** `(base_alt_m, base_temp_c, base_pressure_hPa, base_density_ratio)` — note
161    /// slot 3 is a density RATIO, NOT humidity, even though it rides in the `humidity` field;
162    /// or **Direct** `(air_density, speed_of_sound, 0.0, 0.0)` — slots 2 and 3 are zero
163    /// sentinels. A pressure of 0 that is not the direct-mode sentinel disables drag.
164    pub atmos_params: (f64, f64, f64, f64),
165    pub omega_vector: Option<Vector3<f64>>,
166    pub enable_spin_drift: bool,
167    pub enable_magnus: bool,
168    pub enable_coriolis: bool,
169    pub target_distance_m: f64, // Target horizontal distance in meters
170    pub enable_wind_shear: bool,
171    pub wind_shear_model: String,
172    pub shooter_altitude_m: f64,
173    pub is_twist_right: bool, // True for right-hand twist, false for left-hand
174    pub shooting_angle: f64,  // uphill/downhill angle in radians
175    // MBA-717: real bullet geometry so spin-drift / Magnus / stability on this fast/MC
176    // path use the actual bullet instead of hardcoded .308 / 1.24in / 10-twist placeholders.
177    pub bullet_diameter: f64,                              // meters
178    pub bullet_length: f64, // meters (0.0 -> derivatives falls back to the 4.5-caliber heuristic)
179    pub twist_rate: f64,    // inches per turn
180    pub custom_drag_table: Option<crate::drag::DragTable>, // Custom Drag Model (CDM) data
181    pub bc_segments: Option<Vec<(f64, f64)>>, // Mach-based BC segments: (mach, bc)
182    pub use_bc_segments: bool, // Whether to use BC segment interpolation
183    /// MBA-954: altitude (m, relative to launch) below which integration stops. -1000.0 is the
184    /// historical default — effectively "no early ground impact" for normal flat-fire shots.
185    pub ground_threshold: f64,
186}
187
188/// Build the loop-invariant BallisticInputs for the derivatives function ONCE per integration,
189/// instead of rebuilding it (a "none".to_string() alloc plus bc_segments / custom_drag_table
190/// clones) on every derivative evaluation (4x per RK4 step, 7x per RK45 step). muzzle_velocity is
191/// set to 0.0 because compute_derivatives never reads it on this path; every other field depends
192/// only on `params`, so the struct is constant for the whole integration.
193fn build_inputs(params: &TrajectoryParams) -> BallisticInputs {
194    let mut inputs = BallisticInputs {
195        bc_value: params.bc,
196        bc_type: params.drag_model,
197        bullet_mass: params.mass_kg,             // kg
198        muzzle_velocity: 0.0,                    // unread by compute_derivatives on this path
199        bullet_diameter: params.bullet_diameter, // MBA-717: real geometry, not placeholders
200        bullet_length: params.bullet_length,
201        twist_rate: params.twist_rate,
202        is_twist_right: params.is_twist_right,
203        enable_advanced_effects: params.enable_spin_drift
204            || params.enable_magnus
205            || params.enable_coriolis,
206        enable_magnus: params.enable_magnus,
207        enable_coriolis: params.enable_coriolis,
208        altitude: params.atmos_params.0,
209        temperature: params.atmos_params.1,
210        pressure: params.atmos_params.2,
211        humidity: params.atmos_params.3,
212        tipoff_yaw: 0.0,
213        target_distance: 1000.0, // default
214        muzzle_angle: 0.0,
215        wind_speed: if !params.wind_segments.is_empty() {
216            params.wind_segments[0].0 * 0.2777778 // km/h -> m/s
217        } else {
218            0.0
219        },
220        wind_angle: if !params.wind_segments.is_empty() {
221            params.wind_segments[0].1.to_radians() // degrees -> radians
222        } else {
223            0.0
224        },
225        latitude: None,
226        shooting_angle: params.shooting_angle,
227        azimuth_angle: 0.0,
228        shot_azimuth: 0.0, // this fast path doesn't plumb latitude/bearing (no directional Coriolis here)
229        use_powder_sensitivity: false,
230        powder_temp_sensitivity: 0.0,
231        powder_temp: 59.0,
232        powder_temp_curve: None,
233        tipoff_decay_distance: 0.0,
234        ground_threshold: params.ground_threshold, // MBA-954: honor the configured ground plane
235        bc_segments: params.bc_segments.clone(),
236        caliber_inches: params.bullet_diameter / 0.0254, // MBA-717: from real diameter
237        weight_grains: params.mass_kg / 0.00006479891,
238        use_bc_segments: params.use_bc_segments,
239        bullet_id: None,
240        bc_segments_data: None,
241        use_enhanced_spin_drift: params.enable_spin_drift,
242        use_form_factor: false,
243        manufacturer: None,
244        bullet_model: None,
245        enable_wind_shear: false,
246        wind_shear_model: "none".to_string(),
247        use_cluster_bc: false,
248        bullet_cluster: None,
249        custom_drag_table: params.custom_drag_table.clone(),
250        bc_type_str: None,
251        enable_pitch_damping: false,
252        enable_precession_nutation: false,
253        // MBA-959: aerodynamic jump is intentionally OFF on this path. integrate_trajectory
254        // is a low-level state integrator: it advances a raw initial_state (no muzzle angle to
255        // perturb) and an unread muzzle_velocity, so a meaningful Litz Sg can't be formed here
256        // (Sg would be ~0 and the AJ guard would suppress it regardless). AJ belongs on the
257        // BallisticInputs + TrajectorySolver path, which bindings use and already honors the flag.
258        // (Bullet geometry IS now carried through TrajectoryParams — MBA-717 — so spin-drift /
259        // Magnus on this path use the real diameter/length/twist.)
260        enable_aerodynamic_jump: false,
261        use_rk4: true,
262        use_adaptive_rk45: false,
263        enable_trajectory_sampling: false,
264        sample_interval: 10.0,
265        sight_height: 0.0,
266        muzzle_height: 0.0,
267        target_height: 0.0,
268    };
269
270    // MBA-955: pre-populate velocity-BC segments ONCE here, instead of get_bc_for_velocity
271    // rebuilding them (a model String + a segment Vec) on every derivative evaluation (4-7x per
272    // step). Gated to EXACTLY the case where the per-step path would estimate: use_bc_segments on,
273    // no explicit velocity segments, and no Mach-based bc_segments (those take a different,
274    // unchanged path). bc_used there == params.bc == inputs.bc_value, so the estimated segments are
275    // identical and the per-step fast-path lookup returns the same BC -> byte-identical output.
276    if inputs.use_bc_segments && inputs.bc_segments_data.is_none() && inputs.bc_segments.is_none() {
277        inputs.bc_segments_data =
278            crate::derivatives::estimate_bc_segments_for(&inputs, inputs.bc_value);
279    }
280    inputs
281}
282
283/// Convert state to Vector6 and call compute_derivatives
284fn compute_derivatives_vec(
285    state: &Vector6<f64>,
286    t: f64,
287    params: &TrajectoryParams,
288    inputs: &BallisticInputs,
289) -> Vector6<f64> {
290    let pos = Vector3::new(state[0], state[1], state[2]);
291    let vel = Vector3::new(state[3], state[4], state[5]);
292
293    // Calculate wind at current position with shear support
294    let wind_vector = if !params.wind_segments.is_empty() {
295        if params.enable_wind_shear && params.wind_shear_model != "none" {
296            crate::wind_shear::get_wind_at_position(
297                &pos,
298                &params.wind_segments,
299                params.enable_wind_shear,
300                &params.wind_shear_model,
301                params.shooter_altitude_m,
302            )
303        } else {
304            wind_vector_for_range(pos.x, &params.wind_segments)
305        }
306    } else {
307        Vector3::zeros()
308    };
309
310    // Call compute_derivatives - returns [f64; 6] directly. `inputs` is built once per
311    // integration by build_inputs() and threaded in, instead of rebuilt every call.
312    let deriv_result = compute_derivatives(
313        pos,
314        vel,
315        inputs,
316        wind_vector,
317        params.atmos_params,
318        params.bc,
319        params.omega_vector,
320        t,
321    );
322
323    Vector6::new(
324        deriv_result[0],
325        deriv_result[1],
326        deriv_result[2],
327        deriv_result[3],
328        deriv_result[4],
329        deriv_result[5],
330    )
331}
332
333/// Main trajectory integration function
334pub fn integrate_trajectory(
335    initial_state: [f64; 6],
336    t_span: (f64, f64),
337    params: TrajectoryParams,
338    method: &str,
339    tolerance: f64,
340    max_step: f64,
341) -> Vec<(f64, Vector6<f64>)> {
342    let mut state = Vector6::new(
343        initial_state[0],
344        initial_state[1],
345        initial_state[2],
346        initial_state[3],
347        initial_state[4],
348        initial_state[5],
349    );
350
351    let mut t = t_span.0;
352    let t_end = t_span.1;
353    let mut dt = (t_end - t) / 1000.0; // Initial step size
354
355    let mut trajectory = Vec::with_capacity(10000);
356    trajectory.push((t, state));
357
358    // Build the (loop-invariant) derivative inputs once for the whole integration, instead of
359    // rebuilding the struct on every derivative evaluation.
360    let inputs = build_inputs(&params);
361
362    match method {
363        "RK4" => {
364            // Fixed step RK4 with target detection
365            dt = dt.min(max_step).min(0.001); // Use smaller steps for accuracy
366
367            while t < t_end {
368                if t + dt > t_end {
369                    dt = t_end - t;
370                }
371
372                let new_state = rk4_step(&state, t, dt, &params, &inputs);
373
374                // Check if we're about to pass the target (X is downrange, McCoy)
375                if state[0] < params.target_distance_m && new_state[0] >= params.target_distance_m {
376                    // Interpolate to find exact target crossing
377                    let alpha = (params.target_distance_m - state[0]) / (new_state[0] - state[0]);
378                    let dt_to_target = dt * alpha;
379
380                    // Take a smaller step to reach target exactly
381                    let final_state = rk4_step(&state, t, dt_to_target, &params, &inputs);
382
383                    // Ensure we don't overshoot
384                    let mut corrected_state = final_state;
385                    if corrected_state[0] > params.target_distance_m {
386                        corrected_state[0] = params.target_distance_m;
387                    }
388
389                    trajectory.push((t + dt_to_target, corrected_state));
390                    break; // Stop at target
391                }
392
393                state = new_state;
394                t += dt;
395                trajectory.push((t, state));
396
397                // Check if we've reached or passed the target
398                if state[0] >= params.target_distance_m {
399                    // X is downrange (McCoy)
400                    // Add final point exactly at target
401                    let mut final_state = state;
402                    final_state[0] = params.target_distance_m; // X is downrange
403                    trajectory.push((t, final_state));
404                    break;
405                }
406
407                // Check if bullet hit ground (MBA-954: honor the configured ground plane,
408                // not a hardcoded -1000.0)
409                if state[1] < params.ground_threshold {
410                    break;
411                }
412            }
413        }
414        "RK45" | _ => {
415            // Adaptive RK45 with better sampling
416            let mut last_save_x = 0.0; // X is downrange (McCoy)
417            let save_interval_m = params.target_distance_m / 50.0; // Save ~50 points minimum
418
419            // OPTIMIZATION: Adjust max step size when wind shear is enabled
420            // This improves numerical stability at long ranges
421            let effective_max_step =
422                if params.enable_wind_shear && params.wind_shear_model != "none" {
423                    // Use smaller steps for wind shear, but not TOO small
424                    if params.target_distance_m > 800.0 {
425                        0.01 // Smaller steps for long range with shear (10ms)
426                    } else {
427                        0.02 // Normal steps for medium range with shear (20ms)
428                    }
429                } else {
430                    max_step // Use provided max_step when no wind shear
431                };
432
433            // Set initial step size - ensure it's reasonable
434            dt = dt.min(effective_max_step).max(0.0001); // At least 0.1ms to avoid infinite loops
435
436            // Safety check: maximum iterations to prevent infinite loops
437            let max_iterations = 100000; // Should be more than enough for any realistic trajectory
438            let mut iteration_count = 0;
439
440            while t < t_end && iteration_count < max_iterations {
441                iteration_count += 1;
442
443                // Limit time step for better resolution
444                if t + dt > t_end {
445                    dt = t_end - t;
446                }
447
448                let (new_state, dt_new, _error) =
449                    rk45_step(&state, t, dt, &params, &inputs, tolerance);
450
451                // Check if we're about to pass the target (X is downrange, McCoy)
452                if state[0] < params.target_distance_m && new_state[0] >= params.target_distance_m {
453                    // Interpolate to find exact target crossing
454                    let alpha = (params.target_distance_m - state[0]) / (new_state[0] - state[0]);
455                    let dt_to_target = dt * alpha;
456
457                    // Take a smaller step to reach target exactly
458                    let (final_state, _, _) =
459                        rk45_step(&state, t, dt_to_target, &params, &inputs, tolerance);
460
461                    // Make sure we don't overshoot
462                    let mut corrected_state = final_state;
463                    if corrected_state[0] > params.target_distance_m {
464                        corrected_state[0] = params.target_distance_m;
465                    }
466
467                    trajectory.push((t + dt_to_target, corrected_state));
468                    break; // Stop at target - no more points after this
469                }
470
471                // Update state
472                state = new_state;
473                t += dt;
474
475                // Save trajectory point if we've moved enough distance
476                if state[0] - last_save_x >= save_interval_m || state[0] >= params.target_distance_m
477                {
478                    // X is downrange
479                    trajectory.push((t, state));
480                    last_save_x = state[0];
481                }
482
483                // Limit dt for next step - ensure we get enough resolution
484                dt = dt_new.min(effective_max_step).max(0.0001); // Use effective max step, min 0.1ms
485
486                // Stop if we've reached the target
487                if state[0] >= params.target_distance_m {
488                    // X is downrange (McCoy)
489                    // Add final point at target distance
490                    let mut final_state = state;
491                    final_state[0] = params.target_distance_m; // X is downrange
492                    trajectory.push((t, final_state));
493                    break;
494                }
495
496                // Check if bullet hit ground (MBA-954: honor the configured ground plane,
497                // not a hardcoded -1000.0)
498                if state[1] < params.ground_threshold {
499                    break;
500                }
501            }
502
503            // Warn if we hit the iteration limit
504            if iteration_count >= max_iterations {
505                eprintln!(
506                    "WARNING: Trajectory integration hit maximum iteration limit ({} iterations)",
507                    max_iterations
508                );
509                eprintln!("  Final time: {}, Target time: {}", t, t_end);
510                eprintln!(
511                    "  Final position: downrange(x)={}, Target: {}m",
512                    state[0], params.target_distance_m
513                );
514            }
515        }
516    }
517
518    trajectory
519}
520
521/// Python-exposed function for complete trajectory integration
522pub fn solve_trajectory_rust(
523    initial_state: [f64; 6],
524    t_span: (f64, f64),
525    mass_kg: f64,
526    bc: f64,
527    drag_model: DragModel,
528    wind_segments: Vec<WindSegment>,
529    atmos_params: (f64, f64, f64, f64),
530    omega_vector: Option<Vec<f64>>,
531    enable_spin_drift: bool,
532    enable_magnus: bool,
533    enable_coriolis: bool,
534    method: String,
535    tolerance: f64,
536    max_step: f64,
537    target_distance_m: f64,
538) -> Vec<HashMap<String, f64>> {
539    let omega_vec = omega_vector.map(|v| Vector3::new(v[0], v[1], v[2]));
540
541    let params = TrajectoryParams {
542        mass_kg,
543        bc,
544        drag_model,
545        wind_segments,
546        atmos_params,
547        omega_vector: omega_vec,
548        enable_spin_drift,
549        enable_magnus,
550        enable_coriolis,
551        target_distance_m,
552        enable_wind_shear: false, // Default for test function
553        wind_shear_model: "none".to_string(),
554        shooter_altitude_m: 0.0,
555        is_twist_right: true, // Default for test function
556        shooting_angle: 0.0,  // This legacy entry takes no inclined-fire arg; flat fire only
557        // This legacy entry takes no geometry args; keep the historical placeholders so its
558        // behavior is unchanged (callers needing real geometry use fast_integrate_with_segments).
559        bullet_diameter: 0.0078232,
560        bullet_length: 0.031496,
561        twist_rate: 10.0,
562        custom_drag_table: None, // No CDM for test function
563        bc_segments: None,       // No BC segments for legacy function
564        use_bc_segments: false,
565        ground_threshold: -1000.0, // MBA-954: preserve the historical default
566    };
567
568    let trajectory =
569        integrate_trajectory(initial_state, t_span, params, &method, tolerance, max_step);
570
571    // Convert to Python-friendly format
572    trajectory
573        .into_iter()
574        .map(|(t, state)| {
575            let mut point = HashMap::new();
576            point.insert("t".to_string(), t);
577            point.insert("x".to_string(), state[0]);
578            point.insert("y".to_string(), state[1]);
579            point.insert("z".to_string(), state[2]);
580            point.insert("vx".to_string(), state[3]);
581            point.insert("vy".to_string(), state[4]);
582            point.insert("vz".to_string(), state[5]);
583            point
584        })
585        .collect()
586}
587
588#[cfg(test)]
589mod tests {
590    use super::*;
591
592    fn create_test_params(target_distance_m: f64) -> TrajectoryParams {
593        TrajectoryParams {
594            mass_kg: 0.01134, // 175 grains in kg
595            bc: 0.442,
596            bullet_diameter: 0.0078232, // .308 in
597            bullet_length: 0.031496,    // 1.24 in
598            twist_rate: 10.0,
599            drag_model: DragModel::G7,
600            wind_segments: vec![],
601            atmos_params: (0.0, 59.0, 29.92, 0.0),
602            omega_vector: None,
603            enable_spin_drift: false,
604            enable_magnus: false,
605            enable_coriolis: false,
606            target_distance_m,
607            enable_wind_shear: false,
608            wind_shear_model: "none".to_string(),
609            shooter_altitude_m: 0.0,
610            is_twist_right: true,
611            shooting_angle: 0.0,
612            custom_drag_table: None,
613            bc_segments: None,
614            use_bc_segments: false,
615            ground_threshold: -1000.0,
616        }
617    }
618
619    #[test]
620    fn test_mba954_ground_threshold_honored() {
621        // MBA-954: integrate_trajectory must honor the configured ground plane, not a hardcoded
622        // -1000.0. A descending bullet with a shallow ground_threshold must terminate earlier
623        // (fewer points) than one with the historical deep default.
624        let initial_state = [0.0, 0.0, 0.0, 300.0, -30.0, 0.0]; // descending (vy = -30 m/s)
625
626        let mut shallow = create_test_params(1_000_000.0); // huge target so range never terminates
627        shallow.ground_threshold = -20.0; // stop ~20 m below launch
628        let mut deep = create_test_params(1_000_000.0);
629        deep.ground_threshold = -1000.0; // historical default
630
631        let t_shallow =
632            integrate_trajectory(initial_state, (0.0, 60.0), shallow, "RK4", 1e-6, 0.001);
633        let t_deep = integrate_trajectory(initial_state, (0.0, 60.0), deep, "RK4", 1e-6, 0.001);
634
635        assert!(
636            t_shallow.len() < t_deep.len(),
637            "shallow ground_threshold (-20) should terminate earlier than deep (-1000): \
638             shallow={}, deep={}",
639            t_shallow.len(),
640            t_deep.len()
641        );
642    }
643
644    #[test]
645    fn test_integrate_trajectory_basic() {
646        // Initial state [x,y,z,vx,vy,vz] (McCoy: X=downrange, Z=lateral)
647        // x=0 (downrange start), vx=821.52 (downrange velocity)
648        let initial_state = [0.0, -0.038, 0.0, 821.52, 48.61, 0.0];
649
650        let params = TrajectoryParams {
651            mass_kg: 0.01134, // 175 grains in kg
652            bc: 0.442,
653            bullet_diameter: 0.0078232, // .308 in
654            bullet_length: 0.031496,    // 1.24 in
655            twist_rate: 10.0,
656            drag_model: DragModel::G7,
657            wind_segments: vec![(0.0, 90.0, 914.4)],
658            atmos_params: (0.0, 59.0, 29.92, 0.0),
659            omega_vector: None,
660            enable_spin_drift: false,
661            enable_magnus: false,
662            enable_coriolis: false,
663            target_distance_m: 914.4, // 1000 yards in meters
664            enable_wind_shear: false,
665            wind_shear_model: "none".to_string(),
666            shooter_altitude_m: 0.0,
667            is_twist_right: true,
668            shooting_angle: 0.0,
669            custom_drag_table: None,
670            bc_segments: None,
671            use_bc_segments: false,
672            ground_threshold: -1000.0,
673        };
674
675        println!("Running integrate_trajectory test...");
676        println!("Initial state: {:?}", initial_state);
677        println!("Target distance: {} m", params.target_distance_m);
678
679        let trajectory =
680            integrate_trajectory(initial_state, (0.0, 10.0), params, "RK45", 1e-6, 0.01);
681
682        println!("Trajectory has {} points", trajectory.len());
683
684        // Should have more than just initial point
685        assert!(
686            trajectory.len() > 1,
687            "Trajectory should have more than 1 point, but has {}",
688            trajectory.len()
689        );
690
691        // Check that we actually moved downrange
692        if let Some((_, final_state)) = trajectory.last() {
693            println!("Final state: downrange(x)={}", final_state[0]);
694            assert!(
695                final_state[0] > 0.0,
696                "Final x should be positive (bullet moved downrange)"
697            );
698            assert!(
699                final_state[0] >= 900.0,
700                "Final x should be near target distance"
701            );
702        }
703    }
704
705    #[test]
706    fn test_rk4_vs_rk45_consistency() {
707        // Both methods should give similar results for the same trajectory
708        let initial_state = [0.0, 0.0, 0.0, 800.0, 30.0, 0.0]; // McCoy: vx=downrange
709        let target_distance = 500.0;
710
711        let params_rk4 = create_test_params(target_distance);
712        let params_rk45 = create_test_params(target_distance);
713
714        let trajectory_rk4 =
715            integrate_trajectory(initial_state, (0.0, 5.0), params_rk4, "RK4", 1e-6, 0.001);
716        let trajectory_rk45 =
717            integrate_trajectory(initial_state, (0.0, 5.0), params_rk45, "RK45", 1e-6, 0.01);
718
719        // Both should reach target
720        assert!(!trajectory_rk4.is_empty());
721        assert!(!trajectory_rk45.is_empty());
722
723        let (_, final_rk4) = trajectory_rk4.last().unwrap();
724        let (_, final_rk45) = trajectory_rk45.last().unwrap();
725
726        // Final downrange positions should be within 1% of each other
727        let rk4_z = final_rk4[0];
728        let rk45_z = final_rk45[0];
729        let diff_percent = ((rk4_z - rk45_z) / rk45_z).abs() * 100.0;
730
731        assert!(
732            diff_percent < 1.0,
733            "RK4 and RK45 final positions differ by {}%: RK4={}, RK45={}",
734            diff_percent,
735            rk4_z,
736            rk45_z
737        );
738    }
739
740    #[test]
741    fn test_ground_impact_detection() {
742        // Trajectory with steep downward angle should hit ground
743        let initial_state = [0.0, 100.0, 0.0, 300.0, -50.0, 0.0]; // McCoy: vx=downrange // Steep descent
744
745        let mut params = create_test_params(10000.0); // Far target
746        params.target_distance_m = 10000.0;
747
748        let trajectory =
749            integrate_trajectory(initial_state, (0.0, 20.0), params, "RK45", 1e-6, 0.01);
750
751        // Should stop before reaching target due to ground impact
752        let (_, final_state) = trajectory.last().unwrap();
753
754        // y should be near ground threshold (-1000m)
755        assert!(
756            final_state[1] <= -900.0,
757            "Should hit ground, but y={}",
758            final_state[1]
759        );
760        assert!(
761            final_state[0] < 10000.0,
762            "Should not reach target, but z={}",
763            final_state[0]
764        );
765    }
766
767    #[test]
768    fn test_target_distance_reached() {
769        let initial_state = [0.0, 0.0, 0.0, 800.0, 20.0, 0.0]; // McCoy: vx=downrange
770        let target_distance = 300.0;
771
772        let params = create_test_params(target_distance);
773
774        let trajectory =
775            integrate_trajectory(initial_state, (0.0, 5.0), params, "RK45", 1e-6, 0.01);
776
777        let (_, final_state) = trajectory.last().unwrap();
778
779        // Should stop at or very near target distance
780        assert!(
781            (final_state[0] - target_distance).abs() < 1.0,
782            "Should reach target at {}m, but stopped at {}m",
783            target_distance,
784            final_state[0]
785        );
786    }
787
788    #[test]
789    fn test_wind_affects_trajectory() {
790        // Test that wind segments are properly stored and passed through
791        // The actual wind effect depends on the derivatives computation which
792        // uses the wind vector in the drag calculation
793        let initial_state = [0.0, 0.0, 0.0, 800.0, 30.0, 0.0]; // McCoy: vx=downrange
794        let target_distance = 500.0;
795
796        // No wind
797        let params_no_wind = create_test_params(target_distance);
798
799        // Strong headwind (0 degrees = headwind)
800        let mut params_headwind = create_test_params(target_distance);
801        params_headwind.wind_segments = vec![(72.0, 0.0, 500.0)]; // 72 km/h = 20 m/s headwind
802
803        let trajectory_no_wind = integrate_trajectory(
804            initial_state,
805            (0.0, 5.0),
806            params_no_wind,
807            "RK45",
808            1e-6,
809            0.01,
810        );
811        let trajectory_headwind = integrate_trajectory(
812            initial_state,
813            (0.0, 5.0),
814            params_headwind,
815            "RK45",
816            1e-6,
817            0.01,
818        );
819
820        // Both trajectories should complete
821        assert!(
822            !trajectory_no_wind.is_empty(),
823            "No-wind trajectory should complete"
824        );
825        assert!(
826            !trajectory_headwind.is_empty(),
827            "Headwind trajectory should complete"
828        );
829
830        let (time_no_wind, final_no_wind) = trajectory_no_wind.last().unwrap();
831        let (time_headwind, final_headwind) = trajectory_headwind.last().unwrap();
832
833        // Headwind should slow the bullet, resulting in longer flight time
834        // or different drop at same distance
835        let drop_no_wind = final_no_wind[1];
836        let drop_headwind = final_headwind[1];
837
838        // With headwind, bullet should drop more (more negative y) due to slower velocity
839        // The effect might be small, so we check that both trajectories are valid
840        println!("No wind: time={}, drop={}", time_no_wind, drop_no_wind);
841        println!("Headwind: time={}, drop={}", time_headwind, drop_headwind);
842
843        // Both should reach approximately the target distance
844        assert!(
845            (final_no_wind[0] - target_distance).abs() < 10.0,
846            "No-wind should reach target"
847        );
848        assert!(
849            (final_headwind[0] - target_distance).abs() < 10.0,
850            "Headwind should reach target"
851        );
852    }
853
854    #[test]
855    fn test_solve_trajectory_rust_output_format() {
856        let initial_state = [0.0, 0.0, 0.0, 800.0, 30.0, 0.0]; // McCoy: vx=downrange
857
858        let result = solve_trajectory_rust(
859            initial_state,
860            (0.0, 2.0),
861            0.01134,                 // mass_kg
862            0.442,                   // bc
863            DragModel::G7,           // drag_model
864            vec![],                  // wind_segments
865            (0.0, 59.0, 29.92, 0.0), // atmos_params
866            None,                    // omega_vector
867            false,                   // enable_spin_drift
868            false,                   // enable_magnus
869            false,                   // enable_coriolis
870            "RK45".to_string(),      // method
871            1e-6,                    // tolerance
872            0.01,                    // max_step
873            500.0,                   // target_distance_m
874        );
875
876        // Should return Vec of HashMaps with expected keys
877        assert!(!result.is_empty());
878
879        let first_point = &result[0];
880        assert!(first_point.contains_key("t"));
881        assert!(first_point.contains_key("x"));
882        assert!(first_point.contains_key("y"));
883        assert!(first_point.contains_key("z"));
884        assert!(first_point.contains_key("vx"));
885        assert!(first_point.contains_key("vy"));
886        assert!(first_point.contains_key("vz"));
887    }
888
889    #[test]
890    fn test_left_vs_right_twist() {
891        let initial_state = [0.0, 0.0, 0.0, 800.0, 30.0, 0.0]; // McCoy: vx=downrange
892        let target_distance = 500.0;
893
894        let mut params_right = create_test_params(target_distance);
895        params_right.is_twist_right = true;
896        params_right.enable_spin_drift = true;
897
898        let mut params_left = create_test_params(target_distance);
899        params_left.is_twist_right = false;
900        params_left.enable_spin_drift = true;
901
902        let trajectory_right =
903            integrate_trajectory(initial_state, (0.0, 5.0), params_right, "RK45", 1e-6, 0.01);
904        let trajectory_left =
905            integrate_trajectory(initial_state, (0.0, 5.0), params_left, "RK45", 1e-6, 0.01);
906
907        // Both should complete
908        assert!(!trajectory_right.is_empty());
909        assert!(!trajectory_left.is_empty());
910
911        // Right and left twist should produce valid trajectories
912        let (_, final_right) = trajectory_right.last().unwrap();
913        let (_, final_left) = trajectory_left.last().unwrap();
914
915        // Both should reach approximately the same downrange distance
916        assert!((final_right[2] - final_left[2]).abs() < 10.0);
917    }
918}