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 delta_t;
15mod elpmpp02_data;
16mod julian;
17mod lunisolar;
18mod moon;
19mod names;
20mod new_moon;
21mod nutation_data;
22mod solar_terms;
23mod vsop87d_earth;
24
25pub use delta_t::delta_t_for_year;
26pub use julian::{jd_from_ymd, ymd_from_jd};
27pub use lunisolar::{
28    gregorian_to_lunisolar, lunar_months_for_year, lunar_new_year, CivilDate, LunarMonth,
29    LunisolarDate,
30};
31pub use moon::{moon_position, MoonState};
32pub use names::{EARTHLY_BRANCHES, HEAVENLY_STEMS};
33pub use new_moon::{find_new_moons_in_range, new_moon_jde};
34pub use solar_terms::{
35    find_solar_term_moment, solar_term_for_longitude, SOLAR_TERM_LONGITUDES, SOLAR_TERM_NAMES,
36};
37
38use core::f64::consts::{PI, TAU};
39use nutation_data::{NUT_COEFFS, NUT_OBLIQ};
40use vsop87d_earth::{EARTH_L, EARTH_R};
41
42/// Radians per arcsecond (π / 180 / 3600).
43pub(crate) const ARCSEC_TO_RAD: f64 = PI / 180.0 / 3600.0;
44/// Degrees per radian.
45pub(crate) const RAD_TO_DEG: f64 = 180.0 / PI;
46
47/// Geocentric solar ecliptic state at a Julian Ephemeris Day (TT).
48#[derive(Debug, Clone, Copy, PartialEq)]
49pub struct SolarState {
50    /// Geocentric ecliptic longitude, mean equinox of date (no nutation/
51    /// aberration), in degrees `[0, 360)`.
52    pub true_longitude_degrees: f64,
53    /// Apparent geocentric ecliptic longitude — `true` plus IAU2000B nutation
54    /// in longitude and aberration (true equinox of date), in degrees `[0, 360)`.
55    pub apparent_longitude_degrees: f64,
56    /// Sun–Earth distance in astronomical units.
57    pub radius_au: f64,
58}
59
60/// Compute the Sun's geocentric ecliptic state at the given Julian Ephemeris
61/// Day in Terrestrial Time.
62pub fn solar_ecliptic_state(jde_tt: f64) -> SolarState {
63    let tau = (jde_tt - 2451545.0) / 365250.0; // Julian millennia from J2000 (TT)
64    let t = (jde_tt - 2451545.0) / 36525.0; // Julian centuries from J2000 (TT)
65
66    // VSOP87D heliocentric longitude (rad) and radius (AU).
67    let mut lon = eval_vsop_series(EARTH_L, tau);
68    let r = eval_vsop_series(EARTH_R, tau);
69
70    // DE441-fitted even-polynomial correction (arcseconds -> radians). The
71    // literal 206264.806 mirrors the TypeScript source.
72    let tau2 = tau * tau;
73    lon += (-0.106674 - 0.616597 * tau2 + 0.315446 * tau2 * tau2 - 0.050315 * tau2 * tau2 * tau2)
74        / 206264.806;
75
76    // Heliocentric -> geocentric (+180°): mean equinox of date before nutation
77    // and aberration.
78    let geo_true = lon + PI;
79
80    // Apparent place: + IAU2000B nutation in longitude + aberration.
81    let (dl, dlp, df, dd, dom) = delaunay_args(t);
82    let dpsi = nutation_dpsi(dl, dlp, df, dd, dom, t);
83    let apparent = geo_true + dpsi * ARCSEC_TO_RAD + (-20.4898 / r) * ARCSEC_TO_RAD;
84
85    SolarState {
86        true_longitude_degrees: normalize_radians(geo_true) * RAD_TO_DEG,
87        apparent_longitude_degrees: normalize_radians(apparent) * RAD_TO_DEG,
88        radius_au: r,
89    }
90}
91
92/// Evaluate a VSOP87 series: a sum over powers of `tau`, each power weighting a
93/// sum of `A * cos(B + C * tau)` terms.
94fn eval_vsop_series(series: &[&[[f64; 3]]], tau: f64) -> f64 {
95    let mut result = 0.0;
96    let mut tau_pow = 1.0;
97    for terms in series {
98        let mut sum = 0.0;
99        for term in *terms {
100            sum += term[0] * (term[1] + term[2] * tau).cos();
101        }
102        result += sum * tau_pow;
103        tau_pow *= tau;
104    }
105    result
106}
107
108/// The five Delaunay fundamental arguments (radians) at Julian century `t` (TT).
109/// Returns `(l, l', F, D, Ω)`. Source: IERS Conventions (2010), Table 5.2a.
110pub(crate) fn delaunay_args(t: f64) -> (f64, f64, f64, f64, f64) {
111    let t2 = t * t;
112    let t3 = t2 * t;
113    let t4 = t3 * t;
114    // `% 1296000.0` (arcseconds in 360°) reduces each argument; the f64 `%`
115    // operator matches JavaScript's truncated remainder for a faithful port.
116    let l = ((485868.249036 + 1717915923.2178 * t + 31.8792 * t2 + 0.051635 * t3
117        - 0.00024470 * t4)
118        % 1296000.0)
119        * ARCSEC_TO_RAD;
120    let lp = ((1287104.79305 + 129596581.0481 * t - 0.5532 * t2 + 0.000136 * t3 - 0.00001149 * t4)
121        % 1296000.0)
122        * ARCSEC_TO_RAD;
123    let f = ((335779.526232 + 1739527262.8478 * t - 12.7512 * t2 - 0.001037 * t3
124        + 0.00000417 * t4)
125        % 1296000.0)
126        * ARCSEC_TO_RAD;
127    let d = ((1072260.70369 + 1602961601.2090 * t - 6.3706 * t2 + 0.006593 * t3 - 0.00003169 * t4)
128        % 1296000.0)
129        * ARCSEC_TO_RAD;
130    let om = ((450160.398036 - 6962890.5431 * t + 7.4722 * t2 + 0.007702 * t3 - 0.00005939 * t4)
131        % 1296000.0)
132        * ARCSEC_TO_RAD;
133    (l, lp, f, d, om)
134}
135
136/// Nutation in longitude (Δψ) in arcseconds, IAU2000B (77 lunisolar terms).
137pub(crate) fn nutation_dpsi(l: f64, lp: f64, f: f64, d: f64, om: f64, t: f64) -> f64 {
138    let mut dpsi = 0.0;
139    for row in NUT_COEFFS {
140        let arg = row[0] * l + row[1] * lp + row[2] * f + row[3] * d + row[4] * om;
141        dpsi += (row[5] + row[6] * t) * arg.sin();
142    }
143    // 0.1 microarcseconds -> arcseconds.
144    dpsi / 1e7
145}
146
147/// Nutation in obliquity (Δε) in arcseconds, IAU2000B (parallel to `NUT_COEFFS`).
148pub(crate) fn nutation_deps(l: f64, lp: f64, f: f64, d: f64, om: f64, t: f64) -> f64 {
149    let mut deps = 0.0;
150    for (row, obliq) in NUT_COEFFS.iter().zip(NUT_OBLIQ) {
151        let arg = row[0] * l + row[1] * lp + row[2] * f + row[3] * d + row[4] * om;
152        deps += (obliq[0] + obliq[1] * t) * arg.cos();
153    }
154    // 0.1 microarcseconds -> arcseconds.
155    deps / 1e7
156}
157
158/// Normalize an angle in radians to `[0, 2π)`.
159pub(crate) fn normalize_radians(rad: f64) -> f64 {
160    ((rad % TAU) + TAU) % TAU
161}