I wrote some code for the ising model in python (2d). Everything looks seems to be calculating correctly except the number of good and bad steps. Good steps is defined as steps where the spin is changed because the change in energy (d_energy) is less than or equal to zero. A bad step is when the spin is changed because a random integer is less than the value of e^(-d_energy/(Kb * temp) where kb is boltzman’s constant. And there is a no step where d_energy is greater than zero and rand is > than the e^(what i wrote above).
When i talked to a professor about this, he told me that when I make the temp really high, the number of bad steps should be half of the total number of steps
from numpy import zeros
from random import choice, random
import math
def create_lattice(nx,ny):
possibleSpins = [-1,1]
lattice = zeros((nx,ny))
for i in range(nx):
for j in range(ny):
lattice[i,j] = choice(possibleSpins)
return lattice
def ising_model(nsweeps, nx, ny, Js, kb, temp):
s_energy = 0.0
e_energy = 0.0
d_energy = 0.0
spin = 0.0
rand = 0.0
good = 0.0
bad = 0.0
nostep = 0.0
lattice = create_lattice(nx, ny)
energies = zeros((nx,ny))
print(lattice)
# Each sweep is a complete look at the lattice
for sweeps in range(nsweeps):
for i in range(nx):
for j in range(ny):
spin = lattice[i][j]
s_energy = -1 * Js * spin * (lattice[(i-1)%nx][j] + lattice[i][(j-1)%ny] + lattice[i][(j+1)%ny] + lattice[(i+1)%nx][j])
lattice[i][j] = -1 * spin
e_energy = -1 * Js * lattice[i][j] * (lattice[(i-1)%nx][j] + lattice[i][(j-1)%ny] + lattice[i][(j+1)%ny] + lattice[(i+1)%nx][j])
d_energy = e_energy - s_energy
rand = random()
if d_energy <= 0 :
good = good + 1
elif d_energy > 0 and rand <= math.exp(-1 * d_energy / (kb * temp)):
bad = bad + 1
else:
lattice[i][j] = spin
nostep = nostep + 1
print(math.exp(-1 * d_energy / (kb * temp)))
print(rand)
print(lattice)
print(good)
print(bad)
print(nostep)
# energies array is
return energies
ising_model(10,7,7,1.0,1.0,10000000000000000000000000000.0)
There are only 6 possibilities for the delta in energy: -8, -4, 0, 0, 4, 8. I repeat 0 because there are two different configurations with that energy. The probability of each of those is 1/6 for a random configuration. That means 1/3 for d_energy > 0, 1/3 for d_energy < 0 and 1/3 for d_energy == 0.
If you change
if d_energy <= 0 :toif d_energy < 0 :you will get a third “good” a third “bad” and a third “nostep”. I think what your professor meant is that the number of “bad” steps should be the same as the number of “good” step for infinite temperature. Which is the case if you fix the ‘<=’.Edit: you may also want to have a >= for the elif.