Skip to main content

ballistics_engine/
request_roundtrip.rs

1//! Reverse conversion from the canonical resolved request back into a solvable request
2//! (Phase 0 of the 0.33.0 decision-support train).
3//!
4//! The resolved request was otherwise output-only. The perturbation kernel needs to take a
5//! resolved request, change one input, and re-solve, which is impossible without this
6//! direction. Every resolved value is carried across explicitly: a silently lossy
7//! conversion would misattribute the dropped field's effect to whatever the caller happened
8//! to be perturbing.
9//!
10//! Two fields are deliberately NOT carried straight across, because the resolved sibling
11//! they'd ride along with is already the post-transform value, and re-supplying the
12//! original input mode would apply that transform a second time:
13//!
14//! - `atmosphere.pressure_reference`: when the original request declared `"qnh"`,
15//!   [`ResolvedAtmosphereV1::pressure_pa`] is already the REDUCED absolute station pressure
16//!   (see `resolve_atmosphere`'s QNH branch) -- echoing `"qnh"` back alongside that
17//!   already-reduced value would reduce it a second time. The rebuilt request always states
18//!   `pressure_pa` as absolute (the omitted-field default), which is what the resolved
19//!   value already is.
20//! - `wind.wind_reference`: when the original request declared `"compass"`, every resolved
21//!   wind direction is already converted to shooter-relative (see `resolve_wind`'s
22//!   `to_relative`) -- echoing `"compass"` back alongside an already-relative direction
23//!   would re-reference it against the shot azimuth a second time. The rebuilt request
24//!   always states directions as shooter-relative (the omitted-field default), which is
25//!   what the resolved values already are.
26//!
27//! `ResolvedShotV1` carries both `zero_distance_m` (caller intent) and `muzzle_angle_rad`
28//! (the effective angle after zeroing). Both are carried onto the rebuilt request: an
29//! explicit `muzzle_angle_rad` always takes priority over `zero_distance_m` at resolve time
30//! (see `resolve_shot` and `solve_v1`'s zero-search gate), so supplying both reproduces the
31//! exact original angle -- bit-identical, not just numerically re-converged -- while still
32//! preserving the original zeroing intent as metadata rather than dropping it.
33
34use crate::solve_json::*;
35
36impl From<&ResolvedSolveRequestV1> for SolveRequestV1 {
37    fn from(r: &ResolvedSolveRequestV1) -> Self {
38        SolveRequestV1 {
39            schema_version: SchemaVersionV1,
40            projectile: ProjectileV1 {
41                mass_kg: r.projectile.mass_kg,
42                diameter_m: r.projectile.diameter_m,
43                length_m: r.projectile.length_m,
44                drag_model: r.projectile.drag_model,
45                ballistic_coefficient: r.projectile.ballistic_coefficient,
46            },
47            rifle: RifleV1 {
48                muzzle_velocity_mps: r.rifle.muzzle_velocity_mps,
49                sight_height_m: Some(r.rifle.sight_height_m),
50                muzzle_height_m: Some(r.rifle.muzzle_height_m),
51                twist_rate_m_per_turn: Some(r.rifle.twist_rate_m_per_turn),
52                twist_direction: Some(r.rifle.twist_direction),
53                sight_offset_lateral_m: r.rifle.sight_offset_lateral_m,
54            },
55            shot: ShotV1 {
56                max_range_m: r.shot.max_range_m,
57                zero_distance_m: r.shot.zero_distance_m,
58                // Both are carried: zero_distance_m is caller intent, muzzle_angle_rad is
59                // the angle actually integrated after zeroing. An explicit muzzle_angle_rad
60                // always wins at resolve time (resolve_shot / solve_v1), so this reproduces
61                // the exact original angle with no re-zero, whether or not zero_distance_m
62                // is also present.
63                muzzle_angle_rad: Some(r.shot.muzzle_angle_rad),
64                aim_azimuth_rad: Some(r.shot.aim_azimuth_rad),
65                shot_azimuth_rad: Some(r.shot.shot_azimuth_rad),
66                shooting_angle_rad: Some(r.shot.shooting_angle_rad),
67                cant_angle_rad: Some(r.shot.cant_angle_rad),
68                target_height_m: Some(r.shot.target_height_m),
69                ground_threshold_m: Some(r.shot.ground_threshold_m),
70                zero_poi_up_m: r.shot.zero_poi_up_m,
71                zero_poi_right_m: r.shot.zero_poi_right_m,
72                drops_reference: r.shot.drops_reference,
73            },
74            atmosphere: AtmosphereV1 {
75                altitude_m: Some(r.atmosphere.altitude_m),
76                temperature_k: Some(r.atmosphere.temperature_k),
77                pressure_pa: Some(r.atmosphere.pressure_pa),
78                // See the module doc: pressure_pa above is already absolute station
79                // pressure; echoing a "qnh" reference back would reduce it a second time.
80                pressure_reference: None,
81                relative_humidity: Some(r.atmosphere.relative_humidity),
82                latitude_rad: r.atmosphere.latitude_rad,
83            },
84            wind: wind_from_resolved(&r.wind),
85            solver: SolverV1 {
86                method: Some(r.solver.method),
87                time_step_s: Some(r.solver.time_step_s),
88            },
89            effects: EffectsV1 {
90                magnus: Some(r.effects.magnus),
91                coriolis: Some(r.effects.coriolis),
92                enhanced_spin_drift: Some(r.effects.enhanced_spin_drift),
93            },
94            sampling: SamplingV1 {
95                interval_m: Some(r.sampling.interval_m),
96            },
97            reticle: r.reticle.clone(),
98        }
99    }
100}
101
102fn wind_from_resolved(w: &ResolvedWindV1) -> WindV1 {
103    match w {
104        ResolvedWindV1::Constant(c) => WindV1 {
105            speed_mps: Some(c.speed_mps),
106            direction_from_rad: Some(c.direction_from_rad),
107            vertical_speed_mps: Some(c.vertical_speed_mps),
108            segments: None,
109            // See the module doc: direction_from_rad above is already shooter-relative;
110            // echoing a "compass" reference back would re-reference it a second time.
111            wind_reference: None,
112        },
113        ResolvedWindV1::Segmented(s) => WindV1 {
114            speed_mps: None,
115            direction_from_rad: None,
116            vertical_speed_mps: None,
117            segments: Some(
118                s.segments
119                    .iter()
120                    .map(|g| WindSegmentV1 {
121                        until_distance_m: g.until_distance_m,
122                        speed_mps: g.speed_mps,
123                        direction_from_rad: g.direction_from_rad,
124                        vertical_speed_mps: Some(g.vertical_speed_mps),
125                    })
126                    .collect(),
127            ),
128            // See the module doc and the constant-wind arm above.
129            wind_reference: None,
130        },
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use crate::solve_json::decode_solve_request_v1;
137    use crate::solve_json::{PressureReferenceV1, ResolvedWindV1, SolveRequestV1, WindReferenceV1};
138    use crate::solve_v1::solve_v1;
139
140    fn sample_json() -> String {
141        serde_json::json!({
142            "schema_version": 1,
143            "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
144                           "ballistic_coefficient": 0.243},
145            "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
146            "shot": {"max_range_m": 900.0, "zero_distance_m": 100.0},
147            "atmosphere": {"temperature_k": 288.0, "pressure_pa": 101325.0},
148            "wind": {"speed_mps": 3.0, "direction_from_rad": std::f64::consts::FRAC_PI_2},
149            "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
150        })
151        .to_string()
152    }
153
154    /// Resolution is idempotent through a round-trip: re-solving a request
155    /// rebuilt from a resolved request must resolve to exactly the same values.
156    /// This is the acceptance gate for Phase 0.
157    ///
158    /// Compares the whole success envelope, not just `resolved_request`: some effects (the
159    /// windage-zero convergence bias, see `roundtrip_preserves_the_windage_zero_bias` below)
160    /// never appear in `resolved_request` at all -- on the very first solve, not only after a
161    /// round-trip -- so `resolved_request` equality alone cannot catch every regression.
162    #[test]
163    fn resolution_is_idempotent_through_roundtrip() {
164        let first = solve_v1(decode_solve_request_v1(&sample_json()).unwrap()).unwrap();
165        let rebuilt: SolveRequestV1 = (&first.resolved_request).into();
166        let second = solve_v1(rebuilt).unwrap();
167        assert_eq!(
168            serde_json::to_value(&first.resolved_request).unwrap(),
169            serde_json::to_value(&second.resolved_request).unwrap(),
170            "resolved request changed after a round-trip"
171        );
172        assert_eq!(
173            first.summary, second.summary,
174            "summary changed after a round-trip"
175        );
176        assert_eq!(
177            first.samples, second.samples,
178            "samples changed after a round-trip"
179        );
180    }
181
182    /// The windage-zero convergence bias (`sight_offset_lateral_m` / `zero_poi_right_m`,
183    /// applied via `BallisticInputs::windage_zero_bias_rad`) is a term
184    /// `calculate_and_set_zero_angle` adds to azimuth ALONGSIDE the elevation search -- it is
185    /// not carried by `muzzle_angle_rad`, and (unlike the elevation) it never appears in
186    /// `resolved_request` at all, on the first solve or any later one. Skipping the elevation
187    /// search on a round-tripped request must not also skip this separate term, or an
188    /// offset-mounted sight / deliberate horizontal zero bias would silently stop converging
189    /// the moment a resolved request round-trips. `resolved_request` alone cannot see this
190    /// (compare `first.resolved_request` above with `second.resolved_request` below: they are
191    /// byte-identical even when this regresses), so this compares the solved trajectory too.
192    #[test]
193    fn roundtrip_preserves_the_windage_zero_bias() {
194        let json = serde_json::json!({
195            "schema_version": 1,
196            "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
197                           "ballistic_coefficient": 0.243},
198            "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05,
199                      "sight_offset_lateral_m": 0.03},
200            "shot": {"max_range_m": 900.0, "zero_distance_m": 100.0, "zero_poi_right_m": 0.02},
201            "atmosphere": {"temperature_k": 288.0, "pressure_pa": 101325.0},
202            "wind": {"speed_mps": 3.0, "direction_from_rad": std::f64::consts::FRAC_PI_2},
203            "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
204        })
205        .to_string();
206        let first = solve_v1(decode_solve_request_v1(&json).unwrap()).unwrap();
207        let rebuilt: SolveRequestV1 = (&first.resolved_request).into();
208        let second = solve_v1(rebuilt).unwrap();
209
210        assert_eq!(
211            serde_json::to_value(&first.resolved_request).unwrap(),
212            serde_json::to_value(&second.resolved_request).unwrap(),
213            "resolved request changed after a round-trip"
214        );
215        assert_eq!(
216            first.summary, second.summary,
217            "summary changed after a round-trip -- the windage-zero bias may have been dropped"
218        );
219        assert_eq!(
220            first.samples, second.samples,
221            "samples changed after a round-trip -- the windage-zero bias may have been dropped"
222        );
223        // Sanity check: the bias is actually nonzero in this fixture, so the comparisons
224        // above are exercising the real thing rather than two agreeing zeros.
225        let windage_m = first
226            .samples
227            .last()
228            .expect("at least one sample")
229            .windage_m;
230        assert!(
231            windage_m.abs() > 0.1,
232            "fixture must produce a non-negligible windage-zero bias to be a meaningful test, \
233             got {windage_m} m"
234        );
235    }
236
237    /// A zeroed solve must not silently re-zero on the way back.
238    #[test]
239    fn roundtrip_preserves_the_effective_muzzle_angle() {
240        let first = solve_v1(decode_solve_request_v1(&sample_json()).unwrap()).unwrap();
241        let rebuilt: SolveRequestV1 = (&first.resolved_request).into();
242        assert_eq!(
243            rebuilt.shot.muzzle_angle_rad,
244            Some(first.resolved_request.shot.muzzle_angle_rad)
245        );
246        assert_eq!(
247            rebuilt.shot.zero_distance_m,
248            first.resolved_request.shot.zero_distance_m
249        );
250    }
251
252    /// A QNH-declared pressure is reduced to absolute station pressure exactly once.
253    /// `ResolvedAtmosphereV1::pressure_pa` is already that reduced value; the rebuilt
254    /// request deliberately does not echo `pressure_reference: "qnh"` back alongside it
255    /// (see the module doc), so the round-tripped resolved echo differs in exactly that one
256    /// field. What must NOT differ is the reduced `pressure_pa` value itself: if the
257    /// rebuilt request echoed the mode back too, the second resolve would reduce it a
258    /// second time and silently corrupt it.
259    #[test]
260    fn roundtrip_does_not_double_reduce_a_qnh_pressure() {
261        let json = serde_json::json!({
262            "schema_version": 1,
263            "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
264                           "ballistic_coefficient": 0.243},
265            "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
266            "shot": {"max_range_m": 900.0},
267            "atmosphere": {"altitude_m": 500.0, "temperature_k": 288.0, "pressure_pa": 101325.0,
268                           "pressure_reference": "qnh"},
269            "wind": {}, "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
270        })
271        .to_string();
272        let first = solve_v1(decode_solve_request_v1(&json).unwrap()).unwrap();
273        assert_eq!(
274            first.resolved_request.atmosphere.pressure_reference,
275            Some(PressureReferenceV1::Qnh)
276        );
277
278        let rebuilt: SolveRequestV1 = (&first.resolved_request).into();
279        let second = solve_v1(rebuilt).unwrap();
280
281        // The one deliberate difference: the reference mode is not echoed back (its
282        // transform is already baked into pressure_pa, so re-supplying it would be a
283        // second application, not a no-op).
284        assert_eq!(second.resolved_request.atmosphere.pressure_reference, None);
285        // What actually matters -- the physical quantity -- is unchanged.
286        assert_eq!(
287            second.resolved_request.atmosphere.pressure_pa,
288            first.resolved_request.atmosphere.pressure_pa,
289            "a round-tripped QNH pressure must not be reduced a second time"
290        );
291        assert_eq!(
292            second.resolved_request.atmosphere.altitude_m,
293            first.resolved_request.atmosphere.altitude_m
294        );
295        assert_eq!(
296            second.resolved_request.atmosphere.temperature_k,
297            first.resolved_request.atmosphere.temperature_k
298        );
299    }
300
301    /// A compass-declared wind direction is converted to shooter-relative exactly once.
302    /// The resolved direction is already that converted value; the rebuilt request
303    /// deliberately does not echo `wind_reference: "compass"` back alongside it (see the
304    /// module doc), so the round-tripped resolved echo differs in exactly that one field.
305    /// What must NOT differ is the converted `direction_from_rad` value itself: if the
306    /// rebuilt request echoed the mode back too, the second resolve would re-reference it
307    /// against the shot azimuth a second time and silently corrupt it.
308    #[test]
309    fn roundtrip_does_not_double_reference_a_compass_wind() {
310        let json = serde_json::json!({
311            "schema_version": 1,
312            "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
313                           "ballistic_coefficient": 0.243},
314            "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
315            "shot": {"max_range_m": 900.0, "shot_azimuth_rad": 0.3},
316            "atmosphere": {},
317            "wind": {"speed_mps": 3.0, "direction_from_rad": 1.0, "wind_reference": "compass"},
318            "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
319        })
320        .to_string();
321        let first = solve_v1(decode_solve_request_v1(&json).unwrap()).unwrap();
322        let ResolvedWindV1::Constant(first_wind) = &first.resolved_request.wind else {
323            panic!("constant wind expected");
324        };
325        assert_eq!(first_wind.wind_reference, Some(WindReferenceV1::Compass));
326        // Sanity check: compass mode actually converted the direction (it is not simply
327        // echoing the 1.0 rad bearing the request supplied).
328        assert_ne!(first_wind.direction_from_rad, 1.0);
329        let first_direction_from_rad = first_wind.direction_from_rad;
330
331        let rebuilt: SolveRequestV1 = (&first.resolved_request).into();
332        let second = solve_v1(rebuilt).unwrap();
333        let ResolvedWindV1::Constant(second_wind) = &second.resolved_request.wind else {
334            panic!("constant wind expected");
335        };
336
337        // The one deliberate difference: the reference mode is not echoed back.
338        assert_eq!(second_wind.wind_reference, None);
339        // What actually matters -- the physical quantity -- is unchanged.
340        assert_eq!(
341            second_wind.direction_from_rad, first_direction_from_rad,
342            "a round-tripped compass wind must not be re-referenced a second time"
343        );
344    }
345}