"""Lecture 35: the distance a vehicle can glide from speed V while slowing to nothing at nearly constant height, for
L/D = 1: Vinh's orbital glide (flight.entry.glide_range_angle) and its low-speed limit E V^2/(2 g). g and r are taken
at the surface. Writes img/fig-35-glide-range.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE
from flight import entry as E, performance as PF
from flight.constants import MU, R_EARTH, G0

G = MU / R_EARTH**2
LD = 1.0


def orbital(V):
    u = V**2 / (G * R_EARTH)
    return R_EARTH * E.glide_range_angle(0.0, u, LD)


def low_speed(V):
    """L/D times the kinetic part of the energy height."""
    return LD * PF.energy_height(0.0, V, g=G)


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.4))
    V = np.linspace(10, 0.985 * np.sqrt(G * R_EARTH), 500)
    ax.semilogy(V / 1e3, [orbital(v) / 1e3 for v in V], color=BLUE, lw=2.6, label="round Earth (Lecture 34)")
    ax.semilogy(V / 1e3, [low_speed(v) / 1e3 for v in V], color=MAROON, lw=2.2, ls="--", label="$E\\,V^2/2g$ (flat Earth)")
    ax.set_xlabel("speed at the start (km/s)"); ax.set_ylabel("distance for L/D = 1 (km)"); ax.set_ylim(1e-2, 3e4)
    ax.legend(frameon=False, fontsize=11, loc="lower right")
    despine(ax)
    save(fig, "fig-35-glide-range")
