I’m new to this neighborhood; I’ve tried my greatest to respect the coverage of the neighborhood. I’ve written the Monte Carlo metropolis algorithm for the ising mannequin. I need to optimize the code. I’ve tried my greatest. I need to optimize it additional. The next is the code:
(I’ve used methods like discovering exponential solely as soon as, cautious era of random quantity, and so forth.)
import numpy as np
import time
import random
def monteCarlo(N,state,Power,Magazine,Beta,sweeps):
if sweeps > 10000:
print("Warning:Variety of sweeps exceeded 10000ntsetting variety of sweeps to 10000")
sweeps = 10000
start_time = time.time()
expBeta = np.exp(-Beta*np.arange(0,9))
E = np.zeros(sweeps)
M = np.zeros(sweeps)
for t in vary(sweeps):
for tt in vary(N*N):
a = random.randint(0, N-1)
b = random.randint(0, N-1)
s = state[a,b]
delta_E = 2*s*(state[(a+1)%N,b] + state[a,(b+1)%N] + state[(a-1)%N,b] + state[a,(b-1)%N])
if delta_E < 0:
s *= -1
Power += delta_E
Magazine += 2*s
elif random.random() < expBeta[delta_E]:
s *= -1
Power += delta_E
Magazine += 2*s
state[a, b] = s
E[t] = Power
M[t] = Magazine
print("%d monte carlo sweeps accomplished in %d seconds" %(sweeps,time.time()-start_time))
return E,M #returning record of Power and Magnetization set
#####lattice config#####
"""N is lattice dimension
nt is variety of Temperature factors
sweeps are variety of mc steps per spin"""
print("Beginning Ising Mannequin Simulation")
N = int(enter("Enter lattice dimension : "))
startTime = time.time()
nt = 10
N2 = N*N
sweeps = 10000 #mc steps per spin
"""we'll plot the next wrt temperature, T"""
T = np.linspace(2, 3, nt) #you may set temperature vary
"""getting ready lattice with all spins up"""
state = np.ones((N,N),dtype="int")
Power = -N2
Magazine = N2
#temperature loop
#for okay in tqdm_gui(vary(nt)):
for okay in vary(nt):
temp = T[k]
Beta=1/temp
print("____________________________________nTemperature is %0.2f, time is %d" %(temp,time.time()-startTime))
E, M = monteCarlo(N,state,Power,Magazine,Beta,sweeps) #record of Power and Magnetization
Power = E[-1]
Magazine = M[-1]
#additional code is discovering magnetization, autocorrelation, particular warmth, autocorrelation, and so forth.```