-
Notifications
You must be signed in to change notification settings - Fork 103
/
ducctest.py
280 lines (222 loc) · 8.65 KB
/
ducctest.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
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
import numpy as np
import pyshtools as pysh
from time import time
nthreads = 1
def _l2error(a, b):
return np.sqrt(np.sum(np.abs(a - b) ** 2) / np.sum(np.abs(a) ** 2))
# force SHTOOLS to deallocate temporary buffers
def flush_buffers(grd):
degrees = np.arange(1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
pysh.backends.select_preferred_backend("shtools")
clm = pysh.SHCoeffs.from_random(power, seed=12345)
grid2 = clm.expand(grid=grd)
_ = grid2.expand()
clm = pysh.SHCoeffs.from_random(power, seed=12345, kind="complex")
grid2 = clm.expand(grid=grd)
_ = grid2.expand()
def test_SHT(lmax, grd, csphase, normalization, extend):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345)
clm = clm.convert(normalization=normalization, csphase=csphase, lmax=lmax)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
grid = clm.expand(grid=grd, extend=extend)
cilm = grid.expand()
tducc = time() - t0
pysh.backends.select_preferred_backend("shtools")
t0 = time()
grid2 = clm.expand(grid=grd, extend=extend)
cilm2 = grid2.expand()
tshtools = time() - t0
flush_buffers(grd)
return (
_l2error(grid.to_array(), grid2.to_array())
+ _l2error(cilm.to_array(), cilm2.to_array()),
tshtools / tducc,
)
def test_SHTducc(lmax, grd, nthreads):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345)
clm = clm.convert(normalization="ortho", csphase=1, lmax=lmax)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
grid = clm.expand(grid=grd, extend=False)
cilm = grid.expand(normalization="ortho", csphase=1)
tducc = time() - t0
return _l2error(clm.to_array(), cilm.to_array()), tducc
def test_SHTC(lmax, grd, csphase, normalization, extend):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345, kind="complex")
clm = clm.convert(normalization=normalization, csphase=csphase, lmax=lmax)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
grid = clm.expand(grid=grd, extend=extend)
cilm = grid.expand(normalization=normalization, csphase=csphase)
tducc = time() - t0
pysh.backends.select_preferred_backend("shtools")
t0 = time()
grid2 = clm.expand(grid=grd, extend=extend)
cilm2 = grid2.expand(normalization=normalization, csphase=csphase)
tshtools = time() - t0
flush_buffers(grd)
return (
_l2error(grid.to_array(), grid2.to_array())
+ _l2error(cilm.to_array(), cilm2.to_array()),
tshtools / tducc,
)
def test_SHT_deriv(lmax, grd, csphase, extend):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = 1.0
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345)
clm = clm.convert(csphase=csphase, lmax=lmax)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
grad = clm.gradient(extend=extend, radius=3.4)
tducc = time() - t0
pysh.backends.select_preferred_backend("shtools")
t0 = time()
grad2 = clm.gradient(extend=extend, radius=1.0)
tshtools = time() - t0
flush_buffers(grd)
return (
_l2error(3.4 * grad.phi.to_array(), grad2.phi.to_array())
+ _l2error(3.4 * grad.theta.to_array(), grad2.theta.to_array()),
tshtools / tducc,
)
def test_rot(lmax, alpha, beta, gamma):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
clm_rotated = clm.rotate(alpha, beta, gamma, degrees=True)
tducc = time() - t0
pysh.backends.select_preferred_backend("shtools")
t0 = time()
clm_rotated2 = clm.rotate(alpha, beta, gamma, degrees=True)
tshtools = time() - t0
return (
_l2error(clm_rotated.to_array(), clm_rotated2.to_array()),
tshtools / tducc,
)
def test_rotc(lmax, alpha, beta, gamma):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345, kind="complex")
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
clm_rotated = clm.rotate(alpha, beta, gamma, degrees=True)
tducc = time() - t0
pysh.backends.select_preferred_backend("shtools")
t0 = time()
clm_rotated2 = clm.rotate(alpha, beta, gamma, degrees=True)
tshtools = time() - t0
return (
_l2error(clm_rotated.to_array(), clm_rotated2.to_array()),
tshtools / tducc,
)
def test_rot2(lmax, alpha, beta, gamma):
degrees = np.arange(lmax + 1, dtype=float)
degrees[0] = np.inf
power = degrees ** (-2)
clm = pysh.SHCoeffs.from_random(power, seed=12345)
pysh.backends.select_preferred_backend("ducc", nthreads=nthreads)
t0 = time()
clm_rotated = clm.rotate(alpha, beta, gamma, degrees=True)
clm_rotated = clm_rotated.rotate(-gamma, -beta, -alpha, degrees=True)
tducc = time() - t0
return _l2error(clm.to_array(), clm_rotated.to_array()), tducc
lmax_list = [127, 255, 511, 1023]
print("SHRealCoeff rotation tests:")
for lmax in lmax_list:
for alpha in [47]:
res = test_rot(lmax, alpha, 27, 59)
print(
"lmax={:4}: L2 error={:e}, speedup factor={:f}".format(
lmax, res[0], res[1]
)
)
print("SHComplexCoeff rotation tests:")
for lmax in lmax_list:
for alpha in [47]:
res = test_rotc(lmax, alpha, 27, 59)
print(
"lmax={:4}: L2 error={:e}, speedup factor={:f}".format(
lmax, res[0], res[1]
)
)
lmax_list = [80]
print("SHT tests unnorm:")
for grid in ["GLQ", "DH", "DH2"]:
for csphase in [-1, 1]:
for norm in ["unnorm"]:
for extend in [True, False]:
for lmax in [5, 10, 20, 85]:
res = test_SHT(lmax, grid, csphase, norm, extend)
print(
"{:3}, CS={:2}, norm={:7}, extend={:5}, lmax={:4}: "
"L2 error={:e}, speedup factor={:f}".format(
grid, csphase, norm, extend, lmax, res[0], res[1]
)
)
lmax_list = [127, 255, 511, 1023, 2047]
print("SHT tests:")
for grid in ["GLQ", "DH", "DH2"]:
for csphase in [-1, 1]:
for norm in ["ortho", "4pi", "schmidt"]:
for extend in [True, False]:
for lmax in lmax_list:
res = test_SHT(lmax, grid, csphase, norm, extend)
print(
"{:3}, CS={:2}, norm={:7}, extend={:5}, lmax={:4}: "
"L2 error={:e}, speedup factor={:f}".format(
grid, csphase, norm, extend, lmax, res[0], res[1]
)
)
print("SHTC tests:")
for grid in ["GLQ", "DH", "DH2"]:
for csphase in [-1, 1]:
for norm in ["ortho", "4pi", "schmidt"]:
for extend in [True, False]:
for lmax in lmax_list:
res = test_SHTC(lmax, grid, csphase, norm, extend)
print(
"{:3}, CS={:2}, norm={:7}, extend={:5}, lmax={:4}: "
"L2 error={:e}, speedup factor={:f}".format(
grid, csphase, norm, extend, lmax, res[0], res[1]
)
)
print("SHT gradient tests:")
for grid in ["DH", "DH2"]:
for csphase in [-1, 1]:
for extend in [True, False]:
for lmax in lmax_list:
res = test_SHT_deriv(lmax, grid, csphase, extend)
print(
"{:3}, CS={:2}, extend={:5}, lmax={:4}: L2 error={:e}, "
"speedup factor={:f}".format(
grid, csphase, extend, lmax, res[0], res[1]
)
)
print("DUCC: forward/backward rotation with high band limits:")
for lmax in [4095]:
for alpha in [47]:
res = test_rot2(lmax, alpha, 27, 59)
print(
"lmax={:4}: L2 error={:e}, time={:f}".format(lmax, res[0], res[1])
)
print("DUCC: forward/backward SHT with high band limits:")
for lmax in [8191]:
res = test_SHTducc(lmax, "GLQ", nthreads=nthreads)
print("lmax={:4}: L2 error={:e}, time={:f}".format(lmax, res[0], res[1]))