Skip to content

Commit 89adfcb

Browse files
committed
port examples to arraycontext
1 parent e93ea11 commit 89adfcb

File tree

3 files changed

+77
-55
lines changed

3 files changed

+77
-55
lines changed

examples/curve-pot.py

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import pyopencl as cl
21
import numpy as np
32
import numpy.linalg as la
43

54
try:
65
import matplotlib.pyplot as plt
7-
except ModuleNotFoundError:
8-
plt = None
6+
USE_MATPLOTLIB = True
7+
except ImportError:
8+
USE_MATPLOTLIB = False
99

1010
try:
1111
from mayavi import mlab
12-
except ModuleNotFoundError:
13-
mlab = None
12+
USE_MAYAVI = True
13+
except ImportError:
14+
USE_MAYAVI = False
15+
16+
import logging
17+
logging.basicConfig(level=logging.INFO)
1418

1519

1620
def process_kernel(knl, what_operator):
@@ -45,17 +49,14 @@ def draw_pot_figure(aspect_ratio,
4549
ovsmp_center_exp=0.66,
4650
force_center_side=None):
4751

48-
import logging
49-
logging.basicConfig(level=logging.INFO)
50-
5152
if novsmp is None:
5253
novsmp = 4*nsrc
5354

5455
if what_operator_lpot is None:
5556
what_operator_lpot = what_operator
5657

57-
ctx = cl.create_some_context()
58-
queue = cl.CommandQueue(ctx)
58+
from sumpy.array_context import _acf
59+
actx = _acf()
5960

6061
# {{{ make plot targets
6162

@@ -86,16 +87,18 @@ def draw_pot_figure(aspect_ratio,
8687
knl_kwargs = {}
8788

8889
vol_source_knl, vol_target_knl = process_kernel(knl, what_operator)
89-
p2p = P2P(ctx, source_kernels=(vol_source_knl,),
90+
p2p = P2P(actx.context, source_kernels=(vol_source_knl,),
9091
target_kernels=(vol_target_knl,),
9192
exclude_self=False,
9293
value_dtypes=np.complex128)
9394

9495
lpot_source_knl, lpot_target_knl = process_kernel(knl, what_operator_lpot)
9596

9697
from sumpy.qbx import LayerPotential
97-
lpot = LayerPotential(ctx, expansion=expn_class(knl, order=order),
98-
source_kernels=(lpot_source_knl,), target_kernels=(lpot_target_knl,),
98+
lpot = LayerPotential(actx.context,
99+
expansion=expn_class(knl, order=order),
100+
source_kernels=(lpot_source_knl,),
101+
target_kernels=(lpot_target_knl,),
99102
value_dtypes=np.complex128)
100103

101104
# }}}
@@ -142,8 +145,9 @@ def map_to_curve(t):
142145
+ center_side[:, np.newaxis]
143146
* center_dist*native_curve.normal)
144147

145-
#native_curve.plot()
146-
#plt.show()
148+
if 0:
149+
native_curve.plot()
150+
plt.show()
147151

148152
volpot_kwargs = knl_kwargs.copy()
149153
lpot_kwargs = knl_kwargs.copy()
@@ -169,7 +173,9 @@ def map_to_curve(t):
169173

170174
def apply_lpot(x):
171175
xovsmp = np.dot(fim, x)
172-
evt, (y,) = lpot(queue, native_curve.pos, ovsmp_curve.pos,
176+
evt, (y,) = lpot(actx.queue,
177+
native_curve.pos,
178+
ovsmp_curve.pos,
173179
centers,
174180
[xovsmp * ovsmp_curve.speed * ovsmp_weights],
175181
expansion_radii=np.ones(centers.shape[1]),
@@ -191,18 +197,22 @@ def apply_lpot(x):
191197
mode_nr = 0
192198
density = np.cos(mode_nr*2*np.pi*native_t).astype(np.complex128)
193199
ovsmp_density = np.cos(mode_nr*2*np.pi*ovsmp_t).astype(np.complex128)
194-
evt, (vol_pot,) = p2p(queue, fp.points, native_curve.pos,
200+
evt, (vol_pot,) = p2p(actx.queue,
201+
fp.points,
202+
native_curve.pos,
195203
[native_curve.speed*native_weights*density], **volpot_kwargs)
196204

197-
evt, (curve_pot,) = lpot(queue, native_curve.pos, ovsmp_curve.pos,
205+
evt, (curve_pot,) = lpot(actx.queue,
206+
native_curve.pos,
207+
ovsmp_curve.pos,
198208
centers,
199209
[ovsmp_density * ovsmp_curve.speed * ovsmp_weights],
200210
expansion_radii=np.ones(centers.shape[1]),
201211
**lpot_kwargs)
202212

203213
# }}}
204214

205-
if 0:
215+
if USE_MATPLOTLIB:
206216
# {{{ plot on-surface potential in 2D
207217

208218
plt.plot(curve_pot, label="pot")
@@ -216,7 +226,7 @@ def apply_lpot(x):
216226
("potential", vol_pot.real)
217227
])
218228

219-
if 0:
229+
if USE_MATPLOTLIB:
220230
# {{{ 2D false-color plot
221231

222232
plt.clf()
@@ -230,12 +240,8 @@ def apply_lpot(x):
230240
# close the curve
231241
plt.plot(src[-1::-len(src)+1, 0], src[-1::-len(src)+1, 1], "o-k")
232242

233-
#plt.gca().set_aspect("equal", "datalim")
234243
cb = plt.colorbar(shrink=0.9)
235244
cb.set_label(r"$\log_{10}(\mathdefault{Error})$")
236-
#from matplotlib.ticker import NullFormatter
237-
#plt.gca().xaxis.set_major_formatter(NullFormatter())
238-
#plt.gca().yaxis.set_major_formatter(NullFormatter())
239245
fp.set_matplotlib_limits()
240246

241247
# }}}
@@ -261,7 +267,7 @@ def apply_lpot(x):
261267
plotval_vol[outlier_flag] = sum(
262268
nb[outlier_flag] for nb in neighbors)/len(neighbors)
263269

264-
if mlab is not None:
270+
if USE_MAYAVI:
265271
fp.show_scalar_in_mayavi(scale*plotval_vol, max_val=1)
266272
mlab.colorbar()
267273
if 1:
@@ -275,17 +281,23 @@ def apply_lpot(x):
275281

276282

277283
if __name__ == "__main__":
278-
draw_pot_figure(aspect_ratio=1, nsrc=100, novsmp=100, helmholtz_k=(35+4j)*0.3,
284+
draw_pot_figure(
285+
aspect_ratio=1, nsrc=100, novsmp=100, helmholtz_k=(35+4j)*0.3,
279286
what_operator="D", what_operator_lpot="D", force_center_side=1)
287+
if USE_MATPLOTLIB:
288+
plt.savefig("eigvals-ext-nsrc100-novsmp100.pdf")
289+
plt.clf()
280290

281-
# plt.savefig("eigvals-ext-nsrc100-novsmp100.pdf")
282-
#plt.clf()
283-
#draw_pot_figure(aspect_ratio=1, nsrc=100, novsmp=100, helmholtz_k=0,
284-
# what_operator="D", what_operator_lpot="D", force_center_side=-1)
285-
#plt.savefig("eigvals-int-nsrc100-novsmp100.pdf")
286-
#plt.clf()
287-
#draw_pot_figure(aspect_ratio=1, nsrc=100, novsmp=200, helmholtz_k=0,
288-
# what_operator="D", what_operator_lpot="D", force_center_side=-1)
289-
#plt.savefig("eigvals-int-nsrc100-novsmp200.pdf")
291+
# draw_pot_figure(
292+
# aspect_ratio=1, nsrc=100, novsmp=100, helmholtz_k=0,
293+
# what_operator="D", what_operator_lpot="D", force_center_side=-1)
294+
# plt.savefig("eigvals-int-nsrc100-novsmp100.pdf")
295+
# plt.clf()
296+
297+
# draw_pot_figure(
298+
# aspect_ratio=1, nsrc=100, novsmp=200, helmholtz_k=0,
299+
# what_operator="D", what_operator_lpot="D", force_center_side=-1)
300+
# plt.savefig("eigvals-int-nsrc100-novsmp200.pdf")
301+
# plt.clf()
290302

291303
# vim: fdm=marker

examples/expansion-toys.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
import pyopencl as cl
2-
import sumpy.toys as t
31
import numpy as np
2+
3+
import sumpy.toys as t
44
from sumpy.visualization import FieldPlotter
5+
from sumpy.kernel import ( # noqa: F401
6+
YukawaKernel,
7+
HelmholtzKernel,
8+
LaplaceKernel)
9+
510
try:
611
import matplotlib.pyplot as plt
7-
except ModuleNotFoundError:
8-
plt = None
12+
USE_MATPLOTLIB = True
13+
except ImportError:
14+
USE_MATPLOTLIB = False
915

1016

1117
def main():
12-
from sumpy.kernel import ( # noqa: F401
13-
YukawaKernel, HelmholtzKernel, LaplaceKernel)
18+
from sumpy.array_context import _acf
19+
actx = _acf()
20+
1421
tctx = t.ToyContext(
15-
cl.create_some_context(),
16-
#LaplaceKernel(2),
22+
actx.context,
23+
# LaplaceKernel(2),
1724
YukawaKernel(2), extra_kernel_kwargs={"lam": 5},
18-
#HelmholtzKernel(2), extra_kernel_kwargs={"k": 0.3},
25+
# HelmholtzKernel(2), extra_kernel_kwargs={"k": 0.3},
1926
)
2027

2128
pt_src = t.PointSources(
@@ -25,7 +32,7 @@ def main():
2532

2633
fp = FieldPlotter([3, 0], extent=8)
2734

28-
if 0 and plt is not None:
35+
if USE_MATPLOTLIB:
2936
t.logplot(fp, pt_src, cmap="jet")
3037
plt.colorbar()
3138
plt.show()
@@ -35,12 +42,12 @@ def main():
3542
lexp = t.local_expand(mexp, [3, 0])
3643
lexp2 = t.local_expand(lexp, [3, 1], 3)
3744

38-
#diff = mexp - pt_src
39-
#diff = mexp2 - pt_src
45+
# diff = mexp - pt_src
46+
# diff = mexp2 - pt_src
4047
diff = lexp2 - pt_src
4148

4249
print(t.l_inf(diff, 1.2, center=lexp2.center))
43-
if 1 and plt is not None:
50+
if USE_MATPLOTLIB:
4451
t.logplot(fp, diff, cmap="jet", vmin=-3, vmax=0)
4552
plt.colorbar()
4653
plt.show()

examples/sym-exp-complexity.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
2-
import pyopencl as cl
32
import loopy as lp
3+
44
from sumpy.kernel import LaplaceKernel, HelmholtzKernel
55
from sumpy.expansion.local import (
66
LinearPDEConformingVolumeTaylorLocalExpansion,
@@ -9,14 +9,17 @@
99
LinearPDEConformingVolumeTaylorMultipoleExpansion,
1010
)
1111
from sumpy.e2e import E2EFromCSR
12+
1213
try:
1314
import matplotlib.pyplot as plt
14-
except ModuleNotFoundError:
15-
plt = None
15+
USE_MATPLOTLIB = True
16+
except ImportError:
17+
USE_MATPLOTLIB = False
1618

1719

1820
def find_flops():
19-
ctx = cl.create_some_context()
21+
from sumpy.array_context import _acf
22+
actx = _acf()
2023

2124
if 0:
2225
knl = LaplaceKernel(2)
@@ -35,7 +38,7 @@ def find_flops():
3538
print(order)
3639
m_expn = m_expn_cls(knl, order)
3740
l_expn = l_expn_cls(knl, order)
38-
m2l = E2EFromCSR(ctx, m_expn, l_expn)
41+
m2l = E2EFromCSR(actx.context, m_expn, l_expn)
3942

4043
loopy_knl = m2l.get_kernel()
4144
loopy_knl = lp.add_and_infer_dtypes(
@@ -74,7 +77,7 @@ def plot_flops():
7477
flops = [45, 194, 474, 931, 1650, 2632, 3925, 5591, 7706, 10272]
7578
filename = "helmholtz-m2l-complexity-2d.pdf"
7679

77-
if plt is not None:
80+
if USE_MATPLOTLIB:
7881
plt.rc("font", size=16)
7982
plt.title(case)
8083
plt.ylabel("Flop count")
@@ -86,5 +89,5 @@ def plot_flops():
8689

8790

8891
if __name__ == "__main__":
89-
#find_flops()
92+
# find_flops()
9093
plot_flops()

0 commit comments

Comments
 (0)