celestialgod

Ising model

Apr 19th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.25 KB | None | 0 0
  1. library(lattice)
  2. range_f = function(X, loc) c(X[loc[1], c(loc[2]-1, loc[2]+1)], X[c(loc[1]-1, loc[1]+1), loc[2]])
  3. ising_model = function(N, beta, n, range_f){
  4.   X = replicate(N+2, rbinom(N+2, 1, .5))
  5.   for (i in 1:n)
  6.   {
  7.       X[c(1, N+2),] = X[c(N+1, 2),]
  8.       X[,c(1, N+2)] = X[,c(N+1, 2)]
  9.       loc = floor(N*runif(2)) + 2
  10.       if (runif(1) < exp(2*beta*(2-sum(range_f(X, loc) == X[loc[1], loc[2]]))))
  11.         X[loc[1],loc[2]] = 1 - X[loc[1],loc[2]]
  12.       if(i %% 50 == 0)
  13.       {
  14.         print(levelplot(X, col.regions = rgb(0,0,0,seq(0,1,length=16))))
  15.       }
  16.   }
  17. }
  18. library(animation)
  19. ani.options(ffmpeg = "C:/ffmpeg/bin/ffmpeg.exe", interval = 1/25)
  20. saveVideo(ising_model(100, -1, 100000, range_f), "beta_is_-1.mp4")
  21. saveVideo(ising_model(100, 2, 100000, range_f), "beta_is_2.mp4")
  22. saveVideo(ising_model(100, 6, 100000, range_f), "beta_is_6.mp4")
  23. saveVideo(ising_model(100, 10, 100000, range_f), "beta_is_10.mp4")
  24.  
  25. ani.options(convert ="C:/Program Files/ImageMagick-6.9.1-Q16/convert.exe")
  26. saveGIF(ising_model(100, -1, 100000, range_f), "beta_is_-1.gif")
  27. saveGIF(ising_model(100, 2, 100000, range_f), "beta_is_2.gif")
  28. saveGIF(ising_model(100, 6, 100000, range_f), "beta_is_6.gif")
  29. saveGIF(ising_model(100, 10, 100000, range_f), "beta_is_10.gif")
Advertisement
Add Comment
Please, Sign In to add comment