Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
animate_100_particles.py
Go to the documentation of this file.
1
11
12import matplotlib.pyplot as plt
13import numpy as np
14from matplotlib import animation
15from mpl_toolkits.mplot3d import Axes3D
16
17
18def get_data(files):
19 res = []
20 for file in files:
21 arr = [[], [], []]
22 with open(file, encoding="utf8") as f:
23 lines = f.readlines()
24
25 for line in lines:
26 xi, yi, zi = map(float, line.strip().split(","))
27 arr[0].append(xi)
28 arr[1].append(yi)
29 arr[2].append(zi)
30 res.append(arr)
31
32 return np.array(res)
33
34
35def update(num, lines, arr):
36 for line, a in zip(lines, arr):
37 line.set_data(a[:2, num - 1 : num])
38 line.set_3d_properties(a[2, num])
39
40
41def animate():
42 plt.style.use("dark_background")
43 fig = plt.figure()
44 ax = fig.add_subplot(projection="3d")
45
46 arr = get_data([f"output/simulate_100_particles/particle_{i}_r.txt" for i in range(100)])
47
48 arr = arr[:, :, ::40]
49
50 N = len(arr[0][0])
51
52 lines = [ax.plot(*a[:, 1], "o")[0] for a in arr]
53
54 ax.set_title("100 particles inside a Penning trap")
55 plt.figtext(
56 0.5,
57 0.01,
58 "100 randomly generated particles "
59 "evolving over a time of 50 microseconds.",
60 fontsize=12,
61 horizontalalignment="center",
62 )
63
64 ax.set_xlim3d([-500.0, 500.0])
65 ax.set_xlabel("X (micrometers)")
66
67 ax.set_ylim3d([-500.0, 500.0])
68 ax.set_ylabel("Y (micrometers)")
69
70 ax.set_zlim3d([-500.0, 500.0])
71 ax.set_zlabel("Z (micrometers)")
72
73 ani = animation.FuncAnimation(
74 fig, update, N, fargs=(lines, arr), interval=1, blit=False
75 )
76
77 ani.save("../images/100_particles.gif", writer=animation.FFMpegFileWriter(fps=50))
78
79
80if __name__ == "__main__":
81 animate()