Skip to main content

stem_branch/
lib.rs

1//! Native Rust port of stem-branch's solar ephemeris core.
2//!
3//! Computes the Sun's geocentric ecliptic state from the full VSOP87D Earth
4//! series plus a JPL DE441-fitted correction polynomial and IAU2000B nutation.
5//! Input is a Julian Ephemeris Day in Terrestrial Time (JDE/TT); the UTC<->TT /
6//! ΔT boundary is the caller's responsibility.
7//!
8//! This is a faithful translation of the TypeScript `solarEclipticState`; the
9//! VSOP87D and IAU2000B coefficient tables are generated from the same source
10//! by `scripts/gen-rust-solar-data.mjs`.
11
12#![forbid(unsafe_code)]
13
14mod nutation_data;
15mod vsop87d_earth;
16
17use core::f64::consts::{PI, TAU};
18use nutation_data::NUT_COEFFS;
19use vsop87d_earth::{EARTH_L, EARTH_R};
20
21/// Radians per arcsecond (π / 180 / 3600).
22const ARCSEC_TO_RAD: f64 = PI / 180.0 / 3600.0;
23/// Degrees per radian.
24const RAD_TO_DEG: f64 = 180.0 / PI;
25
26/// Geocentric solar ecliptic state at a Julian Ephemeris Day (TT).
27#[derive(Debug, Clone, Copy, PartialEq)]
28pub struct SolarState {
29    /// Geocentric ecliptic longitude, mean equinox of date (no nutation/
30    /// aberration), in degrees `[0, 360)`.
31    pub true_longitude_degrees: f64,
32    /// Apparent geocentric ecliptic longitude — `true` plus IAU2000B nutation
33    /// in longitude and aberration (true equinox of date), in degrees `[0, 360)`.
34    pub apparent_longitude_degrees: f64,
35    /// Sun–Earth distance in astronomical units.
36    pub radius_au: f64,
37}
38
39/// Compute the Sun's geocentric ecliptic state at the given Julian Ephemeris
40/// Day in Terrestrial Time.
41pub fn solar_ecliptic_state(jde_tt: f64) -> SolarState {
42    let tau = (jde_tt - 2451545.0) / 365250.0; // Julian millennia from J2000 (TT)
43    let t = (jde_tt - 2451545.0) / 36525.0; // Julian centuries from J2000 (TT)
44
45    // VSOP87D heliocentric longitude (rad) and radius (AU).
46    let mut lon = eval_vsop_series(EARTH_L, tau);
47    let r = eval_vsop_series(EARTH_R, tau);
48
49    // DE441-fitted even-polynomial correction (arcseconds -> radians). The
50    // literal 206264.806 mirrors the TypeScript source.
51    let tau2 = tau * tau;
52    lon += (-0.106674 - 0.616597 * tau2 + 0.315446 * tau2 * tau2 - 0.050315 * tau2 * tau2 * tau2)
53        / 206264.806;
54
55    // Heliocentric -> geocentric (+180°): mean equinox of date before nutation
56    // and aberration.
57    let geo_true = lon + PI;
58
59    // Apparent place: + IAU2000B nutation in longitude + aberration.
60    let (dl, dlp, df, dd, dom) = delaunay_args(t);
61    let dpsi = nutation_dpsi(dl, dlp, df, dd, dom, t);
62    let apparent = geo_true + dpsi * ARCSEC_TO_RAD + (-20.4898 / r) * ARCSEC_TO_RAD;
63
64    SolarState {
65        true_longitude_degrees: normalize_radians(geo_true) * RAD_TO_DEG,
66        apparent_longitude_degrees: normalize_radians(apparent) * RAD_TO_DEG,
67        radius_au: r,
68    }
69}
70
71/// Evaluate a VSOP87 series: a sum over powers of `tau`, each power weighting a
72/// sum of `A * cos(B + C * tau)` terms.
73fn eval_vsop_series(series: &[&[[f64; 3]]], tau: f64) -> f64 {
74    let mut result = 0.0;
75    let mut tau_pow = 1.0;
76    for terms in series {
77        let mut sum = 0.0;
78        for term in *terms {
79            sum += term[0] * (term[1] + term[2] * tau).cos();
80        }
81        result += sum * tau_pow;
82        tau_pow *= tau;
83    }
84    result
85}
86
87/// The five Delaunay fundamental arguments (radians) at Julian century `t` (TT).
88/// Returns `(l, l', F, D, Ω)`. Source: IERS Conventions (2010), Table 5.2a.
89fn delaunay_args(t: f64) -> (f64, f64, f64, f64, f64) {
90    let t2 = t * t;
91    let t3 = t2 * t;
92    let t4 = t3 * t;
93    // `% 1296000.0` (arcseconds in 360°) reduces each argument; the f64 `%`
94    // operator matches JavaScript's truncated remainder for a faithful port.
95    let l = ((485868.249036 + 1717915923.2178 * t + 31.8792 * t2 + 0.051635 * t3
96        - 0.00024470 * t4)
97        % 1296000.0)
98        * ARCSEC_TO_RAD;
99    let lp = ((1287104.79305 + 129596581.0481 * t - 0.5532 * t2 + 0.000136 * t3 - 0.00001149 * t4)
100        % 1296000.0)
101        * ARCSEC_TO_RAD;
102    let f = ((335779.526232 + 1739527262.8478 * t - 12.7512 * t2 - 0.001037 * t3
103        + 0.00000417 * t4)
104        % 1296000.0)
105        * ARCSEC_TO_RAD;
106    let d = ((1072260.70369 + 1602961601.2090 * t - 6.3706 * t2 + 0.006593 * t3 - 0.00003169 * t4)
107        % 1296000.0)
108        * ARCSEC_TO_RAD;
109    let om = ((450160.398036 - 6962890.5431 * t + 7.4722 * t2 + 0.007702 * t3 - 0.00005939 * t4)
110        % 1296000.0)
111        * ARCSEC_TO_RAD;
112    (l, lp, f, d, om)
113}
114
115/// Nutation in longitude (Δψ) in arcseconds, IAU2000B (77 lunisolar terms).
116fn nutation_dpsi(l: f64, lp: f64, f: f64, d: f64, om: f64, t: f64) -> f64 {
117    let mut dpsi = 0.0;
118    for row in NUT_COEFFS {
119        let arg = row[0] * l + row[1] * lp + row[2] * f + row[3] * d + row[4] * om;
120        dpsi += (row[5] + row[6] * t) * arg.sin();
121    }
122    // 0.1 microarcseconds -> arcseconds.
123    dpsi / 1e7
124}
125
126/// Normalize an angle in radians to `[0, 2π)`.
127fn normalize_radians(rad: f64) -> f64 {
128    ((rad % TAU) + TAU) % TAU
129}