Penning Trap Simulation Simulate particle behavior inside a Penning Trap

Loading...
Searching...
No Matches
plot_relative_error.py
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4 def main():
5 directories = [
6 "output/relative_error/RK4/",
7 "output/relative_error/euler/",
8 ]
9 files = [
10 "4000_steps.txt",
11 "8000_steps.txt",
12 "16000_steps.txt",
13 "32000_steps.txt",
14 ]
15 labels = [
16 r"4000 steps",
17 r"8000 steps",
18 r"16000 steps",
19 r"32000 steps",
20 ]
21 titles = [
22 "Relative error for the RK4 method",
23 "Relative error for the forward Euler method"
24 ]
25 methods = [
26 "rk4",
27 "euler"
28 ]
29 fig1, axs1 = plt.subplots(2,1)
30 for i, (dir, title) in enumerate(list(zip(directories, titles))):
31 max_err = []
32 for label, file in zip(labels, files):
33 with open(dir+file) as f:
34 lines = f.readlines()
35 t = np.linspace(0, 50, len(lines))
36 r = np.array([float(line.strip()) for line in lines])
37 max_err.append(max(r))
38 axs1[i].plot(t, r, label=label)
39
40 axs1[i].set(xlabel=r"t $(\mu s)$", ylabel = r"relative_error $(\mu m)$")
41 axs1[i].legend()
42 axs1[i].set_title(title)
43
44 conv_rate = 1/3 * sum([np.log2(max_err[i+1]/max_err[i])/np.log2(.5) for i in range(3)])
45 print(f"{methods[i]}: {conv_rate}")
46
47 fig1.savefig("../latex/images/phase_space_2_particles_x.pdf")
48
49
50 if __name__ == "__main__":
51 main()