astronomy: fix mean anomalies for Earth, Venus, Mars, Neptune
- Commit
0cb15f1a325f6b7d5a13ae6e72471f8d7f7cc83b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:42 UTC (28 days ago)
- Parents
87619f1
Symptom: Mercury was the only inner planet in the right place. Earth, Venus and Mars rendered tens of degrees from their true positions (and every other planet shifted with them, because the geocentric conversion subtracts Earth's heliocentric position), e.g. Venus could appear ~69 deg from the Sun even though its greatest elongation is ~47 deg. Root cause: in the bundled orbital elements (assets catalog and the BuiltInCatalogs fallback, which mirror each other), m0Deg for Earth, Venus and Mars was a copy of the mean longitude lDeg instead of the mean anomaly. By definition L = omega + w + M0 (mod 360); Jupiter, Saturn and Uranus satisfy that identity to <0.2 deg, while Earth was off by 91.7 deg, Venus by 131.6 deg, Mars by -24.9 deg and Neptune by 4.1 deg. Mercury's m0Deg was already the true anomaly, but its lDeg duplicated m0Deg instead of holding the mean longitude. Fix: set m0Deg = lDeg - (omegaDeg + wDeg) for Earth (8.78779), Venus (50.41053), Mars (19.41216) and Neptune (260.31224), and Mercury lDeg to omega + w + M0 (252.33145), in both the JSON asset and the Kotlin fallback. Note the catalog generator at scripts/generate_astronomy_catalogs.py (outside astronomy/) holds the same literals and must be synced before the next regeneration, or it will re-emit the wrong values. Verification: new regression tests in SolarAndPlanetaryTest assert the L = omega + w + M0 identity for every built-in planet, that Earth's heliocentric longitude opposes the Sun's at J2000, and that Venus stays within 48 deg elongation over a 400-day span. :astronomy:testDevUnitTest for that class passes.
Diff
astronomy/src/main/assets/catalog/orbital_elements.json
diff --git a/astronomy/src/main/assets/catalog/orbital_elements.json b/astronomy/src/main/assets/catalog/orbital_elements.jsonindex 018add96cc..919251afaf 100644--- a/astronomy/src/main/assets/catalog/orbital_elements.json+++ b/astronomy/src/main/assets/catalog/orbital_elements.json@@ -1 +1 @@-{"planets": [{"id": "SUN", "name": "Sun", "a": 0.0, "e": 0.0, "iDeg": 0.0, "omegaDeg": 0.0, "wDeg": 0.0, "lDeg": 0.0, "m0Deg": 0.0, "nDegPerDay": 0.0, "magBase": -26.7}, {"id": "EARTH", "name": "Earth", "a": 1.0, "e": 0.0167086, "iDeg": 0.0, "omegaDeg": -11.26064, "wDeg": 102.9372, "lDeg": 100.46435, "m0Deg": 100.46435, "nDegPerDay": 0.9856091, "magBase": -3.0}, {"id": "MERCURY", "name": "Mercury", "a": 0.38709893, "e": 0.20563069, "iDeg": 7.00487, "omegaDeg": 48.33167, "wDeg": 29.12478, "lDeg": 174.875, "m0Deg": 174.875, "nDegPerDay": 4.09233445, "magBase": -0.42}, {"id": "VENUS", "name": "Venus", "a": 0.72333199, "e": 0.00677323, "iDeg": 3.39471, "omegaDeg": 76.68069, "wDeg": 54.88378, "lDeg": 181.975, "m0Deg": 181.975, "nDegPerDay": 1.60213034, "magBase": -4.4}, {"id": "MARS", "name": "Mars", "a": 1.52366231, "e": 0.09341233, "iDeg": 1.85061, "omegaDeg": 49.57854, "wDeg": 286.4623, "lDeg": 355.453, "m0Deg": 355.453, "nDegPerDay": 0.52402068, "magBase": -0.5}, {"id": "JUPITER", "name": "Jupiter", "a": 5.20336301, "e": 0.04839266, "iDeg": 1.3053, "omegaDeg": 100.55615, "wDeg": 273.8777, "lDeg": 34.40438, "m0Deg": 20.0202, "nDegPerDay": 0.08308529, "magBase": -9.4}, {"id": "SATURN", "name": "Saturn", "a": 9.53707032, "e": 0.05386179, "iDeg": 2.48446, "omegaDeg": 113.71504, "wDeg": 339.3939, "lDeg": 49.94432, "m0Deg": 317.02, "nDegPerDay": 0.03344414, "magBase": -8.9}, {"id": "URANUS", "name": "Uranus", "a": 19.19126393, "e": 0.04725744, "iDeg": 0.76986, "omegaDeg": 74.22988, "wDeg": 96.73436, "lDeg": 313.23218, "m0Deg": 142.2386, "nDegPerDay": 0.01172834, "magBase": -7.1}, {"id": "NEPTUNE", "name": "Neptune", "a": 30.06896348, "e": 0.00859048, "iDeg": 1.76917, "omegaDeg": 131.72169, "wDeg": 272.8461, "lDeg": 304.88003, "m0Deg": 256.228, "nDegPerDay": 0.00598103, "magBase": -6.9}]}\ No newline at end of file+{"planets": [{"id": "SUN", "name": "Sun", "a": 0.0, "e": 0.0, "iDeg": 0.0, "omegaDeg": 0.0, "wDeg": 0.0, "lDeg": 0.0, "m0Deg": 0.0, "nDegPerDay": 0.0, "magBase": -26.7}, {"id": "EARTH", "name": "Earth", "a": 1.0, "e": 0.0167086, "iDeg": 0.0, "omegaDeg": -11.26064, "wDeg": 102.9372, "lDeg": 100.46435, "m0Deg": 8.78779, "nDegPerDay": 0.9856091, "magBase": -3.0}, {"id": "MERCURY", "name": "Mercury", "a": 0.38709893, "e": 0.20563069, "iDeg": 7.00487, "omegaDeg": 48.33167, "wDeg": 29.12478, "lDeg": 252.33145, "m0Deg": 174.875, "nDegPerDay": 4.09233445, "magBase": -0.42}, {"id": "VENUS", "name": "Venus", "a": 0.72333199, "e": 0.00677323, "iDeg": 3.39471, "omegaDeg": 76.68069, "wDeg": 54.88378, "lDeg": 181.975, "m0Deg": 50.41053, "nDegPerDay": 1.60213034, "magBase": -4.4}, {"id": "MARS", "name": "Mars", "a": 1.52366231, "e": 0.09341233, "iDeg": 1.85061, "omegaDeg": 49.57854, "wDeg": 286.4623, "lDeg": 355.453, "m0Deg": 19.41216, "nDegPerDay": 0.52402068, "magBase": -0.5}, {"id": "JUPITER", "name": "Jupiter", "a": 5.20336301, "e": 0.04839266, "iDeg": 1.3053, "omegaDeg": 100.55615, "wDeg": 273.8777, "lDeg": 34.40438, "m0Deg": 20.0202, "nDegPerDay": 0.08308529, "magBase": -9.4}, {"id": "SATURN", "name": "Saturn", "a": 9.53707032, "e": 0.05386179, "iDeg": 2.48446, "omegaDeg": 113.71504, "wDeg": 339.3939, "lDeg": 49.94432, "m0Deg": 317.02, "nDegPerDay": 0.03344414, "magBase": -8.9}, {"id": "URANUS", "name": "Uranus", "a": 19.19126393, "e": 0.04725744, "iDeg": 0.76986, "omegaDeg": 74.22988, "wDeg": 96.73436, "lDeg": 313.23218, "m0Deg": 142.2386, "nDegPerDay": 0.01172834, "magBase": -7.1}, {"id": "NEPTUNE", "name": "Neptune", "a": 30.06896348, "e": 0.00859048, "iDeg": 1.76917, "omegaDeg": 131.72169, "wDeg": 272.8461, "lDeg": 304.88003, "m0Deg": 260.31224, "nDegPerDay": 0.00598103, "magBase": -6.9}]}\ No newline at end of file
astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.ktindex b52df08047..4a0a658898 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt@@ -168,19 +168,19 @@ object BuiltInCatalogs { val orbital: List<OrbitalElements> = listOf( OrbitalElements("SUN", "Sun", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, null, 0xFFFFD700, -26.7), OrbitalElements(- "EARTH", "Earth", 1.0, 0.0167086, 0.0, -11.26064, 102.9372, 100.46435, 100.46435,+ "EARTH", "Earth", 1.0, 0.0167086, 0.0, -11.26064, 102.9372, 100.46435, 8.78779, 0.9856091, null, 0xFF00FF00, -3.0, ), OrbitalElements(- "MERCURY", "Mercury", 0.38709893, 0.20563069, 7.00487, 48.33167, 29.12478, 174.875, 174.875,+ "MERCURY", "Mercury", 0.38709893, 0.20563069, 7.00487, 48.33167, 29.12478, 252.33145, 174.875, 4.09233445, 4879.0, 0xFFA9A9A9, -0.42, ), OrbitalElements(- "VENUS", "Venus", 0.72333199, 0.00677323, 3.39471, 76.68069, 54.88378, 181.975, 181.975,+ "VENUS", "Venus", 0.72333199, 0.00677323, 3.39471, 76.68069, 54.88378, 181.975, 50.41053, 1.60213034, 12104.0, 0xFFFFE0B0, -4.4, ), OrbitalElements(- "MARS", "Mars", 1.52366231, 0.09341233, 1.85061, 49.57854, 286.4623, 355.453, 355.453,+ "MARS", "Mars", 1.52366231, 0.09341233, 1.85061, 49.57854, 286.4623, 355.453, 19.41216, 0.52402068, 6792.0, 0xFFFF4500, -0.5, ), OrbitalElements(@@ -196,7 +196,7 @@ object BuiltInCatalogs { 0.01172834, 50724.0, 0xFFADD8E6, -7.1, ), OrbitalElements(- "NEPTUNE", "Neptune", 30.06896348, 0.00859048, 1.76917, 131.72169, 272.8461, 304.88003, 256.228,+ "NEPTUNE", "Neptune", 30.06896348, 0.00859048, 1.76917, 131.72169, 272.8461, 304.88003, 260.31224, 0.00598103, 49244.0, 0xFF4169E1, -6.9, ), )
astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/SolarAndPlanetaryTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/SolarAndPlanetaryTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/SolarAndPlanetaryTest.ktindex e0c113b06e..4e346722af 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/SolarAndPlanetaryTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/SolarAndPlanetaryTest.kt@@ -81,6 +81,52 @@ class SolarAndPlanetaryTest { } } + @Test+ fun `orbital elements satisfy mean longitude equals node plus perihelion plus anomaly`() {+ // By definition L = (omega + w + M0) mod 360 for every planet. Earth,+ // Venus and Mars shipped with M0 copied from L, which put their+ // heliocentric longitudes off by the longitude of perihelion+ // (~92 deg for Earth, ~132 deg for Venus, ~-24 deg for Mars).+ for (planet in com.vayunmathur.astronomy.data.BuiltInCatalogs.orbital) {+ if (planet.id == "SUN") continue+ val implied = (planet.omegaDeg + planet.wDeg + planet.m0Deg) % 360.0+ var diff = (implied - planet.lDeg) % 360.0+ if (diff > 180.0) diff -= 360.0+ if (diff < -180.0) diff += 360.0+ assertTrue(abs(diff) < 0.25, "${planet.id}: L=${planet.lDeg} but omega+w+M0=$implied")+ }+ }++ @Test+ fun `earth heliocentric longitude opposes the sun at j2000`() {+ val earth = com.vayunmathur.astronomy.data.BuiltInCatalogs.orbital.first { it.id == "EARTH" }+ val (x, y, _) = PlanetaryCalculator.heliocentricEcliptic(earth, TimeEngine.J2000)+ val earthLon = Math.toDegrees(kotlin.math.atan2(y, x))+ val sunLon = SolarCalculator.calc(TimeEngine.J2000).eclipticLonRad.toDeg()+ var opposition = (earthLon - sunLon) % 360.0+ if (opposition < 0) opposition += 360.0+ assertTrue(abs(opposition - 180.0) < 2.0, "earth lon $earthLon vs sun lon $sunLon")+ }++ @Test+ fun `venus never exceeds its maximum elongation from the sun`() {+ val orbital = com.vayunmathur.astronomy.data.BuiltInCatalogs.orbital+ val earth = orbital.first { it.id == "EARTH" }+ val venus = orbital.first { it.id == "VENUS" }+ val obliq = TimeEngine.meanObliquityRad(TimeEngine.J2000)+ for (offsetDays in listOf(0.0, 100.0, 200.0, 300.0, 400.0)) {+ val jd = TimeEngine.J2000 + offsetDays+ val result = PlanetaryCalculator.geocentricRaDec(venus, earth, jd)+ val ecliptic = CoordinateTransforms.raDecToEcliptic(result.raDec, obliq)+ val sunLon = SolarCalculator.calc(jd).eclipticLonRad+ val elongation = (ecliptic.lonRad - sunLon).normalizePi().toDeg()+ assertTrue(+ abs(elongation) < 48.0,+ "Venus elongation $elongation deg at JD $jd exceeds the ~47 deg maximum",+ )+ }+ }+ @Test fun builtin_catalog_counts() { // BuiltIn catalogs are in main sourceSet; just verify orbital core logic instead