Introduction to the problem
Initial condition:
0<x<1 | 1<x<2 | 2<x<3 | |
---|---|---|---|
0<y<1 | 0 | 0 | 0 |
1<y<2 | 0 | 1 | 0 |
2<y<3 | 0 | 0 | 0 |
Boundary Condition:
u(0,y,t) = u(3,y,t) = u(x,0,t) = u(x,3,t) = 0
Analytical solution xb
Design Algorithm to Solve Problem
i: grid in x direction
j: grid in y direction
n: grid in t
same as v
Code
import numpy as np
nt = 151
tmax = 0.5
dt = tmax/(nt-1)
nx = 31
xmax = 3
ny = 31
ymax = 3
dx = xmax/(nx-1)
dy = ymax/(ny-1)
nu = 0.1
u = np.zeros(((nx,ny,nt)))
v = np.zeros(((nx,ny,nt)))
x = np.zeros(nx)
y = np.zeros(ny)
#IC
u[10:20,10:20,0] = 1
v[10:20,10:20,0] = 1
#BC
u[0:30,0,0:50]=u[0:30,30,0:50]=0
v[0:30,0,0:50]=v[0:30,30,0:50]=0
u[0,0:30,0:50]=u[30,0:30,0:50]=0
v[0,0:30,0:50]=v[30,0:30,0:50]=0
for n in range(0,nt-1):
for i in range(1,nx-1):
for j in range(1,ny-1):
u[i,j,n+1] = ( u[i,j,n]+dt*nu*( ( u[i-1,j,n]-2*u[i,j,n]+u[i+1,j,n] ) /dx**2 + u[i,j-1,n]-2*u[i,j,n]+u[i,j+1,n] ) /dy**2 ) )
v[i,j,n+1] = ( v[i,j,n]+dt*nu*( ( v[i-1,j,n]-2*v[i,j,n]+v[i+1,j,n] ) /dx**2 + v[i,j-1,n]-2*v[i,j,n]+v[i,j+1,n] ) /dy**2 ) )
for i in range(0,nx):
x[i] = i*dx
for j in range(0,ny):
y[j] = j*dy
def plot_3D(u,x,y,time,title,label):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig=plt.figure(figsize=(11,7),dpi=100)
ax=fig.gca(projection='3d')
ax.set_xlabel('x (m)')
ax.set_ylabel('y (m)')
ax.set_zlabel(label)
X,Y=np.meshgrid(x,y)
surf=ax.plot_surface(X,Y,u[:,:,time],rstride=2,cstride=2)
plt.title(title)
plt.show()
(BURR why so ugly)
Analytical solution
See P.124 in Gerald B. Folland《 FOURIER ANALYSISAND ITS APPLICATIONS》