crcbl-phys 0.1.0

Crucible physics: queries, forces, CCD
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
//! Analytic two-body propagation: where a coasting body is at time `t`,
//! without integrating anything to find out.
//!
//! The physics plan, whose rules are in `docs/notes/simulation.md`: "distant
//! bodies = analytic Kepler orbits
//! (`position = f(t)`, zero integration cost, stable forever). Live
//! integration only inside **bubbles** around observers."
//!
//! # Why not just integrate
//!
//! A symplectic integrator bounds energy error but not phase: run one for ten
//! thousand orbits and the body is on the right ellipse at the wrong place on
//! it. Timewarp makes that the normal case rather than the extreme one — at
//! ×1000 a minute of play is most of a day — and doing it by integrating means
//! a thousand times the substeps for the same second of wall clock, which is
//! the cost the rails exist to avoid. An analytic solution has neither problem:
//! it costs the same for `t + 1 s` as for `t + 1000 years`, and it does not
//! drift, because there is nothing accumulating.
//!
//! # Universal variables
//!
//! The propagator is the universal-variable formulation (Bate, Mueller & White,
//! *Fundamentals of Astrodynamics*, §4.4–4.5; Vallado, *Fundamentals of
//! Astrodynamics and Applications*, algorithm `KEPLER`). It advances a state
//! vector directly, so **one code path covers every conic** — a circular
//! parking orbit, a transfer ellipse, an escape hyperbola — and none of the
//! angles the classical elements are built from is ever formed: no ascending
//! node, no argument of periapsis, no anomaly.
//!
//! That last part is the point. The classical elements are singular exactly
//! where a game spends its time: the ascending node is undefined for an
//! equatorial orbit and the argument of periapsis for a circular one, so a
//! propagator built on them is at its worst around the parking orbit every
//! mission starts in. [`Orbit`] reports only the quantities that stay defined
//! there — size, shape, periapsis, apoapsis, period.
//!
//! # What drove it
//!
//! `docs/plan/sample/06-orbit.md` milestone 2, "stable orbit + timewarp", and
//! its exit criterion of an orbital period within 0.1% of the real one.

use crcbl_core::WorldPos;

use crate::frames::State;

/// How many Newton steps the universal-variable solve may take before the
/// bracket it is safeguarded by has to finish the job on its own.
///
/// Newton doubles its correct digits per step from these initial guesses, so a
/// converged solve uses a handful; the cap is not a budget but a guard against
/// a cycle, and reaching it is not a failure because each step that leaves the
/// bracket is replaced by a bisection that halves it.
const MAX_NEWTON_STEPS: u32 = 64;

/// Convergence threshold on the universal anomaly, relative to its own size.
///
/// Near the `f64` floor: the anomaly is `sqrt(a) * E` for an ellipse, so this
/// is a fraction of a millimetre-second on any orbit a game holds.
const CHI_TOLERANCE: f64 = 1.0e-12;

/// The size and shape of a two-body orbit, in the frame of the body it is
/// about.
///
/// Only the quantities that are defined for every orbit: a circular orbit has
/// no argument of periapsis and an equatorial one no ascending node, and a
/// flight UI that displayed either would be showing noise. What it needs —
/// how high, how eccentric, how long — is all here.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Orbit {
    /// Semi-major axis in metres: positive for an ellipse, negative for a
    /// hyperbola, and infinite for the parabolic knife edge between them.
    pub semi_major_axis: f64,
    /// Eccentricity: 0 circular, below 1 elliptic, 1 parabolic, above 1
    /// hyperbolic.
    pub eccentricity: f64,
    /// Semi-latus rectum in metres — the orbit's width at the focus.
    ///
    /// Carried because it is the one size that stays finite across the
    /// parabolic case, where the semi-major axis does not, and because both
    /// apsides come from it.
    pub semi_latus_rectum: f64,
    /// Specific orbital energy in J/kg: `v²/2 - mu/r`, negative for a bound
    /// orbit.
    ///
    /// The invariant to watch. Nothing this module does may change it, so a
    /// test that propagates and compares it is asking whether the analytic
    /// solution really is one.
    pub specific_energy: f64,
}

impl Orbit {
    /// The orbit a body at `state` is on about a body of gravitational
    /// parameter `mu`.
    ///
    /// # Panics
    ///
    /// If `mu` is not positive, or the state sits exactly on the centre of
    /// attraction, where there is no orbit and every element divides by zero.
    #[must_use]
    pub fn from_state(mu: f64, state: State) -> Self {
        assert!(mu > 0.0, "a gravitational parameter is positive, got {mu}");
        let position = state.position.delta(WorldPos::ORIGIN);
        let velocity = state.velocity;
        let radius = position.length();
        assert!(
            radius > 0.0,
            "a body at the centre of attraction is not on an orbit"
        );

        let speed_squared = velocity.length_squared();
        let specific_energy = 0.5 * speed_squared - mu / radius;
        // Infinite on the parabolic edge rather than enormous, which is the
        // honest answer and what `period` and `apoapsis` test. Taken from
        // `inverse_semi_major_axis` rather than from `-mu / 2E`, which is the
        // same quantity algebraically and not the same `f64`: the propagator
        // folds whole revolutions using the first, so a period derived from the
        // second would leave a full revolution a few picoseconds short of one.
        let semi_major_axis = 1.0 / inverse_semi_major_axis(mu, radius, speed_squared);

        // The eccentricity vector points at periapsis and has the eccentricity
        // for its length. Formed from the state rather than from angles, so it
        // is as defined for a circular orbit as for any other — it just comes
        // out near zero, and nothing divides by it.
        let eccentricity_vector =
            ((speed_squared - mu / radius) * position - position.dot(velocity) * velocity) / mu;
        let eccentricity = eccentricity_vector.length();

        // `h²/mu`, which stays finite where the semi-major axis does not.
        let semi_latus_rectum = position.cross(velocity).length_squared() / mu;

        Self {
            semi_major_axis,
            eccentricity,
            semi_latus_rectum,
            specific_energy,
        }
    }

    /// Whether the orbit closes — an ellipse rather than a parabola or
    /// hyperbola.
    ///
    /// Read off the semi-major axis rather than the sign of the energy. The two
    /// say the same thing about an orbit and can disagree by a rounding on the
    /// parabolic edge, and it is the axis the period and the apoapsis are
    /// computed from — so an orbit that reported itself closed and then had no
    /// period would be the worse answer.
    #[inline]
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.semi_major_axis > 0.0 && self.semi_major_axis.is_finite()
    }

    /// Distance from the centre of attraction at the closest point, in metres.
    ///
    /// From the semi-latus rectum rather than from `a(1 - e)`, so it is finite
    /// for an escape trajectory too.
    #[inline]
    #[must_use]
    pub fn periapsis(&self) -> f64 {
        self.semi_latus_rectum / (1.0 + self.eccentricity)
    }

    /// Distance at the furthest point, or `None` for an orbit that does not
    /// come back.
    ///
    /// From `a(1 + e)` rather than from the semi-latus rectum, which is the
    /// opposite choice to [`periapsis`](Self::periapsis) and for the opposite
    /// reason. The two are equal — `p = a(1 - e)(1 + e)` — but they fail at
    /// different ends: `p / (1 - e)` cancels as `e` approaches one, and for a
    /// **radial** trajectory, where the angular momentum is exactly zero, it is
    /// `0 / 0`. That is not a corner case here. A rocket going straight up off
    /// a planet that does not rotate is exactly radial, and it does have an
    /// apoapsis: it is `2a`, the height it stops at before falling back, which
    /// is what this returns.
    #[must_use]
    pub fn apoapsis(&self) -> Option<f64> {
        self.is_closed()
            .then_some(self.semi_major_axis * (1.0 + self.eccentricity))
    }

    /// The orbital period in seconds, or `None` for an orbit that does not
    /// come back.
    ///
    /// Kepler's third law, `T = 2π·sqrt(a³/mu)`.
    #[must_use]
    pub fn period(&self, mu: f64) -> Option<f64> {
        self.is_closed().then(|| {
            std::f64::consts::TAU
                * (self.semi_major_axis * self.semi_major_axis * self.semi_major_axis / mu).sqrt()
        })
    }
}

/// Where a body at `state` will be `dt` seconds later, coasting under the
/// gravity of a body of gravitational parameter `mu` at the frame's origin.
///
/// `dt` may be negative, which propagates backwards. The cost does not depend
/// on its size: a century is the same handful of Newton steps as a second.
///
/// The state is read in whatever frame it was given in, and comes back in the
/// same one. The arithmetic is done on the offset from that frame's origin
/// rather than on the sector split: an orbit is bounded by its frame's sphere
/// of influence, where `f64` metres are good to a fraction of a micrometre.
/// The split earns its keep at the frame conversion, not here.
///
/// Only gravity acts — a burn or an atmosphere makes the trajectory
/// something other than a conic, which is why `06-orbit.md` drops out of
/// timewarp for both.
///
/// # Panics
///
/// If `mu` is not positive, if `dt` is not finite, or if the state sits on the
/// centre of attraction.
#[must_use]
pub fn propagate(mu: f64, state: State, dt: f64) -> State {
    assert!(mu > 0.0, "a gravitational parameter is positive, got {mu}");
    assert!(dt.is_finite(), "cannot propagate by {dt} seconds");

    let position = state.position.delta(WorldPos::ORIGIN);
    let velocity = state.velocity;
    let radius = position.length();
    assert!(
        radius > 0.0,
        "a body at the centre of attraction is not on an orbit"
    );

    // `alpha` is `1/a`: positive elliptic, zero parabolic, negative hyperbolic.
    // Formed this way rather than as `1.0 / a` so the parabolic case is a plain
    // zero instead of a division by an infinity.
    let root_mu = mu.sqrt();
    let alpha = inverse_semi_major_axis(mu, radius, velocity.length_squared());
    // `r·v/sqrt(mu)`, which the time-of-flight equation carries throughout.
    let sigma = position.dot(velocity) / root_mu;

    // A closed orbit repeats exactly, so whole revolutions are not something to
    // solve for: fold them away first. Solving for them instead would ask
    // Newton to find a root ten thousand revolutions along a function that has
    // ten thousand nearby ones, which is where a propagator asked to timewarp
    // stops converging.
    //
    // The remainder keeps the sign of `dt` rather than being made positive.
    // Rewriting a second backwards as most of a revolution forwards is exact in
    // theory and ruinous in practice: the period comes from `alpha`, which for
    // a near-parabolic orbit is a difference of two nearly equal numbers and
    // carries a large relative error, and a whole revolution multiplies it.
    let dt = match period_of(mu, alpha) {
        Some(period) => dt % period,
        None => dt,
    };
    if dt == 0.0 {
        return state;
    }

    let chi = solve_universal_anomaly(root_mu, radius, sigma, alpha, dt);

    // Lagrange's f and g: the propagated state is a combination of the two
    // vectors it started with, which is what makes this exact rather than a
    // step.
    let psi = chi * chi * alpha;
    let (c2, c3) = stumpff(psi);
    let f = 1.0 - chi * chi * c2 / radius;
    let g = dt - chi * chi * chi * c3 / root_mu;
    let new_position = f * position + g * velocity;
    let new_radius = new_position.length();
    let f_dot = root_mu * chi * (psi * c3 - 1.0) / (new_radius * radius);
    let g_dot = 1.0 - chi * chi * c2 / new_radius;

    State::new(
        WorldPos::from_offset(new_position),
        f_dot * position + g_dot * velocity,
    )
}

/// `1/a`, the inverse semi-major axis, from the vis-viva relation.
///
/// The inverse rather than the axis itself because it is what the
/// universal-variable solve wants and because it is the form that stays finite
/// on the parabolic edge, where `a` does not: `alpha` is simply zero there.
/// Positive elliptic, negative hyperbolic.
///
/// One function rather than the expression written at each use, because the
/// two forms of it — this and `-mu / 2E` — are equal on paper and differ in the
/// last bits, and a propagator that folds revolutions by one while reporting a
/// period from the other never quite closes an orbit.
fn inverse_semi_major_axis(mu: f64, radius: f64, speed_squared: f64) -> f64 {
    2.0 / radius - speed_squared / mu
}

/// The period of the orbit with inverse semi-major axis `alpha`, or `None` if
/// it does not close.
fn period_of(mu: f64, alpha: f64) -> Option<f64> {
    (alpha > 0.0).then(|| {
        let a = 1.0 / alpha;
        std::f64::consts::TAU * (a * a * a / mu).sqrt()
    })
}

/// Solves Kepler's time-of-flight equation for the universal anomaly `chi`.
///
/// Newton's method safeguarded by a bracket: time of flight rises strictly
/// with `chi`, so a bracket that contains the root keeps containing it, and any
/// Newton step that leaves it is replaced by a bisection. That is the standard
/// answer to Newton's one real failure mode here — a near-parabolic orbit,
/// where the derivative at the initial guess points the step past the root and
/// an unguarded iteration wanders off. It cannot fail to converge: worst case
/// it is bisection, which halves the bracket every step.
fn solve_universal_anomaly(root_mu: f64, radius: f64, sigma: f64, alpha: f64, dt: f64) -> f64 {
    let target = root_mu * dt;
    // Time of flight minus the time asked for, and its derivative, which is the
    // radius at `chi` — always positive, so the function is monotone.
    let residual = |chi: f64| {
        let psi = chi * chi * alpha;
        let (c2, c3) = stumpff(psi);
        let flight =
            chi * chi * chi * c3 + sigma * chi * chi * c2 + radius * chi * (1.0 - psi * c3);
        let slope = chi * chi * c2 + sigma * chi * (1.0 - psi * c3) + radius * (1.0 - psi * c2);
        (flight - target, slope)
    };

    let (mut low, mut high) = bracket(&residual, alpha, dt);
    // Vallado's opening guess for a closed orbit, `sqrt(mu)·dt/a`, which is
    // exact for a circle and close for anything nearly one — the orbits a game
    // spends its time in. Clamped into the bracket so a poor guess on an
    // eccentric orbit costs a bisection rather than the guarantee.
    let mut chi = if alpha > 0.0 {
        (root_mu * dt * alpha).clamp(low, high)
    } else {
        0.5 * (low + high)
    };
    for _ in 0..MAX_NEWTON_STEPS {
        let (value, slope) = residual(chi);
        if value > 0.0 {
            high = chi
        } else {
            low = chi
        }

        let step = if slope > 0.0 {
            value / slope
        } else {
            f64::INFINITY
        };
        let next = chi - step;
        // Outside the bracket, or a step that is not a number: bisect instead.
        let next = if next > low && next < high {
            next
        } else {
            0.5 * (low + high)
        };

        let moved = (next - chi).abs();
        chi = next;
        if moved <= CHI_TOLERANCE * chi.abs().max(1.0) {
            break;
        }
    }
    chi
}

/// A bracket `[low, high]` around the universal anomaly for `dt`.
///
/// For a closed orbit the answer is exact and free: `chi` is `sqrt(a)` times
/// the eccentric anomaly, which runs over one revolution, and `dt` has already
/// been folded into one. Otherwise the upper end is doubled until the residual
/// changes sign, which terminates because time of flight grows without bound.
fn bracket(residual: &impl Fn(f64) -> (f64, f64), alpha: f64, dt: f64) -> (f64, f64) {
    if alpha > 0.0 {
        // One revolution, on whichever side of the start the time asked for
        // lies: `chi` is `sqrt(a)` times the eccentric anomaly and `dt` has
        // already been folded into a single revolution.
        let revolution = std::f64::consts::TAU / alpha.sqrt();
        return if dt < 0.0 {
            (-revolution, 0.0)
        } else {
            (0.0, revolution)
        };
    }
    // Open orbit: `dt` was not folded, so it can be negative, and `chi` takes
    // the sign of the time.
    let mut edge = dt.signum();
    // The residual at zero has the opposite sign to the one being hunted, so
    // the loop stops as soon as `edge` reaches past the root.
    while residual(edge).0.signum() == residual(0.0).0.signum() {
        edge *= 2.0;
        assert!(
            edge.is_finite(),
            "no universal anomaly brackets a flight time of {dt} seconds"
        );
    }
    if edge < 0.0 { (edge, 0.0) } else { (0.0, edge) }
}

/// The Stumpff functions `C(psi)` and `S(psi)`.
///
/// They are what makes one formula cover every conic: circular functions for a
/// bound orbit, hyperbolic ones for an escape, and the same limit for both as
/// the orbit approaches parabolic.
///
/// Near zero both expressions are a small difference of two nearly equal
/// numbers divided by a small number, which loses most of the mantissa, so the
/// series each converges to is used instead. The threshold is where the two
/// agree to better than `f64` can tell them apart.
#[expect(
    clippy::disallowed_methods,
    reason = "crcbl_core::trig saturates past a million radians, which a long propagate's \
              anomaly passes, and no hyperbolic functions are constructed yet; \
              docs/backlog.md carries both"
)]
fn stumpff(psi: f64) -> (f64, f64) {
    /// Below this the closed forms cancel catastrophically and the series is
    /// both faster and more accurate.
    const SERIES_BELOW: f64 = 1.0e-6;

    if psi > SERIES_BELOW {
        let root = psi.sqrt();
        ((1.0 - root.cos()) / psi, (root - root.sin()) / (root * psi))
    } else if psi < -SERIES_BELOW {
        let root = (-psi).sqrt();
        (
            (root.cosh() - 1.0) / -psi,
            (root.sinh() - root) / (root * -psi),
        )
    } else {
        // The first three terms of each series. At `|psi| <= 1e-6` the next
        // term is below `1e-14` of the leading one, under the `f64` epsilon
        // of the result.
        (
            0.5 - psi / 24.0 + psi * psi / 720.0,
            1.0 / 6.0 - psi / 120.0 + psi * psi / 5040.0,
        )
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Earth's gravitational parameter, JPL DE440 (`GM`, m³/s²).
    const EARTH_MU: f64 = 3.986_004_418e14;

    /// A circular orbit of the given radius, in the plane, going anticlockwise.
    fn circular(radius: f64) -> State {
        State::new(
            WorldPos::from_offset(DVec3::new(radius, 0.0, 0.0)),
            DVec3::new(0.0, (EARTH_MU / radius).sqrt(), 0.0),
        )
    }

    /// An orbit at periapsis `low` with apoapsis `high`, in the plane.
    fn transfer(low: f64, high: f64) -> State {
        let a = 0.5 * (low + high);
        State::new(
            WorldPos::from_offset(DVec3::new(low, 0.0, 0.0)),
            DVec3::new(0.0, (EARTH_MU * (2.0 / low - 1.0 / a)).sqrt(), 0.0),
        )
    }

    fn radius_of(state: State) -> f64 {
        state.position.delta(WorldPos::ORIGIN).length()
    }

    /// **The exit criterion's period check**, against a number that exists
    /// outside this file: a satellite at the geostationary radius takes one
    /// sidereal day to go round.
    ///
    /// The radius is 42 164.17 km and the sidereal day 86 164.0905 s (IERS);
    /// `06-orbit.md` asks for 0.1%, which is far looser than what this
    /// actually manages — but it is the bar the plan set, and a tighter one
    /// here would be asserting on the inputs' own precision rather than on the
    /// formula.
    #[test]
    fn the_period_of_a_geostationary_orbit_is_a_sidereal_day() {
        const SIDEREAL_DAY: f64 = 86_164.090_5;
        let orbit = Orbit::from_state(EARTH_MU, circular(42_164_170.0));
        let period = orbit.period(EARTH_MU).expect("a circular orbit closes");
        assert!(
            (period - SIDEREAL_DAY).abs() <= SIDEREAL_DAY * 0.001,
            "the geostationary period came out {period} s against a sidereal day of {SIDEREAL_DAY} s"
        );
    }

    /// A circle is the case the classical elements cannot describe — no
    /// periapsis to measure an argument from — and the one a mission spends
    /// most of its time in. It propagates like any other.
    #[test]
    fn a_circular_orbit_comes_a_quarter_turn_in_a_quarter_period() {
        const RADIUS: f64 = 7_000_000.0;
        let start = circular(RADIUS);
        let orbit = Orbit::from_state(EARTH_MU, start);
        assert!(
            orbit.eccentricity < 1.0e-12,
            "a circular orbit has no eccentricity to speak of, and nothing may \
             divide by it, but it came out {}",
            orbit.eccentricity
        );
        let period = orbit.period(EARTH_MU).expect("a circular orbit closes");

        let quarter = propagate(EARTH_MU, start, period / 4.0);
        let position = quarter.position.delta(WorldPos::ORIGIN);
        // A quarter turn anticlockwise from `+x` is `+y`, at the same radius.
        let expected = DVec3::new(0.0, RADIUS, 0.0);
        assert!(
            (position - expected).length() < 1.0e-3,
            "a quarter period should reach {expected:?}, came out {position:?}"
        );
        assert!(
            (quarter.velocity.length() - start.velocity.length()).abs() < 1.0e-6,
            "speed is constant on a circle"
        );

        // Half a period puts it on the far side, which the quarter alone could
        // not distinguish from a body that stopped there.
        let half = propagate(EARTH_MU, start, period / 2.0);
        let position = half.position.delta(WorldPos::ORIGIN);
        assert!(
            (position - DVec3::new(-RADIUS, 0.0, 0.0)).length() < 1.0e-3,
            "half a period should reach the far side, came out {position:?}"
        );
    }

    /// The apsides come back as the altitudes the orbit was built from, and
    /// half a period actually arrives at the far one.
    #[test]
    fn a_transfer_orbit_reaches_the_apoapsis_it_reports() {
        const LOW: f64 = 6_571_000.0;
        const HIGH: f64 = 46_371_000.0;
        let start = transfer(LOW, HIGH);
        let orbit = Orbit::from_state(EARTH_MU, start);

        assert!((orbit.periapsis() - LOW).abs() < 1.0e-3, "{:?}", orbit);
        let apoapsis = orbit.apoapsis().expect("a transfer orbit closes");
        assert!((apoapsis - HIGH).abs() < 1.0e-3, "{apoapsis}");
        assert!(
            orbit.eccentricity > 0.0 && orbit.eccentricity < 1.0,
            "a transfer orbit is an ellipse, got e = {}",
            orbit.eccentricity
        );

        let period = orbit.period(EARTH_MU).expect("a transfer orbit closes");
        let arrived = radius_of(propagate(EARTH_MU, start, period / 2.0));
        assert!(
            (arrived - HIGH).abs() < 1.0e-3,
            "half a period should arrive at apoapsis {HIGH} m, came out {arrived} m"
        );

        // **The same orbit read from somewhere that is not an apsis.** Every
        // fixture above starts at periapsis, where the position and the
        // velocity are perpendicular and half the eccentricity vector vanishes
        // — so a term dropped from it would go unnoticed. A third of a period
        // along, nothing is perpendicular to anything.
        let elsewhere = propagate(EARTH_MU, start, period / 3.0);
        let radius = radius_of(elsewhere);
        assert!(
            radius > LOW * 1.01 && radius < HIGH * 0.99,
            "the sample point must be off both apsides, and it is at {radius} m"
        );
        let same = Orbit::from_state(EARTH_MU, elsewhere);
        assert!(
            (same.eccentricity - orbit.eccentricity).abs() < 1.0e-9,
            "read from {radius} m the eccentricity became {} against {}",
            same.eccentricity,
            orbit.eccentricity
        );
        assert!(
            (same.periapsis() - LOW).abs() < 1.0e-3,
            "read from off-apsis the periapsis became {} m",
            same.periapsis()
        );
        assert!(
            (same.apoapsis().expect("still closed") - HIGH).abs() < 1.0e-3,
            "read from off-apsis the apoapsis became {:?} m",
            same.apoapsis()
        );
    }

    /// **`06-orbit.md`'s stability criterion, measured.** Ten thousand
    /// propagations, and the orbit is the orbit it started as.
    ///
    /// Each step feeds the previous result back in, which is the pessimistic
    /// case: a game anchored to a fixed epoch would accumulate nothing at all,
    /// and re-anchoring every step is what a timewarp that keeps re-reading the
    /// live state does. The step is a fraction of a period rather than a whole
    /// one so the fold in [`propagate`] cannot make the test trivial by
    /// returning its input.
    ///
    /// The bound is [`DRIFT`], and it has to leave room for a different `libm`:
    /// the Stumpff functions are built on `cos` and `cosh`, which are correct
    /// to an ulp on every platform and to *the same* ulp on none, and ten
    /// thousand chained steps amplify that disagreement. An earlier bound sized
    /// from a Linux run reddened on macOS at eight parts in a billion — a
    /// number that says nothing about the propagator and everything about whose
    /// `cos` it ran on, so the bound is two orders above that rather than above
    /// the Linux figure.
    ///
    /// What it still catches is a defect: a propagator that had stopped
    /// conserving anything is out by kilometres, not by a hundred-millionth.
    #[test]
    fn ten_thousand_revolutions_leave_the_orbit_where_it_was() {
        const STEPS: u32 = 10_000;
        const STEP_FRACTION: f64 = 0.37;
        const RADIUS: f64 = 42_164_170.0;
        /// How far any of the orbit's invariants may move over [`STEPS`]
        /// chained propagations, as a fraction of itself.
        const DRIFT: f64 = 1.0e-6;
        /// How far the ship may be from where a quarter period puts it after
        /// [`STEPS`] whole revolutions, in metres.
        ///
        /// Dominated by one rounding, not by the propagator: `STEPS · T` is a
        /// single `f64` multiply, and an ulp of it at this period is a tenth of
        /// a microsecond, which at orbital speed is a fraction of a millimetre.
        /// That term is the same arithmetic everywhere, so this bound does not
        /// depend on whose `libm` ran it.
        const PHASE: f64 = 1.0e-2;

        let start = circular(RADIUS);
        let before = Orbit::from_state(EARTH_MU, start);
        let step = before.period(EARTH_MU).expect("a circular orbit closes") * STEP_FRACTION;

        let mut chained = start;
        for _ in 0..STEPS {
            chained = propagate(EARTH_MU, chained, step);
        }
        let after = Orbit::from_state(EARTH_MU, chained);

        let energy_drift =
            (after.specific_energy - before.specific_energy).abs() / before.specific_energy.abs();
        assert!(
            energy_drift < DRIFT,
            "specific energy drifted by {energy_drift} of itself over {STEPS} propagations"
        );
        let size_drift =
            (after.semi_major_axis - before.semi_major_axis).abs() / before.semi_major_axis;
        assert!(
            size_drift < DRIFT,
            "the semi-major axis drifted by {size_drift} of itself"
        );
        assert!(
            after.eccentricity < DRIFT,
            "a circle stayed a circle only to e = {}",
            after.eccentricity
        );

        // The radius, which is the claim the drifts above are about seen from
        // the other side: after ten thousand of them the ship is still on the
        // circle it started on rather than a little above or below it.
        let radius = radius_of(chained);
        assert!(
            (radius - RADIUS).abs() < RADIUS * DRIFT,
            "after {STEPS} propagations the circular radius is {radius} m, not {RADIUS} m"
        );

        // **And the phase, which is what a symplectic integrator loses while
        // keeping the energy.** Ten thousand periods and a quarter, in one
        // call, must land exactly where a quarter period does — nothing has
        // accumulated in between, because nothing was integrated.
        //
        // Measured against the quarter-period state rather than a written-down
        // position, so the comparison is between two answers from the same
        // formula and the tolerance is about the fold rather than about the
        // orbit — see [`PHASE`]. Ten thousand revolutions is four hundred
        // billion metres of travel, and the ship arrives inside a centimetre of
        // where it would have been on the first one.
        let period = before.period(EARTH_MU).expect("a circular orbit closes");
        let far = propagate(EARTH_MU, start, f64::from(STEPS) * period + period / 4.0);
        let quarter = propagate(EARTH_MU, start, period / 4.0);
        let apart = far.position.distance(quarter.position);
        assert!(
            apart < PHASE,
            "{STEPS} periods and a quarter landed {apart} m from a quarter period"
        );
    }

    /// An escape trajectory is a different conic and the same code path. It has
    /// no period and no apoapsis, and saying so is the point — a `0.0` there
    /// would read as "it comes back to the centre".
    #[test]
    fn an_escape_trajectory_has_no_period_and_reverses_exactly() {
        const PERIAPSIS: f64 = 6_571_000.0;
        let escape = State::new(
            WorldPos::from_offset(DVec3::new(PERIAPSIS, 0.0, 0.0)),
            // Half again the speed that just barely escapes.
            DVec3::new(0.0, 1.5 * (2.0 * EARTH_MU / PERIAPSIS).sqrt(), 0.0),
        );
        let orbit = Orbit::from_state(EARTH_MU, escape);
        assert!(orbit.eccentricity > 1.0, "e = {}", orbit.eccentricity);
        assert!(!orbit.is_closed());
        assert_eq!(orbit.period(EARTH_MU), None);
        assert_eq!(orbit.apoapsis(), None);
        assert!(
            (orbit.periapsis() - PERIAPSIS).abs() < 1.0e-3,
            "a hyperbola still has a periapsis, got {}",
            orbit.periapsis()
        );

        let day = 86_400.0;
        let out = propagate(EARTH_MU, escape, day);
        assert!(
            radius_of(out) > radius_of(escape) * 10.0,
            "a day of escape should be a long way out, got {} m",
            radius_of(out)
        );
        let back = propagate(EARTH_MU, out, -day);
        let error = back.position.distance(escape.position);
        assert!(
            error < radius_of(out) * 1.0e-9,
            "out and back over {} m of hyperbola came home {error} m adrift",
            radius_of(out)
        );
    }

    /// **Backwards on a near-parabolic orbit stays backwards.**
    ///
    /// Such an orbit closes, but only just: its semi-major axis comes from a
    /// difference of two nearly equal numbers and carries a large relative
    /// error, and its period is astronomical. Folding a small negative time
    /// into the positive remainder would rewrite one second backwards as
    /// almost a whole revolution forwards — exact on paper, and out by
    /// kilometres with a period known to seven figures.
    #[test]
    fn a_backwards_step_on_a_near_parabolic_orbit_does_not_go_the_long_way() {
        const PERIAPSIS: f64 = 6_571_000.0;
        let escape_speed = (2.0 * EARTH_MU / PERIAPSIS).sqrt();
        let barely = State::new(
            WorldPos::from_offset(DVec3::new(PERIAPSIS, 0.0, 0.0)),
            DVec3::new(0.0, escape_speed * (1.0 - 1.0e-9), 0.0),
        );
        let orbit = Orbit::from_state(EARTH_MU, barely);
        assert!(
            orbit.is_closed() && orbit.eccentricity > 0.999,
            "the fixture must be a barely-closed orbit, got e = {}",
            orbit.eccentricity
        );

        // The periapsis has to survive the near-parabolic case too. `a(1 - e)`
        // would be an enormous semi-major axis times a difference of two
        // numbers that agree to nine figures, which keeps almost none of them;
        // the semi-latus rectum is finite and well conditioned here.
        assert!(
            (orbit.periapsis() - PERIAPSIS).abs() < 1.0e-3,
            "a barely-closed orbit still has a periapsis at {PERIAPSIS} m, got {} m",
            orbit.periapsis()
        );

        let hour = 3_600.0;
        let out = propagate(EARTH_MU, barely, hour);
        let back = propagate(EARTH_MU, out, -hour);
        let error = back.position.distance(barely.position);
        assert!(
            error < 1.0e-3,
            "an hour out and back came home {error} m adrift"
        );
    }

    /// **A trajectory straight up still has a top.**
    ///
    /// Zero angular momentum is not a corner case: a rocket leaving a planet
    /// that does not rotate is exactly radial, and so is anything dropped from
    /// rest. The semi-latus rectum is zero there and the eccentricity is one,
    /// so the apoapsis formula that divides one by the other is `0 / 0` — and a
    /// `Some(NaN)` reads as a number everywhere downstream of it, which is how
    /// it reached a flight computer that concluded it had already arrived.
    #[test]
    fn a_radial_trajectory_has_an_apoapsis_where_it_stops() {
        const HEIGHT: f64 = 7_000_000.0;

        // Dropped from rest: it is at its apoapsis already, and falls to the
        // centre, so the periapsis is zero.
        let dropped = State::new(
            WorldPos::from_offset(DVec3::new(0.0, HEIGHT, 0.0)),
            DVec3::ZERO,
        );
        let orbit = Orbit::from_state(EARTH_MU, dropped);
        assert!(
            orbit.is_closed(),
            "a body dropped from rest comes back down"
        );
        let apoapsis = orbit.apoapsis().expect("a closed orbit has an apoapsis");
        assert!(
            (apoapsis - HEIGHT).abs() < 1.0e-6,
            "a body at rest is at its own apoapsis, and this one reports {apoapsis} m"
        );
        assert!(
            orbit.periapsis() < 1.0e-6,
            "a radial fall reaches the centre, and this one stops at {} m",
            orbit.periapsis()
        );

        // Thrown straight up: it stops higher than it started, and the two
        // agree with the energy it was given.
        let thrown = State::new(
            WorldPos::from_offset(DVec3::new(0.0, HEIGHT, 0.0)),
            DVec3::new(0.0, 1_000.0, 0.0),
        );
        let orbit = Orbit::from_state(EARTH_MU, thrown);
        let apoapsis = orbit.apoapsis().expect("it comes back");
        assert!(
            apoapsis.is_finite(),
            "a vertical throw has a finite top, not {apoapsis}"
        );
        assert!(
            apoapsis > HEIGHT,
            "thrown upwards it should stop above {HEIGHT} m, and it reports {apoapsis} m"
        );
        // **Checked against energy, not against the shape.** A radial body has
        // no speed left at the top, so all of its energy is potential there and
        // `E = -mu / r_apo` — which shares no term with `a(1 + e)` and would
        // not move if the apsis formulas were wrong together.
        let expected = -EARTH_MU / orbit.specific_energy;
        assert!(
            (apoapsis - expected).abs() < expected * 1.0e-9,
            "the apoapsis {apoapsis} m disagrees with where the energy runs out, {expected} m"
        );
    }

    /// A whole number of revolutions is the identity, because that is what the
    /// fold in [`propagate`] makes it — not an accuracy result but the
    /// behaviour timewarp depends on.
    #[test]
    fn a_whole_revolution_returns_the_state_it_started_from() {
        let start = circular(7_000_000.0);
        let period = Orbit::from_state(EARTH_MU, start)
            .period(EARTH_MU)
            .expect("a circular orbit closes");
        let round = propagate(EARTH_MU, start, period);
        assert_eq!(round.position, start.position);
        assert_eq!(round.velocity, start.velocity);
    }

    #[test]
    #[should_panic(expected = "a body at the centre of attraction is not on an orbit")]
    fn a_body_on_the_centre_of_attraction_is_refused() {
        let _ = propagate(EARTH_MU, State::AT_ORIGIN, 1.0);
    }

    #[test]
    #[should_panic(expected = "cannot propagate by NaN seconds")]
    fn a_non_finite_flight_time_is_refused() {
        let _ = propagate(EARTH_MU, circular(7_000_000.0), f64::NAN);
    }
}