Skip to content

Commit 29e4fd3

Browse files
committed
examples: Tidy up 1D/2D example
1 parent d03275a commit 29e4fd3

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

examples/timestepping/ic_superstep.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
'''
66
from argparse import ArgumentParser
77
from dataclasses import dataclass
8+
from functools import reduce
9+
from operator import mul
810

911
import matplotlib.pyplot as plt
1012
import numpy as np
@@ -40,7 +42,14 @@ def gaussian_2d(xx, yy, mu=0, sigma_sq=1):
4042
"""
4143
Generate a 2D Gaussian initial condition
4244
"""
43-
return np.exp(-((xx - mu)**2 + (yy - mu)**2)/(2*sigma_sq))/(np.sqrt(2*np.pi*sigma_sq))
45+
return np.exp(-((xx - mu)**2 + (yy - mu)**2)/(2*sigma_sq))/(2*np.pi*sigma_sq)
46+
47+
48+
def gaussian(dims, mu=0, sigma_sq=1):
49+
"""
50+
Generate an N-dimensional Gaussian initial condition
51+
"""
52+
return reduce(mul, [gaussian_1d(d, mu=mu, sigma_sq=sigma_sq) for d in dims])
4453

4554

4655
def simulate_ic(parameters, step=1, snapshots=-1):
@@ -59,13 +68,12 @@ def simulate_ic(parameters, step=1, snapshots=-1):
5968
stencil = Eq(u.forward, solve(pde, u.forward))
6069

6170
# Initial condition
62-
x = np.linspace(p.origin[0], p.extent[0], p.shape[0])
63-
if d == 1:
64-
ic = gaussian_1d(x, mu=p.mu, sigma_sq=p.sigma_sq)
65-
elif d == 2:
66-
y = np.linspace(p.origin[1], p.extent[1], p.shape[1])
67-
xx, yy = np.meshgrid(x, y)
68-
ic = gaussian_2d(xx, yy, mu=p.mu, sigma_sq=p.sigma_sq)
71+
msh = np.meshgrid(*[
72+
np.linspace(o, e, s) for o, e, s
73+
in zip(p.origin, p.extent, p.shape)
74+
])
75+
ic = gaussian(msh, mu=p.mu, sigma_sq=p.sigma_sq)
76+
ic = gaussian_2d(*msh, mu=p.mu, sigma_sq=p.sigma_sq)
6977

7078
# Stencil and operator
7179
if step == 1:
@@ -88,25 +96,22 @@ def simulate_ic(parameters, step=1, snapshots=-1):
8896
# Snapshot the solution
8997
if snapshots > 0:
9098
factor = int(np.ceil(nt/(snapshots + 1)))
91-
t_sub = ConditionalDimension('t_sub', parent=grid.time_dim, factor=factor)
92-
u_save = TimeFunction(
93-
name='usave', grid=grid,
94-
time_order=0, space_order=2,
95-
save=snapshots//step + 1, time_dim=t_sub
96-
)
99+
time_dim = ConditionalDimension('t_sub', parent=grid.time_dim, factor=factor)
100+
save = snapshots//step + 1
97101
else:
98-
u_save = TimeFunction(
99-
name='usave', grid=grid,
100-
time_order=0, space_order=2,
101-
save=1, time_dim=new_u.time_dim
102-
)
102+
time_dim = new_u.time_dim
103+
save = 1
104+
105+
u_save = TimeFunction(
106+
name='usave', grid=grid,
107+
time_order=0, space_order=2,
108+
save=save, time_dim=time_dim
109+
)
103110
save = Eq(u_save, new_u)
104111

105112
op = Operator([*stencil, save], opt='noop')
106-
if d == 1:
107-
op(time=nt - 2, dt=dt)
108-
elif d == 2:
109-
op(dt=dt)
113+
kwargs = {'time': nt - 2} if d == 1 else {}
114+
op(dt=dt, **kwargs)
110115

111116
if d == 2 and step == 1:
112117
u_save.data[0, :] = ic
@@ -200,7 +205,7 @@ def plot_2d(k, data, parameters, snapshots=1):
200205
# Initial Condition
201206
mu=500,
202207
sigma_sq=5000,
203-
lim=1/(2*np.sqrt(2*np.pi*5000))
208+
lim=1/(4*np.pi*5000)
204209
)
205210

206211
# Supersteps

0 commit comments

Comments
 (0)