"""ADI EM Geometry — reference construction (Python).

Run:  python3 construction.py
Exits 0 on success, 1 on tolerance violation.
"""
import math, sys

R = 1.0  # any positive R works; ratios are scale-invariant
DEG = math.pi / 180

r_e  = R * math.cos(22.5 * DEG)
r_em = R * math.cos(22.5 * DEG) / math.cos(11.25 * DEG)
r_m  = R

print(f"r_e  = {r_e:.12f}")
print(f"r_em = {r_em:.12f}")
print(f"r_m  = {r_m:.12f}")

# Tolerances (see tolerances.json)
checks = [
    ("r_e/R",            r_e / R,             0.923879532, 1e-9),
    ("r_em/R",           r_em / R,            0.941979403, 1e-9),
    ("r_em/r_e",         r_em / r_e,          1.019591158, 1e-9),
    ("R_earth_sun",      math.sqrt(24*24 + 365*365), 365.7881901, 1e-6),
    ("cF",               1.852,               1.852,       1e-4),
]
fail = 0
for name, got, expected, tol in checks:
    ok = abs(got - expected) <= tol
    print(f"  {name:14s} got={got:.10f}  expected={expected}  tol={tol}  -> {'OK' if ok else 'FAIL'}")
    if not ok:
        fail += 1
sys.exit(0 if fail == 0 else 1)
