"""Lecture 33: SRE-1 ballistic entry against the entry angle at 100 km: peak drag deceleration, peak stagnation heat
flux and stagnation heat load. Points: simulator; lines: first-order solution. Writes img/fig-33-angle.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK
import _entry33 as X
from flight import entry as E
from flight.constants import G0, RHO0_SL

V = X.capsule("sre-1")
B, RN = X.B_of(V), V.nose_radius
GG = np.linspace(2.0, 40.0, 200)
RHO_END = RHO0_SL * np.exp(-X.H_STOP / X.H)
CF = dict(a=[E.ballistic_peak_deceleration(X.SRE_V, -np.radians(g), X.H) / G0 for g in GG],
          q=[E.ballistic_peak_heat_flux(X.SRE_V, B, -np.radians(g), X.H, RN) / 1e6 for g in GG],
          Q=[E.ballistic_heat_load(X.SRE_V, B, -np.radians(g), X.H, RN, RHO_END) / 1e6 for g in GG])
SIM = [dict(g=-g, **X.peaks(X.sre(g))) for g in X.GAMMAS]

if __name__ == "__main__":
    fig, axes = plt.subplots(1, 3, figsize=(10.4, 3.9), gridspec_kw=dict(wspace=0.38))
    for ax, key, scale, lab in zip(axes, ("a", "q", "Q"), (1.0, 1e-6, 1e-6),
                                   ("peak deceleration (g)", "peak heat flux (MW/m²)", "heat load (MJ/m²)")):
        ax.plot(GG, CF[key], color=BLUE, lw=2.2, label="first-order solution")
        ax.plot([s["g"] for s in SIM], [s[key] * scale for s in SIM], "o", color=MAROON, ms=6, label="simulator")
        ax.set_xlabel("entry angle (°)"); ax.set_title(lab, fontsize=11); ax.set_ylim(0, None); despine(ax)
    fig.legend(*axes[0].get_legend_handles_labels(), frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, -0.05))
    save(fig, "fig-33-angle")
