ising模型c语言程序,用Python模拟Ising模型

我自学了Metropolis算法,并决定尝试用Python编写它。我选择模拟伊辛模型。我对Python有一个业余的了解,这就是我所想到的-import numpy as np, matplotlib.pyplot as plt, matplotlib.animation as animation

def Ising_H(x,y):

s = L[x,y] * (L[(x+1) % l,y] + L[x, (y+1) % l] + L[(x-1) % l, y] + L[x,(y-1) % l])

H = -J * s

return H

def mcstep(*args): #One Monte-Carlo Step - Metropolis Algorithm

x = np.random.randint(l)

y = np.random.randint(l)

i = Ising_H(x,y)

L[x,y] *= -1

f = Ising_H(x,y)

deltaH = f - i

if(np.random.uniform(0,1) > np.exp(-deltaH/T)):

L[x,y] *= -1

mesh.set_array(L.ravel())

return mesh,

def init_spin_config(opt):

if opt == 'h':

#Hot Start

L = np.random.randint(2, size=(l, l)) #lxl Lattice with random spin configuration

L[L==0] = -1

return L

elif opt =='c':

#Cold Start

L = np.full((l, l), 1, dtype=int) #lxl Lattice with all +1

return L

if __name__=="__main__":

l = 15 #Lattice dimension

J = 0.3 #Interaction strength

T = 2.0 #Temperature

N = 1000 #Number of iterations of MC step

opt = 'h'

L = init_spin_config(opt) #Initial spin configuration

#Simulation Vizualization

fig = plt.figure(figsize=(10, 10), dpi=80)

fig.suptitle("T = %0.1f" % T, fontsize=50)

X, Y = np.meshgrid(range(l), range(l))

mesh = plt.pcolormesh(X, Y, L, cmap = plt.cm.RdBu)

a = animation.FuncAnimation(fig, mcstep, frames = N, interval = 5, blit = True)

plt.show()

除了Tkinter异常的“KeyError”和当我尝试16x16或更高版本时的白色条纹,它看起来和工作都很好。现在我想知道这是否正确,因为-

我对如何使用FuncAnimation来做蒙特卡罗模拟和网格图动画感到不舒服-这有意义吗?在

冷启动怎么样?我得到的只是一个红色的屏幕。在

另外,请告诉我钥匙错误和白带。在

“键盘错误”是-

^{pr2}$

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值