"""Lecture 13, the best lift-to-drag ratio of a thin wing in supersonic flow by linear theory: C_D0 = C_f +
4 tau^2/beta (double wedge, flight.aero.ackeret_thickness_drag) and K = beta/4 (supersonic_induced_factor), so
E_max = 1/(2 sqrt(K C_D0)) (max_lift_to_drag). Friction coefficient C_f = 0.006 ASSUMED; thickness ratios 2, 3, 5 %.
Wing alone: a whole aircraft has more drag. Writes img/fig-13-emax-supersonic.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import aero as AE

CF = 0.006
TAUS = [(0.02, GREEN), (0.03, BLUE), (0.05, MAROON)]
M = np.linspace(1.15, 4.0, 200)


def emax(tau, m):
    return AE.max_lift_to_drag(CF + AE.ackeret_thickness_drag(tau, m), AE.supersonic_induced_factor(m))[1]


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    for tau, c in TAUS:
        ax.plot(M, [emax(tau, m) for m in M], color=c, lw=2.5, label=f"$t/c={tau:.2f}$")
    ax.plot(2.2, emax(0.03, 2.2), "o", color=BLUE, ms=8)
    ax.text(2.3, emax(0.03, 2.2) + 0.4, f"{emax(0.03, 2.2):.1f} at $M=2.2$", color=BLUE, fontsize=12)
    ax.set_xlim(1, 4); ax.set_ylim(0, 16)
    ax.set_xlabel("Mach number"); ax.set_ylabel("$E_{max}$ of the wing")
    ax.legend(frameon=False, fontsize=12, loc="upper right")
    despine(ax)
    save(fig, "fig-13-emax-supersonic")
