siderust 0.7.0

High-precision astronomy and satellite mechanics in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon

//! # Conic Orbit Models
//!
//! ## Scientific scope
//!
//! This module provides orbit model types for the two cases where the
//! plain elliptic six-element [`KeplerianOrbit`](crate::astro::orbit::KeplerianOrbit)
//! is not the right semantic container:
//!
//! - **[`ConicOrbit`]** — a unified conic (elliptic or hyperbolic) expressed
//!   using **periapsis distance** as the shape parameter.  Used for comets,
//!   near-parabolic intruders, and any body whose eccentricity is not
//!   definitively below 1.
//!
//! - **[`MeanMotionOrbit`]** — an elliptic orbit where the **mean daily
//!   motion** is the authoritative element and the epoch corresponds to zero
//!   mean anomaly.  Used for minor planets from the MPC catalog where mean
//!   motion is the directly fitted quantity.
//!
//! The reusable conic geometry (shape + orientation abstractions) lives in
//! `affn::conic`.  This module layers epoch and anomaly semantics on top.
//!
//! ## Technical scope
//!
//! - [`ConicOrbit`] — validated [`OrientedConic<PeriapsisParam<AU>, Ecliptic>`]
//!   plus mean anomaly at epoch and reference epoch.
//! - [`MeanMotionOrbit`] — validated elliptic oriented conic plus typed mean
//!   motion [`AngularRate<Degree, Day>`] and epoch.  The mean motion field
//!   `pub mean_motion: AngularRate<Degree, Day>` replaces the old untyped
//!   `mean_motion_deg_per_day: f64`; callers needing a raw `f64` call
//!   `.mean_motion.value()`.
//! - [`ConicError`] — unified validation error enum for both types.
//!
//! ## References
//!
//! - Meeus, J. (1998). *Astronomical Algorithms* (2nd ed.). Willmann-Bell.
//! - Minor Planet Center. *MPC Orbit (MPCORB) Database*.
//!   <https://minorplanetcenter.net/iau/MPCORB.html>

use crate::qtty::angular_rate::AngularRate;
use crate::qtty::*;
use crate::time::JulianDate;
use affn::conic::{
    ClassifiedSemiMajorAxisParam, ConicOrientation, ConicValidationError, Elliptic, OrientedConic,
    PeriapsisParam, SemiMajorAxisParam, TypedSemiMajorAxisParam,
};
use affn::frames::EclipticMeanJ2000;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub use affn::conic::ConicKind;

/// Validation and propagation errors for conic-based orbit models.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ConicError {
    /// Eccentricity must be finite and non-negative.
    InvalidEccentricity,
    /// Semi-major axis must be finite and strictly positive.
    InvalidSemiMajorAxis,
    /// Periapsis distance must be finite and strictly positive.
    InvalidPeriapsisDistance,
    /// Semi-major axis is undefined for parabolic conics (`e == 1`).
    ParabolicSemiMajorAxis,
    /// Orientation angles must be finite.
    InvalidOrientation,
    /// Parabolic orbits are intentionally not supported yet.
    ParabolicUnsupported,
    /// Hyperbolic eccentricity (`e ≥ 1`) is not valid for this orbit type.
    ///
    /// [`KeplerianOrbit`](crate::astro::orbit::KeplerianOrbit) and
    /// [`MeanMotionOrbit`] only support elliptic orbits (`0 ≤ e < 1`).
    /// Use [`ConicOrbit`] for hyperbolic trajectories.
    HyperbolicNotSupported,
    /// Epoch must be finite (not NaN or infinity).
    InvalidEpoch,
    /// Mean anomaly at epoch must be finite.
    InvalidMeanAnomaly,
    /// Mean motion must be finite and positive.
    InvalidMeanMotion,
    /// The hyperbolic anomaly solver failed to converge.
    HyperbolicSolverFailed,
    /// A parameter value is outside its valid range.
    OutOfRange {
        /// Name of the out-of-range field.
        field: &'static str,
        /// The offending value.
        value: f64,
    },
}

impl std::fmt::Display for ConicError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidEccentricity => write!(f, "invalid eccentricity"),
            Self::InvalidSemiMajorAxis => write!(f, "invalid semi-major axis"),
            Self::InvalidPeriapsisDistance => write!(f, "invalid periapsis distance"),
            Self::ParabolicSemiMajorAxis => {
                write!(
                    f,
                    "semi-major axis is undefined for parabolic conics (e == 1)"
                )
            }
            Self::InvalidOrientation => write!(f, "orientation angles must be finite"),
            Self::ParabolicUnsupported => write!(f, "parabolic orbits are not supported"),
            Self::HyperbolicNotSupported => {
                write!(
                    f,
                    "hyperbolic eccentricity (e >= 1) is not supported by this orbit type; \
                     use ConicOrbit instead"
                )
            }
            Self::InvalidEpoch => write!(f, "epoch must be finite"),
            Self::InvalidMeanAnomaly => write!(f, "mean anomaly at epoch must be finite"),
            Self::InvalidMeanMotion => write!(f, "mean motion must be finite and positive"),
            Self::HyperbolicSolverFailed => {
                write!(f, "hyperbolic anomaly solver failed to converge")
            }
            Self::OutOfRange { field, value } => {
                write!(f, "parameter '{field}' has out-of-range value {value}")
            }
        }
    }
}

impl std::error::Error for ConicError {}

pub(crate) fn map_validation_error(error: ConicValidationError) -> ConicError {
    match error {
        ConicValidationError::InvalidEccentricity => ConicError::InvalidEccentricity,
        ConicValidationError::InvalidSemiMajorAxis => ConicError::InvalidSemiMajorAxis,
        ConicValidationError::InvalidPeriapsisDistance => ConicError::InvalidPeriapsisDistance,
        ConicValidationError::ParabolicSemiMajorAxis => ConicError::ParabolicSemiMajorAxis,
        ConicValidationError::InvalidOrientation => ConicError::InvalidOrientation,
        ConicValidationError::OutOfRange { .. } => ConicError::InvalidOrientation,
    }
}

// =============================================================================
// ConicOrbit
// =============================================================================

/// Unified conic elements expressed using periapsis distance.
///
/// The conic geometry is stored as a validated
/// [`OrientedConic<PeriapsisParam<AstronomicalUnit>, EclipticMeanJ2000>`].
/// After construction the geometry is always valid; `kind()` is infallible.
///
/// Supports elliptic and hyperbolic orbits. Parabolic eccentricity (`e == 1`)
/// is rejected at construction time by [`try_new`](Self::try_new).
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ConicOrbit {
    geometry: OrientedConic<PeriapsisParam<AstronomicalUnit>, EclipticMeanJ2000>,
    /// Mean anomaly at `epoch`.
    pub mean_anomaly_at_epoch: Degrees,
    /// Reference epoch.
    pub epoch: JulianDate,
}

impl ConicOrbit {
    /// Creates a new validated conic-element set.
    ///
    /// Returns an error if any parameter is invalid (non-positive periapsis
    /// distance, non-finite eccentricity, non-finite orientation angles,
    /// non-finite mean anomaly or epoch, or parabolic eccentricity).
    pub fn try_new(
        periapsis_distance: AstronomicalUnits,
        eccentricity: f64,
        inclination: Degrees,
        longitude_of_ascending_node: Degrees,
        argument_of_periapsis: Degrees,
        mean_anomaly_at_epoch: Degrees,
        epoch: JulianDate,
    ) -> Result<Self, ConicError> {
        let shape = PeriapsisParam::try_new(periapsis_distance, eccentricity)
            .map_err(map_validation_error)?;
        if eccentricity == 1.0 {
            return Err(ConicError::ParabolicUnsupported);
        }
        let orientation = ConicOrientation::try_new(
            inclination,
            longitude_of_ascending_node,
            argument_of_periapsis,
        )
        .map_err(map_validation_error)?;
        if !mean_anomaly_at_epoch.is_finite() {
            return Err(ConicError::InvalidMeanAnomaly);
        }
        if !epoch.jd_value().is_finite() {
            return Err(ConicError::InvalidEpoch);
        }
        Ok(Self {
            geometry: OrientedConic::new(shape, orientation),
            mean_anomaly_at_epoch,
            epoch,
        })
    }

    /// Creates a new conic-element set **without validation**.
    ///
    /// Intended for compile-time body constants with known-correct values.
    pub const fn new_unchecked(
        periapsis_distance: AstronomicalUnits,
        eccentricity: f64,
        inclination: Degrees,
        longitude_of_ascending_node: Degrees,
        argument_of_periapsis: Degrees,
        mean_anomaly_at_epoch: Degrees,
        epoch: JulianDate,
    ) -> Self {
        Self {
            geometry: OrientedConic::new(
                PeriapsisParam::new_unchecked(periapsis_distance, eccentricity),
                ConicOrientation::new(
                    inclination,
                    longitude_of_ascending_node,
                    argument_of_periapsis,
                ),
            ),
            mean_anomaly_at_epoch,
            epoch,
        }
    }

    /// The validated oriented geometry.
    #[inline]
    pub fn geometry(&self) -> &OrientedConic<PeriapsisParam<AstronomicalUnit>, EclipticMeanJ2000> {
        &self.geometry
    }

    /// Classifies the orbit from its eccentricity. Infallible.
    pub fn kind(&self) -> ConicKind {
        self.geometry.kind()
    }
}

// =============================================================================
// MeanMotionOrbit
// =============================================================================

/// Mean-motion-driven elliptic elements.
///
/// This is intentionally distinct from [`crate::astro::orbit::KeplerianOrbit`].
/// Here the stored mean daily motion is authoritative and the epoch corresponds
/// to zero mean anomaly.
///
/// Stores `OrientedConic<TypedSemiMajorAxisParam<AstronomicalUnit, Elliptic>,
/// EclipticMeanJ2000>` — elliptic geometry is enforced at construction time.
///
/// The mean motion is typed as [`AngularRate<Degree, Day>`] so callers cannot
/// accidentally use it as radians-per-day or confuse it with other rates.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MeanMotionOrbit {
    geometry: OrientedConic<TypedSemiMajorAxisParam<AstronomicalUnit, Elliptic>, EclipticMeanJ2000>,
    /// Mean motion (degrees per day), typed.
    pub mean_motion: AngularRate<Degree, Day>,
    /// Epoch at which the mean anomaly is defined to be zero.
    pub epoch: JulianDate,
}

impl MeanMotionOrbit {
    /// Creates a new validated elliptic mean-motion orbit.
    ///
    /// Returns an error if any parameter is invalid, if the eccentricity is
    /// not elliptic (`e >= 1`), or if the mean motion or epoch is non-finite.
    pub fn try_new(
        semi_major_axis: AstronomicalUnits,
        eccentricity: f64,
        inclination: Degrees,
        longitude_of_ascending_node: Degrees,
        argument_of_periapsis: Degrees,
        mean_motion: AngularRate<Degree, Day>,
        epoch: JulianDate,
    ) -> Result<Self, ConicError> {
        let sma = SemiMajorAxisParam::try_new(semi_major_axis, eccentricity)
            .map_err(map_validation_error)?;
        let typed = match sma.classify() {
            ClassifiedSemiMajorAxisParam::Elliptic(t) => t,
            ClassifiedSemiMajorAxisParam::Hyperbolic(_) => {
                return Err(ConicError::HyperbolicNotSupported);
            }
        };
        let orientation = ConicOrientation::try_new(
            inclination,
            longitude_of_ascending_node,
            argument_of_periapsis,
        )
        .map_err(map_validation_error)?;
        if !mean_motion.value().is_finite() || mean_motion.value() <= 0.0 {
            return Err(ConicError::InvalidMeanMotion);
        }
        if !epoch.jd_value().is_finite() {
            return Err(ConicError::InvalidEpoch);
        }
        Ok(Self {
            geometry: OrientedConic::new(typed, orientation),
            mean_motion,
            epoch,
        })
    }

    /// Creates a new mean-motion orbit **without validation**.
    ///
    /// Intended for compile-time body constants with known-correct elliptic values.
    pub const fn new_unchecked(
        semi_major_axis: AstronomicalUnits,
        eccentricity: f64,
        inclination: Degrees,
        longitude_of_ascending_node: Degrees,
        argument_of_periapsis: Degrees,
        mean_motion: AngularRate<Degree, Day>,
        epoch: JulianDate,
    ) -> Self {
        Self {
            geometry: OrientedConic::new(
                TypedSemiMajorAxisParam::new_unchecked(SemiMajorAxisParam::new_unchecked(
                    semi_major_axis,
                    eccentricity,
                )),
                ConicOrientation::new(
                    inclination,
                    longitude_of_ascending_node,
                    argument_of_periapsis,
                ),
            ),
            mean_motion,
            epoch,
        }
    }

    /// The validated oriented geometry.
    #[inline]
    pub fn geometry(
        &self,
    ) -> &OrientedConic<TypedSemiMajorAxisParam<AstronomicalUnit, Elliptic>, EclipticMeanJ2000>
    {
        &self.geometry
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn classify_elliptic() {
        let orbit = ConicOrbit::try_new(
            1.0 * AU,
            0.5,
            Degrees::new(0.0),
            Degrees::new(0.0),
            Degrees::new(0.0),
            Degrees::new(0.0),
            JulianDate::J2000,
        )
        .unwrap();
        assert_eq!(orbit.kind(), ConicKind::Elliptic);
    }

    #[test]
    fn classify_hyperbolic() {
        let orbit = ConicOrbit::try_new(
            1.0 * AU,
            1.5,
            Degrees::new(0.0),
            Degrees::new(0.0),
            Degrees::new(0.0),
            Degrees::new(0.0),
            JulianDate::J2000,
        )
        .unwrap();
        assert_eq!(orbit.kind(), ConicKind::Hyperbolic);
    }

    #[test]
    fn negative_eccentricity_is_invalid() {
        assert_eq!(
            ConicOrbit::try_new(
                1.0 * AU,
                -0.1,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                JulianDate::J2000,
            ),
            Err(ConicError::InvalidEccentricity)
        );
    }

    #[test]
    fn mean_motion_rejects_hyperbolic() {
        assert_eq!(
            MeanMotionOrbit::try_new(
                1.0 * AU,
                1.1,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                AngularRate::<Degree, Day>::new(1.0),
                JulianDate::J2000,
            ),
            Err(ConicError::HyperbolicNotSupported)
        );
    }

    #[test]
    fn conic_rejects_nan_epoch() {
        assert_eq!(
            ConicOrbit::try_new(
                1.0 * AU,
                0.5,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                JulianDate::new(f64::NAN),
            ),
            Err(ConicError::InvalidEpoch)
        );
    }

    #[test]
    fn conic_rejects_inf_mean_anomaly() {
        assert_eq!(
            ConicOrbit::try_new(
                1.0 * AU,
                0.5,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(f64::INFINITY),
                JulianDate::J2000,
            ),
            Err(ConicError::InvalidMeanAnomaly)
        );
    }

    #[test]
    fn conic_rejects_parabolic() {
        assert_eq!(
            ConicOrbit::try_new(
                1.0 * AU,
                1.0,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                JulianDate::J2000,
            ),
            Err(ConicError::ParabolicUnsupported)
        );
    }

    #[test]
    fn mean_motion_rejects_nan_mean_motion() {
        assert_eq!(
            MeanMotionOrbit::try_new(
                1.0 * AU,
                0.5,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                AngularRate::<Degree, Day>::new(f64::NAN),
                JulianDate::J2000,
            ),
            Err(ConicError::InvalidMeanMotion)
        );
    }

    #[test]
    fn mean_motion_rejects_negative_mean_motion() {
        assert_eq!(
            MeanMotionOrbit::try_new(
                1.0 * AU,
                0.5,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                AngularRate::<Degree, Day>::new(-1.0),
                JulianDate::J2000,
            ),
            Err(ConicError::InvalidMeanMotion)
        );
    }

    #[test]
    fn mean_motion_rejects_nan_epoch() {
        assert_eq!(
            MeanMotionOrbit::try_new(
                1.0 * AU,
                0.5,
                Degrees::new(0.0),
                Degrees::new(0.0),
                Degrees::new(0.0),
                AngularRate::<Degree, Day>::new(1.0),
                JulianDate::new(f64::NAN),
            ),
            Err(ConicError::InvalidEpoch)
        );
    }
}