├── 2DIsing_metropolics_C++ ├── m1.dat ├── main.cpp ├── main.exe └── utils.h ├── 2DIsing_metropolics_Fortran └── Ising.f90 ├── 2DIsing_metropolics_python ├── MC_numba.py ├── MC_wolff_cluster.py ├── M_T │ ├── 10.npy │ ├── 11.npy │ ├── 12.npy │ ├── 13.npy │ ├── 14.npy │ ├── 15.npy │ ├── 16.npy │ ├── 17.npy │ ├── 18.npy │ ├── 19.npy │ ├── 20.npy │ ├── 21.npy │ ├── 22.npy │ ├── 23.npy │ ├── 24.npy │ ├── 25.npy │ ├── 26.npy │ ├── 27.npy │ ├── 28.npy │ ├── 29.npy │ ├── 3.npy │ ├── 30.npy │ ├── 31.npy │ ├── 32.npy │ ├── 33.npy │ ├── 34.npy │ ├── 35.npy │ ├── 36.npy │ ├── 37.npy │ ├── 38.npy │ ├── 39.npy │ ├── 4.npy │ ├── 5.npy │ ├── 6.npy │ ├── 7.npy │ ├── 8.npy │ └── 9.npy ├── MonteCarlo.py ├── plot.py ├── test.jpg └── test.png ├── 2DIsing_wolff_C++ ├── utils.h ├── wolff_cluster.cpp ├── wolff_cluster.exe └── wolff_utils.h ├── 2DIsing_wolff_python ├── 2DIsing_wolff_cluster.py ├── M_T_H_0.0 │ ├── C_T.txt │ └── M_T.txt ├── M_T_H_0.05 │ ├── C_T.txt │ └── M_T.txt ├── M_T_H_0.1 │ ├── C_T.txt │ └── M_T.txt └── plot.py ├── README.md └── XYmodel_metropolis_python ├── PTMC.py ├── plot_votex.py └── xymodel_mc.py /2DIsing_metropolics_C++/m1.dat: -------------------------------------------------------------------------------- 1 | 0.5 1 0 2 | 0.6 1 0 3 | 0.7 1 0 4 | 0.8 1 0 5 | 0.9 0.999987 1.71875e-08 6 | 1 0.999947 6.91187e-08 7 | 1.1 0.99985 1.78409e-07 8 | 1.2 0.99965 4.23958e-07 9 | 1.3 0.999165 7.85788e-07 10 | 1.4 0.998092 2.23808e-06 11 | 1.5 0.996577 3.67225e-06 12 | 1.6 0.99387 6.94803e-06 13 | 1.7 0.989395 1.23509e-05 14 | 1.8 0.983233 2.57661e-05 15 | 1.9 0.973035 3.5629e-05 16 | 2 0.95973 8.67854e-05 17 | 2.1 0.941392 0.0001194 18 | 2.2 0.90829 0.000302998 19 | 2.3 0.84335 0.000940316 20 | 2.4 0.63882 0.015617 21 | 2.5 0.294425 0.0155482 22 | 2.6 0.147565 0.00441791 23 | 2.7 0.112948 0.00270891 24 | 2.8 0.08251 0.00200721 25 | 2.9 0.0801125 0.00112026 26 | 3 0.0706525 0.000712401 27 | 3.1 0.0730375 0.000666007 28 | 3.2 0.046435 0.000328878 29 | 3.3 0.0594825 0.000531143 30 | 3.4 0.05197 0.000543 31 | 3.5 0.039735 0.000208346 32 | 3.6 0.03365 0.00020995 33 | 3.7 0.0432075 0.000428492 34 | 3.8 0.0425475 0.00016061 35 | 3.9 0.0346175 0.000134425 36 | 4 0.032485 0.000118056 37 | -------------------------------------------------------------------------------- /2DIsing_metropolics_C++/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "utils.h" 9 | using namespace std; 10 | 11 | utils UTILS; 12 | 13 | const int L = 40, B = 1, sweeps=50000, relax = 5000; //L is lattice size and B is Boltzmann constant; 14 | int M = 0, S = L*L; 15 | using Matrix = std::array, L>; 16 | 17 | //Inialize the L*L spin lattice 18 | Matrix InialSpin() { 19 | Matrix m = {}; 20 | for(int i=0; i 0) 60 | { 61 | if (exp(-delta_E/T) > UTILS.random()) SpinM[x_site][y_site] = -SpinM[x_site][y_site]; 62 | } 63 | } 64 | int M = 0; 65 | for(int i=0; i 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define N 999 9 | const int J = 1, l = 30; 10 | const float H = 0.0; 11 | 12 | class utils 13 | { 14 | public: 15 | int funcdeltaE(int loc, int top, int bottom, int left, int right) 16 | { 17 | int delta_E = 0; 18 | int E_loc_former = -1 * loc * (J * (top + bottom + left + right) + H); 19 | int E_loc_next = loc * (J * (top + bottom + left + right) + H); 20 | delta_E = E_loc_next - E_loc_former; 21 | return delta_E; 22 | } 23 | 24 | float random() 25 | { 26 | return rand()%(N+1)/(float)(N+1); 27 | } 28 | 29 | int randint(int Lattice) 30 | { 31 | return (rand()%(Lattice)); 32 | } 33 | int Neigh1(int& Minus) 34 | { 35 | if ((Minus-1)%l == l-1 || (Minus-1)%l == -1) return (Minus-1+l); 36 | else return (Minus-1); 37 | } 38 | 39 | int Neigh2(int& adds) 40 | { 41 | if( (adds+1)%l == 0 ) return (adds+1-l); 42 | else return (adds+1); 43 | } 44 | }; 45 | 46 | #endif -------------------------------------------------------------------------------- /2DIsing_metropolics_Fortran/Ising.f90: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_Fortran/Ising.f90 -------------------------------------------------------------------------------- /2DIsing_metropolics_python/MC_numba.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Aug 21 09:43:02 2022 4 | 5 | @author: 26526 6 | """ 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | from multiprocessing import Pool 10 | from numba import jit 11 | import time 12 | import os 13 | 14 | sweeps = 10000 15 | K = 1 16 | J = 1 17 | H = 0 18 | Lattice = 40 19 | relax = 100 20 | 21 | @jit 22 | def metropolis(spin, t): 23 | mag = [] 24 | mag_2 = [] 25 | for k in range(sweeps + relax): 26 | for i in range(Lattice): 27 | for j in range(Lattice): 28 | tmp = [] 29 | for kk in range(8): 30 | tmp.append(np.random.randint(0, Lattice)) 31 | x_site = tmp[1] 32 | y_site = tmp[4] 33 | top = (x_site - 1) % Lattice 34 | bottom = (x_site + 1) % Lattice 35 | left = (y_site - 1) % Lattice 36 | right = (y_site + 1) % Lattice 37 | delta_E = 2.0 * spin[x_site,y_site] * (J * (spin[top,y_site] + spin[bottom,y_site] + spin[x_site,left] + spin[x_site,right]) + H ) 38 | if delta_E <= 0: 39 | spin[x_site,y_site] = -spin[x_site,y_site] 40 | else: 41 | if np.exp((-delta_E)/t) > np.random.random(): 42 | spin[x_site,y_site] = -spin[x_site,y_site] 43 | #spin[i,j] = 1 44 | 45 | M = np.abs(np.mean(spin)) 46 | #print("M is:", M) 47 | M_2 = np.square(M) 48 | mag_2.append(M_2) 49 | mag.append(M) 50 | 51 | return mag, mag_2 52 | 53 | 54 | 55 | def main_loop(T): 56 | 57 | t = T 58 | spin = np.ones([Lattice, Lattice], dtype = int) 59 | 60 | mag, mag_2 = metropolis(spin, t) 61 | 62 | M_ave = np.mean(mag) 63 | print("t is", t, "M_ave is:", M_ave) 64 | 65 | 66 | # print("result is:", spin) 67 | M_2 = np.mean(mag_2) 68 | C_v = (M_2 - M_ave ** 2) / T # 求磁比热 69 | 70 | return M_2 71 | 72 | def save_data(data): 73 | save_path = "M_T" 74 | if os.path.exists(save_path): 75 | print("save path {} exist".format(save_path)) 76 | else: 77 | print("save path {} not exist".format(save_path)) 78 | os.makedirs(save_path) 79 | print("now makefir the save_path") 80 | 81 | np.savetxt(save_path + "/M_T.txt", data) 82 | 83 | 84 | 85 | def single_run(): 86 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 87 | M = [] 88 | for i in range(len(T)): 89 | M.append(main_loop(T[i])) 90 | 91 | data = [] 92 | print("M is:", M) 93 | for i in range(len(T)): 94 | data.append([T[i], M[i]]) 95 | 96 | save_data(data) 97 | 98 | 99 | def multi_run(): 100 | start = time.time() 101 | 102 | M = [] 103 | ing_argv = [] 104 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 105 | for i in range(len(T)): 106 | ing_argv.append(T[i]) 107 | 108 | with Pool(8) as p: 109 | M.append(p.map(main_loop, ing_argv)) 110 | 111 | print(M[0]) 112 | 113 | data = [] 114 | for i in range(len(T)): 115 | data.append([T[i], M[0][i]]) 116 | 117 | save_data(data) 118 | 119 | if __name__=="__main__": 120 | single_run() 121 | -------------------------------------------------------------------------------- /2DIsing_metropolics_python/MC_wolff_cluster.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from multiprocessing import Pool 4 | from numba import jit 5 | import time 6 | import os 7 | 8 | sweeps = 600 9 | K = 1 10 | J = 1 11 | H = 0 12 | Lattice = 30 13 | relax = 200 14 | 15 | def down(x, y): 16 | if y > Lattice - 1.5: return [x, 0] 17 | else: return [x, y + 1] 18 | 19 | @jit 20 | def wolff_cluster(spin, P_add): 21 | mag = [] 22 | mag_2 = [] 23 | 24 | for k in range(sweeps + relax): 25 | x = np.random.randint(0, Lattice) 26 | y = np.random.randint(0, Lattice) 27 | sign = spin[x, y] 28 | stack = [[x, y]] 29 | lable = [[1 for i in range(Lattice)] for j in range(Lattice)] 30 | lable[x][y] = 0 31 | 32 | while len(stack) > 0.5: 33 | 34 | # While stack is not empty, pop and flip a spin 35 | [x_site, y_site] = stack.pop() 36 | spin[x_site, y_site] = -sign 37 | 38 | # Append neighbor spins 39 | 40 | # Left neighbor 41 | if x_site < 0.5: 42 | [leftx, lefty] = [Lattice - 1, y_site] 43 | else: 44 | [leftx, lefty] = [x_site - 1, y_site] 45 | 46 | if spin[leftx, lefty] * sign > 0.5 and np.random.rand() < P_add: 47 | stack.append([leftx, lefty]) 48 | lable[leftx][lefty] = 0 49 | 50 | # Right neighbor 51 | if x_site > Lattice - 1.5: 52 | [rightx, righty] = [0, y_site] 53 | else: 54 | [rightx, righty] = [x_site + 1, y_site] 55 | 56 | if spin[rightx, righty] * sign > 0.5 and np.random.rand() < P_add: 57 | stack.append([rightx, righty]) 58 | lable[rightx][righty] = 0 59 | 60 | # Up neighbor 61 | if y_site < 0.5: 62 | [upx, upy] = [x_site, Lattice - 1] 63 | else: 64 | [upx, upy] = [x_site, y_site - 1] 65 | 66 | if spin[upx, upy] * sign > 0.5 and np.random.rand() < P_add: 67 | stack.append([upx, upy]) 68 | lable[upx][upy] = 0 69 | 70 | # Down neighbor 71 | if y_site > Lattice - 1.5: 72 | [downx, downy] = [x_site, 0] 73 | else: 74 | [downx, downy] = [x_site, y_site + 1] 75 | 76 | if spin[downx, downy] * sign > 0.5 and np.random.rand() < P_add: 77 | stack.append([downx, downy]) 78 | lable[downx][downy] = 0 79 | 80 | if k > relax: 81 | M = np.abs(np.mean(spin)) 82 | M_2 = np.square(M) 83 | mag_2.append(M_2) 84 | mag.append(M) 85 | 86 | return mag, mag_2 87 | 88 | 89 | def main_loop(T): 90 | t = T 91 | P_add = 1 - np.exp(-2 * J / t) 92 | spin = np.random.randint(-1, 1, (Lattice, Lattice)) 93 | spin[spin == 0] = 1 94 | mag, mag_2 = wolff_cluster(spin, P_add) 95 | M_ave = np.mean(mag) 96 | print("t is", t, "M_ave is:", M_ave) 97 | 98 | # print("result is:", spin) 99 | M_2 = np.mean(mag_2) 100 | C_v = (M_2 - M_ave ** 2) / T # 求磁比热 101 | 102 | return M_2 103 | 104 | 105 | def save_data(data): 106 | save_path = "M_T" 107 | if os.path.exists(save_path): 108 | print("save path {} exist".format(save_path)) 109 | else: 110 | print("save path {} not exist".format(save_path)) 111 | os.makedirs(save_path) 112 | print("now makefir the save_path") 113 | 114 | np.savetxt(save_path + "/M_T.txt", data) 115 | 116 | 117 | 118 | def single_run(): 119 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 120 | M = [] 121 | for i in range(len(T)): 122 | M.append(main_loop(T[i])) 123 | 124 | data = [] 125 | print("M is:", M) 126 | for i in range(len(T)): 127 | data.append([T[i], M[i]]) 128 | 129 | save_data(data) 130 | 131 | 132 | def multi_run(): 133 | start = time.time() 134 | 135 | M = [] 136 | ing_argv = [] 137 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 138 | for i in range(len(T)): 139 | ing_argv.append(T[i]) 140 | 141 | with Pool(8) as p: 142 | M.append(p.map(main_loop, ing_argv)) 143 | 144 | print(M[0]) 145 | 146 | data = [] 147 | for i in range(len(T)): 148 | data.append([T[i], M[0][i]]) 149 | 150 | save_data(data) 151 | 152 | 153 | if __name__ == "__main__": 154 | multi_run() -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/10.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/10.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/11.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/11.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/12.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/12.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/13.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/13.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/14.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/14.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/15.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/15.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/16.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/16.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/17.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/17.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/18.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/18.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/19.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/19.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/20.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/20.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/21.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/21.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/22.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/22.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/23.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/23.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/24.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/24.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/25.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/25.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/26.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/26.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/27.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/27.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/28.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/28.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/29.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/29.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/3.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/3.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/30.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/30.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/31.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/31.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/32.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/32.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/33.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/33.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/34.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/34.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/35.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/35.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/36.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/36.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/37.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/37.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/38.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/38.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/39.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/39.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/4.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/4.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/5.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/5.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/6.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/6.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/7.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/7.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/8.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/8.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/M_T/9.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/M_T/9.npy -------------------------------------------------------------------------------- /2DIsing_metropolics_python/MonteCarlo.py: -------------------------------------------------------------------------------- 1 | ## Author jinyang ni 2 | ## Monte-Carlo simulation for 2D-Ising model 3 | 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from multiprocessing import Pool 7 | from numba import jit 8 | import os 9 | 10 | import time 11 | 12 | class MonteCarlo(): 13 | 14 | def __init__(self, relax, sweeps, K, J, H, Lattice): 15 | self.sweeps = sweeps 16 | self.relax = relax 17 | self.K = K 18 | self.J = J 19 | self.H = H 20 | self.Lattice = Lattice 21 | 22 | def cal_delta_E(self, spin, i, j): 23 | 24 | top = (i - 1) % self.Lattice 25 | bottom = (i + 1) % self.Lattice 26 | left = (j - 1) % self.Lattice 27 | right = (j + 1) % self.Lattice 28 | E_loc_former = -1.0 * spin[i,j] * (self.J * (spin[top,j] + spin[bottom,j] + spin[i,left] + spin[i,right]) + self.H ) 29 | E_loc_next = spin[i,j] * (self.J * (spin[top,j] + spin[bottom,j] + spin[i,left] + spin[i,right]) + self.H ) 30 | delta_E = E_loc_next - E_loc_former 31 | 32 | return delta_E 33 | 34 | #@jit 35 | def main_loop(self, T): 36 | 37 | S = self.Lattice ** 2 38 | #f_M = open('M.dat','w') 39 | #f_C = open('C.dat','w') 40 | 41 | #for T in np.arange(0.1, 2.0, 0.05): 42 | t = T 43 | spin = np.ones([self.Lattice, self.Lattice], dtype = int) 44 | C = 0 45 | C_heat = [] 46 | mag = [] 47 | mag_2 = [] 48 | for sweep in range(self.sweeps + self.relax): 49 | for i in range(self.Lattice): 50 | for j in range(self.Lattice): 51 | tmp = [] 52 | for kk in range(8): 53 | tmp.append(np.random.randint(0, self.Lattice)) 54 | x_site = tmp[1] 55 | y_site = tmp[4] 56 | top = (x_site - 1) % self.Lattice 57 | bottom = (x_site + 1) % self.Lattice 58 | left = (y_site - 1) % self.Lattice 59 | right = (y_site + 1) % self.Lattice 60 | delta_E = 2.0 * spin[x_site,y_site] * (self.J * (spin[top,y_site] + spin[bottom,y_site] + spin[x_site,left] + spin[x_site,right]) + self.H ) 61 | if delta_E <= 0: 62 | spin[x_site,y_site] = -spin[x_site,y_site] 63 | else: 64 | if np.exp((-delta_E)/t) > np.random.random(): 65 | spin[x_site,y_site] = -spin[x_site,y_site] 66 | 67 | M = np.abs(np.mean(spin)) 68 | M_2 = np.square(M) 69 | mag_2.append(M_2) 70 | mag.append(M) 71 | 72 | M_ave = np.mean(mag[self.relax:]) 73 | print("M_ave is:", M_ave) 74 | 75 | #print("result is:", spin) 76 | M_2 = np.mean(mag_2[self.relax:]) 77 | C_v = (M_2 - M_ave ** 2) / T # 求磁比热 78 | 79 | return M_ave 80 | #np.save("M_T/{:d}.npy".format(T), test) 81 | 82 | #f_M.write(str(T)+' '+str(result)+"\n") # 83 | #f_C.write(str(T)+' '+str(result_C)+"\n") 84 | 85 | #f_M.close() 86 | #f_C.close() 87 | 88 | def save_data(self, data): 89 | save_path = "M_T" 90 | if os.path.exists(save_path): 91 | print("save path {} exist".format(save_path)) 92 | else: 93 | print("save path {} not exist".format(save_path)) 94 | os.makedirs(save_path) 95 | print("now makefir the save_path") 96 | 97 | np.savetxt(save_path + "/M_T.txt", data) 98 | 99 | def multi_run(self): 100 | 101 | start = time.time() 102 | 103 | M = [] 104 | ing_argv = [] 105 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 106 | for i in range(len(T)): 107 | ing_argv.append(T[i]) 108 | 109 | with Pool(8) as p: 110 | M.append(p.map(self.main_loop, ing_argv)) 111 | 112 | print(M[0]) 113 | 114 | data = [] 115 | for i in range(len(T)): 116 | data.append([T[i], M[0][i]]) 117 | 118 | self.save_data(data) 119 | 120 | def single_run(self): 121 | start = time.time() 122 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 123 | M = [] 124 | for i in range(len(T)): 125 | M.append(self.main_loop(T[i])) 126 | 127 | data = [] 128 | print("M is:", M) 129 | for i in range(len(T)): 130 | data.append([T[i], M[i]]) 131 | 132 | self.save_data(data) 133 | 134 | if __name__=="__main__": 135 | MC_model = MonteCarlo(100, 2000, 1, 1, 0, 10) 136 | MC_model.single_run() 137 | -------------------------------------------------------------------------------- /2DIsing_metropolics_python/plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import math 4 | from scipy.optimize import curve_fit 5 | import os, glob 6 | 7 | 8 | def load_npy(root): 9 | 10 | path = [] 11 | 12 | #for i in range(len(H)): 13 | path.append(os.path.join(root + "/M_T", "M_T.txt")) 14 | 15 | print("path is:", path) 16 | M = [] 17 | T = [] 18 | 19 | for i in range(len(path)): 20 | M_h, T_h = [], [] 21 | data = np.loadtxt(path[i]) 22 | for i in range(len(data)): 23 | T_h.append(data[i][0]) 24 | M_h.append(data[i][1]) 25 | M.append(M_h) 26 | T.append(T_h) 27 | 28 | print("M is:", M) 29 | 30 | return T, M 31 | 32 | def load_dat(data_path): 33 | 34 | T, M, C = [], [], [] 35 | 36 | for line in open(data_path, "r"): 37 | tmp = line.split( ) 38 | T.append(float(tmp[0])) 39 | M.append(float(tmp[1])) 40 | 41 | return T, M 42 | 43 | 44 | def plot(T, M): 45 | 46 | for i in range(len(T)): 47 | plt.plot(T[i],M[i],"o-") 48 | 49 | #plt.legend(loc="upper right", prop = {'family': "Times New Roman", "weight":"normal", "size":16,}, frameon=False) 50 | 51 | plt.show() 52 | 53 | if __name__=="__main__": 54 | path = "D:/Monte-Carlo-Algorithm-master/Monte-Carlo-Algorithm-master/2DIsing_metropolics_python" 55 | T, M = load_npy(path) 56 | plot(T, M) 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /2DIsing_metropolics_python/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/test.jpg -------------------------------------------------------------------------------- /2DIsing_metropolics_python/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JYNi16/Monte-Carlo-Algorithm/ea3e80dc4dd5b7713aa3b380af9f797041b37eb9/2DIsing_metropolics_python/test.png -------------------------------------------------------------------------------- /2DIsing_wolff_C++/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTILS__ 2 | #define __UTILS__ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define N 999 9 | const int J = 1, l = 30; 10 | const float H = 0.0; 11 | 12 | class utils 13 | { 14 | public: 15 | int funcdeltaE(int loc, int top, int bottom, int left, int right) 16 | { 17 | int delta_E = 0; 18 | int E_loc_former = -1 * loc * (J * (top + bottom + left + right) + H); 19 | int E_loc_next = loc * (J * (top + bottom + left + right) + H); 20 | delta_E = E_loc_next - E_loc_former; 21 | return delta_E; 22 | } 23 | 24 | float random() 25 | { 26 | return rand()%(N+1)/(float)(N+1); 27 | } 28 | 29 | int randint(int Lattice) 30 | { 31 | return (rand()%(Lattice)); 32 | } 33 | int Neigh1(int& Minus) 34 | { 35 | if ((Minus-1)%l == l-1 || (Minus-1)%l == -1) return (Minus-1+l); 36 | else return (Minus-1); 37 | } 38 | 39 | int Neigh2(int& adds) 40 | { 41 | if( (adds+1)%l == 0 ) return (adds+1-l); 42 | else return (adds+1); 43 | } 44 | }; 45 | 46 | #endif -------------------------------------------------------------------------------- /2DIsing_wolff_C++/wolff_cluster.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "utils.h" 10 | #include "wolff_utils.h" 11 | using namespace std; 12 | 13 | utils UTILS; 14 | wolff_utils WOLFF_UTILS; 15 | 16 | const int L = 40, B = 1, sweeps=500, relax = 100; //L is lattice size and B is Boltzmann constant; 17 | int M = 0, S = L*L; 18 | using Matrix = std::array, L>; 19 | bool IsOnes = true; 20 | stack ls; 21 | 22 | //Inialize the L*L spin lattice 23 | Matrix InialSpin(bool IsOnes) { 24 | Matrix m = {}; 25 | for(int i=0; i 0.5 && UTILS.random() < P_add) 79 | { 80 | ls.push(left_x); 81 | ls.push(left_y); 82 | lable[left_x][left_y] =0; 83 | } 84 | 85 | //right 86 | WOLFF_UTILS.right(x_site, y_site, &right_x, &right_y, L); 87 | if (SpinM[right_x][right_y] * sign > 0.5 && UTILS.random() < P_add) 88 | { 89 | ls.push(right_x); 90 | ls.push(right_y); 91 | lable[right_x][right_y] =0; 92 | } 93 | 94 | //up 95 | WOLFF_UTILS.up(x_site, y_site, &up_x, &up_y, L); 96 | if (SpinM[up_x][up_y] * sign > 0.5 && UTILS.random() < P_add) 97 | { 98 | ls.push(up_x); 99 | ls.push(up_y); 100 | lable[up_x][up_y] =0; 101 | } 102 | 103 | //down 104 | WOLFF_UTILS.down(x_site, y_site, &down_x, &down_y, L); 105 | if (SpinM[down_x][down_y] * sign > 0.5 && UTILS.random() < P_add) 106 | { 107 | ls.push(down_x); 108 | ls.push(down_y); 109 | lable[down_x][down_y] =0; 110 | } 111 | } 112 | 113 | if (k>=relax) 114 | { 115 | int M = 0; 116 | for(int i=0; i 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class wolff_utils 10 | { 11 | public: 12 | void left(int x, int y, int* left_x, int* left_y, int L) 13 | { 14 | if (x < 1) 15 | { 16 | *left_x = L-1; 17 | *left_y = y; } 18 | else 19 | { 20 | *left_x = x-1; 21 | *left_y = y; 22 | } 23 | } 24 | 25 | void right(int x, int y, int* right_x, int* right_y, int L) 26 | { 27 | if (x > (L - 1.5)) 28 | { 29 | *right_x = 0; 30 | *right_y = y; } 31 | else 32 | { 33 | *right_x = x+1; 34 | *right_y = y; 35 | } 36 | 37 | } 38 | 39 | void up(int x, int y, int* up_x, int* up_y, int L) 40 | { 41 | if (y < 0.5) 42 | { 43 | *up_x = x; 44 | *up_y = L-1; } 45 | else 46 | { 47 | *up_x = x; 48 | *up_y = y-1; 49 | } 50 | 51 | } 52 | 53 | void down(int x, int y, int* down_x, int* down_y, int L) 54 | { 55 | if (y > (L-1.5)) 56 | { 57 | *down_x = x; 58 | *down_y = 0; } 59 | else 60 | { 61 | *down_x = x; 62 | *down_y = y+1; 63 | } 64 | } 65 | 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/2DIsing_wolff_cluster.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from multiprocessing import Pool 4 | from numba import jit 5 | import time 6 | import os 7 | 8 | sweeps = 500 9 | K = 1 10 | J = 1 11 | H = 0.1 12 | Lattice = 40 13 | relax = 100 14 | 15 | font = {'family': "Times New Roman", "weight":"normal", "size":16,} 16 | 17 | def left(x, y): 18 | if x < 0.5: return [Lattice - 1, y] 19 | else: return [x - 1, y] 20 | 21 | def right(x, y): 22 | if x > Lattice - 1.5: return [0, y] 23 | else: return [x + 1, y] 24 | 25 | def up(x, y): 26 | if y < 0.5: return [x, Lattice - 1] 27 | else: return [x, y - 1] 28 | 29 | def down(x, y): 30 | if y > Lattice - 1.5: return [x, 0] 31 | else: return [x, y + 1] 32 | 33 | #@jit 34 | def wolff_cluster(spin, t): 35 | mag = [] 36 | mag_2 = [] 37 | for k in range(sweeps + relax): 38 | x = np.random.randint(0, Lattice) 39 | y = np.random.randint(0, Lattice) 40 | sign = spin[x, y] 41 | P_add = 1 - np.exp(-(2*J + H) / t) 42 | stack = [[x, y]] 43 | lable = np.ones([Lattice, Lattice], int) 44 | lable[x, y] = 0 45 | 46 | while len(stack) > 0.5: 47 | 48 | # While stack is not empty, pop and flip a spin 49 | [x_site, y_site] = stack.pop() 50 | spin[x_site, y_site] = -sign 51 | 52 | # Append neighbor spins 53 | 54 | # Left neighbor 55 | 56 | [leftx, lefty] = left(x_site, y_site) 57 | 58 | if spin[leftx, lefty] * sign > 0.5 and np.random.rand() < P_add: 59 | stack.append([leftx, lefty]) 60 | lable[leftx, lefty] = 0 61 | 62 | # Right neighbor 63 | 64 | [rightx, righty] = right(x_site, y_site) 65 | 66 | if spin[rightx, righty] * sign > 0.5 and np.random.rand() < P_add: 67 | stack.append([rightx, righty]) 68 | lable[rightx, righty] = 0 69 | 70 | # Up neighbor 71 | 72 | [upx, upy] = up(x_site, y_site) 73 | 74 | if spin[upx, upy] * sign > 0.5 and np.random.rand() < P_add: 75 | stack.append([upx, upy]) 76 | lable[upx, upy] = 0 77 | 78 | # Down neighbor 79 | 80 | [downx, downy] = down(x_site, y_site) 81 | 82 | if spin[downx, downy] * sign > 0.5 and np.random.rand() < P_add: 83 | stack.append([downx, downy]) 84 | lable[downx, downy] = 0 85 | 86 | if k > relax: 87 | M = np.abs(np.mean(spin)) 88 | M_2 = np.square(M) 89 | mag_2.append(M_2) 90 | mag.append(M) 91 | 92 | return mag, mag_2 93 | 94 | 95 | def main_loop(t): 96 | spin = np.ones([Lattice, Lattice], dtype=int) 97 | 98 | mag, mag_2 = wolff_cluster(spin, t) 99 | 100 | result = np.mean(mag) 101 | print("t is", t, "result is:", result) 102 | test = [] 103 | 104 | # print("result is:", spin) 105 | result_2 = np.mean(mag_2) 106 | result_C = (result_2 - result ** 2) / t # 求磁比热 107 | 108 | return result_2 109 | 110 | def save_data(data): 111 | save_path = "M_T_H_{}".format(H) 112 | if os.path.exists(save_path): 113 | print("save path {} exist".format(save_path)) 114 | else: 115 | print("save path {} not exist".format(save_path)) 116 | os.makedirs(save_path) 117 | print("now makefir the save_path") 118 | 119 | np.savetxt(save_path + "/M_T.txt", data) 120 | 121 | 122 | def single_run(): 123 | start = time.time() 124 | for T in range(3, 30, 1): 125 | main_loop(T) 126 | 127 | end = time.time() 128 | print("run time is:", (end - start)) 129 | 130 | def plot(data): 131 | t = [] 132 | kxy = [] 133 | print(data) 134 | for i in range(len(data)): 135 | t.append(data[i][0]) 136 | kxy.append(data[i][1]) 137 | 138 | #print("t is:", t, "kxy is:", kxy) 139 | plt.plot(t, kxy, "o-") 140 | plt.show() 141 | 142 | def multi_run(): 143 | start = time.time() 144 | T = [round(t,2) for t in np.linspace(0.8, 4, 33)] 145 | M = [] 146 | ing_argv = [] 147 | for i in range(len(T)) : 148 | ing_argv.append(T[i]) 149 | 150 | with Pool(8) as p: 151 | M.append(p.map(main_loop, ing_argv)) 152 | 153 | print(M[0]) 154 | 155 | data = [] 156 | for i in range(len(T)): 157 | data.append([T[i], M[0][i]]) 158 | 159 | save_data(data) 160 | plot(data) 161 | 162 | 163 | if __name__ == "__main__": 164 | multi_run() -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.0/C_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 0.000000000000000000e+00 2 | 9.000000000000000222e-01 4.105293635900706295e-08 3 | 1.000000000000000000e+00 7.211316399580169900e-08 4 | 1.100000000000000089e+00 2.188974413851564057e-07 5 | 1.199999999999999956e+00 5.616087719872986384e-07 6 | 1.300000000000000044e+00 1.168621735534959843e-06 7 | 1.399999999999999911e+00 2.590046253501756214e-06 8 | 1.500000000000000000e+00 4.171987930308891826e-06 9 | 1.600000000000000089e+00 7.167320299344970280e-06 10 | 1.699999999999999956e+00 1.451352545942138466e-05 11 | 1.800000000000000044e+00 2.535577305790632141e-05 12 | 1.899999999999999911e+00 4.841281946870402492e-05 13 | 2.000000000000000000e+00 1.025404955702913234e-04 14 | 2.100000000000000089e+00 1.955802709053240467e-04 15 | 2.200000000000000178e+00 4.599290012892064063e-04 16 | 2.299999999999999822e+00 2.231831180003932859e-03 17 | 2.399999999999999911e+00 1.403303810882457724e-02 18 | 2.500000000000000000e+00 1.276827738141613801e-02 19 | 2.600000000000000089e+00 5.748922154467717323e-03 20 | 2.700000000000000178e+00 3.052804139601105089e-03 21 | 2.799999999999999822e+00 2.023992221129921297e-03 22 | 2.899999999999999911e+00 1.158140325291589666e-03 23 | 3.000000000000000000e+00 1.271265539596227014e-03 24 | 3.100000000000000089e+00 1.606350828718007290e-03 25 | 3.200000000000000178e+00 3.167177055137329351e-04 26 | 3.299999999999999822e+00 3.928119987165686099e-04 27 | 3.399999999999999911e+00 2.856452140104799716e-04 28 | 3.500000000000000000e+00 9.689644820703528219e-04 29 | 3.600000000000000089e+00 2.484994203503867213e-04 30 | 3.700000000000000178e+00 1.789418760610643022e-04 31 | 3.799999999999999822e+00 3.076894943245842918e-04 32 | 3.899999999999999911e+00 2.579240233717522662e-04 33 | 4.000000000000000000e+00 1.527249325805918215e-04 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.0/M_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 1.000000000000000000e+00 2 | 9.000000000000000222e-01 9.999599448897794618e-01 3 | 1.000000000000000000e+00 9.998598321643287212e-01 4 | 1.100000000000000089e+00 9.995694451402804548e-01 5 | 1.199999999999999956e+00 9.990590618737474893e-01 6 | 1.300000000000000044e+00 9.980084794589179076e-01 7 | 1.399999999999999911e+00 9.960840149048096892e-01 8 | 1.500000000000000000e+00 9.918671968937875505e-01 9 | 1.600000000000000089e+00 9.856570578657315451e-01 10 | 1.699999999999999956e+00 9.775882483717432958e-01 11 | 1.800000000000000044e+00 9.635892722945892386e-01 12 | 1.899999999999999911e+00 9.421241639529057688e-01 13 | 2.000000000000000000e+00 9.141446862474950086e-01 14 | 2.100000000000000089e+00 8.687542835671343555e-01 15 | 2.200000000000000178e+00 7.970873841432866147e-01 16 | 2.299999999999999822e+00 6.632421593186372810e-01 17 | 2.399999999999999911e+00 3.396669777054107664e-01 18 | 2.500000000000000000e+00 9.184285445891782196e-02 19 | 2.600000000000000089e+00 4.326123496993988099e-02 20 | 2.700000000000000178e+00 2.360538264028055888e-02 21 | 2.799999999999999822e+00 1.692223822645290421e-02 22 | 2.899999999999999911e+00 7.960239228456914007e-03 23 | 3.000000000000000000e+00 6.795334418837675576e-03 24 | 3.100000000000000089e+00 6.322125501002004259e-03 25 | 3.200000000000000178e+00 6.389159569138276595e-03 26 | 3.299999999999999822e+00 3.754718812625250715e-03 27 | 3.399999999999999911e+00 2.932756137274549705e-03 28 | 3.500000000000000000e+00 5.319332414829659739e-03 29 | 3.600000000000000089e+00 4.647554483967935961e-03 30 | 3.700000000000000178e+00 3.343004759519038489e-03 31 | 3.799999999999999822e+00 2.755911823647294676e-03 32 | 3.899999999999999911e+00 3.854054984969939798e-03 33 | 4.000000000000000000e+00 4.771806112224449316e-03 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.05/C_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 3.906234502526473307e-09 2 | 9.000000000000000222e-01 2.062408046812593920e-08 3 | 1.000000000000000000e+00 4.555704580244679391e-08 4 | 1.100000000000000089e+00 1.690391166436261705e-07 5 | 1.199999999999999956e+00 3.677507819562405561e-07 6 | 1.300000000000000044e+00 8.199109796231267679e-07 7 | 1.399999999999999911e+00 2.068336501197849020e-06 8 | 1.500000000000000000e+00 3.599968808609688153e-06 9 | 1.600000000000000089e+00 6.776790994339476626e-06 10 | 1.699999999999999956e+00 9.575668784175328109e-06 11 | 1.800000000000000044e+00 2.078457599906534509e-05 12 | 1.899999999999999911e+00 3.672542585445757134e-05 13 | 2.000000000000000000e+00 5.588349795782043827e-05 14 | 2.100000000000000089e+00 1.254609192140915979e-04 15 | 2.200000000000000178e+00 2.881657489692016029e-04 16 | 2.299999999999999822e+00 7.313020492934016892e-04 17 | 2.399999999999999911e+00 6.175685931607693968e-03 18 | 2.500000000000000000e+00 1.617056274874396557e-02 19 | 2.600000000000000089e+00 7.871496253838030852e-03 20 | 2.700000000000000178e+00 3.778898650302907596e-03 21 | 2.799999999999999822e+00 2.861399809890322488e-03 22 | 2.899999999999999911e+00 1.555624902108827985e-03 23 | 3.000000000000000000e+00 8.429411177063543667e-04 24 | 3.100000000000000089e+00 1.558886908862006700e-03 25 | 3.200000000000000178e+00 8.824592760169644272e-04 26 | 3.299999999999999822e+00 7.879912296202296615e-04 27 | 3.399999999999999911e+00 7.880361653106992539e-04 28 | 3.500000000000000000e+00 6.417932701934066968e-04 29 | 3.600000000000000089e+00 2.449786569353359618e-04 30 | 3.700000000000000178e+00 5.775084772534836005e-04 31 | 3.799999999999999822e+00 3.099371767213364415e-04 32 | 3.899999999999999911e+00 4.656750973042124062e-04 33 | 4.000000000000000000e+00 1.093819998463861339e-04 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.05/M_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 1.000000000000000000e+00 2 | 9.000000000000000222e-01 9.999799787074148405e-01 3 | 1.000000000000000000e+00 9.999349104458918891e-01 4 | 1.100000000000000089e+00 9.997096442885772261e-01 5 | 1.199999999999999956e+00 9.993442290831663355e-01 6 | 1.300000000000000044e+00 9.984586297595192672e-01 7 | 1.399999999999999911e+00 9.967886460420841699e-01 8 | 1.500000000000000000e+00 9.937827968436874215e-01 9 | 1.600000000000000089e+00 9.882068355460920106e-01 10 | 1.699999999999999956e+00 9.809131575651303114e-01 11 | 1.800000000000000044e+00 9.693474480210421129e-01 12 | 1.899999999999999911e+00 9.525192009018036421e-01 13 | 2.000000000000000000e+00 9.285229646793587976e-01 14 | 2.100000000000000089e+00 8.927532565130260611e-01 15 | 2.200000000000000178e+00 8.409409130761522633e-01 16 | 2.299999999999999822e+00 7.434420309368736657e-01 17 | 2.399999999999999911e+00 5.818025832915831863e-01 18 | 2.500000000000000000e+00 2.000313063627254528e-01 19 | 2.600000000000000089e+00 5.583889028056112508e-02 20 | 2.700000000000000178e+00 2.837417647795591191e-02 21 | 2.799999999999999822e+00 1.774258517034067728e-02 22 | 2.899999999999999911e+00 9.641266908817634762e-03 23 | 3.000000000000000000e+00 8.756406563126253526e-03 24 | 3.100000000000000089e+00 8.751640781563124749e-03 25 | 3.200000000000000178e+00 5.946799849699398524e-03 26 | 3.299999999999999822e+00 5.137853832665331168e-03 27 | 3.399999999999999911e+00 5.256121618236474051e-03 28 | 3.500000000000000000e+00 2.808755010020039938e-03 29 | 3.600000000000000089e+00 5.387913326653306217e-03 30 | 3.700000000000000178e+00 3.827470566132265117e-03 31 | 3.799999999999999822e+00 5.973869614228456866e-03 32 | 3.899999999999999911e+00 6.746098446893787452e-03 33 | 4.000000000000000000e+00 2.799048096192384948e-03 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.1/C_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 3.906234502526473307e-09 2 | 9.000000000000000222e-01 0.000000000000000000e+00 3 | 1.000000000000000000e+00 5.181957085653721151e-08 4 | 1.100000000000000089e+00 1.137271808040261209e-07 5 | 1.199999999999999956e+00 2.785610465442604480e-07 6 | 1.300000000000000044e+00 7.283431516876739860e-07 7 | 1.399999999999999911e+00 1.212129383214894774e-06 8 | 1.500000000000000000e+00 2.809183831923670833e-06 9 | 1.600000000000000089e+00 5.003133144998450632e-06 10 | 1.699999999999999956e+00 8.557600583648713864e-06 11 | 1.800000000000000044e+00 1.440601783617465281e-05 12 | 1.899999999999999911e+00 3.137789263804445532e-05 13 | 2.000000000000000000e+00 5.061032008896182433e-05 14 | 2.100000000000000089e+00 7.291539969901341843e-05 15 | 2.200000000000000178e+00 1.519886705914075264e-04 16 | 2.299999999999999822e+00 3.171226460064108309e-04 17 | 2.399999999999999911e+00 2.023896030203300219e-03 18 | 2.500000000000000000e+00 1.555692597118081208e-02 19 | 2.600000000000000089e+00 1.081957497351725833e-02 20 | 2.700000000000000178e+00 5.925812412139651320e-03 21 | 2.799999999999999822e+00 4.003236876522872807e-03 22 | 2.899999999999999911e+00 2.077277992513256247e-03 23 | 3.000000000000000000e+00 9.901349208102242297e-04 24 | 3.100000000000000089e+00 1.201231228861499717e-03 25 | 3.200000000000000178e+00 8.042417265015999002e-04 26 | 3.299999999999999822e+00 7.504149460030160772e-04 27 | 3.399999999999999911e+00 7.179886377434819634e-04 28 | 3.500000000000000000e+00 2.671731740033173952e-04 29 | 3.600000000000000089e+00 7.117686302241537808e-04 30 | 3.700000000000000178e+00 8.098549913562702292e-04 31 | 3.799999999999999822e+00 1.818957831811708549e-04 32 | 3.899999999999999911e+00 2.026391337991742233e-04 33 | 4.000000000000000000e+00 3.943284301569074859e-04 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/M_T_H_0.1/M_T.txt: -------------------------------------------------------------------------------- 1 | 8.000000000000000444e-01 9.999949931112225299e-01 2 | 9.000000000000000222e-01 9.999949931112225299e-01 3 | 1.000000000000000000e+00 9.999399173346693592e-01 4 | 1.100000000000000089e+00 9.998197645290580748e-01 5 | 1.199999999999999956e+00 9.995144319889778250e-01 6 | 1.300000000000000044e+00 9.987086923847696696e-01 7 | 1.399999999999999911e+00 9.973684681863727031e-01 8 | 1.500000000000000000e+00 9.951949179609218543e-01 9 | 1.600000000000000089e+00 9.907611034569137054e-01 10 | 1.699999999999999956e+00 9.851041301352704282e-01 11 | 1.800000000000000044e+00 9.749858341683367291e-01 12 | 1.899999999999999911e+00 9.620713301603206480e-01 13 | 2.000000000000000000e+00 9.407649830911822830e-01 14 | 2.100000000000000089e+00 9.101985377004008226e-01 15 | 2.200000000000000178e+00 8.684468061122244675e-01 16 | 2.299999999999999822e+00 7.942656531813627740e-01 17 | 2.399999999999999911e+00 6.853488539579157823e-01 18 | 2.500000000000000000e+00 4.758517691633266655e-01 19 | 2.600000000000000089e+00 1.273416739729458791e-01 20 | 2.700000000000000178e+00 4.705280247995991677e-02 21 | 2.799999999999999822e+00 1.910887086673346627e-02 22 | 2.899999999999999911e+00 1.412904872244489206e-02 23 | 3.000000000000000000e+00 1.432165894288577046e-02 24 | 3.100000000000000089e+00 8.396157940881764439e-03 25 | 3.200000000000000178e+00 7.753510145290580698e-03 26 | 3.299999999999999822e+00 9.444445140280560702e-03 27 | 3.399999999999999911e+00 8.814234719438877180e-03 28 | 3.500000000000000000e+00 6.174398797595189760e-03 29 | 3.600000000000000089e+00 1.025613727454909724e-02 30 | 3.700000000000000178e+00 5.250501002004008370e-03 31 | 3.799999999999999822e+00 4.538887149298596185e-03 32 | 3.899999999999999911e+00 2.634475200400801694e-03 33 | 4.000000000000000000e+00 3.355673847695390434e-03 34 | -------------------------------------------------------------------------------- /2DIsing_wolff_python/plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import math 4 | from scipy.optimize import curve_fit 5 | import os, glob 6 | 7 | H = [0.0, 0.05, 0.1] 8 | 9 | def load_npy(root): 10 | 11 | 12 | 13 | path = [] 14 | 15 | for i in range(len(H)): 16 | path.append(os.path.join(root + "/M_T_H_{}".format(H[i]), "M_T.txt")) 17 | 18 | print("path is:", path) 19 | M = [] 20 | T = [] 21 | 22 | for i in range(len(path)): 23 | M_h, T_h = [], [] 24 | data = np.loadtxt(path[i]) 25 | for i in range(len(data)): 26 | T_h.append(data[i][0]) 27 | M_h.append(data[i][1]) 28 | M.append(M_h) 29 | T.append(T_h) 30 | 31 | print("M is:", M) 32 | 33 | return T, M 34 | 35 | def load_dat(data_path): 36 | 37 | T, M, C = [], [], [] 38 | 39 | for line in open(data_path, "r"): 40 | tmp = line.split( ) 41 | T.append(float(tmp[0])) 42 | M.append(float(tmp[1])) 43 | 44 | return T, M 45 | 46 | 47 | def plot(T, M): 48 | 49 | for i in range(len(T)): 50 | plt.plot(T[i],M[i],"o-", label="H="+str(H[i])) 51 | 52 | plt.legend(loc="upper right", prop = {'family': "Times New Roman", "weight":"normal", "size":16,}, frameon=False) 53 | 54 | plt.show() 55 | 56 | if __name__=="__main__": 57 | path = "D:/Monte-Carlo-Algorithm-master/Monte-Carlo-Algorithm-master/2DIsing_wolff_python" 58 | T, M = load_npy(path) 59 | plot(T, M) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Monte-Carlo-Algorithm 2 | 3 | This Project include Monte-Carlo simulation for various spin models: 4 | 5 | (1) 2D-Ising model 6 | 7 | (2) 2D XY model 8 | 9 | (3) 2D XXZ model 10 | 11 | (4) Heisenberg model 12 | 13 | In addition, different algorithms are implemented, including: 14 | 15 | (1) metropolics (single-flip method) (2) Wolff cluster method 16 | 17 | Above simulation coded by fortran, python and C++, respectively. 18 | -------------------------------------------------------------------------------- /XYmodel_metropolis_python/PTMC.py: -------------------------------------------------------------------------------- 1 | from xymodel_mc import * 2 | import numpy as np 3 | 4 | L = 50 5 | beta = 1 6 | J = 5 7 | steps = 1000000 8 | V = [] 9 | M2 = [] 10 | C = [] 11 | 12 | def single_run(): 13 | for T in range(4, 25): 14 | t = T/10 15 | sim = XYMetropolis((L, L),beta=1/t,J=J,random_state=5,initial_state='hot') 16 | sim.simulate(steps) 17 | V.append(sim.Vdensity) 18 | M2.append(sim.M2) 19 | C.append(sim.C) 20 | 21 | if __name__=="__mian__": 22 | single_run() -------------------------------------------------------------------------------- /XYmodel_metropolis_python/plot_votex.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pylab as plt 2 | import matplotlib.animation as animation 3 | import matplotlib.patches as patches 4 | from matplotlib.collections import PatchCollection 5 | import matplotlib.cm as cm 6 | from matplotlib.colors import Normalize 7 | import numpy as np 8 | from xymodel_mc import * 9 | 10 | def visualise(sim): 11 | X = np.arange(sim.A.size).reshape(sim.A.shape) % sim.A.shape[0] 12 | Y = (np.arange(sim.A.size).reshape(sim.A.shape) % sim.A.shape[1]).T 13 | #X, Y = np.meshgrid((0, sim.A[0], .2), np.arange(0, sim.A[1], .2)) 14 | 15 | U = cos(sim.A) 16 | V = sin(sim.A) 17 | 18 | fig, ax = plt.subplots() 19 | 20 | # rects = [] 21 | # 22 | # # create rectangles for vortex/antivortex determination 23 | # for i in range(sim.V.shape[0]): 24 | # for j in range(sim.V.shape[1]): 25 | # rect = patches.Rectangle(xy=(i, j), height=1, width=1) 26 | # rects.append(rect) 27 | # rects = PatchCollection(rects) 28 | # 29 | # # Set colors for the rectangles 30 | # col = 'RdBu' 31 | # r_cmap = plt.get_cmap(col) 32 | # r_cmap_r = plt.get_cmap(col + "_r") # eto kostil' =) 33 | # rects.set_cmap(r_cmap) 34 | # rects.set_clim(vmin=-1, vmax=1) 35 | # 36 | # rects.set_animated(True) 37 | # rects.set_array(sim.V.flatten('F') / 2) 38 | # ax.add_collection(rects) 39 | # 40 | # # create legend 41 | # legend_boxes = [patches.Patch(facecolor=r_cmap(0.7), label='Antiortex'), 42 | # patches.Patch(facecolor=r_cmap_r(0.7), label='Vortex')] 43 | # ax.legend(handles=legend_boxes) 44 | 45 | # build an initial quiver plot 46 | #q = ax.quiver(X, Y, U, V, pivot='mid', cmap=plt.cm.get_cmap('hsv'), units='inches', scale=4) 47 | q = ax.quiver(X, Y, U, V, units='width') 48 | qk = ax.quiverkey(q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',coordinates='figure') 49 | #fig.colorbar(q, label='Angles (2 pi)') 50 | # ax.set_xlim(-1, sim.A.shape[0]) 51 | # ax.set_ylim(-1, sim.A.shape[1]) 52 | #qk.set_UVC(U, V, C=sim.A) 53 | 54 | plt.show() 55 | 56 | return q, fig 57 | 58 | 59 | if __name__=="__main__": 60 | sim = XYMetropolis((50,50), 61 | beta=1, 62 | J=10, 63 | random_state=5, 64 | initial_state='hot') 65 | sim.simulate(1000000) 66 | visualise(sim) 67 | -------------------------------------------------------------------------------- /XYmodel_metropolis_python/xymodel_mc.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def cos(angle): 4 | """transforms angle from [0,1] to cos(2pi[0,1])""" 5 | return np.cos(2 * np.pi * angle) 6 | 7 | 8 | def sin(angle): 9 | """transforms angle from [0,1] to sin(2pi[0,1])""" 10 | return np.sin(2 * np.pi * angle) 11 | 12 | 13 | class XYMetropolis: 14 | 15 | def __init__(self, 16 | lattice_shape, 17 | beta=1, 18 | J=5, 19 | random_state=5, 20 | initial_state='hot'): 21 | self.beta = beta 22 | self.J = J 23 | self.rs = np.random.RandomState(seed=random_state) 24 | 25 | # matrix of lattice angles 26 | if initial_state == 'hot': 27 | self.A = self.rs.rand(*lattice_shape) 28 | elif initial_state == 'cold': 29 | self.A = np.zeros(lattice_shape) 30 | else: 31 | raise ValueError('initial_state must be cold or hot') 32 | self.time = 0 33 | 34 | # Matrix of winding numbers 35 | self.V = np.zeros((lattice_shape[0] - 1, lattice_shape[1] - 1)) 36 | 37 | # Correlations (since we have torus topology, we can start from the left top) 38 | self.corr_range = int(self.A.shape[0] / 2) 39 | 40 | # Correlation array of length corr_range 41 | self.C = [] 42 | 43 | # Magnetization 44 | self.M = 0 45 | 46 | # Squared magnetization 47 | self.M2 = 0 48 | 49 | # Vortex density 50 | self.Vdensity = 0 51 | 52 | def step(self): 53 | """Perform one step of metropolis algorithm""" 54 | pos = tuple(self.rs.randint(_) for _ in self.A.shape) 55 | value = self.rs.rand() 56 | delta_H = self.dH(pos, value) 57 | if (delta_H < 0) or (self.rs.rand() < np.exp(-self.beta * delta_H)): 58 | self.A[pos] = value 59 | 60 | def dH(self, pos, val): 61 | """Calculate delta energy""" 62 | delta = 0 63 | old_val = self.A[pos] 64 | pos_list = list(pos) 65 | incr_delta = lambda pos: cos(self.A[pos] - val) - cos(self.A[pos] - old_val) 66 | for i in range(len(self.A.shape)): 67 | pos_list[i] += 1 68 | pos_list[i] %= self.A.shape[i] 69 | delta += incr_delta(tuple(pos_list)) 70 | pos_list[i] -= 2 71 | pos_list[i] %= self.A.shape[i] 72 | delta += incr_delta(tuple(pos_list)) 73 | pos_list[i] += 1 74 | pos_list[i] %= self.A.shape[i] 75 | return -delta * self.J 76 | 77 | def get_V(self): 78 | """Update matrix of winding numbers (Vortex matrix)""" 79 | for i in range(self.V.shape[0]): 80 | for j in range(self.V.shape[1]): 81 | 82 | # create list of angles from the below-down square 83 | a = [self.A[i, j], 84 | self.A[i, j + 1], 85 | self.A[i + 1, j + 1], 86 | self.A[i + 1, j]] 87 | 88 | # run clockwise and calculate sum of angles 89 | a_sum = 0 90 | for k in range(len(a)): 91 | d = a[k] - a[(k + 1) % len(a)] 92 | if abs(d) > 0.5: 93 | d -= np.sign(d) 94 | a_sum += d 95 | 96 | self.V[i, j] = a_sum 97 | 98 | return self.V 99 | 100 | def get_Vdensity(self): 101 | self.Vdensity = np.sum(abs(self.V)) / 2 / np.prod(self.A.shape) 102 | return self.Vdensity 103 | 104 | def get_C(self): 105 | """Update correlations""" 106 | corrs_d = [] # correlations for each dim 107 | self.C = [] 108 | 109 | # compute correlations over each shift (here means distance) 110 | for r in range(int(self.corr_range)): 111 | # and each axis 112 | for d in range(len(self.A.shape)): 113 | # calculate mean over all spins 114 | corr = np.mean(cos(self.A - np.roll(self.A, r, axis=d))) 115 | corrs_d.append(corr) 116 | self.C.append(np.mean(corrs_d)) 117 | return self.C 118 | 119 | def get_M2(self): 120 | """Get squared magnetization""" 121 | self.M2 = ((np.sum(cos(self.A))) ** 2 + (np.sum(sin(self.A))) ** 2) / (np.prod(self.A.shape)) ** 2 122 | return self.M2 123 | 124 | def simulate(self, steps): 125 | for _ in range(steps): 126 | self.step() 127 | self.get_V() 128 | self.get_C() 129 | self.get_M2() 130 | self.get_Vdensity() 131 | 132 | 133 | if __name__=="__main__": 134 | sim = XYMetropolis((50,50), 135 | beta=1, 136 | J=5, 137 | random_state=5, 138 | initial_state='hot') --------------------------------------------------------------------------------