Files

166 lines
6.8 KiB
Rust
Raw Permalink Normal View History

//! Checks the Rust bindings against reference values produced by the upstream
//! `swetest` program (built from the same vendored sources), e.g.
//! `swetest -b14.7.1985 -ut12:30 -p0123456789tAD -fPls -house19.0402,47.4979,P`.
//!
//! Requires the ephemeris files from `scripts/fetch-deps.sh` (data/ephe).
use std::path::PathBuf;
use std::sync::OnceLock;
use astro_engine::chart::{AspectKind, PointId, Sect};
use astro_engine::swiss::SwissEphemeris;
use astro_engine::{Body, ChartInput, ChartOptions, Ephemeris, Frame, GeoPosition, HouseSystem, calculate_chart};
/// 1985-07-14 12:30 UT
const JD: f64 = 2_446_261.020_833_333_5;
const BUDAPEST: GeoPosition = GeoPosition { latitude: 47.4979, longitude: 19.0402, elevation: 0.0 };
fn engine() -> &'static SwissEphemeris {
static ENGINE: OnceLock<SwissEphemeris> = OnceLock::new();
ENGINE.get_or_init(|| {
let dir = std::env::var_os("ASTRO_EPHE_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../data/ephe"));
SwissEphemeris::new(dir).expect("ephemeris files (run scripts/fetch-deps.sh)")
})
}
fn close(actual: f64, expected: f64, tol: f64) -> bool {
(actual - expected).abs() <= tol
}
#[test]
fn body_positions_match_swetest() {
let expected = [
(Body::Sun, 112.0007251, 0.9541281),
(Body::Moon, 71.1646551, 12.2200994),
(Body::Mercury, 138.5356993, 0.9265284),
(Body::Venus, 68.8767351, 1.0967944),
(Body::Mars, 113.0940242, 0.6506060),
(Body::Jupiter, 314.6149088, -0.1098597),
(Body::Saturn, 231.5708687, -0.0181844),
(Body::Uranus, 254.5844730, -0.0296575),
(Body::Neptune, 271.6955376, -0.0248706),
(Body::Pluto, 211.9256159, 0.0012526),
(Body::TrueNode, 46.1491385, -0.0664496),
(Body::MeanLilith, 34.7361561, 0.1108111),
(Body::Chiron, 72.1761740, 0.0654425),
];
for (body, lon, speed) in expected {
let s = engine().position(body, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap();
assert!(close(s.longitude, lon, 1e-6), "{body:?} longitude {} != {lon}", s.longitude);
assert!(close(s.speed_longitude, speed, 1e-6), "{body:?} speed {} != {speed}", s.speed_longitude);
assert!(!s.degraded_precision, "{body:?} should come from the data files");
}
let sun = engine().position(Body::Sun, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap();
assert!(close(sun.distance, 1.016507864, 1e-8));
assert!(close(sun.declination, 21.6455828, 1e-6));
}
#[test]
fn sun_at_j2000() {
// Textbook value: apparent solar longitude at 2000-01-01 12:00 UT = 280.3689°
let s = engine().position(Body::Sun, 2_451_545.0, &Frame::TROPICAL_GEOCENTRIC).unwrap();
assert!(close(s.longitude, 280.3689187, 1e-6), "{}", s.longitude);
}
#[test]
fn placidus_houses_match_swetest() {
let h = engine().houses(JD, &BUDAPEST, HouseSystem::Placidus, &Frame::TROPICAL_GEOCENTRIC).unwrap();
let cusps = [
215.9678137,
244.3181228,
278.8086642,
316.4009907,
349.5231422,
15.5954622,
35.9678137,
64.3181228,
98.8086642,
136.4009907,
169.5231422,
195.5954622,
];
for (i, (a, e)) in h.cusps.iter().zip(cusps).enumerate() {
assert!(close(*a, e, 1e-6), "cusp {} {a} != {e}", i + 1);
}
assert_eq!(h.used, HouseSystem::Placidus);
assert!(close(h.ascendant, 215.9678137, 1e-6));
assert!(close(h.midheaven, 136.4009907, 1e-6));
assert!(close(h.armc, 138.8578266, 1e-6));
assert!(close(h.vertex, 72.3891351, 1e-6));
assert!(close(h.equatorial_ascendant, 231.2864612, 1e-6));
assert!(close(h.ascendant_speed, 264.1614525, 1e-4));
}
#[test]
fn placidus_falls_back_to_porphyry_in_polar_regions() {
let arctic = GeoPosition { latitude: 75.0, ..BUDAPEST };
let h = engine().houses(JD, &arctic, HouseSystem::Placidus, &Frame::TROPICAL_GEOCENTRIC).unwrap();
assert_eq!(h.used, HouseSystem::Porphyry);
assert!(h.fallback_reason.is_some());
// swetest: house 1 = 199°49'46.9801"
assert!(close(h.cusps[0], 199.0 + 49.0 / 60.0 + 46.9801 / 3600.0, 1e-6));
}
#[test]
fn delta_t_1985() {
// swetest: Delta T 54.6303394 s
assert!(close(engine().delta_t(JD) * 86_400.0, 54.6303394, 1e-4));
}
#[test]
fn full_chart() {
let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: true, options: ChartOptions::default() };
let chart = calculate_chart(engine(), &input).unwrap();
assert!(chart.warnings.is_empty(), "{:?}", chart.warnings);
// 10 planets + north/south node + Lilith + Chiron + 4 asteroids
assert_eq!(chart.points.len(), 18);
let get = |id| chart.points.iter().find(|p| p.id == id).unwrap();
let sun = get(PointId::Sun);
assert_eq!(sun.position.sign, astro_engine::ZodiacSign::Cancer);
assert_eq!(sun.house, Some(9)); // 112.0° lies between cusp 9 (98.8°) and 10 (136.4°)
let north = get(PointId::NorthNode);
let south = get(PointId::SouthNode);
assert!(close(south.position.longitude, north.position.longitude + 180.0, 1e-9));
// Sun 112.0007 and Mars 113.0940: conjunction with orb ~1.09°
let conj = chart
.aspects
.iter()
.find(|a| a.point1 == PointId::Sun && a.point2 == PointId::Mars)
.expect("sun-mars conjunction");
assert_eq!(conj.aspect, AspectKind::Conjunction);
assert!(close(conj.orb, 1.0932991, 1e-6));
// Sun in house 9 is above the horizon -> day chart
assert_eq!(chart.summary.sect, Some(Sect::Day));
let lots = chart.lots.as_ref().unwrap();
let expected_fortune = (215.9678137 + 71.1646551 - 112.0007251f64).rem_euclid(360.0);
assert!(close(lots.fortune.position.longitude, expected_fortune, 1e-6));
let json = serde_json::to_value(&chart).unwrap();
assert_eq!(json["points"][0]["id"], "sun");
assert_eq!(json["houses"]["system"], "placidus");
}
#[test]
fn unknown_time_omits_houses() {
let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: false, options: ChartOptions::default() };
let chart = calculate_chart(engine(), &input).unwrap();
assert!(chart.houses.is_none() && chart.angles.is_none() && chart.lots.is_none());
assert!(chart.points.iter().all(|p| p.house.is_none()));
assert!(!chart.warnings.is_empty());
}
#[test]
fn sidereal_zodiac_subtracts_ayanamsa() {
let tropical = engine().position(Body::Sun, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap();
let frame = Frame { sidereal: Some(astro_engine::Ayanamsa::Lahiri), topocentric: None };
let sidereal = engine().position(Body::Sun, JD, &frame).unwrap();
let ayanamsa = engine().ayanamsa(JD, astro_engine::Ayanamsa::Lahiri).unwrap();
assert!((23.0..24.5).contains(&ayanamsa), "lahiri ayanamsa {ayanamsa}");
assert!(close(tropical.longitude - sidereal.longitude, ayanamsa, 1e-3));
}