import numpy as np
import matplotlib.pyplot as plt
Advection equation:
$$ \partial_t u(x, t) + a \partial_x u(x, t) = 0 $$Boundary conditions: 0
Initial conditions: $ u(x, 0) = \psi(x) $
Analytical solution: $$ u(x, t) = \psi(x - at) $$
L = 1
a = 1
xx = np.linspace(0, L, 1000)
tt = np.linspace(0, 10, 100)
def adv_eq_solution(x, t, a, ini=lambda x: 0, bnd=lambda t: 0):
return np.where(a*t <= x, ini(x-a*t), bnd(t-x/a))
def my_ini(x):
return np.where((x >= 0.1) & (x < 0.3), 1, 0)
uu = adv_eq_solution(xx, 0.5, a, ini=my_ini)
plt.plot(xx, uu)
plt.show()
Nx = 50
dt = 0.001
dx = L/Nx
Nt = 1000
Tmax = dt*Nt
xnum = np.linspace(0, L, Nx)
unum = np.zeros((Nx, Nt))
unum[:, 0] = my_ini(xnum)
C = a*dt/(2*dx)
print('C = ', C)
C = 0.025
for j in range(0, Nt - 1):
for i in range(1, Nx-1):
unum[i, j+1] = unum[i,j] - C*(unum[i+1,j] - unum[i-1,j])
tstep = 100
t1 = tstep*dt
uu = adv_eq_solution(xx, t1, a, ini=my_ini)
plt.plot(xx, uu, 'b-', xnum, unum[:, tstep], 'ro-')
plt.show()
Nx = 500
dt = 0.001
dx = L/Nx
Nt = 1000
Tmax = dt*Nt
xnum = np.linspace(0, L, Nx)
unum = np.zeros((Nx, Nt))
unum[:, 0] = my_ini(xnum)
C1 = a*dt/(dx)
print('C1 = ', C1)
for j in range(0, Nt - 1):
for i in range(1, Nx-1):
unum[i, j+1] = unum[i,j] - C1*(unum[i,j] - unum[i-1,j])
C1 = 0.5
tstep = 500
t1 = tstep*dt
uu = adv_eq_solution(xx, t1, a, ini=my_ini)
plt.plot(xx, uu, 'b-', xnum, unum[:, tstep], 'ro-')
plt.title('Time: {}'.format(t1))
plt.show()