/
Nbody6.py
73 lines (61 loc) · 2.19 KB
/
Nbody6.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
#from argparse import ArgumentParser #Discomment to pass the variables
import visual as v
import numpy as np
# This version is for visual 6.11
SF = 1e-6
G = C.G * SF ** 3
DT = 3600 * 24
RATE = 50000
scene = v.display(title="Solar System variations", x=0, y=0, z=0,
width=600, height=600, range=1000e3,
background=v.color.black, center=v.vector(0, 0, 0),
forward=(0, -0.3, -1))
class Body(v.sphere):
def __init__(self, mass, pos, vel, i, *arg, **kwargs):
super(Body, self).__init__(*arg, **kwargs)
self.vel = v.vector(vel)
self.pos = v.vector(pos)
self.mass = mass
self.id = i
def c_pos(self, obj, dt=DT):
xm = self.pos + 0.5 * dt * self.vel
self.vel = self.vel + dt * self.c_acel(xm, obj)
self.pos = xm + 0.5 * dt * self.vel
def c_acel(self, xm, obj):
acel = v.vector(0, 0, 0)
for i in obj:
if i.id != self.id:
acel += (i.mass * (v.norm(i.pos - xm) * G) /
(v.mag(i.pos - xm) ** 2))
return acel
def generate(file):
date = np.loadtxt(file, dtype='float')
obj = []
for i in date:
obj.append(Body(i[0], i[1:4], i[4:], len(obj), color=(1, 0, 0),
radius=30, make_trail=True, trail_type="points",
interval=50, retain=1000))
obj[len(obj) - 1].trail_object.size = 1
return obj
parser = ArgumentParser(prog='N-Body', description='This program make the \
simulation of N-Body')
parser.add_argument('-f', '--INPUT', help='File of intput')
parser.add_argument('-sf', '--SCALE_FACTOR', help='Factor of scale for the \
simulation', type=float)
parser.add_argument('-dt', '--INCRASE', help='Incrase of the time', type=float)
parser.add_argument('-r', '--RATE', help='Velocity of simulation', type=float)
args = parser.parse_args()
if args.SCALE_FACTOR:
SF = args.SCALE_FACTOR
if args.INCRASE:
DT = args.INCRASE
if args.INPUT:
obj = generate(args.INPUT)
if args.RATE:
RATE = args.RATE
else:
obj = generate('SSone.ap')
while True:
for i in obj:
i.c_pos(obj, dt=DT)
v.rate(RATE)