"""Figure for n-body-problem.qmd and tutorial-01.qmd (img/fig-sun-pull-difference.png): the Sun pulls the
Moon harder than the Earth does, yet only the small difference of its pulls on Moon and Earth disturbs
the Moon's motion about the Earth.

Left: the three accelerations at new Moon, to scale (mm/s^2): the Sun on the Earth, the Sun on the Moon,
the Earth on the Moon.  Right: the difference (Sun on Moon minus Sun on Earth) at twelve places round the
Moon's orbit, on a common (much enlarged) scale: stretched along the Sun line (2 mu r / R^3) and squeezed across it (mu r / R^3).

Run from the sfm-slides directory:   python3 codes/fig-sun-pull-difference.py
"""
import numpy as np
import matplotlib.pyplot as plt
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE

GMs, GMe = 1.32712e20, 3.986004e14          # m^3/s^2
AU, rM = 1.495979e11, 3.844e8               # m
aSE = GMs / AU ** 2 * 1e3                   # mm/s^2
aSM = GMs / (AU - rM) ** 2 * 1e3
aEM = GMe / rM ** 2 * 1e3
print(f"Sun on Earth {aSE:.3f}, Sun on Moon (new Moon) {aSM:.3f}, Earth on Moon {aEM:.3f} mm/s^2;"
      f" difference {aSM - aSE:.4f} = {100 * (aSM - aSE) / aEM:.1f}% of the Earth's pull")

fig, (aL, aR) = plt.subplots(1, 2, figsize=(13.5, 5.4), gridspec_kw=dict(width_ratios=[1.15, 1]))
E, M = np.array([0.0, 0.0]), np.array([-1.0, 0.0])         # Sun far to the left; Moon at new Moon
s = 0.28                                                    # drawing units per mm/s^2


def arrow(ax, p, v, col, lab, dy, lw=2.4):
    ax.annotate("", xy=p + v, xytext=p, arrowprops=dict(arrowstyle="-|>", color=col, lw=lw, shrinkA=0, shrinkB=0))
    ax.text(*(p + v / 2 + np.array([0, dy])), lab, color=col, fontsize=10.5, ha="center")


aL.scatter(*E, s=260, color=BLUE, zorder=4); aL.text(0.0, -0.22, "Earth", color=BLUE, ha="center")
aL.scatter(*M, s=80, color="#9a9388", zorder=4); aL.text(-1.0, -0.22, "Moon", color="#6f685e", ha="center")
arrow(aL, E, np.array([-aSE * s, 0]), AMBER, f"Sun on Earth\n{aSE:.2f} mm/s$^2$", 0.12)
arrow(aL, M + np.array([0, 0.45]), np.array([-aSM * s, 0]), AMBER, f"Sun on Moon\n{aSM:.2f} mm/s$^2$", 0.12)
arrow(aL, M + np.array([0, -0.45]), np.array([aEM * s, 0]), GREEN, f"Earth on Moon\n{aEM:.2f} mm/s$^2$", -0.32)
aL.text(-2.55, 0.0, "to the\nSun", color=AMBER, ha="center", va="center", fontsize=10)
aL.annotate("", xy=(-2.85, 0), xytext=(-2.25, 0), arrowprops=dict(arrowstyle="-|>", color=AMBER, lw=1.2))
aL.text(-1.05, -1.05, f"The two amber arrows differ by only {aSM - aSE:.3f} mm/s$^2$:\n"
        f"{100 * (aSM - aSE) / aEM:.1f}% of the Earth's pull on the Moon.", ha="center", fontsize=10)
aL.set_xlim(-3.0, 0.9); aL.set_ylim(-1.3, 1.0); aL.set_aspect("equal"); aL.axis("off")
aL.set_title("At new Moon, to scale (Earth--Moon distance not to scale)", fontsize=11.5)

R = AU
mag = 50 * 0.75 / (2 * GMs * rM / R ** 3)                  # the longest arrow is 0.75 drawing units
th = np.linspace(0, 2 * np.pi, 13)[:-1]
aR.plot(np.cos(np.linspace(0, 2 * np.pi, 200)), np.sin(np.linspace(0, 2 * np.pi, 200)), color="#9a9388", lw=1, ls=":")
aR.scatter(0, 0, s=220, color=BLUE, zorder=4)
for t_ in th:
    rv = rM * np.array([np.cos(t_), np.sin(t_)])
    Sp = np.array([-R, 0.0])                                # Sun position relative to the Earth
    d = GMs * ((Sp - rv) / np.linalg.norm(Sp - rv) ** 3 - Sp / np.linalg.norm(Sp) ** 3)
    p = np.array([np.cos(t_), np.sin(t_)])
    aR.annotate("", xy=p + d * mag / 50, xytext=p, arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=1.8, shrinkA=0, shrinkB=0))
    aR.scatter(*p, s=18, color="#6f685e", zorder=3)
aR.text(0, -1.55, r"along the Sun line: $2\mu_\odot r/R^3=0.030$ mm/s$^2$, outwards"
        "\n" r"across it: $\mu_\odot r/R^3=0.015$ mm/s$^2$, inwards (arrows $\times$ a common scale)",
        ha="center", fontsize=10)
aR.annotate("", xy=(-1.95, 1.3), xytext=(-1.35, 1.3), arrowprops=dict(arrowstyle="-|>", color=AMBER, lw=1.2))
aR.text(-1.65, 1.4, "to the Sun", color=AMBER, ha="center", fontsize=10)
aR.set_xlim(-2.0, 2.0); aR.set_ylim(-1.9, 1.6); aR.set_aspect("equal"); aR.axis("off")
aR.set_title("The difference of the Sun's pulls round the Moon's orbit", fontsize=11.5)
fig.tight_layout()
fig.savefig("img/fig-sun-pull-difference.png", dpi=150, bbox_inches="tight")
print("wrote img/fig-sun-pull-difference.png")
