-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimAnimate.py
285 lines (214 loc) · 11.3 KB
/
SimAnimate.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
# # -*- coding: utf-8 -*-
# """
# Created on Fri Feb 2 12:09:32 2024
# @author: ryanw
# """
# import matplotlib
# matplotlib.use('Agg')
# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib import animation
# import time
# # plt.ioff()
# folder = 'OUTPUT_M8-f0.50-a0.010'
# pos_file = f'{folder}/positions.txt'
# positions_data = np.genfromtxt(pos_file)
# positions_data[:, 2] *= 1e8
# hash1 = positions_data[1, 1]
# hash2 = positions_data[0, 1]
# m1, m2 = positions_data[1, 2], positions_data[0, 2]
# # hash1 = positions_data[6, 1]
# # hash2 = positions_data[7, 1]
# particle1_txyz = positions_data[:, [0, 3, 4, 5]][np.where(positions_data[:, 1] == hash1)]
# particle2_txyz = positions_data[:, [0, 3, 4, 5]][np.where(positions_data[:, 1] == hash2)]
# tmin = 35000
# tmax = 37000
# # tmin = 0
# # tmax = 2000
# particle1_txyz = particle1_txyz[np.where((particle1_txyz[:, 0] <= tmax) & (particle1_txyz[:, 0] >= tmin))]
# particle2_txyz = particle2_txyz[np.where((particle2_txyz[:, 0] <= tmax) & (particle2_txyz[:, 0] >= tmin))]
# if particle1_txyz.shape != particle2_txyz.shape:
# raise RuntimeError("Particle position arrays are different shapes!")
# # particle2_txyz -= particle1_txyz
# # SMBH_txyz = - particle1_txyz
# # particle1_txyz = np.zeros(particle1_txyz.shape)
# # # particle2_txyz -= particle1_txyz
# # SMBH_txyz = np.zeros(particle1_txyz.shape)
# # # particle1_txyz =
# # particle1_txyz -= particle1_txyz
# # particle2_txyz -= particle1_txyz
# SMBH_txyz = np.zeros(particle1_txyz.shape)
# mean_pos = (m1 * particle1_txyz + m2 * particle2_txyz) / (m1 + m2)
# positions = np.zeros((len(particle1_txyz[:, 0]), 4, 2))
# positions[:, :, 0] = particle1_txyz
# positions[:, :, 1] = particle2_txyz
# # positions[:, :, 2] = SMBH_txyz - particle1_txyz
# scales = [50, 30]
# # scales = [50, 30, 100]
# fig = plt.figure(figsize=(12, 12), frameon=False) # we want no frame so that it's a clean black animation
# ax = fig.add_subplot(projection='3d') # 3d axes
# fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None) # removes (most of the) blank border around the plot
# # now do an initial scatter so we can get the axis limits to use through the animation
# particles = ax.scatter(positions[0, 1, :], positions[0, 2, :], positions[0, 3, :], s=scales, marker='.', c='k')
# xmin, xmax = min(positions[:, 1, :].flatten()), max(positions[:, 1, :].flatten())
# ymin, ymax = min(positions[:, 2, :].flatten()), max(positions[:, 2, :].flatten())
# zmin, zmax = min(positions[:, 3, :].flatten()), max(positions[:, 3, :].flatten())
# limmin, limmax = min([xmin, ymin, zmin]), max([xmax, ymax, zmax]) # get the minimum and maximums for the axis limits
# every = 1
# length = 500
# # now calculate some parameters for the animation frames and timing
# nt = len(positions[:, 0, 0]) # number of timesteps
# frames = np.arange(0, nt, every) # iterable for the animation function. Chooses which frames (indices) to animate.
# fps = len(frames) // length # fps for the final animation
# # ax.set_facecolor('k') # black background, since space is blach duh
# theta = np.linspace(0, 2*np.pi, 360)
# r1 = np.sqrt(sum(particle1_txyz[0, 1:]**2))
# r2 = np.sqrt(sum(particle2_txyz[0, 1:]**2))
# Zzero = np.zeros(len(theta))
# circle1 = ax.plot(r1 * np.cos(theta), r1 * np.sin(theta), Zzero, lw=1, c='tab:blue')[0]
# circle2 = ax.plot(r1 * np.cos(theta), r1 * np.sin(theta), Zzero, lw=1, c='tab:red')[0]
# # def animate(i):
# # if (i // every)%20 == 0:
# # print(f"{i // every} / {len(frames)}")
# # ax.clear()
# # ax.scatter(positions[i, 1, :], positions[i, 2, :], positions[i, 3, :], s=scales, marker='.', c='w')
# # ax.set_xlim(limmin, limmax); ax.set_ylim(limmin, limmax); ax.set_zlim(limmin, limmax)
# # # if times[0]: # plot the current time in the corner if we want to!
# # # ax.text(0.7 * limmax, 0.9 * limmax, 0, "$T = " + str(round(times[1][i], 2)) + "$", fontsize=24, color='w')
# # ax.grid(False)
# # ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# # ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# # ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# # ax.elev = 90 # sets the viewing angle to be top-down
# # return fig,
# ax.grid(False)
# ax.set_axis_off()
# ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.elev = 90 # sets the viewing angle to be top-down
# ax.set_xlim(limmin, limmax); ax.set_ylim(limmin, limmax); ax.set_zlim(limmin, limmax)
# def animate(i):
# if (i // every)%20 == 0:
# print(f"{i // every} / {len(frames)}")
# particles._offsets3d = (positions[i, 1, :], positions[i, 2, :], positions[i, 3, :])
# r1 = np.sqrt(sum(particle1_txyz[i, 1:]**2)); r2 = np.sqrt(sum(particle2_txyz[i, 1:]**2))
# circle1.set_data_3d(r1 * np.cos(theta), r1 * np.sin(theta), Zzero)
# circle2.set_data_3d(r2 * np.cos(theta), r2 * np.sin(theta), Zzero)
# return (particles, circle1, circle2)
# ani = animation.FuncAnimation(fig, animate, frames=frames, blit=True, repeat=False)
# ani.save(f"{folder}/animation.mp4", writer='ffmpeg', fps=fps)
# plt.close('all')
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 2 12:09:32 2024
@author: ryanw
"""
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import time
# plt.ioff()
folder = 'OUTPUT_M8-f0.50-a0.010'
pos_file = f'{folder}/positions.txt'
positions_data = np.genfromtxt(pos_file)
positions_data[:, 2] *= 1e8
hash1 = positions_data[1, 1]
hash2 = positions_data[0, 1]
m1, m2 = positions_data[1, 2], positions_data[0, 2]
# hash1 = positions_data[6, 1]
# hash2 = positions_data[7, 1]
particle1_txyz = positions_data[:, [0, 3, 4, 5]][np.where(positions_data[:, 1] == hash1)]
particle2_txyz = positions_data[:, [0, 3, 4, 5]][np.where(positions_data[:, 1] == hash2)]
tmin = 35000
tmax = 39000
# tmin = 0
# tmax = 2000
particle1_txyz = particle1_txyz[np.where((particle1_txyz[:, 0] <= tmax) & (particle1_txyz[:, 0] >= tmin))]
particle2_txyz = particle2_txyz[np.where((particle2_txyz[:, 0] <= tmax) & (particle2_txyz[:, 0] >= tmin))]
if particle1_txyz.shape != particle2_txyz.shape:
raise RuntimeError("Particle position arrays are different shapes!")
particle1_spherical, particle2_spherical = np.zeros(particle1_txyz.shape), np.zeros(particle1_txyz.shape)
particle1_spherical[:, 0] = particle1_txyz[:, 0]
particle1_spherical[:, 1] = np.sqrt(sum([particle1_txyz[:, i]**2 for i in [1, 2, 3]]))
particle1_spherical[:, 2] = np.sign(particle1_txyz[:, 2]) * np.arccos(particle1_txyz[:, 1] / np.sqrt(sum([particle1_txyz[:, i]**2 for i in [1, 2]])))
particle2_spherical[:, 0] = particle2_txyz[:, 0]
particle2_spherical[:, 1] = np.sqrt(sum([particle2_txyz[:, i]**2 for i in [1, 2, 3]]))
particle2_spherical[:, 2] = np.sign(particle2_txyz[:, 2]) * np.arccos(particle2_txyz[:, 1] / np.sqrt(sum([particle2_txyz[:, i]**2 for i in [1, 2]])))
# particle2_spherical[:, 2] -= particle1_spherical[:, 2]
# particle1_spherical[:, 2] = 0
from astropy.stats import circmean
angle_arr = np.array([particle1_spherical[:, 2], particle2_spherical[:, 2]])
weights = np.zeros(angle_arr.shape)
weights[0, :] = m1 / (m1 + m2); weights[1, :] = m2 / (m1 + m2)
# weighted_angular_shift = (m1 * particle1_spherical[:, 2] + m2 * particle2_spherical[:, 2]) / (2 * (m1 + m2))
weighted_angular_shift = circmean(angle_arr, axis=0, weights=weights)
particle2_spherical[:, 2] -= weighted_angular_shift
particle1_spherical[:, 2] -= weighted_angular_shift
particle1_txyz[:, 1] = particle1_spherical[:, 1] * np.cos(particle1_spherical[:, 2])
particle1_txyz[:, 2] = particle1_spherical[:, 1] * np.sin(particle1_spherical[:, 2])
particle2_txyz[:, 1] = particle2_spherical[:, 1] * np.cos(particle2_spherical[:, 2])
particle2_txyz[:, 2] = particle2_spherical[:, 1] * np.sin(particle2_spherical[:, 2])
SMBH_txyz = np.zeros(particle1_txyz.shape)
positions = np.zeros((len(particle1_txyz[:, 0]), 4, 3))
positions[:, :, 0] = particle1_txyz
positions[:, :, 1] = particle2_txyz
positions[:, :, 2] = SMBH_txyz
scales = [200 * m1/(m1 + m2), 200 * m2/(m1 + m2), 1000]
# scales = [50, 30, 100]
fig = plt.figure(figsize=(12, 12), frameon=False) # we want no frame so that it's a clean black animation
ax = fig.add_subplot(projection='3d') # 3d axes
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None) # removes (most of the) blank border around the plot
# now do an initial scatter so we can get the axis limits to use through the animation
particles = ax.scatter(positions[0, 1, :], positions[0, 2, :], positions[0, 3, :], s=scales, marker='.', c='k')
xmin, xmax = min(positions[:, 1, :].flatten()), max(positions[:, 1, :].flatten())
ymin, ymax = min(positions[:, 2, :].flatten()), max(positions[:, 2, :].flatten())
zmin, zmax = min(positions[:, 3, :].flatten()), max(positions[:, 3, :].flatten())
limmin, limmax = min([xmin, ymin, zmin]), max([xmax, ymax, zmax]) # get the minimum and maximums for the axis limits
every = 1
length = 30
# now calculate some parameters for the animation frames and timing
nt = len(positions[:, 0, 0]) # number of timesteps
frames = np.arange(0, nt, every) # iterable for the animation function. Chooses which frames (indices) to animate.
fps = len(frames) // length # fps for the final animation
# ax.set_facecolor('k') # black background, since space is blach duh
theta = np.linspace(0, 2*np.pi, 360)
r1 = np.sqrt(sum(particle1_txyz[0, 1:]**2))
r2 = np.sqrt(sum(particle2_txyz[0, 1:]**2))
Zzero = np.zeros(len(theta))
circle1 = ax.plot(r1 * np.cos(theta), r1 * np.sin(theta), Zzero, lw=1, c='tab:blue')[0]
circle2 = ax.plot(r1 * np.cos(theta), r1 * np.sin(theta), Zzero, lw=1, c='tab:red')[0]
# def animate(i):
# if (i // every)%20 == 0:
# print(f"{i // every} / {len(frames)}")
# ax.clear()
# ax.scatter(positions[i, 1, :], positions[i, 2, :], positions[i, 3, :], s=scales, marker='.', c='w')
# ax.set_xlim(limmin, limmax); ax.set_ylim(limmin, limmax); ax.set_zlim(limmin, limmax)
# # if times[0]: # plot the current time in the corner if we want to!
# # ax.text(0.7 * limmax, 0.9 * limmax, 0, "$T = " + str(round(times[1][i], 2)) + "$", fontsize=24, color='w')
# ax.grid(False)
# ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# ax.elev = 90 # sets the viewing angle to be top-down
# return fig,
ax.grid(False)
ax.set_axis_off()
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.elev = 90 # sets the viewing angle to be top-down
ax.set_xlim(limmin, limmax); ax.set_ylim(limmin, limmax); ax.set_zlim(limmin, limmax)
def animate(i):
if (i // every)%20 == 0:
print(f"{i // every} / {len(frames)}")
particles._offsets3d = (positions[i, 1, :], positions[i, 2, :], positions[i, 3, :])
r1 = np.sqrt(sum(particle1_txyz[i, 1:]**2)); r2 = np.sqrt(sum(particle2_txyz[i, 1:]**2))
circle1.set_data_3d(r1 * np.cos(theta), r1 * np.sin(theta), Zzero)
circle2.set_data_3d(r2 * np.cos(theta), r2 * np.sin(theta), Zzero)
return (particles, circle1, circle2)
ani = animation.FuncAnimation(fig, animate, frames=frames, blit=True, repeat=False)
ani.save(f"{folder}/animation.mp4", writer='ffmpeg', fps=fps)
plt.close('all')