"""Lecture 14, three ways to make the same thrust, T = 50 kN at V = 250 m/s (a HYPOTHETICAL engine, numbers chosen for
illustration): a small fast jet (100 kg/s), a turbofan-like stream (500 kg/s) and a propeller-like stream
(2500 kg/s). Thrust T = mdot (Ve - V) (fuel neglected); the propulsive efficiency 2/(1 + Ve/V)
(flight.propulsion). Air seen from the engine: it arrives at V and leaves at Ve. Stream heights grow with the
square root of the mass flow (area). Writes img/anim-14-jets.gif."""
import numpy as np
from _visual import plt, save_anim, MAROON, BLUE, GREEN, INK, GREY
from flight import propulsion as PR

T, V = 50e3, 250.0
CASES = [("small, fast jet", 100.0, MAROON), ("turbofan-like", 500.0, BLUE), ("propeller-like", 2500.0, GREEN)]
ROWS = []
for lab, md, c in CASES:
    ve = V + T / md
    ROWS.append(dict(lab=lab, md=md, c=c, ve=ve, eta=PR.propulsive_efficiency(V, ve),
                     waste=0.5 * md * (ve - V) ** 2))
rng = np.random.default_rng(3)
DOTS = [rng.uniform(0, 1, (60, 2)) for _ in ROWS]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(11, 5.6))
    fig.subplots_adjust(left=0.02, right=0.98, top=0.93, bottom=0.04)
    N = 60

    def frame(k):
        ax.clear()
        for i, r in enumerate(ROWS):
            yc = 2.0 - 1.4 * i
            h = 0.12 * np.sqrt(r["md"] / 100.0)
            ax.plot([0, 0], [yc - h * 1.1, yc + h * 1.1], color=INK, lw=3)
            for u, v in DOTS[i]:
                x0 = -1.0 + u * 2.4
                # time-shifted position: upstream speed V, downstream Ve (scaled to the screen)
                s = (x0 + k * 0.012 * V / 250.0)
                x = s if s < 0 else s * r["ve"] / V
                x = (x + 1.0) % 2.4 - 1.0
                ax.plot(x, yc + (v - 0.5) * 2 * h, "o", color=r["c"], ms=2.5)
            ax.text(-1.0, yc + h + 0.12, f"{r['lab']}: {r['md']:.0f} kg/s, leaves at {r['ve']:.0f} m/s", fontsize=12, color=r["c"])
            ax.text(1.45, yc - 0.1, f"$\\eta_p={r['eta']:.2f}$\nwasted {r['waste'] / 1e6:.2f} MW", fontsize=12, color=r["c"])
        ax.set_xlim(-1.05, 2.4); ax.set_ylim(-1.9, 2.6); ax.axis("off")
        ax.set_title(f"same thrust $T=50$ kN at $V=250$ m/s; useful power $TV=12.5$ MW", fontsize=13, loc="left")
        return []

    save_anim(fig, frame, N, 12, "anim-14-jets", dpi=90, hold=2.0)
