"""Lecture 33: the first-order peak stagnation heat flux of the SRE-1 model entering at 8 km/s and 5 deg, against nose
radius (flight.entry.ballistic_peak_heat_flux, Sutton-Graves constant). Writes img/fig-33-nose-radius.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
import _entry33 as C
from flight import entry as E

B = C.B_of(C.capsule("sre-1"))
RN = np.geomspace(0.05, 2.0, 200)


def flux(rn):
    return E.ballistic_peak_heat_flux(8000.0, B, np.radians(5.0), C.H, rn) / 1e6


MARKS = [(0.05, "a sharp tip, 5 cm"), (0.5, "SRE-1, about 0.5 m"), (2.0, "2 m")]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    ax.plot(RN, flux(RN), color=MAROON, lw=3)
    for r, lab in MARKS:
        ax.plot(r, flux(r), "o", color=INK, ms=8, clip_on=False)
        ax.annotate(f"{lab}: {flux(r):.1f} MW/m²", (r, flux(r)), xytext=(10, 6) if r < 1 else (-4, 44), textcoords="offset points",
                    fontsize=12, ha="left" if r < 1 else "right")
    ax.set_xscale("log"); ax.set_yscale("log"); ax.set_xlim(0.045, 2.2); ax.set_ylim(1, 12)
    ax.set_xticks([0.05, 0.1, 0.2, 0.5, 1, 2], ["0.05", "0.1", "0.2", "0.5", "1", "2"]); ax.set_yticks([1, 2, 5, 10], ["1", "2", "5", "10"])
    ax.set_xlabel("nose radius (m)"); ax.set_ylabel("peak heat flux (MW/m²)")
    despine(ax)
    save(fig, "fig-33-nose-radius")
