"""Figure for complications-to-impulsive-transfers-chobotov.qmd (img/fig-ci-edelbaum.png): very low thrust transfers.

(a) Edelbaum's pseudo-velocity u = V (cos(pi di/2), sin(pi di/2)) moves at the constant rate a (the thrust acceleration): the least dV is the straight line from
    u0 = (V0, 0) to u1. Shown for LEO (278 km) to GEO with di = 28.5 deg.
(b) dV against the plane change for LEO to GEO: continuous low thrust (Edelbaum), the best two-impulse transfer (plane change split between the burns), and the
    two-impulse transfer with the whole plane change at apogee.

Run from the sfm-slides directory:   python3 codes/fig-ci-edelbaum.py
"""
import numpy as np
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from scipy.optimize import minimize_scalar
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine
from _complic import *

rl, rg = RE + 278, RE + 35786
h = hohmann(rl, rg)
V0, V1 = h["vc1"], h["vc2"]
fig, ax = plt.subplots(1, 2, figsize=(13.4, 5.6))
A = ax[0]
di = 28.5 * D2R; phi1 = np.pi / 2 * di
u0 = np.array([V0, 0.0]); u1 = V1 * np.array([np.cos(phi1), np.sin(phi1)])
th = np.linspace(0, phi1, 100)
A.plot(V1 * np.cos(th), V1 * np.sin(th), color=INK, lw=0.9, ls=":"); A.plot(V0 * np.cos(th), V0 * np.sin(th), color=INK, lw=0.9, ls=":")
A.plot([0, V0], [0, 0], color=BLUE, lw=1.8); A.plot([0, u1[0]], [0, u1[1]], color=MAROON, lw=1.8)
A.annotate("", xy=u1, xytext=u0, arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=3.0))
A.scatter(*u0, color=BLUE, s=50, zorder=6); A.scatter(*u1, color=MAROON, s=50, zorder=6)
A.text(V0 * 0.5, -0.45, r"$V_0=%.3f$ km/s (LEO)" % V0, color=BLUE, fontsize=11, ha="center")
A.text(u1[0] * 0.6 - 0.1, u1[1] * 0.6 + 0.35, r"$V_1=%.3f$ km/s (GEO)" % V1, color=MAROON, fontsize=11, ha="right")
mid = 0.5 * (u0 + u1)
A.text(mid[0] + 0.15, mid[1] + 0.05, r"$\Delta V=|\mathbf{u}_1-\mathbf{u}_0|=%.3f$ km/s" % edelbaum_dv(V0, V1, di), color=GREEN, fontsize=11.5, rotation=0, ha="left")
a_ = np.linspace(0, phi1, 40)
A.plot(1.5 * np.cos(a_), 1.5 * np.sin(a_), color=INK, lw=1.4); A.text(1.7, 0.42, r"$\pi\Delta i/2=%.1f^\circ$" % np.degrees(phi1), fontsize=11)
A.set_aspect("equal"); A.set_xlim(-0.5, 9.5); A.set_ylim(-0.9, 4.0); A.axis("off")
A.set_title(r"(a)  the pseudo-velocity vector moves in a straight line at speed $a$", fontsize=11.5, loc="left")
B = ax[1]
dis = np.linspace(0, 60, 121)
ed = [edelbaum_dv(V0, V1, d * D2R) for d in dis]
best = []
allap = []
for d in dis:
    thd = d * D2R
    f = lambda x: dv_from_angle(V0, h["vp"], x) + dv_from_angle(V1, h["va"], thd - x)
    best.append(minimize_scalar(f, bounds=(0, thd + 1e-12), method="bounded").fun if d > 0 else f(0.0))
    allap.append(f(0.0))
B.plot(dis, ed, color=MAROON, lw=2.6, label="continuous low thrust (Edelbaum)")
B.plot(dis, allap, color=BLUE, lw=2.0, ls="--", label="two impulses, all the plane change at apogee")
B.plot(dis, best, color=GREEN, lw=2.6, label="two impulses, plane change split best")
B.scatter([28.5], [edelbaum_dv(V0, V1, di)], color=MAROON, s=40, zorder=6)
B.set_xlabel(r"plane change $\Delta i$ [deg]"); B.set_ylabel(r"$\Delta V$ [km/s]"); B.set_ylim(3.5, 8.5); B.set_xlim(0, 60)
B.legend(frameon=False, fontsize=10, loc="upper left"); B.set_title("(b)  LEO (278 km) to GEO: what continuous thrust costs", fontsize=11.5, loc="left"); despine(B)
fig.tight_layout(); fig.savefig("img/fig-ci-edelbaum.png", dpi=150, bbox_inches="tight")
print("ok", edelbaum_dv(V0, V1, di))
