Guest User

Untitled

a guest
Apr 19th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. import math
  2. import numpy as np
  3.  
  4. def solution(M, N, gamma):
  5.     # 时间区间[0,1], 空间区间[0,1]
  6.     dx, dt = 1/M, 1/N
  7.     # M, N = 20, 1000
  8.     # gamma = 0.25
  9.     # gamma = 0.75
  10.  
  11.     Us = np.zeros((M, N))
  12.     us = np.zeros((M, N))
  13.  
  14.     mue = dt**gamma/(2*dx**2)
  15.  
  16.     def exact_u(i, j):
  17.         x, t = fx(i), ft(j)
  18.         return math.exp(x) * t**(1+gamma)
  19.  
  20.     def ft(n):
  21.         return n*dt
  22.     def fx(n):
  23.         return n*dx
  24.  
  25.     def f(i, j):
  26.         x, t = fx(i), ft(j)
  27.         tmp = math.gamma(2+gamma)/math.gamma(1+2*gamma)
  28.         return math.exp(x)*((1+gamma)*t**gamma - tmp*t**(2*gamma))
  29.  
  30.     # 赋值精确解
  31.     for i in range(0, M):
  32.         for j in range(0, N):
  33.             us[i,j] = exact_u(i, j)
  34.  
  35.     # 为边缘赋初值
  36.     for n in range(0, N):
  37.         Us[0, n] = exact_u(0, n)
  38.         Us[1, n] = exact_u(1, n)
  39.         # Us[M-1, n] = exact_u(M-1, n)
  40.  
  41.     lm = [1]
  42.     for n in range(1, N):
  43.         v = lm[-1] * (1-(2-gamma)/n)
  44.         lm.append(v)
  45.    
  46.     lm = np.array(lm)
  47.  
  48.     # 为中心区域赋值
  49.     for i in range(2, M):
  50.         for j in range(1, N):
  51.             Us[i, j] = ((1/12+mue*gamma)*Us[i-2, j-1] +
  52.                         (5/6-2*mue*gamma)*Us[i-1, j-1] +
  53.                         (1/12+mue*gamma)*Us[i, j-1] -
  54.                         (1/12-mue)*Us[i-2, j] -
  55.                         (5/6+2*mue)*Us[i-1, j])
  56.             tmp = 0
  57.             for l in range(0, j-1):
  58.                 tmp += mue*(lm[j-l]+lm[j-l-1])*(Us[i-2, l]-2*Us[i-1, l]+Us[i, l])
  59.             Us[i,j] += tmp + dt*((1/24)*(f(i-2, j-1)+f(i-2, j))+
  60.                                  (5/12)*(f(i-1, j-1)+f(i-1, j))+
  61.                                  (1/24)*(f(i, j-1)+f(i, j)))
  62.             Us[i,j] /= (1/12-mue)
  63.    
  64.     e = Us - us
  65.     # e_inf = np.max(e[:, 1])
  66.     # e_2 = np.linalg.norm(e, ord=2)
  67.     # eu_2 = e_2/np.linalg.norm(us, ord=2)
  68.     # print(e_inf, e_2, eu_2)
  69.     # print(e[:, 1])
  70.     np.save(f'e_{M}_{N}_{gamma}.npy', e)
  71.  
  72. solution(4, 4, 0.25)
  73. solution(8, 64, 0.25)
  74. solution(16, 1024, 0.25)
  75.  
  76. solution(8, 8, 0.25)
  77. solution(16, 128, 0.25)
  78. solution(32, 2048, 0.25)
  79.  
  80. solution(4, 4, 0.75)
  81. solution(8, 64, 0.75)
  82. solution(16, 1024, 0.75)
  83.  
  84. solution(8, 8, 0.75)
  85. solution(16, 128, 0.75)
  86. solution(32, 2048, 0.75)
Advertisement
Add Comment
Please, Sign In to add comment