2D Ising模型获得二维自旋晶格的磁力和能量

时间:2015-12-23 12:25:58

标签: python-2.7

我试图获得二维自旋晶格的磁力和能量,但数字不正确。这里,L是晶格尺寸,mn是磁力,Trange是温度。对于二维晶格,我想用Ising模型模拟磁性和平均能量。但这些数字没有得到纠正。

from random import random
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
L=10
J=1.0
NStep=10 #montcarlo steps
J=1.0
def defS(L):
    S=np.random.choice([1,-1],(L,L))
    return S
def energy(S,L):
    E=0.0
    for i in range (L):
        for j in range (L):
            E+=-S[i,j]*(S[(i+1)%L,j]+S[(i-1)%L,j]+S[i,(j+1)%L]+S[i,(j-1)%L])
    E=E/2.0
    return E
def metropolis(W,S):
    E=energy(S,L)
    mn=sum(S)
    i=np.random.randint(0,L)
    j=np.random.randint(0,L)
    NN=(S[(i+1)%L,j]+S[(i-1)%L,j]+S[i,(j+1)%L]+S[i,(j-1)%L])
    if W[4+NN] >=np.random.rand():
        S[i,j]=-S[i,j]
    return mn,E
Eav=[]
mav=[]
Trange=np.linspace(0.5,4,20)
for T in Trange:
    W=np.zeros(10*10)
    W[4+4]=np.exp(-8*J/T)
    W[4+2]=np.exp(-4*J/T)
    W[4+0]=np.exp(0.0*J/T)
    W[4-2]=np.exp(4.0*J/T)
    W[4-4]=np.exp(8.0*J/T)
    for i in range(NStep):
        for j in range(9):
            (mn,E)=metropolis(W,defS(L))
    Eav.append(E/NStep)
    mav.append(mn/NStep)
print Eav, mav
plot(Trange, Eav, label='E(T)')
show()
plot(Trange, mav, label='E(T)')
show()

0 个答案:

没有答案