-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_tsc.py
207 lines (177 loc) · 5.92 KB
/
test_tsc.py
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
"""
Tests of `abacusnbody.analysis.tsc`.
"""
from pathlib import Path
import asdf
import numpy as np
import pytest
testdir = Path(__file__).parent
refdir = testdir / 'ref_tsc'
@pytest.mark.parametrize('ngrid', [10, 256])
@pytest.mark.parametrize('dtype', ['f4', 'f8'])
@pytest.mark.parametrize('nthread', [1, -1], ids=['serial', 'parallel'])
@pytest.mark.filterwarnings('ignore:.*dtype')
@pytest.mark.filterwarnings('ignore:.*npartition')
class TestTSC:
box = 123.0
coord = 0
def test_single(self, ngrid, dtype, nthread):
from abacusnbody.analysis.tsc import tsc_parallel
box = self.box
coord = self.coord
# single particle test
cen = np.array([5, 6, 7])
single = (cen / ngrid * box).astype(dtype).reshape(1, -1)
dens = tsc_parallel(single, ngrid, box, nthread=nthread, coord=coord)
assert (dens == 0).sum() == ngrid**3 - 27
assert np.isclose(dens.sum(), 1.0)
cube = dens[
cen[0] - 1 : cen[0] + 2, cen[1] - 1 : cen[1] + 2, cen[2] - 1 : cen[2] + 2
]
# corners
assert np.allclose(
[
cube[0, 0, 0],
cube[0, 0, 2],
cube[0, 2, 0],
cube[0, 2, 2],
cube[2, 0, 0],
cube[2, 0, 2],
cube[2, 2, 0],
cube[2, 2, 2],
],
0.5**9,
)
# edges
assert np.allclose(
[
cube[0, 0, 1],
cube[0, 1, 0],
cube[1, 0, 0],
cube[0, 2, 1],
cube[0, 1, 2],
cube[1, 0, 2],
cube[2, 0, 1],
cube[2, 1, 0],
cube[1, 2, 0],
cube[2, 2, 1],
cube[2, 1, 2],
cube[1, 2, 2],
],
0.5**6 * 0.75,
)
# faces
assert np.allclose(
[
cube[1, 1, 0],
cube[1, 0, 1],
cube[0, 1, 1],
cube[1, 1, 2],
cube[1, 2, 1],
cube[2, 1, 1],
],
0.5**3 * 0.75**2,
)
# center
assert np.allclose(cube[1, 1, 1], 0.75**3)
def test_multi(
self,
ngrid,
dtype,
nthread,
save_result=False,
save_nbodykit=False,
):
from abacusnbody.analysis.tsc import _tsc_scatter, tsc_parallel
# multi-particle tests
box = self.box
coord = self.coord
N = 10000
seed = 234
rng = np.random.default_rng(seed)
pos = rng.random((N, 3), dtype='f4').astype(dtype) * box
weights = rng.random((N,), dtype='f4').astype(dtype)
dens = tsc_parallel(
pos,
ngrid,
box,
nthread=nthread,
coord=coord,
weights=weights,
)
assert np.isclose(dens.sum(dtype='f8'), weights.sum(dtype='f8'))
# compare with the serial, pure-Python version
pydens = np.zeros((ngrid, ngrid, ngrid), dtype=np.float32)
_tsc_scatter.py_func(pos, pydens, box, weights)
assert np.allclose(dens, pydens)
# compare with a saved result
ref_fn = refdir / f'tsc_ngrid{ngrid}.asdf'
if save_result and nthread == 1 and dtype == 'f8':
with asdf.AsdfFile(dict(pydens=pydens)) as af:
af.write_to(ref_fn, all_array_compression='blsc')
with asdf.open(ref_fn) as af:
savedens = af['pydens']
assert np.allclose(dens, savedens, rtol=1e-4, atol=1e-5)
# compare with nbodykit
nbodykit_fn = refdir / f'nbodykit_tsc_ngrid{ngrid}.asdf'
if save_nbodykit and nthread == 1 and dtype == 'f8':
from nbodykit.source.catalog import ArrayCatalog
cat = ArrayCatalog({'Position': pos, 'Weight': weights}, BoxSize=box)
mesh = np.array(
cat.to_mesh(
Nmesh=ngrid,
resampler='tsc',
compensated=False,
interlaced=False,
dtype='f4',
).compute()
)
mesh *= weights.sum(dtype='f8') / ngrid**3
with asdf.AsdfFile(dict(mesh=mesh)) as af:
af.write_to(nbodykit_fn, all_array_compression='blsc')
with asdf.open(nbodykit_fn) as af:
savedens = af['mesh']
assert np.allclose(dens, savedens, rtol=1e-4, atol=1e-5)
@pytest.mark.parametrize('seed', [123, 456], ids=['seed1', 'seed2'])
@pytest.mark.parametrize('dtype', ['f4', 'f8'])
@pytest.mark.parametrize('npartition', [1, 1000], ids=['1p', 'Np'])
@pytest.mark.parametrize('nthread', [1, -1], ids=['serial', 'parallel'])
def test_partition(seed, dtype, npartition, nthread):
from abacusnbody.analysis.tsc import partition_parallel
rng = np.random.default_rng(seed)
box = 123.0
N = 10000
coord = 0
pos = rng.random((N, 3), dtype=dtype) * box
weights = rng.random((N,), dtype=dtype)
ppart, starts, wpart = partition_parallel(
pos,
npartition,
box,
weights=weights,
coord=coord,
nthread=nthread,
)
# Partition with Numpy
keys = (pos[:, coord] * (npartition / box)).astype(np.int32)
iord = keys.argsort()
pos = pos[iord]
weights = weights[iord]
np_counts = np.bincount(keys, minlength=npartition)
np_starts = np.empty(npartition + 1, dtype=np.int64)
np_starts[0] = 0
np_starts[1:] = np_counts.cumsum()
assert np.all(np_starts == starts)
for i in range(npartition):
assert np.all(
np.isin(
ppart[starts[i] : starts[i + 1]],
pos[np_starts[i] : np_starts[i + 1]],
)
)
assert np.all(
np.isin(
wpart[starts[i] : starts[i + 1]],
weights[np_starts[i] : np_starts[i + 1]],
)
)