1- import pyopencl as cl
21import numpy as np
32import numpy .linalg as la
43
4+ import pyopencl as cl
5+
56try :
67 import matplotlib .pyplot as plt
7- except ModuleNotFoundError :
8- plt = None
8+ USE_MATPLOTLIB = True
9+ except ImportError :
10+ USE_MATPLOTLIB = False
911
1012try :
1113 from mayavi import mlab
12- except ModuleNotFoundError :
13- mlab = None
14+ USE_MAYAVI = True
15+ except ImportError :
16+ USE_MAYAVI = False
17+
18+ import logging
19+ logging .basicConfig (level = logging .INFO )
1420
1521
1622def process_kernel (knl , what_operator ):
@@ -45,17 +51,16 @@ def draw_pot_figure(aspect_ratio,
4551 ovsmp_center_exp = 0.66 ,
4652 force_center_side = None ):
4753
48- import logging
49- logging .basicConfig (level = logging .INFO )
50-
5154 if novsmp is None :
5255 novsmp = 4 * nsrc
5356
5457 if what_operator_lpot is None :
5558 what_operator_lpot = what_operator
5659
60+ from sumpy .array_context import PyOpenCLArrayContext
5761 ctx = cl .create_some_context ()
5862 queue = cl .CommandQueue (ctx )
63+ actx = PyOpenCLArrayContext (queue , force_device_scalars = True )
5964
6065 # {{{ make plot targets
6166
@@ -86,16 +91,18 @@ def draw_pot_figure(aspect_ratio,
8691 knl_kwargs = {}
8792
8893 vol_source_knl , vol_target_knl = process_kernel (knl , what_operator )
89- p2p = P2P (ctx , source_kernels = (vol_source_knl ,),
94+ p2p = P2P (actx . context , source_kernels = (vol_source_knl ,),
9095 target_kernels = (vol_target_knl ,),
9196 exclude_self = False ,
9297 value_dtypes = np .complex128 )
9398
9499 lpot_source_knl , lpot_target_knl = process_kernel (knl , what_operator_lpot )
95100
96101 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 ,),
102+ lpot = LayerPotential (actx .context ,
103+ expansion = expn_class (knl , order = order ),
104+ source_kernels = (lpot_source_knl ,),
105+ target_kernels = (lpot_target_knl ,),
99106 value_dtypes = np .complex128 )
100107
101108 # }}}
@@ -142,8 +149,9 @@ def map_to_curve(t):
142149 + center_side [:, np .newaxis ]
143150 * center_dist * native_curve .normal )
144151
145- #native_curve.plot()
146- #plt.show()
152+ if 0 :
153+ native_curve .plot ()
154+ plt .show ()
147155
148156 volpot_kwargs = knl_kwargs .copy ()
149157 lpot_kwargs = knl_kwargs .copy ()
@@ -169,7 +177,9 @@ def map_to_curve(t):
169177
170178 def apply_lpot (x ):
171179 xovsmp = np .dot (fim , x )
172- evt , (y ,) = lpot (queue , native_curve .pos , ovsmp_curve .pos ,
180+ evt , (y ,) = lpot (actx .queue ,
181+ native_curve .pos ,
182+ ovsmp_curve .pos ,
173183 centers ,
174184 [xovsmp * ovsmp_curve .speed * ovsmp_weights ],
175185 expansion_radii = np .ones (centers .shape [1 ]),
@@ -191,18 +201,22 @@ def apply_lpot(x):
191201 mode_nr = 0
192202 density = np .cos (mode_nr * 2 * np .pi * native_t ).astype (np .complex128 )
193203 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 ,
204+ evt , (vol_pot ,) = p2p (actx .queue ,
205+ fp .points ,
206+ native_curve .pos ,
195207 [native_curve .speed * native_weights * density ], ** volpot_kwargs )
196208
197- evt , (curve_pot ,) = lpot (queue , native_curve .pos , ovsmp_curve .pos ,
209+ evt , (curve_pot ,) = lpot (actx .queue ,
210+ native_curve .pos ,
211+ ovsmp_curve .pos ,
198212 centers ,
199213 [ovsmp_density * ovsmp_curve .speed * ovsmp_weights ],
200214 expansion_radii = np .ones (centers .shape [1 ]),
201215 ** lpot_kwargs )
202216
203217 # }}}
204218
205- if 0 :
219+ if USE_MATPLOTLIB :
206220 # {{{ plot on-surface potential in 2D
207221
208222 plt .plot (curve_pot , label = "pot" )
@@ -216,7 +230,7 @@ def apply_lpot(x):
216230 ("potential" , vol_pot .real )
217231 ])
218232
219- if 0 :
233+ if USE_MATPLOTLIB :
220234 # {{{ 2D false-color plot
221235
222236 plt .clf ()
@@ -230,12 +244,8 @@ def apply_lpot(x):
230244 # close the curve
231245 plt .plot (src [- 1 ::- len (src )+ 1 , 0 ], src [- 1 ::- len (src )+ 1 , 1 ], "o-k" )
232246
233- #plt.gca().set_aspect("equal", "datalim")
234247 cb = plt .colorbar (shrink = 0.9 )
235248 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())
239249 fp .set_matplotlib_limits ()
240250
241251 # }}}
@@ -261,7 +271,7 @@ def apply_lpot(x):
261271 plotval_vol [outlier_flag ] = sum (
262272 nb [outlier_flag ] for nb in neighbors )/ len (neighbors )
263273
264- if mlab is not None :
274+ if USE_MAYAVI :
265275 fp .show_scalar_in_mayavi (scale * plotval_vol , max_val = 1 )
266276 mlab .colorbar ()
267277 if 1 :
@@ -275,17 +285,23 @@ def apply_lpot(x):
275285
276286
277287if __name__ == "__main__" :
278- draw_pot_figure (aspect_ratio = 1 , nsrc = 100 , novsmp = 100 , helmholtz_k = (35 + 4j )* 0.3 ,
288+ draw_pot_figure (
289+ aspect_ratio = 1 , nsrc = 100 , novsmp = 100 , helmholtz_k = (35 + 4j )* 0.3 ,
279290 what_operator = "D" , what_operator_lpot = "D" , force_center_side = 1 )
291+ if USE_MATPLOTLIB :
292+ plt .savefig ("eigvals-ext-nsrc100-novsmp100.pdf" )
293+ plt .clf ()
280294
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")
295+ # draw_pot_figure(
296+ # aspect_ratio=1, nsrc=100, novsmp=100, helmholtz_k=0,
297+ # what_operator="D", what_operator_lpot="D", force_center_side=-1)
298+ # plt.savefig("eigvals-int-nsrc100-novsmp100.pdf")
299+ # plt.clf()
300+
301+ # draw_pot_figure(
302+ # aspect_ratio=1, nsrc=100, novsmp=200, helmholtz_k=0,
303+ # what_operator="D", what_operator_lpot="D", force_center_side=-1)
304+ # plt.savefig("eigvals-int-nsrc100-novsmp200.pdf")
305+ # plt.clf()
290306
291307# vim: fdm=marker
0 commit comments