1- import pyopencl as cl
21import numpy as np
32import numpy .linalg as la
43
54try :
65 import matplotlib .pyplot as plt
7- except ModuleNotFoundError :
8- plt = None
6+ USE_MATPLOTLIB = True
7+ except ImportError :
8+ USE_MATPLOTLIB = False
99
1010try :
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
1620def 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
277283if __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
0 commit comments