Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
and dims == 2
and queue.device.platform.name == "Portable Computing Language"):
# arg list lenghts disagree
pytest.xfail("2D float doesn't work on POCL")
dtype = np.dtype(dtype)
nparticles = 10**5
particles = make_normal_particle_array(queue, nparticles, dims, dtype)
if do_plot:
import matplotlib.pyplot as pt
pt.plot(particles[0].get(), particles[1].get(), "x")
from boxtree import TreeBuilder
tb = TreeBuilder(ctx)
queue.finish()
tree, _ = tb(queue, particles, max_particles_in_box=30, debug=True)
nballs = 10**4
ball_centers = make_normal_particle_array(queue, nballs, dims, dtype)
ball_radii = cl.array.empty(queue, nballs, dtype).fill(0.1)
from boxtree.area_query import (
LeavesToBallsLookupBuilder, SpaceInvaderQueryBuilder)
siqb = SpaceInvaderQueryBuilder(ctx)
# We can use leaves-to-balls lookup to get the set of overlapping balls for
# each box, and from there to compute the outer space invader distance.
lblb = LeavesToBallsLookupBuilder(ctx)
siq, _ = siqb(queue, tree, ball_centers, ball_radii)
lbl, _ = lblb(queue, tree, ball_centers, ball_radii)
# get data to host for test
tree = tree.get(queue=queue)
siq = siq.get(queue=queue)
lbl = lbl.get(queue=queue)
ball_centers = np.array([x.get() for x in ball_centers])
ball_radii = ball_radii.get()
# Find leaf boxes.
from boxtree import box_flags_enum
outer_space_invader_dist = np.zeros(tree.nboxes)
for ibox in range(tree.nboxes):
# We only want leaves here.
if tree.box_flags[ibox] & box_flags_enum.HAS_CHILDREN:
continue
start, end = lbl.balls_near_box_starts[ibox:ibox + 2]
space_invaders = lbl.balls_near_box_lists[start:end]
if len(space_invaders) > 0:
outer_space_invader_dist[ibox] = np.max(np.abs(
tree.box_centers[:, ibox].reshape((-1, 1))
- ball_centers[:, space_invaders]))
assert np.allclose(siq, outer_space_invader_dist)
# }}}
# {{{ test_same_tree_with_zero_weight_particles
@pytest.mark.parametrize("dims", [2, 3])
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
def test_same_tree_with_zero_weight_particles(ctx_factory, dims):
logging.basicConfig(level=logging.INFO)
ntargets_values = [300, 400, 500]
stick_out_factors = [0, 0.1, 0.3, 1]
nsources = 20
ctx = ctx_factory()
queue = cl.CommandQueue(ctx)
from boxtree import TreeBuilder
tb = TreeBuilder(ctx)
trees = []
for stick_out_factor in stick_out_factors:
for ntargets in [40]:
np.random.seed(10)
sources = np.random.rand(dims, nsources)**2
sources[:, 0] = -0.1
sources[:, 1] = 1.1
np.random.seed()
targets = np.random.rand(dims, max(ntargets_values))[:, :ntargets].copy()
target_radii = np.random.rand(max(ntargets_values))[:ntargets]
sources = cl.array.to_device(queue, sources)
targets = cl.array.to_device(queue, targets)
refine_weights = cl.array.empty(queue, nsources + ntargets, np.int32)
refine_weights[:nsources] = 1
refine_weights[nsources:] = 0
tree, _ = tb(queue, sources, targets=targets,
target_radii=target_radii,
stick_out_factor=stick_out_factor,
max_leaf_refine_weight=10,
refine_weights=refine_weights,
debug=True)
tree = tree.get(queue=queue)
trees.append(tree)
print("TREE:", tree.nboxes)
if 0:
import matplotlib.pyplot as plt
for tree in trees:
plt.figure()
tree.plot()
plt.show()
# }}}
# {{{ test_max_levels_error
def test_max_levels_error(ctx_factory):
ctx = ctx_factory()
queue = cl.CommandQueue(ctx)
from boxtree import TreeBuilder
tb = TreeBuilder(ctx)
logging.basicConfig(level=logging.INFO)
sources = [cl.array.zeros(queue, 11, float) for i in range(2)]
from boxtree.tree_build import MaxLevelsExceeded
with pytest.raises(MaxLevelsExceeded):
tree, _ = tb(queue, sources, max_particles_in_box=10, debug=True)
# }}}
# $ python test_tree.py 'test_routine(cl.create_some_context)'