-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_combischeme_utils.py
executable file
·416 lines (359 loc) · 16 KB
/
test_combischeme_utils.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
import combischeme_utils
import combischeme_output
from icecream import ic
import numpy as np
import scipy
import pytest
import time
# run this test file with `pytest test_combischeme_utils.py``
def validate_combischeme(scheme: combischeme_utils.CombinationScheme):
assert (scheme.get_levels_of_nonzero_coefficient() is not None)
assert (len(list(scheme.get_levels_of_nonzero_coefficient())
[0]) == scheme.get_dimensionality())
ic(scheme.get_combination_dictionary())
assert (scheme.get_nonzero_coefficients() is not None)
ic(scheme.get_nonzero_coefficients())
assert (not (0. in scheme.get_nonzero_coefficients()))
sum_coefficients = sum(scheme.get_nonzero_coefficients())
assert (sum_coefficients == 1.)
def test_create_regular_combischeme(dim=3):
lmin = [1]*dim
lmax = [6]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
print(np.sum(scheme.get_nonzero_coefficients))
for d in range(dim):
corner = lmin.copy()
corner[d] = lmax[d]
assert (tuple(corner) in list(
scheme.get_levels_of_nonzero_coefficient()))
for d2 in range(dim):
corner_neighbor = corner.copy()
corner_neighbor[d2] += 1
assert (tuple(corner_neighbor) not in list(
scheme.get_levels_of_nonzero_coefficient()))
validate_combischeme(scheme)
def test_create_non_regular_combischeme(dim=3):
lmin = [1]*dim
lmax = [6]*dim
lmax[0] = 5
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
validate_combischeme(scheme)
def test_create_degenerate_combischeme(dim=3):
lmin = [1]*dim
lmax = [6]*dim
lmax[0] = lmin[0]
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
validate_combischeme(scheme)
assert (len(scheme.get_nonzero_coefficients()) > 1)
def test_create_combischeme_from_dict():
dictionary = {(2, 2, 2): 1,
(2, 2, 3): -2,
(2, 2, 4): 1,
(2, 3, 2): -2,
(2, 3, 3): 1,
(2, 4, 2): 1,
(3, 2, 2): -2,
(3, 2, 3): 1,
(3, 3, 2): 1,
(4, 2, 2): 1}
scheme = combischeme_utils.CombinationSchemeFromCombinationDictionary(
dictionary)
validate_combischeme(scheme)
def test_write_combischeme(dim=4):
lmin = [1]*dim
lmax = [6]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
combischeme_utils.write_scheme_to_json(scheme)
def test_partition_integer(integer=6):
partitions = combischeme_utils.partition_integer(integer)
partitions = list(partitions)
ic(partitions)
assert (len(list(partitions[0])) == integer)
assert ([1]*integer in partitions)
assert ([integer] in partitions)
single_partition = combischeme_utils.partition_integer_in_num_partitions(
integer, 1)
assert (list(single_partition) == [[integer]])
single_partition_of_ones = combischeme_utils.partition_integer_in_num_partitions(
integer, integer)
assert (list(single_partition_of_ones) == [[1]*integer])
single_partition_of_ones = combischeme_utils.partition_integer_in_num_partitions(
integer, integer)
assert (list(single_partition_of_ones) == [[1]*integer])
# assert (len(partitions) == scipy.special.binom(integer, i))
def test_partition_integer_in_num_partitions_with_zeros(integer=6):
filled_partitions = combischeme_utils.partition_integer_in_num_partitions_with_zeros(
integer, integer)
for i in range(integer):
zeros = [0]*(integer)
zeros[i] = integer
assert zeros in filled_partitions
assert ([1]*integer in filled_partitions)
def test_compute_active_set(dim=6):
lmin = [2]*dim
lmax = [8]*dim
tic = time.perf_counter()
active_set1 = combischeme_utils.compute_active_set(lmin, lmax)
toc = time.perf_counter()
time1 = toc - tic
print(f"first active set in {time1:0.4f}seconds")
ic(active_set1)
assert active_set1.intersection([(2, 2, 2, 2, 2, 8)]) != set()
assert active_set1.intersection([(2, 2, 2, 2, 8, 2)]) != set()
assert active_set1.intersection([(2, 2, 2, 8, 2, 2)]) != set()
assert active_set1.intersection([(2, 2, 8, 2, 2, 2)]) != set()
assert active_set1.intersection([(2, 8, 2, 2, 2, 2)]) != set()
assert active_set1.intersection([(8, 2, 2, 2, 2, 2)]) != set()
lmin[0] += 1
tic = time.perf_counter()
active_set2 = combischeme_utils.compute_active_set(lmin, lmax)
toc = time.perf_counter()
time2 = toc - tic
print(f"second active set in {time2:0.4f}seconds")
ic(active_set2)
# assert active_set1.intersection(active_set2) == active_set2
assert len(active_set1) == len(active_set2) + 1
assert time1 < time2
def test_necessary_sparse_grid_spaces(dim=3):
lmin = [1]*dim
lmax = [6]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
subspaces = scheme.get_sparse_grid_spaces()
# this should be a downward closed set up to a reduced level sum
necessary_subspaces = scheme.get_necessary_sparse_grid_spaces()
assert len(necessary_subspaces) < len(subspaces)
assert (tuple(lmin) in list(necessary_subspaces))
assert (tuple([0]*len(lmin)) in list(necessary_subspaces))
for d in range(dim):
corner = lmin.copy()
corner[d] = lmax[d] - 1
assert (tuple(corner) in list(necessary_subspaces))
for d2 in range(dim):
corner_neighbor = corner.copy()
corner_neighbor[d2] += 1
assert (tuple(corner_neighbor) not in list(necessary_subspaces))
assert combischeme_utils.is_downward_closed_set(necessary_subspaces)
num_sg_dof = combischeme_utils.get_num_dof_of_subspaces(
necessary_subspaces, boundary=[2]*dim)
# regression test
if dim == 3:
assert num_sg_dof == 1505
if dim == 6:
assert num_sg_dof == 159489
other_scheme = combischeme_utils.CombinationSchemeFromCombinationDictionary(
scheme.get_combination_dictionary())
other_subspaces = scheme.get_sparse_grid_spaces()
assert other_subspaces == subspaces
with pytest.raises(NotImplementedError):
other_scheme.get_necessary_sparse_grid_spaces()
def test_get_num_dof():
boundary = [2]*3
level_vectors = [[2, 2, 2], [2, 2, 3], [2, 3, 2], [3, 2, 2]]
# fg dof should be 5^3 + 3 * 5^2*9 = 800
num_fg_dof = combischeme_utils.get_num_dof_of_full_grids(
level_vectors, boundary)
assert num_fg_dof == 800
# sg dof should be 2^3 + 3*2^2*4 = 56
num_sg_dof = combischeme_utils.get_num_dof_of_subspaces(
level_vectors, boundary)
assert num_sg_dof == 56
def test_split_scheme():
dim = 4
lmin = [1]*dim
lmax = [6]*dim
boundary = [1]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
scheme1, scheme2 = combischeme_utils.split_scheme_by_level_sum(
scheme)
# check if the split is correct
for level_vector in scheme.get_levels_of_nonzero_coefficient():
found = 0
if level_vector in scheme1.get_levels_of_nonzero_coefficient():
found += 1
if level_vector in scheme2.get_levels_of_nonzero_coefficient():
found += 1
assert found == 1
# compute necessary sg dofs before
sg_spaces_initial = scheme.get_sparse_grid_spaces()
ic(combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial, boundary))
num_dof_sg_initial = combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial, boundary)
assert (num_dof_sg_initial == 8832)
# compute necessary sg dofs before
sg_spaces_initial_reduced = scheme.get_necessary_sparse_grid_spaces()
ic(combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial_reduced, boundary))
num_dof_sg_initial_reduced = combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial_reduced, boundary)
assert (num_dof_sg_initial_reduced == 3072)
# compute sg dofs after
subspaces1 = scheme1.get_sparse_grid_spaces()
num_dof_sg_1 = combischeme_utils.get_num_dof_of_subspaces(
subspaces1, boundary)
subspaces2 = scheme2.get_sparse_grid_spaces()
num_dof_sg_2 = combischeme_utils.get_num_dof_of_subspaces(
subspaces2, boundary)
ic(num_dof_sg_1, num_dof_sg_2)
assert (num_dof_sg_1 == 4928)
assert (num_dof_sg_2 == 4928)
assert subspaces1.union(subspaces2) == sg_spaces_initial
# compute conjoint sg dofs
conjoint_reduced_subspaces = subspaces1.intersection(subspaces2)
num_conjoint_reduced_dof = combischeme_utils.get_num_dof_of_subspaces(
conjoint_reduced_subspaces, boundary)
ic(num_conjoint_reduced_dof)
assert (num_conjoint_reduced_dof == 1024)
assert num_conjoint_reduced_dof < num_dof_sg_1
assert num_conjoint_reduced_dof < num_dof_sg_2
# compare to full subspaces
all_subspaces = scheme.get_sparse_grid_spaces()
all_subspaces1 = scheme1.get_sparse_grid_spaces()
all_subspaces2 = scheme2.get_sparse_grid_spaces()
assert all_subspaces1.union(all_subspaces2) == all_subspaces
conjoint_all_subspaces = all_subspaces1.intersection(all_subspaces2)
assert conjoint_all_subspaces == conjoint_reduced_subspaces
def test_split_scheme_by_single_dimension():
dim = 4
lmin = [1]*dim
lmax = [6]*dim
boundary = [1]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
scheme1, scheme2 = combischeme_utils.split_scheme_by_single_dimension(
scheme, 3)
# check if the split is correct
for level_vector in scheme.get_levels_of_nonzero_coefficient():
found = 0
if level_vector in scheme1.get_levels_of_nonzero_coefficient():
found += 1
assert (level_vector[-1] >= 3)
if level_vector in scheme2.get_levels_of_nonzero_coefficient():
found += 1
assert (level_vector[-1] < 3)
assert found == 1
# compute necessary sg dofs before
sg_spaces_initial = scheme.get_sparse_grid_spaces()
ic(combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial, boundary))
num_dof_sg_initial = combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial, boundary)
assert (num_dof_sg_initial == 8832)
# compute necessary sg dofs before
sg_spaces_initial_reduced = scheme.get_necessary_sparse_grid_spaces()
ic(combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial_reduced, boundary))
num_dof_sg_initial_reduced = combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial_reduced, boundary)
assert (num_dof_sg_initial_reduced == 3072)
# compute sg dofs after
subspaces1 = scheme1.get_sparse_grid_spaces()
num_dof_sg_1 = combischeme_utils.get_num_dof_of_subspaces(
subspaces1, boundary)
subspaces2 = scheme2.get_sparse_grid_spaces()
num_dof_sg_2 = combischeme_utils.get_num_dof_of_subspaces(
subspaces2, boundary)
ic(num_dof_sg_1, num_dof_sg_2)
assert (num_dof_sg_1 == 4032)
assert (num_dof_sg_2 == 6016)
assert subspaces1.union(subspaces2) == sg_spaces_initial
# compute conjoint sg dofs
conjoint_reduced_subspaces = subspaces1.intersection(subspaces2)
num_conjoint_reduced_dof = combischeme_utils.get_num_dof_of_subspaces(
conjoint_reduced_subspaces, boundary)
ic(num_conjoint_reduced_dof)
assert (num_conjoint_reduced_dof == 1216)
assert num_conjoint_reduced_dof < num_dof_sg_1
assert num_conjoint_reduced_dof < num_dof_sg_2
# compare to full subspaces
all_subspaces = scheme.get_sparse_grid_spaces()
all_subspaces1 = scheme1.get_sparse_grid_spaces()
all_subspaces2 = scheme2.get_sparse_grid_spaces()
assert all_subspaces1.union(all_subspaces2) == all_subspaces
conjoint_all_subspaces = all_subspaces1.intersection(all_subspaces2)
assert conjoint_all_subspaces == conjoint_reduced_subspaces
def test_split_scheme_metis():
dim = 4
lmin = [1]*dim
lmax = [6]*dim
boundary = [1]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
# TODO it would be nicer if this was installed in the CI
try:
import metis
except (ImportError, RuntimeError):
print("Skipping test_split_scheme_metis because metis is not installed")
return
# use metis split
schemes_metis = scheme.split_scheme_metis(2)
assert (len(schemes_metis) == 2)
scheme1_metis = schemes_metis[0]
scheme2_metis = schemes_metis[1]
# TODO check that all are assigned
assert len(scheme.get_levels_of_nonzero_coefficient()) == sum(
[len(part_scheme.get_levels_of_nonzero_coefficient()) for part_scheme in schemes_metis])
# compute sg dofs before
sg_spaces_initial = scheme.get_sparse_grid_spaces()
ic(combischeme_utils.get_num_dof_of_subspaces(
sg_spaces_initial, boundary))
# compute sg dofs after
assert combischeme_utils.get_union_subspaces(
schemes_metis) == scheme.get_sparse_grid_spaces()
subspaces1 = scheme1_metis.get_sparse_grid_spaces()
num_dof_sg_1 = combischeme_utils.get_num_dof_of_subspaces(
subspaces1, boundary)
subspaces2 = scheme2_metis.get_sparse_grid_spaces()
num_dof_sg_2 = combischeme_utils.get_num_dof_of_subspaces(
subspaces2, boundary)
ic(num_dof_sg_1, num_dof_sg_2)
# check if they are the same number as for level-sum split
assert (num_dof_sg_1 == 4928)
assert (num_dof_sg_2 == 4928)
# check conjoint spaces /dofs
conjoint_reduced_subspaces = combischeme_utils.get_conjoint_subspaces(
schemes_metis)
num_conjoint_reduced_dof = combischeme_utils.get_num_dof_of_subspaces(
conjoint_reduced_subspaces, boundary)
assert (num_conjoint_reduced_dof == 1024)
def test_integration_create_split_assign(dim=4):
lmin = [1]*dim
lmax = [6]*dim
scheme = combischeme_utils.CombinationSchemeFromMaxLevel(lmax, lmin)
scheme1, scheme2 = combischeme_utils.split_scheme_by_level_sum(scheme)
num_process_groups1 = 16
num_process_groups2 = 13
assignment1, assigned_FG_size1 = combischeme_utils.assign_combischeme_to_groups(
scheme1, num_process_groups1)
assignment2, assigned_FG_size2 = combischeme_utils.assign_combischeme_to_groups(
scheme2, num_process_groups2)
assert (len(assignment1) == num_process_groups1)
assert (len(assignment2) == num_process_groups2)
assert np.max(assigned_FG_size1) < np.min(assigned_FG_size1)*1.2
assert np.max(assigned_FG_size1) < np.min(assigned_FG_size2)
filename1 = "scheme_test_split_1_" + \
format(num_process_groups1, '05d')+"groups.json"
filename2 = "scheme_test_split_2_" + \
format(num_process_groups2, '05d')+"groups.json"
combischeme_output.write_assignment_to_json(
assignment1, filename1)
combischeme_output.write_assignment_to_json(
assignment2, filename2)
scheme1read = combischeme_utils.CombinationSchemeFromFile(filename1)
scheme2read = combischeme_utils.CombinationSchemeFromFile(filename2)
assignment1read, assigned_FG_size_read1 = combischeme_utils.assign_combischeme_to_groups(
scheme1read, num_process_groups1)
assignment2read, assigned_FG_size_read2 = combischeme_utils.assign_combischeme_to_groups(
scheme2read, num_process_groups2)
assert (assigned_FG_size1 == assigned_FG_size_read1)
assert (assigned_FG_size2 == assigned_FG_size_read2)
# now with offset for first group
assignment1read, assigned_FG_size_read1 = combischeme_utils.assign_combischeme_to_groups(
scheme1read, num_process_groups1, 2000)
assignment2read, assigned_FG_size_read2 = combischeme_utils.assign_combischeme_to_groups(
scheme2read, num_process_groups2, 3000)
assert (sum(assigned_FG_size1) == sum(assigned_FG_size_read1))
assert (sum(assigned_FG_size2) == sum(assigned_FG_size_read2))
assert (len(assignment1read) == num_process_groups1)
assert (len(assignment2read) == num_process_groups2)
assert np.max(assigned_FG_size_read1) > np.min(assigned_FG_size_read1)*2
assert np.max(assigned_FG_size_read2) > np.min(assigned_FG_size_read2)*2