"""Lecture 37: the stick force of Hull's business jet per unit of G S_E c_E (gearing times elevator area times
elevator chord), in level flight at 30,000 ft, with the trim tab set so that the force is zero at 150 m/s, for four
CG positions. The slope at the trim speed is negative while the CG is ahead of the stick-free neutral point and
reverses behind it. Hinge derivatives from Hull, Problem 9.1. Writes img/fig-37-stick-force.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK
import _stab as S
from flight import stability as ST

V = np.linspace(100, 220, 200)
VTRIM = 150.0
CGS = ((0.20, BLUE), (0.30, GREEN), (0.40, AMBER), (0.49, MAROON))


def force(x_cg, speed, v_trim=VTRIM):
    """F_s/(G S_E c_E) in Pa with the tab's constant hinge-moment coefficient chosen for zero force at v_trim."""
    c0, c1 = ST.hinge_trim_line(S.WING, S.TAIL, S.HINGE, x_cg, S.THRUST)
    ws = S.WEIGHT / S.AREA
    tab = -(c0 + c1 * ws / (0.5 * S.RHO * v_trim ** 2))
    return ST.stick_force_per_gearing(speed, S.RHO, ws, c0 + tab, c1, S.TAIL.eta)


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.6))
    for x, c in CGS:
        ax.plot(V, force(x, V), color=c, lw=2.6, label=f"CG at {x:.2f}")
    ax.plot(VTRIM, 0, "o", color=INK, ms=9)
    ax.axhline(0, color=INK, lw=0.9)
    ax.set_xlabel("true airspeed (m/s)"); ax.set_ylabel(r"$F_s/(G\,S_E\,\bar c_E)$ (Pa)    pull $+$")
    ax.legend(frameon=False, fontsize=11)
    despine(ax)
    save(fig, "fig-37-stick-force")
