Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. a = 3
  5. b = 1
  6. T = 10
  7. u_1 = 0
  8. dudn_1 = 0
  9. u_2 = 0
  10. dudn_2 = 0
  11. N_x = 50
  12. hx = a / N_x
  13. N_y = 50
  14. hy = b / N_y
  15. tau = hx * hy / np.sqrt(hx**2 + hy**2)
  16. N_t = int(T / tau)
  17.  
  18.  
  19. def u(x, y, t=0):
  20.     return 2*np.cos(np.pi * x / a)
  21.  
  22.  
  23. def dudt(x, y, t=0):
  24.     return np.tan(np.sin(2 * np.pi * x / a)) * np.sin(np.pi * y / b)
  25.  
  26.  
  27. def solver_membrane():
  28.     X = np.linspace(-a/2, a/2, N_x)
  29.     Y = np.linspace(-b/2, b/2, N_y)
  30.     U = np.zeros((N_t, N_y, N_x))
  31.     p = np.zeros((X.shape[0], Y.shape[0]))
  32.     q = np.zeros((X.shape[0], Y.shape[0]))
  33.  
  34.     for i in range(X.shape[0]):
  35.         for j in range(Y.shape[0]):
  36.             p[i][j] = u(X[j], Y[i])
  37.             q[i][j] = dudt(X[j], Y[i])
  38.  
  39.     U[0] = p
  40.  
  41.     for i in range(1, N_y - 1):
  42.         for j in range(1, N_x - 1):
  43.             U[1][i][j] = p[i][j] + tau * q[i][j] + tau ** 2 / 2 * ((p[i - 1][j] - 2 * p[i][j] +
  44.                          p[i + 1][j]) / hx ** 2 + (p[i][j - 1] - 2 * p[i][j] + p[i][j + 1]) / hy ** 2)
  45.  
  46.     for t in range(1, N_t-1):
  47.         for i in range(1, N_x - 1):
  48.             for j in range(1, N_y-1):
  49.                 U[t + 1][i][j] = -U[t][i][j] * (2 * hy ** 2 * tau ** 2 + 2 * hx ** 2 * tau ** 2 - 2 * hy ** 2 * hx ** 2)
  50.                 + U[t][i + 1][j] * hy ** 2 * tau ** 2 + U[t][i - 1][j + 1] * hy ** 2 * tau ** 2
  51.                 + U[t][i][j + 1] * hx ** 2 * tau ** 2 + U[t][i][j - 1] * hx ** 2 * tau ** 2
  52.                 - U[t - 1][i][j] * hx ** 2 * hy ** 2
  53.                 U[t + 1][i][j] /= hx ** 2 * hy ** 2
  54.     return U, X, Y
  55.  
  56.  
  57. def slice_x(u, step, pos, x=None):
  58.     for i, s in enumerate(u[::step, pos, :]):
  59.         plt.plot(x, s)
  60.     plt.show()
  61.  
  62.  
  63. def slice_y(u, step, pos, y):
  64.     for i, s in enumerate(u[::step, :, pos]):
  65.         plt.plot(y, s)
  66.     plt.show()
  67.  
  68.  
  69. U, X, Y = solver_membrane()
  70.  
  71. from mpl_toolkits.mplot3d import Axes3D
  72. fig = plt.figure()
  73. ax = fig.gca(projection='3d')
  74. X_plot = np.zeros(len(X) * len(Y))
  75. Y_plot = np.zeros(len(X) * len(Y))
  76. U_plot = np.zeros(len(X) * len(Y))
  77. for i in range(X.shape[0]):
  78.     for j in range(Y.shape[0]):
  79.         idx = i * X.shape[0] + j
  80.         X_plot[idx] = X[i]
  81.         Y_plot[idx] = Y[j]
  82.         U_plot[idx] = U[10][i][j]
  83. ax.plot(X_plot, Y_plot, U_plot, label='parametric curve')
  84. ax.legend()
  85.  
  86. plt.show()
  87.  
  88. slice_y(U, 20, 2, Y)
  89. slice_x(U, 20, 2, X)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement