"""Lecture 14: schematic of a liquid-propellant rocket engine, showing where the quantities of the thrust equation act.
No numbers. Writes img/fig-14-rocket-schematic.png."""
from matplotlib.patches import FancyBboxPatch, Polygon, Circle
from _visual import plt, save, MAROON, BLUE, GREEN, INK, GREY

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10.5, 4.6))
    for y, lab, c in ((1.35, "fuel", GREEN), (-1.35, "oxidiser", BLUE)):
        ax.add_patch(FancyBboxPatch((0.2, y - 0.65), 2.3, 1.3, boxstyle="round,pad=0.05,rounding_size=0.35", facecolor="none", edgecolor=c, lw=2.4))
        ax.text(1.35, y, lab, ha="center", va="center", fontsize=14, color=c)
        ax.plot([2.55, 3.35, 3.35, 4.3], [y, y, 0.32 * y / 1.35, 0.32 * y / 1.35], color=c, lw=2.4)
        ax.add_patch(Circle((3.35, 0.72 * y / 1.35), 0.22, facecolor="#fbfaf6", edgecolor=c, lw=2, zorder=3))
    ax.text(3.75, 0.95, "pumps", fontsize=12)
    chamber = [(4.3, 0.75), (5.8, 0.75), (6.5, 0.25), (8.1, 1.15), (8.1, -1.15), (6.5, -0.25), (5.8, -0.75), (4.3, -0.75)]
    ax.add_patch(Polygon(chamber, closed=True, facecolor="#e6e1d6", edgecolor=INK, lw=2.2))
    ax.text(5.05, 0, "combustion\nchamber", ha="center", va="center", fontsize=11.5)
    ax.text(6.5, 0.55, "throat", ha="center", fontsize=11.5); ax.text(7.6, -1.5, "nozzle", ha="center", fontsize=12)
    ax.plot([8.1, 8.1], [-1.6, 1.6], color=GREY, lw=1.2, ls="--")
    ax.text(8.15, 2.05, "exit: area $A_e$, pressure $p_e$", fontsize=12.5, ha="center")
    ax.annotate("", xy=(10.2, 0), xytext=(8.15, 0), arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=3))
    ax.text(9.55, 0.25, "exhaust,\nspeed $v_e$\nrelative to rocket", ha="center", va="bottom", fontsize=12, color=MAROON)
    ax.annotate("", xy=(-1.7, 0), xytext=(-0.1, 0), arrowprops=dict(arrowstyle="-|>", color=INK, lw=3.4))
    ax.text(-0.9, 0.25, "thrust $T$", ha="center", fontsize=13.5)
    ax.text(10.0, -1.55, "ambient pressure $p_a$\nall around", ha="center", va="top", fontsize=11.5, color=GREY)
    ax.set_xlim(-1.9, 11.5); ax.set_ylim(-2.5, 2.5); ax.set_aspect("equal"); ax.axis("off")
    save(fig, "fig-14-rocket-schematic")
