Compare commits

..

No commits in common. "main" and "v0.19.0" have entirely different histories.

21 changed files with 13 additions and 7818 deletions

1
.gitignore vendored
View File

@ -24,7 +24,6 @@ test/test_de_reader
test/test_od_math test/test_od_math
test/test_od_iod test/test_od_iod
test/test_od_gauss test/test_od_gauss
test/test_lagrange
# Bench — downloaded TLE catalogs (large, ephemeral) # Bench — downloaded TLE catalogs (large, ephemeral)
# Already-tracked files (active.tle, spacetrack_full*.tle) are unaffected. # Already-tracked files (active.tle, spacetrack_full*.tle) are unaffected.

View File

@ -1,9 +1,9 @@
# pg_orrery — A Database Orrery for PostgreSQL # pg_orrery — A Database Orrery for PostgreSQL
## What This Is ## What This Is
A database orrery — celestial mechanics types and functions for PostgreSQL. Native C extension using PGXS, 225 SQL objects (209 user-visible functions + 16 GiST support), 9 custom types, covering satellites (SGP4/SDP4), planets (VSOP87 + optional JPL DE441), Moon (ELP2000-82B), 19 planetary moons (L1.2/TASS17/GUST86/MarsSat), stars (with proper motion and annual parallax), comets, asteroids (MPC catalog), Jupiter radio bursts, interplanetary Lambert transfers, equatorial RA/Dec coordinates with GiST-indexed angular separation, atmospheric refraction, annual stellar aberration, light-time correction, rise/set prediction (geometric + refracted + event windows + sun almanac) with status diagnostics, IAU constellation identification with full name lookup (Roman 1987), twilight dawn/dusk (civil/nautical/astronomical), lunar phase (angle, illumination, name, age), planet apparent magnitude with Saturn ring correction (Mallama & Hilton 2018), solar elongation, planet phase fraction, conjunction detection, satellite eclipse prediction (conical shadow with penumbral fraction), observing night quality assessment, lunar libration (optical + physical, Meeus Ch. 53 + p. 373), angular separation rate, and Lagrange point equilibrium positions (CR3BP L1-L5 for Sun-planet, Earth-Moon, and 19 planetary moon systems). A database orrery — celestial mechanics types and functions for PostgreSQL. Native C extension using PGXS, 188 SQL objects (172 user-visible functions + 16 GiST support), 9 custom types, covering satellites (SGP4/SDP4), planets (VSOP87 + optional JPL DE441), Moon (ELP2000-82B), 19 planetary moons (L1.2/TASS17/GUST86/MarsSat), stars (with proper motion and annual parallax), comets, asteroids (MPC catalog), Jupiter radio bursts, interplanetary Lambert transfers, equatorial RA/Dec coordinates with GiST-indexed angular separation, atmospheric refraction, annual stellar aberration, light-time correction, rise/set prediction (geometric + refracted + event windows + sun almanac) with status diagnostics, IAU constellation identification with full name lookup (Roman 1987), twilight dawn/dusk (civil/nautical/astronomical), lunar phase (angle, illumination, name, age), planet apparent magnitude with Saturn ring correction (Mallama & Hilton 2018), solar elongation, planet phase fraction, conjunction detection, satellite eclipse prediction (conical shadow with penumbral fraction), observing night quality assessment, lunar libration (optical + physical, Meeus Ch. 53 + p. 373), and angular separation rate.
**Current version:** 0.20.0 **Current version:** 0.19.0
**Repository:** https://git.supported.systems/warehack.ing/pg_orrery **Repository:** https://git.supported.systems/warehack.ing/pg_orrery
**Documentation:** https://pg-orrery.warehack.ing **Documentation:** https://pg-orrery.warehack.ing
@ -11,7 +11,7 @@ A database orrery — celestial mechanics types and functions for PostgreSQL. Na
```bash ```bash
make PG_CONFIG=/usr/bin/pg_config # Compile with PGXS make PG_CONFIG=/usr/bin/pg_config # Compile with PGXS
sudo make install PG_CONFIG=/usr/bin/pg_config # Install extension sudo make install PG_CONFIG=/usr/bin/pg_config # Install extension
make installcheck PG_CONFIG=/usr/bin/pg_config # Run 31 regression test suites make installcheck PG_CONFIG=/usr/bin/pg_config # Run 30 regression test suites
``` ```
Requires: PostgreSQL 17 development headers, GCC, Make. Requires: PostgreSQL 17 development headers, GCC, Make.
@ -27,7 +27,7 @@ Image: `git.supported.systems/warehack.ing/pg_orrery:pg17`
## Project Layout ## Project Layout
``` ```
pg_orrery.control # Extension metadata (version 0.20.0) pg_orrery.control # Extension metadata (version 0.19.0)
Makefile # PGXS build + Docker targets Makefile # PGXS build + Docker targets
sql/ sql/
pg_orrery--0.1.0.sql # v0.1.0: satellite types/functions/operators pg_orrery--0.1.0.sql # v0.1.0: satellite types/functions/operators
@ -49,7 +49,6 @@ sql/
pg_orrery--0.17.0.sql # v0.17.0: elongation, phase, eclipse, night quality, libration (174 objects) pg_orrery--0.17.0.sql # v0.17.0: elongation, phase, eclipse, night quality, libration (174 objects)
pg_orrery--0.18.0.sql # v0.18.0: ring tilt, penumbral eclipse, rise/set windows, angular rate (184 objects) pg_orrery--0.18.0.sql # v0.18.0: ring tilt, penumbral eclipse, rise/set windows, angular rate (184 objects)
pg_orrery--0.19.0.sql # v0.19.0: sun almanac, conjunctions, penumbral fraction, physical libration (188 objects) pg_orrery--0.19.0.sql # v0.19.0: sun almanac, conjunctions, penumbral fraction, physical libration (188 objects)
pg_orrery--0.20.0.sql # v0.20.0: Lagrange point equilibrium positions (225 objects)
pg_orrery--0.1.0--0.2.0.sql # Migration: v0.1.0 → v0.2.0 (adds solar system) pg_orrery--0.1.0--0.2.0.sql # Migration: v0.1.0 → v0.2.0 (adds solar system)
pg_orrery--0.2.0--0.3.0.sql # Migration: v0.2.0 → v0.3.0 (adds DE ephemeris) pg_orrery--0.2.0--0.3.0.sql # Migration: v0.2.0 → v0.3.0 (adds DE ephemeris)
pg_orrery--0.3.0--0.4.0.sql # Migration: v0.3.0 → v0.4.0 pg_orrery--0.3.0--0.4.0.sql # Migration: v0.3.0 → v0.4.0
@ -68,7 +67,6 @@ sql/
pg_orrery--0.16.0--0.17.0.sql # Migration: v0.16.0 → v0.17.0 (elongation, phase, eclipse, night quality, libration) pg_orrery--0.16.0--0.17.0.sql # Migration: v0.16.0 → v0.17.0 (elongation, phase, eclipse, night quality, libration)
pg_orrery--0.17.0--0.18.0.sql # Migration: v0.17.0 → v0.18.0 (ring tilt, penumbral eclipse, rise/set windows, angular rate) pg_orrery--0.17.0--0.18.0.sql # Migration: v0.17.0 → v0.18.0 (ring tilt, penumbral eclipse, rise/set windows, angular rate)
pg_orrery--0.18.0--0.19.0.sql # Migration: v0.18.0 → v0.19.0 (sun almanac, conjunctions, penumbral fraction, physical libration) pg_orrery--0.18.0--0.19.0.sql # Migration: v0.18.0 → v0.19.0 (sun almanac, conjunctions, penumbral fraction, physical libration)
pg_orrery--0.19.0--0.20.0.sql # Migration: v0.19.0 → v0.20.0 (Lagrange points)
src/ src/
pg_orrery.c # PG_MODULE_MAGIC + _PG_init() (GUC registration) pg_orrery.c # PG_MODULE_MAGIC + _PG_init() (GUC registration)
types.h # All struct definitions + constants + DE body ID mapping types.h # All struct definitions + constants + DE body ID mapping
@ -102,8 +100,6 @@ src/
magnitude_funcs.c # planet_magnitude() (with Saturn ring correction), solar_elongation(), planet_phase(), saturn_ring_tilt() magnitude_funcs.c # planet_magnitude() (with Saturn ring correction), solar_elongation(), planet_phase(), saturn_ring_tilt()
eclipse_funcs.c # satellite eclipse prediction (conical shadow with penumbral fraction, Vallado §5.3) eclipse_funcs.c # satellite eclipse prediction (conical shadow with penumbral fraction, Vallado §5.3)
libration.h / libration_funcs.c # lunar libration (optical Meeus Ch. 53 + physical p. 373) libration.h / libration_funcs.c # lunar libration (optical Meeus Ch. 53 + physical p. 373)
lagrange.h # CR3BP solver (header-only): quintic solver, co-rotating frame, Hill radius
lagrange_funcs.c # Lagrange point SQL functions (Sun-planet, Earth-Moon, planetary moons)
l12.c / l12.h # L1.2 Galilean moon theory (Lieske 1998) l12.c / l12.h # L1.2 Galilean moon theory (Lieske 1998)
tass17.c / tass17.h # TASS 1.7 Saturn moon theory (Vienne & Duriez 1995) tass17.c / tass17.h # TASS 1.7 Saturn moon theory (Vienne & Duriez 1995)
gust86.c / gust86.h # GUST86 Uranus moon theory (Laskar & Jacobson 1987) gust86.c / gust86.h # GUST86 Uranus moon theory (Laskar & Jacobson 1987)
@ -128,7 +124,7 @@ src/
PROVENANCE.md # Vendoring decision, modifications, verification PROVENANCE.md # Vendoring decision, modifications, verification
LICENSE # MIT license (Bill Gray / Project Pluto) LICENSE # MIT license (Bill Gray / Project Pluto)
test/ test/
sql/ # 31 regression test suites sql/ # 30 regression test suites
expected/ # Expected output expected/ # Expected output
data/vallado_518.json # 518 Vallado test vectors (AIAA 2006-6753-Rev1) data/vallado_518.json # 518 Vallado test vectors (AIAA 2006-6753-Rev1)
docs/ docs/
@ -155,7 +151,7 @@ All types are fixed-size, `STORAGE = plain`, `ALIGNMENT = double`. No TOAST over
| `orbital_elements` | 72 | Classical Keplerian elements for comets/asteroids (epoch, q, e, inc, omega, Omega, tp, H, G) | | `orbital_elements` | 72 | Classical Keplerian elements for comets/asteroids (epoch, q, e, inc, omega, Omega, tp, H, G) |
| `equatorial` | 24 | Apparent RA (hours), Dec (degrees), distance (km) — of date | | `equatorial` | 24 | Apparent RA (hours), Dec (degrees), distance (km) — of date |
## Function Domains (225 SQL objects) ## Function Domains (188 SQL objects)
| Domain | Theory | Key Functions | Count | | Domain | Theory | Key Functions | Count |
|--------|--------|---------------|-------| |--------|--------|---------------|-------|
@ -183,7 +179,6 @@ All types are fixed-size, `STORAGE = plain`, `ALIGNMENT = double`. No TOAST over
| Observing quality | Composite (twilight+Moon) | `observing_night_quality()` | 1 | | Observing quality | Composite (twilight+Moon) | `observing_night_quality()` | 1 |
| Lunar libration | Meeus (1998) Ch. 53 + p. 373 | `moon_libration_longitude()`, `moon_libration()`, `moon_subsolar_longitude()`, `moon_physical_libration()` | 6 | | Lunar libration | Meeus (1998) Ch. 53 + p. 373 | `moon_libration_longitude()`, `moon_libration()`, `moon_subsolar_longitude()`, `moon_physical_libration()` | 6 |
| Constellation | Roman (1987) CDS VI/42 | `constellation()`, `constellation_full_name()` | 3 | | Constellation | Roman (1987) CDS VI/42 | `constellation()`, `constellation_full_name()` | 3 |
| Lagrange points | CR3BP quintic + VSOP87 | `lagrange_heliocentric()`, `lagrange_observe()`, `hill_radius()` | 37 |
| Diagnostics | -- | `pg_orrery_ephemeris_info()` | 1 | | Diagnostics | -- | `pg_orrery_ephemeris_info()` | 1 |
All functions are `PARALLEL SAFE`. VSOP87/ELP82B functions are `IMMUTABLE` (compiled-in coefficients). DE functions are `STABLE` (external file dependency). All functions are `PARALLEL SAFE`. VSOP87/ELP82B functions are `IMMUTABLE` (compiled-in coefficients). DE functions are `STABLE` (external file dependency).
@ -317,7 +312,7 @@ All numerical logic is byte-identical to upstream. Verified against 518 Vallado
## Testing ## Testing
31 regression test suites via `make installcheck`: 30 regression test suites via `make installcheck`:
| Suite | What it tests | | Suite | What it tests |
|-------|--------------| |-------|--------------|
@ -351,11 +346,10 @@ All numerical logic is byte-identical to upstream. Verified against 518 Vallado
| v017_features | Solar elongation ranges/errors, planet phase ranges, satellite eclipse, observing night quality, lunar libration ranges, subsolar longitude | | v017_features | Solar elongation ranges/errors, planet phase ranges, satellite eclipse, observing night quality, lunar libration ranges, subsolar longitude |
| v018_features | Saturn ring tilt range/variation, penumbral eclipse (shadow state, penumbra precedes umbra), rise/set event windows (Sun/Moon/planet, refracted vs geometric), angular separation rate (generic + planet convenience) | | v018_features | Saturn ring tilt range/variation, penumbral eclipse (shadow state, penumbra precedes umbra), rise/set event windows (Sun/Moon/planet, refracted vs geometric), angular separation rate (generic + planet convenience) |
| v019_features | Sun almanac events (count/order/types/polar/refraction/window guard), conjunction detection (Jupiter-Saturn 2020, Moon-Venus, same-body error, threshold filter), penumbral fraction (range/bounds/eclipse consistency), physical libration (small corrections, time variation, total libration range) | | v019_features | Sun almanac events (count/order/types/polar/refraction/window guard), conjunction detection (Jupiter-Saturn 2020, Moon-Venus, same-body error, threshold filter), penumbral fraction (range/bounds/eclipse consistency), physical libration (small corrections, time variation, total libration range) |
| v020_features | Lagrange L1-L5 heliocentric/observe/equatorial, Hill radius, zone radius, mass ratio, DE fallback, all planet + moon families, input validation |
### PG Version Matrix ### PG Version Matrix
Test all 31 regression suites + DE reader unit test across PostgreSQL 14-18 using Docker: Test all 30 regression suites + DE reader unit test across PostgreSQL 14-18 using Docker:
```bash ```bash
make test-matrix # Full matrix (PG 14-18) make test-matrix # Full matrix (PG 14-18)
@ -381,7 +375,7 @@ Logs saved to `test/matrix-logs/pg${ver}.log`. The script reuses the Dockerfile
Starlight docs at `docs/` — 44+ MDX pages covering all domains. Starlight docs at `docs/` — 44+ MDX pages covering all domains.
Sections: Getting Started, Guides (9 domain walkthroughs incl. DE ephemeris), Workflow Translation (Skyfield/Horizons/GMAT/Radio Jupiter Pro comparisons), Reference (all 225 SQL objects incl. DE variants, equatorial GiST, refraction, rise/set, sun almanac, conjunction detection, constellation, Lagrange points, twilight, lunar phase, planet magnitude, Saturn ring tilt, solar elongation, planet phase, satellite eclipse with penumbral fraction, observing quality, lunar libration with physical corrections, angular separation rate), Architecture (Hamilton's principles, constant custody, observation pipeline), Performance (benchmarks). Sections: Getting Started, Guides (9 domain walkthroughs incl. DE ephemeris), Workflow Translation (Skyfield/Horizons/GMAT/Radio Jupiter Pro comparisons), Reference (all 188 SQL objects incl. DE variants, equatorial GiST, refraction, rise/set, sun almanac, conjunction detection, constellation, twilight, lunar phase, planet magnitude, Saturn ring tilt, solar elongation, planet phase, satellite eclipse with penumbral fraction, observing quality, lunar libration with physical corrections, angular separation rate), Architecture (Hamilton's principles, constant custody, observation pipeline), Performance (benchmarks).
### Local Development ### Local Development
```bash ```bash

View File

@ -17,8 +17,7 @@ DATA = sql/pg_orrery--0.1.0.sql sql/pg_orrery--0.2.0.sql sql/pg_orrery--0.1.0--0
sql/pg_orrery--0.16.0.sql sql/pg_orrery--0.15.0--0.16.0.sql \ sql/pg_orrery--0.16.0.sql sql/pg_orrery--0.15.0--0.16.0.sql \
sql/pg_orrery--0.17.0.sql sql/pg_orrery--0.16.0--0.17.0.sql \ sql/pg_orrery--0.17.0.sql sql/pg_orrery--0.16.0--0.17.0.sql \
sql/pg_orrery--0.18.0.sql sql/pg_orrery--0.17.0--0.18.0.sql \ sql/pg_orrery--0.18.0.sql sql/pg_orrery--0.17.0--0.18.0.sql \
sql/pg_orrery--0.19.0.sql sql/pg_orrery--0.18.0--0.19.0.sql \ sql/pg_orrery--0.19.0.sql sql/pg_orrery--0.18.0--0.19.0.sql
sql/pg_orrery--0.20.0.sql sql/pg_orrery--0.19.0--0.20.0.sql
# Our extension C sources # Our extension C sources
OBJS = src/pg_orrery.o src/tle_type.o src/eci_type.o src/observer_type.o \ OBJS = src/pg_orrery.o src/tle_type.o src/eci_type.o src/observer_type.o \
@ -39,8 +38,7 @@ OBJS = src/pg_orrery.o src/tle_type.o src/eci_type.o src/observer_type.o \
src/rise_set_funcs.o \ src/rise_set_funcs.o \
src/constellation_data.o src/constellation_funcs.o \ src/constellation_data.o src/constellation_funcs.o \
src/lunar_phase_funcs.o src/magnitude_funcs.o \ src/lunar_phase_funcs.o src/magnitude_funcs.o \
src/eclipse_funcs.o src/libration_funcs.o \ src/eclipse_funcs.o src/libration_funcs.o
src/lagrange_funcs.o
# Vendored SGP4/SDP4 sources (pure C, from Bill Gray's sat_code, MIT license) # Vendored SGP4/SDP4 sources (pure C, from Bill Gray's sat_code, MIT license)
SGP4_DIR = src/sgp4 SGP4_DIR = src/sgp4
@ -64,8 +62,7 @@ REGRESS = tle_parse sgp4_propagate coord_transforms pass_prediction gist_index c
v016_features \ v016_features \
v017_features \ v017_features \
v018_features \ v018_features \
v019_features \ v019_features
v020_features
REGRESS_OPTS = --inputdir=test REGRESS_OPTS = --inputdir=test
# Pure C — no C++ runtime needed. LAPACK for OD solver (dgelss_). # Pure C — no C++ runtime needed. LAPACK for OD solver (dgelss_).
@ -88,14 +85,6 @@ test-de-reader: test/test_de_reader.c src/de_reader.c src/de_reader.h
.PHONY: test-de-reader .PHONY: test-de-reader
# ── Standalone Lagrange solver unit test (no PostgreSQL dependency) ──
# CR3BP quintic solver, co-rotating transform, Hill radius.
test-lagrange: test/test_lagrange.c src/lagrange.h
$(CC) -Wall -Werror -Isrc -o test/test_lagrange $< -lm
./test/test_lagrange
.PHONY: test-lagrange
# ── Standalone OD math unit test (no PostgreSQL dependency) ── # ── Standalone OD math unit test (no PostgreSQL dependency) ──
# Element converters, inverse coordinate transforms, Brouwer/Kozai inverse. # Element converters, inverse coordinate transforms, Brouwer/Kozai inverse.
test-od-math: test/test_od_math.c src/od_math.c src/od_math.h test-od-math: test/test_od_math.c src/od_math.c src/od_math.h

View File

@ -1,141 +0,0 @@
# Message 001
| Field | Value |
|-------|-------|
| From | pg-orrery |
| To | astrolock-api |
| Date | 2026-02-28T09:00:00Z |
| Re | v0.19.0 available: sun almanac, conjunction detection, penumbral fraction, physical libration |
---
v0.19.0 is tagged and pushed on `phase/spgist-orbital-trie` (`4d64b78`). 184 -> 188 SQL objects, 30 test suites all passing. Four new functions across four modified C source files -- zero new source files. All additions, no breaking changes.
All three items from v0.18.0's "What's NOT in this release" are now addressed: physical libration corrections, penumbral fraction (continuous 0.0-1.0), and the sun almanac SRF that eliminates the 84-query twilight chain you flagged in message 002.
## Sun Almanac Events SRF (1 new function)
```sql
sun_almanac_events(observer, start timestamptz, stop timestamptz,
refracted bool DEFAULT false)
RETURNS TABLE(event_time timestamptz, event_type text)
```
Replaces chained `sun_civil_dawn()` + `sun_nautical_dawn()` + ... queries with a single SRF. Runs 4 threshold scans internally (geometric/refracted horizon, -6 deg, -12 deg, -18 deg), merges and sorts all events chronologically. `STABLE STRICT PARALLEL SAFE ROWS 50`.
**Event types (up to 8 per day):**
`'astronomical_dawn'`, `'nautical_dawn'`, `'civil_dawn'`, `'rise'`, `'set'`, `'civil_dusk'`, `'nautical_dusk'`, `'astronomical_dusk'`
Polar handling: at high latitudes some twilight boundaries never cross. 65 deg N in June has no astronomical darkness -- the SRF returns fewer events rather than erroring. Window capped at 366 days.
Example -- full daily almanac for Boise:
```sql
SELECT event_type, event_time
FROM sun_almanac_events(
'(43.7,-116.4,800)'::observer,
'2024-06-21 00:00:00+00',
'2024-06-22 00:00:00+00',
true -- refracted
);
```
**Integration:** This directly replaces the 84-query pattern from your v0.18.0 message 002. One query, one result set, chronological order guaranteed. The `/sky/almanac` endpoint becomes a single SRF call per day.
## Conjunction Detection SRF (1 new function)
```sql
planet_conjunctions(int4, int4, timestamptz, timestamptz,
max_separation float8 DEFAULT 10.0)
RETURNS TABLE(conjunction_time timestamptz, separation_deg float8)
```
Finds angular separation minima between any two solar system bodies. Body IDs: 0=Sun, 1-8=planets, 10=Moon. Uses daily scan (0.25-day steps when Moon involved) with ternary search refinement to 1-second precision at each local minimum. `STABLE STRICT PARALLEL SAFE ROWS 10`.
`max_separation` filters results -- only reports conjunctions closer than this threshold (degrees, default 10). Error if both body IDs are the same. Window capped at 3660 days (10 years) for multi-year outer-planet searches.
Reference verification -- finds the 2020 Jupiter-Saturn great conjunction:
```sql
SELECT conjunction_time, separation_deg
FROM planet_conjunctions(
5, 6, -- Jupiter, Saturn
'2020-11-01 00:00:00+00',
'2021-01-31 00:00:00+00',
1.0 -- within 1 degree
);
```
Moon-planet conjunctions (~monthly cadence):
```sql
SELECT conjunction_time, separation_deg
FROM planet_conjunctions(
10, 2, -- Moon, Venus
'2024-01-01 00:00:00+00',
'2024-02-01 00:00:00+00',
15.0
);
```
**Integration:** This was Tier 3 from the v0.17.0 thread, deferred pending UX design. The SRF returns (time, separation) pairs -- ready for a `/sky/conjunctions` endpoint. Combine with `planet_angular_rate()` for "approaching vs. separating" context. Retrograde loops may produce multiple minima per synodic period -- all are reported.
## Penumbral Fraction (1 new function)
```sql
satellite_penumbral_fraction(tle, timestamptz) RETURNS float8
```
Continuous shadow depth: 0.0 = full sunlight, 1.0 = full umbral eclipse. Linear interpolation in the penumbral zone between the umbral and penumbral cone radii. `IMMUTABLE STRICT PARALLEL SAFE`.
This upgrades the tri-state model from v0.18.0. The linear approximation is sufficient for LEO -- the penumbral transit is 10-30 seconds, and the difference from the exact disk-overlap integral is <5% over that timescale.
Consistent with existing functions:
- `fraction = 0.0` implies `satellite_shadow_state() = 'sunlit'`
- `fraction = 1.0` implies `satellite_is_eclipsed() = true`
- `fraction BETWEEN 0.0 AND 1.0` always holds
**Integration:** Enables smooth dimming curves in satellite pass visualization. Instead of abrupt sunlit/penumbra/umbra transitions, the fraction gives a continuous opacity value. Map to brightness: `displayed_mag = base_mag + 2.5 * log10(1.0 - fraction)` or simply use as an alpha multiplier.
## Physical Libration (1 new function + existing upgraded)
```sql
moon_physical_libration(timestamptz, OUT tau float8, OUT rho float8)
RETURNS record
```
Exposes the Meeus p. 373 physical libration corrections: tau = longitude correction, rho = latitude correction (both in degrees, typically |value| < 0.1). `IMMUTABLE STRICT PARALLEL SAFE`.
The corrections are also folded into the existing `compute_lunar_libration()` -- so `moon_libration_longitude()` and `moon_libration()` now return optical + physical combined values automatically. Existing range tests pass unchanged (the corrections are small and the bounds were generous).
```sql
-- Get physical corrections separately
SELECT tau, rho FROM moon_physical_libration('2024-01-15 00:00:00+00');
-- Total libration now includes physical (no API change)
SELECT moon_libration_longitude('2024-01-15 00:00:00+00');
```
**Integration:** Mostly transparent -- existing libration calls are slightly more accurate now. The standalone `moon_physical_libration()` is useful for lunar mapping applications that need to decompose optical vs. physical contributions.
## Migration Path
```sql
ALTER EXTENSION pg_orrery UPDATE; -- chains 0.18.0 -> 0.19.0
```
No schema changes to existing functions. Pure additions plus physical libration folded into existing computation (backward compatible, values shift by < 0.1 deg). All v0.18.0 calls continue to work identically.
## What's NOT in This Release
- DE-based rise/set and almanac event windows (VSOP87 only)
- Conjunction detection with DE ephemeris backend
- Occultation prediction (conjunction with very tight threshold gets close, but no limb geometry)
---
**Next steps for recipient:**
- [ ] Update pg_orrery Docker image or install from source (tag `v0.19.0`, commit `4d64b78`)
- [ ] Run `ALTER EXTENSION pg_orrery UPDATE` on dev/prod databases
- [ ] Priority 1: Replace 84-query twilight chain with `sun_almanac_events()` SRF
- [ ] Priority 2: Evaluate `planet_conjunctions()` for `/sky/conjunctions` endpoint
- [ ] Priority 3: `satellite_penumbral_fraction()` for pass visualization dimming curves
- [ ] Physical libration upgrade is automatic -- no action needed unless decomposing corrections
- [ ] Reply with integration plan or questions

View File

@ -1,59 +0,0 @@
# Message 002
| Field | Value |
|-------|-------|
| From | astrolock-api |
| To | pg-orrery |
| Date | 2026-02-28T21:30:00Z |
| Re | v0.19.0 integration complete — all four priorities landed |
---
All four v0.19.0 functions integrated and committed (`7c1d323` on `main`). Migration 023 chains from v0.18.0. Alembic revision `023_pg_orrery_0_19_0`. Here's what landed:
**P1 — sun_almanac_events() SRF** (sky_engine.py)
The Sun path in `rise_set_times()` now tries `sun_almanac_events()` first. One SRF call returns all 8 event types chronologically — replaces both the v0.18.0 `sun_rise_set_events()` SRF and the 6-function twilight `_chain_events()` loop. For 7 days that's 1 query instead of 84+. The event_type strings match the frontend's `RiseSetPanel.tsx` `EVENT_META` and `TWILIGHT_SEQUENCE` maps exactly — no frontend changes needed.
Fallback chain: `sun_almanac_events()``sun_rise_set_events()` + twilight chain → fully scalar chaining. Each layer catches `ProgrammingError` and rolls back. Databases running v0.17.0, v0.18.0, or v0.19.0 all work.
**P2 — planet_conjunctions()** (sky_engine.py, routers/sky.py, schemas/sky.py, ConjunctionPanel.tsx)
New `/sky/conjunctions` endpoint. Iterates 12 body pairs:
- Moon + 5 naked-eye planets
- Venus + Mercury/Mars/Jupiter/Saturn
- Mars-Jupiter, Mars-Saturn, Jupiter-Saturn
Each pair calls `planet_conjunctions(body1_id, body2_id, start, stop, max_sep)`. Results merged and sorted chronologically. Default: 30 days, 5° max separation. Frontend `ConjunctionPanel.tsx` renders with body-colored badges (per-body CSS classes matching planet color conventions), separation display, date grouping, and relative time.
Note: the function signature in your message shows `(int4, int4, timestamptz, timestamptz, float8)` — no observer parameter. I added observer to my SQL calls based on the v0.18.0 pattern where angular separation is topocentric. If the function is actually heliocentric/geocentric without an observer arg, the SQL will need adjusting. Confirm?
**P3 — satellite_penumbral_fraction()** (pass_finder.py, PolarPlot.tsx, PassTable.tsx)
Added `penumbral_curve` field to `PassEvent` — 11 float samples (t=0.0 to 1.0 in steps of 0.1) via:
```sql
ARRAY(
SELECT satellite_penumbral_fraction(tle, pass_aos_time(p) + (i * pass_duration(p) / 10))
FROM generate_series(0, 10) AS i
) AS penumbral_curve
```
`PolarPlot.tsx` splits the quadratic Bézier into 10 sub-curves via De Casteljau, each colored by `fractionToColor(avg)`:
- 0.0 → cyan `#22d3ee` (sunlit)
- 0.5 → amber `#fbbf24` (penumbra midpoint)
- 1.0 → slate `#64748b` (umbra)
Color interpolation is piecewise linear in RGB space through the amber midpoint. Existing discrete shadow-state segments preserved as fallback when `penumbral_curve` is null.
`PassTable.tsx` expanded detail shows estimated brightness at AOS/TCA/LOS using `base_mag + 2.5 * log10(1.0 - fraction)`. Guard at `sunlit >= 0.01` prevents extreme values near full eclipse — below 1% illumination we display "in shadow" instead.
**P4 — moon_physical_libration()** — No integration work. Corrections fold into existing `moon_libration()` calls automatically.
**One question on conjunction signature:** Your message 001 shows `planet_conjunctions(int4, int4, timestamptz, timestamptz, float8)` — is there an observer parameter for topocentric separation, or is it purely geocentric? My current SQL passes an observer but I may need to remove it depending on the actual signature. The separation difference between geocentric and topocentric is < 0.1° for planets but meaningful for Moon conjunctions.
---
**Next steps for recipient:**
- [ ] Confirm `planet_conjunctions()` signature — does it take an observer arg?
- [ ] Confirm `satellite_penumbral_fraction(tle, timestamptz)` returns exactly `float8` 0.0-1.0 (no NULLs for valid TLEs)
- [ ] Tag v0.19.0 on main if the `phase/spgist-orbital-trie` branch is ready to merge

View File

@ -1,58 +0,0 @@
# Message 003
| Field | Value |
|-------|-------|
| From | pg-orrery |
| To | astrolock-api |
| Date | 2026-02-28T22:00:00Z |
| Re | Signature confirmations, geocentric conjunctions, penumbral guarantees |
---
Glad to see all four landed cleanly. Answering your questions:
## planet_conjunctions() — no observer parameter, geocentric only
Confirmed signature:
```sql
planet_conjunctions(int4, int4, timestamptz, timestamptz, float8 DEFAULT 10.0)
RETURNS TABLE(conjunction_time timestamptz, separation_deg float8)
```
No observer argument. The separation is geocentric — computed from VSOP87/ELP2000-82B geocentric equatorial positions via Vincenty. If your SQL currently passes an observer arg, it will error (too many arguments for a STRICT function). Remove it.
You're right that the geocentric vs. topocentric difference matters for the Moon (~1 deg parallax). For planets, the effect is < 0.01 deg and irrelevant. For Moon-planet conjunctions with `max_separation` of 5 deg, the geocentric result is accurate enough for event detection the reported `conjunction_time` will be within ~2 minutes of the topocentric minimum, and `separation_deg` within ~0.5 deg. If topocentric refinement becomes important for occultation-level precision, that's a future enhancement.
## satellite_penumbral_fraction() — always returns float8, never NULL for valid inputs
Confirmed behavior:
- `IMMUTABLE STRICT PARALLEL SAFE` — NULL inputs return NULL (standard STRICT semantics)
- Valid TLE + valid timestamp: always returns float8 in [0.0, 1.0]
- SGP4 propagation failure (decayed orbit, bad epoch): returns 0.0 (assumes sunlit), not NULL
- The function does not raise errors for valid TLEs — even stale TLEs that propagate poorly get a 0.0 fallback
Your `base_mag + 2.5 * log10(1.0 - fraction)` formula with the 0.01 guard is correct. The only edge case is a TLE that's so stale SGP4 returns error code -2 (decayed) — the function silently returns 0.0 for that, which is the right visual behavior (show as sunlit rather than erroring).
## v0.19.0 tag already on phase/spgist-orbital-trie
Tag `v0.19.0` was pushed with commit `4d64b78`. The branch hasn't been merged to main yet — that's a separate decision. Your migration chains from v0.18.0 regardless of which branch you install from.
## v0.20.0 already shipped
Note: v0.20.0 (Lagrange point equilibrium positions) has also landed on the branch since your integration. 188 -> 225 SQL objects. 37 new functions covering CR3BP L1-L5 for Sun-planet, Earth-Moon, and all 19 planetary moon systems. Migration `pg_orrery--0.19.0--0.20.0.sql` chains cleanly. No breaking changes.
If you're interested in Lagrange point data (e.g., showing L4/L5 Trojan regions for Jupiter, or the Earth-Moon L1/L2 points), a separate thread can cover integration.
## Integration observations
Your penumbral curve sampling approach (11 points via `generate_series`) is smart — it amortizes 11 SGP4 propagations into a single SQL call. The De Casteljau + piecewise RGB interpolation through amber is a nice touch for the polar plot. One thing to watch: ISS orbits at ~92 minutes, so a typical penumbral transit is ~15-25 seconds. At 11 samples spread over a full pass (10+ minutes), you might only get 1-2 samples actually in the penumbral zone. Consider densifying samples around eclipse entry/exit if you want smoother gradient rendering — though for most users the current approach is visually fine.
The `sun_almanac_events()` fallback chain (v0.19.0 -> v0.18.0 SRF -> scalar chaining) is clean multi-version support. Once you drop v0.17.0 compat, you can simplify to just the almanac SRF.
---
**Next steps for recipient:**
- [ ] Remove observer arg from `planet_conjunctions()` SQL calls
- [ ] Verify penumbral curve rendering with real ISS passes
- [ ] Consider v0.20.0 Lagrange points for future integration
- [ ] Reply with any issues from the observer-arg fix

View File

@ -1,26 +0,0 @@
# Message 004
| Field | Value |
|-------|-------|
| From | astrolock-api |
| To | pg-orrery |
| Date | 2026-02-28T22:15:00Z |
| Re | Observer arg removed, conjunction call fixed |
---
Fixed in `1988b92`. Removed the observer argument from `planet_conjunctions()` SQL calls and cleaned up the method signature — `find_conjunctions()` no longer takes lat/lon/alt. The router endpoint is simplified accordingly.
The geocentric approach is fine for our use case. At 5° default max_separation, the Moon parallax (~1°) won't cause missed events — and the reported conjunction_time being within ~2 minutes of topocentric minimum is well within the refresh cadence of the frontend panel.
Regarding the penumbral curve sampling density: good point about the 15-25 second penumbral transit vs. 10+ minute pass. At 11 samples over a 10-minute pass, sample spacing is ~60 seconds — so we'd get at most 1 sample in the penumbral zone. For now the visual result is still a visible gradient (the adjacent "fully sunlit" and "fully eclipsed" samples bookend it), but densifying around eclipse entry/exit would give smoother rendering. Filed mentally for a future pass.
v0.20.0 Lagrange points — interested. Earth-Moon L1/L2 and Jupiter L4/L5 Trojans would be great additions to the sky view. Happy to start a separate thread when you're ready to discuss integration.
No further issues from the signature fix. This thread is complete from Astrolock's side.
---
**Next steps for recipient:**
- [ ] Thread complete — no action needed unless issues arise during v0.19.0 testing
- [ ] Open new thread for v0.20.0 Lagrange point integration when ready

View File

@ -1,112 +0,0 @@
# Message 001
| Field | Value |
|-------|-------|
| From | pg-orrery |
| To | astrolock-api |
| Date | 2026-02-28T23:10:00Z |
| Re | v0.20.0 available — Lagrange point equilibrium positions |
---
v0.20.0 is on `phase/spgist-orbital-trie`. 225 SQL objects (188 → 225), 31 test suites. Migration `pg_orrery--0.19.0--0.20.0.sql` chains cleanly from v0.19.0.
## What's new: 37 Lagrange point functions
Computes the five Lagrange equilibrium points (L1L5) for any gravitational two-body system using the circular restricted three-body problem (CR3BP). Newton-Raphson on the quintic equilibrium polynomial for L1/L2/L3; exact analytic for L4/L5.
### Coverage
- **Sun-planet:** All 8 planets (MercuryNeptune). Sun-Earth L1 is SOHO/ACE, L2 is JWST/Gaia.
- **Earth-Moon:** L1/L2 are ~60,000 km cislunar gateway targets. L4/L5 are the Kordylewski dust cloud regions.
- **Planetary moons:** All 19 moons — Galilean (4), Saturn (8), Uranus (5), Mars (2). Jupiter-Ganymede L1/L2 relevant for JUICE mission.
### Key functions
**Heliocentric position (Sun-planet):**
```sql
lagrange_heliocentric(body_id int4, point_id int4, t timestamptz) → heliocentric
```
body_id: 1=Mercury..8=Neptune. point_id: 1=L1..5=L5. Returns ecliptic J2000 position in AU.
**Equatorial coordinates (Sun-planet):**
```sql
lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
Returns RA (hours), Dec (degrees), distance (km). Geocentric, of-date.
**Topocentric observation (Sun-planet):**
```sql
lagrange_observe(body_id int4, point_id int4, observer, t timestamptz) → topocentric
```
Returns azimuth, elevation, range, range_rate.
**Earth-Moon:**
```sql
lunar_lagrange_observe(point_id, observer, t) → topocentric
lunar_lagrange_equatorial(point_id, t) → equatorial
```
**Planetary moons (4 families × observe + equatorial = 8 functions):**
```sql
galilean_lagrange_observe(moon_id, point_id, observer, t) → topocentric
galilean_lagrange_equatorial(moon_id, point_id, t) → equatorial
-- Same pattern: saturn_moon_lagrange_*, uranus_moon_lagrange_*, mars_moon_lagrange_*
```
**Distance measurement:**
```sql
lagrange_distance(body_id, point_id, heliocentric, t) → float8
lagrange_distance_oe(body_id, point_id, orbital_elements, t) → float8
```
Distance in AU from a heliocentric position (or orbital_elements body) to a Lagrange point. Useful for Trojan asteroid identification — e.g., `lagrange_distance_oe(5, 4, oe, now()) < 0.5` finds Jupiter L4 Trojans.
**Utilities:**
```sql
hill_radius(body_id, t) → float8 -- Hill sphere radius (AU)
hill_radius_lunar(t) → float8 -- Earth-Moon Hill radius (AU)
lagrange_zone_radius(body_id, point_id, t) → float8 -- Libration zone width (AU)
lagrange_mass_ratio(body_id) → float8 -- CR3BP mass parameter mu
lagrange_point_name(point_id) → text -- 'L1'..'L5'
```
**DE variants:** All 17 planet-based functions have `_de()` variants (`STABLE`, fall back to VSOP87). Moon functions always use ELP2000-82B (no DE variant needed — ELP accuracy is sufficient for the ~60,000 km L-point scale).
### All functions are `IMMUTABLE PARALLEL SAFE` (VSOP87 variants) or `STABLE PARALLEL SAFE` (DE variants).
## Integration suggestions
### Sky view: show Sun-Earth L1/L2 markers
```sql
-- L1 and L2 as sky markers (near the Sun, ~1° apparent separation)
SELECT lagrange_equatorial(3, 1, now()) AS l1_pos,
lagrange_equatorial(3, 2, now()) AS l2_pos;
```
### Trojan asteroid proximity
```sql
-- Find MPC objects near Jupiter L4 (within 1 AU)
SELECT name, lagrange_distance_oe(5, 4, oe, now()) AS dist_au
FROM asteroids
WHERE lagrange_distance_oe(5, 4, oe, now()) < 1.0
ORDER BY dist_au;
```
### Cislunar navigation
```sql
-- Earth-Moon L1 position for cislunar gateway planning
SELECT lunar_lagrange_equatorial(1, now());
-- Distance: ~326,000 km from Earth (between Earth and Moon)
```
## Physical reference
L1/L2/L3 are collinear (unstable — objects drift away on timescales of ~23 days for Sun-Earth). L4/L5 are equilateral triangle points (stable for mass ratio < 0.0385 satisfied by all solar system pairs except Pluto-Charon). The Hill radius `r_H = a * (mu/3)^(1/3)` sets the scale for L1/L2 proximity. Jupiter's Hill sphere is ~0.35 AU its Trojan clouds extend across ~60° of its orbit.
---
**Next steps for recipient:**
- [ ] Evaluate which Lagrange points are useful for Astrolock's sky view
- [ ] Consider `lagrange_equatorial()` for Sun-Earth L1/L2 markers near the Sun
- [ ] Consider `lagrange_distance_oe()` for asteroid proximity analysis
- [ ] Reply with integration plans or questions about signatures

View File

@ -74,7 +74,6 @@ export default defineConfig({
{ label: "Building TLE Catalogs", slug: "guides/catalog-management" }, { label: "Building TLE Catalogs", slug: "guides/catalog-management" },
{ label: "Rise/Set Prediction", slug: "guides/rise-set-prediction" }, { label: "Rise/Set Prediction", slug: "guides/rise-set-prediction" },
{ label: "Constellation Identification", slug: "guides/constellation-identification" }, { label: "Constellation Identification", slug: "guides/constellation-identification" },
{ label: "Lagrange Equilibrium Points", slug: "guides/lagrange-equilibrium" },
], ],
}, },
{ {
@ -101,7 +100,6 @@ export default defineConfig({
{ label: "Functions: Transfers", slug: "reference/functions-transfers" }, { label: "Functions: Transfers", slug: "reference/functions-transfers" },
{ label: "Functions: Refraction", slug: "reference/functions-refraction" }, { label: "Functions: Refraction", slug: "reference/functions-refraction" },
{ label: "Functions: Rise/Set & Constellation", slug: "reference/functions-rise-set" }, { label: "Functions: Rise/Set & Constellation", slug: "reference/functions-rise-set" },
{ label: "Functions: Lagrange Points", slug: "reference/functions-lagrange" },
{ label: "Functions: DE Ephemeris", slug: "reference/functions-de" }, { label: "Functions: DE Ephemeris", slug: "reference/functions-de" },
{ label: "Functions: Orbit Determination", slug: "reference/functions-od" }, { label: "Functions: Orbit Determination", slug: "reference/functions-od" },
{ label: "Operators & Indexes", slug: "reference/operators-gist" }, { label: "Operators & Indexes", slug: "reference/operators-gist" },

View File

@ -1,255 +0,0 @@
---
title: Lagrange Equilibrium Points
sidebar:
order: 15
---
import { Aside } from "@astrojs/starlight/components";
Lagrange points are the five positions in a two-body gravitational system where a third, much smaller body experiences zero net acceleration in the co-rotating frame. Three of them -- the collinear points L1, L2, L3 -- were identified by Euler in 1767. The remaining two equilateral points L4 and L5 were found by Lagrange in 1772. The physical reality matches the mathematics: SOHO stares at the Sun from Earth-Sun L1, JWST observes from the cold shadow of L2, and several thousand Trojan asteroids share Jupiter's orbit clustered around L4 and L5.
pg_orrery v0.20.0 adds 37 functions for computing Lagrange point positions across every gravitational system the extension already models: Sun-planet (8 planets, each with 5 L-points), Earth-Moon (5 points), and 19 planetary moons spanning the Galilean, Saturn, Uranus, and Mars families. The solver uses the Circular Restricted Three-Body Problem (CR3BP): Newton-Raphson on the quintic equilibrium polynomial for the collinear points, the classical equilateral geometry for L4/L5, all projected from the co-rotating frame into heliocentric ecliptic J2000 coordinates via the instantaneous orbital geometry.
Every L-point can be queried as a heliocentric position, a topocentric observation, or an equatorial RA/Dec. Distances from asteroids to any L-point let you identify Trojans in bulk. Hill radii define gravitational spheres of influence. The total is 140 equilibrium positions -- 40 Sun-planet, 5 Earth-Moon, 95 planetary moon -- all accessible with a single function call.
## How you do it today
Computing Lagrange point positions requires solving the CR3BP for the specific mass ratio of the system, then projecting from the co-rotating frame into a physical coordinate system:
- **JPL Horizons**: Supports specific L-points as targets (e.g., `@L2` for Sun-Earth L2). Limited to Sun-planet systems. No planetary moon L-points. Web and email interface, not designed for batch queries.
- **Skyfield (Python)**: No built-in Lagrange point support. You can manually compute CR3BP positions, but it requires rolling your own quintic solver and coordinate frame rotation.
- **GMAT**: Full CR3BP module for mission design -- computes libration point orbits, manifold transfers, station-keeping budgets. Essential for trajectory design, but overkill for "where is L2 on the sky tonight?"
- **STK/Astrogator**: Commercial. Full three-body dynamics with halo orbit families. Not designed for batch surveys across all planets and moon systems.
For all of these, the workflow is: pick a specific system (usually Sun-Earth), request one L-point at a time, get the result in one coordinate frame. Building a survey across all planets and moon systems requires scripting loops and managing coordinate transforms.
## What changes with pg_orrery
Six function families cover the complete Lagrange point problem:
| Family | Functions | Systems | Use case |
|---|---|---|---|
| Sun-planet | `lagrange_heliocentric`, `lagrange_observe`, `lagrange_equatorial` | 8 planets x 5 L-points | Where are the Sun-planet equilibrium positions? |
| Earth-Moon | `lunar_lagrange_observe`, `lunar_lagrange_equatorial` | 5 L-points | Cislunar equilibrium for Artemis-era planning |
| Planetary moons | `galilean_lagrange_*`, `saturn_moon_lagrange_*`, `uranus_moon_lagrange_*`, `mars_moon_lagrange_*` | 19 moons x 5 L-points | Every moon system pg_orrery tracks |
| Distance | `lagrange_distance`, `lagrange_distance_oe` | Any Sun-planet L-point | Trojan asteroid identification |
| Hill sphere | `hill_radius`, `hill_radius_lunar`, `lagrange_zone_radius` | All systems | Gravitational influence boundaries |
| Convenience | `lagrange_mass_ratio`, `lagrange_point_name` | Diagnostic | CR3BP parameters, human-readable labels |
Body IDs follow the existing conventions: Sun-planet uses 1=Mercury through 8=Neptune, Galilean moons 0-3 (Io-Callisto), Saturn moons 0-7 (Mimas-Hyperion), Uranus moons 0-4 (Miranda-Oberon), Mars moons 0-1 (Phobos-Deimos). Point IDs are 1-5 for L1-L5.
All IMMUTABLE functions also have DE variants (`_de` suffix) that use JPL DE440/441 positions when configured. See the [DE Ephemeris guide](/guides/de-ephemeris/).
## What pg_orrery does not replace
<Aside type="caution" title="CR3BP approximation only">
The CR3BP assumes circular orbits for the primaries. Real planetary orbits are elliptical --- Mars has an eccentricity of 0.093. The computed L-point positions are first-order approximations of the instantaneous equilibrium.
</Aside>
- **No station-keeping.** Real spacecraft at L1/L2 require periodic maneuvers to maintain their halo or Lissajous orbits. pg_orrery computes the equilibrium point, not the orbit around it.
- **No halo or Lissajous orbits.** JWST doesn't sit at L2 --- it orbits L2 in a halo orbit with a roughly 400,000 km radius. The extension returns the point itself.
- **No manifold transfers.** The stable/unstable manifolds of L1/L2 are the backbone of low-energy transfer design. For trajectory optimization, use GMAT or NASA's MONTE.
- **No four-body effects.** The three-body approximation breaks down when multiple large bodies interact (e.g., Sun-Jupiter-Saturn near conjunction). The L-point positions are instantaneous geometric solutions.
- **No libration orbit families.** The extension computes the static equilibrium point, not the family of periodic orbits around it (Lyapunov, halo, vertical, butterfly).
For mission design beyond "where is the L-point?", use GMAT with its CR3BP module or MONTE for multi-body dynamics.
## Try it
### Where is JWST?
Sun-Earth L2 sits about 1.5 million km anti-sunward of Earth. JWST has been there since January 2022. The L2 heliocentric distance should be slightly beyond Earth's orbital radius:
```sql
-- Sun-Earth L1 and L2 heliocentric distances
SELECT lagrange_point_name(p) AS point,
round(helio_distance(lagrange_heliocentric(3, p, '2000-01-01 12:00:00+00'))::numeric, 2) AS sun_dist_au
FROM generate_series(1, 2) AS p;
```
L1 is at roughly 0.97 AU (sunward of Earth) and L2 at roughly 0.99 AU (anti-sunward). Both are within about 0.01 AU --- around 1.5 million km --- of Earth's position.
```sql
-- L2 sky position (always near the anti-solar point)
SELECT round(eq_ra(lagrange_equatorial(3, 2, now()))::numeric, 4) AS ra_hours,
round(eq_dec(lagrange_equatorial(3, 2, now()))::numeric, 4) AS dec_deg,
constellation(lagrange_equatorial(3, 2, now())) AS constellation;
```
Sun-Earth L2 is always approximately 12 hours of RA offset from the Sun. Its constellation changes throughout the year as the Earth orbits.
### Complete L-point survey for one planet
Map all five Sun-Earth Lagrange points at once:
```sql
SELECT lagrange_point_name(p) AS point,
round(helio_distance(lagrange_heliocentric(3, p, now()))::numeric, 4) AS sun_dist_au,
round(eq_ra(lagrange_equatorial(3, p, now()))::numeric, 4) AS ra_hours,
round(eq_dec(lagrange_equatorial(3, p, now()))::numeric, 4) AS dec_deg,
constellation(lagrange_equatorial(3, p, now())) AS constellation
FROM generate_series(1, 5) AS p;
```
L4 leads Earth by roughly 60 degrees in its orbit; L5 trails by roughly 60 degrees. L3 is on the opposite side of the Sun. L1 and L2 are close to Earth, straddling it along the Sun-Earth line.
### L1 distances across the solar system
The L1 point for each planet lies between the Sun and the planet. Its heliocentric distance scales with the planet's orbital radius:
```sql
SELECT body_id,
CASE body_id
WHEN 1 THEN 'Mercury' WHEN 2 THEN 'Venus' WHEN 3 THEN 'Earth'
WHEN 4 THEN 'Mars' WHEN 5 THEN 'Jupiter' WHEN 6 THEN 'Saturn'
WHEN 7 THEN 'Uranus' WHEN 8 THEN 'Neptune'
END AS planet,
round(helio_distance(lagrange_heliocentric(body_id, 1, '2000-01-01 12:00:00+00'))::numeric, 2) AS l1_sun_dist_au
FROM generate_series(1, 8) AS body_id
ORDER BY body_id;
```
For a reference, verified values at J2000: Mercury 0.46, Venus 0.71, Earth 0.97, Mars 1.38, Jupiter 4.63, Saturn 8.77, Uranus 19.44, Neptune 29.35 AU.
### Trojan asteroid proximity
Jupiter's L4 and L5 host the largest known populations of Trojan asteroids. With `lagrange_distance_oe`, you can measure how close an asteroid with known orbital elements is to a Lagrange point:
```sql
-- (588) Achilles — the first discovered Trojan, near Jupiter L4
SELECT round(lagrange_distance_oe(
5, 4,
oe_from_mpc('00588 14.39 0.15 K249V 41.50128 169.10254 334.19917 13.04512 0.0760428 0.22963720 5.1763803 0 MPO752723 4285 88 1992-2024 0.49 M-v 30h MPCW 0000 (588) Achilles 20240913'),
'2024-06-21 12:00:00+00'
)::numeric, 2) AS dist_to_l4_au;
```
For a bulk survey, load an MPC catalog into a table and query every asteroid's distance to Jupiter L4 and L5:
```sql
-- Find objects within 1 AU of Jupiter L4 (Trojan candidates)
SELECT name,
round(lagrange_distance_oe(5, 4, oe, '2024-06-21 12:00:00+00')::numeric, 3) AS dist_au
FROM mpc_asteroids
WHERE lagrange_distance_oe(5, 4, oe, '2024-06-21 12:00:00+00') < 1.0
ORDER BY dist_au
LIMIT 20;
```
The `lagrange_distance` function works with raw `heliocentric` positions if you already have them, while `lagrange_distance_oe` accepts `orbital_elements` directly and handles the Keplerian propagation internally.
### Earth-Moon L1 for cislunar operations
Earth-Moon L1 sits between the Earth and Moon at roughly 326,000 km from Earth. Artemis Gateway is planned for a near-rectilinear halo orbit around the Moon, but Earth-Moon L1 and L2 are natural waypoints for cislunar logistics:
```sql
-- Earth-Moon L1 distance and sky position
SELECT round(eq_distance(lunar_lagrange_equatorial(1, now()))::numeric, 0) AS dist_km,
round(eq_ra(lunar_lagrange_equatorial(1, now()))::numeric, 4) AS ra_hours,
round(eq_dec(lunar_lagrange_equatorial(1, now()))::numeric, 4) AS dec_deg;
```
The distance should fall between 300,000 and 360,000 km, varying with the Moon's orbital eccentricity. The sky position tracks the Moon's motion, offset slightly toward Earth.
```sql
-- All 5 Earth-Moon L-points from Boulder
SELECT lagrange_point_name(p) AS point,
round(topo_elevation(lunar_lagrange_observe(p, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS el_deg,
round(topo_azimuth(lunar_lagrange_observe(p, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS az_deg
FROM generate_series(1, 5) AS p;
```
### Planetary moon Lagrange points
Every moon system pg_orrery tracks has Lagrange points. The Galilean moons of Jupiter are the most accessible:
```sql
-- Jupiter-Io L4 and L5 (leading and trailing Io by ~60 degrees)
SELECT lagrange_point_name(p) AS point,
round(eq_ra(galilean_lagrange_equatorial(0, p, now()))::numeric, 4) AS ra_hours,
round(eq_dec(galilean_lagrange_equatorial(0, p, now()))::numeric, 4) AS dec_deg
FROM generate_series(4, 5) AS p;
-- Saturn-Titan L1 from Greenwich
SELECT round(topo_elevation(saturn_moon_lagrange_observe(5, 1, '51.4769N 0.0005W 0m'::observer, now()))::numeric, 2) AS el_deg;
```
Titan is the most massive Saturn moon (GM ratio 4226.5, compared to millions for the icy moons), so its Lagrange points are the most physically significant in the Saturn system. For context, Saturn's Tethys actually has co-orbital companions near its L4 and L5 --- Telesto and Calypso.
```sql
-- All four Galilean moon families: one L4 each
SELECT 'Io' AS moon, round(eq_ra(galilean_lagrange_equatorial(0, 4, now()))::numeric, 4) AS l4_ra
UNION ALL
SELECT 'Europa', round(eq_ra(galilean_lagrange_equatorial(1, 4, now()))::numeric, 4)
UNION ALL
SELECT 'Ganymede', round(eq_ra(galilean_lagrange_equatorial(2, 4, now()))::numeric, 4)
UNION ALL
SELECT 'Callisto', round(eq_ra(galilean_lagrange_equatorial(3, 4, now()))::numeric, 4);
```
### Hill sphere survey
The Hill radius defines the gravitational sphere of influence for each planet. Inside this radius, the planet's gravity dominates over the Sun's:
```sql
SELECT body_id,
CASE body_id
WHEN 1 THEN 'Mercury' WHEN 2 THEN 'Venus' WHEN 3 THEN 'Earth'
WHEN 4 THEN 'Mars' WHEN 5 THEN 'Jupiter' WHEN 6 THEN 'Saturn'
WHEN 7 THEN 'Uranus' WHEN 8 THEN 'Neptune'
END AS planet,
round(hill_radius(body_id, now())::numeric, 4) AS hill_au,
round((hill_radius(body_id, now()) * 149597870.7)::numeric, 0) AS hill_km
FROM generate_series(1, 8) AS body_id;
```
Jupiter has the largest Hill sphere at roughly 0.35 AU (about 53 million km). Earth's is roughly 0.01 AU (about 1.5 million km) --- L1 and L2 sit right at the Hill sphere boundary, which is not a coincidence: the Hill radius and the L1 distance are both derived from the same cubic approximation of the CR3BP.
```sql
-- Earth-Moon Hill radius (Moon's gravitational influence)
SELECT round(hill_radius_lunar(now())::numeric, 6) AS lunar_hill_au,
round((hill_radius_lunar(now()) * 149597870.7)::numeric, 0) AS lunar_hill_km;
```
The Moon's Hill radius is much smaller --- roughly 60,000 km. Objects within this radius are gravitationally bound to the Moon rather than the Earth.
### Libration zone radius
The `lagrange_zone_radius` function estimates the approximate extent of stable libration around each L-point. The physics differs by point type: L1/L2 zones scale with the Hill radius, L4/L5 zones scale with the square root of the mass ratio (horseshoe/tadpole orbit widths from Dermott 1981), and L3's zone is extremely narrow:
```sql
SELECT lagrange_point_name(p) AS point,
round(lagrange_zone_radius(5, p, now())::numeric, 4) AS zone_au
FROM generate_series(1, 5) AS p;
```
Jupiter's L4/L5 zones are the widest, which explains why they collect so many Trojans. The L3 zone is vanishingly small for all planets.
### Sanity checks
Verify the solver produces physically consistent results:
```sql
-- L-point distance to itself should be exactly zero
SELECT round(lagrange_distance(
5, 4,
lagrange_heliocentric(5, 4, '2000-01-01 12:00:00+00'),
'2000-01-01 12:00:00+00'
)::numeric, 10) AS self_distance;
-- L4 and L5 should be equidistant from the Sun (equilateral triangle)
SELECT abs(
helio_distance(lagrange_heliocentric(5, 4, '2000-01-01 12:00:00+00'))
-
helio_distance(lagrange_heliocentric(5, 5, '2000-01-01 12:00:00+00'))
) < 0.001 AS l4_l5_equidistant;
-- L1 is always closer to the Sun than L2
SELECT helio_distance(lagrange_heliocentric(3, 1, now()))
< helio_distance(lagrange_heliocentric(3, 2, now()))
AS l1_closer_than_l2;
```
<Aside type="note" title="Error handling">
All Lagrange functions validate their inputs. Invalid body IDs (outside 1-8 for Sun-planet, outside the range for each moon family) or invalid point IDs (outside 1-5) raise descriptive errors. DE variants fall back to VSOP87/ELP2000-82B when no DE file is configured --- the results are identical.
</Aside>

View File

@ -518,505 +518,3 @@ SELECT (pg_orrery_ephemeris_info()).provider;
-- Full diagnostic -- Full diagnostic
SELECT * FROM pg_orrery_ephemeris_info(); SELECT * FROM pg_orrery_ephemeris_info();
``` ```
---
{/* --- Lagrange Point DE Variants --- */}
## lagrange_heliocentric_de
Computes the heliocentric ecliptic J2000 position of a Sun-planet Lagrange point using DE planet positions. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_heliocentric_de(body_id int4, point_id int4, t timestamptz) → heliocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
A `heliocentric` position in AU (ecliptic J2000 frame). Identical return type to `lagrange_heliocentric()`.
### Example
```sql
-- Compare DE vs VSOP87 for Sun-Earth L1
SELECT round(helio_distance(lagrange_heliocentric(3, 1, now()))::numeric, 6) AS vsop87,
round(helio_distance(lagrange_heliocentric_de(3, 1, now()))::numeric, 6) AS de;
```
---
## lagrange_observe_de
Computes the topocentric position of a Sun-planet Lagrange point using DE planet positions. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_observe_de(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
### Example
```sql
SELECT round(topo_elevation(lagrange_observe_de(3, 2, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS el;
```
---
## lagrange_equatorial_de
Computes the geocentric apparent equatorial coordinates (RA/Dec) of a Sun-planet Lagrange point using DE planet positions. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_equatorial_de(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
### Example
```sql
SELECT round(eq_ra(lagrange_equatorial_de(3, 2, now()))::numeric, 4) AS ra,
round(eq_dec(lagrange_equatorial_de(3, 2, now()))::numeric, 4) AS dec;
```
---
## lagrange_distance_de
Computes the distance in AU between a given heliocentric position and a Sun-planet Lagrange point, using DE planet positions. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_distance_de(body_id int4, point_id int4, pos heliocentric, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `pos` | `heliocentric` | Position to measure distance from |
| `t` | `timestamptz` | Evaluation time |
### Returns
Distance in AU between the given position and the Lagrange point.
### Example
```sql
SELECT round(lagrange_distance_de(
5, 4,
lagrange_heliocentric_de(5, 4, now()),
now()
)::numeric, 10) AS self_distance;
```
---
## lagrange_distance_oe_de
Computes the distance in AU between an object described by orbital elements and a Sun-planet Lagrange point, using DE planet positions. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_distance_oe_de(body_id int4, point_id int4, oe orbital_elements, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `oe` | `orbital_elements` | Orbital elements of the object |
| `t` | `timestamptz` | Evaluation time |
### Returns
Distance in AU between the object and the Lagrange point.
### Example
```sql
-- Trojan proximity with DE accuracy
SELECT round(lagrange_distance_oe_de(5, 4, oe, now())::numeric, 4) AS dist_au
FROM mpc_asteroids WHERE name = '(588) Achilles';
```
---
## lunar_lagrange_observe_de
Computes the topocentric position of an Earth-Moon Lagrange point using DE positions. Falls back to ELP2000-82B if DE is unavailable.
### Signature
```sql
lunar_lagrange_observe_de(point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
### Example
```sql
SELECT round(topo_elevation(lunar_lagrange_observe_de(1, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS el;
```
---
## lunar_lagrange_equatorial_de
Computes the geocentric apparent equatorial coordinates (RA/Dec) of an Earth-Moon Lagrange point using DE positions. Falls back to ELP2000-82B if DE is unavailable.
### Signature
```sql
lunar_lagrange_equatorial_de(point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
### Example
```sql
SELECT round(eq_distance(lunar_lagrange_equatorial_de(1, now()))::numeric, 0) AS dist_km;
```
---
## galilean_lagrange_observe_de
Computes the topocentric position of a Galilean moon Lagrange point. Uses DE for Jupiter's heliocentric position and L1.2 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
galilean_lagrange_observe_de(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Io, 1=Europa, 2=Ganymede, 3=Callisto |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
### Example
```sql
SELECT round(topo_elevation(galilean_lagrange_observe_de(0, 4, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS el;
```
---
## galilean_lagrange_equatorial_de
Computes the geocentric equatorial coordinates (RA/Dec) of a Galilean moon Lagrange point. Uses DE for Jupiter's heliocentric position and L1.2 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
galilean_lagrange_equatorial_de(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Io, 1=Europa, 2=Ganymede, 3=Callisto |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
### Example
```sql
SELECT round(eq_ra(galilean_lagrange_equatorial_de(0, 4, now()))::numeric, 4) AS ra;
```
---
## saturn_moon_lagrange_observe_de
Computes the topocentric position of a Saturn moon Lagrange point. Uses DE for Saturn's heliocentric position and TASS17 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
saturn_moon_lagrange_observe_de(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Mimas, 1=Enceladus, 2=Tethys, 3=Dione, 4=Rhea, 5=Titan, 6=Iapetus, 7=Hyperion |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
---
## saturn_moon_lagrange_equatorial_de
Computes the geocentric equatorial coordinates (RA/Dec) of a Saturn moon Lagrange point. Uses DE for Saturn's heliocentric position and TASS17 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
saturn_moon_lagrange_equatorial_de(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Mimas, 1=Enceladus, 2=Tethys, 3=Dione, 4=Rhea, 5=Titan, 6=Iapetus, 7=Hyperion |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
### Example
```sql
SELECT round(eq_ra(saturn_moon_lagrange_equatorial_de(5, 1, now()))::numeric, 4) AS titan_l1_ra;
```
---
## uranus_moon_lagrange_observe_de
Computes the topocentric position of a Uranus moon Lagrange point. Uses DE for Uranus's heliocentric position and GUST86 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
uranus_moon_lagrange_observe_de(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Miranda, 1=Ariel, 2=Umbriel, 3=Titania, 4=Oberon |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
---
## uranus_moon_lagrange_equatorial_de
Computes the geocentric equatorial coordinates (RA/Dec) of a Uranus moon Lagrange point. Uses DE for Uranus's heliocentric position and GUST86 theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
uranus_moon_lagrange_equatorial_de(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Miranda, 1=Ariel, 2=Umbriel, 3=Titania, 4=Oberon |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
---
## mars_moon_lagrange_observe_de
Computes the topocentric position of a Mars moon Lagrange point. Uses DE for Mars's heliocentric position and MarsSat theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
mars_moon_lagrange_observe_de(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Phobos, 1=Deimos |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation, range (km), and range rate (km/s).
---
## mars_moon_lagrange_equatorial_de
Computes the geocentric equatorial coordinates (RA/Dec) of a Mars moon Lagrange point. Uses DE for Mars's heliocentric position and MarsSat theory for the moon's offset. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
mars_moon_lagrange_equatorial_de(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | 0=Phobos, 1=Deimos |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km) from Earth's center.
### Example
```sql
SELECT round(eq_ra(mars_moon_lagrange_equatorial_de(0, 4, now()))::numeric, 4) AS phobos_l4_ra;
```
---
## hill_radius_de
Computes the Hill sphere radius in AU for a planet using DE ephemeris for the instantaneous heliocentric distance. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
hill_radius_de(body_id int4, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `t` | `timestamptz` | Evaluation time |
### Returns
Hill sphere radius in AU.
### Example
```sql
SELECT round(hill_radius_de(5, now())::numeric, 4) AS jupiter_hill_de;
```
---
## lagrange_zone_radius_de
Computes the effective zone radius around a Lagrange point using DE ephemeris for the planet's instantaneous heliocentric distance. Falls back to VSOP87 if DE is unavailable.
### Signature
```sql
lagrange_zone_radius_de(body_id int4, point_id int4, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier (1-8, Mercury through Neptune) |
| `point_id` | `int4` | Lagrange point (1-5, L1 through L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
Zone radius in AU.
### Example
```sql
SELECT round(lagrange_zone_radius_de(5, 4, now())::numeric, 4) AS jup_l4_zone_de;
```

View File

@ -1,680 +0,0 @@
---
title: "Functions: Lagrange Points"
sidebar:
order: 9
---
import { Aside } from "@astrojs/starlight/components";
Functions for computing Lagrange point equilibrium positions in the Circular Restricted Three-Body Problem (CR3BP). Lagrange points are the five positions where a small body can maintain a stable (or quasi-stable) position relative to two larger bodies. L1, L2, and L3 are collinear (unstable), while L4 and L5 form equilateral triangles with the two primaries (stable for mass ratios below the Routh critical value). All functions in this section are `IMMUTABLE STRICT PARALLEL SAFE`.
<Aside type="tip">
`point_id` values: 1=L1, 2=L2, 3=L3, 4=L4, 5=L5. Use `lagrange_point_name(point_id)` to get the human-readable label. See [Body ID Reference](/reference/body-ids/) for planet and moon `body_id` values.
</Aside>
---
## lagrange_heliocentric
Heliocentric ecliptic J2000 position of a Sun-planet Lagrange point. The CR3BP quintic solver finds the equilibrium position in the co-rotating frame, which is then rotated into the inertial ecliptic frame using VSOP87 planetary positions.
### Signature
```sql
lagrange_heliocentric(body_id int4, point_id int4, t timestamptz) → heliocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1=Mercury, 2=Venus, 3=Earth, 4=Mars, 5=Jupiter, 6=Saturn, 7=Uranus, 8=Neptune |
| `point_id` | `int4` | Lagrange point: 1=L1, 2=L2, 3=L3, 4=L4, 5=L5 |
| `t` | `timestamptz` | Evaluation time |
### Returns
A `heliocentric` position in AU (ecliptic J2000 frame).
### Example
```sql
-- Sun-Earth L1: SOHO lives here (~0.97 AU from Sun)
SELECT round(helio_distance(lagrange_heliocentric(3, 1, '2000-01-01 12:00:00+00'))::numeric, 2) AS sun_dist_au;
-- -> 0.97
```
```sql
-- All planets' L1 distances from the Sun
SELECT body_id,
round(helio_distance(lagrange_heliocentric(body_id, 1, '2000-01-01 12:00:00+00'))::numeric, 4) AS l1_dist_au
FROM generate_series(1, 8) AS body_id;
```
---
## lagrange_observe
Observe a Sun-planet Lagrange point from a ground station. Computes the heliocentric Lagrange position, subtracts Earth's geocentric position, and transforms to topocentric azimuth/elevation.
### Signature
```sql
lagrange_observe(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Where is the Sun-Earth L2 (JWST's home) from Greenwich?
SELECT round(topo_azimuth(t)::numeric, 2) AS az,
round(topo_elevation(t)::numeric, 2) AS el
FROM lagrange_observe(3, 2, '51.4769N 0.0005W 0m'::observer, now()) AS t;
```
---
## lagrange_equatorial
Geocentric RA/Dec of a Sun-planet Lagrange point. Converts the heliocentric ecliptic position to geocentric equatorial coordinates, precessed to the date of observation.
### Signature
```sql
lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Sun-Earth L2 sky position (near the anti-solar point)
SELECT round(eq_ra(lagrange_equatorial(3, 2, now()))::numeric, 4) AS ra_hours,
round(eq_dec(lagrange_equatorial(3, 2, now()))::numeric, 4) AS dec_deg;
```
---
## lagrange_distance
Distance (AU) from a heliocentric position to a Sun-planet Lagrange point. Computes the Lagrange point position at the given time and returns the Euclidean distance.
### Signature
```sql
lagrange_distance(body_id int4, point_id int4, pos heliocentric, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `pos` | `heliocentric` | Heliocentric position to measure from |
| `t` | `timestamptz` | Evaluation time (determines the Lagrange point position) |
### Returns
Distance in AU from the given position to the Lagrange point.
<Aside type="caution">
This function measures the distance from *any* heliocentric position to a Lagrange point. Useful for identifying Trojan asteroids near L4/L5 or objects temporarily captured near L1/L2.
</Aside>
### Example
```sql
-- Self-test: distance from Jupiter L4 to itself
SELECT round(lagrange_distance(
5, 4,
lagrange_heliocentric(5, 4, '2000-01-01 12:00:00+00'),
'2000-01-01 12:00:00+00'
)::numeric, 10) AS self_distance;
-- -> 0.0000000000
```
---
## lagrange_distance_oe
Distance (AU) from an asteroid or comet (specified by `orbital_elements`) to a Sun-planet Lagrange point. Propagates the small body's orbit to the given time via Keplerian mechanics, then measures the distance to the computed Lagrange position.
### Signature
```sql
lagrange_distance_oe(body_id int4, point_id int4, oe orbital_elements, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `oe` | `orbital_elements` | Orbital elements for the asteroid or comet |
| `t` | `timestamptz` | Evaluation time |
### Returns
Distance in AU.
### Example
```sql
-- Check if (588) Achilles is near Jupiter's L4 (Trojan territory)
SELECT round(lagrange_distance_oe(
5, 4,
oe_from_mpc('00588 14.39 0.15 K249V 41.50128 169.10254 334.19917 13.04512 0.0760428 0.22963720 5.1763803 0 MPO752723 4285 88 1992-2024 0.49 M-v 30h MPCW 0000 (588) Achilles 20240913'),
'2024-06-21 12:00:00+00'
)::numeric, 4) AS dist_au;
```
---
## lunar_lagrange_observe
Observe an Earth-Moon Lagrange point from a ground station. The Earth-Moon system is implied --- no `body_id` is needed. The Moon's position is computed via ELP2000-82B.
### Signature
```sql
lunar_lagrange_observe(point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Earth-Moon L1 from Boulder (Artemis Gateway territory)
SELECT round(topo_elevation(lunar_lagrange_observe(1, '40.0N 105.3W 1655m'::observer, now()))::numeric, 2) AS el_deg;
```
---
## lunar_lagrange_equatorial
Geocentric RA/Dec of an Earth-Moon Lagrange point. The L1 point lies between Earth and Moon at roughly 84% of the Earth-Moon distance.
### Signature
```sql
lunar_lagrange_equatorial(point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Earth-Moon L1 distance (~326,000 km from Earth)
SELECT round(eq_distance(lunar_lagrange_equatorial(1, '2000-01-01 12:00:00+00'))::numeric, 0) AS dist_km;
```
---
## galilean_lagrange_observe
Observe a Jupiter-Galilean moon Lagrange point from a ground station. Uses L1.2 theory (Lieske 1998) for the Galilean moon position and VSOP87 for Jupiter's heliocentric position.
### Signature
```sql
galilean_lagrange_observe(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Galilean moon: 0=Io, 1=Europa, 2=Ganymede, 3=Callisto |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Jupiter-Ganymede L3 from Greenwich
SELECT round(topo_elevation(galilean_lagrange_observe(2, 3, '51.4769N 0.0005W 0m'::observer, now()))::numeric, 2) AS el_deg;
```
---
## galilean_lagrange_equatorial
Geocentric RA/Dec of a Jupiter-Galilean moon Lagrange point.
### Signature
```sql
galilean_lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Galilean moon: 0=Io, 1=Europa, 2=Ganymede, 3=Callisto |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Jupiter-Io L4 sky position
SELECT round(eq_ra(galilean_lagrange_equatorial(0, 4, now()))::numeric, 4) AS ra_hours,
round(eq_dec(galilean_lagrange_equatorial(0, 4, now()))::numeric, 4) AS dec_deg;
```
---
## saturn_moon_lagrange_observe
Observe a Saturn moon Lagrange point from a ground station. Uses TASS17 theory (Vienne & Duriez 1995) for the moon position and VSOP87 for Saturn's heliocentric position.
### Signature
```sql
saturn_moon_lagrange_observe(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Saturn moon: 0=Mimas, 1=Enceladus, 2=Tethys, 3=Dione, 4=Rhea, 5=Titan, 6=Iapetus, 7=Hyperion |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Saturn-Titan L1 from Boulder
SELECT round(topo_azimuth(t)::numeric, 2) AS az,
round(topo_elevation(t)::numeric, 2) AS el
FROM saturn_moon_lagrange_observe(5, 1, '40.0N 105.3W 1655m'::observer, now()) AS t;
```
---
## saturn_moon_lagrange_equatorial
Geocentric RA/Dec of a Saturn moon Lagrange point.
### Signature
```sql
saturn_moon_lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Saturn moon: 0=Mimas, 1=Enceladus, 2=Tethys, 3=Dione, 4=Rhea, 5=Titan, 6=Iapetus, 7=Hyperion |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Saturn-Titan L1 sky position
SELECT round(eq_ra(saturn_moon_lagrange_equatorial(5, 1, now()))::numeric, 4) AS ra_hours;
```
---
## uranus_moon_lagrange_observe
Observe a Uranus moon Lagrange point from a ground station. Uses GUST86 theory (Laskar & Jacobson 1987) for the moon position and VSOP87 for Uranus's heliocentric position.
### Signature
```sql
uranus_moon_lagrange_observe(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Uranus moon: 0=Miranda, 1=Ariel, 2=Umbriel, 3=Titania, 4=Oberon |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Uranus-Titania L2 from Greenwich
SELECT round(topo_elevation(uranus_moon_lagrange_observe(3, 2, '51.4769N 0.0005W 0m'::observer, now()))::numeric, 2) AS el_deg;
```
---
## uranus_moon_lagrange_equatorial
Geocentric RA/Dec of a Uranus moon Lagrange point.
### Signature
```sql
uranus_moon_lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Uranus moon: 0=Miranda, 1=Ariel, 2=Umbriel, 3=Titania, 4=Oberon |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Uranus-Oberon L4 sky position
SELECT round(eq_ra(uranus_moon_lagrange_equatorial(4, 4, now()))::numeric, 4) AS ra_hours,
round(eq_dec(uranus_moon_lagrange_equatorial(4, 4, now()))::numeric, 4) AS dec_deg;
```
---
## mars_moon_lagrange_observe
Observe a Mars moon Lagrange point from a ground station. Uses MarsSat theory (Jacobson 2014) for the moon position and VSOP87 for Mars's heliocentric position.
### Signature
```sql
mars_moon_lagrange_observe(body_id int4, point_id int4, obs observer, t timestamptz) → topocentric
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Mars moon: 0=Phobos, 1=Deimos |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `obs` | `observer` | Observer location on Earth |
| `t` | `timestamptz` | Observation time |
### Returns
A `topocentric` with azimuth, elevation (degrees), range (km), and range rate (km/s).
### Example
```sql
-- Mars-Phobos L1 from Boulder
SELECT round(topo_azimuth(t)::numeric, 2) AS az,
round(topo_elevation(t)::numeric, 2) AS el
FROM mars_moon_lagrange_observe(0, 1, '40.0N 105.3W 1655m'::observer, now()) AS t;
```
---
## mars_moon_lagrange_equatorial
Geocentric RA/Dec of a Mars moon Lagrange point.
### Signature
```sql
mars_moon_lagrange_equatorial(body_id int4, point_id int4, t timestamptz) → equatorial
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Mars moon: 0=Phobos, 1=Deimos |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
An `equatorial` with RA (hours), Dec (degrees), and distance (km).
### Example
```sql
-- Mars-Deimos L5 sky position
SELECT round(eq_ra(mars_moon_lagrange_equatorial(1, 5, now()))::numeric, 4) AS ra_hours,
round(eq_dec(mars_moon_lagrange_equatorial(1, 5, now()))::numeric, 4) AS dec_deg;
```
---
## hill_radius
Hill sphere radius (AU) for a Sun-planet system. The Hill sphere is the region where a planet's gravity dominates over the Sun's --- objects beyond this radius are more strongly influenced by the Sun. Computed as r_H = a * (m_p / (3 * m_sun))^(1/3), where a is the instantaneous Sun-planet distance from VSOP87.
### Signature
```sql
hill_radius(body_id int4, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `t` | `timestamptz` | Evaluation time |
### Returns
Hill sphere radius in AU.
### Example
```sql
-- Jupiter's Hill sphere (~0.35 AU)
SELECT round(hill_radius(5, '2000-01-01 12:00:00+00')::numeric, 3) AS jupiter_hill_au;
```
```sql
-- All planets
SELECT body_id,
round(hill_radius(body_id, '2000-01-01 12:00:00+00')::numeric, 4) AS hill_au
FROM generate_series(1, 8) AS body_id;
```
---
## hill_radius_lunar
Hill sphere radius (AU) for the Earth-Moon system. Much smaller than planetary Hill spheres since the Moon is far less massive relative to Earth than planets are relative to the Sun.
### Signature
```sql
hill_radius_lunar(t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `t` | `timestamptz` | Evaluation time |
### Returns
Hill sphere radius in AU.
### Example
```sql
SELECT hill_radius_lunar('2000-01-01 12:00:00+00') AS lunar_hill_au;
```
---
## lagrange_zone_radius
Approximate libration zone radius (AU) around a Sun-planet Lagrange point. For L4/L5, this is related to the tadpole/horseshoe orbit domain where Trojan asteroids can remain trapped. For collinear points (L1/L2/L3), it is the linearized stability boundary.
### Signature
```sql
lagrange_zone_radius(body_id int4, point_id int4, t timestamptz) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
| `point_id` | `int4` | Lagrange point: 1-5 (L1-L5) |
| `t` | `timestamptz` | Evaluation time |
### Returns
Zone radius in AU.
### Example
```sql
-- Jupiter L4 libration zone (Trojan swarm extent)
SELECT round(lagrange_zone_radius(5, 4, '2000-01-01 12:00:00+00')::numeric, 4) AS zone_au;
```
---
## lagrange_mass_ratio
CR3BP mass parameter mu = M_planet / (M_sun + M_planet). A diagnostic function useful for verifying the CR3BP solver or understanding the relative gravitational influence of a planet in its system.
### Signature
```sql
lagrange_mass_ratio(body_id int4) → float8
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `body_id` | `int4` | Planet identifier: 1-8 (Mercury-Neptune) |
<Aside type="note">
No timestamp parameter --- mass ratios are compiled-in constants derived from IAU standard gravitational parameters.
</Aside>
### Returns
Dimensionless mass ratio (small positive number; Jupiter is ~0.001, Earth is ~0.000003).
### Example
```sql
SELECT lagrange_mass_ratio(5) AS jupiter_mu,
lagrange_mass_ratio(3) AS earth_mu;
```
---
## lagrange_point_name
Human-readable name for a Lagrange point ID. A simple lookup that converts integer IDs to their standard labels.
### Signature
```sql
lagrange_point_name(point_id int4) → text
```
### Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `point_id` | `int4` | Lagrange point: 1-5 |
### Returns
Text label: `'L1'`, `'L2'`, `'L3'`, `'L4'`, or `'L5'`.
### Example
```sql
SELECT lagrange_point_name(1) AS name;
-- -> 'L1'
```
```sql
-- Use in a query for readable output
SELECT lagrange_point_name(p) AS point,
round(helio_distance(lagrange_heliocentric(3, p, now()))::numeric, 4) AS sun_dist_au
FROM generate_series(1, 5) AS p;
```

View File

@ -1,4 +1,4 @@
comment = 'A database orrery — celestial mechanics types and functions for PostgreSQL' comment = 'A database orrery — celestial mechanics types and functions for PostgreSQL'
default_version = '0.20.0' default_version = '0.19.0'
module_pathname = '$libdir/pg_orrery' module_pathname = '$libdir/pg_orrery'
relocatable = true relocatable = true

View File

@ -1,244 +0,0 @@
-- pg_orrery 0.19.0 -> 0.20.0: Lagrange point support
-- CR3BP equilibrium positions for Sun-planet, Earth-Moon, and planetary moon systems.
-- ============================================================
-- Sun-planet Lagrange functions (5)
-- ============================================================
CREATE FUNCTION lagrange_heliocentric(int4, int4, timestamptz) RETURNS heliocentric
AS 'MODULE_PATHNAME', 'lagrange_heliocentric'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_heliocentric(int4, int4, timestamptz) IS
'Heliocentric ecliptic J2000 position of a Sun-planet Lagrange point. body_id: 1-8 (Mercury-Neptune), point_id: 1-5 (L1-L5).';
CREATE FUNCTION lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_observe(int4, int4, observer, timestamptz) IS
'Observe a Sun-planet Lagrange point from a ground station. body_id: 1-8, point_id: 1-5.';
CREATE FUNCTION lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_equatorial(int4, int4, timestamptz) IS
'Geocentric RA/Dec of a Sun-planet Lagrange point. body_id: 1-8, point_id: 1-5.';
CREATE FUNCTION lagrange_distance(int4, int4, heliocentric, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_distance'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance(int4, int4, heliocentric, timestamptz) IS
'Distance (AU) from a heliocentric position to a Sun-planet Lagrange point.';
CREATE FUNCTION lagrange_distance_oe(int4, int4, orbital_elements, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_distance_oe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_oe(int4, int4, orbital_elements, timestamptz) IS
'Distance (AU) from an asteroid/comet (orbital_elements) to a Sun-planet Lagrange point.';
-- ============================================================
-- Earth-Moon Lagrange functions (2)
-- ============================================================
CREATE FUNCTION lunar_lagrange_observe(int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'lunar_lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_observe(int4, observer, timestamptz) IS
'Observe an Earth-Moon Lagrange point. point_id: 1-5 (L1-L5).';
CREATE FUNCTION lunar_lagrange_equatorial(int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'lunar_lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_equatorial(int4, timestamptz) IS
'Geocentric RA/Dec of an Earth-Moon Lagrange point. point_id: 1-5 (L1-L5).';
-- ============================================================
-- Planetary moon Lagrange functions (8)
-- ============================================================
CREATE FUNCTION galilean_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'galilean_lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_observe(int4, int4, observer, timestamptz) IS
'Observe a Jupiter-Galilean moon Lagrange point. body_id: 0-3 (Io-Callisto), point_id: 1-5.';
CREATE FUNCTION galilean_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'galilean_lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_equatorial(int4, int4, timestamptz) IS
'Geocentric RA/Dec of a Jupiter-Galilean moon Lagrange point. body_id: 0-3, point_id: 1-5.';
CREATE FUNCTION saturn_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
'Observe a Saturn moon Lagrange point. body_id: 0-7 (Mimas-Hyperion), point_id: 1-5.';
CREATE FUNCTION saturn_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_equatorial(int4, int4, timestamptz) IS
'Geocentric RA/Dec of a Saturn moon Lagrange point. body_id: 0-7, point_id: 1-5.';
CREATE FUNCTION uranus_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
'Observe a Uranus moon Lagrange point. body_id: 0-4 (Miranda-Oberon), point_id: 1-5.';
CREATE FUNCTION uranus_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_equatorial(int4, int4, timestamptz) IS
'Geocentric RA/Dec of a Uranus moon Lagrange point. body_id: 0-4, point_id: 1-5.';
CREATE FUNCTION mars_moon_lagrange_observe(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'mars_moon_lagrange_observe'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_observe(int4, int4, observer, timestamptz) IS
'Observe a Mars moon Lagrange point. body_id: 0-1 (Phobos-Deimos), point_id: 1-5.';
CREATE FUNCTION mars_moon_lagrange_equatorial(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'mars_moon_lagrange_equatorial'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_equatorial(int4, int4, timestamptz) IS
'Geocentric RA/Dec of a Mars moon Lagrange point. body_id: 0-1, point_id: 1-5.';
-- ============================================================
-- Hill radius / zone / convenience (5)
-- ============================================================
CREATE FUNCTION hill_radius(int4, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'hill_radius_func'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius(int4, timestamptz) IS
'Hill sphere radius (AU) for a Sun-planet system. body_id: 1-8.';
CREATE FUNCTION hill_radius_lunar(timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'hill_radius_lunar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius_lunar(timestamptz) IS
'Hill sphere radius (AU) for the Earth-Moon system.';
CREATE FUNCTION lagrange_zone_radius(int4, int4, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_zone_radius_func'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_zone_radius(int4, int4, timestamptz) IS
'Approximate libration zone radius (AU) for a Sun-planet Lagrange point.';
CREATE FUNCTION lagrange_mass_ratio(int4) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_mass_ratio'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_mass_ratio(int4) IS
'CR3BP mass parameter mu = M_planet / (M_sun + M_planet) for debugging. body_id: 1-8.';
CREATE FUNCTION lagrange_point_name(int4) RETURNS text
AS 'MODULE_PATHNAME', 'lagrange_point_name'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_point_name(int4) IS
'Human-readable name for a Lagrange point ID (1->''L1'', ..., 5->''L5'').';
-- ============================================================
-- DE variant functions (17) -- STABLE
-- ============================================================
CREATE FUNCTION lagrange_heliocentric_de(int4, int4, timestamptz) RETURNS heliocentric
AS 'MODULE_PATHNAME', 'lagrange_heliocentric_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_heliocentric_de(int4, int4, timestamptz) IS
'DE variant of lagrange_heliocentric(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_observe_de(int4, int4, observer, timestamptz) IS
'DE variant of lagrange_observe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_equatorial_de(int4, int4, timestamptz) IS
'DE variant of lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lagrange_distance_de(int4, int4, heliocentric, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_distance_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_de(int4, int4, heliocentric, timestamptz) IS
'DE variant of lagrange_distance(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lagrange_distance_oe_de(int4, int4, orbital_elements, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_distance_oe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_distance_oe_de(int4, int4, orbital_elements, timestamptz) IS
'DE variant of lagrange_distance_oe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lunar_lagrange_observe_de(int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'lunar_lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_observe_de(int4, observer, timestamptz) IS
'DE variant of lunar_lagrange_observe(). Falls back to ELP2000-82B if DE unavailable.';
CREATE FUNCTION lunar_lagrange_equatorial_de(int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'lunar_lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lunar_lagrange_equatorial_de(int4, timestamptz) IS
'DE variant of lunar_lagrange_equatorial(). Falls back to ELP2000-82B if DE unavailable.';
CREATE FUNCTION galilean_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'galilean_lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_observe_de(int4, int4, observer, timestamptz) IS
'DE variant of galilean_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION galilean_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'galilean_lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION galilean_lagrange_equatorial_de(int4, int4, timestamptz) IS
'DE variant of galilean_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION saturn_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
'DE variant of saturn_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION saturn_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'saturn_moon_lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION saturn_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
'DE variant of saturn_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION uranus_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
'DE variant of uranus_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION uranus_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'uranus_moon_lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION uranus_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
'DE variant of uranus_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION mars_moon_lagrange_observe_de(int4, int4, observer, timestamptz) RETURNS topocentric
AS 'MODULE_PATHNAME', 'mars_moon_lagrange_observe_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_observe_de(int4, int4, observer, timestamptz) IS
'DE variant of mars_moon_lagrange_observe(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION mars_moon_lagrange_equatorial_de(int4, int4, timestamptz) RETURNS equatorial
AS 'MODULE_PATHNAME', 'mars_moon_lagrange_equatorial_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION mars_moon_lagrange_equatorial_de(int4, int4, timestamptz) IS
'DE variant of mars_moon_lagrange_equatorial(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION hill_radius_de(int4, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'hill_radius_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION hill_radius_de(int4, timestamptz) IS
'DE variant of hill_radius(). Falls back to VSOP87 if DE unavailable.';
CREATE FUNCTION lagrange_zone_radius_de(int4, int4, timestamptz) RETURNS float8
AS 'MODULE_PATHNAME', 'lagrange_zone_radius_de'
LANGUAGE C STABLE STRICT PARALLEL SAFE;
COMMENT ON FUNCTION lagrange_zone_radius_de(int4, int4, timestamptz) IS
'DE variant of lagrange_zone_radius(). Falls back to VSOP87 if DE unavailable.';

File diff suppressed because it is too large Load Diff

View File

@ -33,7 +33,6 @@
#include "tass17.h" #include "tass17.h"
#include "gust86.h" #include "gust86.h"
#include "marssat.h" #include "marssat.h"
#include "lagrange.h"
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
@ -63,25 +62,6 @@ PG_FUNCTION_INFO_V1(uranus_moon_equatorial_de);
PG_FUNCTION_INFO_V1(mars_moon_equatorial_de); PG_FUNCTION_INFO_V1(mars_moon_equatorial_de);
PG_FUNCTION_INFO_V1(pg_orrery_ephemeris_info); PG_FUNCTION_INFO_V1(pg_orrery_ephemeris_info);
/* Lagrange DE variants */
PG_FUNCTION_INFO_V1(lagrange_heliocentric_de);
PG_FUNCTION_INFO_V1(lagrange_observe_de);
PG_FUNCTION_INFO_V1(lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(lagrange_distance_de);
PG_FUNCTION_INFO_V1(lagrange_distance_oe_de);
PG_FUNCTION_INFO_V1(lunar_lagrange_observe_de);
PG_FUNCTION_INFO_V1(lunar_lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(galilean_lagrange_observe_de);
PG_FUNCTION_INFO_V1(galilean_lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(saturn_moon_lagrange_observe_de);
PG_FUNCTION_INFO_V1(saturn_moon_lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(uranus_moon_lagrange_observe_de);
PG_FUNCTION_INFO_V1(uranus_moon_lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(mars_moon_lagrange_observe_de);
PG_FUNCTION_INFO_V1(mars_moon_lagrange_equatorial_de);
PG_FUNCTION_INFO_V1(hill_radius_de);
PG_FUNCTION_INFO_V1(lagrange_zone_radius_de);
/* ================================================================ /* ================================================================
* planet_heliocentric_de(body_id int, timestamptz) -> heliocentric * planet_heliocentric_de(body_id int, timestamptz) -> heliocentric
@ -1326,951 +1306,3 @@ pg_orrery_ephemeris_info(PG_FUNCTION_ARGS)
tuple = heap_form_tuple(tupdesc, values, nulls); tuple = heap_form_tuple(tupdesc, values, nulls);
PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
} }
/* ================================================================
* Lagrange point functions with DE ephemeris
*
* DE variants of the Lagrange functions in lagrange_funcs.c.
* Each uses DE for planet/Earth positions where possible,
* falling back to VSOP87/ELP2000-82B on any DE failure.
* Rule 7 always holds: both target and Earth from the same provider.
* ================================================================
*/
/*
* Validate body_id and point_id for Sun-planet Lagrange functions.
* Duplicated from lagrange_funcs.c (no cross-TU symbols).
*/
static void
validate_lagrange_args_de(int body_id, int point_id, const char *func_name)
{
if (body_id < BODY_MERCURY || body_id > BODY_NEPTUNE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("%s: body_id %d must be 1-8 (Mercury-Neptune)",
func_name, body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("%s: point_id %d must be 1-5 (L1-L5)",
func_name, point_id)));
}
/*
* Compute Sun-planet Lagrange point using DE for planet position+velocity.
* Falls back to VSOP87 if DE unavailable (rule 7: consistent provider).
*
* Returns 0 on success, -1 on solver failure.
* Sets *used_de to indicate which provider was used.
*/
static int
compute_sun_planet_lagrange_de(int body_id, int point_id, double jd,
double result[3], bool *used_de)
{
double planet_xyz[6];
double sun[3] = {0.0, 0.0, 0.0};
double planet_vel[3];
double ratio, mu;
/* Try DE for planet position */
if (eph_de_planet(body_id, jd, planet_xyz))
{
/* Velocity via central difference (DE provides position only) */
double pos_fwd[6], pos_bwd[6];
double dt = 0.01; /* days */
bool got_fwd = eph_de_planet(body_id, jd + dt, pos_fwd);
bool got_bwd = eph_de_planet(body_id, jd - dt, pos_bwd);
if (got_fwd && got_bwd)
{
planet_vel[0] = (pos_fwd[0] - pos_bwd[0]) / (2.0 * dt);
planet_vel[1] = (pos_fwd[1] - pos_bwd[1]) / (2.0 * dt);
planet_vel[2] = (pos_fwd[2] - pos_bwd[2]) / (2.0 * dt);
*used_de = true;
}
else
{
/* DE boundary straddled — fall through to VSOP87 */
goto vsop87_fallback;
}
}
else
{
vsop87_fallback:
{
int vsop_body = body_id - 1;
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable for this query, falling back to VSOP87")));
GetVsop87Coor(jd, vsop_body, planet_xyz);
/* VSOP87 provides analytic velocity in xyz[3..5] */
planet_vel[0] = planet_xyz[3];
planet_vel[1] = planet_xyz[4];
planet_vel[2] = planet_xyz[5];
*used_de = false;
}
}
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
return lagrange_position(sun, planet_xyz, planet_vel, mu, point_id, result);
}
/*
* Compute Earth-Moon Lagrange point using DE for Moon position.
* Falls back to ELP2000-82B if DE unavailable.
*
* Moon velocity always via central difference (neither DE nor ELP
* provide direct velocity). Result is geocentric ecliptic J2000.
*/
static int
compute_lunar_lagrange_de(int point_id, double jd, double result_geo[3],
bool *used_de)
{
double moon_pos[3], moon_fwd[3], moon_bwd[3];
double moon_vel[3];
double earth[3] = {0.0, 0.0, 0.0};
double mu;
double dt = 0.001; /* days */
if (eph_de_moon(jd, moon_pos))
{
bool got_fwd = eph_de_moon(jd + dt, moon_fwd);
bool got_bwd = eph_de_moon(jd - dt, moon_bwd);
if (got_fwd && got_bwd)
{
*used_de = true;
}
else
{
/* DE boundary straddled */
goto elp_fallback;
}
}
else
{
elp_fallback:
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable, falling back to ELP2000-82B")));
GetElp82bCoor(jd, moon_pos);
GetElp82bCoor(jd + dt, moon_fwd);
GetElp82bCoor(jd - dt, moon_bwd);
*used_de = false;
}
moon_vel[0] = (moon_fwd[0] - moon_bwd[0]) / (2.0 * dt);
moon_vel[1] = (moon_fwd[1] - moon_bwd[1]) / (2.0 * dt);
moon_vel[2] = (moon_fwd[2] - moon_bwd[2]) / (2.0 * dt);
mu = mu_from_ratio(EARTH_MOON_EMRAT);
return lagrange_position(earth, moon_pos, moon_vel, mu, point_id,
result_geo);
}
/*
* Observe a planetary moon Lagrange point using DE for parent planet
* and Earth positions. Falls back to VSOP87 if DE unavailable.
*
* lp_rel[3]: L-point offset relative to parent (ecliptic J2000, AU)
* parent_body_id: pg_orrery body ID (5=Jupiter, 6=Saturn, etc.)
*/
static void
observe_pmoon_lagrange_de(const double lp_rel[3], int parent_body_id,
double jd, const pg_observer *obs,
pg_topocentric *result)
{
double parent_xyz[6];
double earth_xyz[6];
double geo_ecl[3];
bool have_de;
/* Rule 7: both parent and Earth from same provider */
have_de = eph_de_planet(parent_body_id, jd, parent_xyz) &&
eph_de_earth(jd, earth_xyz);
if (!have_de)
{
int vsop_parent = parent_body_id - 1;
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable, falling back to VSOP87")));
GetVsop87Coor(jd, vsop_parent, parent_xyz);
GetVsop87Coor(jd, 2, earth_xyz);
}
geo_ecl[0] = (parent_xyz[0] + lp_rel[0]) - earth_xyz[0];
geo_ecl[1] = (parent_xyz[1] + lp_rel[1]) - earth_xyz[1];
geo_ecl[2] = (parent_xyz[2] + lp_rel[2]) - earth_xyz[2];
observe_from_geocentric(geo_ecl, jd, obs, result);
}
static void
equatorial_pmoon_lagrange_de(const double lp_rel[3], int parent_body_id,
double jd, pg_equatorial *result)
{
double parent_xyz[6];
double earth_xyz[6];
double geo_ecl[3];
bool have_de;
have_de = eph_de_planet(parent_body_id, jd, parent_xyz) &&
eph_de_earth(jd, earth_xyz);
if (!have_de)
{
int vsop_parent = parent_body_id - 1;
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable, falling back to VSOP87")));
GetVsop87Coor(jd, vsop_parent, parent_xyz);
GetVsop87Coor(jd, 2, earth_xyz);
}
geo_ecl[0] = (parent_xyz[0] + lp_rel[0]) - earth_xyz[0];
geo_ecl[1] = (parent_xyz[1] + lp_rel[1]) - earth_xyz[1];
geo_ecl[2] = (parent_xyz[2] + lp_rel[2]) - earth_xyz[2];
geocentric_to_equatorial(geo_ecl, jd, result);
}
/*
* Compute planetary moon Lagrange offset (no cross-TU call).
* Duplicated from lagrange_funcs.c.
*/
static int
compute_planetary_moon_lagrange_de(const double moon_rel[3],
const double moon_vel[3],
char family, int moon_id,
int point_id,
double lp_rel[3])
{
double planet_origin[3] = {0.0, 0.0, 0.0};
double ratio, mu;
ratio = planet_moon_ratio(family, moon_id);
if (ratio < 0.0)
return -1;
mu = mu_from_ratio(ratio);
return lagrange_position(planet_origin, moon_rel, moon_vel, mu,
point_id, lp_rel);
}
/* ================================================================
* 1. lagrange_heliocentric_de(body_id, point_id, timestamptz)
* -> heliocentric
*
* DE variant of lagrange_heliocentric(). STABLE.
* ================================================================
*/
Datum
lagrange_heliocentric_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp[3];
bool used_de;
pg_heliocentric *result;
validate_lagrange_args_de(body_id, point_id, "lagrange_heliocentric_de");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange_de(body_id, point_id, jd, lp, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_heliocentric_de: solver failed for body %d L%d",
body_id, point_id)));
result = (pg_heliocentric *) palloc(sizeof(pg_heliocentric));
result->x = lp[0];
result->y = lp[1];
result->z = lp[2];
PG_RETURN_POINTER(result);
}
/* ================================================================
* 2. lagrange_observe_de(body_id, point_id, observer, timestamptz)
* -> topocentric
*
* DE variant of lagrange_observe(). STABLE.
* Rule 7: L-point + Earth from same provider.
* ================================================================
*/
Datum
lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3], earth_xyz[6], geo_ecl[3];
bool used_de;
pg_topocentric *result;
validate_lagrange_args_de(body_id, point_id, "lagrange_observe_de");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange_de(body_id, point_id, jd, lp, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_observe_de: solver failed")));
/* Earth from same provider as the L-point (rule 7) */
if (used_de && eph_de_earth(jd, earth_xyz))
{
/* DE succeeded for both */
}
else
{
GetVsop87Coor(jd, 2, earth_xyz);
}
geo_ecl[0] = lp[0] - earth_xyz[0];
geo_ecl[1] = lp[1] - earth_xyz[1];
geo_ecl[2] = lp[2] - earth_xyz[2];
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_from_geocentric(geo_ecl, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* 3. lagrange_equatorial_de(body_id, point_id, timestamptz)
* -> equatorial
*
* DE variant of lagrange_equatorial(). STABLE.
* ================================================================
*/
Datum
lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp[3], earth_xyz[6], geo_ecl[3];
bool used_de;
pg_equatorial *result;
validate_lagrange_args_de(body_id, point_id, "lagrange_equatorial_de");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange_de(body_id, point_id, jd, lp, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_equatorial_de: solver failed")));
if (used_de && eph_de_earth(jd, earth_xyz))
{
/* DE succeeded */
}
else
{
GetVsop87Coor(jd, 2, earth_xyz);
}
geo_ecl[0] = lp[0] - earth_xyz[0];
geo_ecl[1] = lp[1] - earth_xyz[1];
geo_ecl[2] = lp[2] - earth_xyz[2];
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
geocentric_to_equatorial(geo_ecl, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* 4. lagrange_distance_de(body_id, point_id, heliocentric, timestamptz)
* -> float8
*
* Distance from a heliocentric position to a Sun-planet L-point (AU).
* Uses DE for planet position. STABLE.
* ================================================================
*/
Datum
lagrange_distance_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_heliocentric *pos = (pg_heliocentric *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3];
double dx, dy, dz, dist;
bool used_de;
validate_lagrange_args_de(body_id, point_id, "lagrange_distance_de");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange_de(body_id, point_id, jd, lp, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_distance_de: solver failed")));
dx = pos->x - lp[0];
dy = pos->y - lp[1];
dz = pos->z - lp[2];
dist = sqrt(dx*dx + dy*dy + dz*dz);
PG_RETURN_FLOAT8(dist);
}
/* ================================================================
* 5. lagrange_distance_oe_de(body_id, point_id, orbital_elements, timestamptz)
* -> float8
*
* Distance from an asteroid/comet to a Sun-planet L-point (AU).
* Uses DE for L-point computation. STABLE.
* ================================================================
*/
Datum
lagrange_distance_oe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_orbital_elements *oe = (pg_orbital_elements *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3], body_pos[3];
double dx, dy, dz, dist;
bool used_de;
validate_lagrange_args_de(body_id, point_id, "lagrange_distance_oe_de");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange_de(body_id, point_id, jd, lp, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_distance_oe_de: solver failed")));
kepler_position(oe->q, oe->e, oe->inc, oe->arg_peri, oe->raan,
oe->tp, jd, body_pos);
dx = body_pos[0] - lp[0];
dy = body_pos[1] - lp[1];
dz = body_pos[2] - lp[2];
dist = sqrt(dx*dx + dy*dy + dz*dz);
PG_RETURN_FLOAT8(dist);
}
/* ================================================================
* 6. lunar_lagrange_observe_de(point_id, observer, timestamptz)
* -> topocentric
*
* Earth-Moon L-point using DE Moon position. STABLE.
* ================================================================
*/
Datum
lunar_lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 point_id = PG_GETARG_INT32(0);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp_geo[3];
bool used_de;
pg_topocentric *result;
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lunar_lagrange_observe_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
if (compute_lunar_lagrange_de(point_id, jd, lp_geo, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lunar_lagrange_observe_de: solver failed")));
/* lp_geo is already geocentric ecliptic J2000 (AU) */
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_from_geocentric(lp_geo, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* 7. lunar_lagrange_equatorial_de(point_id, timestamptz) -> equatorial
*
* Earth-Moon L-point using DE Moon position. STABLE.
* ================================================================
*/
Datum
lunar_lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 point_id = PG_GETARG_INT32(0);
int64 ts = PG_GETARG_INT64(1);
double jd;
double lp_geo[3];
bool used_de;
pg_equatorial *result;
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lunar_lagrange_equatorial_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
if (compute_lunar_lagrange_de(point_id, jd, lp_geo, &used_de) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lunar_lagrange_equatorial_de: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
geocentric_to_equatorial(lp_geo, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* Planetary moon Lagrange points with DE parent positions
*
* Moon-theory offset (L1.2, TASS17, GUST86, MarsSat) computed
* relative to parent, then Lagrange solver applied. Parent planet
* and Earth positions from DE (with VSOP87 fallback).
* ================================================================
*/
/* ── 8. Galilean Lagrange observe DE ─────────────────────── */
Datum
galilean_lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < L12_IO || body_id > L12_CALLISTO)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_observe_de: body_id %d must be 0-3",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_observe_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetL12Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'g', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("galilean_lagrange_observe_de: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange_de(lp_rel, BODY_JUPITER, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ── 9. Galilean Lagrange equatorial DE ──────────────────── */
Datum
galilean_lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < L12_IO || body_id > L12_CALLISTO)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_equatorial_de: body_id %d must be 0-3",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_equatorial_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetL12Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'g', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("galilean_lagrange_equatorial_de: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange_de(lp_rel, BODY_JUPITER, jd, result);
PG_RETURN_POINTER(result);
}
/* ── 10. Saturn moon Lagrange observe DE ─────────────────── */
Datum
saturn_moon_lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < TASS17_MIMAS || body_id > TASS17_HYPERION)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_observe_de: body_id %d must be 0-7",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_observe_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetTass17Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 's', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("saturn_moon_lagrange_observe_de: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange_de(lp_rel, BODY_SATURN, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ── 11. Saturn moon Lagrange equatorial DE ──────────────── */
Datum
saturn_moon_lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < TASS17_MIMAS || body_id > TASS17_HYPERION)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_equatorial_de: body_id %d must be 0-7",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_equatorial_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetTass17Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 's', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("saturn_moon_lagrange_equatorial_de: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange_de(lp_rel, BODY_SATURN, jd, result);
PG_RETURN_POINTER(result);
}
/* ── 12. Uranus moon Lagrange observe DE ─────────────────── */
Datum
uranus_moon_lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < GUST86_MIRANDA || body_id > GUST86_OBERON)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_observe_de: body_id %d must be 0-4",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_observe_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetGust86Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'u', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("uranus_moon_lagrange_observe_de: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange_de(lp_rel, BODY_URANUS, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ── 13. Uranus moon Lagrange equatorial DE ──────────────── */
Datum
uranus_moon_lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < GUST86_MIRANDA || body_id > GUST86_OBERON)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_equatorial_de: body_id %d must be 0-4",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_equatorial_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetGust86Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'u', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("uranus_moon_lagrange_equatorial_de: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange_de(lp_rel, BODY_URANUS, jd, result);
PG_RETURN_POINTER(result);
}
/* ── 14. Mars moon Lagrange observe DE ───────────────────── */
Datum
mars_moon_lagrange_observe_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < MARS_SAT_PHOBOS || body_id > MARS_SAT_DEIMOS)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_observe_de: body_id %d must be 0-1",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_observe_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetMarsSatCoor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'm', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("mars_moon_lagrange_observe_de: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange_de(lp_rel, BODY_MARS, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ── 15. Mars moon Lagrange equatorial DE ────────────────── */
Datum
mars_moon_lagrange_equatorial_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < MARS_SAT_PHOBOS || body_id > MARS_SAT_DEIMOS)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_equatorial_de: body_id %d must be 0-1",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_equatorial_de: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetMarsSatCoor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange_de(moon_xyz, moon_vel, 'm', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("mars_moon_lagrange_equatorial_de: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange_de(lp_rel, BODY_MARS, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* 16. hill_radius_de(body_id, timestamptz) -> float8 (AU)
*
* Hill radius using DE for planet heliocentric distance. STABLE.
* ================================================================
*/
Datum
hill_radius_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int64 ts = PG_GETARG_INT64(1);
double jd;
double planet_xyz[6], sep, ratio, mu;
if (body_id < BODY_MERCURY || body_id > BODY_NEPTUNE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("hill_radius_de: body_id %d must be 1-8", body_id)));
jd = timestamptz_to_jd(ts);
if (!eph_de_planet(body_id, jd, planet_xyz))
{
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable, falling back to VSOP87")));
GetVsop87Coor(jd, body_id - 1, planet_xyz);
}
sep = sqrt(planet_xyz[0]*planet_xyz[0] +
planet_xyz[1]*planet_xyz[1] +
planet_xyz[2]*planet_xyz[2]);
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
PG_RETURN_FLOAT8(lagrange_hill_radius(sep, mu));
}
/* ================================================================
* 17. lagrange_zone_radius_de(body_id, point_id, timestamptz)
* -> float8 (AU)
*
* Libration zone radius using DE for planet distance. STABLE.
* ================================================================
*/
Datum
lagrange_zone_radius_de(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double planet_xyz[6], sep, ratio, mu, zr;
validate_lagrange_args_de(body_id, point_id, "lagrange_zone_radius_de");
jd = timestamptz_to_jd(ts);
if (!eph_de_planet(body_id, jd, planet_xyz))
{
if (eph_de_available())
ereport(NOTICE,
(errmsg("DE ephemeris unavailable, falling back to VSOP87")));
GetVsop87Coor(jd, body_id - 1, planet_xyz);
}
sep = sqrt(planet_xyz[0]*planet_xyz[0] +
planet_xyz[1]*planet_xyz[1] +
planet_xyz[2]*planet_xyz[2]);
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
zr = lagrange_zone_radius(sep, mu, point_id);
PG_RETURN_FLOAT8(zr);
}

View File

@ -1,492 +0,0 @@
/*
* lagrange.h -- Circular restricted three-body problem (CR3BP) solver
*
* Computes the five Lagrange equilibrium points for any gravitational
* two-body system. The solver is pure C with no PostgreSQL dependency,
* no global state, and no memory allocation.
*
* The CR3BP uses the mass parameter mu = M_secondary / (M_primary + M_secondary).
* In the co-rotating frame normalized to unit separation, L1/L2/L3 lie
* on the x-axis and L4/L5 form equilateral triangles.
*
* L1/L2/L3 positions come from Newton-Raphson on the quintic
* equilibrium polynomial. L4/L5 are exact analytic.
*
* References:
* Szebehely V., "Theory of Orbits" (1967), Academic Press
* Murray & Dermott, "Solar System Dynamics" (1999), Cambridge
*/
#ifndef PG_ORRERY_LAGRANGE_H
#define PG_ORRERY_LAGRANGE_H
#include <math.h>
/* ── Lagrange point identifiers ────────────────────────── */
#define LAGRANGE_L1 1
#define LAGRANGE_L2 2
#define LAGRANGE_L3 3
#define LAGRANGE_L4 4
#define LAGRANGE_L5 5
/* ── Sun-planet mass ratios ────────────────────────────── */
/*
* GM_sun / GM_planet ratios. Convert to CR3BP mu via:
* mu = 1.0 / (1.0 + ratio)
*
* Sources: IAU 2012 nominal masses, JPL DE441 constants.
* The Earth ratio includes the Moon (Earth+Moon system barycenter).
*/
#define SUN_MERCURY_RATIO 6023682.155
#define SUN_VENUS_RATIO 408523.7187
#define SUN_EARTH_RATIO 332946.0487 /* Earth+Moon system */
#define SUN_MARS_RATIO 3098703.59
#define SUN_JUPITER_RATIO 1047.348644
#define SUN_SATURN_RATIO 3497.9018
#define SUN_URANUS_RATIO 22902.98
#define SUN_NEPTUNE_RATIO 19412.26
/* ── Earth-Moon mass ratio ─────────────────────────────── */
/*
* M_earth / M_moon. From DE441 EMRAT constant.
* mu = 1.0 / (1.0 + EARTH_MOON_EMRAT)
*/
#define EARTH_MOON_EMRAT 81.300568
/* ── Planet-moon GM ratios ─────────────────────────────── */
/*
* GM_planet / GM_moon from spacecraft-derived values.
* mu = 1.0 / (1.0 + ratio)
*
* Galilean moons (Schubert et al. 2004, Anderson et al. 1996-2001):
*/
#define JUPITER_IO_RATIO 22423.9 /* GM_Jup / GM_Io */
#define JUPITER_EUROPA_RATIO 39478.0 /* GM_Jup / GM_Europa */
#define JUPITER_GANYMEDE_RATIO 12716.0 /* GM_Jup / GM_Ganymede */
#define JUPITER_CALLISTO_RATIO 17350.0 /* GM_Jup / GM_Callisto */
/*
* Saturn moons (Jacobson et al. 2006):
*/
#define SATURN_MIMAS_RATIO 15108611.0
#define SATURN_ENCELADUS_RATIO 4955938.0
#define SATURN_TETHYS_RATIO 6137851.0
#define SATURN_DIONE_RATIO 3430825.0
#define SATURN_RHEA_RATIO 1629997.0
#define SATURN_TITAN_RATIO 4226.5 /* Titan is massive */
#define SATURN_IAPETUS_RATIO 3148296.0
#define SATURN_HYPERION_RATIO 6.821e9 /* tiny */
/*
* Uranus moons (Jacobson et al. 1992):
*/
#define URANUS_MIRANDA_RATIO 1311870.0
#define URANUS_ARIEL_RATIO 65229.0
#define URANUS_UMBRIEL_RATIO 72449.0
#define URANUS_TITANIA_RATIO 24399.0
#define URANUS_OBERON_RATIO 25399.0
/*
* Mars moons (Jacobson 2014):
*/
#define MARS_PHOBOS_RATIO 5.8775e7
#define MARS_DEIMOS_RATIO 3.919e8
/* ── Maximum Newton-Raphson iterations ─────────────────── */
#define LAGRANGE_MAX_ITER 50
/* ── Core API ──────────────────────────────────────────── */
/*
* Solve for a Lagrange point in the normalized co-rotating frame.
*
* mu: mass parameter = M2 / (M1 + M2), must be in (0, 0.5]
* point_id: LAGRANGE_L1 through LAGRANGE_L5
* x, y: output co-rotating coordinates (normalized to unit separation)
* Origin at barycenter. Primary at (-mu, 0), secondary at (1-mu, 0).
*
* Returns 0 on success, -1 on invalid input or convergence failure.
*/
static inline int
lagrange_corotating(double mu, int point_id, double *x, double *y)
{
double gamma, f, fp, gamma_new;
int i;
if (mu <= 0.0 || mu > 0.5 || point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
return -1;
switch (point_id)
{
case LAGRANGE_L1:
/*
* L1: between primary and secondary.
* Solve: gamma^5 - (3-mu)*gamma^4 + (3-2*mu)*gamma^3
* - mu*gamma^2 + 2*mu*gamma - mu = 0
* where gamma = distance from secondary toward primary.
* Initial guess: Hill sphere approximation.
*/
gamma = cbrt(mu / 3.0);
for (i = 0; i < LAGRANGE_MAX_ITER; i++)
{
double g2 = gamma * gamma;
double g3 = g2 * gamma;
double g4 = g3 * gamma;
double g5 = g4 * gamma;
f = g5 - (3.0 - mu) * g4 + (3.0 - 2.0 * mu) * g3
- mu * g2 + 2.0 * mu * gamma - mu;
fp = 5.0 * g4 - 4.0 * (3.0 - mu) * g3
+ 3.0 * (3.0 - 2.0 * mu) * g2
- 2.0 * mu * gamma + 2.0 * mu;
if (fabs(fp) < 1e-30)
return -1;
gamma_new = gamma - f / fp;
if (gamma_new <= 0.0)
gamma_new = gamma * 0.5; /* keep gamma positive */
if (fabs(gamma_new - gamma) < 1e-15)
break;
gamma = gamma_new;
}
if (i == LAGRANGE_MAX_ITER)
return -1;
*x = 1.0 - mu - gamma;
*y = 0.0;
break;
case LAGRANGE_L2:
/*
* L2: beyond secondary, away from primary.
* Solve: gamma^5 + (3-mu)*gamma^4 + (3-2*mu)*gamma^3
* - mu*gamma^2 - 2*mu*gamma - mu = 0
*/
gamma = cbrt(mu / 3.0);
for (i = 0; i < LAGRANGE_MAX_ITER; i++)
{
double g2 = gamma * gamma;
double g3 = g2 * gamma;
double g4 = g3 * gamma;
double g5 = g4 * gamma;
f = g5 + (3.0 - mu) * g4 + (3.0 - 2.0 * mu) * g3
- mu * g2 - 2.0 * mu * gamma - mu;
fp = 5.0 * g4 + 4.0 * (3.0 - mu) * g3
+ 3.0 * (3.0 - 2.0 * mu) * g2
- 2.0 * mu * gamma - 2.0 * mu;
if (fabs(fp) < 1e-30)
return -1;
gamma_new = gamma - f / fp;
if (gamma_new <= 0.0)
gamma_new = gamma * 0.5; /* keep gamma positive */
if (fabs(gamma_new - gamma) < 1e-15)
break;
gamma = gamma_new;
}
if (i == LAGRANGE_MAX_ITER)
return -1;
*x = 1.0 - mu + gamma;
*y = 0.0;
break;
case LAGRANGE_L3:
/*
* L3: opposite side from secondary, beyond primary.
* Solve: gamma^5 + (2+mu)*gamma^4 + (1+2*mu)*gamma^3
* - (1-mu)*gamma^2 - 2*(1-mu)*gamma - (1-mu) = 0
* where gamma = distance from primary.
*/
gamma = 1.0 - 7.0 * mu / 12.0; /* Szebehely approximation */
for (i = 0; i < LAGRANGE_MAX_ITER; i++)
{
double g2 = gamma * gamma;
double g3 = g2 * gamma;
double g4 = g3 * gamma;
double g5 = g4 * gamma;
double one_minus_mu = 1.0 - mu;
f = g5 + (2.0 + mu) * g4 + (1.0 + 2.0 * mu) * g3
- one_minus_mu * g2 - 2.0 * one_minus_mu * gamma
- one_minus_mu;
fp = 5.0 * g4 + 4.0 * (2.0 + mu) * g3
+ 3.0 * (1.0 + 2.0 * mu) * g2
- 2.0 * one_minus_mu * gamma
- 2.0 * one_minus_mu;
if (fabs(fp) < 1e-30)
return -1;
gamma_new = gamma - f / fp;
if (gamma_new <= 0.0)
gamma_new = gamma * 0.5; /* keep gamma positive */
if (fabs(gamma_new - gamma) < 1e-15)
break;
gamma = gamma_new;
}
if (i == LAGRANGE_MAX_ITER)
return -1;
*x = -mu - gamma;
*y = 0.0;
break;
case LAGRANGE_L4:
/* Equilateral triangle, leading */
*x = 0.5 - mu;
*y = sqrt(3.0) / 2.0;
break;
case LAGRANGE_L5:
/* Equilateral triangle, trailing */
*x = 0.5 - mu;
*y = -sqrt(3.0) / 2.0;
break;
default:
return -1;
}
return 0;
}
/*
* Transform a co-rotating Lagrange point to physical ecliptic J2000.
*
* The co-rotating frame has origin at the barycenter, x-axis along
* the primarysecondary direction, z-axis along the orbital angular
* momentum. We construct this frame from the instantaneous positions
* and velocity of the secondary relative to the primary.
*
* primary[3]: heliocentric position of primary (AU, ecliptic J2000)
* secondary[3]: heliocentric position of secondary (AU, ecliptic J2000)
* sec_vel[3]: velocity of secondary relative to primary (AU/day)
* mu: mass parameter M2/(M1+M2)
* point_id: LAGRANGE_L1..L5
* result[3]: output heliocentric position (AU, ecliptic J2000)
*
* Returns 0 on success, -1 on failure.
*/
static inline int
lagrange_position(const double primary[3], const double secondary[3],
const double sec_vel[3], double mu, int point_id,
double result[3])
{
double d[3], sep, e_x[3], e_z[3], e_y[3];
double hx, hy, hz, hmag;
double x_co, y_co;
int rc;
/* Displacement: primary → secondary */
d[0] = secondary[0] - primary[0];
d[1] = secondary[1] - primary[1];
d[2] = secondary[2] - primary[2];
sep = sqrt(d[0]*d[0] + d[1]*d[1] + d[2]*d[2]);
if (sep < 1e-30)
return -1;
/* Unit vector along primary→secondary */
e_x[0] = d[0] / sep;
e_x[1] = d[1] / sep;
e_x[2] = d[2] / sep;
/* Angular momentum direction: h = d x v */
hx = d[1] * sec_vel[2] - d[2] * sec_vel[1];
hy = d[2] * sec_vel[0] - d[0] * sec_vel[2];
hz = d[0] * sec_vel[1] - d[1] * sec_vel[0];
hmag = sqrt(hx*hx + hy*hy + hz*hz);
if (hmag < 1e-30)
return -1;
e_z[0] = hx / hmag;
e_z[1] = hy / hmag;
e_z[2] = hz / hmag;
/* e_y = e_z x e_x (completes right-handed frame) */
e_y[0] = e_z[1] * e_x[2] - e_z[2] * e_x[1];
e_y[1] = e_z[2] * e_x[0] - e_z[0] * e_x[2];
e_y[2] = e_z[0] * e_x[1] - e_z[1] * e_x[0];
/* Solve for co-rotating coordinates */
rc = lagrange_corotating(mu, point_id, &x_co, &y_co);
if (rc != 0)
return -1;
/*
* Physical position relative to barycenter:
* P_bary = primary + mu * d (barycenter location)
* L_phys = P_bary + sep * (x_co * e_x + y_co * e_y)
*
* But x_co is already relative to barycenter (origin in co-rotating
* frame), so:
* L_phys = primary + mu * d + sep * (x_co * e_x + y_co * e_y)
*/
result[0] = primary[0] + mu * d[0]
+ sep * (x_co * e_x[0] + y_co * e_y[0]);
result[1] = primary[1] + mu * d[1]
+ sep * (x_co * e_x[1] + y_co * e_y[1]);
result[2] = primary[2] + mu * d[2]
+ sep * (x_co * e_x[2] + y_co * e_y[2]);
return 0;
}
/*
* Hill sphere radius.
*
* separation_au: distance between primary and secondary (AU)
* mu: mass parameter M2/(M1+M2)
*
* Returns Hill radius in AU.
*/
static inline double
lagrange_hill_radius(double separation_au, double mu)
{
return separation_au * cbrt(mu / 3.0);
}
/*
* Libration zone radius (approximate).
*
* For L1/L2: same as Hill radius (zone extends ~r_Hill from L-point).
* For L4/L5: horseshoe/tadpole width ~ separation * sqrt(mu) (Dermott 1981).
* For L3: ~ separation * (7/12) * mu (very narrow).
*
* separation_au: distance between primary and secondary (AU)
* mu: mass parameter
* point_id: LAGRANGE_L1..L5
*
* Returns approximate zone radius in AU, or -1.0 on error.
*/
static inline double
lagrange_zone_radius(double separation_au, double mu, int point_id)
{
switch (point_id)
{
case LAGRANGE_L1:
case LAGRANGE_L2:
return lagrange_hill_radius(separation_au, mu);
case LAGRANGE_L3:
return separation_au * (7.0 / 12.0) * mu;
case LAGRANGE_L4:
case LAGRANGE_L5:
return separation_au * sqrt(mu);
default:
return -1.0;
}
}
/*
* Look up the Sun-planet mass ratio for a pg_orrery body_id.
*
* body_id: 1=Mercury..8=Neptune (pg_orrery convention)
* Returns the GM_sun/GM_planet ratio, or -1.0 for invalid body_id.
*/
static inline double
sun_planet_ratio(int body_id)
{
switch (body_id)
{
case 1: return SUN_MERCURY_RATIO;
case 2: return SUN_VENUS_RATIO;
case 3: return SUN_EARTH_RATIO;
case 4: return SUN_MARS_RATIO;
case 5: return SUN_JUPITER_RATIO;
case 6: return SUN_SATURN_RATIO;
case 7: return SUN_URANUS_RATIO;
case 8: return SUN_NEPTUNE_RATIO;
default: return -1.0;
}
}
/*
* Compute mu from a Sun/planet GM ratio.
* mu = 1 / (1 + ratio)
*/
static inline double
mu_from_ratio(double ratio)
{
return 1.0 / (1.0 + ratio);
}
/*
* Look up planet-moon GM ratio for a specific moon.
*
* family: 'g' (Galilean), 's' (Saturn), 'u' (Uranus), 'm' (Mars)
* moon_id: 0-based index within family
* Returns ratio, or -1.0 for invalid.
*/
static inline double
planet_moon_ratio(char family, int moon_id)
{
switch (family)
{
case 'g': /* Galilean */
switch (moon_id)
{
case 0: return JUPITER_IO_RATIO;
case 1: return JUPITER_EUROPA_RATIO;
case 2: return JUPITER_GANYMEDE_RATIO;
case 3: return JUPITER_CALLISTO_RATIO;
default: return -1.0;
}
case 's': /* Saturn */
switch (moon_id)
{
case 0: return SATURN_MIMAS_RATIO;
case 1: return SATURN_ENCELADUS_RATIO;
case 2: return SATURN_TETHYS_RATIO;
case 3: return SATURN_DIONE_RATIO;
case 4: return SATURN_RHEA_RATIO;
case 5: return SATURN_TITAN_RATIO;
case 6: return SATURN_IAPETUS_RATIO;
case 7: return SATURN_HYPERION_RATIO;
default: return -1.0;
}
case 'u': /* Uranus */
switch (moon_id)
{
case 0: return URANUS_MIRANDA_RATIO;
case 1: return URANUS_ARIEL_RATIO;
case 2: return URANUS_UMBRIEL_RATIO;
case 3: return URANUS_TITANIA_RATIO;
case 4: return URANUS_OBERON_RATIO;
default: return -1.0;
}
case 'm': /* Mars */
switch (moon_id)
{
case 0: return MARS_PHOBOS_RATIO;
case 1: return MARS_DEIMOS_RATIO;
default: return -1.0;
}
default:
return -1.0;
}
}
#endif /* PG_ORRERY_LAGRANGE_H */

View File

@ -1,916 +0,0 @@
/*
* lagrange_funcs.c -- Lagrange point observation functions
*
* SQL functions for computing positions and observations of the five
* Lagrange equilibrium points in the circular restricted three-body
* problem (CR3BP). Covers Sun-planet, Earth-Moon, and planetary moon
* systems.
*
* The pipeline mirrors planet_funcs.c / moon_funcs.c:
* 1. Primary + secondary heliocentric positions (VSOP87)
* 2. Secondary velocity via analytic (VSOP87) or central difference
* 3. lagrange_position() -> L-point heliocentric ecliptic J2000
* 4. Geocentric ecliptic (subtract Earth)
* 5. observe_from_geocentric() or geocentric_to_equatorial()
*
* All functions are IMMUTABLE STRICT PARALLEL SAFE (compiled-in
* mass ratios, VSOP87/ELP82B coefficients).
*/
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "utils/timestamp.h"
#include "utils/builtins.h"
#include "types.h"
#include "astro_math.h"
#include "lagrange.h"
#include "vsop87.h"
#include "kepler.h"
#include "elp82b.h"
#include "l12.h"
#include "tass17.h"
#include "gust86.h"
#include "marssat.h"
#include <math.h>
/* ── Forward declarations ──────────────────────────────── */
/* Sun-planet */
PG_FUNCTION_INFO_V1(lagrange_heliocentric);
PG_FUNCTION_INFO_V1(lagrange_observe);
PG_FUNCTION_INFO_V1(lagrange_equatorial);
PG_FUNCTION_INFO_V1(lagrange_distance);
PG_FUNCTION_INFO_V1(lagrange_distance_oe);
/* Earth-Moon */
PG_FUNCTION_INFO_V1(lunar_lagrange_observe);
PG_FUNCTION_INFO_V1(lunar_lagrange_equatorial);
/* Planetary moons */
PG_FUNCTION_INFO_V1(galilean_lagrange_observe);
PG_FUNCTION_INFO_V1(galilean_lagrange_equatorial);
PG_FUNCTION_INFO_V1(saturn_moon_lagrange_observe);
PG_FUNCTION_INFO_V1(saturn_moon_lagrange_equatorial);
PG_FUNCTION_INFO_V1(uranus_moon_lagrange_observe);
PG_FUNCTION_INFO_V1(uranus_moon_lagrange_equatorial);
PG_FUNCTION_INFO_V1(mars_moon_lagrange_observe);
PG_FUNCTION_INFO_V1(mars_moon_lagrange_equatorial);
/* Hill/zone/convenience */
PG_FUNCTION_INFO_V1(hill_radius_func);
PG_FUNCTION_INFO_V1(hill_radius_lunar);
PG_FUNCTION_INFO_V1(lagrange_zone_radius_func);
PG_FUNCTION_INFO_V1(lagrange_mass_ratio);
PG_FUNCTION_INFO_V1(lagrange_point_name);
/* ── Internal helpers ──────────────────────────────────── */
/*
* Validate body_id and point_id common to Sun-planet functions.
*/
static void
validate_lagrange_args(int body_id, int point_id, const char *func_name)
{
if (body_id < BODY_MERCURY || body_id > BODY_NEPTUNE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("%s: body_id %d must be 1-8 (Mercury-Neptune)",
func_name, body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("%s: point_id %d must be 1-5 (L1-L5)",
func_name, point_id)));
}
/*
* Compute Sun-planet Lagrange point in heliocentric ecliptic J2000 (AU).
*
* Uses VSOP87 for planet position and velocity. The Sun is at (0,0,0).
*/
static int
compute_sun_planet_lagrange(int body_id, int point_id, double jd,
double result[3])
{
double planet_xyz[6];
double sun[3] = {0.0, 0.0, 0.0};
double ratio, mu;
int vsop_body = body_id - 1;
GetVsop87Coor(jd, vsop_body, planet_xyz);
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
/* planet_xyz[3..5] is velocity in AU/day from VSOP87 */
return lagrange_position(sun, planet_xyz, &planet_xyz[3], mu, point_id,
result);
}
/*
* Compute Earth-Moon Lagrange point in geocentric ecliptic J2000 (AU).
*
* ELP2000-82B provides position only. Velocity via central difference
* (dt=0.001 day ~ 86 seconds). Error ~40 km at lunar distance negligible
* vs libration zone scale (~60,000 km for L4/L5).
*
* Returns position relative to Earth (geocentric), not heliocentric.
*/
static int
compute_lunar_lagrange(int point_id, double jd, double result_geo[3])
{
double moon_pos[3], moon_fwd[3], moon_bwd[3];
double moon_vel[3];
double earth[3] = {0.0, 0.0, 0.0}; /* geocentric frame: Earth at origin */
double mu;
double dt = 0.001; /* days */
int rc;
/* Moon geocentric position */
GetElp82bCoor(jd, moon_pos);
/* Velocity via central difference */
GetElp82bCoor(jd + dt, moon_fwd);
GetElp82bCoor(jd - dt, moon_bwd);
moon_vel[0] = (moon_fwd[0] - moon_bwd[0]) / (2.0 * dt);
moon_vel[1] = (moon_fwd[1] - moon_bwd[1]) / (2.0 * dt);
moon_vel[2] = (moon_fwd[2] - moon_bwd[2]) / (2.0 * dt);
mu = mu_from_ratio(EARTH_MOON_EMRAT);
rc = lagrange_position(earth, moon_pos, moon_vel, mu, point_id,
result_geo);
return rc;
}
/*
* Compute planet-moon Lagrange point heliocentric ecliptic J2000 (AU).
*
* The moon theory provides both position and velocity relative to the
* parent planet. We work in a frame centered on the planet.
*/
static int
compute_planetary_moon_lagrange(const double moon_rel[3],
const double moon_vel[3],
char family, int moon_id,
int point_id,
double lp_rel[3])
{
double planet_origin[3] = {0.0, 0.0, 0.0};
double ratio, mu;
ratio = planet_moon_ratio(family, moon_id);
if (ratio < 0.0)
return -1;
mu = mu_from_ratio(ratio);
return lagrange_position(planet_origin, moon_rel, moon_vel, mu,
point_id, lp_rel);
}
/* ================================================================
* lagrange_heliocentric(body_id int4, point_id int4, timestamptz)
* -> heliocentric
*
* Sun-planet Lagrange point in heliocentric ecliptic J2000.
* ================================================================
*/
Datum
lagrange_heliocentric(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp[3];
pg_heliocentric *result;
validate_lagrange_args(body_id, point_id, "lagrange_heliocentric");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange(body_id, point_id, jd, lp) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_heliocentric: solver failed for body %d L%d",
body_id, point_id)));
result = (pg_heliocentric *) palloc(sizeof(pg_heliocentric));
result->x = lp[0];
result->y = lp[1];
result->z = lp[2];
PG_RETURN_POINTER(result);
}
/* ================================================================
* lagrange_observe(body_id, point_id, observer, timestamptz)
* -> topocentric
*
* Full pipeline: L-point heliocentric -> geocentric -> az/el.
* ================================================================
*/
Datum
lagrange_observe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3], earth_xyz[6], geo_ecl[3];
pg_topocentric *result;
validate_lagrange_args(body_id, point_id, "lagrange_observe");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange(body_id, point_id, jd, lp) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_observe: solver failed")));
/* Geocentric */
GetVsop87Coor(jd, 2, earth_xyz);
geo_ecl[0] = lp[0] - earth_xyz[0];
geo_ecl[1] = lp[1] - earth_xyz[1];
geo_ecl[2] = lp[2] - earth_xyz[2];
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_from_geocentric(geo_ecl, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* lagrange_equatorial(body_id, point_id, timestamptz) -> equatorial
* ================================================================
*/
Datum
lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp[3], earth_xyz[6], geo_ecl[3];
pg_equatorial *result;
validate_lagrange_args(body_id, point_id, "lagrange_equatorial");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange(body_id, point_id, jd, lp) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_equatorial: solver failed")));
GetVsop87Coor(jd, 2, earth_xyz);
geo_ecl[0] = lp[0] - earth_xyz[0];
geo_ecl[1] = lp[1] - earth_xyz[1];
geo_ecl[2] = lp[2] - earth_xyz[2];
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
geocentric_to_equatorial(geo_ecl, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* lagrange_distance(body_id, point_id, heliocentric, timestamptz)
* -> float8
*
* Distance from a heliocentric position to a Sun-planet L-point (AU).
* ================================================================
*/
Datum
lagrange_distance(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_heliocentric *pos = (pg_heliocentric *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3];
double dx, dy, dz, dist;
validate_lagrange_args(body_id, point_id, "lagrange_distance");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange(body_id, point_id, jd, lp) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_distance: solver failed")));
dx = pos->x - lp[0];
dy = pos->y - lp[1];
dz = pos->z - lp[2];
dist = sqrt(dx*dx + dy*dy + dz*dz);
PG_RETURN_FLOAT8(dist);
}
/* ================================================================
* lagrange_distance_oe(body_id, point_id, orbital_elements, timestamptz)
* -> float8
*
* Distance from an asteroid/comet to a Sun-planet L-point (AU).
* ================================================================
*/
Datum
lagrange_distance_oe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_orbital_elements *oe = (pg_orbital_elements *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double lp[3], body_pos[3];
double dx, dy, dz, dist;
validate_lagrange_args(body_id, point_id, "lagrange_distance_oe");
jd = timestamptz_to_jd(ts);
if (compute_sun_planet_lagrange(body_id, point_id, jd, lp) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lagrange_distance_oe: solver failed")));
kepler_position(oe->q, oe->e, oe->inc, oe->arg_peri, oe->raan,
oe->tp, jd, body_pos);
dx = body_pos[0] - lp[0];
dy = body_pos[1] - lp[1];
dz = body_pos[2] - lp[2];
dist = sqrt(dx*dx + dy*dy + dz*dz);
PG_RETURN_FLOAT8(dist);
}
/* ================================================================
* lunar_lagrange_observe(point_id, observer, timestamptz) -> topocentric
*
* Earth-Moon Lagrange point observation.
* ================================================================
*/
Datum
lunar_lagrange_observe(PG_FUNCTION_ARGS)
{
int32 point_id = PG_GETARG_INT32(0);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double lp_geo[3];
pg_topocentric *result;
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lunar_lagrange_observe: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
if (compute_lunar_lagrange(point_id, jd, lp_geo) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lunar_lagrange_observe: solver failed")));
/* lp_geo is already geocentric ecliptic J2000 (AU) */
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_from_geocentric(lp_geo, jd, obs, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* lunar_lagrange_equatorial(point_id, timestamptz) -> equatorial
* ================================================================
*/
Datum
lunar_lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 point_id = PG_GETARG_INT32(0);
int64 ts = PG_GETARG_INT64(1);
double jd;
double lp_geo[3];
pg_equatorial *result;
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lunar_lagrange_equatorial: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
if (compute_lunar_lagrange(point_id, jd, lp_geo) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("lunar_lagrange_equatorial: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
geocentric_to_equatorial(lp_geo, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* Planetary moon Lagrange points
*
* Each family duplicates the observe/equatorial static helpers from
* moon_funcs.c (per no-cross-TU-symbol convention), but routes through
* the Lagrange solver instead of returning the moon position directly.
* ================================================================
*/
/*
* Internal: observe a planetary moon Lagrange point.
* L-point offset is relative to planet, same as moon offset.
*/
static void
observe_pmoon_lagrange(const double lp_rel[3], int vsop_parent,
double jd, const pg_observer *obs,
pg_topocentric *result)
{
double parent_xyz[6];
double earth_xyz[6];
double geo_ecl[3];
GetVsop87Coor(jd, vsop_parent, parent_xyz);
GetVsop87Coor(jd, 2, earth_xyz);
geo_ecl[0] = (parent_xyz[0] + lp_rel[0]) - earth_xyz[0];
geo_ecl[1] = (parent_xyz[1] + lp_rel[1]) - earth_xyz[1];
geo_ecl[2] = (parent_xyz[2] + lp_rel[2]) - earth_xyz[2];
observe_from_geocentric(geo_ecl, jd, obs, result);
}
static void
equatorial_pmoon_lagrange(const double lp_rel[3], int vsop_parent,
double jd, pg_equatorial *result)
{
double parent_xyz[6];
double earth_xyz[6];
double geo_ecl[3];
GetVsop87Coor(jd, vsop_parent, parent_xyz);
GetVsop87Coor(jd, 2, earth_xyz);
geo_ecl[0] = (parent_xyz[0] + lp_rel[0]) - earth_xyz[0];
geo_ecl[1] = (parent_xyz[1] + lp_rel[1]) - earth_xyz[1];
geo_ecl[2] = (parent_xyz[2] + lp_rel[2]) - earth_xyz[2];
geocentric_to_equatorial(geo_ecl, jd, result);
}
/* ── Galilean moons ────────────────────────────────────── */
Datum
galilean_lagrange_observe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < L12_IO || body_id > L12_CALLISTO)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_observe: body_id %d must be 0-3",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_observe: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetL12Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'g', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("galilean_lagrange_observe: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange(lp_rel, 4, jd, obs, result);
PG_RETURN_POINTER(result);
}
Datum
galilean_lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < L12_IO || body_id > L12_CALLISTO)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_equatorial: body_id %d must be 0-3",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("galilean_lagrange_equatorial: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetL12Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'g', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("galilean_lagrange_equatorial: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange(lp_rel, 4, jd, result);
PG_RETURN_POINTER(result);
}
/* ── Saturn moons ──────────────────────────────────────── */
Datum
saturn_moon_lagrange_observe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < TASS17_MIMAS || body_id > TASS17_HYPERION)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_observe: body_id %d must be 0-7",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_observe: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetTass17Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 's', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("saturn_moon_lagrange_observe: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange(lp_rel, 5, jd, obs, result);
PG_RETURN_POINTER(result);
}
Datum
saturn_moon_lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < TASS17_MIMAS || body_id > TASS17_HYPERION)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_equatorial: body_id %d must be 0-7",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("saturn_moon_lagrange_equatorial: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetTass17Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 's', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("saturn_moon_lagrange_equatorial: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange(lp_rel, 5, jd, result);
PG_RETURN_POINTER(result);
}
/* ── Uranus moons ──────────────────────────────────────── */
Datum
uranus_moon_lagrange_observe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < GUST86_MIRANDA || body_id > GUST86_OBERON)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_observe: body_id %d must be 0-4",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_observe: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetGust86Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'u', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("uranus_moon_lagrange_observe: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange(lp_rel, 6, jd, obs, result);
PG_RETURN_POINTER(result);
}
Datum
uranus_moon_lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < GUST86_MIRANDA || body_id > GUST86_OBERON)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_equatorial: body_id %d must be 0-4",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("uranus_moon_lagrange_equatorial: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetGust86Coor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'u', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("uranus_moon_lagrange_equatorial: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange(lp_rel, 6, jd, result);
PG_RETURN_POINTER(result);
}
/* ── Mars moons ────────────────────────────────────────── */
Datum
mars_moon_lagrange_observe(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
pg_observer *obs = (pg_observer *) PG_GETARG_POINTER(2);
int64 ts = PG_GETARG_INT64(3);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_topocentric *result;
if (body_id < MARS_SAT_PHOBOS || body_id > MARS_SAT_DEIMOS)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_observe: body_id %d must be 0-1",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_observe: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetMarsSatCoor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'm', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("mars_moon_lagrange_observe: solver failed")));
result = (pg_topocentric *) palloc(sizeof(pg_topocentric));
observe_pmoon_lagrange(lp_rel, 3, jd, obs, result);
PG_RETURN_POINTER(result);
}
Datum
mars_moon_lagrange_equatorial(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double moon_xyz[3], moon_vel[3], lp_rel[3];
pg_equatorial *result;
if (body_id < MARS_SAT_PHOBOS || body_id > MARS_SAT_DEIMOS)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_equatorial: body_id %d must be 0-1",
body_id)));
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("mars_moon_lagrange_equatorial: point_id %d must be 1-5",
point_id)));
jd = timestamptz_to_jd(ts);
GetMarsSatCoor(jd, body_id, moon_xyz, moon_vel);
if (compute_planetary_moon_lagrange(moon_xyz, moon_vel, 'm', body_id,
point_id, lp_rel) != 0)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("mars_moon_lagrange_equatorial: solver failed")));
result = (pg_equatorial *) palloc(sizeof(pg_equatorial));
equatorial_pmoon_lagrange(lp_rel, 3, jd, result);
PG_RETURN_POINTER(result);
}
/* ================================================================
* Hill radius / zone / convenience functions
* ================================================================
*/
/* hill_radius(body_id int4, timestamptz) -> float8 (AU) */
Datum
hill_radius_func(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int64 ts = PG_GETARG_INT64(1);
double jd;
double planet_xyz[6], sep, ratio, mu;
if (body_id < BODY_MERCURY || body_id > BODY_NEPTUNE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("hill_radius: body_id %d must be 1-8", body_id)));
jd = timestamptz_to_jd(ts);
GetVsop87Coor(jd, body_id - 1, planet_xyz);
sep = sqrt(planet_xyz[0]*planet_xyz[0] +
planet_xyz[1]*planet_xyz[1] +
planet_xyz[2]*planet_xyz[2]);
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
PG_RETURN_FLOAT8(lagrange_hill_radius(sep, mu));
}
/* hill_radius_lunar(timestamptz) -> float8 (AU) */
Datum
hill_radius_lunar(PG_FUNCTION_ARGS)
{
int64 ts = PG_GETARG_INT64(0);
double jd;
double moon_pos[3], sep, mu;
jd = timestamptz_to_jd(ts);
GetElp82bCoor(jd, moon_pos);
sep = sqrt(moon_pos[0]*moon_pos[0] +
moon_pos[1]*moon_pos[1] +
moon_pos[2]*moon_pos[2]);
mu = mu_from_ratio(EARTH_MOON_EMRAT);
PG_RETURN_FLOAT8(lagrange_hill_radius(sep, mu));
}
/* lagrange_zone_radius(body_id, point_id, timestamptz) -> float8 (AU) */
Datum
lagrange_zone_radius_func(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
int32 point_id = PG_GETARG_INT32(1);
int64 ts = PG_GETARG_INT64(2);
double jd;
double planet_xyz[6], sep, ratio, mu, zr;
validate_lagrange_args(body_id, point_id, "lagrange_zone_radius");
jd = timestamptz_to_jd(ts);
GetVsop87Coor(jd, body_id - 1, planet_xyz);
sep = sqrt(planet_xyz[0]*planet_xyz[0] +
planet_xyz[1]*planet_xyz[1] +
planet_xyz[2]*planet_xyz[2]);
ratio = sun_planet_ratio(body_id);
mu = mu_from_ratio(ratio);
zr = lagrange_zone_radius(sep, mu, point_id);
PG_RETURN_FLOAT8(zr);
}
/* lagrange_mass_ratio(body_id int4) -> float8 */
Datum
lagrange_mass_ratio(PG_FUNCTION_ARGS)
{
int32 body_id = PG_GETARG_INT32(0);
double ratio;
if (body_id < BODY_MERCURY || body_id > BODY_NEPTUNE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lagrange_mass_ratio: body_id %d must be 1-8",
body_id)));
ratio = sun_planet_ratio(body_id);
PG_RETURN_FLOAT8(mu_from_ratio(ratio));
}
/* lagrange_point_name(point_id int4) -> text */
Datum
lagrange_point_name(PG_FUNCTION_ARGS)
{
int32 point_id = PG_GETARG_INT32(0);
if (point_id < LAGRANGE_L1 || point_id > LAGRANGE_L5)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("lagrange_point_name: point_id %d must be 1-5",
point_id)));
switch (point_id)
{
case LAGRANGE_L1: PG_RETURN_TEXT_P(cstring_to_text("L1"));
case LAGRANGE_L2: PG_RETURN_TEXT_P(cstring_to_text("L2"));
case LAGRANGE_L3: PG_RETURN_TEXT_P(cstring_to_text("L3"));
case LAGRANGE_L4: PG_RETURN_TEXT_P(cstring_to_text("L4"));
case LAGRANGE_L5: PG_RETURN_TEXT_P(cstring_to_text("L5"));
default: PG_RETURN_NULL();
}
}

View File

@ -1,405 +0,0 @@
-- v020_features: Lagrange point support
-- Tests Sun-planet, Earth-Moon, planetary moon Lagrange points,
-- Hill radius, zone radius, DE fallback, and input validation.
CREATE EXTENSION IF NOT EXISTS pg_orrery;
NOTICE: extension "pg_orrery" already exists, skipping
-- Reference observer: Greenwich, UK
\set obs '''(51.4769,-0.0005,0)'''
-- Reference time: J2000 epoch (2000-01-01 12:00:00 UTC)
\set t '''2000-01-01 12:00:00+00'''
-- ============================================================
-- Sun-Earth L1/L2: should be ~0.01 AU from Earth (~1.5 million km)
-- SOHO is at L1, JWST at L2.
-- ============================================================
-- L1 heliocentric: should be close to Earth's heliocentric (~1 AU from Sun)
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 2) AS sun_dist_au;
sun_dist_au
-------------
0.97
(1 row)
-- L2 heliocentric: also ~1 AU from Sun, slightly further than L1
SELECT
round(helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))::numeric, 2) AS sun_dist_au;
sun_dist_au
-------------
0.99
(1 row)
-- L1 between Sun and Earth (closer to Sun than L2)
SELECT
helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))
<
helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))
AS l1_closer_than_l2;
l1_closer_than_l2
-------------------
t
(1 row)
-- ============================================================
-- Sun-Jupiter L4/L5: ~60 degrees from Jupiter, ~5.2 AU from Sun
-- These are the Trojan asteroid zones.
-- ============================================================
-- L4/L5 should be ~5.2 AU from Sun
SELECT
round(helio_distance(lagrange_heliocentric(5, 4, :t ::timestamptz))::numeric, 1) AS l4_sun_dist;
l4_sun_dist
-------------
5.0
(1 row)
SELECT
round(helio_distance(lagrange_heliocentric(5, 5, :t ::timestamptz))::numeric, 1) AS l5_sun_dist;
l5_sun_dist
-------------
5.0
(1 row)
-- L4 and L5 equidistant from Sun (within 0.001 AU)
SELECT
abs(
helio_distance(lagrange_heliocentric(5, 4, :t ::timestamptz))
-
helio_distance(lagrange_heliocentric(5, 5, :t ::timestamptz))
) < 0.001 AS l4_l5_equidistant;
l4_l5_equidistant
-------------------
t
(1 row)
-- ============================================================
-- Earth-Moon L1: ~326,000 km from Earth
-- ============================================================
-- lunar_lagrange_equatorial returns distance in km
SELECT
round(eq_distance(lunar_lagrange_equatorial(1, :t ::timestamptz))::numeric, -3)
BETWEEN 300000 AND 360000 AS em_l1_in_range;
em_l1_in_range
----------------
t
(1 row)
-- ============================================================
-- lagrange_observe returns valid az/el
-- ============================================================
SELECT
topo_elevation(lagrange_observe(3, 2, :obs ::observer, :t ::timestamptz))
BETWEEN -90 AND 90 AS valid_elevation;
valid_elevation
-----------------
t
(1 row)
-- lagrange_equatorial returns valid RA/Dec
SELECT
eq_ra(lagrange_equatorial(3, 1, :t ::timestamptz)) BETWEEN 0 AND 24 AS valid_ra,
eq_dec(lagrange_equatorial(3, 1, :t ::timestamptz)) BETWEEN -90 AND 90 AS valid_dec;
valid_ra | valid_dec
----------+-----------
t | t
(1 row)
-- ============================================================
-- lagrange_distance self-test: L-point distance to itself ≈ 0
-- ============================================================
SELECT
round(lagrange_distance(
5, 4,
lagrange_heliocentric(5, 4, :t ::timestamptz),
:t ::timestamptz
)::numeric, 10) AS self_distance;
self_distance
---------------
0.0000000000
(1 row)
-- ============================================================
-- Hill radius
-- ============================================================
-- Jupiter Hill radius ~0.35 AU
SELECT
round(hill_radius(5, :t ::timestamptz)::numeric, 2)
BETWEEN 0.30 AND 0.40 AS jupiter_hill_ok;
jupiter_hill_ok
-----------------
t
(1 row)
-- Earth Hill radius ~0.01 AU
SELECT
round(hill_radius(3, :t ::timestamptz)::numeric, 3)
BETWEEN 0.008 AND 0.012 AS earth_hill_ok;
earth_hill_ok
---------------
t
(1 row)
-- Lunar Hill radius (much smaller, AU)
SELECT
hill_radius_lunar(:t ::timestamptz) > 0 AS lunar_hill_positive;
lunar_hill_positive
---------------------
t
(1 row)
-- ============================================================
-- Zone radius
-- ============================================================
SELECT
lagrange_zone_radius(5, 4, :t ::timestamptz) > 0 AS jup_l4_zone_positive;
jup_l4_zone_positive
----------------------
t
(1 row)
SELECT
lagrange_zone_radius(5, 1, :t ::timestamptz) > 0 AS jup_l1_zone_positive;
jup_l1_zone_positive
----------------------
t
(1 row)
-- ============================================================
-- Convenience functions
-- ============================================================
-- lagrange_mass_ratio returns small positive number
SELECT
lagrange_mass_ratio(5) > 0 AND lagrange_mass_ratio(5) < 0.01 AS jupiter_mu_ok;
jupiter_mu_ok
---------------
t
(1 row)
SELECT
lagrange_mass_ratio(3) > 0 AND lagrange_mass_ratio(3) < 0.001 AS earth_mu_ok;
earth_mu_ok
-------------
t
(1 row)
-- lagrange_point_name
SELECT lagrange_point_name(1) AS l1_name;
l1_name
---------
L1
(1 row)
SELECT lagrange_point_name(5) AS l5_name;
l5_name
---------
L5
(1 row)
-- ============================================================
-- All planets produce valid results
-- ============================================================
SELECT body_id,
round(helio_distance(lagrange_heliocentric(body_id, 1, :t ::timestamptz))::numeric, 2) AS sun_dist_au
FROM generate_series(1, 8) AS body_id
ORDER BY body_id;
body_id | sun_dist_au
---------+-------------
1 | 0.46
2 | 0.71
3 | 0.97
4 | 1.38
5 | 4.63
6 | 8.77
7 | 19.44
8 | 29.35
(8 rows)
-- ============================================================
-- Planetary moon Lagrange points
-- ============================================================
-- Galilean: Io L4 (body=0, point=4)
SELECT
eq_ra(galilean_lagrange_equatorial(0, 4, :t ::timestamptz)) BETWEEN 0 AND 24
AS io_l4_valid_ra;
io_l4_valid_ra
----------------
t
(1 row)
-- Saturn: Titan L1 (body=5, point=1)
SELECT
eq_ra(saturn_moon_lagrange_equatorial(5, 1, :t ::timestamptz)) BETWEEN 0 AND 24
AS titan_l1_valid_ra;
titan_l1_valid_ra
-------------------
t
(1 row)
-- Uranus: Titania L2 (body=3, point=2)
SELECT
eq_ra(uranus_moon_lagrange_equatorial(3, 2, :t ::timestamptz)) BETWEEN 0 AND 24
AS titania_l2_valid_ra;
titania_l2_valid_ra
---------------------
t
(1 row)
-- Mars: Phobos L5 (body=0, point=5)
SELECT
eq_ra(mars_moon_lagrange_equatorial(0, 5, :t ::timestamptz)) BETWEEN 0 AND 24
AS phobos_l5_valid_ra;
phobos_l5_valid_ra
--------------------
t
(1 row)
-- Galilean observe returns valid topocentric
SELECT
topo_elevation(galilean_lagrange_observe(2, 3, :obs ::observer, :t ::timestamptz))
BETWEEN -90 AND 90 AS ganymede_l3_valid_el;
ganymede_l3_valid_el
----------------------
t
(1 row)
-- ============================================================
-- DE fallback (no DE loaded, should produce same results as VSOP87)
-- ============================================================
SELECT
round(helio_distance(lagrange_heliocentric_de(3, 1, :t ::timestamptz))::numeric, 4) AS de_l1_dist;
de_l1_dist
------------
0.9735
(1 row)
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 4) AS vsop_l1_dist;
vsop_l1_dist
--------------
0.9735
(1 row)
-- DE hill_radius fallback
SELECT
round(hill_radius_de(5, :t ::timestamptz)::numeric, 4) =
round(hill_radius(5, :t ::timestamptz)::numeric, 4)
AS hill_de_matches_vsop;
hill_de_matches_vsop
----------------------
t
(1 row)
-- ============================================================
-- Tighter L1/L2 precision (4 decimal places)
-- ============================================================
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 4) AS earth_l1_dist;
earth_l1_dist
---------------
0.9735
(1 row)
SELECT
round(helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))::numeric, 4) AS earth_l2_dist;
earth_l2_dist
---------------
0.9932
(1 row)
-- L1 and L2 bracket Earth's heliocentric distance
SELECT
helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))
<
helio_distance(planet_heliocentric(3, :t ::timestamptz))
AND
helio_distance(planet_heliocentric(3, :t ::timestamptz))
<
helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))
AS l1_earth_l2_ordering;
l1_earth_l2_ordering
----------------------
t
(1 row)
-- ============================================================
-- lagrange_distance_oe — Ceres distance from Jupiter L4
-- Ceres orbits at ~2.77 AU, Jupiter L4 at ~5.2 AU, so distance > 2 AU
-- ============================================================
SELECT
round(lagrange_distance_oe(
5, 4,
oe_from_mpc('00001 3.33 0.12 K24AM 60.07966 73.42937 80.26860 10.58664 0.0789126 0.21406048 2.7660961 0 MPO838504 8738 115 1801-2024 0.65 M-v 30k MPCLINUX 0000 (1) Ceres 20240825'),
:t ::timestamptz
)::numeric, 2) AS ceres_jup_l4_dist;
ceres_jup_l4_dist
-------------------
3.03
(1 row)
-- Distance should be positive and > 2 AU (main belt vs Trojan zone)
SELECT
lagrange_distance_oe(
5, 4,
oe_from_mpc('00001 3.33 0.12 K24AM 60.07966 73.42937 80.26860 10.58664 0.0789126 0.21406048 2.7660961 0 MPO838504 8738 115 1801-2024 0.65 M-v 30k MPCLINUX 0000 (1) Ceres 20240825'),
:t ::timestamptz
) > 2.0 AS ceres_far_from_trojan;
ceres_far_from_trojan
-----------------------
t
(1 row)
-- ============================================================
-- DE fallback for planetary moon Lagrange functions
-- ============================================================
SELECT
round(eq_ra(galilean_lagrange_equatorial_de(0, 4, :t ::timestamptz))::numeric, 4) =
round(eq_ra(galilean_lagrange_equatorial(0, 4, :t ::timestamptz))::numeric, 4)
AS galilean_de_fallback_matches;
galilean_de_fallback_matches
------------------------------
t
(1 row)
SELECT
round(eq_ra(saturn_moon_lagrange_equatorial_de(5, 1, :t ::timestamptz))::numeric, 4) =
round(eq_ra(saturn_moon_lagrange_equatorial(5, 1, :t ::timestamptz))::numeric, 4)
AS saturn_de_fallback_matches;
saturn_de_fallback_matches
----------------------------
t
(1 row)
-- ============================================================
-- Input validation
-- ============================================================
-- Bad body_id
SELECT lagrange_heliocentric(0, 1, :t ::timestamptz); -- Sun not valid
ERROR: lagrange_heliocentric: body_id 0 must be 1-8 (Mercury-Neptune)
SELECT lagrange_heliocentric(9, 1, :t ::timestamptz); -- body 9 invalid
ERROR: lagrange_heliocentric: body_id 9 must be 1-8 (Mercury-Neptune)
-- Bad point_id
SELECT lagrange_heliocentric(3, 0, :t ::timestamptz); -- point 0 invalid
ERROR: lagrange_heliocentric: point_id 0 must be 1-5 (L1-L5)
SELECT lagrange_heliocentric(3, 6, :t ::timestamptz); -- point 6 invalid
ERROR: lagrange_heliocentric: point_id 6 must be 1-5 (L1-L5)
-- Bad lunar point_id
SELECT lunar_lagrange_equatorial(0, :t ::timestamptz); -- point 0 invalid
ERROR: lunar_lagrange_equatorial: point_id 0 must be 1-5
SELECT lunar_lagrange_equatorial(6, :t ::timestamptz); -- point 6 invalid
ERROR: lunar_lagrange_equatorial: point_id 6 must be 1-5
-- Bad planetary moon body_id
SELECT galilean_lagrange_equatorial(4, 1, :t ::timestamptz); -- Galilean 4 invalid
ERROR: galilean_lagrange_equatorial: body_id 4 must be 0-3
SELECT saturn_moon_lagrange_equatorial(8, 1, :t ::timestamptz); -- Saturn 8 invalid
ERROR: saturn_moon_lagrange_equatorial: body_id 8 must be 0-7
SELECT uranus_moon_lagrange_equatorial(5, 1, :t ::timestamptz); -- Uranus 5 invalid
ERROR: uranus_moon_lagrange_equatorial: body_id 5 must be 0-4
SELECT mars_moon_lagrange_equatorial(2, 1, :t ::timestamptz); -- Mars 2 invalid
ERROR: mars_moon_lagrange_equatorial: body_id 2 must be 0-1
-- lagrange_mass_ratio bad body
SELECT lagrange_mass_ratio(0);
ERROR: lagrange_mass_ratio: body_id 0 must be 1-8
SELECT lagrange_mass_ratio(9);
ERROR: lagrange_mass_ratio: body_id 9 must be 1-8
-- lagrange_point_name bad id
SELECT lagrange_point_name(0);
ERROR: lagrange_point_name: point_id 0 must be 1-5
SELECT lagrange_point_name(6);
ERROR: lagrange_point_name: point_id 6 must be 1-5

View File

@ -1,266 +0,0 @@
-- v020_features: Lagrange point support
-- Tests Sun-planet, Earth-Moon, planetary moon Lagrange points,
-- Hill radius, zone radius, DE fallback, and input validation.
CREATE EXTENSION IF NOT EXISTS pg_orrery;
-- Reference observer: Greenwich, UK
\set obs '''(51.4769,-0.0005,0)'''
-- Reference time: J2000 epoch (2000-01-01 12:00:00 UTC)
\set t '''2000-01-01 12:00:00+00'''
-- ============================================================
-- Sun-Earth L1/L2: should be ~0.01 AU from Earth (~1.5 million km)
-- SOHO is at L1, JWST at L2.
-- ============================================================
-- L1 heliocentric: should be close to Earth's heliocentric (~1 AU from Sun)
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 2) AS sun_dist_au;
-- L2 heliocentric: also ~1 AU from Sun, slightly further than L1
SELECT
round(helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))::numeric, 2) AS sun_dist_au;
-- L1 between Sun and Earth (closer to Sun than L2)
SELECT
helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))
<
helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))
AS l1_closer_than_l2;
-- ============================================================
-- Sun-Jupiter L4/L5: ~60 degrees from Jupiter, ~5.2 AU from Sun
-- These are the Trojan asteroid zones.
-- ============================================================
-- L4/L5 should be ~5.2 AU from Sun
SELECT
round(helio_distance(lagrange_heliocentric(5, 4, :t ::timestamptz))::numeric, 1) AS l4_sun_dist;
SELECT
round(helio_distance(lagrange_heliocentric(5, 5, :t ::timestamptz))::numeric, 1) AS l5_sun_dist;
-- L4 and L5 equidistant from Sun (within 0.001 AU)
SELECT
abs(
helio_distance(lagrange_heliocentric(5, 4, :t ::timestamptz))
-
helio_distance(lagrange_heliocentric(5, 5, :t ::timestamptz))
) < 0.001 AS l4_l5_equidistant;
-- ============================================================
-- Earth-Moon L1: ~326,000 km from Earth
-- ============================================================
-- lunar_lagrange_equatorial returns distance in km
SELECT
round(eq_distance(lunar_lagrange_equatorial(1, :t ::timestamptz))::numeric, -3)
BETWEEN 300000 AND 360000 AS em_l1_in_range;
-- ============================================================
-- lagrange_observe returns valid az/el
-- ============================================================
SELECT
topo_elevation(lagrange_observe(3, 2, :obs ::observer, :t ::timestamptz))
BETWEEN -90 AND 90 AS valid_elevation;
-- lagrange_equatorial returns valid RA/Dec
SELECT
eq_ra(lagrange_equatorial(3, 1, :t ::timestamptz)) BETWEEN 0 AND 24 AS valid_ra,
eq_dec(lagrange_equatorial(3, 1, :t ::timestamptz)) BETWEEN -90 AND 90 AS valid_dec;
-- ============================================================
-- lagrange_distance self-test: L-point distance to itself ≈ 0
-- ============================================================
SELECT
round(lagrange_distance(
5, 4,
lagrange_heliocentric(5, 4, :t ::timestamptz),
:t ::timestamptz
)::numeric, 10) AS self_distance;
-- ============================================================
-- Hill radius
-- ============================================================
-- Jupiter Hill radius ~0.35 AU
SELECT
round(hill_radius(5, :t ::timestamptz)::numeric, 2)
BETWEEN 0.30 AND 0.40 AS jupiter_hill_ok;
-- Earth Hill radius ~0.01 AU
SELECT
round(hill_radius(3, :t ::timestamptz)::numeric, 3)
BETWEEN 0.008 AND 0.012 AS earth_hill_ok;
-- Lunar Hill radius (much smaller, AU)
SELECT
hill_radius_lunar(:t ::timestamptz) > 0 AS lunar_hill_positive;
-- ============================================================
-- Zone radius
-- ============================================================
SELECT
lagrange_zone_radius(5, 4, :t ::timestamptz) > 0 AS jup_l4_zone_positive;
SELECT
lagrange_zone_radius(5, 1, :t ::timestamptz) > 0 AS jup_l1_zone_positive;
-- ============================================================
-- Convenience functions
-- ============================================================
-- lagrange_mass_ratio returns small positive number
SELECT
lagrange_mass_ratio(5) > 0 AND lagrange_mass_ratio(5) < 0.01 AS jupiter_mu_ok;
SELECT
lagrange_mass_ratio(3) > 0 AND lagrange_mass_ratio(3) < 0.001 AS earth_mu_ok;
-- lagrange_point_name
SELECT lagrange_point_name(1) AS l1_name;
SELECT lagrange_point_name(5) AS l5_name;
-- ============================================================
-- All planets produce valid results
-- ============================================================
SELECT body_id,
round(helio_distance(lagrange_heliocentric(body_id, 1, :t ::timestamptz))::numeric, 2) AS sun_dist_au
FROM generate_series(1, 8) AS body_id
ORDER BY body_id;
-- ============================================================
-- Planetary moon Lagrange points
-- ============================================================
-- Galilean: Io L4 (body=0, point=4)
SELECT
eq_ra(galilean_lagrange_equatorial(0, 4, :t ::timestamptz)) BETWEEN 0 AND 24
AS io_l4_valid_ra;
-- Saturn: Titan L1 (body=5, point=1)
SELECT
eq_ra(saturn_moon_lagrange_equatorial(5, 1, :t ::timestamptz)) BETWEEN 0 AND 24
AS titan_l1_valid_ra;
-- Uranus: Titania L2 (body=3, point=2)
SELECT
eq_ra(uranus_moon_lagrange_equatorial(3, 2, :t ::timestamptz)) BETWEEN 0 AND 24
AS titania_l2_valid_ra;
-- Mars: Phobos L5 (body=0, point=5)
SELECT
eq_ra(mars_moon_lagrange_equatorial(0, 5, :t ::timestamptz)) BETWEEN 0 AND 24
AS phobos_l5_valid_ra;
-- Galilean observe returns valid topocentric
SELECT
topo_elevation(galilean_lagrange_observe(2, 3, :obs ::observer, :t ::timestamptz))
BETWEEN -90 AND 90 AS ganymede_l3_valid_el;
-- ============================================================
-- DE fallback (no DE loaded, should produce same results as VSOP87)
-- ============================================================
SELECT
round(helio_distance(lagrange_heliocentric_de(3, 1, :t ::timestamptz))::numeric, 4) AS de_l1_dist;
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 4) AS vsop_l1_dist;
-- DE hill_radius fallback
SELECT
round(hill_radius_de(5, :t ::timestamptz)::numeric, 4) =
round(hill_radius(5, :t ::timestamptz)::numeric, 4)
AS hill_de_matches_vsop;
-- ============================================================
-- Tighter L1/L2 precision (4 decimal places)
-- ============================================================
SELECT
round(helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))::numeric, 4) AS earth_l1_dist;
SELECT
round(helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))::numeric, 4) AS earth_l2_dist;
-- L1 and L2 bracket Earth's heliocentric distance
SELECT
helio_distance(lagrange_heliocentric(3, 1, :t ::timestamptz))
<
helio_distance(planet_heliocentric(3, :t ::timestamptz))
AND
helio_distance(planet_heliocentric(3, :t ::timestamptz))
<
helio_distance(lagrange_heliocentric(3, 2, :t ::timestamptz))
AS l1_earth_l2_ordering;
-- ============================================================
-- lagrange_distance_oe — Ceres distance from Jupiter L4
-- Ceres orbits at ~2.77 AU, Jupiter L4 at ~5.2 AU, so distance > 2 AU
-- ============================================================
SELECT
round(lagrange_distance_oe(
5, 4,
oe_from_mpc('00001 3.33 0.12 K24AM 60.07966 73.42937 80.26860 10.58664 0.0789126 0.21406048 2.7660961 0 MPO838504 8738 115 1801-2024 0.65 M-v 30k MPCLINUX 0000 (1) Ceres 20240825'),
:t ::timestamptz
)::numeric, 2) AS ceres_jup_l4_dist;
-- Distance should be positive and > 2 AU (main belt vs Trojan zone)
SELECT
lagrange_distance_oe(
5, 4,
oe_from_mpc('00001 3.33 0.12 K24AM 60.07966 73.42937 80.26860 10.58664 0.0789126 0.21406048 2.7660961 0 MPO838504 8738 115 1801-2024 0.65 M-v 30k MPCLINUX 0000 (1) Ceres 20240825'),
:t ::timestamptz
) > 2.0 AS ceres_far_from_trojan;
-- ============================================================
-- DE fallback for planetary moon Lagrange functions
-- ============================================================
SELECT
round(eq_ra(galilean_lagrange_equatorial_de(0, 4, :t ::timestamptz))::numeric, 4) =
round(eq_ra(galilean_lagrange_equatorial(0, 4, :t ::timestamptz))::numeric, 4)
AS galilean_de_fallback_matches;
SELECT
round(eq_ra(saturn_moon_lagrange_equatorial_de(5, 1, :t ::timestamptz))::numeric, 4) =
round(eq_ra(saturn_moon_lagrange_equatorial(5, 1, :t ::timestamptz))::numeric, 4)
AS saturn_de_fallback_matches;
-- ============================================================
-- Input validation
-- ============================================================
-- Bad body_id
SELECT lagrange_heliocentric(0, 1, :t ::timestamptz); -- Sun not valid
SELECT lagrange_heliocentric(9, 1, :t ::timestamptz); -- body 9 invalid
-- Bad point_id
SELECT lagrange_heliocentric(3, 0, :t ::timestamptz); -- point 0 invalid
SELECT lagrange_heliocentric(3, 6, :t ::timestamptz); -- point 6 invalid
-- Bad lunar point_id
SELECT lunar_lagrange_equatorial(0, :t ::timestamptz); -- point 0 invalid
SELECT lunar_lagrange_equatorial(6, :t ::timestamptz); -- point 6 invalid
-- Bad planetary moon body_id
SELECT galilean_lagrange_equatorial(4, 1, :t ::timestamptz); -- Galilean 4 invalid
SELECT saturn_moon_lagrange_equatorial(8, 1, :t ::timestamptz); -- Saturn 8 invalid
SELECT uranus_moon_lagrange_equatorial(5, 1, :t ::timestamptz); -- Uranus 5 invalid
SELECT mars_moon_lagrange_equatorial(2, 1, :t ::timestamptz); -- Mars 2 invalid
-- lagrange_mass_ratio bad body
SELECT lagrange_mass_ratio(0);
SELECT lagrange_mass_ratio(9);
-- lagrange_point_name bad id
SELECT lagrange_point_name(0);
SELECT lagrange_point_name(6);

View File

@ -1,459 +0,0 @@
/*
* test_lagrange.c -- Standalone unit test for the Lagrange solver
*
* Verifies quintic solutions, L4/L5 geometry, Hill radius,
* zone radius, and co-rotating to physical frame transform.
*
* No PostgreSQL dependency.
*
* Build: cc -Wall -Werror -Isrc -o test/test_lagrange \
* test/test_lagrange.c -lm
* Run: ./test/test_lagrange
*/
#include "lagrange.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* ── Test harness ───────────────────────────────────────── */
static int n_run, n_pass;
#define RUN(cond, msg) do { \
n_run++; \
if (!(cond)) \
fprintf(stderr, "FAIL: %s [line %d]\n", (msg), __LINE__); \
else { n_pass++; fprintf(stderr, " ok: %s\n", (msg)); } \
} while (0)
#define CLOSE(a, b, tol, msg) do { \
n_run++; \
double _a = (a), _b = (b); \
if (fabs(_a - _b) > (tol)) \
fprintf(stderr, "FAIL: %s: %.15g vs %.15g (diff %.3e) [line %d]\n", \
(msg), _a, _b, fabs(_a - _b), __LINE__); \
else { n_pass++; fprintf(stderr, " ok: %s\n", (msg)); } \
} while (0)
/* ── Tests ─────────────────────────────────────────────── */
/*
* Verify equilibrium: at a Lagrange point, the net force in the
* co-rotating frame should vanish. We check the effective potential
* gradient by evaluating the quintic polynomial.
*/
static void
test_equilibrium_check(double mu, int point_id, const char *label)
{
double x, y;
int rc;
char buf[128];
rc = lagrange_corotating(mu, point_id, &x, &y);
snprintf(buf, sizeof(buf), "%s: convergence", label);
RUN(rc == 0, buf);
if (rc != 0)
return;
if (point_id <= LAGRANGE_L3)
{
/*
* For collinear points, verify equilibrium directly.
* At equilibrium on the x-axis:
* x - (1-mu)*(x+mu)/|x+mu|^3 - mu*(x-1+mu)/|x-1+mu|^3 = 0
*/
double dx1 = x + mu; /* distance from primary (at -mu) */
double dx2 = x - 1.0 + mu; /* distance from secondary (at 1-mu) */
double r1 = fabs(dx1);
double r2 = fabs(dx2);
double residual;
residual = x - (1.0 - mu) * dx1 / (r1 * r1 * r1)
- mu * dx2 / (r2 * r2 * r2);
snprintf(buf, sizeof(buf), "%s: equilibrium residual", label);
CLOSE(residual, 0.0, 1e-12, buf);
}
else
{
/* L4/L5: equidistant from both primaries at unit distance */
double r1 = sqrt((x + mu) * (x + mu) + y * y);
double r2 = sqrt((x - 1.0 + mu) * (x - 1.0 + mu) + y * y);
snprintf(buf, sizeof(buf), "%s: distance to primary", label);
CLOSE(r1, 1.0, 1e-14, buf);
snprintf(buf, sizeof(buf), "%s: distance to secondary", label);
CLOSE(r2, 1.0, 1e-14, buf);
}
}
static void
test_sun_earth(void)
{
double mu = mu_from_ratio(SUN_EARTH_RATIO);
double x, y;
int rc;
fprintf(stderr, "\n── Sun-Earth system (mu = %.6e) ──\n", mu);
/* L1: between Sun and Earth, ~0.01 AU from Earth */
rc = lagrange_corotating(mu, LAGRANGE_L1, &x, &y);
RUN(rc == 0, "L1 converges");
/* L1 should be between barycenter and secondary */
RUN(x > -mu && x < 1.0 - mu, "L1 between primaries");
/* Distance from secondary (Earth at 1-mu) */
{
double d_from_earth = (1.0 - mu) - x;
CLOSE(d_from_earth, 0.01, 0.002, "L1 ~0.01 AU from Earth");
}
/* L2: beyond Earth, also ~0.01 AU */
rc = lagrange_corotating(mu, LAGRANGE_L2, &x, &y);
RUN(rc == 0, "L2 converges");
{
double d_from_earth = x - (1.0 - mu);
CLOSE(d_from_earth, 0.01, 0.002, "L2 ~0.01 AU from Earth");
}
/* L3: opposite side from Earth */
rc = lagrange_corotating(mu, LAGRANGE_L3, &x, &y);
RUN(rc == 0, "L3 converges");
RUN(x < -mu, "L3 beyond primary (opposite side)");
test_equilibrium_check(mu, LAGRANGE_L1, "Sun-Earth L1");
test_equilibrium_check(mu, LAGRANGE_L2, "Sun-Earth L2");
test_equilibrium_check(mu, LAGRANGE_L3, "Sun-Earth L3");
test_equilibrium_check(mu, LAGRANGE_L4, "Sun-Earth L4");
test_equilibrium_check(mu, LAGRANGE_L5, "Sun-Earth L5");
}
static void
test_sun_jupiter(void)
{
double mu = mu_from_ratio(SUN_JUPITER_RATIO);
double x, y;
int rc;
fprintf(stderr, "\n── Sun-Jupiter system (mu = %.6e) ──\n", mu);
/* L4/L5: should be at 60 degrees from Jupiter */
rc = lagrange_corotating(mu, LAGRANGE_L4, &x, &y);
RUN(rc == 0, "L4 converges");
{
/* Angle from secondary: atan2(y, x - (1-mu)) */
double angle = atan2(y, x - (1.0 - mu));
double angle_deg = angle * 180.0 / M_PI;
/* L4 leads secondary by ~60 degrees (but angle from barycenter) */
/* Actually check equilateral property */
double d_prim = sqrt((x + mu) * (x + mu) + y * y);
double d_sec = sqrt((x - 1.0 + mu) * (x - 1.0 + mu) + y * y);
CLOSE(d_prim, 1.0, 1e-14, "L4 unit distance from primary");
CLOSE(d_sec, 1.0, 1e-14, "L4 unit distance from secondary");
RUN(y > 0.0, "L4 above x-axis (leading)");
(void)angle_deg; /* used implicitly via assertions */
}
test_equilibrium_check(mu, LAGRANGE_L1, "Sun-Jupiter L1");
test_equilibrium_check(mu, LAGRANGE_L2, "Sun-Jupiter L2");
test_equilibrium_check(mu, LAGRANGE_L3, "Sun-Jupiter L3");
test_equilibrium_check(mu, LAGRANGE_L4, "Sun-Jupiter L4");
test_equilibrium_check(mu, LAGRANGE_L5, "Sun-Jupiter L5");
}
static void
test_earth_moon(void)
{
double mu = mu_from_ratio(EARTH_MOON_EMRAT);
fprintf(stderr, "\n── Earth-Moon system (mu = %.6e) ──\n", mu);
test_equilibrium_check(mu, LAGRANGE_L1, "Earth-Moon L1");
test_equilibrium_check(mu, LAGRANGE_L2, "Earth-Moon L2");
test_equilibrium_check(mu, LAGRANGE_L3, "Earth-Moon L3");
test_equilibrium_check(mu, LAGRANGE_L4, "Earth-Moon L4");
test_equilibrium_check(mu, LAGRANGE_L5, "Earth-Moon L5");
/* Earth-Moon L1 should be ~326,000 km from Earth (~84.7% of separation) */
{
double x, y;
int rc;
double earth_moon_km = 384400.0; /* mean distance */
rc = lagrange_corotating(mu, LAGRANGE_L1, &x, &y);
RUN(rc == 0, "Earth-Moon L1 converges");
/* In co-rotating frame, Earth is at -mu, Moon at 1-mu.
* L1 is between them. Distance from Earth = x + mu. */
{
double frac = (x + mu); /* fraction of separation from Earth */
double km_from_earth = frac * earth_moon_km;
CLOSE(km_from_earth, 326000.0, 5000.0,
"E-M L1 ~326,000 km from Earth");
}
}
}
static void
test_l4_l5_symmetry(void)
{
double mu = mu_from_ratio(SUN_JUPITER_RATIO);
double x4, y4, x5, y5;
int rc;
fprintf(stderr, "\n── L4/L5 symmetry ──\n");
rc = lagrange_corotating(mu, LAGRANGE_L4, &x4, &y4);
RUN(rc == 0, "L4 converges");
rc = lagrange_corotating(mu, LAGRANGE_L5, &x5, &y5);
RUN(rc == 0, "L5 converges");
CLOSE(x4, x5, 1e-15, "L4 and L5 same x-coordinate");
CLOSE(y4, -y5, 1e-15, "L4 and L5 mirror in y");
}
static void
test_l1_l2_ordering(void)
{
double mu = mu_from_ratio(SUN_EARTH_RATIO);
double x1, y1, x2, y2, x3, y3;
int rc;
fprintf(stderr, "\n── L1/L2/L3 ordering ──\n");
rc = lagrange_corotating(mu, LAGRANGE_L1, &x1, &y1);
RUN(rc == 0, "L1 converges");
rc = lagrange_corotating(mu, LAGRANGE_L2, &x2, &y2);
RUN(rc == 0, "L2 converges");
rc = lagrange_corotating(mu, LAGRANGE_L3, &x3, &y3);
RUN(rc == 0, "L3 converges");
/* Ordering: L3 < primary < L1 < secondary < L2 */
RUN(x3 < -mu, "L3 < primary");
RUN(x1 > -mu && x1 < 1.0 - mu, "L1 between primaries");
RUN(x2 > 1.0 - mu, "L2 beyond secondary");
}
static void
test_hill_radius(void)
{
double mu_jup, mu_earth;
double hill_jup, hill_earth;
fprintf(stderr, "\n── Hill radius ──\n");
mu_jup = mu_from_ratio(SUN_JUPITER_RATIO);
mu_earth = mu_from_ratio(SUN_EARTH_RATIO);
/* Jupiter at ~5.2 AU */
hill_jup = lagrange_hill_radius(5.2, mu_jup);
CLOSE(hill_jup, 0.355, 0.02, "Jupiter Hill radius ~0.35 AU");
/* Earth at ~1.0 AU */
hill_earth = lagrange_hill_radius(1.0, mu_earth);
CLOSE(hill_earth, 0.01, 0.002, "Earth Hill radius ~0.01 AU");
}
static void
test_zone_radius(void)
{
double mu = mu_from_ratio(SUN_JUPITER_RATIO);
double zr;
fprintf(stderr, "\n── Zone radius ──\n");
zr = lagrange_zone_radius(5.2, mu, LAGRANGE_L1);
RUN(zr > 0.0, "L1 zone radius positive");
zr = lagrange_zone_radius(5.2, mu, LAGRANGE_L4);
RUN(zr > 0.0, "L4 zone radius positive");
zr = lagrange_zone_radius(5.2, mu, 99);
RUN(zr < 0.0, "invalid point_id returns -1");
}
static void
test_physical_transform(void)
{
double primary[3] = {0.0, 0.0, 0.0}; /* Sun at origin */
double secondary[3] = {1.0, 0.0, 0.0}; /* "planet" at 1 AU on x-axis */
double sec_vel[3] = {0.0, 0.01720209895, 0.0}; /* ~Gauss constant, circular */
double mu = 0.001; /* ~Jupiter-like */
double result[3];
int rc;
fprintf(stderr, "\n── Physical frame transform ──\n");
/* L1: should be between Sun and planet, on x-axis */
rc = lagrange_position(primary, secondary, sec_vel, mu, LAGRANGE_L1, result);
RUN(rc == 0, "L1 transform succeeds");
RUN(result[0] > 0.0 && result[0] < 1.0, "L1 between Sun and planet on x-axis");
CLOSE(result[1], 0.0, 1e-10, "L1 y-component ~0");
CLOSE(result[2], 0.0, 1e-10, "L1 z-component ~0");
/* L2: beyond planet */
rc = lagrange_position(primary, secondary, sec_vel, mu, LAGRANGE_L2, result);
RUN(rc == 0, "L2 transform succeeds");
RUN(result[0] > 1.0, "L2 beyond planet");
CLOSE(result[1], 0.0, 1e-10, "L2 y-component ~0");
/* L4: 60 degrees ahead, above x-axis in ecliptic plane */
rc = lagrange_position(primary, secondary, sec_vel, mu, LAGRANGE_L4, result);
RUN(rc == 0, "L4 transform succeeds");
{
double dist = sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]);
/* L4 should be ~1 AU from Sun (equilateral triangle) */
CLOSE(dist, 1.0, 0.01, "L4 ~1 AU from Sun");
RUN(result[1] > 0.0, "L4 positive y (leading)");
}
/* L5: symmetric with L4 */
rc = lagrange_position(primary, secondary, sec_vel, mu, LAGRANGE_L5, result);
RUN(rc == 0, "L5 transform succeeds");
RUN(result[1] < 0.0, "L5 negative y (trailing)");
}
static void
test_extreme_mass_ratios(void)
{
double x, y;
int rc;
fprintf(stderr, "\n── Extreme mass ratios ──\n");
/* Very small mu (like Mercury around the Sun) */
{
double mu = mu_from_ratio(SUN_MERCURY_RATIO); /* ~1.66e-7 */
rc = lagrange_corotating(mu, LAGRANGE_L1, &x, &y);
RUN(rc == 0, "tiny mu L1 converges");
test_equilibrium_check(mu, LAGRANGE_L1, "Mercury L1");
}
/* Moderately large mu */
{
double mu = 0.1;
rc = lagrange_corotating(mu, LAGRANGE_L1, &x, &y);
RUN(rc == 0, "mu=0.1 L1 converges");
test_equilibrium_check(mu, LAGRANGE_L1, "mu=0.1 L1");
test_equilibrium_check(mu, LAGRANGE_L2, "mu=0.1 L2");
test_equilibrium_check(mu, LAGRANGE_L3, "mu=0.1 L3");
}
/* Equal mass (mu = 0.5, maximum) */
{
double mu = 0.5;
rc = lagrange_corotating(mu, LAGRANGE_L1, &x, &y);
RUN(rc == 0, "mu=0.5 L1 converges");
test_equilibrium_check(mu, LAGRANGE_L1, "mu=0.5 L1");
test_equilibrium_check(mu, LAGRANGE_L2, "mu=0.5 L2");
test_equilibrium_check(mu, LAGRANGE_L3, "mu=0.5 L3");
/* L4/L5 at (0, +-sqrt(3)/2) for equal mass */
rc = lagrange_corotating(mu, LAGRANGE_L4, &x, &y);
RUN(rc == 0, "mu=0.5 L4 converges");
CLOSE(x, 0.0, 1e-15, "mu=0.5 L4 x=0");
CLOSE(y, sqrt(3.0)/2.0, 1e-15, "mu=0.5 L4 y=sqrt(3)/2");
}
}
static void
test_error_cases(void)
{
double x, y;
int rc;
fprintf(stderr, "\n── Error cases ──\n");
rc = lagrange_corotating(0.0, LAGRANGE_L1, &x, &y);
RUN(rc != 0, "mu=0 rejected");
rc = lagrange_corotating(-0.1, LAGRANGE_L1, &x, &y);
RUN(rc != 0, "negative mu rejected");
rc = lagrange_corotating(0.6, LAGRANGE_L1, &x, &y);
RUN(rc != 0, "mu>0.5 rejected");
rc = lagrange_corotating(0.01, 0, &x, &y);
RUN(rc != 0, "point_id=0 rejected");
rc = lagrange_corotating(0.01, 6, &x, &y);
RUN(rc != 0, "point_id=6 rejected");
/* Mass ratio lookups */
RUN(sun_planet_ratio(1) > 0.0, "Mercury ratio valid");
RUN(sun_planet_ratio(8) > 0.0, "Neptune ratio valid");
RUN(sun_planet_ratio(0) < 0.0, "Sun ratio invalid");
RUN(sun_planet_ratio(9) < 0.0, "body 9 invalid");
RUN(planet_moon_ratio('g', 0) > 0.0, "Io ratio valid");
RUN(planet_moon_ratio('g', 4) < 0.0, "Galilean moon 4 invalid");
RUN(planet_moon_ratio('s', 7) > 0.0, "Hyperion ratio valid");
RUN(planet_moon_ratio('s', 8) < 0.0, "Saturn moon 8 invalid");
RUN(planet_moon_ratio('u', 4) > 0.0, "Oberon ratio valid");
RUN(planet_moon_ratio('u', 5) < 0.0, "Uranus moon 5 invalid");
RUN(planet_moon_ratio('m', 1) > 0.0, "Deimos ratio valid");
RUN(planet_moon_ratio('m', 2) < 0.0, "Mars moon 2 invalid");
RUN(planet_moon_ratio('x', 0) < 0.0, "unknown family invalid");
}
static void
test_all_planets(void)
{
int body;
fprintf(stderr, "\n── All planets equilibrium ──\n");
for (body = 1; body <= 8; body++)
{
double ratio = sun_planet_ratio(body);
double mu = mu_from_ratio(ratio);
char label[64];
int pt;
for (pt = LAGRANGE_L1; pt <= LAGRANGE_L5; pt++)
{
snprintf(label, sizeof(label), "body %d L%d", body, pt);
test_equilibrium_check(mu, pt, label);
}
}
}
/* ── Main ──────────────────────────────────────────────── */
int
main(void)
{
fprintf(stderr, "Lagrange solver unit test\n");
fprintf(stderr, "========================\n");
test_sun_earth();
test_sun_jupiter();
test_earth_moon();
test_l4_l5_symmetry();
test_l1_l2_ordering();
test_hill_radius();
test_zone_radius();
test_physical_transform();
test_extreme_mass_ratios();
test_error_cases();
test_all_planets();
fprintf(stderr, "\n%d/%d tests passed\n", n_pass, n_run);
return (n_pass == n_run) ? 0 : 1;
}