|
| 1 | +from __future__ import print_function |
| 2 | +from __future__ import division |
| 3 | +import numpy as np |
| 4 | +from numpy.random import rand |
| 5 | +import matplotlib |
| 6 | +matplotlib.use('Agg') |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | +from mpi4py import MPI |
| 10 | +comm = MPI.COMM_WORLD |
| 11 | +rank = comm.Get_rank() |
| 12 | +size = comm.Get_size() |
| 13 | + |
| 14 | +#---------------------------------------------------------------------- |
| 15 | +## BLOCK OF FUNCTIONS USED IN THE MAIN CODE |
| 16 | +#---------------------------------------------------------------------- |
| 17 | +def read_input(): |
| 18 | + |
| 19 | + ''' a subroutine to get AO and MO number and number of kpoints and otehr information from input.woops''' |
| 20 | + |
| 21 | + dataset={} |
| 22 | + file = open('input.MC', "r") |
| 23 | + #Default |
| 24 | + data = file.readlines() |
| 25 | + nt = 18 # number of temperature points |
| 26 | + N = 16 # size of the lattice, N x N |
| 27 | + eqSteps = 8000 # number of MC sweeps for equilibration |
| 28 | + mcSteps = 4000 # number of MC sweeps for calculation |
| 29 | + D_data = 1. |
| 30 | + T_low = 1.53 |
| 31 | + T_high = 3.28 |
| 32 | + |
| 33 | + for line in data: |
| 34 | + key, value = line.split("=") |
| 35 | + dataset[key.strip()] = value.strip() |
| 36 | + # Read data |
| 37 | + nt = int(dataset["nt"]) |
| 38 | + N = int(dataset["N"]) |
| 39 | + eqSteps = int(dataset["eqSteps"]) |
| 40 | + mcSteps = int(dataset["mcSteps"]) |
| 41 | + D_data = float(dataset["D_data"]) |
| 42 | + T_low = float(dataset["T_low"]) |
| 43 | + T_high = float(dataset["T_high"]) |
| 44 | + return nt, N, eqSteps, mcSteps, D_data, T_low, T_high |
| 45 | + |
| 46 | +def initialstate(N): |
| 47 | + ''' generates a random spin configuration for initial condition''' |
| 48 | + state = 2*np.random.randint(2, size=(N,N))-1 |
| 49 | + return state |
| 50 | + |
| 51 | + |
| 52 | +def mcmove(config, beta,D_data): |
| 53 | + '''Monte Carlo move using Metropolis algorithm ''' |
| 54 | + for i in range(N): |
| 55 | + for j in range(N): |
| 56 | + a = np.random.randint(0, N) |
| 57 | + b = np.random.randint(0, N) |
| 58 | + s = config[a, b] |
| 59 | + nb = config[(a+1)%N,b] + config[a,(b+1)%N] + config[(a-1)%N,b] + config[a,(b-1)%N] |
| 60 | + nnb = config[(a+1)%N,(b+1)%N] + config[(a+1)%N,(b-1)%N] + config[(a-1)%N,(b-1)%N] + config[(a-1)%N,(b+1)%N] |
| 61 | + cost = 2*s*D_data*nb#+2*0.5*s*nnb |
| 62 | + if cost < 0: |
| 63 | + s *= -1 |
| 64 | + elif rand() < np.exp(-cost*beta): |
| 65 | + s *= -1 |
| 66 | + config[a, b] = s |
| 67 | + return config |
| 68 | + |
| 69 | + |
| 70 | +def calcEnergy(config): |
| 71 | + '''Energy of a given configuration''' |
| 72 | + energy = 0 |
| 73 | + for i in range(len(config)): |
| 74 | + for j in range(len(config)): |
| 75 | + S = config[i,j] |
| 76 | + nb = config[(i+1)%N, j] + config[i,(j+1)%N] + config[(i-1)%N, j] + config[i,(j-1)%N] |
| 77 | + energy += -nb*S |
| 78 | + return energy/4. |
| 79 | + |
| 80 | + |
| 81 | +def calcMag(config): |
| 82 | + '''Magnetization of a given configuration''' |
| 83 | + mag = np.abs(np.sum(config)) |
| 84 | + return mag |
| 85 | + |
| 86 | +######################################################################### |
| 87 | +# Here comes the Model parameters |
| 88 | +######################################################################### |
| 89 | +## change these parameters for a smaller (faster) simulation |
| 90 | +nt, N, eqSteps, mcSteps, D_data, T_low, T_high = read_input() |
| 91 | +######################################################################### |
| 92 | +T = np.linspace(T_low, T_high, nt); |
| 93 | +E,M,C,X = np.zeros(nt), np.zeros(nt), np.zeros(nt), np.zeros(nt) |
| 94 | +E_1,M_1,C_1,X_1 = np.zeros(nt), np.zeros(nt), np.zeros(nt), np.zeros(nt) |
| 95 | +n1, n2 = 1.0/(mcSteps*N*N), 1.0/(mcSteps*mcSteps*N*N) |
| 96 | +# divide by number of samples, and by system size to get intensive values |
| 97 | + |
| 98 | +#---------------------------------------------------------------------- |
| 99 | +# MAIN PART OF THE CODE |
| 100 | +#---------------------------------------------------------------------- |
| 101 | + |
| 102 | +for tt in range(int(nt*rank/size),int(nt*(rank+1)/size)): |
| 103 | + E1 = M1 = E2 = M2 = 0 |
| 104 | + config = initialstate(N) |
| 105 | + iT=1.0/T[tt]; iT2=iT*iT; |
| 106 | + |
| 107 | + for i in range(eqSteps): # equilibrate |
| 108 | + mcmove(config, iT, D_data) # Monte Carlo moves |
| 109 | + |
| 110 | + for i in range(mcSteps): |
| 111 | + mcmove(config, iT, D_data) |
| 112 | + Ene = calcEnergy(config) # calculate the energy |
| 113 | + Mag = calcMag(config) # calculate the magnetisation |
| 114 | + |
| 115 | + E1 = E1 + Ene |
| 116 | + M1 = M1 + Mag |
| 117 | + M2 = M2 + Mag*Mag |
| 118 | + E2 = E2 + Ene*Ene |
| 119 | + |
| 120 | + E_1[tt] = n1*E1 |
| 121 | + M_1[tt] = n1*M1 |
| 122 | + C_1[tt] = (n1*E2 - n2*E1*E1)*iT2 |
| 123 | + X_1[tt] = (n1*M2 - n2*M1*M1)*iT |
| 124 | + |
| 125 | +comm.send(E_1,dest=0,tag=rank) |
| 126 | +comm.send(M_1,dest=0,tag=rank) |
| 127 | +comm.send(C_1,dest=0,tag=rank) |
| 128 | +comm.send(X_1,dest=0,tag=rank) |
| 129 | +#print(rank,E_1) |
| 130 | + |
| 131 | +if rank == 0: |
| 132 | + for i in range(0,size): |
| 133 | + E += comm.recv(source=i,tag=i) |
| 134 | + M += comm.recv(source=i,tag=i) |
| 135 | + C += comm.recv(source=i,tag=i) |
| 136 | + X += comm.recv(source=i,tag=i) |
| 137 | + with open('Energy.txt', 'w') as f: |
| 138 | + for i in range(nt): |
| 139 | + print("{0:4d} {1:5f}".format(i,E[i]),file=f) |
| 140 | + with open('Polarization.txt', 'w') as f: |
| 141 | + for i in range(nt): |
| 142 | + print("{0:4d} {1:5f}".format(i,M[i]),file=f) |
| 143 | + with open('Specific_Heat.txt', 'w') as f: |
| 144 | + for i in range(nt): |
| 145 | + print("{0:4d} {1:5f}".format(i,C[i]),file=f) |
| 146 | + with open('Susceptibility.txt', 'w') as f: |
| 147 | + for i in range(nt): |
| 148 | + print("{0:4d} {1:5f}".format(i,X[i]),file=f) |
| 149 | + |
| 150 | + f = plt.figure(figsize=(18, 10)); # plot the calculated values |
| 151 | + |
| 152 | + sp = f.add_subplot(2, 2, 1 ); |
| 153 | + plt.scatter(T, E, s=50, marker='o', color='IndianRed') |
| 154 | + plt.xlabel("Temperature (T)", fontsize=20); |
| 155 | + plt.ylabel("Energy ", fontsize=20); plt.axis('tight'); |
| 156 | + |
| 157 | + sp = f.add_subplot(2, 2, 2 ); |
| 158 | + plt.scatter(T, abs(M), s=50, marker='o', color='RoyalBlue') |
| 159 | + plt.xlabel("Temperature (T)", fontsize=20); |
| 160 | + plt.ylabel("Magnetization ", fontsize=20); plt.axis('tight'); |
| 161 | + |
| 162 | + sp = f.add_subplot(2, 2, 3 ); |
| 163 | + plt.scatter(T, C, s=50, marker='o', color='IndianRed') |
| 164 | + plt.xlabel("Temperature (T)", fontsize=20); |
| 165 | + plt.ylabel("Specific Heat ", fontsize=20); plt.axis('tight'); |
| 166 | + |
| 167 | + sp = f.add_subplot(2, 2, 4 ); |
| 168 | + plt.scatter(T, X, s=50, marker='o', color='RoyalBlue') |
| 169 | + plt.xlabel("Temperature (T)", fontsize=20); |
| 170 | + plt.ylabel("Susceptibility", fontsize=20); plt.axis('tight'); |
| 171 | + #plt.show() |
| 172 | + plt.savefig("MC.png") |
0 commit comments