"""Lecture 28, the payload ratio of n equal stages against the velocity required (u = V/V_E, no gravity, no drag),
structural factor 0.1 (Miele sec. 16-5, Fig. 4). Writes img/fig-28-payload.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, GREY
from flight import rocket as R

EPS = 0.1
U = np.linspace(0.0, 5.0, 400)
CURVES = [(n, c) for n, c in ((1, MAROON), (2, BLUE), (3, GREEN), (4, AMBER))]
CASES = [(2.0, 1), (2.0, 2), (2.6, 2), (3.7, 2), (3.7, 3), (3.7, 4)]     # the values Miele quotes

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.0))
    for n, c in CURVES:
        y = np.array([R.payload_ratio(u, EPS, n) for u in U])
        ax.semilogy(U[y > 0], y[y > 0], color=c, lw=2.4, label=f"{n} stage" + ("s" if n > 1 else ""))
        if n <= 2:
            ax.axvline(R.limit_velocity(EPS, n), color=c, lw=1, ls=":")
    ax.semilogy(U, R.payload_ratio_infinite(U, EPS), color=GREY, lw=2, ls="--", label="infinitely many")
    ax.set_xlabel("velocity required, $u = V/V_E$"); ax.set_ylabel("payload / lift-off mass")
    ax.set_xlim(0, 5); ax.set_ylim(1e-3, 1)
    ax.legend(frameon=False, fontsize=11, loc="upper right")
    despine(ax)
    save(fig, "fig-28-payload")
