"""Lecture 14, ram compression: the ratio of total to static pressure of the incoming air, isentropic and behind a
single normal shock (flight.gasdynamics), and the total temperature at 20 km, against Mach number. The compressor
pressure ratio 20 of Sadraey's example turbofan (2017, sec. 4.7.3.3) and the 2400 K inlet/combustor temperature
problem of ramjets (Sadraey sec. 4.4.5) are marked. Writes img/fig-14-ram.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import gasdynamics as GD, atmosphere as A

M = np.linspace(0.0, 8.0, 321)
ISEN = GD.stagnation_pressure_ratio(M)
SHOCK = np.array([GD.rayleigh_pitot(m) if m > 1 else GD.stagnation_pressure_ratio(m) for m in M])
T20 = A.isa(20000.0)[0]
TT = T20 * GD.stagnation_temperature_ratio(M)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.9))
    fig.subplots_adjust(wspace=0.42)
    a1.semilogy(M, ISEN, color=BLUE, lw=2.5, label="slowed without loss")
    a1.semilogy(M, SHOCK, color=MAROON, lw=2.5, label="through one normal shock")
    a1.axhline(20, color=GREY, ls=":", lw=1.2)
    a1.text(3.9, 11, "compressor ratio 20\n(Sadraey's turbofan)", fontsize=10, color=GREY, va="top")
    a1.plot(3, GD.stagnation_pressure_ratio(3.0), "o", color=BLUE, ms=7)
    a1.annotate(f"{GD.stagnation_pressure_ratio(3.0):.1f} at Mach 3", (3, GD.stagnation_pressure_ratio(3.0)), (0.3, 180), color=BLUE, fontsize=12,
                arrowprops=dict(arrowstyle="->", color=BLUE))
    a1.set_xlabel("Mach number"); a1.set_ylabel("ram pressure ratio $p_t/p$"); a1.set_xlim(0, 8); a1.set_ylim(1, 1e4)
    a1.legend(frameon=False, fontsize=11, loc="upper left")
    a2.plot(M, TT, color=AMBER, lw=2.5)
    a2.axhline(2400, color=GREY, ls=":", lw=1.2)
    a2.text(0.2, 2500, "2400 K: ramjet temperature\nproblems (Sadraey)", fontsize=11, color=GREY)
    a2.set_xlabel("Mach number"); a2.set_ylabel(f"total temperature at 20 km (K)"); a2.set_xlim(0, 8); a2.set_ylim(0, 3200)
    despine(a1); despine(a2)
    save(fig, "fig-14-ram")
