Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using GR, Random, Memoize
  2.  
  3. xmin = ymin = -3
  4. xmax = ymax = 3
  5. resx = resy = 400
  6.  
  7. setviewport(0, 1, 0, 1)
  8. setwindow(xmin, xmax, ymin, ymax)
  9. setcolormap(3)
  10.  
  11. # Hodge podge parameters
  12. n = 100 # Number of states
  13. k1 = 3 # Contagion for infected cells
  14. k2 = 3 # Contagion factor for ill cells
  15. g = 25 # Infection evolution speed
  16.  
  17. # Initialise grids - 2 for alternation
  18. cells1 = Array{UInt32}(undef, resx, resy)
  19. rand!(cells1, 0:n)
  20. cells2 = copy(cells1)
  21. cells = [cells1, cells2]
  22.  
  23. # Evolution functions
  24. @inline isHealthy(t, i, j) = cells[t][i,j] == 0
  25. @inline isInfected(t, i, j) = cells[t][i,j] > 0 && cells[t][i,j] < n
  26. @inline isIll(t, i, j) = cells[t][i,j] >= n
  27.  
  28. @inline cx(x) = x <= 0 ? resx + x : x > resx ? x - resx : x
  29. @inline cy(y) = y <= 0 ? resy + y : y > resy ? y - resy : y
  30.  
  31. @memoize neighbours(i, j) = [[cx(i - 1), cy(j - 1)], [cx(i), cy(j - 1)], [cx(i + 1), cy(j - 1)],
  32. [cx(i - 1), cy(j)], [cx(i + 1), cy(j)],
  33. [cx(i - 1), cy(j + 1)], [cx(i), cy(j + 1)], [cx(i + 1), cy(j + 1)]]
  34.  
  35. @inline a(t, i, j) = count(c -> isInfected(t, c[1], c[2]), neighbours(i,j))
  36. @inline b(t, i, j) = count(c -> isIll(t, c[1], c[2]), neighbours(i,j))
  37. @inline s(t, i, j) = sum(c -> cells[t][c[1], c[2]], neighbours(i,j)) + cells[t][i, j]
  38.  
  39. t = 1
  40. while true
  41.  
  42. # Display grid with scaled colour map (this slows down sumulation considerably)
  43. clearws()
  44. cellarray(xmin, xmax, ymin, ymax, resx, resy, floor.(8 .+ 64*cells[t]/n) )
  45. updatews()
  46.  
  47. # Update next time step, alternating between in-memory grids
  48. tnext = (t == 1) ? 2 : 1
  49. for i in 1:resx
  50. for j in 1:resy
  51. # (a) If the cell is healthy (i.e., in state 0) then its new state is [a/k1] + [b/k2],
  52. # where a is the number of infected cells among its eight neighbors, b is the number of
  53. # ill cells among its neighbors, and k1 and k2 are constants. Here “[]” means the
  54. # integer part of the number enclosed, so that, for example, [7/3] = [2+1/3] = 2.
  55. if(isHealthy(t,i,j))
  56. cells[tnext][i,j] = floor(a(t,i,j) / k1) + floor(b(t,i,j) / k2)
  57.  
  58. # (b) If the cell is ill (i.e., in state n) then it miraculously becomes healthy (i.e.,
  59. # its state becomes 0).
  60. elseif(isIll(t,i,j))
  61. cells[tnext][i,j] = 0
  62.  
  63. # (c) If the cell is infected (i.e., in a state other than 0 and n) then its new state
  64. # is [s/(a+b+1)] + g, where a and b are as above, s is the sum of the states of the cell
  65. # and of its neighbors and g is a constant.
  66. else
  67. cells[tnext][i,j] = floor(s(t,i,j) / (a(t,i,j) + b(t,i,j) + 1)) + g
  68. end
  69. end
  70. end
  71.  
  72. # Alternate grids
  73. global t
  74. t = tnext
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement