Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. T = 300 # Kelvin
  2. kb = 1.38e-34
  3. beta = 1/(kb/T)
  4. steps = 2000
  5. size = 150
  6.  
  7. lattice = np.random.choice([1,-1],size=(size, size))
  8.  
  9. fig = plt.figure(figsize=(10,10))
  10. im = plt.imshow(lattice, interpolation='none')
  11. title = plt.text(0,0,"Iteration: ")
  12.  
  13. def init():
  14.     im.set_data(lattice)
  15.     return im, title
  16.  
  17. def get_spin(m,n):
  18.     global lattice
  19.     if m == 0 or m == lattice.shape[0] or n == 0 or n == lattice.shape[0]:
  20.         return 1
  21.     return lattice[m,n]
  22.  
  23. def neighbor_sum(m,n, N=size):
  24.     global lattice
  25.     a, b = m, n
  26.     return lattice[(a+1)%N,b] + lattice[a,(b+1)%N] + lattice[(a-1)%N,b] + lattice[a,(b-1)%N]
  27.  
  28. def Emn( n, m, size=size):
  29.     global lattice
  30.     cum = 0
  31.     s_mn = lattice[n][m]
  32.     for i in range(n-1, n+2):
  33.         for j in range(m-1, m+2):
  34.             if i==n and j == m: continue;
  35.             cum +=  lattice[i %size, j % size]
  36.     return 2*cum*s_mn
  37.  
  38. def p(emn, beta=beta):
  39.     return np.exp(-1*beta*emn)
  40.  
  41. def flip_energy(m,n):
  42.     global lattice
  43.     e_mn = Emn(m,n)
  44.     if e_mn <= 0:
  45.         lattice[m][n] *= -1
  46.     elif p(e_mn) > np.random.rand():
  47.         lattice[m][n] *= -1
  48.  
  49. def animate(i):
  50.     global lattice
  51.     m,n = np.random.randint(0,lattice.shape[0]-1, size=(2,))
  52.     flip_energy(m,n)
  53.     im.set_data(lattice)
  54.  
  55. def batch_update(i):
  56.     for t in range(200):
  57.         animate(i)
  58.     title.set_text(f"Iteration: {i}")
  59.     if (i%100 == 0):
  60.         print(f"Iteration: {i}")
  61.     return im, title
  62.  
  63. anim = animation.FuncAnimation(fig, batch_update, init_func=init,
  64.                                frames=steps, interval=100)
  65. print("Saving animation...")
  66. anim.save('ising model.mp4',fps=30, extra_args=['-vcodec', 'libx264'])
  67. print("Done :D")
  68. # HTML(anim.to_html5_video())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement