Skip to main content

sidereon_core/spp/
mod.rs

1//! Single-point positioning (SPP).
2//!
3//! Recovers a receiver ECEF position and clock bias from a set of pseudoranges,
4//! a satellite ephemeris source (a precise SP3 product or a broadcast navigation
5//! message, via the [`EphemerisSource`] trait), and broadcast ionosphere /
6//! Saastamoinen-Niell troposphere correction models. GPS L1 C/A, Galileo E1,
7//! BeiDou B1I, and GLONASS G1 are supported; GPS, BeiDou, and GLONASS use
8//! broadcast Klobuchar coefficients with carrier-frequency scaling, while
9//! Galileo can use its broadcast NeQuick-G `ai0`/`ai1`/`ai2` coefficients when
10//! supplied. GLONASS is FDMA, so its per-satellite carrier is resolved from the
11//! broadcast/observation channel number ([`SolveInputs::glonass_channels`]) and
12//! the Klobuchar L1 delay is scaled to it by `(f_L1 / f_k)^2`, matching
13//! RTKLIB-demo5, which applies no per-satellite inter-frequency bias and carries
14//! the single GLO-GPS offset on the per-system receiver clock. A satellite whose
15//! carrier cannot be resolved is rejected when the ionosphere correction is
16//! requested.
17//!
18//! The state vector is `[x_m, y_m, z_m, clk_0, clk_1, ...]`: three ECEF position
19//! components (meters) followed by one receiver clock per distinct GNSS in the
20//! solve, expressed as a length (meters). A single-system solve reduces to the
21//! classic `[x_m, y_m, z_m, b_m]`; a multi-system solve adds an inter-system
22//! bias parameter for each additional constellation. The seconds value
23//! `rx_clock_s = clk_0 / c` (the reference system) and the per-system clocks are
24//! reported only at the API boundary.
25//!
26//! The per-satellite predicted pseudorange is built in a pinned operation order:
27//! a fixed-count transmit-time iteration (receive time minus geometric range
28//! over `c`) locates the satellite ephemeris at transmission, an Earth-rotation
29//! (Sagnac) closed-form rotation brings the satellite into the receive-time
30//! frame, the geometric range and the line-of-sight azimuth/elevation follow,
31//! then the ionosphere and troposphere delays are added to the predicted range
32//! left-to-right. The residual the solver sees is `sqrt(w) * (P_meas - P_hat)`
33//! with an elevation-based weight evaluated once at the frozen initial-guess
34//! geometry.
35//!
36//! The geometric/clock/correction substrate and its 2-point finite-difference
37//! Jacobian are arithmetic over the libm-bound model functions and are a
38//! bit-exact (0-ULP) parity target against the reference recipe. The converged
39//! position is produced by the trust-region least-squares solver in the
40//! `sidereon-core` solver core, whose linear-algebra step is not bit-reproducible
41//! across BLAS builds; the converged solution is therefore a sub-micron
42//! solver-agreement result, not a 0-ULP claim.
43//!
44//! The bit-exact claim depends on the fused-multiply-add policy matching the
45//! reference exactly. The substrate uses no contracted `a*b+c` anywhere the
46//! reference computes the two roundings separately; the single deliberate
47//! exception is the 3x3-by-vector rotation primitive, which uses `mul_add` to
48//! reproduce the reference's rounding of that product. The certified target
49//! pins `target-cpu`/features so the compiler neither introduces nor drops a
50//! contraction; on a host that auto-contracts these expressions the last bit
51//! can differ and the goldens are not expected to hold.
52
53use crate::astro::angles::rad_to_deg_ref;
54use crate::astro::math::least_squares::{
55    self, solve_trf_with, LeastSquaresProblem, SolveOptions, Status, TrustRegionSolve,
56};
57use nalgebra::DVector;
58use std::collections::BTreeMap;
59
60mod config;
61mod fallback;
62mod source;
63use crate::astro::math::robust::{huber_weight, mad_scale, RobustError};
64pub use config::{
65    DEFAULT_HUBER_K, DEFAULT_ROBUST_MAX_OUTER, DEFAULT_ROBUST_OUTER_TOL_M,
66    DEFAULT_ROBUST_SCALE_FLOOR_M, ELEVATION_MASK_RAD, SIGMA0_M, TRANSMIT_TIME_ITERATIONS,
67};
68pub use fallback::{
69    solve_broadcast, solve_with_fallback, BroadcastReason, FallbackError, FixSource,
70    SourcedSolution,
71};
72pub use source::EphemerisSource;
73
74pub use crate::constants::{C_M_S, F_L1_HZ, OMEGA_E_DOT_RAD_S};
75use crate::dop::{dop, dop_multi, Dop, LineOfSight};
76use crate::estimation::recipe::{
77    EstimationRecipe, FrameRecipe, RangeRecipe, SagnacRecipe, SolverRecipe,
78};
79use crate::estimation::substrate::frames::{az_el_from_ecef, geodetic_from_ecef};
80use crate::estimation::substrate::parameters::ParameterLayout;
81use crate::estimation::substrate::range::{geometric_range, rotate_transmit_satellite};
82use crate::frame::{ItrfPositionM, Wgs84Geodetic};
83use crate::frequencies;
84use crate::id::{GnssSatelliteId, GnssSystem};
85pub use crate::ionex::GalileoNequickCoeffs;
86use crate::ionex::{
87    galileo_nequick_g_native_unchecked, klobuchar_native_unchecked, GalileoNequickEval,
88    KlobucharParams,
89};
90use crate::quality::{
91    validate_receiver_solution, SolutionValidationError, SolutionValidationOptions,
92};
93use crate::sbas::SbasIonoGrid;
94use crate::tropo::slant_components;
95use crate::validate;
96
97/// The single-frequency carrier (Hz) the ionosphere correction is reported on
98/// for a constellation with one fixed single-frequency carrier, or `None` for a
99/// system that has none (GLONASS, whose FDMA carrier is per-satellite). GPS L1
100/// C/A and Galileo E1 are both at [`F_L1_HZ`]; BeiDou uses B1I. Klobuchar and
101/// Galileo broadcast delays are reported on this carrier. GLONASS is resolved
102/// per satellite by [`spp_iono_frequency_hz`] from its FDMA channel instead.
103pub(crate) const fn carrier_frequency_hz(system: GnssSystem) -> Option<f64> {
104    match system {
105        GnssSystem::Sbas => Some(F_L1_HZ),
106        _ => frequencies::default_spp_frequency_hz(system),
107    }
108}
109
110/// The carrier frequency (Hz) the broadcast ionosphere delay is scaled to for a
111/// single satellite, or `None` if the satellite's system has no carrier the
112/// model can resolve.
113///
114/// For the fixed-carrier systems (GPS L1, Galileo E1, BeiDou B1I) this is the
115/// system carrier from [`carrier_frequency_hz`]. GLONASS is FDMA, so its carrier
116/// is per-satellite: it is resolved from `glonass_channels` (the broadcast /
117/// observation FDMA channel `k` keyed by GLONASS slot number) as the G1
118/// frequency `1602.0 MHz + k * 562.5 kHz`. A GLONASS satellite whose channel is
119/// not in the map, or whose channel is outside the valid FDMA range
120/// `[-7, +6]` (the same domain the RINEX nav/obs parsers enforce via
121/// [`crate::rinex_nav::valid_glonass_frequency_channel`]), has no resolvable
122/// carrier and returns `None` -- `glonass_g1_frequency_hz` is a pure
123/// `1602.0 MHz + k * 562.5 kHz` evaluation that would otherwise return a
124/// bogus-but-positive carrier for an out-of-domain `k`. Mirroring RTKLIB-demo5,
125/// the single GLO-GPS inter-system offset is carried by the existing per-system
126/// receiver clock (see [`clock_systems`]) rather than a separate
127/// inter-frequency-bias parameter, and the only GLONASS-specific term in the
128/// measurement model is this per-satellite `(f_L1 / f_k)^2` ionosphere scaling.
129pub(crate) fn spp_iono_frequency_hz(
130    sat: GnssSatelliteId,
131    glonass_channels: &BTreeMap<u8, i8>,
132) -> Option<f64> {
133    match sat.system {
134        GnssSystem::Glonass => glonass_channels
135            .get(&sat.prn)
136            .copied()
137            .filter(|&k| crate::rinex_nav::valid_glonass_frequency_channel(i32::from(k)))
138            .map(frequencies::glonass_g1_frequency_hz),
139        _ => carrier_frequency_hz(sat.system),
140    }
141}
142use crate::constants::MEAN_EARTH_RADIUS_M;
143const PI: f64 = std::f64::consts::PI;
144
145// Agreement-track stopping thresholds for the independent SPP least-squares
146// solver. These drive the solver to the true fixed point of the noise-free,
147// by-construction-zero-residual problem so the converged position agrees with
148// the reference solution to the documented sub-micron bound; they are the
149// solver's own stopping thresholds, not a parity target's pinned scipy options.
150/// Canonical light-time convergence tolerance (s). The canonical range recipe
151/// ([`RangeRecipe::CanonicalLightTimeClosedFormSagnac`]) iterates the
152/// transmit-epoch light-time loop until the signal travel time changes by less
153/// than this between iterations, instead of the reference recipe's fixed
154/// [`TRANSMIT_TIME_ITERATIONS`] truncation. `1e-13 s` is ~30 microns of range
155/// (`tol * C_M_S`), far below the pseudorange noise floor; the loop is
156/// quadratically convergent so it reaches this in ~3 iterations.
157const CANONICAL_LIGHT_TIME_TOL_S: f64 = 1.0e-13;
158/// Iteration cap for the canonical light-time loop, a safety bound the
159/// quadratically convergent iteration never reaches in practice (it converges in
160/// ~3 iterations); present so a pathological geometry cannot spin forever.
161const CANONICAL_LIGHT_TIME_MAX_ITERS: usize = 10;
162/// First-order optimality tolerance on `||J^T r||_inf`.
163const SPP_SOLVER_GTOL: f64 = 1e-14;
164/// Relative-cost-reduction tolerance.
165const SPP_SOLVER_FTOL: f64 = 1e-15;
166/// Relative-step tolerance.
167const SPP_SOLVER_XTOL: f64 = 1e-14;
168/// Maximum number of residual evaluations.
169const SPP_SOLVER_MAX_NFEV: usize = 400;
170
171/// A single GPS L1 pseudorange observation.
172///
173/// The input boundary of the pipeline is the pseudorange; raw observation
174/// formation (RINEX decoding, code tracking) is out of scope. The receive epoch
175/// and the time-of-day / day-of-year arguments are common to all observations
176/// in one solve and are carried on [`SolveInputs`], not here.
177#[derive(Debug, Clone, Copy, PartialEq)]
178pub struct Observation {
179    /// The transmitting satellite.
180    pub satellite_id: GnssSatelliteId,
181    /// Measured pseudorange in meters.
182    pub pseudorange_m: f64,
183}
184
185/// Why a satellite was excluded from the solve, in pinned priority order.
186#[derive(Debug, Clone, Copy, PartialEq, Eq)]
187pub enum RejectionReason {
188    /// The SP3 product has no usable position or clock for the satellite at the
189    /// transmit epoch.
190    NoEphemeris,
191    /// The satellite is below the elevation mask at the frozen geometry.
192    LowElevation,
193    /// The bound augmentation source withdrew the satellite.
194    SbasWithdrawn,
195    /// The augmentation ionosphere grid does not cover the satellite line of sight.
196    SbasIonoUncovered,
197}
198
199/// A rejected satellite paired with its rejection reason.
200#[derive(Debug, Clone, Copy, PartialEq, Eq)]
201pub struct RejectedSat {
202    /// The excluded satellite.
203    pub satellite_id: GnssSatelliteId,
204    /// The first matching rejection reason.
205    pub reason: RejectionReason,
206}
207
208/// Models and convergence detail describing how a solution was produced.
209#[derive(Debug, Clone, PartialEq)]
210pub struct SolutionMetadata {
211    /// Number of accepted solver iterations.
212    pub iterations: usize,
213    /// Whether the solver reached a convergence stopping criterion (as opposed
214    /// to exhausting its evaluation budget).
215    pub converged: bool,
216    /// The solver's termination status.
217    pub status: Status,
218    /// Whether the ionosphere correction was applied.
219    pub ionosphere_applied: bool,
220    /// Whether the troposphere correction was applied.
221    pub troposphere_applied: bool,
222    /// Number of outer robust-reweighting iterations performed. `0` on the
223    /// static path (`robust = None`); on the robust path this counts the
224    /// reweighted resolves beyond the warm-start solve.
225    pub outer_iterations: usize,
226    /// The final MAD robust scale (m) of the last outer iteration, or `None` on
227    /// the static path.
228    pub final_robust_scale_m: Option<f64>,
229    /// Number of satellites used in the final solve.
230    pub used_count: usize,
231    /// Distinct GNSS systems present in the final solve, in ascending order.
232    pub systems: Vec<GnssSystem>,
233    /// Degrees of freedom, `used_count - (3 + systems.len())`.
234    pub redundancy: isize,
235    /// Whether residual-based RAIM can test the final solve (`redundancy >= 1`).
236    pub raim_checkable: bool,
237}
238
239/// A receiver position/clock solution with its geometry diagnostics.
240#[derive(Debug, Clone)]
241pub struct ReceiverSolution {
242    /// Converged receiver position, ITRF/IGS ECEF meters.
243    pub position: ItrfPositionM,
244    /// The geodetic form of the position, if the conversion was requested.
245    pub geodetic: Option<Wgs84Geodetic>,
246    /// Receiver clock bias in seconds (`clk_0 / c`) for the reference GNSS - the
247    /// first entry of `system_clocks_s`. For a single-system solve this is the
248    /// only clock; for a multi-system solve the other systems' absolute clocks
249    /// are in `system_clocks_s`.
250    pub rx_clock_s: f64,
251    /// The absolute receiver clock for each GNSS in the solve, in ascending
252    /// system order, in seconds. One entry for a single-system solve; one per
253    /// constellation for a multi-system solve. The first entry equals
254    /// `rx_clock_s`; the inter-system bias for any other system is *its clock
255    /// minus that reference* (these are absolute per-system clocks, not biases).
256    pub system_clocks_s: Vec<(GnssSystem, f64)>,
257    /// Dilution-of-precision scalars from the converged geometry. A
258    /// single-system solve uses the 0-ULP four-state cofactor; a multi-system
259    /// solve uses the general inverse with one clock column per constellation (a
260    /// deterministic diagnostic, not a 0-ULP target). `None` only if the
261    /// converged geometry is rank-deficient.
262    pub dop: Option<Dop>,
263    /// Per-constellation time (clock) DOP, one entry per GNSS in the solve, in
264    /// the same ascending system order as `system_clocks_s`: the square root of
265    /// that system's clock cofactor variance. The first entry's value equals
266    /// `dop.tdop` (the reference clock). One entry for a single-system solve.
267    /// Empty only when `dop` is `None` (rank-deficient geometry).
268    ///
269    /// This is exactly `dop.system_tdops`: the geometry layer reports the
270    /// per-system TDOPs already GNSS-tagged in [`Dop::system_tdops`], so this is
271    /// a direct copy and needs no re-tagging.
272    pub system_tdops: Vec<(GnssSystem, f64)>,
273    /// Post-fit residuals in meters, in `used_sats` order (unweighted
274    /// `P_meas - P_hat`).
275    pub residuals_m: Vec<f64>,
276    /// The satellites that contributed to the solve, ascending id order.
277    pub used_sats: Vec<GnssSatelliteId>,
278    /// The excluded satellites, each with its reason.
279    pub rejected_sats: Vec<RejectedSat>,
280    /// Iteration / convergence / model metadata.
281    pub metadata: SolutionMetadata,
282}
283
284impl ReceiverSolution {
285    /// Root-mean-square of the post-fit pseudorange residuals over the used satellites (0.0 when empty).
286    pub fn residual_rms_m(&self) -> f64 {
287        residual_rms(&self.residuals_m)
288    }
289}
290
291/// Which correction terms a solve applies, building up incrementally.
292#[derive(Debug, Clone, Copy, PartialEq, Eq)]
293pub struct Corrections {
294    /// Apply the Klobuchar L1 ionosphere delay.
295    pub ionosphere: bool,
296    /// Apply the Saastamoinen/Niell troposphere delay.
297    pub troposphere: bool,
298}
299
300impl Corrections {
301    /// No atmospheric corrections (geometry + clock + Sagnac only).
302    pub const NONE: Self = Self {
303        ionosphere: false,
304        troposphere: false,
305    };
306    /// Ionosphere only.
307    pub const IONO: Self = Self {
308        ionosphere: true,
309        troposphere: false,
310    };
311    /// Ionosphere and troposphere.
312    pub const IONO_TROPO: Self = Self {
313        ionosphere: true,
314        troposphere: true,
315    };
316}
317
318/// Broadcast Klobuchar coefficients for the ionosphere term.
319#[derive(Debug, Clone, Copy, PartialEq)]
320pub struct KlobucharCoeffs {
321    /// Cosine-amplitude polynomial coefficients (a0..a3).
322    pub alpha: [f64; 4],
323    /// Period polynomial coefficients (b0..b3).
324    pub beta: [f64; 4],
325}
326
327/// Surface meteorology for the troposphere term.
328#[derive(Debug, Clone, Copy, PartialEq)]
329pub struct SurfaceMet {
330    /// Total pressure (hPa).
331    pub pressure_hpa: f64,
332    /// Temperature (K).
333    pub temperature_k: f64,
334    /// Relative humidity, fraction in `[0, 1]`.
335    pub relative_humidity: f64,
336}
337
338impl Default for SurfaceMet {
339    /// Standard atmosphere: 1013.25 hPa, 288.15 K, 0.5 relative humidity.
340    fn default() -> Self {
341        Self {
342            pressure_hpa: 1013.25,
343            temperature_k: 288.15,
344            relative_humidity: 0.5,
345        }
346    }
347}
348
349/// Opt-in Huber/IRLS robust-reweighting configuration.
350///
351/// When a [`SolveInputs::robust`] is `Some(_)`, the solve runs an outer
352/// iteratively-reweighted least-squares loop on top of the static elevation
353/// weighting: a warm-start solve at the base elevation weights (bit-identical to
354/// the static path), then re-solves that rebuild the weight vector each outer
355/// iteration as `base_elevation_weight * huber(r_i / s)`, where `r_i` is the
356/// current unweighted post-fit residual and `s` is a floored MAD scale. With
357/// `robust = None` the solve is byte-identical to the static elevation-weighted
358/// solve. `Default` matches the `DEFAULT_*` config constants.
359#[derive(Debug, Clone, Copy, PartialEq)]
360pub struct RobustConfig {
361    /// Huber tuning constant `k`; residuals scaled below this keep full weight.
362    pub huber_k: f64,
363    /// Floor (m) on the MAD scale, preventing a near-perfect fit from
364    /// down-weighting every satellite.
365    pub scale_floor_m: f64,
366    /// Maximum total outer solves (the warm start plus reweighted resolves).
367    pub max_outer: usize,
368    /// Outer-loop position L2 step tolerance (m).
369    pub outer_tol_m: f64,
370}
371
372impl Default for RobustConfig {
373    fn default() -> Self {
374        Self {
375            huber_k: DEFAULT_HUBER_K,
376            scale_floor_m: DEFAULT_ROBUST_SCALE_FLOOR_M,
377            max_outer: DEFAULT_ROBUST_MAX_OUTER,
378            outer_tol_m: DEFAULT_ROBUST_OUTER_TOL_M,
379        }
380    }
381}
382
383/// Everything one SPP solve needs besides the SP3 product itself.
384///
385/// The receive epoch is carried as seconds-since-J2000 (`t_rx_j2000_s`), the
386/// argument the transmit-time iteration differences against the geometric range
387/// to land the satellite ephemeris at transmission, with no Julian-date
388/// round-trip inside the loop. The Klobuchar diurnal argument
389/// (`t_rx_second_of_day_s`) and the Niell seasonal argument (`day_of_year`) are
390/// supplied directly so the correction kernels run in their bit-exact native
391/// units.
392#[derive(Debug, Clone)]
393pub struct SolveInputs {
394    /// The pseudorange observations (any order; the solve sorts them).
395    pub observations: Vec<Observation>,
396    /// Receive epoch, seconds since J2000 in the SP3 product's time scale.
397    pub t_rx_j2000_s: f64,
398    /// GPS second-of-day of the receive epoch (Klobuchar diurnal argument).
399    pub t_rx_second_of_day_s: f64,
400    /// Fractional day-of-year of the receive epoch (Niell seasonal argument).
401    pub day_of_year: f64,
402    /// Initial guess `[x_m, y_m, z_m, b_m]`.
403    pub initial_guess: [f64; 4],
404    /// The correction terms to apply.
405    pub corrections: Corrections,
406    /// Broadcast Klobuchar coefficients (used iff `corrections.ionosphere`).
407    /// Applied to every system unless `beidou_klobuchar` overrides BeiDou.
408    pub klobuchar: KlobucharCoeffs,
409    /// Optional BeiDou-specific Klobuchar coefficients (the broadcast `BDSA`/
410    /// `BDSB` set). When present, BeiDou satellites use these instead of
411    /// [`klobuchar`](Self::klobuchar); both feed the same model, frequency-scaled
412    /// to B1I. `None` falls back to `klobuchar` for BeiDou too.
413    pub beidou_klobuchar: Option<KlobucharCoeffs>,
414    /// Optional Galileo-specific NeQuick-G coefficients (the broadcast `GAL`
415    /// `ai0`/`ai1`/`ai2` set). When present, Galileo satellites use these instead
416    /// of the GPS Klobuchar coefficients. `None` preserves the historical
417    /// Klobuchar fallback so existing zero-Galileo goldens stay bit-identical.
418    pub galileo_nequick: Option<GalileoNequickCoeffs>,
419    /// Optional augmentation ionosphere grid.
420    pub sbas_iono: Option<SbasIonoGrid>,
421    /// GLONASS FDMA channel numbers keyed by GLONASS slot (PRN), from the
422    /// broadcast nav `freq_channel` field or the observation header's
423    /// `GLONASS SLOT / FRQ #` records. Used only to resolve the per-satellite
424    /// GLONASS carrier for the ionosphere `(f_L1 / f_k)^2` scaling; an empty map
425    /// is correct for any solve with no GLONASS observation and leaves every
426    /// other constellation bit-identical. A GLONASS observation with the
427    /// ionosphere correction requested but no channel here is rejected with
428    /// [`SppError::IonosphereUnsupported`].
429    pub glonass_channels: BTreeMap<u8, i8>,
430    /// Surface meteorology (used iff `corrections.troposphere`).
431    pub met: SurfaceMet,
432    /// Opt-in Huber/IRLS robust reweighting. `None` (the default behavior)
433    /// runs the static elevation-weighted solve byte-identically; `Some(_)`
434    /// adds the outer reweighting loop described on [`RobustConfig`].
435    pub robust: Option<RobustConfig>,
436}
437
438impl Default for SolveInputs {
439    fn default() -> Self {
440        Self {
441            observations: Vec::new(),
442            t_rx_j2000_s: 0.0,
443            t_rx_second_of_day_s: 0.0,
444            day_of_year: 1.0,
445            initial_guess: [0.0; 4],
446            corrections: Corrections::NONE,
447            klobuchar: KlobucharCoeffs {
448                alpha: [0.0; 4],
449                beta: [0.0; 4],
450            },
451            beidou_klobuchar: None,
452            galileo_nequick: None,
453            sbas_iono: None,
454            glonass_channels: BTreeMap::new(),
455            met: SurfaceMet::default(),
456            robust: None,
457        }
458    }
459}
460
461/// Input-validation failure category for SPP public entry points.
462#[derive(Debug, Clone, Copy, PartialEq, Eq)]
463pub enum SppInputErrorKind {
464    /// A floating-point input was NaN or infinite.
465    NonFinite,
466    /// A positive physical input was zero or negative.
467    NotPositive,
468    /// A non-negative physical input was negative.
469    Negative,
470    /// A finite numeric input was outside its accepted range.
471    OutOfRange,
472    /// A required input field was absent.
473    Missing,
474    /// A text field could not be parsed as a float.
475    FloatParse,
476    /// A text field could not be parsed as an integer.
477    IntParse,
478    /// A civil date field was out of range.
479    InvalidCivilDate,
480    /// A civil time field was out of range.
481    InvalidCivilTime,
482}
483
484impl core::fmt::Display for SppInputErrorKind {
485    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
486        let label = match self {
487            Self::NonFinite => "not finite",
488            Self::NotPositive => "not positive",
489            Self::Negative => "negative",
490            Self::OutOfRange => "out of range",
491            Self::Missing => "missing",
492            Self::FloatParse => "invalid float",
493            Self::IntParse => "invalid integer",
494            Self::InvalidCivilDate => "invalid civil date",
495            Self::InvalidCivilTime => "invalid civil time",
496        };
497        f.write_str(label)
498    }
499}
500
501impl From<&validate::FieldError> for SppInputErrorKind {
502    fn from(error: &validate::FieldError) -> Self {
503        match error {
504            validate::FieldError::Missing { .. } => Self::Missing,
505            validate::FieldError::NonFinite { .. } => Self::NonFinite,
506            validate::FieldError::NotPositive { .. } => Self::NotPositive,
507            validate::FieldError::Negative { .. } => Self::Negative,
508            validate::FieldError::OutOfRange { .. } => Self::OutOfRange,
509            validate::FieldError::FloatParse { .. } => Self::FloatParse,
510            validate::FieldError::IntParse { .. } => Self::IntParse,
511            validate::FieldError::InvalidCivilDate { .. } => Self::InvalidCivilDate,
512            validate::FieldError::InvalidCivilTime { .. } => Self::InvalidCivilTime,
513        }
514    }
515}
516
517/// Error from [`solve`].
518#[derive(Debug, Clone)]
519pub enum SppError {
520    /// A public SPP input was malformed, non-finite, or outside its physical
521    /// domain. Boundary validation rejects this before satellite selection or
522    /// least-squares evaluation.
523    InvalidInput {
524        /// The invalid input field.
525        field: &'static str,
526        /// The validation failure category.
527        kind: SppInputErrorKind,
528    },
529    /// Fewer usable satellites survived rejection than the solve has parameters
530    /// (`3 + n_systems`: three position components plus one receiver clock per
531    /// GNSS), so the solve is underdetermined.
532    TooFewSatellites {
533        /// The number of satellites that survived rejection.
534        used: usize,
535        /// The number of satellites required (`3 + n_systems`).
536        required: usize,
537    },
538    /// The trust-region step hit a rank-deficient Jacobian (degenerate geometry).
539    Singular(least_squares::SolveError),
540    /// The same satellite appears in more than one observation. One pseudorange
541    /// per satellite is required, so the input is rejected rather than silently
542    /// picking one (which would make the result depend on observation order).
543    DuplicateObservation {
544        /// The satellite that was observed more than once.
545        satellite: GnssSatelliteId,
546    },
547    /// A satellite that survived the frozen selection had no usable SP3
548    /// position/clock at a transmit epoch reached during the solve. Returned
549    /// instead of panicking; normally precluded by the selection step.
550    EphemerisLost {
551        /// The satellite whose ephemeris became unavailable during the solve.
552        satellite: GnssSatelliteId,
553    },
554    /// The ionosphere correction was requested but an observed satellite has no
555    /// resolvable carrier frequency, so the L1 Klobuchar delay cannot be scaled
556    /// to it. GPS L1, Galileo E1, and BeiDou B1I have fixed carriers; a GLONASS
557    /// satellite resolves its per-satellite FDMA carrier from
558    /// [`SolveInputs::glonass_channels`], so a GLONASS observation whose channel
559    /// is absent from that map -- or present but outside the valid FDMA range
560    /// `[-7, +6]` -- (rather than GLONASS as a whole) is rejected here rather
561    /// than corrected with an undefined or out-of-domain frequency.
562    IonosphereUnsupported {
563        /// The satellite the ionosphere model does not cover.
564        satellite: GnssSatelliteId,
565    },
566}
567
568impl core::fmt::Display for SppError {
569    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
570        match self {
571            SppError::InvalidInput { field, kind } => {
572                write!(f, "invalid SPP input {field}: {kind}")
573            }
574            SppError::TooFewSatellites { used, required } => write!(
575                f,
576                "only {used} usable satellites; need at least {required} \
577                 (3 position + 1 clock per GNSS)"
578            ),
579            SppError::Singular(e) => write!(f, "degenerate geometry: {e}"),
580            SppError::DuplicateObservation { satellite } => {
581                write!(f, "satellite {satellite} observed more than once")
582            }
583            SppError::EphemerisLost { satellite } => {
584                write!(f, "satellite {satellite} lost ephemeris during the solve")
585            }
586            SppError::IonosphereUnsupported { satellite } => write!(
587                f,
588                "ionosphere correction has no modeled carrier frequency for {satellite}"
589            ),
590        }
591    }
592}
593
594impl std::error::Error for SppError {
595    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
596        match self {
597            SppError::Singular(error) => Some(error),
598            _ => None,
599        }
600    }
601}
602
603impl From<least_squares::SolveError> for SppError {
604    fn from(e: least_squares::SolveError) -> Self {
605        SppError::Singular(e)
606    }
607}
608
609/// Language-independent SPP solve policy used by the public API boundary.
610#[derive(Debug, Clone, Copy, Default, PartialEq)]
611pub struct SolvePolicy {
612    /// Business-level solution validation gates.
613    pub validation: SolutionValidationOptions,
614    /// Optional count of near-surface golden-spiral seeds for cold starts.
615    pub coarse_search_seeds: Option<usize>,
616}
617
618/// Error from [`solve_with_policy`].
619#[derive(Debug, Clone)]
620pub enum SolvePolicyError {
621    /// The underlying SPP solver failed.
622    Solve(SppError),
623    /// The solved receiver state failed a business-level validation gate.
624    Validation(SolutionValidationError),
625    /// Coarse search found no converged redundant candidate.
626    NoCoarseSolution,
627}
628
629impl From<SppError> for SolvePolicyError {
630    fn from(error: SppError) -> Self {
631        Self::Solve(error)
632    }
633}
634
635impl From<SolutionValidationError> for SolvePolicyError {
636    fn from(error: SolutionValidationError) -> Self {
637        Self::Validation(error)
638    }
639}
640
641impl core::fmt::Display for SolvePolicyError {
642    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
643        match self {
644            Self::Solve(error) => write!(f, "SPP solve failed: {error}"),
645            Self::Validation(error) => write!(f, "SPP validation failed: {error}"),
646            Self::NoCoarseSolution => write!(f, "coarse search found no converged SPP solution"),
647        }
648    }
649}
650
651impl std::error::Error for SolvePolicyError {
652    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
653        match self {
654            Self::Solve(error) => Some(error),
655            Self::Validation(error) => Some(error),
656            Self::NoCoarseSolution => None,
657        }
658    }
659}
660
661/// The SPP measurement-model operation-order selections, resolved from a
662/// strategy's [`EstimationRecipe`]: the transmit-time light-time range recipe,
663/// the Sagnac rotation recipe, and the receiver-frame (geodetic / az-el) recipe.
664///
665/// Threading these into [`sat_model`] is what makes SPP consume its
666/// `recipe.range` / `recipe.sagnac` / `recipe.frame` rather than hard-coding a
667/// single op-order. [`Self::reference`] is the SPP Skyfield reference selection,
668/// so the legacy entry points reproduce the current behavior bit-for-bit.
669#[derive(Debug, Clone, Copy, PartialEq, Eq)]
670pub(crate) struct SppModelRecipe {
671    pub range: RangeRecipe,
672    pub sagnac: SagnacRecipe,
673    pub frame: FrameRecipe,
674}
675
676impl SppModelRecipe {
677    /// The model selections carried by `recipe` (its range/sagnac/frame stages).
678    pub(crate) const fn from_recipe(recipe: &EstimationRecipe) -> Self {
679        Self {
680            range: recipe.range,
681            sagnac: recipe.sagnac,
682            frame: recipe.frame,
683        }
684    }
685
686    /// The SPP Skyfield reference model selections (the
687    /// [`EstimationRecipe::spp`] range/sagnac/frame stages).
688    pub(crate) const fn reference() -> Self {
689        Self::from_recipe(&EstimationRecipe::spp())
690    }
691}
692
693/// Per-satellite model used by the solve path: the Sagnac-rotated satellite
694/// position, the topocentric az/el, and the predicted pseudorange.
695///
696/// In test builds the struct additionally carries the named intermediate
697/// quantities (transmit time, satellite ECEF, Sagnac angle, geometric range,
698/// ionosphere, troposphere) so the 0-ULP trace-replay parity test can assert
699/// each one bit-for-bit against the reference recipe; the solve path never
700/// reads them, so they are gated out of production builds.
701#[derive(Debug, Clone, Copy)]
702pub(crate) struct SatModel {
703    pub sat_rot_ecef_m: [f64; 3],
704    pub el_rad: f64,
705    pub p_hat_m: f64,
706    #[cfg(all(test, sidereon_repo_tests))]
707    pub az_rad: f64,
708    #[cfg(all(test, sidereon_repo_tests))]
709    pub tau_s: f64,
710    #[cfg(all(test, sidereon_repo_tests))]
711    pub t_tx_j2000_s: f64,
712    #[cfg(all(test, sidereon_repo_tests))]
713    pub sat_ecef_m: [f64; 3],
714    #[cfg(all(test, sidereon_repo_tests))]
715    pub dt_sat_s: f64,
716    #[cfg(all(test, sidereon_repo_tests))]
717    pub theta_rad: f64,
718    #[cfg(all(test, sidereon_repo_tests))]
719    pub rho_m: f64,
720    #[cfg(all(test, sidereon_repo_tests))]
721    pub iono_m: f64,
722    #[cfg(all(test, sidereon_repo_tests))]
723    pub tropo_m: f64,
724}
725
726/// The broadcast ionosphere correction a satellite's system uses.
727#[derive(Debug, Clone, Copy, PartialEq)]
728pub(crate) enum SppIonosphere<'a> {
729    /// GPS/BeiDou Klobuchar alpha/beta model.
730    Klobuchar(KlobucharCoeffs),
731    /// Galileo NeQuick-G effective-ionisation coefficients.
732    GalileoNequick(GalileoNequickCoeffs),
733    /// Augmentation grid delay model.
734    SbasGrid(&'a SbasIonoGrid),
735}
736
737/// The ionosphere coefficients a satellite's system uses: Galileo prefers its
738/// `galileo_nequick` (`GAL`) set when present; BeiDou prefers its
739/// `beidou_klobuchar` (`BDSA`/`BDSB`) set when present; all missing
740/// constellation-specific sets fall back to the shared GPS Klobuchar values to
741/// preserve existing callers.
742fn ionosphere_for<'a>(system: GnssSystem, inputs: &'a SolveInputs) -> SppIonosphere<'a> {
743    if let Some(grid) = inputs
744        .sbas_iono
745        .as_ref()
746        .filter(|_| inputs.corrections.ionosphere)
747    {
748        return SppIonosphere::SbasGrid(grid);
749    }
750    match (system, inputs.galileo_nequick, inputs.beidou_klobuchar) {
751        (GnssSystem::Galileo, Some(gal), _) => SppIonosphere::GalileoNequick(gal),
752        (GnssSystem::BeiDou, _, Some(bds)) => SppIonosphere::Klobuchar(bds),
753        _ => SppIonosphere::Klobuchar(inputs.klobuchar),
754    }
755}
756
757/// Per-epoch inputs shared by every satellite's [`sat_model`] evaluation in a
758/// solve: the ephemeris source plus the epoch and correction arguments that do
759/// not vary between satellites. Bundling them lets [`sat_model`] take only the
760/// per-satellite arguments (id, receiver state, measurement, system Klobuchar)
761/// instead of a long positional parameter list.
762pub(crate) struct SatModelEnv<'a> {
763    pub eph: &'a dyn EphemerisSource,
764    /// Receive epoch, seconds since J2000 in the SP3 product's time scale.
765    pub t_rx_j2000_s: f64,
766    /// GPS second-of-day of the receive epoch (Klobuchar diurnal argument).
767    pub t_rx_second_of_day_s: f64,
768    /// Fractional day-of-year of the receive epoch (Niell seasonal argument).
769    pub day_of_year: f64,
770    /// The correction terms to apply.
771    pub corrections: Corrections,
772    /// Surface meteorology (used iff `corrections.troposphere`).
773    pub met: &'a SurfaceMet,
774    /// GLONASS FDMA channel numbers keyed by slot (PRN), used to resolve the
775    /// per-satellite GLONASS carrier for the ionosphere scaling.
776    pub glonass_channels: &'a BTreeMap<u8, i8>,
777    /// The range/sagnac/frame operation-order selections [`sat_model`] consumes,
778    /// resolved from the strategy's recipe.
779    pub model: SppModelRecipe,
780}
781
782/// Build the per-satellite predicted pseudorange in the SPP operation order
783/// SELECTED BY THE RECIPE on [`SatModelEnv::model`], sharing the
784/// parity-sensitive range and frame substrate with the other strategies.
785///
786/// The three model stages are read from the recipe rather than hard-coded:
787/// - **range** (`env.model.range`): the transmit-time light-time iteration.
788///   [`RangeRecipe::SppMeasuredPseudorangeFixedIter`] (the SPP reference) seeds
789///   `tau` from the measured pseudorange and runs a fixed iteration count (no
790///   convergence test). [`RangeRecipe::CanonicalLightTimeClosedFormSagnac`] (the
791///   canonical strategy) seeds the same way but iterates the light-time loop to
792///   convergence (the IERS-rigorous op-order). These are the two light-time
793///   recipes the SPP measurement model implements; the observable
794///   rounded-microsecond and RTK provided-transmit recipes are other strategies'
795///   range models and never reach here.
796/// - **sagnac** (`env.model.sagnac`): the closed-form Sagnac Z-rotation and the
797///   pre/post-rotation geometric range route through
798///   [`crate::estimation::substrate::range`] under the selected recipe.
799/// - **frame** (`env.model.frame`): the receiver geodetic conversion and the
800///   geodetic ENU azimuth/elevation route through
801///   [`crate::estimation::substrate::frames`] under the selected recipe (the SPP
802///   reference selects [`FrameRecipe::SppSkyfieldAuThreeIter`], the Skyfield AU
803///   three-iteration solve).
804///
805/// The raw residual ([`residual_unweighted`], `P_meas - P_hat`) the trust-region
806/// finite-difference solver differences carries no design rows of its own; the
807/// substrate [`crate::estimation::substrate::rows`] `ResidualRow` assembly serves
808/// the RTK/PPP normal-equation stacks.
809///
810/// Returns `None` if the ephemeris source has no usable position/clock for the
811/// satellite at the transmit epoch.
812pub(crate) fn sat_model(
813    env: &SatModelEnv,
814    sat: GnssSatelliteId,
815    rx_ecef_m: [f64; 3],
816    b_m: f64,
817    p_meas_m: f64,
818    ionosphere: SppIonosphere<'_>,
819) -> Option<SatModel> {
820    let sagnac = env.model.sagnac;
821    let frame = env.model.frame;
822
823    // Transmit-time light-time iteration, selected by the range recipe.
824    let (sat_pos, dt_sat, tau) = match env.model.range {
825        RangeRecipe::SppMeasuredPseudorangeFixedIter => {
826            // Fixed iteration count, no inner convergence test; seed tau from the
827            // measured pseudorange.
828            let mut tau = p_meas_m / C_M_S;
829            let mut t_tx = env.t_rx_j2000_s - tau;
830            let mut sat_pos = [0.0f64; 3];
831            let mut dt_sat = 0.0f64;
832            for _ in 0..TRANSMIT_TIME_ITERATIONS {
833                let (pos, clk) = env.eph.position_clock_at_j2000_s(sat, t_tx)?;
834                sat_pos = pos;
835                dt_sat = clk;
836                // Pre-rotation geometric range through the shared substrate (the
837                // closed-form recipe = plain `norm3(sub3(sat, recv))`).
838                let rho0 = geometric_range(sagnac, sat_pos, rx_ecef_m, OMEGA_E_DOT_RAD_S, C_M_S);
839                tau = rho0 / C_M_S;
840                t_tx = env.t_rx_j2000_s - tau;
841            }
842            (sat_pos, dt_sat, tau)
843        }
844        RangeRecipe::CanonicalLightTimeClosedFormSagnac => {
845            // Full iterative light-time (the IERS-rigorous op-order): iterate the
846            // transmit epoch until the signal travel time stops changing, rather
847            // than the reference recipe's fixed two-iteration truncation. Seeded,
848            // like the reference, from the measured pseudorange; the iteration
849            // converges to the geometric light-time fixed point
850            // `t_tx = t_rx - rho(t_tx)/c` with the closed-form Sagnac range (never
851            // a first-order scalar Sagnac). The satellite clock `dt_sat` returned
852            // by the ephemeris already carries the relativistic periodic term
853            // (the broadcast Keplerian evaluation applies `F*e*sqrt(A)*sin(E)`;
854            // SP3 precise clocks include it, the SPP L3 no-op), so the canonical
855            // relativistically-correct range consumes it directly with no
856            // double-counting term.
857            let mut tau = p_meas_m / C_M_S;
858            let mut t_tx = env.t_rx_j2000_s - tau;
859            let mut sat_pos = [0.0f64; 3];
860            let mut dt_sat = 0.0f64;
861            let mut prev_tau = f64::INFINITY;
862            for _ in 0..CANONICAL_LIGHT_TIME_MAX_ITERS {
863                let (pos, clk) = env.eph.position_clock_at_j2000_s(sat, t_tx)?;
864                sat_pos = pos;
865                dt_sat = clk;
866                let rho0 = geometric_range(sagnac, sat_pos, rx_ecef_m, OMEGA_E_DOT_RAD_S, C_M_S);
867                tau = rho0 / C_M_S;
868                t_tx = env.t_rx_j2000_s - tau;
869                if (tau - prev_tau).abs() <= CANONICAL_LIGHT_TIME_TOL_S {
870                    break;
871                }
872                prev_tau = tau;
873            }
874            (sat_pos, dt_sat, tau)
875        }
876        RangeRecipe::ObservableRoundedMicrosecondFixedIter
877        | RangeRecipe::RtkProvidedTxFirstOrderSagnac => unreachable!(
878            "the SPP measurement model runs only the measured-pseudorange or canonical light-time recipe"
879        ),
880    };
881
882    // Sagnac / Earth-rotation rotation over the flight time, selected by recipe.
883    let sat_rot = rotate_transmit_satellite(sagnac, sat_pos, tau, OMEGA_E_DOT_RAD_S);
884
885    // Geometric range (post-Sagnac) through the shared substrate.
886    let rho = geometric_range(sagnac, sat_rot, rx_ecef_m, OMEGA_E_DOT_RAD_S, C_M_S);
887
888    // Geometry for corrections: az/el from rx and the Sagnac-rotated satellite,
889    // through the recipe-selected frame substrate.
890    let g = az_el_from_ecef(frame, rx_ecef_m, sat_rot);
891
892    let mut iono_m = 0.0;
893    let mut tropo_m = 0.0;
894    if env.corrections.ionosphere {
895        // The SPP 0-ULP trace oracle pins this multiply-then-divide order, which
896        // `rad_to_deg_ref` implements (`rad * 180 / PI`).
897        let lat_deg = rad_to_deg_ref(g.geodetic.lat_rad);
898        let lon_deg = rad_to_deg_ref(g.geodetic.lon_rad);
899        let az_deg = rad_to_deg_ref(g.az_rad);
900        let el_deg = rad_to_deg_ref(g.el_rad);
901        // A used satellite always has a resolvable carrier here (the solve
902        // rejects an ionosphere request for any satellite that does not, GLONASS
903        // included via its FDMA channel), so the fallback is unreachable. The
904        // GLONASS per-satellite carrier makes the Klobuchar delay scale by
905        // `(f_L1 / f_k)^2` inside the kernel, exactly as RTKLIB-demo5 does.
906        let freq_hz = spp_iono_frequency_hz(sat, env.glonass_channels).unwrap_or(F_L1_HZ);
907        iono_m = match ionosphere {
908            SppIonosphere::Klobuchar(klobuchar) => klobuchar_native_unchecked(
909                &KlobucharParams {
910                    alpha: klobuchar.alpha,
911                    beta: klobuchar.beta,
912                },
913                lat_deg,
914                lon_deg,
915                az_deg,
916                el_deg,
917                env.t_rx_second_of_day_s,
918                freq_hz,
919            ),
920            SppIonosphere::GalileoNequick(coeffs) => galileo_nequick_g_native_unchecked(
921                &coeffs,
922                GalileoNequickEval {
923                    lat_deg,
924                    lon_deg,
925                    el_deg,
926                    t_gal_s: env.t_rx_second_of_day_s,
927                    day_of_year: env.day_of_year,
928                    frequency_hz: freq_hz,
929                },
930            ),
931            SppIonosphere::SbasGrid(grid) => {
932                grid.slant_delay_m(g.geodetic, g.el_rad, g.az_rad, freq_hz)?
933            }
934        };
935    }
936    if env.corrections.troposphere {
937        tropo_m = slant_components(
938            g.el_rad,
939            g.geodetic,
940            env.met.pressure_hpa,
941            env.met.temperature_k,
942            env.met.relative_humidity,
943            env.day_of_year,
944        )
945        .slant_m;
946    }
947
948    // Predicted pseudorange, left-to-right; c*dt_sat is a single multiply.
949    let p_hat = rho + b_m - C_M_S * dt_sat + iono_m + tropo_m;
950
951    Some(SatModel {
952        sat_rot_ecef_m: sat_rot,
953        el_rad: g.el_rad,
954        p_hat_m: p_hat,
955        #[cfg(all(test, sidereon_repo_tests))]
956        az_rad: g.az_rad,
957        #[cfg(all(test, sidereon_repo_tests))]
958        tau_s: tau,
959        // Bit-identical to the loop's final `t_tx = t_rx - tau` (same operands).
960        #[cfg(all(test, sidereon_repo_tests))]
961        t_tx_j2000_s: env.t_rx_j2000_s - tau,
962        #[cfg(all(test, sidereon_repo_tests))]
963        sat_ecef_m: sat_pos,
964        #[cfg(all(test, sidereon_repo_tests))]
965        dt_sat_s: dt_sat,
966        #[cfg(all(test, sidereon_repo_tests))]
967        theta_rad: OMEGA_E_DOT_RAD_S * tau,
968        #[cfg(all(test, sidereon_repo_tests))]
969        rho_m: rho,
970        #[cfg(all(test, sidereon_repo_tests))]
971        iono_m,
972        #[cfg(all(test, sidereon_repo_tests))]
973        tropo_m,
974    })
975}
976
977/// The frozen-geometry selection: used satellites (ascending id), rejected
978/// satellites with reason, and the per-used-sat weight from the elevation at
979/// the initial-guess geometry.
980pub(crate) struct Selection {
981    pub used: Vec<GnssSatelliteId>,
982    pub rejected: Vec<RejectedSat>,
983    /// `weight` per used satellite, index-aligned to `used`.
984    pub weights: Vec<f64>,
985}
986
987pub(crate) fn select_sats(
988    eph: &dyn EphemerisSource,
989    inputs: &SolveInputs,
990    model: SppModelRecipe,
991) -> Selection {
992    let rx0 = [
993        inputs.initial_guess[0],
994        inputs.initial_guess[1],
995        inputs.initial_guess[2],
996    ];
997    let b0 = inputs.initial_guess[3];
998
999    // Ascending satellite-id order, never observation order.
1000    let mut obs: Vec<&Observation> = inputs.observations.iter().collect();
1001    obs.sort_by_key(|o| o.satellite_id);
1002
1003    let mut used = Vec::new();
1004    let mut rejected = Vec::new();
1005    let mut weights = Vec::new();
1006
1007    let env = SatModelEnv {
1008        eph,
1009        t_rx_j2000_s: inputs.t_rx_j2000_s,
1010        t_rx_second_of_day_s: inputs.t_rx_second_of_day_s,
1011        day_of_year: inputs.day_of_year,
1012        corrections: inputs.corrections,
1013        met: &inputs.met,
1014        glonass_channels: &inputs.glonass_channels,
1015        model,
1016    };
1017    for ob in obs {
1018        let ionosphere = ionosphere_for(ob.satellite_id.system, inputs);
1019        let uses_sbas_grid = matches!(ionosphere, SppIonosphere::SbasGrid(_));
1020        let model = sat_model(&env, ob.satellite_id, rx0, b0, ob.pseudorange_m, ionosphere);
1021        let Some(model) = model else {
1022            rejected.push(RejectedSat {
1023                satellite_id: ob.satellite_id,
1024                reason: if uses_sbas_grid {
1025                    RejectionReason::SbasIonoUncovered
1026                } else {
1027                    RejectionReason::NoEphemeris
1028                },
1029            });
1030            continue;
1031        };
1032        if model.el_rad < ELEVATION_MASK_RAD {
1033            rejected.push(RejectedSat {
1034                satellite_id: ob.satellite_id,
1035                reason: RejectionReason::LowElevation,
1036            });
1037            continue;
1038        }
1039        let sin_el = model.el_rad.sin();
1040        let weight = (sin_el * sin_el) / (SIGMA0_M * SIGMA0_M);
1041        used.push(ob.satellite_id);
1042        weights.push(weight);
1043    }
1044
1045    Selection {
1046        used,
1047        rejected,
1048        weights,
1049    }
1050}
1051
1052/// The distinct GNSS present in `used`, in ascending system order.
1053///
1054/// The receiver-clock part of the state has one entry per system, each the
1055/// *absolute* receiver clock for that system (not a bias); the first is the
1056/// reference clock and a system's inter-system bias is its clock minus that
1057/// reference. For a single-system solve this is one element and the state is the
1058/// classic `[x, y, z, b]`.
1059pub(crate) fn clock_systems(used: &[GnssSatelliteId]) -> Vec<GnssSystem> {
1060    let mut systems: Vec<GnssSystem> = used
1061        .iter()
1062        .map(|s| match s.system {
1063            GnssSystem::Sbas => GnssSystem::Gps,
1064            system => system,
1065        })
1066        .collect();
1067    systems.sort_unstable();
1068    systems.dedup();
1069    systems
1070}
1071
1072/// The unweighted residual vector `P_meas - P_hat` at state `x`, in `used` order.
1073///
1074/// The state is `[x, y, z, clk_0, clk_1, ...]` where `clk_i` is the absolute
1075/// receiver clock for the i-th system returned by [`clock_systems`] (in meters).
1076/// Each satellite's residual uses its own system's clock, so a multi-GNSS set is
1077/// solved with one absolute receiver clock per system (a system's inter-system
1078/// bias is its clock minus the reference `clk_0`). A single-system set reduces to
1079/// `[x, y, z, b]` and `clk_0 = x[3]`.
1080///
1081/// Returns `Err(satellite)` if a used satellite has no observation or no usable
1082/// ephemeris at `x` (the frozen used set is fixed, but a finite-difference probe
1083/// could in principle reach an epoch off the ephemeris coverage). The caller
1084/// turns that into an [`SppError`] rather than panicking.
1085pub(crate) fn residual_unweighted(
1086    eph: &dyn EphemerisSource,
1087    used: &[GnssSatelliteId],
1088    obs_by_id: &[(GnssSatelliteId, f64)],
1089    x: &[f64],
1090    inputs: &SolveInputs,
1091    model: SppModelRecipe,
1092) -> Result<Vec<f64>, GnssSatelliteId> {
1093    let rx = [x[0], x[1], x[2]];
1094    let systems = clock_systems(used);
1095    let env = SatModelEnv {
1096        eph,
1097        t_rx_j2000_s: inputs.t_rx_j2000_s,
1098        t_rx_second_of_day_s: inputs.t_rx_second_of_day_s,
1099        day_of_year: inputs.day_of_year,
1100        corrections: inputs.corrections,
1101        met: &inputs.met,
1102        glonass_channels: &inputs.glonass_channels,
1103        model,
1104    };
1105    let mut out = Vec::with_capacity(used.len());
1106    for &sat in used {
1107        let p_meas = obs_by_id
1108            .iter()
1109            .find(|(id, _)| *id == sat)
1110            .map(|(_, p)| *p)
1111            .ok_or(sat)?;
1112        // The clock for this satellite's system (index 0 = reference clock).
1113        let sat_clock_system = match sat.system {
1114            GnssSystem::Sbas => GnssSystem::Gps,
1115            system => system,
1116        };
1117        let sys_idx = systems
1118            .iter()
1119            .position(|s| *s == sat_clock_system)
1120            .unwrap_or(0);
1121        let b = x[3 + sys_idx];
1122        let m =
1123            sat_model(&env, sat, rx, b, p_meas, ionosphere_for(sat.system, inputs)).ok_or(sat)?;
1124        out.push(p_meas - m.p_hat_m);
1125    }
1126    Ok(out)
1127}
1128
1129/// Run the SPP solve from synthesized/measured pseudoranges.
1130///
1131/// Uses the core trust-region weighted least-squares solver over the
1132/// `sqrt(w) * (P_meas - P_hat)` residual. The converged position/clock is a
1133/// sub-micron solver-agreement result (the linear-algebra step is not
1134/// bit-reproducible across BLAS builds), not a 0-ULP claim. The residual /
1135/// Jacobian substrate evaluated at recorded states is the 0-ULP target and is
1136/// exercised by the trace-replay parity test, not by this entry point.
1137///
1138/// This is the reference SPP entry point: it runs the legacy
1139/// [`SolverRecipe::NalgebraTrfLegacy`] trust-region factorization, so its
1140/// existing goldens are unchanged. [`solve_with_solver`] selects the owned
1141/// deterministic kernel.
1142pub fn solve(
1143    eph: &dyn EphemerisSource,
1144    inputs: &SolveInputs,
1145    with_geodetic: bool,
1146) -> Result<ReceiverSolution, SppError> {
1147    validate_solve_inputs(inputs)?;
1148    solve_inner(
1149        eph,
1150        inputs,
1151        with_geodetic,
1152        SppModelRecipe::reference(),
1153        TrustRegionSolve::NalgebraLu,
1154    )
1155}
1156
1157/// SPP's trust-region stage recognizes the owned deterministic solver
1158/// ([`SolverRecipe::OwnedDeterministicTrf`]), which owns the dense subproblem
1159/// factorization with a fixed reduction order and its own frozen-bits golden;
1160/// every other recipe selects the legacy nalgebra LU path that [`solve`] uses.
1161/// The other [`SolverRecipe`] variants name other strategies' linear-solve
1162/// stages (RTK first-tie, PPP last-tie, host LAPACK) and are not SPP
1163/// trust-region solvers.
1164const fn trust_region_solve(solver: SolverRecipe) -> TrustRegionSolve {
1165    match solver {
1166        SolverRecipe::OwnedDeterministicTrf => TrustRegionSolve::OwnedGaussianFirstTie,
1167        _ => TrustRegionSolve::NalgebraLu,
1168    }
1169}
1170
1171/// SPP solve with an explicit [`SolverRecipe`] for the trust-region stage.
1172///
1173/// Selecting [`SolverRecipe::NalgebraTrfLegacy`] is bit-identical to [`solve`].
1174/// [`SolverRecipe::OwnedDeterministicTrf`] swaps in the owned deterministic
1175/// Gaussian-elimination factorization for the dense trust-region subproblem (no
1176/// nalgebra LU, no black-box BLAS in that solve), pinned to its own frozen-bits
1177/// golden; all other model stages are unchanged. The owned kernel owns ONLY the
1178/// subproblem factorization: the normal-matrix / gradient / norm reductions that
1179/// build the subproblem still go through nalgebra's CPU-dispatched dense
1180/// algebra, so the cross-platform bit guarantee is scoped to the factorization
1181/// (the converged bits are this build's reproducible output, not a portable
1182/// constant).
1183pub fn solve_with_solver(
1184    eph: &dyn EphemerisSource,
1185    inputs: &SolveInputs,
1186    with_geodetic: bool,
1187    solver: SolverRecipe,
1188) -> Result<ReceiverSolution, SppError> {
1189    validate_solve_inputs(inputs)?;
1190    solve_inner(
1191        eph,
1192        inputs,
1193        with_geodetic,
1194        SppModelRecipe::reference(),
1195        trust_region_solve(solver),
1196    )
1197}
1198
1199fn solve_inner(
1200    eph: &dyn EphemerisSource,
1201    inputs: &SolveInputs,
1202    with_geodetic: bool,
1203    model: SppModelRecipe,
1204    linear_solve: TrustRegionSolve,
1205) -> Result<ReceiverSolution, SppError> {
1206    // One pseudorange per satellite. Reject duplicates deterministically (by
1207    // the smallest repeated id) so the result can never depend on observation
1208    // order and the parameter-count check below (`sel.used.len() < n_params`,
1209    // where `n_params = 3 + n_clocks`) counts distinct satellites.
1210    let mut ids: Vec<GnssSatelliteId> =
1211        inputs.observations.iter().map(|o| o.satellite_id).collect();
1212    ids.sort_unstable();
1213    if let Some(w) = ids.windows(2).find(|w| w[0] == w[1]) {
1214        return Err(SppError::DuplicateObservation { satellite: w[0] });
1215    }
1216
1217    // The broadcast Klobuchar delay is computed on L1 and scaled to each
1218    // satellite's carrier by `(f_L1 / f)^2`. GPS L1, Galileo E1, and BeiDou B1I
1219    // have fixed carriers; GLONASS is FDMA, so its carrier is resolved per
1220    // satellite from `glonass_channels`. A satellite whose carrier cannot be
1221    // resolved (a GLONASS observation with no channel in the map, or a channel
1222    // outside the valid `[-7, +6]` FDMA range) cannot be scaled, so reject an
1223    // ionosphere-corrected solve that includes it rather
1224    // than apply an undefined correction. This runs before selection so the
1225    // model is never evaluated for it (`select_sats` would otherwise call
1226    // `sat_model` with the correction for every observation).
1227    if inputs.corrections.ionosphere {
1228        if let Some(sat) = ids
1229            .iter()
1230            .find(|s| spp_iono_frequency_hz(**s, &inputs.glonass_channels).is_none())
1231        {
1232            return Err(SppError::IonosphereUnsupported { satellite: *sat });
1233        }
1234    }
1235
1236    let sel = select_sats(eph, inputs, model);
1237
1238    // One receiver-clock parameter per distinct GNSS (a reference clock plus an
1239    // inter-system bias for each additional system), so the state has
1240    // `3 + n_systems` parameters and needs at least that many usable satellites.
1241    // Floor the clock count at one: the minimum solve is the four-parameter
1242    // single-system form even when no satellite survives selection.
1243    let systems = clock_systems(&sel.used);
1244    let n_clocks = systems.len();
1245    // SPP's weighted-residual rows feed the trust-region solver, which owns the
1246    // normal-equation factorization (NormalRecipe::SppWeightedResidualFiniteDifference
1247    // via SolverRecipe::NalgebraTrfLegacy); only the parameter stack is named here.
1248    let n_params = ParameterLayout::spp(n_clocks.max(1)).dim();
1249    if sel.used.len() < n_params {
1250        return Err(SppError::TooFewSatellites {
1251            used: sel.used.len(),
1252            required: n_params,
1253        });
1254    }
1255
1256    let obs_by_id: Vec<(GnssSatelliteId, f64)> = inputs
1257        .observations
1258        .iter()
1259        .map(|o| (o.satellite_id, o.pseudorange_m))
1260        .collect();
1261
1262    let used = sel.used.clone();
1263    let inputs_ref = inputs.clone();
1264    let obs_ref = obs_by_id.clone();
1265    let eph_ref = eph;
1266    let n_used = used.len();
1267
1268    // The least-squares solver's residual closure cannot return an error, so an
1269    // ephemeris loss during a probe is recorded here and surfaced as an
1270    // SppError after the solve (rather than panicking inside the closure).
1271    let lost = std::rc::Rc::new(std::cell::Cell::new(None::<GnssSatelliteId>));
1272    let lost_in = lost.clone();
1273    let residual = move |x: &DVector<f64>| -> DVector<f64> {
1274        match residual_unweighted(eph_ref, &used, &obs_ref, x.as_slice(), &inputs_ref, model) {
1275            Ok(r) => DVector::from_vec(r),
1276            Err(sat) => {
1277                lost_in.set(Some(sat));
1278                DVector::from_vec(vec![0.0; n_used])
1279            }
1280        }
1281    };
1282
1283    // Extend the 4-element initial guess `[x, y, z, b_ref]` with a zero starting
1284    // value for each additional system's inter-system bias.
1285    let mut x0v = inputs.initial_guess.to_vec();
1286    x0v.extend(std::iter::repeat_n(0.0, n_clocks - 1));
1287    let x0 = DVector::from_vec(x0v);
1288    // Agreement-track stopping thresholds (see the SPP_SOLVER_* constants).
1289    let opts = SolveOptions {
1290        gtol: SPP_SOLVER_GTOL,
1291        ftol: SPP_SOLVER_FTOL,
1292        xtol: SPP_SOLVER_XTOL,
1293        max_nfev: SPP_SOLVER_MAX_NFEV,
1294    };
1295
1296    // The static elevation weights (base weights), index-aligned to `sel.used`.
1297    let base_weights = DVector::from_row_slice(&sel.weights);
1298
1299    // The warm-start solve uses the base elevation weights exactly. On the
1300    // static path (`robust == None`) this is the literal current sequence: a
1301    // single `with_weights(residual, x0, base_weights)` solve and nothing else,
1302    // so the byte output is unchanged. On the robust path it seeds the outer
1303    // loop.
1304    //
1305    // Check for an ephemeris loss recorded by the residual closure BEFORE
1306    // propagating a solver error: a lost satellite zeroes its residual row,
1307    // which can itself make the Jacobian singular, and EphemerisLost is the
1308    // more specific, actionable cause.
1309    let problem = LeastSquaresProblem::with_weights(&residual, x0, base_weights);
1310    let report_result = solve_trf_with(&problem, &opts, linear_solve);
1311    if let Some(satellite) = lost.get() {
1312        return Err(SppError::EphemerisLost { satellite });
1313    }
1314    let mut report = report_result?;
1315
1316    let mut outer_iterations = 0usize;
1317    let mut final_robust_scale_m: Option<f64> = None;
1318
1319    // Outer Huber/IRLS reweighting loop, ONLY on the robust path. Each iteration
1320    // recomputes the unweighted post-fit residuals at the current converged
1321    // state, derives a floored MAD scale, builds the effective weight vector
1322    // `base_elevation_weight * huber(r_i / s)` index-aligned to `sel.used`,
1323    // rebuilds the problem warm-started at the previous state, and re-solves. It
1324    // stops when the position step drops below `outer_tol_m` or the reweighted
1325    // solve budget left after the warm start is hit (recording
1326    // `converged = false` if the inner solve itself did not converge on the
1327    // final pass).
1328    if let Some(rc) = inputs.robust {
1329        for _ in 0..rc.max_outer.saturating_sub(1) {
1330            if lost.get().is_some() {
1331                break;
1332            }
1333            // Unweighted post-fit residuals at the current state, in used order.
1334            let post = match residual_unweighted(
1335                eph,
1336                &sel.used,
1337                &obs_by_id,
1338                report.x.as_slice(),
1339                inputs,
1340                model,
1341            ) {
1342                Ok(r) => r,
1343                Err(satellite) => return Err(SppError::EphemerisLost { satellite }),
1344            };
1345            let scale = mad_scale(&post, rc.scale_floor_m).map_err(map_robust_error)?;
1346            // Effective weight per used sat: base elevation weight times the
1347            // Huber multiplier of the scaled residual.
1348            let eff: Vec<f64> = post
1349                .iter()
1350                .zip(sel.weights.iter())
1351                .map(|(&r, &bw)| bw * huber_weight(r / scale, rc.huber_k))
1352                .collect();
1353            let eff_w = DVector::from_row_slice(&eff);
1354            let x_prev = report.x.clone();
1355            let problem = LeastSquaresProblem::with_weights(&residual, x_prev.clone(), eff_w);
1356            let next = solve_trf_with(&problem, &opts, linear_solve);
1357            if let Some(satellite) = lost.get() {
1358                return Err(SppError::EphemerisLost { satellite });
1359            }
1360            report = next?;
1361            outer_iterations += 1;
1362            final_robust_scale_m = Some(scale);
1363            // Position L2 step between successive outer solves.
1364            let dpos = ((report.x[0] - x_prev[0]).powi(2)
1365                + (report.x[1] - x_prev[1]).powi(2)
1366                + (report.x[2] - x_prev[2]).powi(2))
1367            .sqrt();
1368            if dpos < rc.outer_tol_m {
1369                break;
1370            }
1371        }
1372    }
1373
1374    let xs = &report.x;
1375    let position = ItrfPositionM::new(xs[0], xs[1], xs[2]).expect("valid ITRF position");
1376    let rx_clock_s = xs[3] / C_M_S;
1377    // One receiver clock (seconds) per system, in the same order as the state's
1378    // clock parameters. The first equals `rx_clock_s` (the reference system).
1379    let system_clocks_s: Vec<(GnssSystem, f64)> = systems
1380        .iter()
1381        .enumerate()
1382        .map(|(i, &sys)| (sys, xs[3 + i] / C_M_S))
1383        .collect();
1384    let geodetic = if with_geodetic {
1385        Some(geodetic_from_ecef(model.frame, [xs[0], xs[1], xs[2]]))
1386    } else {
1387        None
1388    };
1389
1390    // Post-fit unweighted residuals in used order.
1391    let residuals_m = residual_unweighted(eph, &sel.used, &obs_by_id, xs.as_slice(), inputs, model)
1392        .map_err(|satellite| SppError::EphemerisLost { satellite })?;
1393
1394    // DOP from the converged geometry: line-of-sight unit vectors to the
1395    // Sagnac-rotated satellite positions, with the frozen weights. A
1396    // single-system solve uses the 0-ULP four-state cofactor inverse; a
1397    // multi-system solve uses the general (3 + n_systems) inverse with one clock
1398    // column per GNSS (a deterministic geometry diagnostic, not a 0-ULP target).
1399    // The receiver-clock argument does not affect the line of sight, so the
1400    // reference clock is passed for every satellite.
1401    let rx_ecef = [xs[0], xs[1], xs[2]];
1402    let geo = geodetic_from_ecef(model.frame, [xs[0], xs[1], xs[2]]);
1403    let mut los = Vec::with_capacity(sel.used.len());
1404    let mut clock_index = Vec::with_capacity(sel.used.len());
1405    let env = SatModelEnv {
1406        eph,
1407        t_rx_j2000_s: inputs.t_rx_j2000_s,
1408        t_rx_second_of_day_s: inputs.t_rx_second_of_day_s,
1409        day_of_year: inputs.day_of_year,
1410        corrections: inputs.corrections,
1411        met: &inputs.met,
1412        glonass_channels: &inputs.glonass_channels,
1413        model,
1414    };
1415    for &sat in &sel.used {
1416        let p_meas = obs_by_id
1417            .iter()
1418            .find(|(id, _)| *id == sat)
1419            .map(|(_, p)| *p)
1420            .ok_or(SppError::EphemerisLost { satellite: sat })?;
1421        let m = sat_model(
1422            &env,
1423            sat,
1424            rx_ecef,
1425            xs[3],
1426            p_meas,
1427            ionosphere_for(sat.system, inputs),
1428        )
1429        .ok_or(SppError::EphemerisLost { satellite: sat })?;
1430        let dx = m.sat_rot_ecef_m[0] - rx_ecef[0];
1431        let dy = m.sat_rot_ecef_m[1] - rx_ecef[1];
1432        let dz = m.sat_rot_ecef_m[2] - rx_ecef[2];
1433        let n = (dx * dx + dy * dy + dz * dz).sqrt();
1434        los.push(LineOfSight::new(dx / n, dy / n, dz / n));
1435        let idx = systems.iter().position(|s| *s == sat.system).unwrap_or(0);
1436        clock_index.push(idx);
1437    }
1438    // `systems` is the clock-column ordering: `clock_index[k] ==
1439    // systems.position(sat.system)`, so `systems[c]` owns clock column `c` (the
1440    // same ordering `system_clocks_s` uses). The multi-system path is handed
1441    // that mapping and returns `Dop::system_tdops` already GNSS-tagged; the
1442    // single-system 0-ULP `dop` carries no constellation identity, so tag its
1443    // lone clock here with the one system in the solve.
1444    let dop_result = if n_clocks == 1 {
1445        dop(&los, &sel.weights, geo).ok().map(|mut d| {
1446            d.system_tdops = vec![(systems[0], d.tdop)];
1447            d
1448        })
1449    } else {
1450        dop_multi(&los, &clock_index, &systems, n_clocks, &sel.weights, geo).ok()
1451    };
1452    // The solution's per-system TDOPs come straight from the now-tagged
1453    // `Dop::system_tdops`; empty when the converged geometry is rank-deficient.
1454    let system_tdops: Vec<(GnssSystem, f64)> = dop_result
1455        .as_ref()
1456        .map(|d| d.system_tdops.clone())
1457        .unwrap_or_default();
1458
1459    let converged = matches!(
1460        report.status,
1461        Status::GradientTolerance | Status::CostTolerance | Status::StepTolerance
1462    );
1463    let metadata_used_count = sel.used.len();
1464    let metadata_redundancy = redundancy(&systems, metadata_used_count);
1465
1466    Ok(ReceiverSolution {
1467        position,
1468        geodetic,
1469        rx_clock_s,
1470        system_clocks_s,
1471        dop: dop_result,
1472        system_tdops,
1473        residuals_m,
1474        used_sats: sel.used,
1475        rejected_sats: sel.rejected,
1476        metadata: SolutionMetadata {
1477            iterations: report.iterations,
1478            converged,
1479            status: report.status,
1480            ionosphere_applied: inputs.corrections.ionosphere,
1481            troposphere_applied: inputs.corrections.troposphere,
1482            outer_iterations,
1483            final_robust_scale_m,
1484            used_count: metadata_used_count,
1485            systems,
1486            redundancy: metadata_redundancy,
1487            raim_checkable: metadata_redundancy >= 1,
1488        },
1489    })
1490}
1491
1492/// Run SPP under the public API's language-independent validation/orchestration
1493/// policy.
1494///
1495/// Thin compatibility wrapper over the runtime strategy selector
1496/// ([`crate::estimation::strategies::estimate`]): it drives the shared
1497/// per-technique implementation [`run`] under the SPP reference strategy, which
1498/// resolves to the SPP reference recipe. The reference strategy always yields an
1499/// SPP solution or an SPP error, so the result is bit-identical to the recipe
1500/// driving [`run`] directly.
1501pub fn solve_with_policy(
1502    eph: &dyn EphemerisSource,
1503    inputs: &SolveInputs,
1504    with_geodetic: bool,
1505    policy: SolvePolicy,
1506) -> Result<ReceiverSolution, SolvePolicyError> {
1507    use crate::estimation::recipe::StrategyId;
1508    use crate::estimation::strategies::{
1509        estimate, EstimateError, EstimateInput, EstimateOptions, EstimateOutput,
1510    };
1511    match estimate(
1512        EstimateInput::Spp {
1513            eph,
1514            inputs,
1515            with_geodetic,
1516            policy,
1517        },
1518        EstimateOptions::new(StrategyId::spp_reference()),
1519    ) {
1520        Ok(EstimateOutput::Spp(solution)) => Ok(*solution),
1521        Err(EstimateError::Spp(error)) => Err(error),
1522        Ok(_) | Err(_) => {
1523            unreachable!("the SPP reference strategy yields an SPP solution or an SPP error")
1524        }
1525    }
1526}
1527
1528/// Solve a batch of independent SPP epochs against a shared ephemeris, serially.
1529///
1530/// Element `i` of the result is [`solve_with_policy`] applied to `epochs[i]`,
1531/// with the shared `eph`, `with_geodetic`, and `policy` (every epoch is one
1532/// receive instant's [`SolveInputs`]; the receiver's clock and position are
1533/// re-estimated per epoch, so the epochs are independent). The first solve error
1534/// for an epoch becomes that element's `Err`. This is the single-threaded
1535/// reference the parallel [`solve_spp_batch_parallel`] is proven bit-identical
1536/// against.
1537pub fn solve_spp_batch_serial(
1538    eph: &dyn EphemerisSource,
1539    epochs: &[SolveInputs],
1540    with_geodetic: bool,
1541    policy: SolvePolicy,
1542) -> Vec<Result<ReceiverSolution, SolvePolicyError>> {
1543    epochs
1544        .iter()
1545        .map(|inputs| solve_with_policy(eph, inputs, with_geodetic, policy))
1546        .collect()
1547}
1548
1549/// Solve a batch of independent SPP epochs against a shared ephemeris, fanning
1550/// the independent per-epoch solves across a rayon thread pool.
1551///
1552/// Each epoch is solved by the same serial [`solve_with_policy`] kernel and the
1553/// indexed parallel collect preserves input order, so element `i` is
1554/// byte-for-byte identical to element `i` of [`solve_spp_batch_serial`]: the
1555/// epochs share only the immutable `eph`/`policy`, there is no cross-epoch state
1556/// and no reduction, and a single solve is unchanged. The work is embarrassingly
1557/// parallel (epochs are independent), so throughput scales with cores while
1558/// every value stays bit-exact. `eph` must be [`Sync`] to be shared across the
1559/// pool.
1560pub fn solve_spp_batch_parallel(
1561    eph: &(dyn EphemerisSource + Sync),
1562    epochs: &[SolveInputs],
1563    with_geodetic: bool,
1564    policy: SolvePolicy,
1565) -> Vec<Result<ReceiverSolution, SolvePolicyError>> {
1566    use rayon::prelude::*;
1567    epochs
1568        .par_iter()
1569        .map(|inputs| solve_with_policy(eph, inputs, with_geodetic, policy))
1570        .collect()
1571}
1572
1573/// Drive SPP from a resolved [`EstimationRecipe`]: the shared per-technique
1574/// implementation that [`crate::estimation::strategies::estimate`] dispatches to.
1575/// The recipe's range/sagnac/frame stages select the SPP measurement-model
1576/// operation order ([`SppModelRecipe`], threaded into [`sat_model`]) and its
1577/// [`SolverRecipe`] selects the trust-region factorization; the public
1578/// validation/orchestration policy is applied here. For the SPP reference recipe
1579/// every selected order equals the value the legacy [`solve`] path hard-coded, so
1580/// this is bit-identical to it.
1581pub(crate) fn run(
1582    recipe: &EstimationRecipe,
1583    eph: &dyn EphemerisSource,
1584    inputs: &SolveInputs,
1585    with_geodetic: bool,
1586    policy: SolvePolicy,
1587) -> Result<ReceiverSolution, SolvePolicyError> {
1588    validate_solve_inputs(inputs)?;
1589    let model = SppModelRecipe::from_recipe(recipe);
1590    match policy.coarse_search_seeds {
1591        Some(seed_count) => solve_coarse(
1592            eph,
1593            inputs,
1594            with_geodetic,
1595            policy,
1596            seed_count,
1597            model,
1598            recipe.solver,
1599        ),
1600        None => solve_validated(
1601            eph,
1602            inputs,
1603            with_geodetic,
1604            policy.validation,
1605            model,
1606            recipe.solver,
1607        ),
1608    }
1609}
1610
1611fn solve_validated(
1612    eph: &dyn EphemerisSource,
1613    inputs: &SolveInputs,
1614    with_geodetic: bool,
1615    validation: SolutionValidationOptions,
1616    model: SppModelRecipe,
1617    solver: SolverRecipe,
1618) -> Result<ReceiverSolution, SolvePolicyError> {
1619    let solution = solve_inner(
1620        eph,
1621        inputs,
1622        with_geodetic,
1623        model,
1624        trust_region_solve(solver),
1625    )?;
1626    validate_receiver_solution(&solution, validation)?;
1627    Ok(solution)
1628}
1629
1630fn solve_coarse(
1631    eph: &dyn EphemerisSource,
1632    inputs: &SolveInputs,
1633    with_geodetic: bool,
1634    policy: SolvePolicy,
1635    seed_count: usize,
1636    model: SppModelRecipe,
1637    solver: SolverRecipe,
1638) -> Result<ReceiverSolution, SolvePolicyError> {
1639    let mut candidates = Vec::new();
1640    let mut last_error = SolvePolicyError::NoCoarseSolution;
1641
1642    for seed in std::iter::once(inputs.initial_guess).chain(coarse_seeds(seed_count)) {
1643        let mut seeded = inputs.clone();
1644        seeded.initial_guess = seed;
1645        match solve_validated(
1646            eph,
1647            &seeded,
1648            with_geodetic,
1649            policy.validation,
1650            model,
1651            solver,
1652        ) {
1653            Ok(solution) => candidates.push(solution),
1654            Err(error) => last_error = error,
1655        }
1656    }
1657
1658    select_coarse_candidate(&candidates)
1659        .cloned()
1660        .ok_or(last_error)
1661}
1662
1663fn coarse_seeds(n: usize) -> Vec<[f64; 4]> {
1664    let golden = PI * (3.0 - 5.0_f64.sqrt());
1665    (0..n)
1666        .map(|i| {
1667            let z = 1.0 - 2.0 * (i as f64 + 0.5) / n as f64;
1668            let r = (1.0 - z * z).max(0.0).sqrt();
1669            let theta = golden * i as f64;
1670            [
1671                MEAN_EARTH_RADIUS_M * r * theta.cos(),
1672                MEAN_EARTH_RADIUS_M * r * theta.sin(),
1673                MEAN_EARTH_RADIUS_M * z,
1674                0.0,
1675            ]
1676        })
1677        .collect()
1678}
1679
1680fn select_coarse_candidate(candidates: &[ReceiverSolution]) -> Option<&ReceiverSolution> {
1681    candidates
1682        .iter()
1683        .filter(|solution| solution.metadata.converged && solution.metadata.redundancy >= 1)
1684        .min_by(|a, b| compare_coarse_candidates(a, b))
1685}
1686
1687fn compare_coarse_candidates(a: &ReceiverSolution, b: &ReceiverSolution) -> core::cmp::Ordering {
1688    b.used_sats
1689        .len()
1690        .cmp(&a.used_sats.len())
1691        .then_with(|| residual_rms(&a.residuals_m).total_cmp(&residual_rms(&b.residuals_m)))
1692        .then_with(|| candidate_gdop(a).total_cmp(&candidate_gdop(b)))
1693}
1694
1695fn candidate_gdop(solution: &ReceiverSolution) -> f64 {
1696    solution
1697        .dop
1698        .as_ref()
1699        .map(|dop| dop.gdop)
1700        .unwrap_or(f64::INFINITY)
1701}
1702
1703/// Root-mean-square of post-fit pseudorange residuals (0.0 when empty).
1704///
1705/// Exposed so language bindings can delegate residual-RMS reporting to the core
1706/// rather than recomputing the formula.
1707pub fn residual_rms(residuals: &[f64]) -> f64 {
1708    if residuals.is_empty() {
1709        return 0.0;
1710    }
1711    let sum_sq = residuals.iter().map(|r| r * r).sum::<f64>();
1712    (sum_sq / residuals.len() as f64).sqrt()
1713}
1714
1715fn redundancy(systems: &[GnssSystem], used_count: usize) -> isize {
1716    used_count as isize - (3 + systems.len() as isize)
1717}
1718
1719fn validate_solve_inputs(inputs: &SolveInputs) -> Result<(), SppError> {
1720    validate::finite(inputs.t_rx_j2000_s, "t_rx_j2000_s").map_err(map_input_error)?;
1721    validate::second_of_day(inputs.t_rx_second_of_day_s, "t_rx_second_of_day_s")
1722        .map_err(map_input_error)?;
1723    validate::finite_in_range_exclusive_upper(inputs.day_of_year, 1.0, 367.0, "day_of_year")
1724        .map_err(map_input_error)?;
1725    validate::finite_slice(&inputs.initial_guess, "initial_guess").map_err(map_input_error)?;
1726    validate_klobuchar(&inputs.klobuchar, "klobuchar")?;
1727    if let Some(klobuchar) = &inputs.beidou_klobuchar {
1728        validate_klobuchar(klobuchar, "beidou_klobuchar")?;
1729    }
1730    if let Some(nequick) = &inputs.galileo_nequick {
1731        validate_galileo_nequick(nequick)?;
1732    }
1733    if inputs.corrections.troposphere {
1734        validate_met(&inputs.met)?;
1735    }
1736    validate_observations(&inputs.observations)?;
1737    if let Some(robust) = inputs.robust {
1738        if robust.max_outer == 0 {
1739            return Err(SppError::InvalidInput {
1740                field: "robust.max_outer",
1741                kind: SppInputErrorKind::NotPositive,
1742            });
1743        }
1744        validate::finite_positive(robust.huber_k, "robust.huber_k").map_err(map_input_error)?;
1745        validate::finite_positive(robust.scale_floor_m, "robust.scale_floor_m")
1746            .map_err(map_input_error)?;
1747        validate::finite_positive(robust.outer_tol_m, "robust.outer_tol_m")
1748            .map_err(map_input_error)?;
1749    }
1750    Ok(())
1751}
1752
1753fn validate_klobuchar(coeffs: &KlobucharCoeffs, field: &'static str) -> Result<(), SppError> {
1754    validate::finite_slice(&coeffs.alpha, field).map_err(map_input_error)?;
1755    validate::finite_slice(&coeffs.beta, field).map_err(map_input_error)
1756}
1757
1758fn validate_galileo_nequick(coeffs: &GalileoNequickCoeffs) -> Result<(), SppError> {
1759    validate::finite(coeffs.ai0, "galileo_nequick").map_err(map_input_error)?;
1760    validate::finite(coeffs.ai1, "galileo_nequick").map_err(map_input_error)?;
1761    validate::finite(coeffs.ai2, "galileo_nequick").map_err(map_input_error)?;
1762    Ok(())
1763}
1764
1765fn validate_met(met: &SurfaceMet) -> Result<(), SppError> {
1766    validate::finite_positive(met.pressure_hpa, "met.pressure_hpa").map_err(map_input_error)?;
1767    validate::finite_positive(met.temperature_k, "met.temperature_k").map_err(map_input_error)?;
1768    validate::fraction(met.relative_humidity, "met.relative_humidity").map_err(map_input_error)?;
1769    Ok(())
1770}
1771
1772fn validate_observations(observations: &[Observation]) -> Result<(), SppError> {
1773    for obs in observations {
1774        validate::finite_positive(obs.pseudorange_m, "observation.pseudorange_m")
1775            .map_err(map_input_error)?;
1776    }
1777    Ok(())
1778}
1779
1780fn map_input_error(error: validate::FieldError) -> SppError {
1781    SppError::InvalidInput {
1782        field: error.field(),
1783        kind: SppInputErrorKind::from(&error),
1784    }
1785}
1786
1787fn map_robust_error(error: RobustError) -> SppError {
1788    let field = match error.field() {
1789        "scale_floor" => "robust.scale_floor_m",
1790        "residuals" | "values" => "robust.residuals",
1791        other => other,
1792    };
1793    let kind = match error.reason() {
1794        "not finite" => SppInputErrorKind::NonFinite,
1795        "not positive" => SppInputErrorKind::NotPositive,
1796        "negative" => SppInputErrorKind::Negative,
1797        "out of range" => SppInputErrorKind::OutOfRange,
1798        _ => SppInputErrorKind::OutOfRange,
1799    };
1800    SppError::InvalidInput { field, kind }
1801}
1802
1803/// The core km/deg geodetic recipe, for the boundary cross-check against the
1804/// meters-native helper.
1805#[cfg(all(test, sidereon_repo_tests))]
1806pub(crate) mod test_support {
1807    use super::*;
1808
1809    pub fn geodetic_from_ecef_m_for_test(x_m: f64, y_m: f64, z_m: f64) -> Wgs84Geodetic {
1810        geodetic_from_ecef(FrameRecipe::SppSkyfieldAuThreeIter, [x_m, y_m, z_m])
1811    }
1812
1813    pub fn sat_model_for_test(
1814        env: &SatModelEnv,
1815        sat: GnssSatelliteId,
1816        rx: [f64; 3],
1817        b_m: f64,
1818        p_meas: f64,
1819        klobuchar: &KlobucharCoeffs,
1820    ) -> Option<SatModel> {
1821        sat_model(
1822            env,
1823            sat,
1824            rx,
1825            b_m,
1826            p_meas,
1827            SppIonosphere::Klobuchar(*klobuchar),
1828        )
1829    }
1830
1831    pub fn sat_model_with_ionosphere_for_test(
1832        env: &SatModelEnv,
1833        sat: GnssSatelliteId,
1834        rx: [f64; 3],
1835        b_m: f64,
1836        p_meas: f64,
1837        ionosphere: SppIonosphere<'_>,
1838    ) -> Option<SatModel> {
1839        sat_model(env, sat, rx, b_m, p_meas, ionosphere)
1840    }
1841
1842    /// The core km/deg geodetic recipe (Skyfield AU-internal), returning the
1843    /// public `(lat_deg, lon_deg, alt_km)`, for the boundary cross-check.
1844    pub fn itrs_to_geodetic_core_km(x_km: f64, y_km: f64, z_km: f64) -> (f64, f64, f64) {
1845        crate::astro::frames::transforms::itrs_to_geodetic_compute(x_km, y_km, z_km)
1846            .expect("valid ITRS coordinates")
1847    }
1848}
1849
1850#[cfg(all(test, sidereon_repo_tests))]
1851mod tests;