2017-02-03 123 views
3

我自学了Metropolis算法,并决定尝试在Python中进行编码。我选择模拟Ising模型。我的Python爱好者了解和这里就是我想出了 -在Python中模拟Ising模型

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异常”当我尝试上面,一个16×16或任何东西,它看起来并且工作正常。现在我想知道的是,如果这是正确的,因为 -

我对我如何使用FuncAnimation进行蒙特卡罗模拟和动画我的网格绘图很不舒服 - 这是否有意义?

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

另外,请告诉我关于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> 
+0

在哪里特别是你会得到一个KeyError?你能提供一个堆栈跟踪吗? – lordingtar

回答

3

你问了很多问题,在同一时间。

  • KeyError:不能再现。奇怪的是,它应该只发生在一些数组大小而不是其他数组中。也许事情是错误的后端,您可以尝试通过将这些线路在脚本
    import matplotlib
    matplotlib.use("Qt4Agg")
  • 白色条纹的顶部使用一个不同:也不能被复制,但可能他们来自一个自动轴缩放。为了避免这种情况,您可以设置轴限制手动 plt.xlim(0,l-1) plt.ylim(0,l-1)
  • 使用FuncAnimation做蒙特卡罗模拟是完全没有问题。当然,这不是最快的方法,但如果你想在屏幕上跟随你的模拟,那没有什么问题。然而,人们可能会问,为什么每个时间单位只有一个自旋翻转。但这更多的是关于物理学的问题,而不是编程问题。

  • 用于冷启动的红色屏幕:在冷启动的情况下,仅用1 s初始化电网。这意味着网格中的最小值最大值为1。因此,pcolormesh的颜色表被标准化为范围[1,1],全部为红色。一般而言,您希望颜色图跨越[-1,1],这可以使用vminvmax参数完成。

    mesh = plt.pcolormesh(X, Y, L, cmap = plt.cm.RdBu, vmin=-1, vmax=1)
    这应该会给你“冷启动”的预期行为。

+0

我不知道KeyError,但我用可能的解决方案更新了答案。 – ImportanceOfBeingErnest

+0

您的回答帮助我解决了冷启动问题并删除了白色条纹,谢谢!就像你说的那样,这两种情况都是错误的范围。如果你想看看,我还用KeyError问题的堆栈跟踪编辑了我的问题!我确实同意FuncAnimation不会是最快的,我期待着玩,并且实际上看到系统达到平衡。我宁愿看看磁化和自相关函数。但有没有另一种方式来动态更新matplotlib图并可视化模拟? –

+0

除了使用'FuncAnimation'外,您还可以手动或在一个循环内调用'mcstep',这需要添加'fig.canvas.draw()'和可能''plt.pause(1e-5)'来更新绘制并防止窗口变得无法响应。但是让我问一下:不使用FuncAnimation的原因是什么? – ImportanceOfBeingErnest