Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d import Axes3D
  5.  
  6.  
  7. func = lambda x, y, t: math.exp(t) * x * y * (1 - x)
  8.  
  9. def progonka(A, f):
  10.     size = A.shape[1]
  11.  
  12.     a = np.zeros(size)
  13.     b = np.zeros(size)
  14.     c = np.zeros(size)
  15.     c[size - 1] = A[size - 1, size - 1]
  16.  
  17.     for i in range(size - 1):
  18.         a[i + 1], c[i], b[i] = A[i + 1, i], A[i, i], A[i, i + 1]
  19.  
  20.     al = np.zeros(size)
  21.     bet = np.zeros(size)
  22.  
  23.     al[1] = - b[0] / c[0]
  24.     bet[1] = f[0] / c[0]
  25.     for i in range(1, size - 1):
  26.         al[i + 1] = -b[i] / (a[i] * al[i] + c[i])
  27.         bet[i + 1] = (f[i] - a[i] * bet[i]) / (a[i] * al[i] + c[i])
  28.  
  29.     x = np.zeros(size)
  30.     x[-1] = (f[-1] - a[-1] * bet[-1]) / (c[-1] + a[-1] * al[-1])
  31.  
  32.     for i in range(size - 2, -1, -1):
  33.         x[i] = al[i + 1] * x[i + 1] + bet[i + 1]
  34.  
  35.     return x
  36.  
  37. # Grid
  38. n_x = 20
  39. n_y = 50
  40. t_max = 0.1
  41. n_t = 20
  42. X = np.linspace(0, 1, n_x, endpoint = False)
  43. Y = np.linspace(0, 1, n_y, endpoint = False)
  44. T = np.linspace(0, t_max, n_t)
  45. h_x = 1 / n_x
  46. h_y = 1 / n_y
  47. h_t = t_max / n_t
  48. cnst_x = 9 * h_t / h_x ** 2
  49. cnst_y = 9 * h_t / h_y ** 2
  50.  
  51. u = np.zeros((n_t, n_x, n_y))
  52. # u[0, : , :] = [[1 for _ in range(n_y)] for _ in range(n_x)]
  53.  
  54. A_x = np.zeros((n_x - 2, n_x - 2))
  55. A_x += np.diag([1 + cnst_x] * (n_x -2))
  56. A_x += np.diag([-cnst_x / 2] * (n_x - 3), -1)
  57. A_x += np.diag([-cnst_x / 2] * (n_x - 3), 1)
  58.  
  59. A_y = np.zeros((n_y - 2, n_y - 2))
  60. A_y += np.diag([1 + cnst_y] * (n_y - 2))
  61. A_y += np.diag([-cnst_y / 2] * (n_y - 3), -1)
  62. A_y += np.diag([-cnst_y / 2] * (n_y - 3), 1)
  63.  
  64. for i_t in range(n_t - 1):
  65.     temp_slice = u[0, : -2, : -2]
  66.     for i_y in range(n_y - 2):
  67.         f = np.zeros(n_x - 2)
  68.         for i_x in range(n_x - 2):
  69.             f[i_x] = (h_t / 2 * func((i_x + 1) * h_x, (i_y + 1) * h_y, (i_t + 0.5) * h_t) + cnst_y / 2 * u[i_t, i_x + 1, i_y]
  70.                         + (1 - cnst_y) * u[i_t, i_x + 1, i_y + 1] + cnst_y / 2 * u[i_t, i_x + 1, i_y + 2] )
  71.         temp_slice[:, i_y] = progonka(A_x, f)
  72.  
  73.  
  74.     for i_x in range(n_x - 2):
  75.         f = np.zeros(n_y - 2)
  76.         for i_y in range(n_y - 2):
  77.             if i_x == 0:
  78.                 f[i_y] = (h_t / 2 * func((i_x + 1) * h_x, (i_y + 1)* h_y, (i_t + 0.5) * h_t) +
  79.                         + (1 - cnst_x) * temp_slice[i_x, i_y] + cnst_x / 2 * temp_slice[i_x + 1 , i_y])
  80.             elif i_x == n_x - 3:
  81.                 f[i_y] = (h_t / 2 * func((i_x + 1) * h_x, (i_y + 1)* h_y, (i_t + 0.5) * h_t) + cnst_x / 2 * temp_slice[i_x - 1, i_y]
  82.                         + (1 - cnst_x) * temp_slice[i_x, i_y])
  83.             else:
  84.                 f[i_y] = (h_t / 2 * func((i_x + 1) * h_x, (i_y + 1)* h_y, (i_t + 0.5) * h_t) + cnst_x / 2 * temp_slice[i_x - 1, i_y]
  85.                         + (1 - cnst_x) * temp_slice[i_x, i_y] + cnst_x / 2 * temp_slice[i_x + 1, i_y])
  86.        
  87.         u[i_t + 1, 1 + i_x, 0] = 0
  88.         u[i_t + 1, 1 + i_x, 1 :-1] = progonka(A_y, f)
  89.         u[i_t + 1, 1 + i_x, -1] = u[i_t + 1, 1 + i_x, -2]
  90.         print(u[i_t, 0, :])
  91.  
  92.  
  93. y, x = np.meshgrid(Y, X)
  94. fig = plt.figure()
  95. ax = fig.add_subplot(111, projection = '3d')
  96. ax.set_xlabel('x')
  97. ax.set_ylabel('y')
  98. ax.plot_surface(x, y, u[19][:])
  99. plt.show()
  100.  
  101. # print(A, f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement