Advertisement
Guest User

Updated Ising model for critical temperature

a guest
Dec 18th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. -- Gotta modify the mod function because taking the mod of a negative number just returns that same number.
  2. local function mod(x, y)
  3.     return (x>=0) and math.fmod(x,y) or math.fmod(x,y)+y
  4. end
  5.  
  6. local function initialize(width, height)
  7.     local grid={}
  8.     for i=0,width-1 do
  9.         grid[i]={}
  10.         for j=0,height-1 do
  11.             --grid[i][j]=math.floor(2*math.random()-1)      -- Selecting random numbers in this way doesn't work and I have no idea why.
  12.             grid[i][j]=2*math.random(2)-3
  13.             --grid[i][j]=-1
  14.         end
  15.     end
  16.     return grid
  17. end
  18.  
  19. -- Copy a table.
  20. local function tablecopy(t)
  21.     local tcopy={}
  22.     for k, v in pairs(t) do
  23.         if type(v)=="table" then
  24.             tcopy[k]=tablecopy(v)
  25.         else
  26.             tcopy[k]=v
  27.         end
  28.     end
  29.     return tcopy
  30. end
  31.  
  32. local function pickrandom(dimension)        -- dimension can be either width or height
  33.     return math.random(dimension)-1         -- Subtract 1 because indexing starts at 0.
  34. end
  35.  
  36. local function magnetization(grid)
  37.     local mag=0
  38.     local width, height = #grid+1, #grid[1]+1
  39.     for i=0,#grid do
  40.         for j=0,#grid[i] do
  41.             mag=mag+grid[i][j]
  42.         end
  43.     end
  44.    
  45.     --mag=mag/(width*height)
  46.     return mag
  47. end
  48.  
  49. local function displaygrid(grid)
  50.     local colorkey={"white", [-1]="black"}
  51.     for i=0,#grid do
  52.         for j=0,#grid[i] do
  53.             gui.drawPixel(i, j, colorkey[grid[i][j]])
  54.         end
  55.     end
  56. end
  57.  
  58. local function neighbors(grid, i, j, width, height)
  59.     local l=mod(i-1, width)
  60.     local r=mod(i+1, width)
  61.     local u=mod(j-1, height)
  62.     local d=mod(j+1, height)
  63.    
  64.     local nearest = {grid[l][j], grid[r][j], grid[i][u], grid[i][d]}
  65.    
  66.     return nearest
  67. end
  68.  
  69. local function deltaU(grid, i, j, width, height)
  70.     nearest=neighbors(grid, i, j, width, height)
  71.     local sum=0
  72.     for ii=1,#nearest do        -- Already using i as an argument, although Lua's scoping means we could use i if we wanted.
  73.         sum=sum+nearest[ii]
  74.     end
  75.    
  76.     return 2*grid[i][j]*sum
  77. end
  78.  
  79. local function atrandom(grid, width, height, temp, iterations)
  80.     for k=1,iterations do
  81.         i, j = pickrandom(width), pickrandom(height)
  82.         local Ediff = deltaU(grid, i, j, width, height)
  83.         if Ediff<=0 or math.random()<math.exp(-Ediff/temp) then
  84.             grid[i][j] = -grid[i][j]
  85.         end
  86.     end
  87.     return grid
  88. end
  89.  
  90. local width=240     -- Width and height are 240 and 160 (for GBA), but indexing starts at 0 so we subtract 1 when initializing the grid.  We still use these values for modular arithmetic.
  91. local height=160
  92. -- Critical temperature is ~2.2692
  93. local kT=3
  94. local rate=0.2*width*height     -- Number of iterations to perform before drawing all the pixels, which is computationally expensive.
  95. local startframe=emu.framecount()
  96. local grid = initialize(width, height)
  97.  
  98. while true do
  99.     local mag = magnetization(grid)
  100.    
  101.     local t = emu.framecount()-startframe
  102.     local A = 0.8
  103.     local omega = math.pi/500
  104.     local lambda = math.log(1/0.8)/500      -- omega*t=pi ==> exp(-lambda*t) = 0.8 ==> -lambda*pi/omega = ln(0.8) ==> lambda = omega/pi * ln(1/0.8)
  105.     local temp = 2.2692 - A * math.exp(-lambda*t) * math.cos(omega*t)
  106.    
  107.     displaygrid(grid)
  108.     local grid = atrandom(grid, width, height, temp, rate)
  109.     --gui.pixelText(80, 20, kT,nil,"black")
  110.     --gui.pixelText(80, 30, emu.framecount()-startframe,nil,"black")
  111.    
  112.     emu.frameadvance()
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement