-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit3.py
62 lines (50 loc) · 1.54 KB
/
orbit3.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
## This can be run from a python shell by typing
## import orbit
## or from unix command line with
## python orbit.py
from visual import *
## By default, "visual" creates a 3-D object called scene
scene.autoscale=0
scene.range=2
## create three objects, set their initial
## position, radius, color, and other
## properties: mass, momentum("p")
giant = sphere()
giant.pos = vector(-0.201,0.020,0)
giant.radius = 0.075 ; giant.color = color.red
giant.mass = 1
giant.p = vector(0, 0.051, -0.01) * giant.mass
dwarf = sphere()
dwarf.pos = vector(1.5,0,0)
dwarf.radius = 0.056 ; dwarf.color = color.yellow
dwarf.mass = 0.125
dwarf.p = -giant.p
moon = sphere()
moon.pos = vector(0,0.5,0.5)
moon.radius = 0.04 ; moon.color = color.cyan
moon.mass = 0.00125
moon.p = 0.035 * dwarf.p
## tweak initial condition so that total momentum is zero
giant.p -= moon.p
## create 'curve' objects showing where we've been
for a in [giant, dwarf, moon]:
a.orbit = curve(color=a.color, radius = 0.01)
def pstep( giant, dwarf ):
dist = dwarf.pos - giant.pos
force = G * giant.mass * dwarf.mass * dist / mag(dist)**3
giant.p = giant.p + force*dt
dwarf.p = dwarf.p - force*dt
dist = dwarf.pos - giant.pos
dt = 0.01
G = 1
while 1:
## set the picture update rate (100 times per second)
rate(100)
pstep( giant, dwarf )
pstep( giant, moon )
pstep( moon, dwarf )
for a in [giant, dwarf, moon]:
a.pos = a.pos + a.p/a.mass * dt
a.orbit.append(pos=a.pos)
## For an intro to visual python see
## http://wiki.aims.ac.za/mediawiki/index.php/Vpython:Getting_Started