"""Lecture 15, the drag of any aircraft with a parabolic polar in level flight, in Hull's nondimensional form
(flight.performance.drag_ratio): D/D* against u = V/V*, with its zero-lift and induced parts, and the power required
P/(D*V*) (power_ratio). Writes img/fig-15-universal-drag.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import performance as PF

U = np.linspace(0.35, 2.4, 400)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.9))
    fig.subplots_adjust(wspace=0.28)
    a1.plot(U, 0.5 * U**2, color=BLUE, lw=2, label="zero-lift part $u^2/2$")
    a1.plot(U, 0.5 / U**2, color=GREEN, lw=2, label="induced part $1/(2u^2)$")
    a1.plot(U, PF.drag_ratio(U), color=MAROON, lw=2.8, label="drag $D/D^*$")
    a1.plot(1, 1, "o", color=MAROON, ms=8)
    a1.annotate("minimum drag\nat $u=1$", (1, 1), (0.37, 0.4), fontsize=12, color=MAROON, arrowprops=dict(arrowstyle="->", color=MAROON))
    a1.set_xlim(0.3, 2.4); a1.set_ylim(0, 3); a1.set_xlabel("$u=V/V^*$"); a1.set_ylabel("$D/D^*$")
    a1.legend(frameon=False, fontsize=11, loc="upper center")
    a2.plot(U, PF.power_ratio(U), color=AMBER, lw=2.8)
    um = PF.U_MIN_POWER
    a2.plot(um, PF.power_ratio(um), "o", color=AMBER, ms=8)
    a2.annotate(f"minimum power\n$u=3^{{-1/4}}={um:.2f}$", (um, PF.power_ratio(um)), (0.4, 2.4), fontsize=12, color=AMBER, arrowprops=dict(arrowstyle="->", color=AMBER))
    a2.plot(1, 1, "o", color=MAROON, ms=7)
    a2.text(1.05, 0.55, "minimum drag", fontsize=11, color=MAROON)
    a2.set_xlim(0.3, 2.4); a2.set_ylim(0, 4); a2.set_xlabel("$u=V/V^*$"); a2.set_ylabel("$P/(D^*V^*)$")
    despine(a1); despine(a2)
    save(fig, "fig-15-universal-drag")
