Skip to content

Instantly share code, notes, and snippets.

@emwdx
Created November 13, 2012 10:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emwdx/4065070 to your computer and use it in GitHub Desktop.
Save emwdx/4065070 to your computer and use it in GitHub Desktop.
Earth-Moon orbit simulator
from math import *
#All distance units are in m, velocity in m/s
#Enter initial simulation data here
initial_x = -6.38E6
initial_y = 12E6
initial_vx = 1.1E4 #-6.38E6,12E6,1.1E4,3.5E3
initial_vy = 3.5E3
M = 50 #satellite mass
end_time = 10000 #This is how many seconds you want the simulation to go.
#Data about Earth and the Moon goes here:
Earth = [0.0,0.0]
Moon = [385E6,0]
mE = 6.0E24
mM = 7.0E22
rE = 6.38E6
rM = 1.738E6
G = 6.67E-11
Fburn = 3850 #F = 3850, start 43080, end 43180
startBurn = 43080
endBurn = 43180
#Simulation data goes here
t = 0
delta_t = 1
crashed = False
data = []
data.append([initial_x,initial_y,initial_vx,initial_vy,crashed])
i = 0
def isBurnTime(startBurn,endBurn):
if(t>=startBurn and t<=endBurn):
return 1
else:
return 0
while (t<end_time and crashed == False):
isBurning = isBurnTime(startBurn,endBurn)
dE = sqrt((data[i][0] - Earth[0])**2+(data[i][1] - Earth[1])**2)
dM = sqrt((data[i][0] - Moon[0])**2+(data[i][1] - Moon[1])**2)
Fe = G*M*mE*1./(dE**2)
Fm = G*M*mM*1./(dM**2)
speed = sqrt(data[i][2]**2+data[i][3]**2)
accel_x = 1./M*(-1*Fe*(data[i][0]-Earth[0])*1./dE -Fm*(data[i][0]-Moon[0])*1./dM - isBurning*Fburn*data[i][2]*1./speed)
accel_y = 1./M*(-1*Fe*(data[i][1]-Earth[1])*1./dE -Fm*(data[i][1]-Moon[1])*1./dM - isBurning*Fburn*data[i][3]*1./speed)
v_x = data[i][2]+accel_x*delta_t
v_y = data[i][3]+accel_y*delta_t
x = data[i][0]+ data[i][2]*delta_t+0.5*accel_x*delta_t**2
y = data[i][1] + data[i][3]*delta_t + 0.5*accel_y*delta_t**2
if(dE<rE or dM<rM):
crashed = True
print "Crash!"
data.append([x,y,v_x,v_y,crashed])
i = i + 1
t+=delta_t
print "Completed calculations."
outFile = open('output.txt','w')
time_int = 600
count = 0
for row in data:
count+=1
if(count>=time_int):
count = 0
new_x = row[0]*1./(1.0E6)
new_y = row[1]*1./(1.0E6)
output = str(new_x)+','+str(new_y)+'\n'
outFile.write(output)
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment