-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvection.py
executable file
·58 lines (42 loc) · 1.32 KB
/
advection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
c = 1 # Advection speed.
nx = 128 # Grid size in space.
x = np.linspace(0, 1, nx, endpoint=False) # Grid in space.
dx = x[1] - x[0] # Step in space.
nu = 0.1 # CFL number.
tmax = 0.1 # Time range.
dt = dx * nu / c # Step in time.
t = np.arange(0, tmax + 0.1 * dt, dt) # Grid in time.
nt = len(t)
# Initial condition.
u_init = np.exp(-20 * (x - 0.5)**2)
#u_init = np.sign(x - 0.5)
def upwind():
u = np.zeros((nt, nx)) # Solution.
u[0] = u_init
for n in range(nt - 1):
for i in range(0, nx):
im = i - 1 if i > 0 else nx - 1
ip = i + 1 if i < nx - 1 else 0
u[n + 1, i] = (1 - nu) * u[n, i] + nu * u[n, im]
return u
def central():
u = np.zeros((nt, nx)) # Solution.
u[0] = u_init
for n in range(nt - 1):
for i in range(0, nx):
im = i - 1 if i > 0 else nx - 1
ip = i + 1 if i < nx - 1 else 0
u[n + 1, i] = u[n, i] + 0.5 * nu * (u[n, im] - u[n, ip])
return u
u = upwind()
#u = central()
plt.figure(figsize=(4, 4))
for n in np.linspace(0, nt - 1, 3).astype(int):
plt.plot(x, u[n], label="t={:.2f}".format(t[n]))
plt.xlabel('x')
plt.ylabel('u')
plt.legend(bbox_to_anchor=(1.4, 1.))
plt.savefig("advection.pdf", bbox_inches='tight')