"""Lecture 11, the transonic drag rise: wave drag C_Dw = 20 (M - M_c)^4 above the critical Mach number, with
M_c = M_dd - 0.108 (Filippone 2012, eqs. 4.44-4.46; flight.aero.transonic_wave_drag), on top of a representative
subsonic zero-lift drag coefficient of 0.018. An older section (M_dd = 0.72) against a supercritical one (0.80).
Writes img/fig-11-drag-rise.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, GREEN, INK, GREY
from flight import aero as AE

CD0 = 0.018
M = np.linspace(0.5, 0.9, 401)
CASES = [(0.72, MAROON, "older section"), (0.80, GREEN, "supercritical section")]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    for mdd, c, lab in CASES:
        cd = CD0 + np.array([AE.transonic_wave_drag(m, mdd) for m in M])
        ax.plot(M, cd, color=c, lw=2.6, label=f"{lab}, $M_{{dd}}={mdd}$")
        mc = AE.critical_from_divergence(mdd)
        ax.plot(mc, CD0, "|", color=c, ms=18, mew=2)
        ax.text(mc, CD0 - 0.0025, f"$M_{{cr}}={mc:.3f}$", color=c, ha="center", va="top", fontsize=12)
        cdd = CD0 + AE.transonic_wave_drag(mdd, mdd)
        ax.plot(mdd, cdd, "o", color=c, ms=7)
        ax.text(mdd - 0.012, cdd + 0.001, "$M_{dd}$", color=c, ha="right", fontsize=13)
    ax.set_xlim(0.5, 0.9); ax.set_ylim(0.010, 0.05)
    ax.set_xlabel("Mach number"); ax.set_ylabel("zero-lift drag coefficient")
    ax.legend(frameon=False, fontsize=12, loc="upper left")
    despine(ax)
    save(fig, "fig-11-drag-rise")
