"""Lecture 13, Newtonian flow. Left: lift, drag and L/D of a flat plate against angle of attack (C_p,max = 2,
flight.aero.newtonian_flat_plate). Right: the drag coefficient of a sharp cone against its half-angle and of a
sphere, with Newton's C_p,max = 2 and the modified value at Mach 25 behind the bow shock (modified_newtonian_cpmax).
Writes img/fig-13-newtonian.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import aero as AE

ALPHA = np.radians(np.linspace(0.5, 60, 200))
CPM25 = AE.modified_newtonian_cpmax(25.0)
DELTA = np.radians(np.linspace(1, 60, 200))

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.9))
    fig.subplots_adjust(wspace=0.35)
    cl, cd = AE.newtonian_flat_plate(ALPHA)
    deg = np.degrees(ALPHA)
    a1.plot(deg, cl, color=BLUE, lw=2.5, label="$C_L$")
    a1.plot(deg, cd, color=MAROON, lw=2.5, label="$C_D$")
    a1b = a1.twinx()
    a1b.plot(deg, cl / cd, color=GREEN, lw=2, ls="--")
    a1b.set_ylabel("$L/D=\\cot\\alpha$", color=GREEN); a1b.set_ylim(0, 6)
    a1.set_xlabel("angle of attack (deg)"); a1.set_xlim(0, 60); a1.set_ylim(0, 1.6)
    a1.legend(frameon=False, fontsize=12, loc="center left")
    a1.spines["top"].set_visible(False)
    a2.plot(np.degrees(DELTA), AE.newtonian_cone_cd(DELTA), color=INK, lw=2.5, label="cone, $C_{p,max}=2$")
    a2.plot(np.degrees(DELTA), AE.newtonian_cone_cd(DELTA, CPM25), color=AMBER, lw=2.5, label=f"cone, $C_{{p,max}}={CPM25:.2f}$")
    a2.axhline(AE.newtonian_sphere_cd(), color=INK, ls=":", lw=1.2)
    a2.axhline(AE.newtonian_sphere_cd(CPM25), color=AMBER, ls=":", lw=1.2)
    a2.text(2, 1.03, "sphere", fontsize=12)
    a2.set_xlabel("cone half-angle (deg)"); a2.set_ylabel("$C_D$ (base area)")
    a2.set_xlim(0, 60); a2.set_ylim(0, 1.6)
    a2.legend(frameon=False, fontsize=11, loc="upper left")
    despine(a2)
    save(fig, "fig-13-newtonian")
