-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
149 lines (126 loc) · 3.96 KB
/
sim.py
File metadata and controls
149 lines (126 loc) · 3.96 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Simple Solar System Simulation: User interface module
"""
from model import planets, timestep
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from astropy.time import Time, TimeDelta
from numpy.linalg import norm
units = []
units.append({"name": "UA", "scale": 149597870700.0})
units.append({"name": "m", "scale": 1.0})
class Controller:
def __init__(self):
self.datetime = Time.now()
self.dt = 3600 * 24
self.pause = True
self.show_labels = True
self.show_legend = True
self.draw = False
self.unit = 0
def on_press(self, event):
one_day = 3600 * 24
if event.key.isspace():
self.pause ^= True
elif event.key == "enter":
self.show_labels ^= True
elif event.key == "t":
self.show_legend ^= True
elif event.key == "d":
self.draw ^= True
elif event.key == "u":
self.unit = (self.unit + 1) % len(units)
r = get_viz_r()
ax.set_xlim([-1.1 * r, 1.1 * r])
ax.set_ylim([-1.1 * r, 1.1 * r])
elif event.key == "left":
self.dt -= one_day / 10.0
elif event.key == "down":
self.dt -= one_day
elif event.key == "right":
self.dt += one_day / 10.0
elif event.key == "up":
self.dt += one_day
if self.dt >= 30 * one_day:
self.dt = 30 * one_day
if self.dt < 0:
self.dt = 0
def update(i, lines):
for t in ax.texts:
t.remove()
if ctrl.show_labels:
ax.text(0, 0, "sun")
dt = ctrl.dt
if ctrl.pause:
dt = 0
timestep(dt)
ctrl.datetime = ctrl.datetime + TimeDelta(dt, format="sec")
n = 1
lines[0].set_data(0, 0)
vel = f"{'sun':7}: {0:4.2e} m/s"
lines[0].set_label(vel)
for name in planets.keys():
if ctrl.draw:
xs, ys = lines[n].get_data()
else:
xs = []
ys = []
xs.append(planets[name].x[0] / units[ctrl.unit]["scale"])
ys.append(planets[name].x[1] / units[ctrl.unit]["scale"])
lines[n].set_data(xs, ys)
if ctrl.draw:
lines[n].set_linestyle("-")
lines[n].set_marker(",")
else:
lines[n].set_linestyle("")
lines[n].set_marker(".")
vel = f"{name:7}: {norm(planets[name].v):4.2e} m/s"
lines[n].set_label(vel)
if ctrl.show_labels:
t = ax.text(
planets[name].x[0] / units[ctrl.unit]["scale"],
planets[name].x[1] / units[ctrl.unit]["scale"],
name,
)
n += 1
date = ctrl.datetime.to_value("iso", subfmt="date")
plt.title(f"Date: {date}, dt: {ctrl.dt/24/3600:.2f} days")
ax.set_xlabel(units[ctrl.unit]["name"])
ax.set_ylabel(units[ctrl.unit]["name"])
if ctrl.show_legend:
ax.legend(
loc="lower center", prop={"family": "monospace"}, ncol=3,
)
elif ax.get_legend():
ax.get_legend().remove()
return lines
def get_viz_r():
r = 0
for p in planets.keys():
pr = norm(planets[p].x) / units[ctrl.unit]["scale"]
if r < pr:
r = pr
return r
if __name__ == "__main__":
plt.style.use("dark_background")
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot()
ax.set_xlabel("AU")
ax.set_ylabel("AU")
ctrl = Controller()
lines = []
(line,) = ax.plot([0], [0], ".", color="y", label="sun")
texts = ax.text(0, 0, "sun")
lines.append(line)
for name in planets.keys():
(line,) = ax.plot(planets[name].x, ".", label=name)
texts = ax.text(0, 0, name)
lines.append(line)
r = get_viz_r()
ax.set_xlim([-1.1 * r, 1.1 * r])
ax.set_ylim([-1.1 * r, 1.1 * r])
fig.canvas.mpl_connect("key_press_event", ctrl.on_press)
ani = FuncAnimation(
fig, update, blit=False, interval=1, repeat=False, fargs=(lines,)
)
plt.show()