|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import time |
| 4 | + |
| 5 | +from numba import jit |
| 6 | + |
| 7 | + |
| 8 | +@jit(nopython=True) |
| 9 | +def energy(system, i, j, L): |
| 10 | + """Energy function of spins connected to site (i, j).""" |
| 11 | + return -1. * system[i, j] * (system[np.mod(i - 1, L), j] + system[np.mod(i + 1, L), j] + |
| 12 | + system[i, np.mod(j - 1, L)] + system[i, np.mod(j + 1, L)]) |
| 13 | + |
| 14 | + |
| 15 | +@jit |
| 16 | +def prepare_system(L): |
| 17 | + """Initialize the system.""" |
| 18 | + system = 2 * (0.5 - np.random.randint(0, 2, size=[L, L])) |
| 19 | + return system |
| 20 | + |
| 21 | +@jit(nopython=True) |
| 22 | +def measure_energy(system): |
| 23 | + L = system.shape[0] |
| 24 | + E = 0 |
| 25 | + for i in range(L): |
| 26 | + for j in range(L): |
| 27 | + E += energy(system, i, j, L) / 2. |
| 28 | + return E |
| 29 | + |
| 30 | + |
| 31 | +@jit(nopython=True) |
| 32 | +def metropolis_loop(system, T, N_sweeps, N_eq, N_flips): |
| 33 | + """ Main loop doing the Metropolis algorithm.""" |
| 34 | + E = measure_energy(system) |
| 35 | + L = system.shape[0] |
| 36 | + E_list = [] |
| 37 | + for step in range(N_sweeps + N_eq): |
| 38 | + i = np.random.randint(0, L) |
| 39 | + j = np.random.randint(0, L) |
| 40 | + |
| 41 | + dE = -2. * energy(system, i, j, L) |
| 42 | + if dE <= 0.: |
| 43 | + system[i, j] *= -1 |
| 44 | + E += dE |
| 45 | + elif np.exp(-1. / T * dE) > np.random.rand(): |
| 46 | + system[i, j] *= -1 |
| 47 | + E += dE |
| 48 | + |
| 49 | + if step >= N_eq and np.mod(step, N_flips) == 0: |
| 50 | + # measurement |
| 51 | + E_list.append(E) |
| 52 | + return np.array(E_list) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + """ Scan through some temperatures """ |
| 59 | + # Set parameters here |
| 60 | + L = 4 # Linear system size |
| 61 | + N_sweeps = 5000 # Number of steps for the measurements |
| 62 | + N_eq = 1000 # Number of equilibration steps before the measurements start |
| 63 | + N_flips = 10 # Number of steps between measurements |
| 64 | + N_bins = 10 # Number of bins use for the error analysis |
| 65 | + |
| 66 | + T_range = np.arange(1.5, 3.1, 0.1) |
| 67 | + |
| 68 | + C_list = [] |
| 69 | + system = prepare_system(L) |
| 70 | + for T in T_range: |
| 71 | + C_list_bin = [] |
| 72 | + for k in range(N_bins): |
| 73 | + Es = metropolis_loop(system, T, N_sweeps, N_eq, N_flips) |
| 74 | + |
| 75 | + mean_E = np.mean(Es) |
| 76 | + mean_E2 = np.mean(Es**2) |
| 77 | + |
| 78 | + C_list_bin.append(1. / T**2. / L**2. * (mean_E2 - mean_E**2)) |
| 79 | + C_list.append([np.mean(C_list_bin), np.std(C_list_bin) / np.sqrt(N_bins)]) |
| 80 | + |
| 81 | + print(T, mean_E, C_list[-1]) |
| 82 | + |
| 83 | + # Plot the results |
| 84 | + C_list = np.array(C_list) |
| 85 | + plt.errorbar(T_range, C_list[:, 0], C_list[:, 1]) |
| 86 | + Tc = 2. / np.log(1. + np.sqrt(2)) |
| 87 | + print(Tc) |
| 88 | + plt.axvline(Tc, color='r', linestyle='--') |
| 89 | + plt.xlabel('$T$') |
| 90 | + plt.ylabel('$c$') |
| 91 | + plt.show() |
0 commit comments