"""Lecture 13, a flat plate at 6 deg in supersonic flow as the Mach number rises from 1.4 to 4: the oblique shock
below and the expansion fan above (flight.gasdynamics), the pressure on each side (arrows), and the lift and drag
coefficients by shock-expansion theory. The forces on the plate fall as the Mach number rises.
Writes img/anim-13-plate.gif."""
import numpy as np
from matplotlib.patches import FancyArrowPatch
from _visual import plt, save_anim, MAROON, BLUE, GREEN, INK, GREY
from flight import gasdynamics as GD, aero as AE

ALPHA = np.radians(6.0)
MACHS = np.linspace(1.4, 4.0, 80)
CLCD = np.array([AE.shock_expansion_flat_plate(ALPHA, m) for m in MACHS])

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 5.0), gridspec_kw=dict(width_ratios=[1.25, 1]))
    fig.subplots_adjust(wspace=0.25, bottom=0.14)

    def frame(k):
        a1.clear(); a2.clear()
        M = MACHS[k]
        ca, sa = np.cos(ALPHA), np.sin(ALPHA)
        le, te = np.array([0.0, 0.0]), np.array([ca, -sa])
        a1.plot([le[0], te[0]], [le[1], te[1]], color=INK, lw=4)
        beta = GD.oblique_shock_angle(M, ALPHA)
        a1.plot([0, 1.4 * np.cos(beta)], [0, -1.4 * np.sin(beta)], color=MAROON, lw=2)
        a1.text(1.4 * np.cos(beta) * 0.75, -1.4 * np.sin(beta) * 0.75 - 0.12, "shock", color=MAROON, fontsize=12)
        mu1 = GD.mach_angle(M)
        ex = GD.expansion(M, ALPHA)
        mu2 = GD.mach_angle(ex["M2"])
        for ang in np.linspace(mu1, mu2 - ALPHA, 6):      # first Mach line to the free stream, last to the turned flow
            a1.plot([0, 1.2 * np.cos(ang)], [0, 1.2 * np.sin(ang)], color=BLUE, lw=1)
        a1.text(0.55, 0.55, "expansion fan", color=BLUE, fontsize=12)
        q = 0.5 * GD.G * M**2
        cp_low = (GD.oblique_shock(M, ALPHA)["p"] - 1) / q
        cp_up = (ex["p"] - 1) / q
        nrm = np.array([sa, ca])
        for f in (0.2, 0.45, 0.7, 0.95):
            pt = le + f * (te - le)
            a1.add_patch(FancyArrowPatch(tuple(pt - nrm * 1.6 * cp_low), tuple(pt), arrowstyle="-|>", mutation_scale=12, color=MAROON, lw=1.6))
            a1.add_patch(FancyArrowPatch(tuple(pt), tuple(pt - nrm * 1.6 * cp_up), arrowstyle="-|>", mutation_scale=12, color=BLUE, lw=1.6))
        a1.annotate("", (-0.2, 0.0), (-0.6, 0.0), arrowprops=dict(arrowstyle="->", color=GREY))
        a1.text(-0.62, 0.06, f"$M={M:.2f}$", fontsize=13)
        a1.set_xlim(-0.7, 1.5); a1.set_ylim(-1.0, 1.0); a1.set_aspect("equal"); a1.axis("off")
        a1.set_title("flat plate at $6^\\circ$: suction above (blue), pressure below (maroon)", fontsize=12, loc="left")
        a2.plot(MACHS[:k + 1], CLCD[:k + 1, 0], color=INK, lw=2.5, label="$C_L$")
        a2.plot(MACHS[:k + 1], 10 * CLCD[:k + 1, 1], color=MAROON, lw=2.5, label="$10\\,C_D$")
        a2.set_xlim(1.3, 4.1); a2.set_ylim(0, 0.45)
        a2.set_xlabel("Mach number"); a2.legend(frameon=False, fontsize=12, loc="upper right")
        for s in ("top", "right"):
            a2.spines[s].set_visible(False)
        return []

    save_anim(fig, frame, len(MACHS), 12, "anim-13-plate", dpi=90, hold=2.5)
