SnaKy_EyeS

Untitled

Mar 24th, 2021 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. from copy import copy
  5. from matplotlib.animation import FuncAnimation
  6. from scipy.fft import rfft2, irfft2, rfftfreq, fftfreq
  7.  
  8.  
  9. def update(i):
  10.     """
  11.    Animation update function
  12.    """
  13.     for _ in range(skip_frame):
  14.         c = next(sol)
  15.  
  16.     img.set_data(irfft2(c))
  17.     title.set_text(f"Time = {i*skip_frame*dt:.6f}")
  18.     return img, title,
  19.  
  20.  
  21. def imex2(c, dt):
  22.     """
  23.    Time-stepping/integration scheme using the IMEX-BDF2 scheme
  24.    """
  25.     f = lambda c: rfft2(irfft2(c)**3) - c
  26.  
  27.     # First step
  28.     c_prev = copy(c)
  29.     f_prev = f(c)
  30.     c -= dt*k*f_prev
  31.     c /= (1 + dt*(a*k)**2)
  32.     yield c
  33.  
  34.     # Loop !
  35.     while True:
  36.         c_curr = copy(c)
  37.         f_curr = f(c_curr)
  38.         c = 4*c_curr - c_prev - 2*dt*k*(2*f_curr - f_prev)
  39.         c /= (3 + 2*dt*(a*k)**2)
  40.         c_prev = copy(c_curr)
  41.         f_prev = copy(f_curr)
  42.         yield c
  43.  
  44.  
  45. def imex4(c, dt):
  46.     """
  47.    Time-stepping/integration scheme using the IMEX-BDF4 scheme
  48.    (not working for the moment)
  49.    """
  50.     f = lambda c: rfft2(irfft2(c)**3) - c
  51.  
  52.     # First step
  53.     c_3 = copy(c)
  54.     f_3 = f(c)
  55.     c = c_3 - dt*k*f_3
  56.     c /= (1 + dt*(a*k)**2)
  57.     yield c
  58.  
  59.     # Second step
  60.     c_2 = copy(c)
  61.     f_2 = f(c)
  62.     c = 4*c_2 - c_3 - 2*dt*k*(2*f_2 - f_3)
  63.     c /= (3 + 2*dt*(a*k)**2)
  64.     yield c
  65.  
  66.     # Third step
  67.     c_1 = copy(c)
  68.     f_1 = f(c)
  69.     c = 18*c_1 - 9*c_2 + 2*c_3 - 6*dt*k*(3*f_1 - 3*f_2 + f_3)
  70.     c /= (11 + 6*dt*(a*k)**2)
  71.     yield c
  72.  
  73.     # Loop !
  74.     while True:
  75.         c_0 = copy(c)
  76.         f_0 = f(c)
  77.         c = 48*c_0 - 36*c_1 + 16*c_2 - 3*c_3 - 12*dt*k*(4*f_0 - 6*f_1 + 4*f_2 - f_3)
  78.         c /= (25 + 12*dt*(a*k)**2)
  79.         c_3 = copy(c_2)
  80.         c_2 = copy(c_1)
  81.         c_1 = copy(c_0)
  82.         f_3 = copy(f_2)
  83.         f_2 = copy(f_1)
  84.         f_1 = copy(f_0)
  85.         yield c
  86.  
  87.  
  88. def rk4(c, dt):
  89.     """
  90.    Time stepping/integration scheme using the classical Runge-Kutta 4 scheme
  91.    """
  92.     f = lambda c: -k*(rfft2(irfft2(c)**3) - c + a**2*k*c)
  93.  
  94.     # Loop !
  95.     while True:
  96.         k1 = f(c)
  97.         k2 = f(c + dt*k1/2)
  98.         k3 = f(c + dt*k2/2)
  99.         k4 = f(c + dt*k3)
  100.  
  101.         c += dt*(k1 + 2*k2 + 2*k3 + k4)/6
  102.         yield c
  103.  
  104.  
  105. # Problem parameters
  106. a = 1e-2
  107. n = 256
  108. dt = 1e-6
  109. n_step = 12000*4
  110. skip_frame = 10
  111.  
  112. # Initialise vectors
  113. x, h = np.linspace(0, 1, n, endpoint=False, retstep=True)
  114. c = rfft2(2*np.random.rand(n, n) - 1)   # We work in frequency domain
  115. sol = imex4(c, dt)
  116.  
  117. # Initialize wavelength for second derivative to avoid a repetitive operation
  118. # Since we use rfftn, one dim is n/2+1 (rfftfreq) and the other is n (fftfreq)
  119. k_x, k_y = np.meshgrid(rfftfreq(n, h/(2*np.pi)), fftfreq(n, h/(2*np.pi)))
  120. k = k_x**2 + k_y**2
  121.  
  122. # Initialize animation
  123. if __name__ == "__main__":
  124.     fig, ax = plt.subplots()
  125.     img = ax.imshow(irfft2(c), cmap="jet", vmin=-1, vmax=1)
  126.     fig.colorbar(img, ax=ax)
  127.     ax.axis("off")
  128.     title = ax.text(.5, .1, "", bbox={'facecolor': 'w', 'alpha': 0.7, 'pad': 5}, transform=ax.transAxes, ha="center")
  129.  
  130.     # Start animation
  131.     anim = FuncAnimation(fig, update, frames=int(n_step/skip_frame), interval=1, blit=True)
  132.     plt.show()
  133.  
Add Comment
Please, Sign In to add comment