我在python(2d)中为ising模型编写了一些代码。除了好的和坏的步数之外,一切看起来似乎都在正确计算。好的步骤被定义为由于能量的变化(d_energy)小于或等于零而改变自旋的步骤。一个不好的步骤是当旋转被改变时,因为随机整数小于e ^的值( - d_energy /(Kb * temp),其中kb是bolzman的常数。并且d_energy大于零和rand没有步骤是>比e ^(我上面写的)。
当我和一位教授谈到这个问题时,他告诉我,当我把温度调得很高时,不良步骤的数量应该是步骤总数的一半
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)
答案 0 :(得分:2)
能量增量只有6种可能性:-8,-4,0,0,4,8。我重复0,因为有两种不同的能量配置。对于随机配置,每个的概率是1/6。这意味着d_energy&gt;的1/3; 0,1 / 3表示d_energy&lt; 0和1/3表示d_energy == 0。
如果您将if d_energy <= 0 :
更改为if d_energy < 0 :
,您将获得第三个“好”第三个“坏”和第三个“nostep”。我认为你的教授的意思是“坏”步骤的数量应该与无限温度的“好”步数相同。如果您修复'&lt; ='。
编辑:您可能还想为elif设置一个&gt; =