"""Lecture 33: the first-order (Allen-Eggers) peak deceleration of ballistic entry over entry angle and speed, with 8 g
and 12 g lines. Right: for SRE-1 at 8 km/s, the formula's peak against the simulator's (_entry33), and the same
for the stagnation heat load (nose 0.5 m, formula taken down to 10 km). Writes img/fig-33-corridor.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
import _entry33 as C
from flight import entry as E
from flight.constants import G0, RHO0_SL

V = C.capsule("sre-1")
B, RN = C.B_of(V), V.info["vehicle"].get("nose_radius", 0.5)
RHO_END = RHO0_SL * np.exp(-C.H_STOP / C.H)
ANG = np.linspace(1.0, 20.0, 191)
SPD = np.linspace(4000.0, 8000.0, 161)


def peak_g(speed, ang_deg):
    return E.ballistic_peak_deceleration(speed, np.radians(ang_deg), C.H) / G0


def heat(speed, ang_deg):
    return E.ballistic_heat_load(speed, B, np.radians(ang_deg), C.H, RN, RHO_END) / 1e6


Q_REF = heat(8000.0, 3.0)
SIM_ANG = [2.0, 3.0, 5.0, 8.0, 12.0, 20.0]

SIM = [C.peaks(C.sre(-g)) for g in SIM_ANG]

if __name__ == "__main__":
    fig = plt.figure(figsize=(10.5, 5.6))
    a = fig.add_axes([0.07, 0.11, 0.49, 0.85])
    b = fig.add_axes([0.66, 0.58, 0.32, 0.38]); c = fig.add_axes([0.66, 0.11, 0.32, 0.38])
    X, Y = np.meshgrid(ANG, SPD / 1000)
    G = peak_g(Y * 1000, X)
    a.contourf(X, Y, G, levels=[0, 8, 12], colors=["#d8e6d2", "#f3e3c3"])
    cs = a.contour(X, Y, G, levels=[8, 12, 20, 30], colors=[MAROON], linewidths=[1.6, 2.8, 1.2, 1.2])
    a.clabel(cs, fmt=lambda v: f"{v:.0f} g", fontsize=11, manual=[(3.9, 6.7), (6.6, 6.7), (11.5, 6.4), (16.5, 6.2)])
    a.axhline(8.0, color=INK, lw=1.2)
    a.text(19.6, 7.9, "SRE-1: 8 km/s, angle not published", ha="right", va="top", fontsize=10.5)
    care = C.CARE_V / 1000
    sim_care = -C.care_angle()
    formula_care = float(np.degrees(np.arcsin(C.CARE_G * G0 * 2 * np.e * C.H / C.CARE_V**2)))
    a.plot(sim_care, care, "o", color=INK, ms=8); a.plot(formula_care, care, "s", color=INK, ms=7, mfc="none")
    a.text(sim_care - 0.3, care - 0.12, "CARE,\nsimulator", ha="right", va="top", fontsize=10)
    a.text(formula_care + 0.35, care + 0.05, "CARE, formula", ha="left", fontsize=10)
    a.set_xlabel("entry angle below the horizontal (deg)"); a.set_ylabel("entry speed (km/s)"); a.set_xlim(1, 20); a.set_ylim(4, 8.25)
    aa = np.linspace(1.5, 20, 100)
    b.plot(aa, peak_g(8000.0, aa), color=MAROON, lw=2.4, label="formula")
    b.plot(SIM_ANG, [x["a"] for x in SIM], "o", color=INK, ms=6, label="simulator")
    b.axhline(12, color=GREY, lw=1, ls="--")
    b.set_ylabel("peak (g)"); b.set_xlim(0, 20.5); b.set_ylim(0, 64); b.set_xticklabels([])
    b.legend(frameon=False, fontsize=10.5, loc="upper left")
    c.plot(aa, [heat(8000.0, g) for g in aa], color=BLUE, lw=2.4)
    c.plot(SIM_ANG, [x["Q"] / 1e6 for x in SIM], "o", color=INK, ms=6)
    c.set_ylabel("heat (MJ/m²)"); c.set_xlabel("entry angle (deg)"); c.set_xlim(0, 20.5); c.set_ylim(0, 200)
    for ax in (a, b, c):
        despine(ax)
    save(fig, "fig-33-corridor")
