"""Checks every number in the answers and solutions of 01-one-physics-questions.qmd.
Run from anywhere:  python3 codes/01-one-physics-questions-verify.py"""
import os, sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import _paths  # noqa
import numpy as np
from _verify import check, truth, Objective, run
from flight import atmosphere
from flight.constants import G0, A_EARTH as R, MU, KT

PAGE = os.path.join(HERE, "..", "01-one-physics-questions.qmd")
gR = G0 * R
a0, a11, a30 = (atmosphere.ISA.state(h)[3] for h in (0.0, 11e3, 30e3))


def data():
    """The data box: speeds of sound and the knot."""
    check("a at sea level (m/s)", a0, 340.3)
    check("a at 11 km (m/s)", a11, 295.2)
    check("a at 30 km (m/s)", a30, 301.7)
    check("knot (m/s)", KT, 0.5144)


def objective():
    """The 12 objective questions: form, answer key, and the numbers in the marked options."""
    q = Objective(PAGE, 12)
    num = q.num
    vh = 96 * KT
    num(5, vh**2 / gR, r"3.9\times10^{-5}", x=3.9e-5, tol=5e-7)
    num(6, np.sqrt(gR) / 1e3, "7.91")
    check("Q6 escape speed sqrt(2) v_c (km/s)", np.sqrt(2) * np.sqrt(MU / R) / 1e3, 11.2)
    check("Q6 geostationary speed (km/s)", np.sqrt(MU / 42164e3) / 1e3, 3.07)
    num(9, np.sqrt(0.01 * gR), "791")
    num(10, (590 / 250) ** 2, "5.57")
    check("Q10 distractor 590/250", 590 / 250, 2.36)
    vs = 6 * a30
    check("Q11 HSTDV speed (m/s)", vs, 1810, tol=1)
    check("Q11 ratio", vs**2 / gR, 0.052)
    num(11, vs**2 / gR * 100, "5", tol=0.5)
    vi = np.sqrt(MU / (R + 400e3))
    check("Q12 circular speed at 400 km (m/s)", vi, 7668.6)
    check("Q12 ratio with surface values", vi**2 / gR, 0.94)
    check("Q12 ratio with local values", vi**2 * (R + 400e3) / MU, 1.000)
    check("Q5 Hansa-3 speed (m/s)", vh, 49.4)


def five_vehicles():
    """Drill 1: five Indian vehicles."""
    check("gR (m^2/s^2)", gR / 1e7, 6.2548)
    rows = dict(hansa=96 * KT, a320=0.78 * a11, brahmos=2.8 * a0, hstdv=6 * a30, carto=np.sqrt(MU / (R + 509e3)))
    for k, (v, q) in dict(hansa=(49.4, "3.9e-5"), a320=(230.2, "8.47e-4"), brahmos=(952.8, 0.0145), hstdv=(1810, 0.0524),
                          carto=(7608, 0.925)).items():
        check(f"P1 {k} speed (m/s)", rows[k], v, tol=0.5 if v > 1000 else None)
        check(f"P1 {k} ratio", rows[k] ** 2 / gR, float(q), rel=6e-3 if isinstance(q, str) else None)
    check("P1 Cartosat radius (km)", (R + 509e3) / 1e3, 6887.137)
    for k, f in dict(hansa=0.99996, a320=0.99915, brahmos=0.985, hstdv=0.948).items():
        check(f"P1 {k} lift fraction", 1 - rows[k] ** 2 / gR, f)
    truth("P1 flat Earth within 1 %: Hansa-3 and A320 only",
          [k for k in rows if rows[k] ** 2 / gR < 0.01] == ["hansa", "a320"])
    check("P1 BrahMos error (%)", rows["brahmos"] ** 2 / gR * 100, 1.5, tol=0.05)


def lift_saved():
    """Drill 2: the lift an airliner saves."""
    m, V, r = 70e3, 230.2, R + 11e3
    F = m * V**2 / r
    check("P2 r (m)", r, 6389137)
    check("P2 m V^2/r (N)", F, 581)
    check("P2 weight (N)", m * G0, 686466)
    check("P2 percentage", F / (m * G0) * 100, 0.085)
    check("P2 fuel burn per minute (kg)", 2500 / 60, 41.7)
    check("P2 weight burnt per minute (N)", 2500 / 60 * G0, 409)
    check("P2 minutes", F / (2500 / 60 * G0), 1.4)


def circular_line():
    """Extension 3: the circular-orbit line."""
    import sympy as sp
    V, r, mu = sp.symbols("V r mu", positive=True)
    truth("P3 V^2/(g r) with g = mu/r^2 equals V^2 r/mu", sp.simplify(V**2 / ((mu / r**2) * r) - V**2 * r / mu) == 0)
    check("P3 v_c surface (m/s)", np.sqrt(MU / R), 7905)
    check("P3 v_c 400 km (m/s)", np.sqrt(MU / (R + 400e3)), 7669)
    check("P3 v_c GEO (m/s)", np.sqrt(MU / 42164e3), 3075)
    check("P3 mu/R^2", MU / R**2, 9.798)
    check("P3 speed difference (m/s)", np.sqrt(gR) - np.sqrt(MU / R), 3, tol=0.5)
    check("P3 difference (%)", (np.sqrt(gR) / np.sqrt(MU / R) - 1) * 100, 0.04)
    from flight import gravity, frames
    g45 = np.linalg.norm(gravity.effective(frames.ecef_from_geodetic(np.radians(45), 0, 0)))
    check("P3 measured-like (effective, J2) gravity at 45 deg is close to g0", g45, G0, tol=0.002)
    rr = MU / 7000**2
    check("P3 r for 7 km/s (km)", rr / 1e3, 8135)
    check("P3 altitude (km)", (rr - R) / 1e3, 1757)


if __name__ == "__main__":
    run([data, objective, five_vehicles, lift_saved, circular_line])
