-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
356 lines (291 loc) · 8.63 KB
/
main.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from modsim import *
import matplotlib
import matplotlib.animation as animation
import platform
import sys
import dill as pickle
from astropy.time import Time
from astropy.coordinates import *
from pdb import set_trace
##############
# Calculations
##############
def linear_slope_func(sun, t, system):
x, y, vx, vy = sun
unpack(system)
return vx, vy, 0, 0
def projectile_slope_func(projectile, t, system):
"""
System, must contain an other_bodies property, which is
an array of dictionaries containing information about each
body, with the following structure:
Body: {
mass: num
radius: num
position_interpolations: dict
x: function
y: function
positions: TimeFrame
x: TimeSeries
y: TimeSeries
}
"""
x, y, vx, vy = projectile
other_bodies = system.other_bodies
G = system.G
pos = Vector(x, y)
acc_net = Vector(0.0,0.0)
for body in other_bodies:
x_body = body["position_interpolations"]['x'](t)
y_body = body["position_interpolations"]['y'](t)
pos_body = Vector(x_body, y_body)
distance = pos.dist(pos_body).m
if distance > body["radius"] and not system.crashed:
acc_net += - (G * body["mass"] / (distance**2)) * (pos-pos_body).hat()
else:
# hit body surface
system.crashed = True
vx = 0.0001
vy = 0.0001
acc_net = Vector(0.0001,0.0001)
return vx, vy, acc_net.x.m, acc_net.y.m
def generate_planet_orbit(x, y, vx, vy, mass, radius, planet_name, sun, system):
"""
Returns a dictionary representing a planet and its trajectory:
Planet: {
mass: num
radius: num
positions: TimeFrame
x: TimeSeries
y: TimeSeries
vx: TimeSeries
vy: TimeSeries
}
"""
new_planet = State(x=x, y=y, vx=vx, vy=vy)
system.init = new_planet
system.other_bodies = [sun]
run_odeint(system, projectile_slope_func)
return {
"mass": mass,
"radius": radius,
"name": planet_name,
"position_interpolations": {
"x": interpolate(system.results.x),
"y": interpolate(system.results.y),
},
"positions": system.results,
}
def generate_planets(system, sun):
filepath = 'build/planets.pickle'
# Regen
if ('regen_planets' in sys.argv):
additional_info = {
"pluto" : {
"mass": np.float64(1.309e22),
"radius": np.float64(1.187e6),
},
"neptune" : {
"mass": np.float64(1.024e26),
"radius": np.float64(24622e3),
},
"uranus" : {
"mass": np.float64(8.681e25),
"radius": np.float64(25362e3),
},
"saturn" : {
"mass": np.float64(5.683e26),
"radius": np.float64(58232e3),
},
"jupiter" : {
"mass": np.float64(1.898e27),
"radius": np.float64(69911e3),
},
"mars" : {
"mass": np.float64(6.39e23),
"radius": np.float64(3390e3),
},
"earth" : {
"mass": np.float64(5.972e24),
"radius": np.float64(6371e3),
},
"venus" : {
"mass": np.float64(4.867e24),
"radius": np.float64(6052e3),
},
"mercury" : {
"mass": np.float64(3.285e23),
"radius": np.float64(2440e3),
}
}
# Get initial starting conditions
with solar_system_ephemeris.set('de432s'):
t = Time('1977-08-20')
planet_names = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune']
planets = []
for planet_name in planet_names:
helio_pos = get_body(planet_name, t).icrs.cartesian
icrs_vel = get_body_barycentric_posvel(planet_name, t)[1]
print(planet_name)
planets.append(generate_planet_orbit(
x = helio_pos.x.si.to_value(),
y = helio_pos.y.si.to_value(),
vx = icrs_vel.x.si.to_value(),
vy = icrs_vel.y.si.to_value(),
mass = additional_info[planet_name]["mass"],
radius = additional_info[planet_name]["radius"],
planet_name = planet_name,
sun = sun,
system = system
))
with open(filepath, 'wb') as file_handle:
pickle.dump(planets, file_handle, pickle.HIGHEST_PROTOCOL)
return planets
# Don't regen, read from pickle
try:
with open(filepath, 'rb') as file_handle:
return pickle.load(file_handle)
except FileNotFoundError as error:
print(f'No planets data saved at {filepath}. Rerun this script passing in `regen_planets` as an argument.')
exit()
def sweep_voyager(vmag, vy0, vyf, num, planets):
filepath = 'build/voyager.pickle'
# Regen
if ('regen_voyager' in sys.argv):
vy = linspace(vy0,vyf, num)
earth = get_body('earth', Time('1977-08-20')).icrs.cartesian
runs = []
for i in range(num):
vx = np.sqrt(vmag**2 - vy[i]**2)
print(f'Computing voyager trajectory #{i}: vx: {vx} vy: {vy[i]}')
voyager_init = State(
x = earth.x.to_value('m'),
y = earth.y.to_value('m') + 6371e3,
vx = vx,
vy = vy[i])
system.init = voyager_init
system.other_bodies = planets +[sun]
run_odeint(system, projectile_slope_func)
runs.append({
"mass": 721,
"radius":20,
"name": "voyager",
"position_interpolations": {
"x": interpolate(system.results.x),
"y": interpolate(system.results.y),
},
"positions": system.results
})
with open(filepath, 'wb') as file_handle:
pickle.dump(runs, file_handle, pickle.HIGHEST_PROTOCOL)
print('Done computing voyager trajectories')
return runs
# Don't regen, read from pickle
try:
with open(filepath, 'rb') as file_handle:
return pickle.load(file_handle)
except FileNotFoundError as error:
print(f'No voyager data saved at {filepath}. Rerun this script passing in `regen_voyager` as an argument.')
exit()
start_year = 1977
end_year = 2018
start_unix = Time(f'{start_year}-01-01').unix
end_unix = Time(f'{end_year}-01-01').unix
duration = end_unix - start_unix
system = System(
init=None,
G=np.float64(6.67408e-11),
ts=linspace(0,duration,10000),
crashed=False
)
sun_frame = TimeFrame({"x": 0, "y": 0, "vx": 0, "vy": 0},[0,1])
sun = {
"mass": 1.989e30,
"radius": 695700e3,
"name": 'sun',
"position_interpolations": {
"x": interpolate(sun_frame.x),
"y": interpolate(sun_frame.y)
},
"positions": sun_frame,
}
planets = generate_planets(system, sun)
runs = sweep_voyager(vmag=44556.31534, vy0=41.0e3, vyf=41.4e3, num=10, planets=planets)
bodies = runs + planets
##########
# Graphing
##########
mode = 'update'
if ('trail' in sys.argv):
mode = 'trail'
if ('update' in sys.argv):
mode = 'update'
# Position
# ========
def radius_transform(radius):
return np.power(radius, 1/4) * 8e8
limit_distance = 5e12
colors = ['#009bdf','#e31d3c','#f47920','#ffc20e','#c0d028','#8ebe3f','#349e49','#26aaa5','#6bc1d3','#7b5aa6','#ed037c','#750324','#00677e'] * 1000
# Setup figure
fig_pos = plt.figure()
fig_pos.set_dpi(100)
fig_pos.set_size_inches(20,20)
plt.title('Gravity Slingshot (position)')
ax = plt.axes(xlim=(-limit_distance,limit_distance), ylim=(-limit_distance,limit_distance))
# Setup modes
if (mode == 'update'):
for idx, body in enumerate(bodies):
color = colors[idx]
positions = body['positions']
circle = plt.Circle((positions.x[0], positions.y[0]), radius_transform(body['radius']), color=color)
line, = plt.plot([], [], color)
ax.add_artist(circle)
ax.add_artist(line)
body['color'] = color
body['artists'] = (circle, line)
if (mode == 'trail'):
for idx, body in enumerate(bodies):
body['color'] = colors[idx]
# Animation
def animate(t, bodies, ax):
if (mode == 'update'):
def _generate(x, y, ax, circle, line):
circle.center = (x, y)
line_x = np.append(line.get_xdata(), x)
line_y = np.append(line.get_ydata(), y)
line.set_data(line_x, line_y)
for body in bodies:
interp = body['position_interpolations']
circle, line = body['artists']
_generate(interp['x'](t), interp['y'](t), ax, circle, line)
return [body['artists'][1] for body in bodies]
if (mode == 'trail'):
def _generate(x, y, radius, ax, color):
ax.add_artist(plt.Circle((x,y), radius, color=color))
for body in bodies:
interp = body['position_interpolations']
_generate(interp['x'](t), interp['y'](t), 1e11, ax, body['color'])
return []
num_frames = 200 if (mode == 'update') else 100
frames = linspace(0,duration, num_frames)
ani = animation.FuncAnimation(fig_pos, animate, frames, fargs=(bodies, ax), interval=200, blit=True)
# Save animation
if (platform.system() == "Darwin"):
ani.save(f'build/slingshot_{mode}.gif', writer='imagemagick')
else:
ani.save(f'build/slingshot_{mode}.mp4', writer='ffmpeg')
fig_pos.savefig('build/position.png')
# Velocity
# --------
def plot_velocity(bodies, name):
fig_v = plt.figure()
fig_v.set_size_inches(20,20)
plt.title(f'Speed ({name})')
for body in bodies:
vx = body['positions'].vx
vy = body['positions'].vy
speed = np.sqrt(vx**2 + vy**2)
plt.plot(speed, color=body['color'])
fig_v.savefig(f'build/speed_{name}.png')
plot_velocity([body for body in bodies if body['name'] != 'voyager'], 'planets')
plot_velocity([body for body in bodies if body['name'] == 'voyager'], 'voyager')