"""Checks every number and identity in the answers and solutions of impulsive-model-questions.qmd
(The Impulsive Model: Questions).

Each function below checks one group of questions. The labels it prints use the problem and question
numbers of the page. Run from the sfm-slides directory, or from anywhere:

    python3 codes/impulsive-model-questions-verify.py
"""
from _verify import check, Objective, run


def objective():
    """The 6 objective questions: numbering, options and answer key, and every number in an answer, recomputed and found in the option the page marks as correct."""
    import numpy as np

    # data, as in the page's data box
    g0 = 9.80665

    num = Objective("impulsive-model-questions.qmd", 6).num

    num(2, 300 * g0 * np.log(2) / 1e3, "2.04")

    num(6, 100 * (1 - np.exp(-3000 / (450 * g0))), "49")


def rocket_equation():
    """Problem 1, the rocket equation for one and two stages."""
    import numpy as np

    g0 = 9.80665e-3

    # the exercise
    ve = 320*g0
    check("left", 100*np.exp(-5/ve), 20.3, 0.05); check("payload", 100*(np.exp(-5/ve) - 0.1), 10.3, 0.05)
    s = (np.exp(-2.5/ve) - 0.1)**2; check("two stages", 100*s, 12.3, 0.05); check("more", 100*(s/(np.exp(-5/ve) - 0.1) - 1), 19, 0.5)
    check("exercise solution: v_e", ve, 3.1381, 5e-5); check("exponent", 5/ve, 1.5933, 5e-5); check("m_f/m_0", np.exp(-5/ve), 0.2033, 5e-5)
    x2 = np.exp(-2.5/ve); check("x", x2, 0.4508, 5e-5); check("m_1/m_0", x2 - 0.1, 0.3508, 5e-5); check("payload fraction", s, 0.1231, 5e-5)
    check("ratio 1.19", s/(np.exp(-5/ve) - 0.1), 1.19, 5e-3)
    x3 = np.exp(-5/(3*ve)) - 0.1; check("three stages: base", x3, 0.4880, 5e-5); check("three stages: payload %", 100*x3**3, 11.6, 0.05)


def burn_errors():
    """Problems 2, 3, 4 and 5, burn errors, finite burns, Mangalyaan's injection, and a GEO budget."""
    import sys
    import numpy as np
    sys.path.insert(0, "codes")
    from _mqburn import burn, MU, RE                                   # noqa: E402
    from _mqcommon import eta_exact, half_arc                          # noqa: E402

    from orbit.constants import G0

    vis = lambda r, a: np.sqrt(MU * (2 / r - 1 / a))

    ve = 310 * G0 * 1e-3     # as set in the Chandrayaan-3 section of the old script

    # ======================================================================= P2
    print("P2. first-order burn errors")
    r1, r2 = 6678.137, 42164.0
    at = (r1 + r2) / 2; vt1 = vis(r1, at); vc = np.sqrt(MU / r1); dv = vt1 - vc; et = (r2 - r1) / (r2 + r1)
    check("Hohmann burn 1 [km/s]", dv, 2.42573, 5e-6)
    check("delta r_a per m/s [km]", 4 * at ** 2 * vt1 / MU * 1e-3, 60.8, 0.05)
    check("  for 1% [km]", 4 * at ** 2 * vt1 / MU * 0.01 * dv, 1474, 1)
    check("pointing 1 deg: delta a [km]", -at ** 2 / MU * vc * dv * np.radians(1) ** 2, -8.5, 0.05)
    gain = dv / vt1 * (1 + et) / et
    check("delta omega / alpha", -gain, -0.568, 5e-4)
    check("arrival shift for 1 deg [km]", gain * np.radians(1) * r2, 418, 1)
    n1 = np.sqrt(MU / r1 ** 3)
    check("timing: arrival shift per second [km]", n1 * r2, 48.8, 0.05)
    check("  inertial hold [km per s]", n1 * r2 * (1 - gain), 21, 0.5)
    check("P2(d) radial [km]", 0.002 * dv * 1e3 * 4 * at ** 2 * vt1 / MU * 1e-3, 295, 1)
    check("P2(d) along-track RSS [km]", np.hypot(0.3 * gain * np.radians(1) * r2, n1 * r2), 134, 1)

    # ======================================================================= P3
    print("P3. finite burns (exact average of the coasting speed)")
    rp, e = 7000.0, 0.5; ra = rp * (1 + e) / (1 - e)
    check("eta formula, e = 0.5, theta = 30 deg", 1 - e * np.radians(30) ** 2 / (6 * (1 + e) ** 2), 0.9898, 5e-5)
    check("eta exact for t_b = 818 s", eta_exact(rp, ra, 818), 0.9896, 5e-5)
    check("half-arc for 818 s [deg]", np.degrees(half_arc(rp, ra, 818)), 30.0, 0.05)
    check("half-arc for 1803 s [deg]", np.degrees(half_arc(rp, ra, 1803)), 60.0, 0.1)
    for tb, k, stated in ((1803, 1, 4.39), (902, 2, 1.25), (601, 3, 0.57)):
        check(f"loss for {k} burn(s) of {tb} s [%]", 100 * (1 - eta_exact(rp, ra, tb)), stated, 0.006)

    # ======================================================================= P4
    print("P4. Mangalyaan")
    rpm, ram = RE + 252, RE + 192874; am = (rpm + ram) / 2; em = (ram - rpm) / (ram + rpm); vpm = vis(rpm, am)
    check("e", em, 0.93559, 5e-6)
    check("v_p [km/s]", vpm, 10.7873, 5e-5)
    check("half arc of the 1328.89 s burn [deg]", np.degrees(half_arc(rpm, ram, 1328.89)), 53.3, 0.05)
    check("eta, exact average", eta_exact(rpm, ram, 1328.89), 0.960, 5e-4)
    mdot = 440 / (310 * G0)
    check("propellant [kg]", mdot * 1328.89, 192, 0.5)
    m0 = mdot * 1328.89 / (1 - np.exp(-0.648 / ve))
    check("initial mass [kg]", m0, 1002, 0.5)
    R = burn(am, em, m0, 440, 310, 1328.89, tb=1328.89)
    vinf = np.sqrt(2 * R["eps"])
    check("integrated burn: dv [m/s]", R["dv"] * 1e3, 648, 0.5)
    check("  v_inf [km/s]", vinf, 3.14, 0.005)
    check("  C3 [km2/s2]", vinf ** 2, 9.9, 0.05)
    vimp = np.sqrt(2 * MU / rpm + vinf ** 2) - vpm
    check("  waste against an impulsive burn [m/s]", (R["dv"] - vimp) * 1e3, 28, 0.5)
    # speed and thrust acceleration at cutoff, for (d)
    m1 = m0 - mdot * 1328.89
    check("speed at cutoff [km/s]", R["vend"], 10.1, 0.05)
    check("one second late: delta v_inf [m/s]", R["vend"] / vinf * 440 / m1, 1.8, 0.05)
    check("thrust acceleration at cutoff [m/s2]", 440 / m1, 0.54, 0.005)

    # ======================================================================= P5
    print("P5. GEO budget")
    vech = 320 * G0
    check("chemical propellant [kg]", 3000 * (1 - np.exp(-806 / vech)), 680, 0.5)
    check("electric propellant [kg]", 3000 * (1 - np.exp(-806 / (1800 * G0))), 134, 0.5)
    check("gauge uncertainty [m/s]", vech * 0.03 * 680 / 2320, 27.6, 0.05)
    check("life at 55 m/s/yr [yr]", (806 - 11) / 58, 13.7, 0.05)
    check("3-sigma propellant [kg]", 3000 * (1 - np.exp(-941 / vech)), 777, 0.5)
    check("life with 2% low Isp [yr]", (0.98 * vech * np.log(3000 / 2320) - 11) / 53, 14.7, 0.05)


BLOCKS = [objective, rocket_equation, burn_errors]


if __name__ == "__main__":
    run(BLOCKS)
