Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
plot_relative_error.py
Go to the documentation of this file.
1
11
12import matplotlib.pyplot as plt
13import numpy as np
14import seaborn as sns
15
16sns.set_theme()
17params = {
18 "font.family": "Serif",
19 "font.serif": "Roman",
20 "text.usetex": True,
21 "axes.titlesize": "large",
22 "axes.labelsize": "large",
23 "xtick.labelsize": "large",
24 "ytick.labelsize": "large",
25 "legend.fontsize": "medium"
26}
27plt.rcParams.update(params)
28
29def main():
30 directories = [
31 "output/relative_error/RK4/",
32 "output/relative_error/euler/",
33 ]
34 files = [
35 "4000_steps.txt",
36 "8000_steps.txt",
37 "16000_steps.txt",
38 "32000_steps.txt",
39 ]
40 labels = [
41 r"$n_1$",
42 r"$n_2$",
43 r"$n_3$",
44 r"$n_4$",
45 ]
46 titles = [
47 "(a)",
48 "(b)"
49 ]
50 methods = [
51 "rk4",
52 "euler"
53 ]
54 colors = [
55 "seagreen",
56 "darkred",
57 "darkgoldenrod",
58 "steelblue"
59 ]
60 fig1, axs1 = plt.subplots(2,1)
61 for i, (dir, title) in enumerate(list(zip(directories, titles))):
62 max_err = []
63 for label, file, color in zip(labels, files, colors):
64 with open(dir+file) as f:
65 lines = f.readlines()
66 t = np.linspace(0, 50, len(lines))
67 r = np.array([float(line.strip()) for line in lines])
68 max_err.append(max(r))
69 axs1[i].plot(t, r, label=label, color=color)
70
71 axs1[i].set(xlabel=r"t $(\mu s)$", ylabel = r"relative error $(\mu m)$")
72 axs1[i].legend()
73 axs1[i].set_title(title)
74
75 conv_rate = 1/3 * sum([np.log2(max_err[i+1]/max_err[i])/np.log2(.5) for i in range(3)])
76 print(f"{methods[i]}: {conv_rate}")
77
78 plt.tight_layout()
79 fig1.savefig("../latex/images/relative_error.pdf", bbox_inches="tight")
80
81
82if __name__ == "__main__":
83 main()