"""Lecture 18, drag per unit weight of the Tejas-class fighter against Mach number for several dimensionless wing
loadings omega = 2W/(gamma p S) (after Miele ch. 11, Figs. 1 and 17), and the lower envelope 1/E_max(M), reached at
omega = M^2 sqrt(C_D0/K) (flight.performance.min_drag_wing_loading). Writes img/fig-18-drag-per-weight.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, PURPLE, INK
import _fighter as F
from flight import performance as PF, aero as AE

M = np.linspace(0.3, 2.4, 500)
A = F.TEJ.aero
OMEGAS = [(0.05, GREEN), (0.1, BLUE), (0.2, PURPLE), (0.4, MAROON)]
ENVELOPE = np.array([1.0 / AE.max_lift_to_drag(A.cd0(x), A.k(x))[1] for x in M])


def dw(x, w):
    return PF.drag_per_weight_mach(x, w, A.cd0(x), A.k(x))


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5))
    for w, c in OMEGAS:
        ax.plot(M, [dw(x, w) for x in M], color=c, lw=2.4, label=f"$\\omega={w}$")
    ax.plot(M, ENVELOPE, color=INK, lw=1.8, ls="--", label="$1/E_{\\max}(M)$")
    ax.set_ylim(0, 0.8); ax.set_xlabel("Mach number"); ax.set_ylabel("$D/W$")
    ax.legend(frameon=False, fontsize=12, loc="upper center", ncol=3)
    despine(ax)
    save(fig, "fig-18-drag-per-weight")
