arika 0.2.0

Coordinate frames, reference transforms, epochs, and celestial body ephemerides for orbital mechanics.
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
//! Meeus analytical solar ephemeris and Equation of Time.
//!
//! Low-precision (~1 arcminute) analytic model from Meeus "Astronomical
//! Algorithms" Chapter 25 / Chapter 28. Returns positions in [`crate::frame::Gcrs`]
//! (the analytical "geocentric inertial") rather than [`crate::frame::SimpleEci`] —
//! Meeus ephemerides apply no precession/nutation/frame-bias, so numerically
//! the two frames agree, but the returned type documents that this is not a
//! propagator state vector.
//!
//! # Time scale
//!
//! Meeus ephemerides take a dynamical time argument (TDB). The public
//! signatures accept `&Epoch<Utc>` (the default alias) for ergonomics; the
//! UTC epoch is converted to TDB internally via leap seconds + TT offset +
//! Fairhead-Bretagnon periodic correction.

use nalgebra::Vector3;

#[allow(unused_imports)]
use crate::math::F64Ext;

use crate::epoch::Epoch;
use crate::frame::{self, Vec3};
use crate::planets;
use crate::sun::AU_KM;

/// Solar orbital elements at epoch.
///
/// Intermediate values used by both `sun_direction_eci` and `equation_of_time`.
struct SolarElements {
    /// Mean longitude [degrees] (not normalized to 0-360)
    l0_deg: f64,
    /// Ecliptic longitude [radians]
    lambda_rad: f64,
    /// Obliquity of the ecliptic [radians]
    epsilon_rad: f64,
}

/// Compute solar orbital elements at the given epoch.
///
/// Reference: Meeus, "Astronomical Algorithms", Chapter 25.
fn solar_elements(epoch: &Epoch) -> SolarElements {
    let t = epoch.to_tdb().centuries_since_j2000();

    // Mean longitude (degrees)
    let l0 = 280.46646 + 36000.76983 * t;
    // Mean anomaly (degrees)
    let m_deg = 357.52911 + 35999.05029 * t;
    let m = m_deg.to_radians();

    // Equation of center (degrees)
    let c = (1.9146 - 0.004817 * t) * m.sin() + 0.019993 * (2.0 * m).sin();

    // Sun's ecliptic longitude (radians)
    let lambda = (l0 + c).to_radians();

    // Obliquity of the ecliptic
    let epsilon = planets::obliquity(epoch);

    SolarElements {
        l0_deg: l0,
        lambda_rad: lambda,
        epsilon_rad: epsilon,
    }
}

/// Approximate sun direction (unit vector) in ECI (J2000) frame.
///
/// Uses a low-precision analytical model based on mean orbital elements.
/// Accuracy is ~1 arcminute, sufficient for visualization purposes.
///
/// Reference: Meeus, "Astronomical Algorithms", Chapter 25.
pub fn sun_direction_eci(epoch: &Epoch) -> Vec3<frame::Gcrs> {
    let el = solar_elements(epoch);

    // Sun direction in ECI (equatorial coordinates)
    let x = el.lambda_rad.cos();
    let y = el.epsilon_rad.cos() * el.lambda_rad.sin();
    let z = el.epsilon_rad.sin() * el.lambda_rad.sin();

    Vec3::from_raw(Vector3::new(x, y, z).normalize())
}

/// Equation of Time [hours].
///
/// Returns `apparent_solar_time - mean_solar_time`.
/// Positive means the apparent Sun is ahead of the mean Sun.
///
/// Range: approximately -0.27 to +0.27 hours (-16 to +16 minutes).
///
/// Reference: Meeus, "Astronomical Algorithms", Chapter 28.
pub fn equation_of_time(epoch: &Epoch) -> f64 {
    let el = solar_elements(epoch);

    // Right ascension from ecliptic longitude
    let alpha_rad = f64::atan2(
        el.epsilon_rad.cos() * el.lambda_rad.sin(),
        el.lambda_rad.cos(),
    );

    // EoT = L₀ - α (apparent - mean), then convert to hours
    // Positive in November (sundial fast), negative in February (sundial slow).
    let l0_rad = el.l0_deg.to_radians();
    let mut eot_rad = l0_rad - alpha_rad;

    // Normalize to [-π, π]
    eot_rad = ((eot_rad + core::f64::consts::PI) % core::f64::consts::TAU + core::f64::consts::TAU)
        % core::f64::consts::TAU
        - core::f64::consts::PI;

    // Convert radians to hours: 2π rad = 24 hours
    eot_rad * 24.0 / core::f64::consts::TAU
}

/// Sun-Earth distance [km] at the given epoch.
///
/// Uses simplified Meeus model with eccentricity correction.
/// Accuracy: ~0.01 AU (~1.5 million km), sufficient for perturbation calculations.
///
/// Reference: Meeus, "Astronomical Algorithms", Chapter 25.
pub fn sun_distance_km(epoch: &Epoch) -> f64 {
    // Meeus ephemeris uses TDB dynamical time; convert UTC → TDB internally.
    let t = epoch.to_tdb().centuries_since_j2000();

    let m_deg = 357.52911 + 35999.05029 * t;
    let m = m_deg.to_radians();

    // Distance in AU (Meeus Eq. 25.5)
    let r_au = 1.000_140_12 - 0.016_708_17 * m.cos() - 0.000_139_89 * (2.0 * m).cos();

    r_au * AU_KM
}

/// Sun position vector in ECI (J2000) frame [km].
///
/// Returns the geocentric position of the Sun. Combines direction and distance.
pub fn sun_position_eci(epoch: &Epoch) -> Vec3<frame::Gcrs> {
    let direction = sun_direction_eci(epoch);
    let distance = sun_distance_km(epoch);
    direction * distance
}

/// Sun distance [km] from a given central body.
///
/// - `"earth"` / `"moon"`: delegates to [`sun_distance_km`]
/// - Other known planets: computed from heliocentric orbital elements
/// - Unknown bodies: fallback to Earth-Sun distance
pub fn sun_distance_from_body(body: &str, epoch: &Epoch) -> f64 {
    match body {
        "earth" | "moon" => sun_distance_km(epoch),
        _ => planets::heliocentric_position_ecliptic(body, epoch)
            .map(|p| p.magnitude())
            .unwrap_or_else(|| sun_distance_km(epoch)),
    }
}

/// Sun direction (unit vector) as seen from a given central body, in J2000 equatorial frame.
///
/// - `"earth"` / `"moon"`: delegates to [`sun_direction_eci`] (Moon parallax < 0.15°, negligible)
/// - Other known planets: computed from heliocentric orbital elements
/// - Unknown bodies: fallback to +X direction (vernal equinox)
///
/// The returned vector points FROM the body TOWARD the Sun.
pub fn sun_direction_from_body(body: &str, epoch: &Epoch) -> Vec3<frame::Gcrs> {
    match body {
        "earth" | "moon" => sun_direction_eci(epoch),
        _ => {
            if let Some(body_pos_ecl) = planets::heliocentric_position_ecliptic(body, epoch) {
                // Sun is at origin in heliocentric frame, so direction to sun = -body_pos
                let sun_dir_ecl = -body_pos_ecl;
                let epsilon = planets::obliquity(epoch);
                Vec3::from_raw(planets::ecliptic_to_equatorial(&sun_dir_ecl, epsilon).normalize())
            } else {
                // Unknown body: fallback to +X (vernal equinox direction)
                Vec3::new(1.0, 0.0, 0.0)
            }
        }
    }
}

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

    // --- Equation of Time tests ---

    #[test]
    fn eot_february_negative() {
        // Mid-February: EoT ≈ -14 minutes (sundial slow, apparent sun behind mean sun)
        let epoch = Epoch::from_gregorian(2024, 2, 12, 12, 0, 0.0);
        let eot_min = equation_of_time(&epoch) * 60.0;
        assert!(
            (eot_min - (-14.0)).abs() < 2.0,
            "Feb 12: EoT={eot_min:.1} min, expected ~-14"
        );
    }

    #[test]
    fn eot_november_positive() {
        // Early November: EoT ≈ +16 minutes (sundial fast, apparent sun ahead of mean sun)
        let epoch = Epoch::from_gregorian(2024, 11, 3, 12, 0, 0.0);
        let eot_min = equation_of_time(&epoch) * 60.0;
        assert!(
            (eot_min - 16.0).abs() < 2.0,
            "Nov 3: EoT={eot_min:.1} min, expected ~+16"
        );
    }

    #[test]
    fn eot_april_near_zero() {
        // Mid-April: EoT ≈ 0 minutes (one of the four zero-crossings)
        let epoch = Epoch::from_gregorian(2024, 4, 15, 12, 0, 0.0);
        let eot_min = equation_of_time(&epoch) * 60.0;
        assert!(
            eot_min.abs() < 2.0,
            "Apr 15: EoT={eot_min:.1} min, expected ~0"
        );
    }

    #[test]
    fn eot_annual_range() {
        // EoT should stay within ±17 minutes throughout the year
        for month in 1..=12 {
            let epoch = Epoch::from_gregorian(2024, month, 15, 12, 0, 0.0);
            let eot_min = equation_of_time(&epoch) * 60.0;
            assert!(
                eot_min.abs() < 17.0,
                "Month {month}: EoT={eot_min:.1} min, out of range"
            );
        }
    }

    // --- Sun direction tests ---

    #[test]
    fn sun_direction_is_unit_vector() {
        // Check at several dates across a year
        let dates = [
            Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 3, 20, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 6, 21, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 9, 22, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 12, 21, 12, 0, 0.0),
        ];
        for epoch in &dates {
            let dir = sun_direction_eci(epoch);
            let norm = dir.magnitude();
            assert!(
                (norm - 1.0).abs() < 1e-10,
                "Not unit vector at JD {}: norm = {norm}",
                epoch.jd()
            );
        }
    }

    #[test]
    fn march_equinox_sun_near_plus_x() {
        // At March equinox (~2024-03-20), sun is near +X direction (RA ≈ 0°)
        let epoch = Epoch::from_gregorian(2024, 3, 20, 3, 6, 0.0); // ~03:06 UTC is 2024 equinox
        let dir = sun_direction_eci(&epoch);

        // X should be dominant and positive
        assert!(
            dir.x() > 0.9,
            "March equinox: x={:.3} should be > 0.9",
            dir.x()
        );
        // Y and Z should be small
        assert!(
            dir.y().abs() < 0.2,
            "March equinox: y={:.3} should be near 0",
            dir.y()
        );
        assert!(
            dir.z().abs() < 0.1,
            "March equinox: z={:.3} should be near 0",
            dir.z()
        );
    }

    #[test]
    fn june_solstice_sun_positive_z() {
        // At June solstice (~2024-06-20), sun has significant +Z (northern declination ~23.4°)
        let epoch = Epoch::from_gregorian(2024, 6, 20, 20, 51, 0.0);
        let dir = sun_direction_eci(&epoch);

        // Z should be positive and near sin(23.44°) ≈ 0.398
        assert!(
            dir.z() > 0.35,
            "June solstice: z={:.3} should be > 0.35",
            dir.z()
        );
        // X should be near 0 (RA ≈ 90°)
        assert!(
            dir.x().abs() < 0.15,
            "June solstice: x={:.3} should be near 0",
            dir.x()
        );
        // Y should be dominant and positive
        assert!(
            dir.y() > 0.85,
            "June solstice: y={:.3} should be > 0.85",
            dir.y()
        );
    }

    #[test]
    fn september_equinox_sun_near_minus_x() {
        // At September equinox (~2024-09-22), sun is near -X direction (RA ≈ 180°)
        let epoch = Epoch::from_gregorian(2024, 9, 22, 12, 44, 0.0);
        let dir = sun_direction_eci(&epoch);

        // X should be dominant and negative
        assert!(
            dir.x() < -0.9,
            "September equinox: x={:.3} should be < -0.9",
            dir.x()
        );
        // Y and Z should be small
        assert!(
            dir.y().abs() < 0.2,
            "September equinox: y={:.3} should be near 0",
            dir.y()
        );
        assert!(
            dir.z().abs() < 0.1,
            "September equinox: z={:.3} should be near 0",
            dir.z()
        );
    }

    #[test]
    fn december_solstice_sun_negative_z() {
        // At December solstice (~2024-12-21), sun has significant -Z (southern declination ~-23.4°)
        let epoch = Epoch::from_gregorian(2024, 12, 21, 9, 21, 0.0);
        let dir = sun_direction_eci(&epoch);

        // Z should be negative and near -sin(23.44°) ≈ -0.398
        assert!(
            dir.z() < -0.35,
            "December solstice: z={:.3} should be < -0.35",
            dir.z()
        );
        // Y should be negative (RA ≈ 270°)
        assert!(
            dir.y() < -0.85,
            "December solstice: y={:.3} should be < -0.85",
            dir.y()
        );
    }

    #[test]
    fn sun_direction_varies_over_year() {
        // Verify the sun position actually changes throughout the year
        let epoch1 = Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0);
        let epoch2 = Epoch::from_gregorian(2024, 7, 1, 12, 0, 0.0);
        let dir1 = sun_direction_eci(&epoch1);
        let dir2 = sun_direction_eci(&epoch2);

        // Should be significantly different (roughly opposite)
        let dot = dir1.dot(&dir2);
        assert!(
            dot < 0.0,
            "Jan vs Jul sun directions should be roughly opposite, dot={dot:.3}"
        );
    }

    // --- Sun distance tests ---

    #[test]
    fn sun_distance_approximately_1au() {
        let epoch = Epoch::from_gregorian(2024, 3, 20, 12, 0, 0.0);
        let d = sun_distance_km(&epoch);
        let d_au = d / AU_KM;
        assert!(
            (d_au - 1.0).abs() < 0.02,
            "Sun distance should be ~1 AU, got {d_au:.4} AU"
        );
    }

    #[test]
    fn perihelion_closer_than_aphelion() {
        // Perihelion ~Jan 3, Aphelion ~Jul 4
        let perihelion = Epoch::from_gregorian(2024, 1, 3, 12, 0, 0.0);
        let aphelion = Epoch::from_gregorian(2024, 7, 5, 12, 0, 0.0);

        let d_peri = sun_distance_km(&perihelion);
        let d_aph = sun_distance_km(&aphelion);

        assert!(
            d_peri < d_aph,
            "Perihelion ({d_peri:.0} km) should be closer than aphelion ({d_aph:.0} km)"
        );
        // Eccentricity ~0.0167, so difference should be ~3.3%
        let ratio = d_aph / d_peri;
        assert!(
            (ratio - 1.034).abs() < 0.01,
            "Aphelion/perihelion ratio should be ~1.034, got {ratio:.4}"
        );
    }

    #[test]
    fn sun_position_magnitude_matches_distance() {
        let epoch = Epoch::from_gregorian(2024, 6, 15, 12, 0, 0.0);
        let pos = sun_position_eci(&epoch);
        let dist = sun_distance_km(&epoch);

        let rel_err = (pos.magnitude() - dist).abs() / dist;
        assert!(
            rel_err < 1e-10,
            "Position magnitude should match distance, rel_err={rel_err:.6e}"
        );
    }

    // --- sun_direction_from_body tests ---

    #[test]
    fn sun_direction_from_body_earth_matches_eci() {
        let dates = [
            Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 6, 21, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 9, 22, 12, 0, 0.0),
        ];
        for epoch in &dates {
            let from_body = sun_direction_from_body("earth", epoch);
            let eci = sun_direction_eci(epoch);
            let diff = (from_body - eci).magnitude();
            assert!(
                diff < 1e-10,
                "earth should match sun_direction_eci, diff={diff:.2e}"
            );
        }
    }

    #[test]
    fn sun_direction_from_body_moon_matches_eci() {
        let epoch = Epoch::from_gregorian(2024, 3, 20, 12, 0, 0.0);
        let from_body = sun_direction_from_body("moon", &epoch);
        let eci = sun_direction_eci(&epoch);
        let diff = (from_body - eci).magnitude();
        assert!(
            diff < 1e-10,
            "moon should match sun_direction_eci, diff={diff:.2e}"
        );
    }

    #[test]
    fn sun_direction_from_body_mars_is_unit_vector() {
        let dates = [
            Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 6, 15, 12, 0, 0.0),
            Epoch::from_gregorian(2024, 12, 1, 12, 0, 0.0),
        ];
        for epoch in &dates {
            let dir = sun_direction_from_body("mars", epoch);
            let norm = dir.magnitude();
            assert!(
                (norm - 1.0).abs() < 1e-10,
                "Mars sun direction should be unit vector, norm={norm}"
            );
        }
    }

    #[test]
    fn sun_direction_from_body_mars_varies() {
        let epoch1 = Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0);
        let epoch2 = Epoch::from_gregorian(2024, 7, 1, 12, 0, 0.0);
        let dir1 = sun_direction_from_body("mars", &epoch1);
        let dir2 = sun_direction_from_body("mars", &epoch2);
        let dot = dir1.dot(&dir2);
        assert!(
            dot < 0.9,
            "Mars sun direction should change significantly over 6 months, dot={dot:.3}"
        );
    }

    #[test]
    fn sun_direction_from_body_unknown_fallback() {
        let epoch = Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0);
        let dir = sun_direction_from_body("pluto", &epoch);
        assert!(
            (dir.x() - 1.0).abs() < 1e-10 && dir.y().abs() < 1e-10 && dir.z().abs() < 1e-10,
            "Unknown body should return +X fallback, got ({}, {}, {})",
            dir.x(),
            dir.y(),
            dir.z()
        );
    }

    // --- sun_distance_from_body tests ---

    #[test]
    fn sun_distance_from_body_earth_matches() {
        let epoch = Epoch::from_gregorian(2024, 6, 15, 12, 0, 0.0);
        let from_body = sun_distance_from_body("earth", &epoch);
        let direct = sun_distance_km(&epoch);
        assert!(
            (from_body - direct).abs() < 1.0,
            "earth distance should match sun_distance_km: {from_body} vs {direct}"
        );
    }

    #[test]
    fn sun_distance_from_body_mars() {
        let epoch = Epoch::from_gregorian(2024, 6, 15, 12, 0, 0.0);
        let dist = sun_distance_from_body("mars", &epoch);
        let dist_au = dist / AU_KM;
        assert!(
            dist_au > 1.3 && dist_au < 1.7,
            "Mars-Sun distance should be 1.3-1.7 AU, got {dist_au:.4} AU"
        );
    }

    #[test]
    fn sun_distance_from_body_jupiter() {
        let epoch = Epoch::from_gregorian(2024, 6, 15, 12, 0, 0.0);
        let dist = sun_distance_from_body("jupiter", &epoch);
        let dist_au = dist / AU_KM;
        assert!(
            dist_au > 4.5 && dist_au < 5.8,
            "Jupiter-Sun distance should be 4.5-5.8 AU, got {dist_au:.4} AU"
        );
    }

    #[test]
    fn sun_distance_from_body_unknown_fallback() {
        let epoch = Epoch::from_gregorian(2024, 1, 1, 12, 0, 0.0);
        let dist = sun_distance_from_body("pluto", &epoch);
        let earth_dist = sun_distance_km(&epoch);
        assert!(
            (dist - earth_dist).abs() < 1.0,
            "Unknown body should fall back to Earth distance"
        );
    }

    #[test]
    fn sun_position_direction_matches() {
        let epoch = Epoch::from_gregorian(2024, 9, 22, 12, 0, 0.0);
        let pos = sun_position_eci(&epoch);
        let dir = sun_direction_eci(&epoch);

        let pos_dir = pos.normalize();
        let diff = (pos_dir - dir).magnitude();
        assert!(
            diff < 1e-10,
            "Position direction should match unit direction, diff={diff:.6e}"
        );
    }
}