-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwobody.py
executable file
·80 lines (67 loc) · 2.65 KB
/
twobody.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
import numpy
G = 6.67408*10**-11
step = 2000
endtime = 23681400
# UNITS KM, KG, N, S
class Body(object):
def __init__(self, Pos0, mass0, V0, A0, R0, F0=[0,0]):
self.Pos = Pos0
self.mass = mass0
self.V = V0
self.A = A0
self.R = R0
self.F = F0
def __call__(self):
print self.Pos, self.V, self.A, self.F
def updateAcc(self):
self.A = numpy.divide(self.F, self.mass)/1000
def updateVel(self, time):
self.V = [self.V[0] + self.A[0]*time, self.V[1] + self.A[1]*time]
def updatePos(self, time):
self.Pos = [self.Pos[0] + self.V[0]*time, self.Pos[1] + self.V[1]*time]
def Move(self, time):
self.updateAcc()
self.updateVel(time)
self.updatePos(time)
def DistBetween(body1, body2):
return math.sqrt((body1.Pos[0]-body2.Pos[0])**2+(body1.Pos[1]-body2.Pos[1])**2)
def PosUnitDirVct(body1, body2):
return numpy.divide([body2.Pos[0]-body1.Pos[0], body2.Pos[1]-body1.Pos[1]], DistBetween(body1, body2))
def SpdUnitDirVct(body1):
return numpy.divide(body1.V, math.sqrt(body1.V[0]**2+body1.V[1]**2))
def FgravMag(body1, body2):
return G*body1.mass*body2.mass/(DistBetween(body1, body2)*10**3)**2
def FgravVct(body1, body2):
return numpy.multiply(PosUnitDirVct(body1, body2), FgravMag(body1, body2))
Earth = Body([0,0], 5.97237*10**24, [-0.01185,0], [0,0], 6371)
Moon = Body([0,405400], 7.341*10**22, [0.963553002013,0], [0,0], 1737)
Moonposplot = [Moon.Pos]
Earthposplot = [Earth.Pos]
for t in numpy.linspace(0,endtime,endtime/step):
Moon.F = FgravVct(Moon, Earth)
Earth.F = FgravVct(Earth, Moon)
Moon.Move(step)
Earth.Move(step)
Moonposplot.append(Moon.Pos)
Earthposplot.append(Earth.Pos)
Ecircle = plt.Circle((Earth.Pos[0], Earth.Pos[1]), Earth.R, color='g')
Mcircle = plt.Circle((Moon.Pos[0], Moon.Pos[1]), Moon.R, color='grey')
fig = plt.figure()
ax = plt.axes(xlim=(-415.4*10**3, 415.4*10**3),ylim=(-415.4*10**3, 415.4*10**3))
Mline, = ax.plot(map(list, zip(*Moonposplot))[0],map(list, zip(*Moonposplot))[1], lw=0.5)
Eline, = ax.plot(map(list, zip(*Earthposplot))[0],map(list, zip(*Earthposplot))[1], lw=0.5)
def init():
ax.add_artist(Ecircle)
ax.add_artist(Mcircle)
return Mcircle, Ecircle
def anim(i):
Mcircle.center = (Moonposplot[i][0], Moonposplot[i][1])
Ecircle.center = (Earthposplot[i][0], Earthposplot[i][1])
return Mcircle, Ecircle, Mline, Eline
ani = animation.FuncAnimation(fig, anim, init_func=init, frames=int(endtime/step), interval=1, blit=True)
plt.show()