Skip to main content

deep_time/dt/
trajectory.rs

1use crate::{ClockDrift, Dt, LocalSpacetime, Real, TSpan};
2
3impl Dt {
4    /// Computes the accumulated **proper time** (Δτ) experienced by a clock moving along a
5    /// coordinate-time path from `self` to `end`.
6    ///
7    /// Proper time is the actual time measured by a real physical clock (onboard spacecraft
8    /// clock, probe, etc.). This function evaluates the exact relativistic rate
9    /// dτ/dt = √K_eff from the library’s unified master Lagrangian at each sample point
10    /// and integrates using composite Simpson’s rule.
11    ///
12    /// Use this whenever velocity, gravitational potential, or spacetime curvature changes
13    /// along the trajectory (e.g. planetary flybys, cislunar transfers, deep-space maneuvers,
14    /// or strong-field regions). It automatically includes special-relativistic velocity
15    /// effects, general-relativistic gravitational time dilation, and the built-in
16    /// Planck-scale saturation term.
17    ///
18    /// # Parameters
19    /// - `end` — the ending coordinate time of the interval.
20    /// - `samples` — slice of `LocalSpacetime` snapshots evaluated at **uniformly spaced**
21    ///   points along the path (must contain at least two entries). These samples can be
22    ///   freely reused elsewhere (e.g. for light-time calculations in `ObserverState`).
23    ///
24    /// # Returns
25    /// The accumulated proper-time interval Δτ (exact 36-digit precision).
26    ///
27    /// # Example
28    /// ```rust
29    /// use deep_time::{Scale, TSpan, LocalSpacetime, Dt};
30    ///
31    /// let start = Dt::from_sec(0, Scale::TAI);
32    /// let end   = Dt::from_sec(1000, Scale::TAI);
33    ///
34    /// // Constant metric example (α = 0.9 → dτ/dt = 0.9)
35    /// let slow = LocalSpacetime::new(0.9, 0.0, 0.0);
36    /// let samples = [slow; 2];
37    ///
38    /// let delta_tau = start.proper_time_interval_samples(end, &samples);
39    /// assert_eq!(delta_tau, TSpan::from_sec(900));
40    ///
41    /// // Update onboard proper time clock
42    /// let onboard_tau = start.to(Scale::Custom).add(delta_tau);
43    /// ```
44    pub const fn proper_time_interval_samples(self, end: Dt, samples: &[LocalSpacetime]) -> TSpan {
45        if samples.len() < 2 || self.eq(&end) {
46            return TSpan::ZERO;
47        }
48
49        let mut dt = end.to_tai_since(self);
50        let sign = if dt.sec < 0 { f!(-1.0) } else { f!(1.0) };
51        if sign < f!(0.0) {
52            dt = dt.neg();
53        }
54
55        let dt_sec = dt.to_sec_f();
56        let num_intervals = samples.len() - 1;
57
58        if num_intervals <= 1 {
59            // Fast trapezoidal rule for constant-rate cases
60            let rate0 = Self::rate_from_local(&samples[0]);
61            let rate1 = Self::rate_from_local(&samples[samples.len() - 1]);
62            let integral = f!(0.5) * (rate0 + rate1 - f!(2.0)) * dt_sec;
63            return TSpan::from_sec_f(sign * (dt_sec + integral));
64        }
65
66        // Simpson’s rule quadrature (high-order accuracy)
67        let n = f!(num_intervals);
68        let h = dt_sec / n;
69        let mut s = f!(0.0);
70
71        let mut i = 0;
72        while i <= num_intervals {
73            let local = &samples[i];
74            let rate = Self::rate_from_local(local);
75
76            let coeff = if i == 0 || i == num_intervals {
77                f!(1.0)
78            } else if i % 2 == 0 {
79                f!(2.0)
80            } else {
81                f!(4.0)
82            };
83            s += coeff * (rate - f!(1.0));
84
85            i += 1;
86        }
87
88        let integral = (h / f!(3.0)) * s;
89        TSpan::from_sec_f(sign * (dt_sec + integral))
90    }
91
92    /// Computes the relativistic correction (Δτ − Δt) using pre-computed samples.
93    ///
94    /// Returns how much the onboard clock has gained or lost relative to coordinate time.
95    /// Positive values mean the clock ran fast; negative values mean it ran slow.
96    ///
97    /// # Parameters
98    /// - `end` — ending coordinate time.
99    /// - `samples` — uniformly spaced `LocalSpacetime` snapshots (see
100    ///   [`proper_time_interval_samples`] for details and example).
101    ///
102    /// # Returns
103    /// The relativistic correction as a `TSpan`.
104    pub const fn relativistic_correction_with_samples(
105        self,
106        end: Dt,
107        samples: &[LocalSpacetime],
108    ) -> TSpan {
109        let dtau = self.proper_time_interval_samples(end, samples);
110        let dt = end.to_tai_since(self);
111        dtau.sub(dt)
112    }
113
114    /// Private helper: instantaneous proper-time rate dτ/dt from a `LocalSpacetime` snapshot.
115    #[inline]
116    const fn rate_from_local(spacetime: &LocalSpacetime) -> Real {
117        let drift = ClockDrift::from_local_spacetime(spacetime);
118        f!(1.0) + drift.rate().to_sec_f()
119    }
120}