Advertisement
Guest User

Untitled

a guest
Jun 10th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.animation import FuncAnimation
  4. from functools import partial
  5.  
  6. plt.style.use("ggplot")
  7.  
  8. #initializes static elements
  9. def init_fig(fig, axes, artist):
  10.     axes.axes.get_xaxis().set_visible(False)
  11.     axes.axes.get_yaxis().set_visible(False)
  12.     axes.get_xaxis().tick_bottom()
  13.     artist.set_clim([0.0, 1.0])
  14.     artist.set_cmap("binary")
  15.     artist.set_interpolation("quadric")
  16.     fig.colorbar(heat_map)
  17.     return artist,
  18.  
  19. #returns data needed to update artist
  20. def animate(t, N, heat_map, A, B, D_A, D_B, conv, f, k):
  21.     print(t)
  22.    
  23.     #derivative matrix
  24.     dA = np.zeros([N, N])
  25.     dB = np.zeros([N, N])
  26.        
  27.     #iterate over each non-border cell
  28.     for i in range(1, N-1):
  29.         for j in range(1, N-1):
  30.             dA[i, j] = D_A*laplace(A, (i, j), conv) - A[i, j]*(B[i, j])**2 + f*(1-A[i, j])
  31.             dB[i, j] = D_B*laplace(B, (i, j), conv) + A[i, j]*(B[i, j])**2 - (k+f)*B[i, j]
  32.  
  33.     #evolving
  34.     A += dA
  35.     B += dB
  36.  
  37.     heat_map.set_data(A)
  38.     return heat_map,
  39.  
  40. #Laplace operator at a point
  41. def laplace(matrix, cell, conv):
  42.     values = matrix[cell[0]-1 : cell[0]+2, cell[1]-1 : cell[1]+2]
  43.     return np.sum(conv*values)
  44.  
  45. #initial data
  46. conv = np.array([[0.05, 0.2, 0.05], [0.2, -1.0, 0.2], [0.05, 0.2, 0.05]])
  47. D_A, D_B, f, k = 1.0, 0.5, 0.0545, 0.062
  48. t = 100
  49.  
  50. N = 100
  51. A, B = np.ones([N, N]), np.zeros([N, N])
  52. for i in range(-int(N/4), int(N/4)):
  53.     for j in range(-int(N/4), int(N/4)):
  54.         B[int(N/2)-10+i, int(N/2)-10+j] = 1.0
  55.  
  56. #figure setup
  57. fig, axes = plt.figure(), plt.axes(frameon = True)
  58. heat_map = axes.imshow(A, animated = True)
  59.  
  60. init = partial(init_fig, fig=fig, axes=axes, artist=heat_map)
  61.  
  62. #animate
  63. anim = FuncAnimation(fig, func=animate, frames=t, init_func=init, blit=True, fargs=(N, heat_map, A, B, D_A, D_B, conv, f, k))
  64. anim.save("out.mp4", fps=40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement