Copyright (C) 2010-2020 Luke Olson
Copyright (C) 2020 Andreas Kloeckner
One-way wave equation: $$ u_t + c u_x = 0$$ $$ u(0,x) = f(x) \quad \text{on} \, [0,1] $$
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
dx
: grid spacing in the $x$-direction x
: grid coordinates nx = 100
x = np.linspace(0, 1, nx, endpoint=False)
dx = x[1] - x[0]
c = 1.0 # speed
T = 1.0 / c # end time
lmbda = 0.93
dt = dx * lmbda / c
nt = int(T//dt)
print('T = %g' % T)
print('tsteps = %d' % nt)
print(' dx = %g' % dx)
print(' dt = %g' % dt)
print('lambda = %g' % lmbda)
T = 1 tsteps = 107 dx = 0.01 dt = 0.0093 lambda = 0.93
(Square wave with amplitude 1)
def f(x):
return ((x>0.4) & (x<0.6)).astype(np.float64)
import time
u = f(x)
fig = plt.figure(figsize=(8,8))
plt.title('u vs x')
line1, = plt.plot(x, u, lw=3, clip_on=False)
def timestepper(n):
uex = f((x - c * (n+1) * dt) % 1.0)
line1.set_data(x, uex)
return [line1]
from matplotlib import animation
from IPython.display import HTML
ani = animation.FuncAnimation(fig, timestepper, frames=nt, interval=20)
html = HTML(ani.to_jshtml())
plt.clf()
html
<Figure size 576x576 with 0 Axes>