Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
animate_100_particles.py
1import matplotlib.pyplot as plt
2import numpy as np
3from mpl_toolkits.mplot3d import Axes3D
4from matplotlib import animation
5
6def get_data(files):
7 res = []
8 for file in files:
9 arr = [[], [], []]
10 with open(file, encoding="utf8") as f:
11 lines = f.readlines()
12
13 for line in lines:
14 xi,yi,zi = map(float, line.strip().split(","))
15 arr[0].append(xi)
16 arr[1].append(yi)
17 arr[2].append(zi)
18 res.append(arr)
19
20 return np.array(res)
21
22def update(num, lines, arr):
23 for line, a in zip(lines, arr):
24 line.set_data(a[:2, num])
25 line.set_3d_properties(a[2, num])
26
27
28
29def animate():
30 plt.style.use("dark_background")
31 fig = plt.figure()
32 ax = fig.add_subplot(projection="3d")
33
34
35 arr = get_data([f"output/p{i}_RK4.txt" for i in range(100)])
36
37 arr = arr[:,:,::10]
38
39 N = len(arr[0][0])
40
41 lines = [ax.plot(*a[:,1], "o")[0] for a in arr]
42
43 ax.set_xlim3d([-500.0, 500.0])
44 ax.set_xlabel('X')
45
46 ax.set_ylim3d([-500.0, 500.0])
47 ax.set_ylabel('Y')
48
49 ax.set_zlim3d([-500.0, 500.0])
50 ax.set_zlabel('Z')
51
52 ani = animation.FuncAnimation(fig, update, N, fargs=(lines, arr),
53 interval=1,
54 blit=False)
55
56
57 ani.save("100_particles.gif", writer=animation.FFMpegFileWriter(fps=30))
58 plt.show()
59
60
61if __name__ == "__main__":
62 animate()
63
64