"""Lecture 11, the jumps across a normal shock against the upstream Mach number (flight.gasdynamics.normal_shock).
Left: pressure, density and temperature ratios. Right: the Mach number behind the shock and the fraction of the total
pressure that survives. Writes img/fig-11-normal-shock.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, PURPLE, INK, GREY
from flight import gasdynamics as GD

M1 = np.linspace(1.0, 5.0, 161)
NS = [GD.normal_shock(m) for m in M1]
P, RHO, T = (np.array([n[k] for n in NS]) for k in ("p", "rho", "T"))
M2, P0 = (np.array([n[k] for n in NS]) for k in ("M2", "p0"))
AT2 = GD.normal_shock(2.0)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 5.0))
    fig.subplots_adjust(wspace=0.28)
    a1.plot(M1, P, color=MAROON, lw=2.5, label="$p_2/p_1$")
    a1.plot(M1, RHO, color=BLUE, lw=2.5, label="$\\rho_2/\\rho_1$")
    a1.plot(M1, T, color=AMBER, lw=2.5, label="$T_2/T_1$")
    a1.axhline(6.0, color=BLUE, ls=":", lw=1)
    a1.text(3.3, 6.5, "density limit 6", color=BLUE, fontsize=12)
    a1.set_xlabel("Mach number ahead, $M_1$"); a1.set_ylabel("ratio across the shock")
    a1.set_ylim(0, 30); a1.legend(frameon=False, fontsize=13, loc="upper left", bbox_to_anchor=(0, 0.92))
    a2.plot(M1, M2, color=GREEN, lw=2.5, label="$M_2$ behind")
    a2.plot(M1, P0, color=PURPLE, lw=2.5, label="$p_{0,2}/p_{0,1}$ kept")
    a2.plot(2.0, AT2["p0"], "o", color=PURPLE, ms=7)
    a2.text(2.25, AT2["p0"] + 0.02, f"{AT2['p0']:.2f} at $M_1=2$", color=PURPLE, fontsize=12)
    a2.set_xlabel("Mach number ahead, $M_1$"); a2.set_ylim(0, 1.05)
    a2.legend(frameon=False, fontsize=13, loc="upper right")
    despine(a1); despine(a2)
    save(fig, "fig-11-normal-shock")
