"""Lecture 28, the exponential cost of speed: the mass ratio m0/m1 = exp(dV/V_E) of the rocket equation (eq-rocket,
flight.rocket) against the speed gained, for three exit velocities: 2.64 km/s (the PSLV's PS1 in the course dataset,
g0 x 269 s), 3.0 km/s, and 4.4 km/s (an assumed hydrogen-oxygen engine, about 450 s). The vertical line is the circular
speed at 867 km, 7.42 km/s, the target of Lecture 30. Writes img/fig-28-mass-ratio.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import vehicle
from flight.constants import G0, MU, R_EARTH

VE_PS1 = vehicle.load("pslv-ca").stages[0].engine.isp_vac * G0
EXITS = [(VE_PS1, MAROON, "PS1, $V_E$ = 2.64 km/s"), (3000.0, BLUE, "$V_E$ = 3.0 km/s"), (4400.0, GREEN, "hydrogen–oxygen, $V_E$ = 4.4 km/s")]
V_ORBIT = np.sqrt(MU / (R_EARTH + 867e3))
DV = np.linspace(0, 12000, 400)
AT_ORBIT = {round(ve): np.exp(V_ORBIT / ve) for ve, _, _ in EXITS}

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    for ve, c, lab in EXITS:
        ax.semilogy(DV / 1e3, np.exp(DV / ve), color=c, lw=2.6, label=lab)
        ax.plot(V_ORBIT / 1e3, np.exp(V_ORBIT / ve), "o", color=c, ms=8)
    ax.axvline(V_ORBIT / 1e3, color=GREY, lw=1, ls="--")
    ax.text(V_ORBIT / 1e3 + 0.15, 1.3, "circular speed\nat 867 km", fontsize=11, color=GREY)
    ax.set_xlim(0, 12); ax.set_ylim(1, 200)
    ax.set_xlabel("speed gained, $\\Delta V$ (km/s)"); ax.set_ylabel("mass ratio $m_0/m_1$")
    ax.legend(frameon=False, fontsize=12, loc="upper left")
    despine(ax)
    save(fig, "fig-28-mass-ratio")
