Copyright (C) 2010-2020 Luke Olson
Copyright (C) 2020 Andreas Kloeckner
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
Consider $$u_t+au_x=0$$ with periodic boundary conditions.
Set up parameters:
a
for the advection speedlmbda
for the CFL numberdx
for the grid spacing in $x$dt
for the time stepks
for the range of wave numbers to considera = 1
lmbda = 0.6/a
dx = .1
dt = dx*lmbda
ks = np.arange(1,16)
Find $\omega(\kappa)$. Recall $\lambda = ah_t / h_x$.
ETBS: $$ u_{k, \ell + 1} = \lambda u_{k - 1 , \ell} + (1 - \lambda) u_{k, \ell} $$
Recall:
kappa = ks*dx
p_ETBS = 1
q_ETBS = lmbda*np.exp(-1j*kappa) + (1-lmbda)
s_ETBS = q_ETBS/p_ETBS
omega_ETBS = 1j*np.log(s_ETBS)/dt
Again recall $\lambda = ah_t / h_x$.
Lax-Wendroff: $$ u_{k, \ell + 1} - u_{k, \ell} = -\frac{\lambda}2 (u_{k + 1, \ell} - u_{k - 1, \ell}) + \frac{\lambda^2}{2} ( u_{k + 1, \ell} - 2 u_{k, \ell} + u_{k - 1, \ell}) $$
p_LW = 1
q_LW = (
# u_{k,l}
1 - 2*lmbda**2/2
# u_{k+1,l}
+ np.exp(1j*kappa) * (-lmbda/2 + lmbda**2/2)
# u_{k-1,l}
+ np.exp(-1j*kappa) * (lmbda/2 + lmbda**2/2)
)
s_LW = q_LW/p_LW
omega_LW = 1j*np.log(s_LW)/dt
plt.plot(ks, omega_ETBS.real, label="ETBS")
plt.plot(ks, omega_LW.real, label="Lax-Wendroff")
plt.plot(ks, a*ks, color='black', label='exact')
plt.legend(loc="best")
<matplotlib.legend.Legend at 0x7f3c400c0850>
plt.plot( ks, omega_ETBS.imag, label="ETBS")
plt.plot( ks, omega_LW.imag, label="Lax-Wendroff")
plt.legend(loc="best")
<matplotlib.legend.Legend at 0x7f3c40009410>