Last active
February 8, 2022 04:39
-
-
Save Veedrac/08c3d6935c08d6722dd8f102eaccd212 to your computer and use it in GitHub Desktop.
Very roughly simulate whether SpaceX could hop Starship from Boca Chica to 39B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from numpy.linalg import norm | |
import math | |
earth_radius = 6.3781e6 | |
gravity = 9.81 | |
def lla2cart(lat, long, alt=earth_radius): | |
lat, long = math.radians(lat), math.radians(long) | |
x = math.cos(long) * math.cos(lat) | |
y = math.sin(long) * math.cos(lat) | |
z = math.sin(lat) | |
return numpy.array([x, y, z]) * alt | |
def cart2lla(x, y, z): | |
alt = norm([x, y, z]) | |
lat = math.degrees(math.asin(z / alt)) | |
long = math.degrees(math.atan2(y, x)) | |
return lat, long, alt | |
def sim(pos, vel, dt, *, impulse=0): | |
pos = pos.copy() | |
vel = vel.copy() | |
while True: | |
yield pos, vel | |
vel -= gravity * dt * pos / norm(pos) | |
vel += impulse * dt | |
pos += vel * dt | |
def sim_landing_point(pos, vel, dt): | |
for pos, vel in sim(pos, vel, dt): | |
if norm(pos) + 1 < earth_radius: | |
return pos | |
def fmt_lla(lat, long, alt): | |
return f"{{ {lat:>6.3f} {long:>7.3f} {alt-earth_radius:>6.0f} }}" | |
def fmt_ll(lat, long, _alt): | |
return f"{{ {lat:>6.3f} {long:>7.3f} }}" | |
start = lla2cart(25.997407091653063, -97.15718040356279) | |
vel = numpy.array([0.0, 0.0, 0.0]) | |
impulse = numpy.array([0.0, 0.0, 0.0]) | |
dry_mass = 126_000 | |
propellant_mass = 1200_000 | |
targets = [ | |
lla2cart(24.2172687156, -82.2451010924), | |
lla2cart(24.1741747933, -81.4646902746), | |
lla2cart(24.4521235059, -80.4662804471), | |
lla2cart(25.1690508346, -79.7467954413), | |
lla2cart(26.3645084546, -79.5654348773), | |
lla2cart(27.2460928840, -79.7122031701), | |
lla2cart(28.6002368345, -80.4351409697), | |
] | |
targets_boost = {7, 6, 5, 4} | |
t = 0 | |
dt = 0.01 | |
skipcheck = 1 | |
for i, (pos, vel) in enumerate(sim(start, vel, dt, impulse=impulse)): | |
if targets and not i % skipcheck: | |
landing_point = sim_landing_point(pos, vel, dt) | |
if targets and propellant_mass > 0: | |
# 1800 kN / engine | |
accel = 1800_000 * 9 / (dry_mass + propellant_mass) | |
# momentum/engine = 1800 kN * dt = 3300 m/s * M kg/s * dt | |
# M kg/s ~ 545 kg/s | |
propellant_mass -= 545 * 9 * dt | |
up_dir = pos / norm(pos) | |
target_dir = targets[0] - landing_point | |
target_dir -= up_dir * (up_dir @ target_dir) | |
target_dir /= norm(target_dir) | |
if len(targets) in targets_boost: | |
# up_strength = 0.5 + 245250./(-98100 + math.sqrt(-3.84944e10 + 5.e8 * accel**2)) | |
up_strength = 0.6 + 266832./(-117720 + math.sqrt(-3.84944e10 + 5.44e8 * accel**2)) | |
# up_strength = 0.7 + 292338./(-137340 + math.sqrt(-3.84944e10 + 5.96e8 * accel**2)) | |
# up_strength = 0.8 + 321768/(-156960 + math.sqrt(-3.84944e10 + 6.56e8 * accel**2)) | |
else: | |
up_strength = 0 | |
dir = up_dir * up_strength + target_dir | |
dir /= norm(dir) | |
impulse[:] = accel * dir | |
else: | |
impulse[:] = 0 | |
lat, long, alt = pos_lla = cart2lla(*pos) | |
if alt + 0.01 < earth_radius: | |
break | |
if targets: | |
delta = norm(landing_point - targets[0]) | |
skipcheck = 2 ** math.ceil(math.log2(100 * delta / (10_000_000 * dt))) | |
if delta < (100_000 * dt): # chosen by trial and error | |
targets.pop(0) | |
i = 0 | |
if not i % int(1 / dt): | |
print(f"{t:>5.1f} :: {fmt_lla(*pos_lla)} → {fmt_ll(*cart2lla(*landing_point))} :: {norm(vel):>4.0f} m/s {propellant_mass/1000:>4.0f}t T{len(targets)} :: DEBUG {delta:>8.0f} {accel:>3.2f} {up_strength:>3.2f} {accel * up_strength:>3.2f} {vel @ pos / norm(pos):>4.0f} {skipcheck}") | |
t += dt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Format: