Skip to content

Commit 9bec07b

Browse files
committed
examples: Add a 1D wave on a string superstep example
1 parent 7051473 commit 9bec07b

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
''' Script that demonstrates the functionality of the superstep in 1D
2+
"Wave on a string"
3+
'''
4+
import matplotlib.pyplot as plt
5+
import numpy as np
6+
from devito import Eq, Function, Grid, Operator, TimeFunction, solve
7+
from devito.timestepping.superstep import superstep_generator
8+
9+
# Parameters
10+
## Spatial
11+
shape = (501, )
12+
pad = (0, )
13+
origin= (0, )
14+
extent = (1, )
15+
# Time
16+
t0 = 0
17+
t1 = 0.15
18+
critical_dt = 0.0014142
19+
# Initial Condition
20+
mu = 0.5
21+
sigma_sq = 0.005
22+
ylim = np.ceil(1/np.sqrt(2*np.pi*sigma_sq))
23+
xlim = (0, 1)
24+
25+
def gaussian(x, mu=0, sigma_sq=1):
26+
''' Generate a Gaussian initial condition
27+
'''
28+
return np.exp(-((x - mu)**2)/(2*sigma_sq))/(np.sqrt(2*np.pi*sigma_sq))
29+
30+
def wave_on_string(step=1):
31+
grid = Grid(shape=shape, extent=extent)
32+
33+
velocity = Function(name='velocity', grid=grid, space_order=(0, step, step))
34+
velocity.data[:] = 1
35+
36+
u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2)
37+
38+
pde = (1/velocity**2)*u.dt2 - u.dx2
39+
stencil = Eq(u.forward, solve(pde, u.forward))
40+
41+
# Initial condition
42+
x = np.linspace(0, 1, *shape)
43+
ic = gaussian(x, mu=mu, sigma_sq=sigma_sq)
44+
45+
if step == 1:
46+
# Non-superstep case
47+
newu = u
48+
newu.data[0, :] = ic
49+
newu.data[1, :] = ic
50+
op = Operator(stencil)
51+
else:
52+
# Superstepping
53+
newu, newu_p, stencil1, stencil2 = superstep_generator(u, stencil.rhs, k=step)
54+
55+
newu.data[0, :] = ic
56+
newu.data[1, :] = ic
57+
newu_p.data[0, :] = ic
58+
newu_p.data[1, :] = ic
59+
60+
op = Operator([
61+
stencil1,
62+
stencil2,
63+
], opt='noop')
64+
65+
tn = int(np.ceil(t1/critical_dt))
66+
dt = t1/tn
67+
68+
op(time=tn, dt=dt)
69+
70+
idx = tn % 3
71+
return newu.data[idx]
72+
73+
if __name__ == '__main__':
74+
fig, ax = plt.subplots(1, 1)
75+
x = np.linspace(0, 1, *shape)
76+
ax.plot(
77+
x, gaussian(x, mu=mu, sigma_sq=sigma_sq),
78+
color='k', ls='--', label='Initial Condition'
79+
)
80+
81+
for ii in range(1, 6):
82+
label = 'Normal timestepping' if ii == 1 else f'Superstep size {ii}'
83+
ax.plot(x, wave_on_string(ii), label=label)
84+
85+
ax.set_xlim(*xlim)
86+
ax.set_ylim(-ylim, ylim)
87+
ax.legend()
88+
plt.show()

0 commit comments

Comments
 (0)