在 Python 中模拟伊辛模型

Simulating the Ising Model in Python

我自学了 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 进行 Monte Carlo 模拟并为我的网格图设置动画感到不舒服 - 这是否有意义?

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

另外,请告诉我有关 KeyError 和白条的信息。

'KeyError' 结果是 -

Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
      return self.func(*args)
   File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
   func(*args)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 147, in _on_timer
      TimerBase._on_timer(self)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1305, in _on_timer
      ret = func(*args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 1049, in _step
      still_going = Animation._step(self, *args)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 855, in _step
      self._draw_next_frame(framedata, self._blit)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 873, in _draw_next_frame
      self._pre_draw(framedata, blit)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 886, in _pre_draw
      self._blit_clear(self._drawn_artists, self._blit_cache)
   File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 926, in _blit_clear
      a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x7fd468b2f2d0>

你一次问了很多问题。

  • KeyError:无法重现。奇怪的是它应该只发生在某些数组大小而不是其他数组大小上。后端可能有问题,您可以通过将这些行放在脚本顶部来尝试使用不同的后端
    import matplotlib
    matplotlib.use("Qt4Agg")
  • 白带:也无法重现,但可能来自自动轴缩放。为避免这种情况,您可以手动设置轴限制 plt.xlim(0,l-1) plt.ylim(0,l-1)
  • 使用FuncAnimation 进行Monte Carlo 模拟 非常好。当然这不是最快的方法,但如果你想在屏幕上跟随你的模拟,那没有错。然而,有人可能会问为什么每个时间单位只有一次自旋翻转。但这更多的是物理问题而不是编程问题。

  • 冷启动红屏:在冷启动的情况下,您仅用 1 初始化您的网格。这意味着网格中的最小 最大值为 1。因此,pcolormesh 的颜色图被归一化到范围 [1,1] 并且全是红色。一般来说,您希望颜色图跨越 [-1,1],这可以使用 vminvmax 参数来完成。

    mesh = plt.pcolormesh(X, Y, L, cmap = plt.cm.RdBu, vmin=-1, vmax=1)
    这也应该为 "cold start".

  • 提供预期的行为