"""Lecture 18, the thrust equation of the Tejas-class fighter at 11 km and 8500 kg in Mach-number form (after Vinh,
Figs. 6.8, 6.10 and 6.11): dimensionless thrust required tau_r = M^2 C_D and available tau_a = 2T/(gamma p S) for
military thrust, a partial afterburner (72 % of full) and full afterburner (left); the same as coefficients, C_D and
C_T = tau_a/M^2 (right). Writes img/fig-18-sound-barrier.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
import _fighter as F

V, H, MASS = F.TEJ, 11000.0, F.M_REF
M = np.linspace(0.3, 2.2, 600)
LEVELS = [("military", False, 1.0, GREEN), ("72 % afterburner", True, 0.72, AMBER), ("full afterburner", True, 1.0, BLUE)]
ROOTS = {lab: F.level_machs(V, MASS, H, ab, f) for lab, ab, f, _ in LEVELS}
TAU_R = np.array([F.tau_r(V, x, MASS, H) for x in M])

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.9))
    fig.subplots_adjust(wspace=0.3)
    a1.plot(M, TAU_R, color=MAROON, lw=2.7, label="required $\\tau_r$")
    for lab, ab, f, c in LEVELS:
        ta = np.array([F.tau_a(V, x, H, ab, f) for x in M])
        a1.plot(M, ta, color=c, lw=2.0, ls="--", label=lab)
        a1.plot(ROOTS[lab], [F.tau_a(V, x, H, ab, f) for x in ROOTS[lab]], "o", color=c, ms=7)
        a2.plot(M, ta / M**2, color=c, lw=2.0, ls="--", label=f"$C_T$, {lab}")
    a2.plot(M, TAU_R / M**2, color=MAROON, lw=2.7, label="$C_D$")
    a1.set_xlim(0.3, 2.2); a1.set_ylim(0, 0.2); a1.set_xlabel("Mach number"); a1.set_ylabel("$\\tau=2F/(\\gamma pS)$")
    a1.legend(frameon=False, fontsize=11, loc="upper left")
    a2.set_xlim(0.6, 2.2); a2.set_ylim(0, 0.12); a2.set_xlabel("Mach number"); a2.set_ylabel("$C_D$, $C_T$")
    a2.legend(frameon=False, fontsize=11, loc="upper right")
    despine(a1); despine(a2)
    save(fig, "fig-18-sound-barrier")
