-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanetAlignment.py
More file actions
68 lines (55 loc) · 2.31 KB
/
PlanetAlignment.py
File metadata and controls
68 lines (55 loc) · 2.31 KB
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
from Solar import Solar
class PlanetAlignment(object):
def __init__(self, tolerance = 10, logfile = 'planetAlignment.txt'):
self.sim = Solar('solardata.csv')
self.logfile = logfile
self.tolerance = tolerance
# this function cycles through all the plants and calculates an angle from each, to every other (skipping the sun)
# Then if the angle is less than the tolerance - it continues checking the others - before finally returning True
# if and only if all the angles are below the tolerance
def isAligned(self):
aligned = False
sun = self.sim.getPlanet('Sun')
for p1 in self.sim.planets:
# skip the sun
if p1 == sun:
continue
for p2 in self.sim.planets:
if p2 == sun:
continue
if p1 != p2:
angle = self.sim.getAngleBetween(p2, p1)
# if the angle is less than some value, the they are aligned
# otherwise not
if angle < Solar.degreesToRadians(self.tolerance):
aligned = True
else:
return False
return aligned
# Works -- but quite slow
def runSimulation(self,):
# run simulation for 1 year before checking if planets are aligned - so they're initially misaligned
earth = self.sim.getPlanet('Earth')
year = self.sim.calculateOrbitalPeriods(earth)
while self.sim.time < year:
self.sim.runTimestep()
while not self.isAligned():
self.sim.runTimestep()
self.sim.time = 0
while self.sim.time < year:
self.sim.runTimestep()
while not self.isAligned():
self.sim.runTimestep()
time_of_alignment = self.sim.time / year
# the time until the alignment happens is then logged.
self.logResult(time_of_alignment)
def logResult(self, time):
with open(self.logfile, 'a') as myFile:
message = "The Planets Have Aligned to within " + str(self.tolerance) + " degrees after " + \
str(time) + " years." + '\n'
myFile.write(message)
def __main__():
for s in range(10, 20):
a = PlanetAlignment(s)
a.runSimulation()
__main__()