Skip to main content

deep_time/physics/
drift.rs

1//! Quadratic polynomial for relativistic corrections, clock drift, and custom timescale steering.
2//!
3//! Used to model the accumulated difference between Proper time (τ)
4//! and a coordinate time such as TT (or any other `Scale`).
5//!
6//! Information on the underlying physical model (the master Lagrangian, different
7//! regimes of behavior, and its relationship to general relativity) can be found
8//! [here](https://github.com/ragardner/deep-time/blob/main/docs/relativity.md).
9
10use crate::{
11    ATTOS_PER_SEC_I128, C_SQUARED, Dt, PLANCK_LENGTH_4, Real, Scale, Spacetime, Velocity, sqrt,
12};
13
14/// Quadratic polynomial that describes the accumulated difference between an
15/// observer’s proper time (the time measured by a real clock moving through
16/// spacetime) and a chosen coordinate time such as TT, TAI, or any other
17/// `Scale`.
18///
19/// The polynomial follows the classic form  
20/// Δt = constant + rate·Δt + accel·(Δt)²  
21/// where the three coefficients capture any fixed offset, constant drift, and
22/// quadratic acceleration of the clock. This structure is used throughout
23/// spacecraft navigation, GNSS systems, and relativistic timing pipelines to
24/// steer clocks, predict time offsets, and maintain synchronization over long
25/// durations.
26///
27/// All three coefficients are stored using [`Dt`].
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29#[cfg_attr(feature = "tsify", derive(tsify::Tsify))]
30#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
31pub struct Drift {
32    /// Constant term a₀ expressed in seconds.  
33    /// This represents any fixed time offset between the observer’s proper time
34    /// and the chosen coordinate time.
35    pub constant: Dt,
36
37    /// Linear drift rate a₁ expressed in seconds per second.  
38    /// This term captures a steady fractional rate difference (for example, a
39    /// clock that runs consistently fast or slow).
40    pub rate: Dt,
41
42    /// Quadratic acceleration term a₂ expressed in seconds per second squared.  
43    /// This term accounts for any changing drift rate, such as the gradual
44    /// acceleration caused by relativistic effects or hardware aging.
45    pub accel: Dt,
46}
47
48impl Drift {
49    /// Creates a new `Drift` polynomial from its three coefficients.
50    #[inline]
51    pub const fn new(constant: Dt, rate: Dt, accel: Dt) -> Drift {
52        Self {
53            constant,
54            rate,
55            accel,
56        }
57    }
58
59    /// The zero polynomial representing no correction at all.
60    ///
61    /// Use this when the observer’s clock is already perfectly synchronized with
62    /// the chosen coordinate time.
63    pub const ZERO: Self = Self::new(Dt::ZERO, Dt::ZERO, Dt::ZERO);
64
65    /// Creates a [`Drift`] consisting of a pure constant offset.
66    ///
67    /// This is the most common constructor when only a fixed time bias is known
68    /// (for example, after a one-time clock synchronization or leap-second
69    /// adjustment).
70    #[inline]
71    pub const fn from_constant(c: Dt) -> Drift {
72        Self::new(c, Dt::ZERO, Dt::ZERO)
73    }
74
75    /// Creates a [`Drift`] consisting of a constant offset together with a
76    /// constant linear drift rate.  
77    ///
78    /// This form is very common for GNSS receivers and spacecraft clock steering,
79    /// where a steady fractional frequency offset must be corrected in addition
80    /// to any fixed bias.
81    #[inline]
82    pub const fn from_offset_and_rate(offset: Dt, rate: Dt) -> Drift {
83        Self::new(offset, rate, Dt::ZERO)
84    }
85
86    /// Returns the instantaneous proper-time rate `dτ/dt` (dimensionless).
87    ///
88    /// This value tells you how fast a real physical clock (such as a spacecraft
89    /// onboard clock) is advancing compared to coordinate time. A value of
90    /// `1.0` means the clock runs at the normal rate. Values slightly below `1.0`
91    /// are typical when the clock is moving or sitting in a gravitational well.
92    ///
93    /// The rate includes special-relativistic velocity effects, gravitational
94    /// time dilation, and the library’s built-in Planck-scale saturation term.
95    #[inline]
96    pub const fn proper_time_rate(&self) -> Real {
97        f!(1.0) + self.rate.to_sec_f()
98    }
99
100    /// Evaluates the polynomial at the given elapsed coordinate time span.  
101    ///
102    /// Returns the accumulated time difference (in seconds) between proper
103    /// time and coordinate time after the interval span has passed.
104    pub const fn time_diff_after(&self, span: &Dt) -> Dt {
105        let dt_attos = span.to_attos();
106        let mut total_attos = self.constant.to_attos();
107
108        if !self.rate.is_zero() || !self.accel.is_zero() {
109            // Linear term: rate * dt
110            let rate_attos: i128 = self.rate.to_attos();
111            let rate_term = rate_attos.wrapping_mul(dt_attos) / ATTOS_PER_SEC_I128;
112            total_attos = total_attos.wrapping_add(rate_term);
113
114            // Quadratic term: accel * dt²
115            let accel_attos: i128 = self.accel.to_attos();
116            let accel_dt = accel_attos.wrapping_mul(dt_attos) / ATTOS_PER_SEC_I128;
117            let accel_term = accel_dt.wrapping_mul(dt_attos) / ATTOS_PER_SEC_I128;
118            total_attos = total_attos.saturating_add(accel_term);
119        }
120
121        Dt::span(total_attos)
122    }
123
124    /// Evaluates the deterministic relativistic/polynomial correction **and**
125    /// adds a user-supplied stochastic offset (in seconds).
126    ///
127    /// This is the single production method for realistic stochastic clock
128    /// modeling. In real mission pipelines the deterministic part (this
129    /// polynomial) is kept perfectly clean; stochastic noise (white phase noise,
130    /// random-walk frequency noise, Monte-Carlo realizations, Kalman process
131    /// noise, measured clock residuals, etc.) is added at evaluation time.
132    ///
133    /// Pass `0.0` (or simply call the original `time_diff_after`) when you
134    /// want purely deterministic behavior.
135    #[inline]
136    pub fn time_diff_after_with_noise(&self, span: &Dt, stochastic_offset_sec: Real) -> Dt {
137        self.time_diff_after(span)
138            .add(Dt::from_sec_f(stochastic_offset_sec, Scale::TAI))
139    }
140
141    /// Creates a `Drift` directly from an observer’s velocity and total
142    /// local gravitational potential using the library’s unified master-Lagrangian
143    /// proper-time rate.  
144    ///
145    /// It automatically computes the relativistic clock rate that includes both
146    /// special-relativistic velocity effects and gravitational time dilation,
147    /// then returns a [`Drift`] that can be evaluated at any future time.
148    ///
149    /// The `characteristic_length_scale` parameter controls whether the
150    /// weak-field or strong-field formulation is used:
151    ///
152    /// - In the weak-field regime (where |Φ|/c² ≪ 1), simply pass
153    ///   `characteristic_length_scale = 0.0`. This returns the same
154    ///   relativistic clock rate used by JPL, ESA, GNSS systems, and all modern
155    ///   solar-system navigation pipelines.
156    /// - In strong-field conditions, supply a non-zero length scale (in meters)
157    ///   over which the gravitational potential changes at the observer’s
158    ///   location. This activates the library’s intrinsic Planck-scale saturation
159    ///   term when spacetime curvature becomes extreme.
160    pub const fn from_velocity_potential_and_scale(
161        velocity_m_s: Real,
162        grav_potential_m2_s2: Real,
163        characteristic_length_scale: Real,
164    ) -> Drift {
165        let phi = grav_potential_m2_s2 / C_SQUARED;
166        let velocity = Velocity::from_speed(velocity_m_s);
167        let spacetime = Spacetime::from_potential_velocity_and_scale(
168            phi,
169            velocity,
170            characteristic_length_scale,
171        );
172        Self::from_spacetime(&spacetime)
173    }
174
175    /// Canonical low-level constructor that implements the library's general
176    /// relativity formula.
177    ///
178    /// This function is the single source of truth for the proper-time rate
179    /// calculation used throughout the library. Most users will never call it
180    /// directly; the high-level constructors `from_velocity_potential_and_scale`
181    /// and `from_spacetime` are the intended entry points.
182    ///
183    /// The internal expression is  
184    /// K_eff = [δ(1 + x) + x(1−δ)²] / (1 + x)  
185    /// where δ = α²(1−β²) and x = ℓ_Pl⁴ 𝒦.
186    ///
187    /// The returned rate offset is then applied as a linear term in the `Drift`
188    /// polynomial.
189    pub const fn from_unified_proper_time_rate(u: Real, kretschmann: Real) -> Drift {
190        let delta = u.max(f!(0.0));
191        let x = PLANCK_LENGTH_4 * kretschmann.max(f!(0.0));
192
193        let one_minus_delta = f!(1.0) - delta;
194        let num = delta * (f!(1.0) + x) + x * (one_minus_delta * one_minus_delta);
195        let k_eff = num / (f!(1.0) + x);
196
197        let rate_factor = sqrt(k_eff).max(f!(0.0));
198        let rate_offset = rate_factor - f!(1.0);
199
200        Self::from_offset_and_rate(Dt::ZERO, Dt::from_sec_f(rate_offset, Scale::TAI))
201    }
202
203    /// Creates a `Drift` from a fully resolved `Spacetime` snapshot.  
204    ///
205    /// This is the canonical high-level entry point when you already hold a
206    /// `Spacetime` object containing the gravitational lapse factor α, the
207    /// local velocity β, and the Kretschmann scalar. It internally computes the
208    /// unified proper-time rate and packages the result as a `Drift`
209    /// polynomial ready for evaluation at any future time.
210    #[inline]
211    pub const fn from_spacetime(spacetime: &Spacetime) -> Drift {
212        let u = spacetime.alpha * spacetime.alpha * (f!(1.0) - spacetime.beta * spacetime.beta);
213        Self::from_unified_proper_time_rate(u, spacetime.kretschmann)
214    }
215}
216
217impl Dt {
218    /// Builds a clock-drift model in which this [`Dt`] is treated as the
219    /// initial fixed time difference between the observer’s proper time and
220    /// the chosen coordinate time.
221    ///
222    /// In practice you often compute or measure a one-time offset (for example
223    /// after a clock synchronization or a leap-second jump) and then want to
224    /// combine it with a steady rate difference and any quadratic change.
225    /// This method lets you do that directly from a [`Dt`] without having to
226    /// call the more verbose [`Drift::new`].
227    ///
228    /// The other two arguments describe how the difference between the two
229    /// clocks will evolve:
230    /// - `rate` — the constant fractional speed difference (how much faster or
231    ///   slower one clock runs compared with the other).
232    /// - `accel` — how quickly that speed difference itself is changing (for
233    ///   example because the spacecraft is moving through a varying gravitational
234    ///   field).
235    ///
236    /// See [`Drift`] and [`Drift::from_offset_and_rate`] for more background on
237    /// why these three numbers are used to model real clocks.
238    #[inline]
239    pub const fn to_drift_as_constant(self, rate: Dt, accel: Dt) -> Drift {
240        Drift::new(self, rate, accel)
241    }
242
243    /// Builds a clock-drift model in which this [`Dt`] supplies the constant
244    /// fractional rate difference between the observer’s proper time and the
245    /// chosen coordinate time.
246    ///
247    /// If you have already calculated (or measured) a steady rate offset as a
248    /// [`Dt`], you can use this method to attach an initial time offset and a
249    /// quadratic term and obtain a complete [`Drift`] polynomial.
250    ///
251    /// Physically, the rate term captures the fact that two clocks that are
252    /// moving at different velocities or sitting at different gravitational
253    /// potentials will accumulate a steadily growing time difference. The
254    /// other two parameters let you also describe any starting bias and any
255    /// change in that rate over time.
256    ///
257    /// See the documentation on [`Drift`] for the meaning of the three
258    /// coefficients in a relativistic timing context.
259    #[inline]
260    pub const fn to_drift_as_rate(self, constant: Dt, accel: Dt) -> Drift {
261        Drift::new(constant, self, accel)
262    }
263
264    /// Builds a clock-drift model in which this [`Dt`] supplies the quadratic
265    /// term that describes how the rate difference itself is changing.
266    ///
267    /// Some situations (a spacecraft on a highly elliptical orbit, a clock
268    /// whose frequency is aging, or a trajectory that takes it through regions
269    /// of changing gravitational potential) cause the *rate* at which two
270    /// clocks diverge to change over time. If you have computed that changing
271    /// rate as a [`Dt`], this method lets you combine it with an initial offset
272    /// and a base rate to form a full [`Drift`].
273    ///
274    /// The other two arguments are:
275    /// - `constant` — any fixed time bias present at the start.
276    /// - `rate` — the base fractional rate difference that will itself be
277    ///   modified by the quadratic term supplied by `self`.
278    ///
279    /// See [`Drift`] for more explanation of why a quadratic model is used for
280    /// relativistic clock predictions.
281    #[inline]
282    pub const fn to_drift_as_accel(self, constant: Dt, rate: Dt) -> Drift {
283        Drift::new(constant, rate, self)
284    }
285
286    /// Advances this `Dt` by the given elapsed duration while applying the relativistic proper-time correction
287    /// derived from the supplied `Spacetime` model.
288    ///
289    /// - This method is intended for simulation of remote clocks (e.g., Earth time as observed from a spacecraft).
290    /// - For a local hardware proper-time clock, use the plain `add` methods instead.
291    #[inline]
292    pub const fn adjusted_advance(&mut self, elapsed: &Dt, spacetime: &Spacetime) {
293        let dtau = elapsed.add(Drift::from_spacetime(spacetime).time_diff_after(elapsed));
294        *self = self.add(dtau);
295    }
296
297    /// Advances this `Dt` by the given elapsed duration while applying the relativistic proper-time correction
298    /// from a pre-computed `Drift` value.
299    ///
300    /// - This is an optimized variant of [`Dt::adjusted_advance`](../struct.Dt.html#method.adjusted_advance)
301    ///   for callers that already hold a [`Drift`] instance.
302    /// - This method is intended for simulation of remote clocks (e.g., Earth time as observed from a spacecraft).
303    /// - For a local hardware proper-time clock, use the plain `add` methods instead.
304    #[inline]
305    pub const fn adjusted_advance_using_drift(&mut self, elapsed: &Dt, drift: &Drift) {
306        let dtau = elapsed.add(drift.time_diff_after(elapsed));
307        *self = self.add(dtau);
308    }
309
310    /// Converts this instant to any other [`Scale`] while applying an exact quadratic relativistic
311    /// or clock-drift correction defined by a [`Drift`] model relative to a reference instant.
312    pub const fn convert_using_drift(self, reference: Dt, drift: Drift) -> Dt {
313        let span = self.to_diff_raw(reference);
314        let correction = drift.time_diff_after(&span);
315        self.add(correction)
316    }
317
318    /// Performs the inverse conversion of [`Dt::convert_using_drift`], recovering the original proper
319    /// time on the source clock scale.
320    ///
321    /// A fixed-point iteration (at most 16 steps) is used to solve the implicit equation. For the common
322    /// case of a pure constant offset the function returns immediately without iteration.
323    pub const fn convert_back_using_drift(self, reference: Dt, drift: Drift) -> Dt {
324        if drift.rate.is_zero() && drift.accel.is_zero() {
325            return self.sub(drift.constant);
326        }
327        let mut guess = self;
328        let mut i = 0u32;
329        while i < 16 {
330            let span = guess.to_diff_raw(reference);
331            let correction = drift.time_diff_after(&span);
332            guess = self.sub(correction);
333            i += 1;
334        }
335        guess
336    }
337}
338
339#[cfg(feature = "wire")]
340impl Drift {
341    /// Current wire format version.
342    pub const WIRE_VERSION: u8 = 1;
343
344    /// Size of the canonical wire representation in bytes.
345    pub const WIRE_SIZE: usize = 3 * Dt::WIRE_SIZE; // 3 × 17 = 51
346
347    /// Serializes this [`Drift`] polynomial into a fixed buffer.
348    ///
349    /// The layout is the concatenation of the three `Dt` fields.
350    pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
351        let mut buf = [0u8; Self::WIRE_SIZE];
352        let c = self.constant.to_wire_bytes();
353        let r = self.rate.to_wire_bytes();
354        let a = self.accel.to_wire_bytes();
355
356        buf[0..Dt::WIRE_SIZE].copy_from_slice(&c);
357        buf[Dt::WIRE_SIZE..2 * Dt::WIRE_SIZE].copy_from_slice(&r);
358        buf[2 * Dt::WIRE_SIZE..].copy_from_slice(&a);
359        buf
360    }
361
362    /// Deserializes a [`Drift`] from exactly `WIRE_SIZE` bytes of wire data.
363    ///
364    /// Returns `None` if any nested `Dt` fails validation or if the version
365    /// byte is unknown.
366    ///
367    /// ## Security
368    ///
369    /// Composes the safety guarantees of
370    /// [`from_wire_bytes`](docs.rs/deep-time/latest/deep_time/struct.Dt.html#method.from_wire_bytes).
371    ///
372    /// Fixed size and layered validation make it safe for untrusted input.
373    pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
374        if bytes.len() != Self::WIRE_SIZE {
375            return None;
376        }
377
378        if bytes[0] != Self::WIRE_VERSION {
379            return None;
380        }
381
382        let constant = Dt::from_wire_bytes(&bytes[0..Dt::WIRE_SIZE])?;
383        let rate = Dt::from_wire_bytes(&bytes[Dt::WIRE_SIZE..2 * Dt::WIRE_SIZE])?;
384        let accel = Dt::from_wire_bytes(&bytes[2 * Dt::WIRE_SIZE..])?;
385
386        Some(Self::new(constant, rate, accel))
387    }
388}