Advertisement
SnaKy_EyeS

Untitled

Mar 24th, 2021
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. import sys
  2. import numpy as np
  3.  
  4. from copy import copy
  5. from scipy.linalg import norm
  6. from spectral import irfft2, rfft2, n
  7. from spectral import imex4 as integrate
  8.  
  9.  
  10. # Load initial & reference solutions
  11. # Reference solution is taken at t = 1e-3 w/ RK4 using dt = 1e-9
  12. c_init = rfft2(np.loadtxt("c_128_init.txt").reshape(n, n))
  13. c_ref = np.loadtxt("c_128_ref.txt").reshape(n, n)
  14.  
  15. # Initialise stuff
  16. dts = [1e-6/16, 1e-6/32]
  17. error = list()
  18.  
  19. for dt in dts:
  20.     # Time scheme - imex & rk4 as ref
  21.     step = integrate(copy(c_init), dt)
  22.     iter = 0
  23.  
  24.     # Iterate to t = 0.001
  25.     print(f"\rStarting iterations for dt = {dt*1e3:.4f} [ms]...")
  26.     while not dt*iter == .001:
  27.         c = next(step)
  28.  
  29.         sys.stdout.write(f"\rt = {dt*iter:.6f} [s]")
  30.         iter += 1
  31.     error.append(norm(irfft2(c) - c_ref))
  32.  
  33. print(f"\rError 1 = {error[0]}")
  34. print(f"\rError 2 = {error[1]}")
  35. print(f"n_est = {np.log(error[0] / error[1]) / np.log(dts[0]/dts[1])}")
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement