"""Lecture 12, the finite wing: the lift-curve slope against aspect ratio from Sadraey eq. 2.13 with the thin-airfoil
section slope 2 pi (flight.aero.finite_wing_slope), and the lift curves of three wings. Aspect ratios: 3 (a short
wing, as on a combat aircraft), 9.1 (the HANSA-3-class wing of fig-12-polar.py) and 21.4 (the representative glider,
15 m span on 10.5 m^2). Writes img/fig-12-lift-curve.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from _scripts import load
from flight import aero as AE, vehicle

P = load("fig-12-polar")
GL = vehicle.load("glider-representative")
WINGS = [("short wing", 3.0, BLUE), ("HANSA-3 class", P.AR_H, MAROON), ("glider", AE.aspect_ratio(GL.span, GL.aero.S), AMBER)]
AR = np.linspace(1, 30, 300)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 5.0))
    fig.subplots_adjust(wspace=0.3)
    a1.plot(AR, AE.finite_wing_slope(AR), color=INK, lw=2.5)
    a1.axhline(2 * np.pi, color=GREY, ls=":", lw=1)
    a1.text(13, 2 * np.pi + 0.12, "$2\\pi$: the section", fontsize=12, color=GREY)
    for lab, ar, c in WINGS:
        a1.plot(ar, AE.finite_wing_slope(ar), "o", color=c, ms=8)
        x_, y_ = {3.0: (4.2, 2.3)}.get(round(ar, 1), (8.0, 3.95) if ar < 15 else (21.0, 3.1))
        a1.text(x_, y_, f"{lab}\nAR {ar:.1f}: {AE.finite_wing_slope(ar):.2f}", color=c, fontsize=11)
    a1.set_xlim(0, 30); a1.set_ylim(0, 7)
    a1.set_xlabel("aspect ratio AR"); a1.set_ylabel("lift-curve slope (1/rad)")
    al = np.radians(np.linspace(0, 12, 50))
    a2.plot(np.degrees(al), 2 * np.pi * al, color=GREY, lw=1.5, ls="--", label="section, $2\\pi$")
    for lab, ar, c in WINGS:
        a2.plot(np.degrees(al), AE.finite_wing_slope(ar) * al, color=c, lw=2.5, label=f"AR {ar:.1f}")
    a2.set_xlabel("angle of attack from zero lift (deg)"); a2.set_ylabel("$C_L$")
    a2.legend(frameon=False, fontsize=12, loc="upper left")
    despine(a1); despine(a2)
    save(fig, "fig-12-lift-curve")
