2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
phase_transition.py
1from pathlib import Path
2
3import matplotlib.pyplot as plt
4import numpy as np
5from scipy.stats import linregress
6
7
8def plot_phase_transition(indir, outdir):
9 files = [
10 "size_20.txt",
11 "size_40.txt",
12 "size_60.txt",
13 "size_80.txt",
14 "size_100.txt",
15 ]
16 labels = [
17 "L = 20",
18 "L = 40",
19 "L = 60",
20 "L = 80",
21 "L = 100",
22 ]
23
24 figure1, ax1 = plt.subplots()
25 figure2, ax2 = plt.subplots()
26 figure3, ax3 = plt.subplots()
27 figure4, ax4 = plt.subplots()
28 figure5, ax5 = plt.subplots()
29
30 # For linear regression
31 L = []
32 Tc = []
33 size = 20
34
35 for file, label in zip(files, labels):
36 t = []
37 e = []
38 m = []
39 CV = []
40 X = []
41
42 # Append the lattice size
43 L.append(size)
44 size += 20
45
46 with open(Path(indir, file)) as f:
47 lines = f.readlines()
48 for line in lines:
49 l = line.strip().split(",")
50 t.append(float(l[0]))
51 e.append(float(l[1]))
52 m.append(float(l[2]))
53 CV.append(float(l[3]))
54 X.append(float(l[4]))
55
56 # Append the critical temp for the current lattice size
57 Tc.append(t[X.index(max(X))])
58
59 ax1.plot(t, e, label=label)
60 ax2.plot(t, m, label=label)
61 ax3.plot(t, CV, label=label)
62 ax4.plot(t, X, label=label)
63
64 # Attempt linear regression
65 x = np.linspace(0, 100, 1001)
66 regression = linregress(L, Tc)
67 f = lambda x: regression[0] * x + regression[1]
68 ax5.scatter(L, Tc)
69 ax5.plot(x, f(x), label=f"m = {regression[0]}")
70
71 figure1.legend()
72 figure2.legend()
73 figure3.legend()
74 figure4.legend()
75 figure5.legend()
76
77 figure1.savefig(Path(outdir, "energy.pdf"))
78 figure2.savefig(Path(outdir, "magnetization.pdf"))
79 figure3.savefig(Path(outdir, "heat_capacity.pdf"))
80 figure4.savefig(Path(outdir, "susceptibility.pdf"))
81 figure5.savefig(Path(outdir, "linreg.pdf"))
82
83 plt.close(figure1)
84 plt.close(figure2)
85 plt.close(figure3)
86 plt.close(figure4)
87 plt.close(figure5)
88
89
90if __name__ == "__main__":
91 plot_phase_transition(
92 "fox_output/phase_transition/wide/10M/",
93 "../latex/images/phase_transition/fox/wide/10M/",
94 )
95 plot_phase_transition(
96 "fox_output/phase_transition/wide/1M/",
97 "../latex/images/phase_transition/fox/wide/1M/",
98 )
99 plot_phase_transition(
100 "fox_output/phase_transition/narrow/10M/",
101 "../latex/images/phase_transition/fox/narrow/10M/",
102 )
103 plot_phase_transition(
104 "output/phase_transition/", "../latex/images/phase_transition/hp/"
105 )
106 plot_phase_transition(
107 "output/phase_transition/", "../latex/images/phase_transition/hp/"
108 )
109 plot_phase_transition(
110 "output/phase_transition/",
111 "../latex/images/phase_transition/hp/",
112 )