-
Notifications
You must be signed in to change notification settings - Fork 45
/
stencil2d.py
182 lines (148 loc) · 5.49 KB
/
stencil2d.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# ******************************************************
# Program: stencil2d-cupy
# Author: Stefano Ubbiali, Oliver Fuhrer
# Email: subbiali@phys.ethz.ch, ofuhrer@ethz.ch
# Date: 04.06.2020
# Description: CuPy implementation of 4th-order diffusion
# ******************************************************
import click
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import time
def laplacian(in_field, lap_field, num_halo, extend=0):
""" Compute the Laplacian using 2nd-order centered differences.
Parameters
----------
in_field : array-like
Input field (nz x ny x nx with halo in x- and y-direction).
lap_field : array-like
Result (must be same size as ``in_field``).
num_halo : int
Number of halo points.
extend : `int`, optional
Extend computation into halo-zone by this number of points.
"""
ib = num_halo - extend
ie = -num_halo + extend
jb = num_halo - extend
je = -num_halo + extend
lap_field[:, jb:je, ib:ie] = (
-4.0 * in_field[:, jb:je, ib:ie]
+ in_field[:, jb:je, ib - 1 : ie - 1]
+ in_field[:, jb:je, ib + 1 : ie + 1 if ie != -1 else None]
+ in_field[:, jb - 1 : je - 1, ib:ie]
+ in_field[:, jb + 1 : je + 1 if je != -1 else None, ib:ie]
)
def update_halo(field, num_halo):
""" Update the halo-zone using an up/down and left/right strategy.
Parameters
----------
field : array-like
Input/output field (nz x ny x nx with halo in x- and y-direction).
num_halo : int
Number of halo points.
Note
----
Corners are updated in the left/right phase of the halo-update.
"""
# bottom edge (without corners)
field[:, :num_halo, num_halo:-num_halo] = field[
:, -2 * num_halo : -num_halo, num_halo:-num_halo
]
# top edge (without corners)
field[:, -num_halo:, num_halo:-num_halo] = field[
:, num_halo : 2 * num_halo, num_halo:-num_halo
]
# left edge (including corners)
field[:, :, :num_halo] = field[:, :, -2 * num_halo : -num_halo]
# right edge (including corners)
field[:, :, -num_halo:] = field[:, :, num_halo : 2 * num_halo]
def apply_diffusion(in_field, out_field, alpha, num_halo, num_iter=1):
""" Integrate 4th-order diffusion equation by a certain number of iterations.
Parameters
----------
in_field : array-like
Input field (nz x ny x nx with halo in x- and y-direction).
lap_field : array-like
Result (must be same size as ``in_field``).
alpha : float
Diffusion coefficient (dimensionless).
num_iter : `int`, optional
Number of iterations to execute.
"""
tmp_field = np.empty_like(in_field)
for n in range(num_iter):
update_halo(in_field, num_halo)
laplacian(in_field, tmp_field, num_halo=num_halo, extend=1)
laplacian(tmp_field, out_field, num_halo=num_halo, extend=0)
out_field[:, num_halo:-num_halo, num_halo:-num_halo] = (
in_field[:, num_halo:-num_halo, num_halo:-num_halo]
- alpha * out_field[:, num_halo:-num_halo, num_halo:-num_halo]
)
if n < num_iter - 1:
in_field, out_field = out_field, in_field
else:
update_halo(out_field, num_halo)
@click.command()
@click.option(
"--nx", type=int, required=True, help="Number of gridpoints in x-direction"
)
@click.option(
"--ny", type=int, required=True, help="Number of gridpoints in y-direction"
)
@click.option(
"--nz", type=int, required=True, help="Number of gridpoints in z-direction"
)
@click.option("--num_iter", type=int, required=True, help="Number of iterations")
@click.option(
"--num_halo",
type=int,
default=2,
help="Number of halo-pointers in x- and y-direction",
)
@click.option(
"--plot_result", type=bool, default=False, help="Make a plot of the result?"
)
def main(nx, ny, nz, num_iter, num_halo=2, plot_result=False):
"""Driver for apply_diffusion that sets up fields and does timings"""
assert 0 < nx <= 1024 * 1024, "You have to specify a reasonable value for nx"
assert 0 < ny <= 1024 * 1024, "You have to specify a reasonable value for ny"
assert 0 < nz <= 1024, "You have to specify a reasonable value for nz"
assert (
0 < num_iter <= 1024 * 1024
), "You have to specify a reasonable value for num_iter"
assert (
2 <= num_halo <= 256
), "Your have to specify a reasonable number of halo points"
alpha = 1.0 / 32.0
in_field = np.zeros((nz, ny + 2 * num_halo, nx + 2 * num_halo))
in_field[
nz // 4 : 3 * nz // 4,
num_halo + ny // 4 : num_halo + 3 * ny // 4,
num_halo + nx // 4 : num_halo + 3 * nx // 4,
] = 1.0
out_field = np.copy(in_field)
np.save("in_field", in_field)
if plot_result:
plt.ioff()
plt.imshow(in_field[in_field.shape[0] // 2, :, :], origin="lower")
plt.colorbar()
plt.savefig("in_field.png")
plt.close()
# warmup caches
apply_diffusion(in_field, out_field, alpha, num_halo)
# time the actual work
tic = time.time()
apply_diffusion(in_field, out_field, alpha, num_halo, num_iter=num_iter)
toc = time.time()
print(f"Elapsed time for work = {toc - tic} s")
np.save("out_field", out_field)
if plot_result:
plt.imshow(out_field[out_field.shape[0] // 2, :, :], origin="lower")
plt.colorbar()
plt.savefig("out_field.png")
plt.close()
if __name__ == "__main__":
main()