"""The Homecoming: a crew that waits beside a spinning black hole and lands on Earth the day a
message from Andromeda arrives.

Copyright (c) 2026 Hugo Flayac. All rights reserved. You may download and run this script,
modified or not, for the sole purpose of checking the result. Do not redistribute it.

Every number of the page "The Homecoming" is printed here, from closed forms:

1. A rotating (Kerr) black hole, units G = M = c = 1, spin a: horizon r+ = 1 + sqrt(1 - a^2);
   innermost stable prograde circular orbit (Bardeen, Press and Teukolsky 1972); on a
   prograde circular orbit in the equatorial plane, Omega = 1 / (r^(3/2) + a) and the
   clock rate against a distant clock
       dtau/dt = sqrt(1 - 3/r + 2a r^(-3/2)) / (1 + a r^(-3/2)),
   checked here against the metric itself (Boyer-Lindquist, equator).
2. The outer legs, Earth to the black hole and back, in special relativity: constant proper
   acceleration of 1 g up to a speed cap, a coast, the same braking (Baez, The Relativistic
   Rocket). A burn to rapidity eta takes sinh(eta)/g of distant time, eta/g aboard, and
   covers (cosh(eta) - 1)/g.
3. The timing: the message from Andromeda leaves at Vesper's arrival and travels the fixed
   2.5-million-light-year baseline at the speed of light; Peregrine leaves its orbit exactly
   one return leg before the message reaches Earth.

The mission values quoted for comparison come from the simulator of Waiting Time The Game
(research ledger, rows WG-83 and WG-84, read 2026-12-03), which also includes the transfer
between a staging point at 100 GM/c^2 and the orbit: a few hours, invisible at this scale.

Run:  python homecoming.py      (numpy only)
"""

import numpy as np

# Constants (SI) and the adopted black hole
G, C, M_SUN = 6.67430e-11, 299792458.0, 1.98847e30
YEAR = 365.25 * 86400.0                       # Julian year
LY = C * YEAR
MASS = 4.297e6 * M_SUN                        # Sagittarius A* (GRAVITY Collaboration 2022)
SPIN = 0.9                                    # illustrative: the spin of Sagittarius A* is not settled
R_ORBIT = 2.5                                 # the waiting orbit, in GM/c^2
T_UNIT = G * MASS / C**3                      # one GM/c^3 in seconds
L_UNIT = G * MASS / C**2                      # one GM/c^2 in metres

# Mission (Waiting Time The Game, rows WG-83 and WG-84, read 2026-12-03)
VESPER_ARRIVAL = 2_552_092.311969             # mission year of Vesper's arrival at Neria (Andromeda)
BASELINE_LY = 2.5e6                           # Earth to Neria, fixed
TO_SGR_LY = 26_000.0                          # Earth to Sagittarius A*
CAP, ACCEL = 0.9, 9.80665                     # speed cap (c), proper acceleration (m/s^2)
GAME_HOLD, GAME_ABOARD = 4_994_312.104, 2_080_947.542


def horizon(a):
    return 1 + np.sqrt(1 - a * a)


def isco_prograde(a):
    z1 = 1 + (1 - a * a) ** (1 / 3) * ((1 + a) ** (1 / 3) + (1 - a) ** (1 / 3))
    z2 = np.sqrt(3 * a * a + z1 * z1)
    return 3 + z2 - np.sqrt((3 - z1) * (3 + z1 + 2 * z2))


def clock_rate(r, a):
    return np.sqrt(1 - 3 / r + 2 * a * r**-1.5) / (1 + a * r**-1.5)


def clock_rate_from_metric(r, a):
    """The same rate from the Boyer-Lindquist metric on the equator: dtau/dt =
    sqrt(-(g_tt + 2 g_tphi Omega + g_phiphi Omega^2))."""
    omega = 1 / (r**1.5 + a)
    g_tt = -(1 - 2 / r)
    g_tphi = -2 * a / r
    g_pp = r * r + a * a + 2 * a * a / r
    return np.sqrt(-(g_tt + 2 * g_tphi * omega + g_pp * omega**2))


def leg(distance_ly, cap=CAP, accel=ACCEL):
    """Distant and aboard years for a leg at rest at both ends: 1 g up to the cap, coast,
    1 g down."""
    g = accel * YEAR**2 / LY                  # in light-years per year squared
    eta = np.arctanh(cap)
    burn_t, burn_tau, burn_x = np.sinh(eta) / g, eta / g, (np.cosh(eta) - 1) / g
    coast_x = distance_ly - 2 * burn_x
    coast_t = coast_x / cap
    return 2 * burn_t + coast_t, 2 * burn_tau + coast_t * np.sqrt(1 - cap * cap)


def main():
    r_plus, r_isco = horizon(SPIN), isco_prograde(SPIN)
    rate = clock_rate(R_ORBIT, SPIN)
    rate_metric = clock_rate_from_metric(R_ORBIT, SPIN)
    print(f"Sagittarius A*: GM/c^2 = {L_UNIT / 1e9:.3f} million km, GM/c^3 = {T_UNIT:.2f} s")
    print(f"horizon r+ = {r_plus:.5f} GM/c^2; last stable prograde orbit {r_isco:.4f} GM/c^2")
    print(f"waiting orbit r = {R_ORBIT} GM/c^2 = {R_ORBIT * L_UNIT / 1.495978707e11:.3f} AU from the centre")
    print(f"clock rate on the orbit: {rate:.6f} (closed form), {rate_metric:.6f} (from the metric)")
    assert abs(rate - rate_metric) < 1e-12 and R_ORBIT > r_isco
    period = 2 * np.pi * (R_ORBIT**1.5 + SPIN) * T_UNIT
    print(f"one turn: {period / 60:.2f} min on a distant clock, {period * rate / 60:.2f} min aboard")

    t_leg, tau_leg = leg(TO_SGR_LY)
    print(f"each outer leg ({TO_SGR_LY:,.0f} ly, 1 g, {CAP} c cap): {t_leg:,.1f} distant years, {tau_leg:,.1f} aboard")
    reception = VESPER_ARRIVAL + BASELINE_LY
    hold = reception - 2 * t_leg
    aboard = 2 * tau_leg + hold * rate
    print(f"Vesper arrives in mission year {VESPER_ARRIVAL:,.6f}; its message reaches Earth in year {reception:,.6f}")
    print(f"Peregrine waits in orbit {hold:,.1f} distant years (game: {GAME_HOLD:,.3f}) "
          f"and lives {aboard:,.1f} years aboard in all (game: {GAME_ABOARD:,.3f})")
    print(f"the crew comes home {reception - aboard:,.0f} years younger than Earth's clock, "
          f"a factor {reception / aboard:.3f}")
    assert abs(hold - GAME_HOLD) < 2 and abs(aboard - GAME_ABOARD) < 2


if __name__ == "__main__":
    main()
