Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dissipation and dispersion
import numpy as np
import matplotlib.pyplot as plt
ngrid = 100
nstep = 40
dt = 0.1
c = 0.1
x = np.linspace(-1, 1, ngrid)
dx = x[1] - x[0]
u0 = np.ones(ngrid)
u0[int(ngrid / 3) : int(ngrid / 3 * 2)] = 2.0
u = u0.copy()
un = u.copy()
for n in range(nstep):
un2 = u.copy()
for i in range(1, ngrid - 1):
un2[i] = u[i] - c * dt / dx * (un[i + 1] - un[i - 1])
u = un
un = un2
f, axarr = plt.subplots(2, sharex=True)
axarr[0].grid()
axarr[0].set_title("Initial condition")
axarr[0].set_ylim([0, 2.5])
axarr[0].plot(x, u0)
axarr[1].grid()
axarr[1].set_title("Central in time and space")
axarr[1].set_ylim([0, 2.5])
axarr[1].plot(x, un)
f.savefig("convect-central.svg", transparent=True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
ngrid = 100
nstep = 40
dt = 0.1
c = 0.1
x = np.linspace(-1, 1, ngrid)
dx = x[1] - x[0]
u0 = np.ones(ngrid)
u0[int(ngrid / 3) : int(ngrid / 3 * 2)] = 2.0
u = u0.copy()
for n in range(nstep):
un = u.copy()
for i in range(1, ngrid):
u[i] = un[i] - c * dt / dx * (un[i] - un[i - 1])
f, axarr = plt.subplots(2, sharex=True)
axarr[0].grid()
axarr[0].set_title("Initial condition")
axarr[0].set_ylim([0, 2.5])
axarr[0].plot(x, u0)
axarr[1].grid()
axarr[1].set_title("Forward in time, backward in space")
axarr[1].set_ylim([0, 2.5])
axarr[1].plot(x, u)
f.savefig("convect.svg", transparent=True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.