"""Checks for the Lecture 31 question page."""
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, sym_zero, Objective, run
import _sounding as S
from flight import rocket as R
from flight.constants import R_EARTH, MU, G0

PAGE = os.path.join(HERE, "..", "31-suborbital-questions.qmd")


def objective():
    q = Objective(PAGE, 12)
    q.num(5, S.uniform_g_apogee(1510.0) / 1e3, "156", x=156)
    ratio = S.time_above(1e5, 470e3) / S.time_above(1e5, 160e3)
    q.num(11, ratio, "2.6", x=2.6)
    truth("Q12: an 80 km flight spends no time above 100 km", S.time_above(1e5, 80e3) == 0.0)
    truth("Q8: quadruples", "$4$" == q.right(8))
    truth("Q7: half the apogee radius", "r_a/2" in q.right(7))


def drills():
    rb, ra = R_EARTH + 50e3, R_EARTH + 250e3
    v = float(R.vertical_speed_for_apogee(rb, ra, MU))
    check("D1 speed", v, 1934, tol=0.5)
    check("D1 constant g (km)", 50 + v**2 / (2 * G0) / 1e3, 240.8, tol=0.05)
    check("D1 a (km)", ra / 2e3, 3314, tol=0.5)
    check("D2 time above 100 km", 2 * R.vertical_time_to_apogee(R_EARTH + 1e5, ra, MU), 362, tol=0.5)
    check("E3 constant-g time", 2 * np.sqrt(2 * 370e3 / 8.50), 590, tol=0.5)
    check("E3 exact", S.time_above(1e5, 470e3), 585, tol=0.5)


def derivations():
    import sympy as sp
    mu, a, eta = sp.symbols("mu a eta", positive=True)
    r, ra = a * (1 - sp.cos(eta)), 2 * a
    sym_zero("G4(b)", sp.simplify(2 * mu * (ra - r) / (r * ra) - mu / a * (1 + sp.cos(eta)) / (1 - sp.cos(eta))))
    dtde = sp.sqrt(a**3 / mu) * (1 - sp.cos(eta))
    sym_zero("G4(c)", sp.simplify((sp.diff(r, eta) / dtde)**2 - mu / a * (1 + sp.cos(eta)) / (1 - sp.cos(eta))))
    d0, rbs = sp.symbols("d0 r_b", positive=True)
    d = d0 / (1 - d0 / rbs)
    sym_zero("G5(b)", sp.simplify(d / (1 + d / rbs) - d0))
    check("G5(c) approximate", 400 * (1 + 400 / 6418), 425, tol=0.5)
    check("G5(c) exact", 400 / (1 - 400 / 6418), 426.6, tol=0.05)


def derivations_more():
    import sympy as sp
    from flight import entry as E
    D, g = sp.symbols("D g", positive=True)
    sym_zero("G6(a) upper quarter takes half as long", sp.sqrt(2 * D / (4 * g)) - sp.sqrt(2 * D / g) / 2)
    ra = R_EARTH + 470e3
    share = R.vertical_time_to_apogee(R_EARTH + 362.5e3, ra, MU) / R.vertical_time_to_apogee(R_EARTH + 40e3, ra, MU)
    check("G6(c) share", share, 0.504, tol=5e-4)
    rho, B, H, Vi = sp.symbols("rho B H V_i", positive=True)
    a = Vi**2 / (2 * B) * rho * sp.exp(-rho * H / B)
    sym_zero("G7(b) stationary at B/H", sp.diff(a, rho).subs(rho, B / H))
    sym_zero("G7(c) value", sp.simplify(a.subs(rho, B / H) - Vi**2 / (2 * sp.E * H)))
    check("G7 equals the engine", E.ballistic_peak_deceleration(2500.0, np.pi / 2, 7000.0), 2500.0**2 / (2 * np.e * 7000.0), tol=1e-9)
    check("G7 engine peak density", E.ballistic_peak_density(400.0, np.pi / 2, 7000.0), 400.0 / 7000.0, tol=1e-12)


if __name__ == "__main__":
    run([objective, drills, derivations, derivations_more])
