"""Lecture 26, the forces on Hull's SBJ along its take-off ground run at sea level (flaps 20 deg, 13,000 lb): Hull's
thrust parabola (Problem 6.8), the drag, the rolling friction mu (W - L) and the net accelerating force, against airspeed
up to the lift-off speed 1.2 V_stall (the picture of Ojha Fig. 16.5). Writes img/fig-26-forces.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
import _takeoff as T
from flight import atmosphere as A
from flight.constants import RHO0_SL

RHO = A.isa(0.0)[2]
VLO = 1.2 * T.stall(RHO, T.TO["clmax"])
V = np.linspace(0, VLO, 300)
Q = 0.5 * RHO * V**2 * T.S
THRUST = T.thrust_parabola(V)
DRAG = Q * T.TO["cd"]
FRICTION = T.TO["mu"] * (T.W - Q * T.TO["cl"])
NET = np.array([T.net_force(v, RHO, T.thrust_parabola(v), T.TO) for v in V])

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    k = 1e-3
    for y, lab, c, ls in ((THRUST, "thrust (Hull's parabola)", MAROON, "-"), (NET, "net force", INK, "-"),
                          (DRAG, "drag", BLUE, "--"), (FRICTION, "rolling friction $\\mu(W-L)$", GREEN, "--")):
        ax.plot(V, y * k, color=c, lw=2.4, ls=ls, label=lab)
    ax.axvline(VLO, color=GREY, lw=1)
    ax.text(VLO - 1, 13, f"lift-off\n{VLO:.1f} m/s", ha="right", fontsize=11, color=GREY)
    ax.set_xlabel("airspeed (m/s)"); ax.set_ylabel("force (kN)"); ax.set_xlim(0, 72); ax.set_ylim(0, 29)
    fig.legend(frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-26-forces")
