"""
Equilibrium tide + rotational flattening of the sea surface, Earth-Moon system
==============================================================================
Single 3D figure with:
- Buttons to switch between Tidal, Total, and Rotation (centrifugal) fields
- Buttons to toggle Opacity (Opaque / Glass) and Reference Frames (0, 1, 2, 3)
- Origins (B and E) and basis vectors (x_i, y_i, z_i) for Frames 0, 1, 2, 3
Run in Google Colab as a single cell (plotly and numpy are preinstalled).
"""
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio
pio.renderers.default = "notebook_connected"
# ───────────────────────── physical constants (SI) ─────────────────────────
G = 6.67430e-11
M_E = 5.9722e24 # Earth mass [kg]
M_m = 7.342e22 # Moon mass [kg]
r0 = 6.371e6 # mean Earth radius [m]
R_EM = 3.844e8 # Earth-Moon distance [m]
Omega = 7.2921159e-5 # Earth spin rate [rad/s]
# ───────────────────────── user parameters ─────────────────────────
EPS_DEG = 23.44 # obliquity: angle between spin axis and orbit normal
THETA1_DEG = 0.0 # orbital phase of the Earth centre shown at start
SPIN_DEG = 30.0 # theta_3: initial Earth rotation phase (non-zero makes Frame 3 distinct from Frame 2)
N_FRAMES = 24 # frames of the theta1 slider/animation
EXAG = {"tidal": 1e6, "total": 30.0, "rotation": 30.0} # radial relief exaggeration
EARTH_OPACITY = 0.45 # initial opacity (<1 shows the CM/orbit inside the Earth)
SHOW_FRAMES_INIT = True # whether Frames 0..3 triads are visible initially
eps = np.radians(EPS_DEG)
theta3 = np.radians(SPIN_DEG)
spin = theta3
# ───────────────────────── derived quantities ─────────────────────────
a_E = R_EM * M_m / (M_E + M_m) # radius of the Earth-centre orbit around the CM
g0 = G * M_E / r0**2
A = 1.5 * (M_m / M_E) * r0**4 / R_EM**3 # coefficient of (x1.R0)^2 [m]
B = 0.5 * Omega**2 * r0**4 / (G * M_E) # coefficient of cos^2(lat) [m]
# ───────────────────────── reference frames (0, 1, 2, 3) ─────────────────────────
# Frame (0): inertial frame centered at B (CM)
X0 = np.array([1.0, 0.0, 0.0])
Y0 = np.array([0.0, 1.0, 0.0])
Z0 = np.array([0.0, 0.0, 1.0])
def frame1_basis(theta1):
"""Frame (1): centered at B, rotated by theta1 around z0."""
c1, s1 = np.cos(theta1), np.sin(theta1)
x1 = c1 * X0 + s1 * Y0
y1 = -s1 * X0 + c1 * Y0
z1 = Z0.copy()
return x1, y1, z1
def frame2_basis():
"""Frame (2): centered at E, tilted by obliquity eps around y0."""
ce, se = np.cos(eps), np.sin(eps)
x2 = ce * X0 - se * Z0
y2 = Y0.copy()
z2 = se * X0 + ce * Z0
return x2, y2, z2
def frame3_basis(th3=theta3):
"""Frame (3): centered at E, rotated by theta3 around z2."""
x2, y2, z2 = frame2_basis()
c3, s3 = np.cos(th3), np.sin(th3)
x3 = c3 * x2 + s3 * y2
y3 = -s3 * x2 + c3 * y2
z3 = z2.copy()
return x3, y3, z3
EZ, EA, EB = frame2_basis()[2], frame2_basis()[0], frame2_basis()[1]
def R0_hat(theta, lat):
"""Unit vector (inertial frame) of the surface point (theta, lat)."""
theta, lat = np.broadcast_arrays(theta, lat)
return ((np.cos(lat) * np.cos(theta))[..., None] * EA
+ (np.cos(lat) * np.sin(theta))[..., None] * EB
+ np.sin(lat)[..., None] * EZ)
def x1_hat(theta1):
return frame1_basis(theta1)[0]
def cos_psi(theta1, theta, lat):
"""x1 . R0 in closed form."""
return (np.cos(theta1) * (np.cos(eps) * np.cos(lat) * np.cos(theta)
+ np.sin(eps) * np.sin(lat))
+ np.sin(theta1) * np.cos(lat) * np.sin(theta))
# ───────────────────────── fields ─────────────────────────
def zeta_tidal(theta1, theta, lat):
return A * (cos_psi(theta1, theta, lat) ** 2 - 1.0 / 3.0)
def zeta_rot(lat):
return B * (np.cos(lat) ** 2 - 2.0 / 3.0)
def zeta_full(theta1, theta, lat, zeta0):
return zeta0 + A * cos_psi(theta1, theta, lat) ** 2 + B * np.cos(lat) ** 2
def sphere_mean(f, n=64, m=128):
x, w = np.polynomial.legendre.leggauss(n)
lat = np.arcsin(x)
lon = np.linspace(0, 2 * np.pi, m, endpoint=False)
LON, LAT = np.meshgrid(lon, lat)
return np.sum(f(LON, LAT) * w[:, None]) / (w.sum() * m)
def find_zeta0(theta1):
return -sphere_mean(lambda th, la: zeta_full(theta1, th, la, 0.0))
# ───────────────────────── helper: sub-lunar point ─────────────────────────
def wrap180(x):
return (x + 180.0) % 360.0 - 180.0
def sublunar(theta1):
m = -x1_hat(theta1)
lat_s = np.degrees(np.arcsin(m @ EZ))
L_s = wrap180(np.degrees(np.arctan2(m @ EB, m @ EA)) - SPIN_DEG)
return [L_s, wrap180(L_s + 180.0)], [lat_s, -lat_s]
def add_controls(fig, names, labels, prefix="day = "):
anim = dict(frame=dict(duration=120, redraw=True), transition=dict(duration=0))
fig.update_layout(
updatemenus=[dict(type="buttons", direction="left", x=0.0, y=-0.02, xanchor="left", yanchor="top",
buttons=[dict(label="Play", method="animate", args=[None, dict(anim, fromcurrent=True)]),
dict(label="Pause", method="animate",
args=[[None], dict(mode="immediate", frame=dict(duration=0, redraw=False),
transition=dict(duration=0))])])],
sliders=[dict(active=0, x=0.12, len=0.85, y=-0.02, yanchor="top",
currentvalue=dict(prefix=prefix),
steps=[dict(method="animate", label=lb,
args=[[nm], dict(mode="immediate", frame=dict(duration=0, redraw=True),
transition=dict(duration=0))])
for nm, lb in zip(names, labels)])])
theta1_list = np.radians(THETA1_DEG) + 2 * np.pi * np.arange(N_FRAMES) / N_FRAMES
names = [str(k) for k in range(N_FRAMES)]
labels = ["%.0f deg" % (np.degrees(t) % 360) for t in theta1_list]
# ═════════════════════════ 2D maps ═════════════════════════
lon_d = np.linspace(-180, 180, 181)
lat_d = np.linspace(-90, 90, 91)
LON2, LAT2 = np.meshgrid(np.radians(lon_d), np.radians(lat_d))
def maps2d(theta1):
th = LON2 + spin
z0 = find_zeta0(theta1)
return zeta_full(theta1, th, LAT2, z0), zeta_tidal(theta1, th, LAT2)
tot_all = [maps2d(t)[0] for t in theta1_list]
lim_tot = max(np.abs(z).max() for z in tot_all)
lim_tid = 2 * A / 3
fig2d = make_subplots(rows=2, cols=1, vertical_spacing=0.12,
subplot_titles=("Total zeta(theta, lat) [m]", "Tidal part only [m]"))
def frame2d(k):
Zt, Zd = maps2d(theta1_list[k])
Ls, Bs = sublunar(theta1_list[k])
mk = dict(size=10, color="gold", line=dict(color="black", width=1.5), symbol="star")
return [go.Heatmap(x=lon_d, y=lat_d, z=np.round(Zt, 4), zmin=-lim_tot, zmax=lim_tot, colorscale="RdBu_r",
colorbar=dict(title="m", x=1.0, y=.75, len=0.42),
hovertemplate="lon %{x:.0f} lat %{y:.0f}<br>zeta = %{z:.3f} m<extra></extra>"),
go.Heatmap(x=lon_d, y=lat_d, z=np.round(Zd, 4), zmin=-lim_tid, zmax=lim_tid, colorscale="RdBu_r",
colorbar=dict(title="m", x=1.0, y=.25, len=0.52),
hovertemplate="lon %{x:.0f} lat %{y:.0f}<br>zeta_tide = %{z:.3f} m<extra></extra>"),
go.Scatter(x=Ls, y=Bs, mode="markers", marker=mk, name="sub-lunar / antipode", showlegend=True),
go.Scatter(x=Ls, y=Bs, mode="markers", marker=mk, showlegend=False)]
d0 = frame2d(0)
fig2d.add_trace(d0[0], row=1, col=1)
fig2d.add_trace(d0[1], row=2, col=1)
fig2d.add_trace(d0[2], row=1, col=1)
fig2d.add_trace(d0[3], row=2, col=1)
fig2d.frames = [go.Frame(name=names[k], data=frame2d(k), traces=[0, 1, 2, 3]) for k in range(N_FRAMES)]
for c in (1, 2):
fig2d.update_xaxes(title_text="geographic longitude [deg]", range=[-180, 180], row=c, col=1)
fig2d.update_yaxes(title_text="latitude [deg]", range=[-90, 90], row=c, col=1)
fig2d.update_layout(height=800, width=600, margin=dict(b=90),
title="Equilibrium sea surface (eps = %.2f deg, spin = %.0f deg)" % (EPS_DEG, SPIN_DEG))
add_controls(fig2d, names, labels)
fig2d.show()