outfit 4.1.0

Orbit determination toolkit in Rust. Provides astrometric parsing, observer management, and initial orbit determination (Gauss method) with JPL ephemeris support.
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
//! Generalized Stumpff-like auxiliary functions for the universal-variable
//! formulation of the two-body problem.

/// Compute the generalized Stumpff-like auxiliary functions (s0, s1, s2, s3)
/// used in the **universal variable formulation** of the two-body problem.
///
/// These functions extend the classical Stumpff functions `C(z), S(z)` and
/// appear in semi-analytical formulations such as the **f–g series** for
/// universal Kepler propagation. They allow one to compute position and
/// velocity vectors from the *universal anomaly* `ψ` across all orbit types
/// (elliptic, parabolic, hyperbolic).
///
/// Scientific background
/// ---------------------
/// Let α = 2E (twice the specific orbital energy). Define β = α ψ².
/// The generalized series are:
///
/// * s0(ψ, α) ≈ 1 + α Σ (cosine-like),
/// * s1(ψ, α) ≈ ψ + α Σ (sine-like),
/// * s2 = (s0 − 1) / α,
/// * s3 = (s1 − ψ) / α.
///
/// They enter the f–g Lagrange coefficients:
///
/// ```text
/// f = 1 − (μ/r0) · s2
/// g = Δt − μ · s3
/// ```
///
/// Algorithm
/// ---------
/// Two regimes are used depending on the size of `β = α ψ²`:
///
/// **CASE 1 – Small |β| (rapidly convergent):**
///   * Expand s2 and s3 by power series in ψ.
///   * Reconstruct s0 and s1 via defining relations.
///   * This is efficient and stable for |β| < BETA_SERIES_THRESHOLD (≈ 100).
///
/// **CASE 2 – Large |β| (poor convergence):**
///   1. Halve ψ until |β| is small enough (β → β/4 per halving).
///   2. Evaluate s0 and s1 by series expansion at the reduced ψ.
///   3. Apply *duplication formulas* recursively to scale back up:
///      - s0(2ψ) = 2 s0(ψ)² − 1   (analogue of cos(2x))
///      - s1(2ψ) = 2 s0(ψ)·s1(ψ)  (analogue of sin(2x))
///   4. Recover s2 and s3 from their definitions.
///   * This ensures stability but s2, s3 may lose precision due to subtraction.
///
/// Arguments
/// ---------
/// * `psi`   – Universal anomaly (integration parameter).
/// * `alpha` – Twice the specific orbital energy (2·E). Negative for elliptic,
///   zero for parabolic, positive for hyperbolic motion.
///
/// Return
/// ----------
/// * `(s0, s1, s2, s3)` – Tuple of the four auxiliary functions.
///   - `s0` : cosine-like series,
///   - `s1` : sine-like series,
///   - `s2` : (s0 − 1)/α,
///   - `s3` : (s1 − ψ)/α.
///
/// Remarks
/// ----------
/// * For parabolic motion (α = 0), the limiting form should be used (not handled here).
/// * For very large `|α ψ²|`, reconstruction of s2, s3 involves subtracting
///   large numbers and may lose accuracy.
///
/// References
/// ----------
/// * Everhart, E. & Pitkin, E.T., *American Journal of Physics*, 51(8), 712–717 (1983).
/// * Goodyear, W.H., *Astronomical Journal*, 70, 189–192 (1965).
/// * Battin, R.H., *An Introduction to the Mathematics and Methods of Astrodynamics*.
///
/// # See also
/// * [`velocity_correction`](crate::kepler::velocity_correction) – Uses these functions in the f–g propagation.
/// * [`solve_kepuni`](crate::kepler::solve_kepuni) – Universal Kepler solver relying on these functions.
#[inline(always)]
pub fn s_funct(psi: f64, alpha: f64) -> (f64, f64, f64, f64) {
    // Maximum number of series terms (guards against slow convergence).
    const MAX_SERIES_TERMS: usize = 70;
    // Maximum number of halving iterations for the duplication strategy.
    const MAX_HALVING_STEPS: usize = 30;
    // Threshold between the "small beta" and "large beta" regimes.
    const BETA_SERIES_THRESHOLD: f64 = 100.0;

    let machine_epsilon = f64::EPSILON;
    // Series terms smaller than this are considered negligible.
    let series_term_convergence_tolerance = 100.0 * machine_epsilon;
    // Series terms larger than this indicate divergence: abort early.
    let series_term_overflow_limit = 1.0 / machine_epsilon;

    // Fast path: psi = 0 gives trivial, exact values.
    if psi == 0.0 {
        return (1.0, 0.0, 0.0, 0.0);
    }

    // Core expansion parameter: beta = alpha * psi^2.
    let psi_squared = psi * psi;
    let beta = alpha * psi_squared;

    if beta.abs() < BETA_SERIES_THRESHOLD {
        // CASE 1: direct power-series expansion (fast and stable for small |beta|).
        compute_stumpff_via_power_series(
            psi,
            psi_squared,
            beta,
            alpha,
            series_term_convergence_tolerance,
            series_term_overflow_limit,
            MAX_SERIES_TERMS,
        )
    } else {
        // CASE 2: halve psi until |beta| is small, expand there, then scale
        // back up using duplication formulas (stable for large |beta|).
        compute_stumpff_via_halving_and_duplication(
            psi,
            beta,
            alpha,
            series_term_convergence_tolerance,
            series_term_overflow_limit,
            BETA_SERIES_THRESHOLD,
            MAX_HALVING_STEPS,
            MAX_SERIES_TERMS,
        )
    }
}

/// Evaluate `(s0, s1, s2, s3)` by direct power-series expansion in `psi`.
///
/// This branch is used when `|beta| = |alpha * psi^2|` is small enough for
/// the series to converge quickly and safely. `s2` and `s3` are expanded
/// directly; `s0` and `s1` are then recovered from the defining relations
/// `s0 = 1 + alpha * s2` and `s1 = psi + alpha * s3`.
#[allow(clippy::too_many_arguments)]
fn compute_stumpff_via_power_series(
    psi: f64,
    psi_squared: f64,
    beta: f64,
    alpha: f64,
    convergence_tolerance: f64,
    overflow_limit: f64,
    max_terms: usize,
) -> (f64, f64, f64, f64) {
    // Series initialization: s2 = psi^2/2, s3 = psi^3/6.
    let mut s2 = 0.5 * psi_squared;
    let mut series_term_s2 = s2;

    let mut s3 = (s2 * psi) / 3.0;
    let mut series_term_s3 = s3;

    // Denominators for the recurrence evolve as (3·4), (5·6), ... for s2,
    // and (4·5), (6·7), ... for s3.
    let mut denominator_s2_low = 3.0;
    let mut denominator_s2_high = 4.0;
    let mut denominator_s3_low = 4.0;
    let mut denominator_s3_high = 5.0;

    for _ in 1..=max_terms {
        // Next correction term for s2.
        series_term_s2 *= beta / (denominator_s2_low * denominator_s2_high);
        s2 += series_term_s2;

        // Next correction term for s3.
        series_term_s3 *= beta / (denominator_s3_low * denominator_s3_high);
        s3 += series_term_s3;

        // Early exit once both terms are negligible, or if either diverges.
        let term_s2_is_negligible = series_term_s2.abs() < convergence_tolerance;
        let term_s3_is_negligible = series_term_s3.abs() < convergence_tolerance;
        let term_s2_is_diverging = series_term_s2.abs() > overflow_limit;
        let term_s3_is_diverging = series_term_s3.abs() > overflow_limit;

        if (term_s2_is_negligible && term_s3_is_negligible)
            || term_s2_is_diverging
            || term_s3_is_diverging
        {
            break;
        }

        // Advance denominators by 2 at each step.
        denominator_s2_low += 2.0;
        denominator_s2_high += 2.0;
        denominator_s3_low += 2.0;
        denominator_s3_high += 2.0;
    }

    // Recover s1 and s0 via their defining relations.
    let s1 = psi + alpha * s3;
    let s0 = 1.0 + alpha * s2;
    (s0, s1, s2, s3)
}

/// Evaluate `(s0, s1, s2, s3)` for large `|beta|` via halving + duplication.
///
/// The universal anomaly `psi` is repeatedly halved until `|beta|` falls
/// below `beta_threshold`, where a direct series expansion of `s0` and `s1`
/// is safe. The result is then scaled back to the original `psi` using
/// cosine/sine-like duplication formulas, and `s2`, `s3` are reconstructed
/// from their defining relations.
#[allow(clippy::too_many_arguments)]
fn compute_stumpff_via_halving_and_duplication(
    psi: f64,
    beta: f64,
    alpha: f64,
    convergence_tolerance: f64,
    overflow_limit: f64,
    beta_threshold: f64,
    max_halving_steps: usize,
    max_terms: usize,
) -> (f64, f64, f64, f64) {
    let (reduced_psi, reduced_beta, halving_count) =
        reduce_psi_until_beta_is_small(psi, beta, beta_threshold, max_halving_steps);

    let (mut s0, mut s1) = expand_s0_s1_series_at_reduced_psi(
        reduced_psi,
        reduced_beta,
        convergence_tolerance,
        overflow_limit,
        max_terms,
    );

    // Scale s0, s1 back up to the original psi using duplication formulas:
    //   s0(2*psi) = 2*s0(psi)^2 - 1   (analogue of cos(2x))
    //   s1(2*psi) = 2*s0(psi)*s1(psi) (analogue of sin(2x))
    for _ in 0..halving_count {
        let cosine_like = s0;
        let sine_like = s1;
        s0 = 2.0 * cosine_like * cosine_like - 1.0;
        s1 = 2.0 * cosine_like * sine_like;
    }

    // Reconstruct s2 and s3 from their defining relations. This subtraction
    // can lose precision for very large |beta|, as documented on `s_funct`.
    let s3 = (s1 - psi) / alpha;
    let s2 = (s0 - 1.0) / alpha;

    (s0, s1, s2, s3)
}

/// Halve `psi` (and correspondingly divide `beta` by 4) until `|beta|` falls
/// below `beta_threshold`, or until `max_halving_steps` halvings have been
/// performed. Returns the reduced `(psi, beta)` pair and the number of
/// halvings actually applied.
fn reduce_psi_until_beta_is_small(
    psi: f64,
    beta: f64,
    beta_threshold: f64,
    max_halving_steps: usize,
) -> (f64, f64, usize) {
    let mut reduced_psi = psi;
    let mut reduced_beta = beta;
    let mut halving_count = 0usize;

    while reduced_beta.abs() >= beta_threshold && halving_count < max_halving_steps {
        reduced_psi *= 0.5;
        reduced_beta *= 0.25; // beta ∝ psi^2, so halving psi divides beta by 4.
        halving_count += 1;
    }

    (reduced_psi, reduced_beta, halving_count)
}

/// Expand `s0` and `s1` by power series at the (already reduced) `psi`,
/// where `|beta|` is guaranteed to be small enough for fast convergence.
fn expand_s0_s1_series_at_reduced_psi(
    reduced_psi: f64,
    reduced_beta: f64,
    convergence_tolerance: f64,
    overflow_limit: f64,
    max_terms: usize,
) -> (f64, f64) {
    let mut s0 = 1.0;
    let mut s1 = reduced_psi;

    // First nontrivial term for s0 is reduced_beta/(1·2); for s1 it is
    // reduced_psi · reduced_beta/(2·3).
    let mut series_term_s0 = 1.0;
    let mut series_term_s1 = reduced_psi;

    for term_index in 1..=max_terms {
        series_term_s0 *= reduced_beta / ((2 * term_index - 1) as f64 * (2 * term_index) as f64);
        s0 += series_term_s0;
        if series_term_s0.abs() < convergence_tolerance || series_term_s0.abs() > overflow_limit {
            break;
        }
    }

    for term_index in 1..=max_terms {
        series_term_s1 *= reduced_beta / ((2 * term_index) as f64 * (2 * term_index + 1) as f64);
        s1 += series_term_s1;
        if series_term_s1.abs() < convergence_tolerance || series_term_s1.abs() > overflow_limit {
            break;
        }
    }

    (s0, s1)
}

#[cfg(test)]
mod tests_s_funct {
    use approx::assert_relative_eq;

    use super::s_funct;

    fn check_invariants(psi: f64, alpha: f64, s0: f64, s1: f64, s2: f64, s3: f64) {
        let tol = 1e-12;
        assert!(
            (s0 - (1.0 + alpha * s2)).abs() < tol,
            "Invariant s0 = 1 + α*s2 violated: {} vs {}",
            s0,
            1.0 + alpha * s2
        );
        assert!(
            (s1 - (psi + alpha * s3)).abs() < tol,
            "Invariant s1 = ψ + α*s3 violated: {} vs {}",
            s1,
            psi + alpha * s3
        );
    }

    #[test]
    fn test_small_beta() {
        // Small psi and alpha -> beta small, direct series expansion branch
        let psi = 0.01;
        let alpha = 0.1;
        let (s0, s1, s2, s3) = s_funct(psi, alpha);

        // Basic sanity
        assert!(s0 > 0.0);
        assert!(s1 > 0.0);
        check_invariants(psi, alpha, s0, s1, s2, s3);
    }

    #[test]
    fn test_large_beta() {
        let psi = 10.0;
        let alpha = 5.0;
        let (s0, s1, s2, s3) = s_funct(psi, alpha);

        // Vérification de la validité numérique
        assert!(s0.is_finite() && s1.is_finite() && s2.is_finite() && s3.is_finite());

        // Tolérance plus relâchée pour le grand beta
        let rel_tol = 1e-7;

        // Invariants (version Fortran)
        assert_relative_eq!(s0, 1.0 + alpha * s2, max_relative = rel_tol);
        assert_relative_eq!(s1, psi + alpha * s3, max_relative = rel_tol);
    }

    #[test]
    fn test_zero_alpha() {
        // When alpha = 0, expansions should reduce to s0=1, s1=psi, s2=psi^2/2, s3=psi^3/6
        let psi = 2.0;
        let alpha = 0.0;
        let (s0, s1, s2, s3) = s_funct(psi, alpha);

        assert!((s0 - 1.0).abs() < 1e-14);
        assert!((s1 - psi).abs() < 1e-14);
        assert!((s2 - psi.powi(2) / 2.0).abs() < 1e-14);
        assert!((s3 - psi.powi(3) / 6.0).abs() < 1e-14);
    }

    #[test]
    fn test_zero_psi() {
        // When psi = 0, expansions simplify
        let psi = 0.0;
        let alpha = 2.0;
        let (s0, s1, s2, s3) = s_funct(psi, alpha);

        assert!((s0 - 1.0).abs() < 1e-14);
        assert!((s1 - 0.0).abs() < 1e-14);
        assert!((s2 - 0.0).abs() < 1e-14);
        assert!((s3 - 0.0).abs() < 1e-14);
    }

    #[test]
    fn test_symmetry_negative_psi() {
        // s_funct should be odd in psi for s1 and s3, even for s0 and s2
        let psi = 1.0;
        let alpha = 0.5;
        let (s0_pos, s1_pos, s2_pos, s3_pos) = s_funct(psi, alpha);
        let (s0_neg, s1_neg, s2_neg, s3_neg) = s_funct(-psi, alpha);

        let tol = 1e-12;
        // Even functions
        assert!((s0_pos - s0_neg).abs() < tol);
        assert!((s2_pos - s2_neg).abs() < tol);
        // Odd functions
        assert!((s1_pos + s1_neg).abs() < tol);
        assert!((s3_pos + s3_neg).abs() < tol);
    }

    #[test]
    fn test_consistency_large_vs_small() {
        // For moderate values, the two branches should give consistent results
        let psi = 2.5;
        let alpha = 1.0;
        let (s0, s1, s2, s3) = s_funct(psi, alpha);
        check_invariants(psi, alpha, s0, s1, s2, s3);
    }

    #[test]
    fn test_s_funct_real_data() {
        let psi = -15.279808141051223;
        let alpha = -1.6298946008705195e-4;

        let (s0, s1, s2, s3) = s_funct(psi, alpha);

        assert_eq!(s0, 0.9810334785583247);
        assert_eq!(s1, -15.183083836892674);
        assert_eq!(s2, 116.3665517484714);
        assert_eq!(s3, -593.4390119881925);
    }
}