Newer
Older
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from __future__ import division
__copyright__ = "Copyright (C) 2013 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy as np
import numpy.linalg as la
import pyopencl as cl
import pytools.test
from pyopencl.tools import pytest_generate_tests_for_pyopencl \
as pytest_generate_tests
from boxtree.tools import make_particle_array, particle_array_to_host
# {{{ fmm interaction completeness test
class ConstantOneExpansionWrangler:
"""This implements the 'analytical routines' for a Green's function that is
constant 1 everywhere. For 'charges' of 'ones', this should get every particle
a copy of the particle count.
"""
def __init__(self, tree):
self.tree = tree
def expansion_zeros(self):
return np.zeros(self.tree.nboxes, dtype=np.float64)
def potential_zeros(self):
return np.zeros(self.tree.ntargets, dtype=np.float64)
def _get_source_slice(self, ibox):
pstart = self.tree.box_source_starts[ibox]
return slice(
pstart, pstart + self.tree.box_source_counts[ibox])
def _get_target_slice(self, ibox):
pstart = self.tree.box_target_starts[ibox]
return slice(
pstart, pstart + self.tree.box_target_counts[ibox])
def reorder_src_weights(self, src_weights):
return src_weights[self.tree.user_source_ids]
def reorder_potentials(self, potentials):
return potentials[self.tree.sorted_target_ids]
def form_multipoles(self, leaf_boxes, src_weights):
mpoles = self.expansion_zeros()
for ibox in leaf_boxes:
pslice = self._get_source_slice(ibox)
mpoles[ibox] += np.sum(src_weights[pslice])
return mpoles
def coarsen_multipoles(self, parent_boxes, start_parent_box, end_parent_box,
mpoles):
tree = self.tree
for ibox in parent_boxes[start_parent_box:end_parent_box]:
for child in tree.box_child_ids[:, ibox]:
if child:
mpoles[ibox] += mpoles[child]
def eval_direct(self, leaf_boxes, neighbor_leaves_starts, neighbor_leaves_lists,
src_weights):
pot = self.potential_zeros()
for itgt_leaf, itgt_box in enumerate(leaf_boxes):
tgt_pslice = self._get_target_slice(itgt_box)
src_sum = 0
start, end = neighbor_leaves_starts[itgt_leaf:itgt_leaf+2]
for isrc_box in neighbor_leaves_lists[start:end]:
src_pslice = self._get_source_slice(isrc_box)
src_sum += np.sum(src_weights[src_pslice])
pot[tgt_pslice] = src_sum
return pot
def multipole_to_local(self, starts, lists, mpole_exps):
local_exps = self.expansion_zeros()
for itgt_box in xrange(self.tree.nboxes):
start, end = starts[itgt_box:itgt_box+2]
contrib = 0
#print itgt_box, "<-", lists[start:end]
for isrc_box in lists[start:end]:
contrib += mpole_exps[isrc_box]
local_exps[itgt_box] += contrib
return local_exps
def eval_multipoles(self, leaf_boxes, sep_smaller_nonsiblings_starts,
sep_smaller_nonsiblings_lists, mpole_exps):
pot = self.potential_zeros()
for itgt_leaf, itgt_box in enumerate(leaf_boxes):
tgt_pslice = self._get_target_slice(itgt_box)
contrib = 0
start, end = sep_smaller_nonsiblings_starts[itgt_leaf:itgt_leaf+2]
for isrc_box in sep_smaller_nonsiblings_lists[start:end]:
contrib += mpole_exps[isrc_box]
pot[tgt_pslice] += contrib
return pot
def refine_locals(self, start_box, end_box, local_exps):
for ibox in xrange(start_box, end_box):
local_exps[ibox] += local_exps[self.tree.box_parent_ids[ibox]]
return local_exps
def eval_locals(self, leaf_boxes, local_exps):
pot = self.potential_zeros()
for ibox in leaf_boxes:
tgt_pslice = self._get_target_slice(ibox)
pot[tgt_pslice] += local_exps[ibox]
return pot
@pytools.test.mark_test.opencl
def test_fmm_completeness(ctx_getter):
"""Tests whether the built FMM traversal structures and driver completely
capture all interactions.
"""
ctx = ctx_getter()
queue = cl.CommandQueue(ctx)
for dims in [
2,
3
]:
for nsources, ntargets in [
(10**6, None),
(10**5, 3 * 10**5),
]:
dtype = np.float64
sources = make_particle_array(queue, nsources, dims, dtype, seed=15)
if ntargets is None:
# This says "same as sources" to the tree builder.
targets = None
else:
targets = make_particle_array(
queue, ntargets, dims, dtype, seed=18)
from boxtree import TreeBuilder
tb = TreeBuilder(ctx)
tree = tb(queue, sources, targets=targets,
max_particles_in_box=30, debug=True)
print "tree built"
from boxtree.traversal import FMMTraversalBuilder
tg = FMMTraversalBuilder(ctx)
trav = tg(queue, tree).get()
print "traversal built"
weights = np.random.randn(nsources)
#weights = np.ones(nparticles)
weights_sum = np.sum(weights)
from boxtree.fmm import drive_fmm
wrangler = ConstantOneExpansionWrangler(trav.tree)
if ntargets is None:
# This check only works for targets == sources.
assert (wrangler.reorder_potentials(
wrangler.reorder_src_weights(weights)) == weights).all()
pot = drive_fmm(trav, wrangler, weights)
# {{{ build, evaluate matrix (and identify missing interactions)
if 0:
mat = np.zeros((ntargets, nsources), dtype)
from pytools import ProgressBar
pb = ProgressBar("matrix", nsources)
for i in xrange(nsources):
unit_vec = np.zeros(nsources, dtype=dtype)
unit_vec[i] = 1
mat[:,i] = drive_fmm(trav, wrangler, unit_vec)
pb.progress()
pb.finished()
missing_tgts, missing_srcs = np.where(mat == 0)
if len(missing_tgts):
import matplotlib.pyplot as pt
from boxtree.visualization import TreePlotter
plotter = TreePlotter(tree)
plotter.draw_tree(fill=False, edgecolor="black")
plotter.draw_box_numbers()
plotter.set_bounding_box()
for tgt, src in zip(missing_tgts, missing_srcs):
pt.plot(
trav.tree.particles[0][tgt],
trav.tree.particles[1][tgt],
"ro")
pt.plot(
trav.tree.particles[0][src],
trav.tree.particles[1][src],
"go")
pt.show()
#pt.spy(mat)
#pt.show()
# }}}
assert la.norm((pot - weights_sum) / nsources) < 1e-8
# }}}
# {{{ test Helmholtz fmm with pyfmmlib
@pytools.test.mark_test.opencl
def test_pyfmmlib_fmm(ctx_getter):
from pytest import importorskip
importorskip("pyfmmlib")
ctx = ctx_getter()
queue = cl.CommandQueue(ctx)
nsources = 10**3
ntargets = 10**3
dims = 2
dtype = np.float64
helmholtz_k = 2
sources = make_particle_array(queue, nsources, dims, dtype, seed=15)
targets = (
make_particle_array(queue, ntargets, dims, dtype, seed=18)
+ np.array([2, 0]))
sources_host = particle_array_to_host(sources)
targets_host = particle_array_to_host(targets)
from boxtree import TreeBuilder
tb = TreeBuilder(ctx)
tree = tb(queue, sources, targets=targets,
max_particles_in_box=30, debug=True)
print "tree built"
from boxtree.traversal import FMMTraversalBuilder
tg = FMMTraversalBuilder(ctx)
trav = tg(queue, tree).get()
print "traversal built"
from pyopencl.clrandom import RanluxGenerator
rng = RanluxGenerator(queue, seed=20)
weights = rng.uniform(queue, nsources, dtype=np.float64).get()
#weights = np.ones(nsources)
from pyfmmlib import hpotgrad2dall_vec
ref_pot, _, _ = hpotgrad2dall_vec(ifgrad=False, ifhess=False,
sources=sources_host.T, charge=weights,
targets=targets_host.T, zk=helmholtz_k)
from boxtree.pyfmmlib_integration import Helmholtz2DExpansionWrangler
wrangler = Helmholtz2DExpansionWrangler(trav.tree, helmholtz_k, nterms=30)
from boxtree.fmm import drive_fmm
pot = drive_fmm(trav, wrangler, weights)
rel_err = la.norm(pot - ref_pot) / la.norm(ref_pot)
print rel_err
#assert < 1e-8
#assert la.norm((pot - weights_sum) / nparticles) < 1e-8
# }}}
# You can test individual routines by typing
# $ python test_fmm.py 'test_routine(cl.create_some_context)'
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
from py.test.cmdline import main
main([__file__])
# vim: fdm=marker