"""Lecture 28, vertical flight in a vacuum from rest, propellant fraction 0.8 (Miele sec. 15-2): burnout velocity and
peak height against the initial thrust-to-weight ratio, for constant thrust and for constant acceleration.
Writes img/fig-28-vertical.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREY
from flight import rocket as R

ZETA = 0.8
TAU = np.linspace(1.001, 8.0, 400)
L = -np.log(1.0 - ZETA)
PROGRAMS = [(R.vertical_constant_thrust, MAROON, "constant thrust"),
            (R.vertical_constant_acceleration, BLUE, "constant acceleration")]

if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10, 4.2), gridspec_kw=dict(wspace=0.3))
    for f, c, lab in PROGRAMS:
        d = [f(ZETA, t) for t in TAU]
        a.plot(TAU, [x["u_c"] for x in d], color=c, lw=2.4, label=lab)
        b.plot(TAU, [x["eta_f"] for x in d], color=c, lw=2.4, label=lab)
    for ax, y, lab in ((a, L, "no gravity loss"), (b, 0.5 * L**2, "impulsive limit")):
        ax.axhline(y, color=GREY, ls="--", lw=1.2); ax.text(7.9, y * 1.03, lab, ha="right", fontsize=11, color=GREY)
        ax.set_xlabel("initial thrust / weight"); ax.set_xlim(1, 8); despine(ax)
    a.set_ylabel("burnout velocity $V/V_E$"); a.set_ylim(0, 1.8)
    b.set_ylabel("peak height $gh/V_E^2$"); b.set_ylim(0, 1.45)
    b.legend(frameon=False, fontsize=11, loc="lower right")
    save(fig, "fig-28-vertical")
