"""Lecture 21, the rate of climb of every jet with a parabolic polar in Hull's units: hdot E*/V* = tau u - (u^3 + 1/u)/2
against u = V/V* for five thrust ratios tau = T E*/W, with the steepest climb (u = 1) and the fastest climb
(u^2 = (tau + sqrt(tau^2 + 3))/3, Hull eq. 5.53). Writes img/fig-21-universal.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, PURPLE, INK
from flight import performance as PF

U = np.linspace(0.3, 3.2, 500)
TAUS = [(1.3, PURPLE), (2.0, GREEN), (3.2, AMBER), (5.0, BLUE), (6.76, MAROON)]
TAU_LINE = np.linspace(1.0, 7.0, 200)
UF = PF.fastest_climb_u(TAU_LINE)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.8), gridspec_kw=dict(width_ratios=[1.35, 1]))
    fig.subplots_adjust(wspace=0.3)
    for tau, c in TAUS:
        r = np.array([PF.climb_rate_ideal(u, tau, 1.0, 1.0) for u in U])
        a1.plot(U[r > 0], r[r > 0], color=c, lw=2.3, label=f"$\\tau={tau}$")
        uf = PF.fastest_climb_u(tau)
        a1.plot(uf, PF.climb_rate_ideal(uf, tau, 1.0, 1.0), "s", color=c, ms=7)
        a1.plot(1.0, PF.climb_rate_ideal(1.0, tau, 1.0, 1.0), "o", color=c, ms=7)
    a1.set_xlabel("$u=V/V^*$"); a1.set_ylabel("$\\dot h\\,E^*/V^*$"); a1.set_ylim(0, 14)
    a1.legend(frameon=False, fontsize=11, loc="upper left")
    a2.plot(TAU_LINE, UF, color=INK, lw=2.5, label="fastest")
    a2.axhline(1.0, color=GREY if False else INK, lw=1.5, ls="--", label="steepest")
    a2.set_xlabel("$\\tau=TE^*/W$"); a2.set_ylabel("$u$"); a2.set_ylim(0.8, 2.6)
    a2.legend(frameon=False, fontsize=11, loc="upper left")
    despine(a1); despine(a2)
    save(fig, "fig-21-universal")
