This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBody.py
56 lines (49 loc) · 1.79 KB
/
Body.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
'''
Object describes a celestial body that interacts with other bodies
by gravity
'''
import numpy as np
class Body(object):
def __init__(self, readFromFile, filename):
if readFromFile:
self.readInfo(filename)
#CurrForce gets added later
#Dimention of Force vector is same as dimention of
#position vector, same with acceleration and potential
self.force = np.zeros(self.pos.size, dtype=float)
self.acc = np.zeros(self.pos.size, dtype=float)
self.pot = 0.
self.prevAcc = self.acc
#retruns unsigned angle from origin in the usual convention
def angle(self):
angle = np.arctan2(self.pos[1], self.pos[0])
if angle < 0.:
angle = 2.*np.pi + angle
return angle
#reads in initial info for a body
def readInfo(self, filename):
filein = open(filename, "r")
lines = filein.readlines()
#list of lines without the comment lines
noCommentLines = []
for i in lines:
if i[0] != "#":
noCommentLines.append(i)
for i in range(len(noCommentLines)):
#splits the line on ": " disregards the first bit
tokens = noCommentLines[i].split(": ")
#removes trailing \n and whitespace
data = tokens[1].rstrip()
if i == 0:
self.name = str(data)
elif i == 1:
self.mass = float(data)
elif i == 2:
self.pos = np.fromstring(data, dtype=float, sep=',')
elif i == 3:
self.vel = np.fromstring(data, dtype=float, sep=',')
elif i == 4:
self.radius = float(data)
elif i == 5:
self.colour = str(data)
filein.close()