Skip to main content

sidereon_core/astro/propagator/
decay.rs

1//! Orbital decay helper built on the numerical propagator and drag force.
2//!
3//! The scan samples geodetic altitude on a coarse elapsed-time grid and refines
4//! the first endpoint that falls at or below the reentry altitude. A coarse step
5//! longer than the time spent below the threshold at perigee can miss a brief
6//! crossing; callers should keep `scan_step_s` well below the orbital period,
7//! for example no more than about one twentieth of the period in near-circular
8//! decay cases.
9
10use crate::astro::atmosphere::MAX_ALTITUDE_KM;
11use crate::astro::constants::{J2_EARTH, MU_EARTH, RE_EARTH};
12use crate::astro::error::PropagationError;
13use crate::astro::events::root::{try_bisect_crossing_until, RootError};
14use crate::astro::forces::drag::geodetic_altitude_km;
15use crate::astro::forces::{DragForce, DragParameters};
16use crate::astro::propagator::api::IntegratorOptions;
17use crate::astro::propagator::driver::PropagationForceModel;
18use crate::astro::propagator::numerical::{ForceModelKind, IntegratorKind, StatePropagator};
19use crate::astro::state::CartesianState;
20
21/// Result of an orbital-lifetime estimate.
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub struct DecayEstimate {
24    /// Seconds from the initial epoch to reentry.
25    pub time_to_decay_s: f64,
26    /// State at reentry.
27    pub reentry_state: CartesianState,
28    /// Geodetic altitude at the reported state, km.
29    pub reentry_altitude_km: f64,
30}
31
32/// Configuration for a decay run.
33#[derive(Debug, Clone, Copy, PartialEq)]
34pub struct DecayConfig {
35    /// Gravity choice layered under drag.
36    pub force_model: PropagationForceModel,
37    /// Gravitational-parameter override, km^3/s^2.
38    pub mu_km3_s2: Option<f64>,
39    /// Numerical integrator.
40    pub integrator: IntegratorKind,
41    /// Integrator options.
42    pub options: IntegratorOptions,
43    /// Drag parameters.
44    pub drag: DragParameters,
45    /// Reentry threshold altitude, km.
46    pub reentry_altitude_km: f64,
47    /// Coarse scan step, s.
48    pub scan_step_s: f64,
49    /// Bisection time tolerance, s.
50    pub crossing_tolerance_s: f64,
51    /// Maximum elapsed scan horizon, s.
52    pub max_duration_s: f64,
53    /// Maximum number of coarse scan samples.
54    pub max_scan_samples: u32,
55}
56
57impl DecayConfig {
58    /// Default reentry altitude, km.
59    pub const DEFAULT_REENTRY_ALTITUDE_KM: f64 = DragForce::DEFAULT_REENTRY_ALTITUDE_KM;
60    /// Default coarse scan step, s.
61    pub const DEFAULT_SCAN_STEP_S: f64 = 60.0;
62    /// Default bisection time tolerance, s.
63    pub const DEFAULT_CROSSING_TOLERANCE_S: f64 = 1.0;
64    /// Default maximum coarse scan samples.
65    pub const DEFAULT_MAX_SCAN_SAMPLES: u32 = 200_000;
66    /// Default maximum elapsed scan horizon, s.
67    pub const DEFAULT_MAX_DURATION_S: f64 =
68        Self::DEFAULT_MAX_SCAN_SAMPLES as f64 * Self::DEFAULT_SCAN_STEP_S;
69
70    /// Build a decay config around required drag parameters.
71    pub fn new(drag: DragParameters) -> Self {
72        Self {
73            force_model: PropagationForceModel::default(),
74            mu_km3_s2: None,
75            integrator: IntegratorKind::Dp54,
76            options: IntegratorOptions::default(),
77            drag,
78            reentry_altitude_km: Self::DEFAULT_REENTRY_ALTITUDE_KM,
79            scan_step_s: Self::DEFAULT_SCAN_STEP_S,
80            crossing_tolerance_s: Self::DEFAULT_CROSSING_TOLERANCE_S,
81            max_duration_s: Self::DEFAULT_MAX_DURATION_S,
82            max_scan_samples: Self::DEFAULT_MAX_SCAN_SAMPLES,
83        }
84    }
85
86    /// Replace the gravity choice.
87    pub fn with_force_model(mut self, force_model: PropagationForceModel) -> Self {
88        self.force_model = force_model;
89        self
90    }
91
92    /// Replace the gravitational parameter override.
93    pub fn with_mu_km3_s2(mut self, mu_km3_s2: Option<f64>) -> Self {
94        self.mu_km3_s2 = mu_km3_s2;
95        self
96    }
97
98    /// Replace the integrator.
99    pub fn with_integrator(mut self, integrator: IntegratorKind) -> Self {
100        self.integrator = integrator;
101        self
102    }
103
104    /// Replace the integrator options.
105    pub fn with_options(mut self, options: IntegratorOptions) -> Self {
106        self.options = options;
107        self
108    }
109
110    /// Replace the reentry altitude, km.
111    pub fn with_reentry_altitude_km(mut self, reentry_altitude_km: f64) -> Self {
112        self.reentry_altitude_km = reentry_altitude_km;
113        self
114    }
115
116    /// Replace the coarse scan step, s.
117    pub fn with_scan_step_s(mut self, scan_step_s: f64) -> Self {
118        self.scan_step_s = scan_step_s;
119        self
120    }
121
122    /// Replace the crossing tolerance, s.
123    pub fn with_crossing_tolerance_s(mut self, crossing_tolerance_s: f64) -> Self {
124        self.crossing_tolerance_s = crossing_tolerance_s;
125        self
126    }
127
128    /// Replace the maximum scan horizon, s.
129    pub fn with_max_duration_s(mut self, max_duration_s: f64) -> Self {
130        self.max_duration_s = max_duration_s;
131        self
132    }
133
134    /// Replace the maximum coarse sample count.
135    pub fn with_max_scan_samples(mut self, max_scan_samples: u32) -> Self {
136        self.max_scan_samples = max_scan_samples;
137        self
138    }
139}
140
141/// Error returned by [`estimate_decay`].
142#[derive(Debug, Clone, thiserror::Error)]
143pub enum DecayError {
144    /// Propagation or altitude evaluation failed.
145    #[error("propagation failed: {0}")]
146    Propagation(PropagationError),
147    /// A config field was out of domain.
148    #[error("invalid decay config field: {0}")]
149    InvalidConfig(&'static str),
150    /// The orbit did not decay within the requested horizon.
151    #[error("no decay within horizon {horizon_s} s")]
152    NoDecayWithinHorizon { horizon_s: f64 },
153    /// The coarse scan hit its sample budget before the horizon.
154    #[error("scan budget exhausted after {samples} samples and {scanned_s} s")]
155    ScanBudgetExhausted { scanned_s: f64, samples: u32 },
156}
157
158/// Estimate time to reentry using drag-perturbed numerical propagation.
159pub fn estimate_decay(
160    initial: CartesianState,
161    config: &DecayConfig,
162) -> Result<DecayEstimate, DecayError> {
163    validate_config(config)?;
164
165    let initial_altitude = geodetic_altitude_km(&initial).map_err(DecayError::Propagation)?;
166    if initial_altitude <= config.reentry_altitude_km {
167        return Ok(DecayEstimate {
168            time_to_decay_s: 0.0,
169            reentry_state: initial,
170            reentry_altitude_km: initial_altitude,
171        });
172    }
173
174    let initial_epoch = initial.epoch_tdb_seconds;
175    let mut elapsed_s = 0.0;
176    let mut samples = 0_u32;
177    let mut current = initial;
178
179    loop {
180        if elapsed_s >= config.max_duration_s {
181            return Err(DecayError::NoDecayWithinHorizon {
182                horizon_s: config.max_duration_s,
183            });
184        }
185        if samples >= config.max_scan_samples {
186            return Err(DecayError::ScanBudgetExhausted {
187                scanned_s: elapsed_s,
188                samples,
189            });
190        }
191
192        let next_elapsed_s = (elapsed_s + config.scan_step_s).min(config.max_duration_s);
193        let next_state = propagate_segment(current, initial_epoch + next_elapsed_s, config)?;
194        samples += 1;
195        let next_altitude = geodetic_altitude_km(&next_state).map_err(DecayError::Propagation)?;
196
197        if next_altitude <= config.reentry_altitude_km {
198            return refine_reentry(initial_epoch, current, elapsed_s, next_elapsed_s, config);
199        }
200
201        elapsed_s = next_elapsed_s;
202        current = next_state;
203    }
204}
205
206fn validate_config(config: &DecayConfig) -> Result<(), DecayError> {
207    validate_positive("scan_step_s", config.scan_step_s)?;
208    validate_positive("crossing_tolerance_s", config.crossing_tolerance_s)?;
209    validate_positive("max_duration_s", config.max_duration_s)?;
210    if config.max_scan_samples == 0 {
211        return Err(DecayError::InvalidConfig("max_scan_samples"));
212    }
213    if !config.reentry_altitude_km.is_finite() {
214        return Err(DecayError::InvalidConfig("reentry_altitude_km"));
215    }
216    if !(0.0..=MAX_ALTITUDE_KM).contains(&config.reentry_altitude_km) {
217        return Err(DecayError::InvalidConfig("reentry_altitude_km"));
218    }
219    if config.reentry_altitude_km < config.drag.cutoff_altitude_km() {
220        return Err(DecayError::InvalidConfig("reentry_altitude_km"));
221    }
222    Ok(())
223}
224
225fn validate_positive(field: &'static str, value: f64) -> Result<(), DecayError> {
226    if !value.is_finite() || value <= 0.0 {
227        return Err(DecayError::InvalidConfig(field));
228    }
229    Ok(())
230}
231
232fn refine_reentry(
233    initial_epoch: f64,
234    low_state: CartesianState,
235    low_elapsed_s: f64,
236    high_elapsed_s: f64,
237    config: &DecayConfig,
238) -> Result<DecayEstimate, DecayError> {
239    let threshold = config.reentry_altitude_km;
240    let root_elapsed_s = try_bisect_crossing_until(
241        low_elapsed_s,
242        high_elapsed_s,
243        |elapsed_s| {
244            let state = propagate_segment(low_state, initial_epoch + elapsed_s, config)?;
245            let altitude = geodetic_altitude_km(&state).map_err(DecayError::Propagation)?;
246            Ok(altitude - threshold)
247        },
248        |lo, hi| (lo + hi) * 0.5,
249        |lo, hi| (hi - lo).abs() <= config.crossing_tolerance_s,
250    )
251    .map_err(map_root_error)?;
252
253    let reentry_state = propagate_segment(low_state, initial_epoch + root_elapsed_s, config)?;
254    let reentry_altitude_km =
255        geodetic_altitude_km(&reentry_state).map_err(DecayError::Propagation)?;
256    Ok(DecayEstimate {
257        time_to_decay_s: reentry_state.epoch_tdb_seconds - initial_epoch,
258        reentry_state,
259        reentry_altitude_km,
260    })
261}
262
263fn map_root_error(error: RootError<DecayError>) -> DecayError {
264    match error {
265        RootError::Predicate(error) => error,
266        RootError::InvalidInput { field, reason } => DecayError::Propagation(
267            PropagationError::NumericalFailure(format!("drag decay bisection {field} {reason}")),
268        ),
269    }
270}
271
272fn propagate_segment(
273    initial: CartesianState,
274    t_end_tdb_seconds: f64,
275    config: &DecayConfig,
276) -> Result<CartesianState, DecayError> {
277    let propagator = StatePropagator {
278        initial,
279        force_model: force_model_kind(config),
280        integrator: config.integrator,
281        options: config.options,
282        drag: Some(config.drag),
283    };
284    Ok(propagator
285        .propagate_to(t_end_tdb_seconds)
286        .map_err(DecayError::Propagation)?
287        .final_state)
288}
289
290fn force_model_kind(config: &DecayConfig) -> ForceModelKind {
291    let mu_km3_s2 = config.mu_km3_s2.unwrap_or(MU_EARTH);
292    match config.force_model {
293        PropagationForceModel::TwoBody => ForceModelKind::TwoBody { mu_km3_s2 },
294        PropagationForceModel::TwoBodyJ2 => ForceModelKind::TwoBodyJ2 {
295            mu_km3_s2,
296            re_km: RE_EARTH,
297            j2: J2_EARTH,
298        },
299    }
300}
301
302#[cfg(test)]
303mod tests {
304    use super::*;
305    use crate::astro::constants::RE_EARTH;
306    use crate::astro::forces::SpaceWeather;
307    use crate::astro::frames::transforms::{
308        geodetic_to_itrs, greenwich_mean_sidereal_time_radians_from_j2000_seconds,
309    };
310
311    const TEST_EPOCH_S: f64 = 646_315_200.0;
312
313    fn state_from_geodetic_alt(epoch: f64, altitude_km: f64) -> CartesianState {
314        let (x_ecef, y_ecef, z_ecef) =
315            geodetic_to_itrs(0.0, 0.0, altitude_km).expect("valid geodetic");
316        let theta =
317            greenwich_mean_sidereal_time_radians_from_j2000_seconds(epoch).expect("valid gmst");
318        let c = theta.cos();
319        let s = theta.sin();
320        let x_eci = c * x_ecef - s * y_ecef;
321        let y_eci = s * x_ecef + c * y_ecef;
322        let r = RE_EARTH + altitude_km;
323        let v = (MU_EARTH / r).sqrt();
324        CartesianState::new(
325            epoch,
326            [x_eci, y_eci, z_ecef],
327            [-v * y_eci / r, v * x_eci / r, 0.0],
328        )
329    }
330
331    fn decay_drag() -> DragParameters {
332        DragParameters::from_bc_factor_m2_kg(
333            0.8,
334            SpaceWeather::default(),
335            DragForce::DEFAULT_REENTRY_ALTITUDE_KM,
336        )
337        .expect("valid drag")
338    }
339
340    fn decay_options() -> IntegratorOptions {
341        IntegratorOptions {
342            abs_tol: 1.0e-8,
343            rel_tol: 1.0e-10,
344            initial_step: 5.0,
345            min_step: 1.0e-6,
346            max_step: 30.0,
347            max_steps: 200_000,
348            dense_output: false,
349        }
350    }
351
352    fn base_config() -> DecayConfig {
353        DecayConfig::new(decay_drag())
354            .with_options(decay_options())
355            .with_scan_step_s(60.0)
356            .with_crossing_tolerance_s(2.0)
357            .with_max_duration_s(50_000.0)
358            .with_max_scan_samples(2_000)
359    }
360
361    #[test]
362    fn estimate_decay_brackets_and_refines_reentry() {
363        let initial = state_from_geodetic_alt(TEST_EPOCH_S, 125.0);
364        let config = base_config();
365        let estimate = estimate_decay(initial, &config).expect("decays within horizon");
366
367        assert!(estimate.time_to_decay_s > 0.0);
368        assert_eq!(
369            estimate.time_to_decay_s.to_bits(),
370            (estimate.reentry_state.epoch_tdb_seconds - initial.epoch_tdb_seconds).to_bits()
371        );
372        let before = propagate_segment(
373            initial,
374            initial.epoch_tdb_seconds + estimate.time_to_decay_s - config.crossing_tolerance_s,
375            &config,
376        )
377        .expect("before crossing");
378        let after = propagate_segment(
379            initial,
380            initial.epoch_tdb_seconds + estimate.time_to_decay_s + config.crossing_tolerance_s,
381            &config,
382        )
383        .expect("after crossing");
384        let before_alt = geodetic_altitude_km(&before).expect("before altitude");
385        let after_alt = geodetic_altitude_km(&after).expect("after altitude");
386        let altitude_window_km = (before_alt - after_alt).abs().max(1.0e-6);
387        assert!(
388            (estimate.reentry_altitude_km - config.reentry_altitude_km).abs() <= altitude_window_km
389        );
390
391        let high = state_from_geodetic_alt(TEST_EPOCH_S, 700.0);
392        let no_decay = base_config()
393            .with_max_duration_s(120.0)
394            .with_max_scan_samples(10);
395        match estimate_decay(high, &no_decay).expect_err("short horizon should stop") {
396            DecayError::NoDecayWithinHorizon { horizon_s } => assert_eq!(horizon_s, 120.0),
397            other => panic!("expected horizon stop, got {other:?}"),
398        }
399
400        let budget = base_config()
401            .with_max_duration_s(10_000.0)
402            .with_max_scan_samples(2);
403        match estimate_decay(high, &budget).expect_err("sample budget should stop") {
404            DecayError::ScanBudgetExhausted { scanned_s, samples } => {
405                assert_eq!(scanned_s, 120.0);
406                assert_eq!(samples, 2);
407            }
408            other => panic!("expected scan budget stop, got {other:?}"),
409        }
410    }
411
412    #[test]
413    fn estimate_decay_zero_when_initial_below_threshold() {
414        let initial = state_from_geodetic_alt(TEST_EPOCH_S, 90.0);
415        let estimate = estimate_decay(initial, &base_config()).expect("initial is below threshold");
416
417        assert_eq!(estimate.time_to_decay_s, 0.0);
418        assert_eq!(estimate.reentry_state, initial);
419        assert!(estimate.reentry_altitude_km <= DecayConfig::DEFAULT_REENTRY_ALTITUDE_KM);
420    }
421}