ballistics_engine/perturbation/access.rs
1//! Reading and writing one taxonomy axis on a canonical request.
2//!
3//! `with_axis` produces a full `SolveRequestV1` via the Phase 0 reverse conversion
4//! (`request_roundtrip.rs`) and then overwrites exactly one field, so every other input is
5//! carried across unchanged.
6//!
7//! Two axes are enum-valued rather than scalar or boolean (`DragModel`:
8//! G1/G2/G5/G6/G7/G8/GI/GS/RA4; `TwistDirection`: left/right), so [`AxisValue`] carries a
9//! dedicated variant for each instead of round-tripping through a string -- a string round-trip
10//! would turn a typo into a runtime [`KernelError::TypeMismatch`] instead of a compile error.
11//!
12//! `with_axis` also refuses two axis/request combinations that would otherwise silently build
13//! a physically WRONG counterfactual, because [the reverse conversion](crate::solve_json)
14//! (see `request_roundtrip.rs`'s module doc) always emits absolute station pressure and
15//! shooter-relative wind regardless of how the original request entered them:
16//!
17//! - `Altitude` when the original request declared a QNH pressure: perturbing altitude would
18//! change air density-by-altitude without moving the QNH-referenced station pressure the
19//! way the caller means -- the opposite of the intended counterfactual.
20//! - `ShotAzimuth` when the original request declared compass-referenced wind: perturbing the
21//! shot azimuth would rotate the wind WITH the rifle instead of keeping it earth-fixed --
22//! physically inverted.
23//!
24//! Both are detected from the ORIGINAL resolved request's echoed reference mode (added in
25//! Task 1 precisely so this is detectable) and rejected with
26//! [`KernelError::AxisUnsupportedForRequest`] rather than producing a silently-wrong request.
27//! See `taxonomy.rs`'s "KNOWN LIMITATIONS" comment, items (a) and (b).
28//!
29//! A third check keeps `with_axis` consistent with `read_axis` rather than physics-aware: the
30//! three wind axes have no single scalar to write when the resolved wind is
31//! [`ResolvedWindV1::Segmented`] (taxonomy.rs Known Limitation (c)). `read_axis` already
32//! returns `None` for that combination; `with_axis` now returns [`KernelError::AxisAbsent`]
33//! for the same combination instead of silently writing a constant-wind field (`speed_mps`
34//! etc.) alongside the still-present `wind.segments`, which `solve_v1`'s `resolve_wind` would
35//! otherwise reject downstream as an opaque `$.wind` "segments cannot be combined with
36//! constant-wind fields" error.
37//!
38//! The zero-affecting-axis rezero clear (see `with_axis`'s body) is evaluated from
39//! `req.shot.zero_distance_m` **after** the axis has been written, not before: for the
40//! `ZeroDistance` axis itself, the value that matters is the NEW distance being written, not
41//! whatever `zero_distance_m` was on the original request (which may have been absent).
42//! Gating on the pre-write value would let a request originally specified purely by
43//! `muzzle_angle_rad` silently keep that stale angle after a `ZeroDistance` write, so
44//! `solve_v1` would skip the elevation search entirely (it takes an explicit angle over a
45//! zero distance whenever both are present) and the new zero distance would have no effect on
46//! elevation at all.
47
48use crate::perturbation::taxonomy::{axis_meta, InputAxis};
49use crate::solve_json::{
50 DragModelV1, PressureReferenceV1, ResolvedSolveRequestV1, ResolvedWindV1, SolveErrorCodeV1,
51 SolveRequestV1, TwistDirectionV1, WindReferenceV1,
52};
53use crate::trajectory_observation::TrajectoryObservationError;
54
55/// One taxonomy axis's value, read from or written to a request.
56///
57/// `Scalar`/`Flag` cover every continuous and boolean axis. `DragModel`/`TwistDirection`
58/// cover the two enum-valued axes losslessly -- see the module doc for why a `String` variant
59/// would be the wrong choice.
60#[derive(Debug, Clone, Copy, PartialEq)]
61pub enum AxisValue {
62 Scalar(f64),
63 Flag(bool),
64 DragModel(DragModelV1),
65 TwistDirection(TwistDirectionV1),
66}
67
68#[derive(Debug, Clone, PartialEq)]
69pub enum KernelError {
70 /// Reserved for a later (differentiation) task: an axis whose `axis_meta(axis).kind` is
71 /// `AxisKind::Categorical` was asked to be treated as continuous. Not constructed by
72 /// `read_axis`/`with_axis` themselves, which accept categorical axes just like any other.
73 CategoricalAxis(InputAxis),
74 /// The axis is structurally unavailable on THIS request's resolved shape -- currently only
75 /// the three wind axes when the resolved wind is `ResolvedWindV1::Segmented`, mirroring
76 /// `read_axis` returning `None` for the identical condition (see the module doc).
77 AxisAbsent(InputAxis),
78 /// The `AxisValue` variant supplied to `with_axis` does not match what `axis` expects
79 /// (e.g. a `Scalar` for `DragModel`, or a `Flag` for any continuous axis).
80 TypeMismatch(InputAxis),
81 /// The axis is well-formed and present, but this particular request's OTHER inputs make
82 /// perturbing it physically wrong rather than merely unrepresentable -- see the guards
83 /// documented on `with_axis`.
84 AxisUnsupportedForRequest {
85 axis: InputAxis,
86 reason: &'static str,
87 },
88 /// The rebuilt request failed to resolve or solve. Not constructed here -- `with_axis` only
89 /// builds the request, it does not solve it; [`crate::perturbation::evaluate`] (0.33.0
90 /// decision-support Task 6) is the constructor, uniformly for every stage that can fail
91 /// (`solve_v1::prepare_request`, `solve_v1::build_zeroed_solver`, and
92 /// `TrajectorySolver::solve`).
93 ///
94 /// `code` is carried alongside `message` (review fix I4(a), `derive.rs`) specifically so a
95 /// caller -- most notably `central_difference`'s one-sided fallback -- can tell a domain
96 /// rejection (`SolveErrorCodeV1::InvalidValue`, produced by `require_range`/
97 /// `require_non_negative`/`require_positive` in `solve_v1.rs`) apart from a genuine solver
98 /// failure (`SolveFailed`, `ResourceLimit`, `InternalError`) or a malformed-request bug
99 /// (`ConflictingFields`, `MissingField`, `UnknownField`, ...) that merely happened to be
100 /// triggered by one particular perturbed value. Collapsing this to a `String` (the previous
101 /// shape) made that distinction unrecoverable except by parsing the message.
102 Solve {
103 code: SolveErrorCodeV1,
104 message: String,
105 },
106 /// Post-solve observation extraction failed -- most notably a requested range outside the
107 /// computed trajectory. Not constructed here for the same reason as `Solve`;
108 /// [`crate::perturbation::evaluate`] constructs it directly from a
109 /// [`crate::trajectory_observation::TrajectoryObservationError`], preserved WHOLE (not
110 /// collapsed to its `Display` string) for the same reason as `Solve`'s `code` above: only
111 /// [`TrajectoryObservationError::OutOfRange`] is a domain rejection eligible for
112 /// `central_difference`'s one-sided fallback -- `NonMonotonicTrajectory`, `NonFiniteState`,
113 /// `SampleLimitExceeded`, `AllocationFailed`, and the rest are genuine bugs that must
114 /// propagate, not be silently reinterpreted as "this side left the domain."
115 Observation(TrajectoryObservationError),
116 /// A `Scalar` value supplied to `with_axis` was not finite (NaN or infinite).
117 NonFinite(InputAxis),
118 /// Both perturbed sides of a central difference failed to produce a usable observation --
119 /// neither `x + attempted` nor `x - attempted` evaluates -- so not even a one-sided
120 /// difference can be built (`central_difference`'s domain fallback,
121 /// `src/perturbation/derive.rs`). Distinct from a derivative of zero: this means
122 /// "undifferentiable with this step," never "no effect."
123 StepOutOfDomain { axis: InputAxis, attempted: f64 },
124 /// The same axis was declared more than once in a caller-supplied source list. Not
125 /// constructed by `read_axis`/`with_axis`/`central_difference` (none of which see more than
126 /// one axis at a time) -- `crate::error_budget::error_budget` (0.33.0 decision-support Task
127 /// 10, MBA-1347 review) constructs this from its own up-front validation, because two
128 /// entries for the same axis would double-count that axis's variance and corrupt its
129 /// leave-one-out counterfactual (removing "the" declaration for that axis is ambiguous when
130 /// there are two). `KernelError` had not shipped on any released version when this variant
131 /// was added, so there is no compatibility concern in extending it.
132 DuplicateAxis(InputAxis),
133 /// The domain supplied to `crate::tolerance::tolerance_envelope` for one axis is missing, or
134 /// does not have the shape a one-variable bisection needs: both bounds finite, the lower
135 /// bound strictly less than the upper, and the axis's own current value strictly between
136 /// them. A domain where the current value sits AT (not strictly inside) one edge would make
137 /// that search direction a zero-width probe -- `bisect_axis` would report `Ok(None)` for it
138 /// (the two identical endpoints trivially agree), and that `None` would be indistinguishable
139 /// from a genuine "stays inside throughout" result even though nothing beyond the current
140 /// value was ever actually searched. 0.33.0 decision-support Task 12 (MBA-1350): never
141 /// constructed by `with_axis`/`evaluate`/`bisect_axis` themselves -- `tolerance_envelope`
142 /// validates its own domain argument up front, before any solve, the same way
143 /// `crate::error_budget::error_budget` validates its `ranges_m`/`sources` arguments.
144 InvalidDomain {
145 axis: InputAxis,
146 reason: &'static str,
147 },
148}
149
150impl std::fmt::Display for KernelError {
151 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152 match self {
153 KernelError::CategoricalAxis(a) => {
154 write!(f, "axis {a:?} is categorical and cannot be differentiated")
155 }
156 KernelError::AxisAbsent(a) => write!(f, "axis {a:?} is not present in this request"),
157 KernelError::TypeMismatch(a) => write!(f, "value type does not match axis {a:?}"),
158 KernelError::AxisUnsupportedForRequest { axis, reason } => {
159 write!(
160 f,
161 "axis {axis:?} is not supported for this request: {reason}"
162 )
163 }
164 KernelError::Solve { code, message } => write!(f, "solve failed ({code:?}): {message}"),
165 KernelError::Observation(e) => write!(f, "observation failed: {e}"),
166 KernelError::NonFinite(a) => write!(f, "axis {a:?} produced a non-finite result"),
167 KernelError::StepOutOfDomain { axis, attempted } => write!(
168 f,
169 "axis {axis:?} could not be differentiated with step {attempted}: both the \
170 forward and backward perturbed values failed to evaluate"
171 ),
172 KernelError::DuplicateAxis(a) => {
173 write!(f, "axis {a:?} was declared more than once")
174 }
175 KernelError::InvalidDomain { axis, reason } => {
176 write!(
177 f,
178 "invalid tolerance-envelope domain for axis {axis:?}: {reason}"
179 )
180 }
181 }
182 }
183}
184impl std::error::Error for KernelError {}
185
186impl KernelError {
187 /// True when this error means the specific perturbed VALUE fell outside the axis's physical
188 /// domain -- an `InvalidValue` solve rejection (from `require_range`/`require_non_negative`/
189 /// `require_positive` in `solve_v1.rs`), or an `OutOfRange` observation query (a requested
190 /// range that fell outside a trajectory shrunk by the perturbation) -- as opposed to a
191 /// genuine solver or trajectory failure that merely happened to be triggered by one
192 /// particular perturbed value.
193 ///
194 /// This is the ONLY thing [`crate::perturbation::central_difference`]'s one-sided fallback
195 /// (`derive.rs`) may gate on. Review fix I4(a): an earlier revision gated on `Err(_)`
196 /// (any failure at all on one side), which silently reinterpreted genuine bugs -- a
197 /// non-convergent zero search, a non-finite trajectory state, a sample-limit overrun -- as
198 /// if they were domain boundaries, answering with a fabricated derivative instead of
199 /// reporting the real failure.
200 pub fn is_domain_rejection(&self) -> bool {
201 matches!(
202 self,
203 KernelError::Solve {
204 code: SolveErrorCodeV1::InvalidValue,
205 ..
206 } | KernelError::Observation(TrajectoryObservationError::OutOfRange { .. })
207 )
208 }
209}
210
211/// The request's wind-reference echo, whichever wind shape it resolved to.
212///
213/// `pub(crate)` (0.33.0 decision-support Task 9, review round 4): `explain.rs`'s
214/// `is_compass_referenced` needs the identical lookup to detect the `WindDirection` derived-
215/// value hazard under compass wind (a different axis than the `ShotAzimuth` guard just below,
216/// which is what this function was written for), so it shares this one instead of duplicating
217/// the two-arm match a second time.
218pub(crate) fn wind_reference_of(w: &ResolvedWindV1) -> Option<WindReferenceV1> {
219 match w {
220 ResolvedWindV1::Constant(c) => c.wind_reference,
221 ResolvedWindV1::Segmented(s) => s.wind_reference,
222 }
223}
224
225/// Read the current value of one taxonomy axis off a resolved request.
226///
227/// Returns `None` in two DIFFERENT situations a caller should not conflate:
228/// - the axis is an optional request field that was never supplied (`Length`, `Latitude`,
229/// `ZeroPoiUp`/`ZeroPoiRight`, `SightOffsetLateral`) -- the axis exists, it just has no
230/// value on this particular request; or
231/// - the axis is structurally unavailable given how a sibling field resolved -- currently only
232/// `WindSpeed`/`WindDirection`/`WindVertical` under `ResolvedWindV1::Segmented`, where there
233/// is no single scalar wind value to read at all (taxonomy.rs Known Limitation (c)).
234///
235/// Either way, `None` means "there is nothing to perturb here": a caller sweeping
236/// `InputAxis::ALL` should skip the axis, never invent a value for it.
237pub fn read_axis(r: &ResolvedSolveRequestV1, axis: InputAxis) -> Option<AxisValue> {
238 use InputAxis::*;
239 let wind_speed = match &r.wind {
240 ResolvedWindV1::Constant(c) => Some(c.speed_mps),
241 ResolvedWindV1::Segmented(_) => None,
242 };
243 let wind_dir = match &r.wind {
244 ResolvedWindV1::Constant(c) => Some(c.direction_from_rad),
245 ResolvedWindV1::Segmented(_) => None,
246 };
247 let wind_vert = match &r.wind {
248 ResolvedWindV1::Constant(c) => Some(c.vertical_speed_mps),
249 ResolvedWindV1::Segmented(_) => None,
250 };
251 Some(match axis {
252 Mass => AxisValue::Scalar(r.projectile.mass_kg),
253 Diameter => AxisValue::Scalar(r.projectile.diameter_m),
254 Length => AxisValue::Scalar(r.projectile.length_m?),
255 BallisticCoefficient => AxisValue::Scalar(r.projectile.ballistic_coefficient),
256 TwistRate => AxisValue::Scalar(r.rifle.twist_rate_m_per_turn),
257 TwistDirection => AxisValue::TwistDirection(r.rifle.twist_direction),
258 DragModel => AxisValue::DragModel(r.projectile.drag_model),
259 MuzzleVelocityMps => AxisValue::Scalar(r.rifle.muzzle_velocity_mps),
260 SightHeight => AxisValue::Scalar(r.rifle.sight_height_m),
261 ZeroDistance => AxisValue::Scalar(r.shot.zero_distance_m?),
262 ZeroPoiUp => AxisValue::Scalar(r.shot.zero_poi_up_m?),
263 ZeroPoiRight => AxisValue::Scalar(r.shot.zero_poi_right_m?),
264 SightOffsetLateral => AxisValue::Scalar(r.rifle.sight_offset_lateral_m?),
265 MuzzleHeight => AxisValue::Scalar(r.rifle.muzzle_height_m),
266 MuzzleAngle => AxisValue::Scalar(r.shot.muzzle_angle_rad),
267 Altitude => AxisValue::Scalar(r.atmosphere.altitude_m),
268 Temperature => AxisValue::Scalar(r.atmosphere.temperature_k),
269 Pressure => AxisValue::Scalar(r.atmosphere.pressure_pa),
270 RelativeHumidity => AxisValue::Scalar(r.atmosphere.relative_humidity),
271 Latitude => AxisValue::Scalar(r.atmosphere.latitude_rad?),
272 WindSpeed => AxisValue::Scalar(wind_speed?),
273 WindDirection => AxisValue::Scalar(wind_dir?),
274 WindVertical => AxisValue::Scalar(wind_vert?),
275 TargetDistance => AxisValue::Scalar(r.shot.max_range_m),
276 ShootingAngle => AxisValue::Scalar(r.shot.shooting_angle_rad),
277 Cant => AxisValue::Scalar(r.shot.cant_angle_rad),
278 ShotAzimuth => AxisValue::Scalar(r.shot.shot_azimuth_rad),
279 AimAzimuth => AxisValue::Scalar(r.shot.aim_azimuth_rad),
280 TargetHeight => AxisValue::Scalar(r.shot.target_height_m),
281 MagnusEnabled => AxisValue::Flag(r.effects.magnus),
282 CoriolisEnabled => AxisValue::Flag(r.effects.coriolis),
283 EnhancedSpinDriftEnabled => AxisValue::Flag(r.effects.enhanced_spin_drift),
284 })
285}
286
287/// Rebuild `r` as a solvable [`SolveRequestV1`] with exactly one axis overwritten to `v`.
288///
289/// Every other input is carried across unchanged via the reverse conversion
290/// (`request_roundtrip.rs`). Writing a `requires_rezero` axis (see
291/// [`crate::perturbation::axis_meta`]) while a `zero_distance_m` is present on the REBUILT
292/// request clears the carried effective `muzzle_angle_rad`, so the next solve re-zeroes at
293/// the (possibly just-changed) distance instead of reusing a stale angle -- see the module
294/// doc for why this is checked after the axis is written, not before.
295///
296/// # Errors
297///
298/// - [`KernelError::AxisUnsupportedForRequest`] if `axis` is well-formed and present, but this
299/// request's OTHER inputs make perturbing it physically wrong (the two guards in the module
300/// doc: `Altitude` under QNH pressure, `ShotAzimuth` under compass wind).
301/// - [`KernelError::AxisAbsent`] if `axis` is structurally unavailable on this request (the
302/// three wind axes under segmented wind), mirroring `read_axis` returning `None` for the
303/// same condition.
304/// - [`KernelError::TypeMismatch`] if `v`'s kind does not match `axis` (e.g. a `Scalar` for
305/// `DragModel`).
306/// - [`KernelError::NonFinite`] if `v` is a non-finite `Scalar`.
307pub fn with_axis(
308 r: &ResolvedSolveRequestV1,
309 axis: InputAxis,
310 v: AxisValue,
311) -> Result<SolveRequestV1, KernelError> {
312 // Physics guards (see the module doc): detected from the ORIGINAL resolved request's
313 // echoed reference mode, before anything else, so a dangerous combination never reaches
314 // the reverse conversion below.
315 if axis == InputAxis::Altitude
316 && r.atmosphere.pressure_reference == Some(PressureReferenceV1::Qnh)
317 {
318 return Err(KernelError::AxisUnsupportedForRequest {
319 axis,
320 reason: "the original request declared a QNH pressure_reference; the rebuilt \
321 request always carries absolute station pressure (request_roundtrip.rs \
322 cannot re-derive the original altimeter setting), so perturbing altitude \
323 would change air density-by-altitude without moving the QNH-referenced \
324 station pressure the way the caller means",
325 });
326 }
327 if axis == InputAxis::ShotAzimuth
328 && wind_reference_of(&r.wind) == Some(WindReferenceV1::Compass)
329 {
330 return Err(KernelError::AxisUnsupportedForRequest {
331 axis,
332 reason: "the original request declared compass-referenced wind; the rebuilt \
333 request always carries shooter-relative wind (request_roundtrip.rs \
334 cannot re-derive the original earth-fixed bearing), so perturbing the \
335 shot azimuth would rotate the wind WITH the rifle instead of keeping it \
336 earth-fixed",
337 });
338 }
339 // Third check (see the module doc): keeps with_axis consistent with read_axis, which
340 // already returns None for these three axes under segmented wind. Without this, writing
341 // e.g. WindSpeed here would set req.wind.speed_mps alongside the still-present
342 // req.wind.segments, which solve_v1's resolve_wind rejects downstream as an opaque
343 // "segments cannot be combined with constant-wind fields" error instead of this specific,
344 // named one.
345 if matches!(
346 axis,
347 InputAxis::WindSpeed | InputAxis::WindDirection | InputAxis::WindVertical
348 ) && matches!(r.wind, ResolvedWindV1::Segmented(_))
349 {
350 return Err(KernelError::AxisAbsent(axis));
351 }
352
353 let mut req: SolveRequestV1 = r.into();
354 let scalar = |v: AxisValue| -> Result<f64, KernelError> {
355 match v {
356 AxisValue::Scalar(x) if x.is_finite() => Ok(x),
357 AxisValue::Scalar(_) => Err(KernelError::NonFinite(axis)),
358 _ => Err(KernelError::TypeMismatch(axis)),
359 }
360 };
361 let flag = |v: AxisValue| -> Result<bool, KernelError> {
362 match v {
363 AxisValue::Flag(b) => Ok(b),
364 _ => Err(KernelError::TypeMismatch(axis)),
365 }
366 };
367 let drag_model = |v: AxisValue| -> Result<DragModelV1, KernelError> {
368 match v {
369 AxisValue::DragModel(m) => Ok(m),
370 _ => Err(KernelError::TypeMismatch(axis)),
371 }
372 };
373 let twist_direction = |v: AxisValue| -> Result<TwistDirectionV1, KernelError> {
374 match v {
375 AxisValue::TwistDirection(d) => Ok(d),
376 _ => Err(KernelError::TypeMismatch(axis)),
377 }
378 };
379 use InputAxis::*;
380 match axis {
381 Mass => req.projectile.mass_kg = scalar(v)?,
382 Diameter => req.projectile.diameter_m = scalar(v)?,
383 Length => req.projectile.length_m = Some(scalar(v)?),
384 BallisticCoefficient => req.projectile.ballistic_coefficient = scalar(v)?,
385 TwistRate => req.rifle.twist_rate_m_per_turn = Some(scalar(v)?),
386 TwistDirection => req.rifle.twist_direction = Some(twist_direction(v)?),
387 DragModel => req.projectile.drag_model = drag_model(v)?,
388 MuzzleVelocityMps => req.rifle.muzzle_velocity_mps = scalar(v)?,
389 SightHeight => req.rifle.sight_height_m = Some(scalar(v)?),
390 ZeroDistance => req.shot.zero_distance_m = Some(scalar(v)?),
391 ZeroPoiUp => req.shot.zero_poi_up_m = Some(scalar(v)?),
392 ZeroPoiRight => req.shot.zero_poi_right_m = Some(scalar(v)?),
393 SightOffsetLateral => req.rifle.sight_offset_lateral_m = Some(scalar(v)?),
394 MuzzleHeight => req.rifle.muzzle_height_m = Some(scalar(v)?),
395 MuzzleAngle => req.shot.muzzle_angle_rad = Some(scalar(v)?),
396 Altitude => req.atmosphere.altitude_m = Some(scalar(v)?),
397 Temperature => req.atmosphere.temperature_k = Some(scalar(v)?),
398 Pressure => req.atmosphere.pressure_pa = Some(scalar(v)?),
399 RelativeHumidity => req.atmosphere.relative_humidity = Some(scalar(v)?),
400 Latitude => req.atmosphere.latitude_rad = Some(scalar(v)?),
401 WindSpeed => req.wind.speed_mps = Some(scalar(v)?),
402 WindDirection => req.wind.direction_from_rad = Some(scalar(v)?),
403 WindVertical => req.wind.vertical_speed_mps = Some(scalar(v)?),
404 TargetDistance => req.shot.max_range_m = scalar(v)?,
405 ShootingAngle => req.shot.shooting_angle_rad = Some(scalar(v)?),
406 Cant => req.shot.cant_angle_rad = Some(scalar(v)?),
407 ShotAzimuth => req.shot.shot_azimuth_rad = Some(scalar(v)?),
408 AimAzimuth => req.shot.aim_azimuth_rad = Some(scalar(v)?),
409 TargetHeight => req.shot.target_height_m = Some(scalar(v)?),
410 MagnusEnabled => req.effects.magnus = Some(flag(v)?),
411 CoriolisEnabled => req.effects.coriolis = Some(flag(v)?),
412 EnhancedSpinDriftEnabled => req.effects.enhanced_spin_drift = Some(flag(v)?),
413 }
414 // Changing a zero-affecting axis invalidates the stored effective angle: drop it so the
415 // service re-zeroes from zero_distance_m rather than reusing a stale angle. Checked AFTER
416 // the match (not before, which was a bug -- see the module doc): for the ZeroDistance axis
417 // itself, this must react to the NEW distance just written, not whatever zero_distance_m
418 // was on the original request.
419 if axis_meta(axis).requires_rezero && req.shot.zero_distance_m.is_some() {
420 req.shot.muzzle_angle_rad = None;
421 }
422 Ok(req)
423}
424
425#[cfg(test)]
426mod tests {
427 use super::*;
428 use crate::perturbation::InputAxis;
429
430 /// Non-default values for every axis this file's tests touch, INCLUDING the five
431 /// previously-omitted optional fields (`length_m`, `zero_poi_up_m`, `zero_poi_right_m`,
432 /// `sight_offset_lateral_m`, `latitude_rad`) so that all 32 taxonomy axes -- not just 27
433 /// of them -- read back `Some(..)` here. Without these five, `read_axis` returns `None`
434 /// for `Length`/`ZeroPoiUp`/`ZeroPoiRight`/`SightOffsetLateral`/`Latitude` on this fixture
435 /// and a naive per-axis loop (see `every_present_axis_round_trips_without_disturbing_any_other_field`
436 /// below) would silently skip exactly the pair -- `ZeroPoiUp`/`ZeroPoiRight` -- most likely
437 /// to be transposed by a future edit.
438 fn resolved() -> crate::solve_json::ResolvedSolveRequestV1 {
439 let json = serde_json::json!({
440 "schema_version": 1,
441 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
442 "ballistic_coefficient": 0.243, "length_m": 0.032},
443 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05,
444 "muzzle_height_m": 0.02, "twist_rate_m_per_turn": 0.2794,
445 "twist_direction": "left", "sight_offset_lateral_m": 0.03},
446 "shot": {"max_range_m": 900.0, "zero_distance_m": 100.0, "target_height_m": 0.3,
447 "zero_poi_up_m": 0.01, "zero_poi_right_m": 0.02},
448 "atmosphere": {"latitude_rad": 0.6},
449 "wind": {"speed_mps": 3.0, "direction_from_rad": std::f64::consts::FRAC_PI_2},
450 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
451 })
452 .to_string();
453 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
454 crate::solve_v1::solve_v1(req).unwrap().resolved_request
455 }
456
457 #[test]
458 fn read_then_write_is_identity() {
459 let r = resolved();
460 let v = read_axis(&r, InputAxis::MuzzleVelocityMps).expect("axis present");
461 let rebuilt = with_axis(&r, InputAxis::MuzzleVelocityMps, v).unwrap();
462 assert_eq!(
463 rebuilt.rifle.muzzle_velocity_mps,
464 r.rifle.muzzle_velocity_mps
465 );
466 }
467
468 #[test]
469 fn writing_an_axis_changes_only_that_axis() {
470 let r = resolved();
471 let changed =
472 with_axis(&r, InputAxis::MuzzleVelocityMps, AxisValue::Scalar(900.0)).unwrap();
473 let baseline: crate::solve_json::SolveRequestV1 = (&r).into();
474 assert_eq!(changed.rifle.muzzle_velocity_mps, 900.0);
475 assert_eq!(
476 changed.atmosphere.pressure_pa,
477 baseline.atmosphere.pressure_pa
478 );
479 assert_eq!(changed.wind.speed_mps, baseline.wind.speed_mps);
480 assert_eq!(changed.shot.max_range_m, baseline.shot.max_range_m);
481 // The five axes added after the brief was written must also be carried unchanged.
482 assert_eq!(
483 changed.projectile.drag_model,
484 baseline.projectile.drag_model
485 );
486 assert_eq!(
487 changed.rifle.twist_rate_m_per_turn,
488 baseline.rifle.twist_rate_m_per_turn
489 );
490 assert_eq!(
491 changed.rifle.twist_direction,
492 baseline.rifle.twist_direction
493 );
494 assert_eq!(
495 changed.rifle.muzzle_height_m,
496 baseline.rifle.muzzle_height_m
497 );
498 assert_eq!(changed.shot.target_height_m, baseline.shot.target_height_m);
499 }
500
501 #[test]
502 fn writing_a_flag_into_a_scalar_axis_is_a_type_error() {
503 let r = resolved();
504 let e = with_axis(&r, InputAxis::MuzzleVelocityMps, AxisValue::Flag(true));
505 assert!(matches!(e, Err(KernelError::TypeMismatch(_))));
506 }
507
508 #[test]
509 fn effect_flags_round_trip() {
510 let r = resolved();
511 let changed = with_axis(&r, InputAxis::CoriolisEnabled, AxisValue::Flag(true)).unwrap();
512 assert_eq!(changed.effects.coriolis, Some(true));
513 }
514
515 /// DragModel is a nine-way enum (G1/G2/G5/G6/G7/G8/GI/GS/RA4), not a bool or float:
516 /// AxisValue needs a dedicated variant to carry it losslessly (see the module doc).
517 #[test]
518 fn drag_model_axis_reads_and_writes_the_enum_value() {
519 let r = resolved();
520 let v = read_axis(&r, InputAxis::DragModel).expect("axis present");
521 assert_eq!(v, AxisValue::DragModel(DragModelV1::G7));
522
523 let changed = with_axis(
524 &r,
525 InputAxis::DragModel,
526 AxisValue::DragModel(DragModelV1::G1),
527 )
528 .unwrap();
529 assert_eq!(changed.projectile.drag_model, DragModelV1::G1);
530 // Nothing else moved.
531 assert_eq!(changed.projectile.mass_kg, r.projectile.mass_kg);
532 assert_eq!(
533 changed.projectile.ballistic_coefficient,
534 r.projectile.ballistic_coefficient
535 );
536 }
537
538 /// TwistDirection is a two-way enum (left/right); same rationale as DragModel above.
539 #[test]
540 fn twist_direction_axis_reads_and_writes_the_enum_value() {
541 let r = resolved();
542 let v = read_axis(&r, InputAxis::TwistDirection).expect("axis present");
543 assert_eq!(v, AxisValue::TwistDirection(TwistDirectionV1::Left));
544
545 let changed = with_axis(
546 &r,
547 InputAxis::TwistDirection,
548 AxisValue::TwistDirection(TwistDirectionV1::Right),
549 )
550 .unwrap();
551 assert_eq!(changed.rifle.twist_direction, Some(TwistDirectionV1::Right));
552 }
553
554 /// Extending AxisValue with enum variants must not weaken the existing type-checking:
555 /// a scalar is still rejected for an enum-valued axis.
556 #[test]
557 fn writing_a_scalar_into_the_drag_model_axis_is_a_type_error() {
558 let r = resolved();
559 let e = with_axis(&r, InputAxis::DragModel, AxisValue::Scalar(1.0));
560 assert!(matches!(
561 e,
562 Err(KernelError::TypeMismatch(InputAxis::DragModel))
563 ));
564 }
565
566 /// ...and the reverse: an enum value is rejected for a scalar axis.
567 #[test]
568 fn writing_a_drag_model_into_a_scalar_axis_is_a_type_error() {
569 let r = resolved();
570 let e = with_axis(&r, InputAxis::Mass, AxisValue::DragModel(DragModelV1::G1));
571 assert!(matches!(e, Err(KernelError::TypeMismatch(InputAxis::Mass))));
572 }
573
574 /// ...and the two enum-valued axes are not interchangeable with EACH OTHER either: both
575 /// are "enum-shaped" AxisValue variants now, so it's worth confirming DragModel's closure
576 /// rejects a TwistDirection value (and not just a Scalar/Flag) as a TypeMismatch.
577 #[test]
578 fn writing_a_twist_direction_into_the_drag_model_axis_is_a_type_error() {
579 let r = resolved();
580 let e = with_axis(
581 &r,
582 InputAxis::DragModel,
583 AxisValue::TwistDirection(TwistDirectionV1::Left),
584 );
585 assert!(matches!(
586 e,
587 Err(KernelError::TypeMismatch(InputAxis::DragModel))
588 ));
589 }
590
591 /// Read/write identity for three of the five axes added after the brief was written,
592 /// exercising each field mapping directly rather than only checking non-interference.
593 #[test]
594 fn twist_rate_and_muzzle_height_and_target_height_round_trip() {
595 let r = resolved();
596 for axis in [
597 InputAxis::TwistRate,
598 InputAxis::MuzzleHeight,
599 InputAxis::TargetHeight,
600 ] {
601 let v = read_axis(&r, axis).unwrap_or_else(|| panic!("{axis:?} should be present"));
602 let rebuilt = with_axis(&r, axis, v).unwrap();
603 match (axis, v) {
604 (InputAxis::TwistRate, AxisValue::Scalar(x)) => {
605 assert_eq!(rebuilt.rifle.twist_rate_m_per_turn, Some(x))
606 }
607 (InputAxis::MuzzleHeight, AxisValue::Scalar(x)) => {
608 assert_eq!(rebuilt.rifle.muzzle_height_m, Some(x))
609 }
610 (InputAxis::TargetHeight, AxisValue::Scalar(x)) => {
611 assert_eq!(rebuilt.shot.target_height_m, Some(x))
612 }
613 _ => panic!("expected a scalar for {axis:?}"),
614 }
615 }
616 }
617
618 /// read_axis returns None for the three wind axes under segmented wind: there is no
619 /// single scalar to perturb (taxonomy.rs Known Limitation (c)). This is intended -- a
620 /// caller must treat the None as "axis absent," never invent a uniform per-segment
621 /// perturbation. with_axis must AGREE with read_axis about this (I4): it must not
622 /// silently write a constant-wind field alongside the still-present wind.segments, which
623 /// solve_v1 would later reject downstream as an opaque, differently-worded error.
624 #[test]
625 fn wind_axes_are_absent_under_segmented_wind() {
626 let json = serde_json::json!({
627 "schema_version": 1,
628 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
629 "ballistic_coefficient": 0.243},
630 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
631 "shot": {"max_range_m": 900.0},
632 "atmosphere": {},
633 "wind": {"segments": [{"until_distance_m": 900.0, "speed_mps": 3.0,
634 "direction_from_rad": 1.0}]},
635 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
636 })
637 .to_string();
638 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
639 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
640 assert!(matches!(r.wind, ResolvedWindV1::Segmented(_)));
641
642 assert_eq!(read_axis(&r, InputAxis::WindSpeed), None);
643 assert_eq!(read_axis(&r, InputAxis::WindDirection), None);
644 assert_eq!(read_axis(&r, InputAxis::WindVertical), None);
645
646 for axis in [
647 InputAxis::WindSpeed,
648 InputAxis::WindDirection,
649 InputAxis::WindVertical,
650 ] {
651 let e = with_axis(&r, axis, AxisValue::Scalar(1.0));
652 assert!(
653 matches!(e, Err(KernelError::AxisAbsent(a)) if a == axis),
654 "{axis:?}: expected AxisAbsent, got {e:?}"
655 );
656 }
657 }
658
659 /// Regression test (I1): the rezero-clear used to be gated on the PRE-write
660 /// `zero_distance_m`, evaluated before the match instead of after. For a request
661 /// originally specified purely by `muzzle_angle_rad` (no `zero_distance_m` at all),
662 /// writing `ZeroDistance` would then see the ORIGINAL (absent) zero distance, decide there
663 /// was nothing to gate on, and leave the carried angle in place. The rebuilt request would
664 /// carry both a brand new `zero_distance_m` AND the stale `muzzle_angle_rad`, and
665 /// `solve_v1` always prefers an explicit angle over a zero distance when both are present
666 /// (see the module doc), so the elevation search would never run: the new zero distance
667 /// would have zero effect on elevation, and a sensitivity sweep over `ZeroDistance` would
668 /// silently report "no effect" instead of the truth.
669 #[test]
670 fn writing_zero_distance_onto_an_angle_only_request_clears_the_carried_angle() {
671 let json = serde_json::json!({
672 "schema_version": 1,
673 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
674 "ballistic_coefficient": 0.243},
675 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
676 "shot": {"max_range_m": 900.0, "muzzle_angle_rad": 0.01},
677 "atmosphere": {}, "wind": {},
678 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
679 })
680 .to_string();
681 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
682 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
683 // Sanity check on the fixture: this request has NO zero_distance_m at all, only a
684 // directly-supplied angle, so the bug's precondition actually holds.
685 assert_eq!(r.shot.zero_distance_m, None);
686 assert_eq!(r.shot.muzzle_angle_rad, 0.01);
687
688 let rebuilt = with_axis(&r, InputAxis::ZeroDistance, AxisValue::Scalar(100.0)).unwrap();
689 assert_eq!(rebuilt.shot.zero_distance_m, Some(100.0));
690 assert_eq!(
691 rebuilt.shot.muzzle_angle_rad, None,
692 "the carried angle must be cleared so solve_v1 actually re-zeroes at the new \
693 distance instead of skipping the elevation search because an explicit angle was \
694 still present"
695 );
696 }
697
698 /// I2, assertion 1 of 3 ("clears"): a `requires_rezero` axis clears the carried
699 /// `muzzle_angle_rad` when a `zero_distance_m` is present.
700 #[test]
701 fn rezero_axis_clears_the_carried_muzzle_angle_when_a_zero_distance_is_present() {
702 let r = resolved(); // zero_distance_m = Some(100.0) in this fixture.
703 assert!(r.shot.zero_distance_m.is_some());
704 assert!(
705 axis_meta(InputAxis::Mass).requires_rezero,
706 "fixture assumption: Mass must be a requires_rezero axis for this test to mean \
707 anything"
708 );
709
710 let changed = with_axis(&r, InputAxis::Mass, AxisValue::Scalar(0.02)).unwrap();
711 assert_eq!(changed.shot.muzzle_angle_rad, None);
712 }
713
714 /// I2, assertion 2 of 3 ("preserves"): a non-`requires_rezero` axis leaves the carried
715 /// `muzzle_angle_rad` untouched, even when a `zero_distance_m` is present. (Perturbing
716 /// `MuzzleVelocityMps` in `writing_an_axis_changes_only_that_axis` does NOT demonstrate
717 /// this -- that axis IS a rezero axis, so its angle is expected to change; that test never
718 /// asserts on `muzzle_angle_rad` at all.)
719 #[test]
720 fn non_rezero_axis_preserves_the_carried_muzzle_angle() {
721 let r = resolved(); // zero_distance_m = Some(100.0) in this fixture.
722 assert!(r.shot.zero_distance_m.is_some());
723 assert!(
724 !axis_meta(InputAxis::WindSpeed).requires_rezero,
725 "fixture assumption: WindSpeed must NOT be a requires_rezero axis for this test to \
726 mean anything"
727 );
728
729 let changed = with_axis(&r, InputAxis::WindSpeed, AxisValue::Scalar(5.0)).unwrap();
730 assert_eq!(changed.shot.muzzle_angle_rad, Some(r.shot.muzzle_angle_rad));
731 }
732
733 /// I2, assertion 3 of 3 ("the gate"): a `requires_rezero` axis does NOT clear the carried
734 /// angle when there is no `zero_distance_m` to begin with -- the clearing is gated on
735 /// `zero_distance_m` being present, not on `requires_rezero` alone.
736 #[test]
737 fn rezero_axis_does_not_clear_the_angle_when_no_zero_distance_is_present() {
738 let json = serde_json::json!({
739 "schema_version": 1,
740 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
741 "ballistic_coefficient": 0.243},
742 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
743 "shot": {"max_range_m": 900.0, "muzzle_angle_rad": 0.01},
744 "atmosphere": {}, "wind": {},
745 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
746 })
747 .to_string();
748 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
749 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
750 assert_eq!(r.shot.zero_distance_m, None);
751 assert!(axis_meta(InputAxis::Mass).requires_rezero);
752
753 let changed = with_axis(&r, InputAxis::Mass, AxisValue::Scalar(0.02)).unwrap();
754 assert_eq!(changed.shot.muzzle_angle_rad, Some(r.shot.muzzle_angle_rad));
755 }
756
757 /// Guards the "changes only that axis" property: for each axis present on the fixture,
758 /// read its current value and write that SAME value back. Since nothing numerically
759 /// changes, the rebuilt request must be identical to `SolveRequestV1::from(&r)` in EVERY
760 /// field except `shot.muzzle_angle_rad`, which a `requires_rezero` axis deliberately
761 /// clears whenever `zero_distance_m` is present (see the module doc).
762 ///
763 /// CORRECTNESS CAVEAT (found by review, do not re-widen this claim): this loop is
764 /// structurally BLIND to a *self-consistent* transposition, where `read_axis` and
765 /// `with_axis` are both wrong about the same axis in the same way -- e.g. both mapping
766 /// `ZeroPoiUp` to `zero_poi_right_m`. In that case `read_axis(ZeroPoiUp)` returns
767 /// `zero_poi_right_m`'s current value, `with_axis` writes that SAME value back onto that
768 /// SAME (wrong) field, and the result is a byte-identical no-op: there is nothing for a
769 /// whole-struct diff to see. The same blindness covers a two-axis alias (e.g. `AimAzimuth`
770 /// reading AND writing `shot_azimuth_rad`). This loop therefore does NOT prove the mapping
771 /// is correct, only that whichever field each axis actually touches, it touches only that
772 /// one. `every_axis_writes_to_its_own_named_destination_field` below is what actually
773 /// pins each axis to its correct field, via a hardcoded per-axis destination that is
774 /// independent of (and so cannot silently agree with) a bug in `read_axis`.
775 #[test]
776 fn every_present_axis_round_trips_without_disturbing_any_other_field() {
777 let r = resolved();
778 let baseline: crate::solve_json::SolveRequestV1 = (&r).into();
779 // In THIS fixture zero_distance_m is present, so "will the angle be cleared" reduces
780 // to "is this a requires_rezero axis" -- computed once, outside the loop, from the
781 // same (pre-write) state with_axis itself will see for every axis except
782 // ZeroDistance, whose own write cannot change whether zero_distance_m.is_some() (it
783 // was already Some, and it is written back Some).
784 assert!(r.shot.zero_distance_m.is_some());
785
786 let mut exercised = 0usize;
787 for &axis in InputAxis::ALL {
788 let Some(v) = read_axis(&r, axis) else {
789 continue;
790 };
791 exercised += 1;
792 let rebuilt = with_axis(&r, axis, v).unwrap();
793
794 let mut expected = baseline.clone();
795 if axis_meta(axis).requires_rezero {
796 expected.shot.muzzle_angle_rad = None;
797 }
798 assert_eq!(
799 rebuilt, expected,
800 "writing {axis:?} back onto its own current value changed a field other than \
801 itself (and, for a rezero axis, the carried angle)"
802 );
803 }
804 // Prerequisite the fixture must satisfy for the loop above to mean anything: every one
805 // of the 32 taxonomy axes must actually be present (Some) on this fixture, or the loop
806 // would silently skip whichever axes read back None -- exactly how a naive version of
807 // this fixture would have skipped the ZeroPoiUp/ZeroPoiRight pair before it was
808 // enriched with the five previously-omitted optional fields (see `resolved`'s doc).
809 assert_eq!(
810 exercised,
811 InputAxis::ALL.len(),
812 "fixture does not make every axis readable -- this test is silently under-covering"
813 );
814 }
815
816 /// The actual mapping check (I3, corrected): for every one of the 32 axes, write a
817 /// SENTINEL value distinct from whatever that axis's own field currently holds, then
818 /// assert -- via a hardcoded, explicitly-named `match axis { .. }`, the same pattern
819 /// `twist_rate_and_muzzle_height_and_target_height_round_trip` already uses for three
820 /// axes -- that the sentinel landed in that axis's OWN specific destination field.
821 ///
822 /// This is a second, independent statement of the axis -> field mapping, written directly
823 /// against the real `SolveRequestV1` field paths rather than derived from `read_axis`. That
824 /// independence is the whole point: `every_present_axis_round_trips_without_disturbing_any_other_field`
825 /// above reads a value FROM `read_axis` and writes it back, so if `read_axis` and
826 /// `with_axis` are both wrong about the same axis in the same way, that loop's read and
827 /// write agree with each other and the bug is invisible. This test never calls `read_axis`
828 /// at all: the expected destination for every axis is spelled out here by hand, so it
829 /// cannot silently agree with a mistake made in `access.rs`'s implementation.
830 ///
831 /// Distinctness matters: a sentinel MUST differ from the field's pre-write value, or a
832 /// transposition that leaves the correct field untouched (because it wrote the sentinel to
833 /// the WRONG field instead) would go unnoticed -- the untouched field would coincidentally
834 /// already equal the "expected" value. `MuzzleAngle`/`Temperature`/`Pressure` add a fixed
835 /// delta to whatever the fixture's resolved value happens to be (avoiding a dependency on
836 /// the exact auto-zeroed angle or ICAO default) instead of a hardcoded literal.
837 #[test]
838 fn every_axis_writes_to_its_own_named_destination_field() {
839 let r = resolved();
840 use InputAxis::*;
841
842 for &axis in InputAxis::ALL {
843 match axis {
844 Mass => {
845 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.0199)).unwrap();
846 assert_eq!(rebuilt.projectile.mass_kg, 0.0199, "{axis:?}");
847 }
848 Diameter => {
849 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.0090)).unwrap();
850 assert_eq!(rebuilt.projectile.diameter_m, 0.0090, "{axis:?}");
851 }
852 Length => {
853 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.040)).unwrap();
854 assert_eq!(rebuilt.projectile.length_m, Some(0.040), "{axis:?}");
855 }
856 BallisticCoefficient => {
857 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.300)).unwrap();
858 assert_eq!(rebuilt.projectile.ballistic_coefficient, 0.300, "{axis:?}");
859 }
860 TwistRate => {
861 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.3556)).unwrap();
862 assert_eq!(
863 rebuilt.rifle.twist_rate_m_per_turn,
864 Some(0.3556),
865 "{axis:?}"
866 );
867 }
868 TwistDirection => {
869 let rebuilt =
870 with_axis(&r, axis, AxisValue::TwistDirection(TwistDirectionV1::Right))
871 .unwrap();
872 assert_eq!(
873 rebuilt.rifle.twist_direction,
874 Some(TwistDirectionV1::Right),
875 "{axis:?}"
876 );
877 }
878 DragModel => {
879 let rebuilt =
880 with_axis(&r, axis, AxisValue::DragModel(DragModelV1::G1)).unwrap();
881 assert_eq!(rebuilt.projectile.drag_model, DragModelV1::G1, "{axis:?}");
882 }
883 MuzzleVelocityMps => {
884 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(900.0)).unwrap();
885 assert_eq!(rebuilt.rifle.muzzle_velocity_mps, 900.0, "{axis:?}");
886 }
887 SightHeight => {
888 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.06)).unwrap();
889 assert_eq!(rebuilt.rifle.sight_height_m, Some(0.06), "{axis:?}");
890 }
891 ZeroDistance => {
892 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(200.0)).unwrap();
893 assert_eq!(rebuilt.shot.zero_distance_m, Some(200.0), "{axis:?}");
894 }
895 ZeroPoiUp => {
896 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.05)).unwrap();
897 assert_eq!(rebuilt.shot.zero_poi_up_m, Some(0.05), "{axis:?}");
898 }
899 ZeroPoiRight => {
900 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.06)).unwrap();
901 assert_eq!(rebuilt.shot.zero_poi_right_m, Some(0.06), "{axis:?}");
902 }
903 SightOffsetLateral => {
904 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.10)).unwrap();
905 assert_eq!(rebuilt.rifle.sight_offset_lateral_m, Some(0.10), "{axis:?}");
906 }
907 MuzzleHeight => {
908 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.05)).unwrap();
909 assert_eq!(rebuilt.rifle.muzzle_height_m, Some(0.05), "{axis:?}");
910 }
911 MuzzleAngle => {
912 let sentinel = r.shot.muzzle_angle_rad + 0.01;
913 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(sentinel)).unwrap();
914 assert_eq!(rebuilt.shot.muzzle_angle_rad, Some(sentinel), "{axis:?}");
915 }
916 Altitude => {
917 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(1500.0)).unwrap();
918 assert_eq!(rebuilt.atmosphere.altitude_m, Some(1500.0), "{axis:?}");
919 }
920 Temperature => {
921 let sentinel = r.atmosphere.temperature_k + 5.0;
922 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(sentinel)).unwrap();
923 assert_eq!(rebuilt.atmosphere.temperature_k, Some(sentinel), "{axis:?}");
924 }
925 Pressure => {
926 let sentinel = r.atmosphere.pressure_pa + 500.0;
927 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(sentinel)).unwrap();
928 assert_eq!(rebuilt.atmosphere.pressure_pa, Some(sentinel), "{axis:?}");
929 }
930 RelativeHumidity => {
931 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.7)).unwrap();
932 assert_eq!(rebuilt.atmosphere.relative_humidity, Some(0.7), "{axis:?}");
933 }
934 Latitude => {
935 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.3)).unwrap();
936 assert_eq!(rebuilt.atmosphere.latitude_rad, Some(0.3), "{axis:?}");
937 }
938 WindSpeed => {
939 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(7.0)).unwrap();
940 assert_eq!(rebuilt.wind.speed_mps, Some(7.0), "{axis:?}");
941 }
942 WindDirection => {
943 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.4)).unwrap();
944 assert_eq!(rebuilt.wind.direction_from_rad, Some(0.4), "{axis:?}");
945 }
946 WindVertical => {
947 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(1.5)).unwrap();
948 assert_eq!(rebuilt.wind.vertical_speed_mps, Some(1.5), "{axis:?}");
949 }
950 TargetDistance => {
951 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(1000.0)).unwrap();
952 assert_eq!(rebuilt.shot.max_range_m, 1000.0, "{axis:?}");
953 }
954 ShootingAngle => {
955 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.2)).unwrap();
956 assert_eq!(rebuilt.shot.shooting_angle_rad, Some(0.2), "{axis:?}");
957 }
958 Cant => {
959 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.15)).unwrap();
960 assert_eq!(rebuilt.shot.cant_angle_rad, Some(0.15), "{axis:?}");
961 }
962 ShotAzimuth => {
963 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.25)).unwrap();
964 assert_eq!(rebuilt.shot.shot_azimuth_rad, Some(0.25), "{axis:?}");
965 }
966 AimAzimuth => {
967 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.35)).unwrap();
968 assert_eq!(rebuilt.shot.aim_azimuth_rad, Some(0.35), "{axis:?}");
969 }
970 TargetHeight => {
971 let rebuilt = with_axis(&r, axis, AxisValue::Scalar(0.5)).unwrap();
972 assert_eq!(rebuilt.shot.target_height_m, Some(0.5), "{axis:?}");
973 }
974 MagnusEnabled => {
975 let rebuilt = with_axis(&r, axis, AxisValue::Flag(true)).unwrap();
976 assert_eq!(rebuilt.effects.magnus, Some(true), "{axis:?}");
977 }
978 CoriolisEnabled => {
979 let rebuilt = with_axis(&r, axis, AxisValue::Flag(true)).unwrap();
980 assert_eq!(rebuilt.effects.coriolis, Some(true), "{axis:?}");
981 }
982 EnhancedSpinDriftEnabled => {
983 let rebuilt = with_axis(&r, axis, AxisValue::Flag(true)).unwrap();
984 assert_eq!(rebuilt.effects.enhanced_spin_drift, Some(true), "{axis:?}");
985 }
986 }
987 }
988 }
989
990 /// Physics guard (a): perturbing altitude on a QNH-pressure request would silently build
991 /// a wrong counterfactual (the rebuilt request always carries absolute station pressure --
992 /// see request_roundtrip.rs -- so density-by-altitude would move but the QNH-referenced
993 /// station pressure would not, backwards from what a QNH-entering caller means). Detected
994 /// from the ORIGINAL resolved request's pressure_reference echo and rejected outright.
995 #[test]
996 fn altitude_axis_is_unsupported_when_the_original_pressure_was_qnh() {
997 let json = serde_json::json!({
998 "schema_version": 1,
999 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
1000 "ballistic_coefficient": 0.243},
1001 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
1002 "shot": {"max_range_m": 900.0},
1003 "atmosphere": {"altitude_m": 500.0, "temperature_k": 288.0, "pressure_pa": 101325.0,
1004 "pressure_reference": "qnh"},
1005 "wind": {}, "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
1006 })
1007 .to_string();
1008 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
1009 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
1010 assert_eq!(
1011 r.atmosphere.pressure_reference,
1012 Some(PressureReferenceV1::Qnh)
1013 );
1014
1015 let e = with_axis(&r, InputAxis::Altitude, AxisValue::Scalar(600.0));
1016 match e {
1017 Err(KernelError::AxisUnsupportedForRequest {
1018 axis: InputAxis::Altitude,
1019 reason,
1020 }) => {
1021 assert!(
1022 reason.to_lowercase().contains("qnh"),
1023 "reason should name QNH: {reason}"
1024 );
1025 }
1026 other => panic!("expected AxisUnsupportedForRequest, got {other:?}"),
1027 }
1028 }
1029
1030 /// The QNH guard must be specific to QNH mode: an OMITTED pressure_reference (the default,
1031 /// and every request that predates MBA-1397) must still allow perturbing altitude
1032 /// normally. This only proves the guard doesn't fire on absence; see
1033 /// `altitude_axis_is_perturbable_when_pressure_is_explicitly_absolute` below for the
1034 /// stronger claim that it doesn't fire on an explicit non-QNH value either.
1035 #[test]
1036 fn altitude_axis_is_perturbable_when_pressure_is_absolute() {
1037 let r = resolved();
1038 assert_eq!(r.atmosphere.pressure_reference, None);
1039 let changed = with_axis(&r, InputAxis::Altitude, AxisValue::Scalar(1200.0)).unwrap();
1040 assert_eq!(changed.atmosphere.altitude_m, Some(1200.0));
1041 }
1042
1043 /// Stronger version of the test above: an EXPLICIT `pressure_reference: "absolute"` (not
1044 /// just an omitted field defaulting to it) must still allow perturbing altitude normally.
1045 /// Guards against a hypothetical `!= Some(Qnh)` -> `== None` typo that happens to pass the
1046 /// omitted-field test above but would reject this explicit-but-equivalent case.
1047 #[test]
1048 fn altitude_axis_is_perturbable_when_pressure_is_explicitly_absolute() {
1049 let json = serde_json::json!({
1050 "schema_version": 1,
1051 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
1052 "ballistic_coefficient": 0.243},
1053 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
1054 "shot": {"max_range_m": 900.0},
1055 "atmosphere": {"altitude_m": 500.0, "temperature_k": 288.0, "pressure_pa": 101325.0,
1056 "pressure_reference": "absolute"},
1057 "wind": {}, "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
1058 })
1059 .to_string();
1060 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
1061 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
1062 assert_eq!(
1063 r.atmosphere.pressure_reference,
1064 Some(PressureReferenceV1::Absolute)
1065 );
1066
1067 let changed = with_axis(&r, InputAxis::Altitude, AxisValue::Scalar(600.0)).unwrap();
1068 assert_eq!(changed.atmosphere.altitude_m, Some(600.0));
1069 }
1070
1071 /// Physics guard (b): perturbing shot azimuth on a compass-referenced-wind request would
1072 /// silently build a wrong counterfactual (the rebuilt request always carries
1073 /// shooter-relative wind -- see request_roundtrip.rs -- so the wind would rotate WITH the
1074 /// rifle instead of staying earth-fixed). Detected from the ORIGINAL resolved request's
1075 /// wind_reference echo and rejected outright.
1076 #[test]
1077 fn shot_azimuth_axis_is_unsupported_when_the_original_wind_was_compass_referenced() {
1078 let json = serde_json::json!({
1079 "schema_version": 1,
1080 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
1081 "ballistic_coefficient": 0.243},
1082 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
1083 "shot": {"max_range_m": 900.0, "shot_azimuth_rad": 0.3},
1084 "atmosphere": {},
1085 "wind": {"speed_mps": 3.0, "direction_from_rad": 1.0, "wind_reference": "compass"},
1086 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
1087 })
1088 .to_string();
1089 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
1090 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
1091
1092 let e = with_axis(&r, InputAxis::ShotAzimuth, AxisValue::Scalar(0.5));
1093 match e {
1094 Err(KernelError::AxisUnsupportedForRequest {
1095 axis: InputAxis::ShotAzimuth,
1096 reason,
1097 }) => {
1098 assert!(
1099 reason.to_lowercase().contains("compass"),
1100 "reason should name compass wind: {reason}"
1101 );
1102 }
1103 other => panic!("expected AxisUnsupportedForRequest, got {other:?}"),
1104 }
1105 }
1106
1107 /// The compass guard must be specific to compass mode: an OMITTED wind_reference (the
1108 /// default) must still allow perturbing shot azimuth normally. This only proves the guard
1109 /// doesn't fire on absence; see
1110 /// `shot_azimuth_axis_is_perturbable_when_wind_is_explicitly_shooter_relative` below for
1111 /// the stronger claim that it doesn't fire on an explicit non-compass value either.
1112 #[test]
1113 fn shot_azimuth_axis_is_perturbable_when_wind_is_shooter_relative() {
1114 let r = resolved();
1115 assert_eq!(
1116 match &r.wind {
1117 ResolvedWindV1::Constant(c) => c.wind_reference,
1118 ResolvedWindV1::Segmented(_) => panic!("constant wind expected"),
1119 },
1120 None
1121 );
1122 let changed = with_axis(&r, InputAxis::ShotAzimuth, AxisValue::Scalar(0.5)).unwrap();
1123 assert_eq!(changed.shot.shot_azimuth_rad, Some(0.5));
1124 }
1125
1126 /// Stronger version of the test above: an EXPLICIT `wind_reference: "shooter"` (not just
1127 /// an omitted field defaulting to it) must still allow perturbing shot azimuth normally.
1128 /// Guards against a hypothetical `!= Some(Compass)` -> `== None` typo that happens to pass
1129 /// the omitted-field test above but would reject this explicit-but-equivalent case.
1130 #[test]
1131 fn shot_azimuth_axis_is_perturbable_when_wind_is_explicitly_shooter_relative() {
1132 let json = serde_json::json!({
1133 "schema_version": 1,
1134 "projectile": {"mass_kg": 0.0113, "diameter_m": 0.00782, "drag_model": "G7",
1135 "ballistic_coefficient": 0.243},
1136 "rifle": {"muzzle_velocity_mps": 823.0, "sight_height_m": 0.05},
1137 "shot": {"max_range_m": 900.0, "shot_azimuth_rad": 0.3},
1138 "atmosphere": {},
1139 "wind": {"speed_mps": 3.0, "direction_from_rad": 1.0, "wind_reference": "shooter"},
1140 "solver": {}, "effects": {}, "sampling": {"interval_m": 50.0}
1141 })
1142 .to_string();
1143 let req = crate::solve_json::decode_solve_request_v1(&json).unwrap();
1144 let r = crate::solve_v1::solve_v1(req).unwrap().resolved_request;
1145 let ResolvedWindV1::Constant(wind) = &r.wind else {
1146 panic!("constant wind expected");
1147 };
1148 assert_eq!(wind.wind_reference, Some(WindReferenceV1::Shooter));
1149
1150 let changed = with_axis(&r, InputAxis::ShotAzimuth, AxisValue::Scalar(0.5)).unwrap();
1151 assert_eq!(changed.shot.shot_azimuth_rad, Some(0.5));
1152 }
1153}