"""Lecture 16, point performance of Hull's business jet at 35,000 ft (_isbj.py): the specific range V/(cD) (distance
per unit weight of fuel) and the specific endurance 1/(cD) against speed at three weights (flight.performance).
The range peaks at u = 3^(1/4), the endurance at u = 1. Writes img/fig-16-specific-range.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
import _isbj as J
from flight import performance as PF

WEIGHTS = [(13000, MAROON), (11000, BLUE), (9000, GREEN)]
V = np.linspace(110, 270, 300)


def sr(v, w_lb):
    d0, di = PF.level_flight_drag(w_lb * J.LB, J.RHO, v, J.S, J.CD0, J.K)
    return PF.specific_range(v, J.C, d0 + di)


if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.9))
    fig.subplots_adjust(wspace=0.38)
    for w, c in WEIGHTS:
        vs = PF.min_drag_speed(w * J.LB, J.RHO, J.S, J.CD0, J.K)
        a1.plot(V, sr(V, w) * J.LB / J.MILE, color=c, lw=2.4, label=f"{w:,} lb")
        vr = 3**0.25 * vs
        a1.plot(vr, sr(vr, w) * J.LB / J.MILE, "o", color=c, ms=7)
        se = sr(V, w) / V * J.LB * 1000 / 3600          # hours per 1000 lb of fuel
        a2.plot(V, se, color=c, lw=2.4, label=f"{w:,} lb")
        a2.plot(vs, sr(vs, w) / vs * J.LB * 1000 / 3600, "o", color=c, ms=7)
    a1.set_xlabel("true airspeed (m/s)"); a1.set_ylabel("specific range (miles per lb of fuel)")
    a2.set_xlabel("true airspeed (m/s)"); a2.set_ylabel("specific endurance (hours per 1000 lb)")
    a1.legend(frameon=False, fontsize=11, loc="lower center"); a1.set_title("dots: $u=3^{1/4}$", fontsize=12, loc="left")
    a2.set_title("dots: $u=1$", fontsize=12, loc="left")
    a2.set_ylim(0, None)
    a1.set_ylim(0, None)
    despine(a1); despine(a2)
    save(fig, "fig-16-specific-range")
