# Constructs a list neigh[1:4,1:nn] of neighbors of each site function lattice(ll) neigh=Array{Int}(undef,4,nn) for s0=1:ll*ll x0=mod(s0-1,ll) y0=div(s0-1,ll) x1=mod(x0+1,ll) x2=mod(x0-1+ll,ll) y1=mod(y0+1,ll) y2=mod(y0-1+ll,ll) neigh[1,s0]=1+x1+y0*ll neigh[2,s0]=1+x0+y1*ll neigh[3,s0]=1+x2+y0*ll neigh[4,s0]=1+x0+y2*ll end return neigh end # Constructs the initial random spin configuration function initspin(nn::Int) spin=Array{Int32}(undef,nn) for i=1:nn spin[i]=rand(-1:2:1) end return spin end # Performs one MC sweep. This version computes the neighbors on the fly. function mcstep1(ll::Int,pflp,spin) nn::Int=ll*ll for i=1:nn s::Int=rand(1:nn) x::Int=mod(s-1,ll) y::Int=div(s-1,ll) s1::Int=spin[1+mod(x+1,ll)+y*ll] s2::Int=spin[1+x+mod(y+1,ll)*ll] s3::Int=spin[1+mod(x-1,ll)+y*ll] s4::Int=spin[1+x+mod(y-1,ll)*ll] if rand()