-
Notifications
You must be signed in to change notification settings - Fork 56
/
check.py
executable file
·267 lines (208 loc) · 10.5 KB
/
check.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
import sys
import numpy as np
from collections import OrderedDict
sys.path = sys.path + ['../python','../../python']
from rfac import *
def _check_header(actual, expected):
if 'E0' in actual:
assert np.allclose(actual['E0'], expected['E0'], rtol=0.01)
if 'NBlocks' in actual:
assert actual['NBlocks'] == expected['NBlocks']
def _check_block(actual_blocks, expected_blocks, atols={}, rtols={}):
""" Compare each values in two blocks.
atol: mapping from key to corresponding maximum absolute errors.
rtol: mapping from key to corresponding maximum relative errors.
"""
def _raise(id, key, actual, expected):
message = 'Large difference is found in block {} key {}.'.format(
id, key)
if isinstance(actual, np.ndarray):
ind_a = np.unravel_index(np.argmax(np.abs(actual - expected)),
actual.shape)
ind_r = np.unravel_index(
np.argmax(0.5 * np.abs(actual - expected) /
(np.abs(actual) + np.abs(expected))), actual.shape)
message += ' The largest abs difference is in {}'.format(ind_a)
message += ' with {} vs {} '.format(actual[ind_a], expected[ind_a])
message += ' The largest rel difference is in {}'.format(ind_r)
message += ' with {} vs {} '.format(actual[ind_r], expected[ind_r])
else:
message += ' {} vs {}'.format(actual, expected)
raise ValueError(message)
for i, (actual_bl, expected_bl) in enumerate(
zip(actual_blocks, expected_blocks)):
for key in actual_bl:
assert key in expected_bl
actual = actual_bl[key]
expected = expected_bl[key]
if isinstance(actual, np.ndarray) :
if actual.dtype.kind in 'ifc':
if not np.allclose(actual_bl[key], expected_bl[key],
atol=atols.get(key, 1.0e-8),
rtol=rtols.get(key, 1.0e-5)):
_raise(i, key, actual, expected)
else: # string array
if not (actual == expected).all():
raise ValueError('found in inconsistency in {}'.format(key))
elif isinstance(actual, list): # list of np.ndarray
for ac, ex in zip(actual_bl[key], expected_bl[key]):
if not np.allclose(ac, ex,
atol=atols.get(key, 1.0e-8),
rtol=rtols.get(key, 1.0e-5)):
_raise(i, key, actual, expected)
elif isinstance(actual, float):
if not np.allclose(actual_bl[key], expected_bl[key],
atol=atols.get(key, 1.0e-8),
rtol=rtols.get(key, 1.0e-5)):
_raise(i, key, actual, expected)
else:
if not actual == expected:
pass
def _sort_array(arrays, ref_arrays, *keys):
""" Sort arrays so that arrays[key] == ref_arrays[key] """
size = len(ref_arrays[keys[0]])
new_indexes = np.zeros(size, dtype=int)
for j in range(size):
for i in range(size):
if all(ref_arrays[k][i] == arrays[k][j] for k in keys):
new_indexes[i] = j
break
new_arrays = OrderedDict()
for k, v in arrays.items():
if isinstance(v, np.ndarray) and len(v) == size:
new_arrays[k] = v[new_indexes]
elif isinstance(v, list) and len(v) == size:
new_arrays[k] = []
for ind in new_indexes:
new_arrays[k].append(v[ind])
else:
new_arrays[k] = v
return new_arrays
def check_en(actual_file, expected_file):
actual_header, actual_blocks = read_lev(actual_file)
expected_header, expected_blocks = read_lev(expected_file)
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'ENERGY': 1.0e1},
rtols={'ENERGY': 0.01})
def check_ai(actual_file, expected_file):
actual_header, actual_blocks = read_ai(actual_file)
expected_header, expected_blocks = read_ai(expected_file)
# we need to sort arrays, because with openmp, the order may differ
actual_blocks = [_sort_array(ac, ex, 'bound_index', 'free_index')
for ac, ex in zip (actual_blocks, expected_blocks)]
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'rate': 1.0e1, 'Delta E': 0.5},
rtols={'rate': 0.05, 'DC strength': 0.05,
'AI rate': 0.05, 'Delta E': 0.01,
'EGRID': 1.0e-2})
def check_tr(actual_file, expected_file):
actual_header, actual_blocks = read_tr(actual_file)
expected_header, expected_blocks = read_tr(expected_file)
# we need to sort arrays, because with openmp, the order may differ
actual_blocks = [_sort_array(ac, ex, 'lower_index', 'upper_index')
for ac, ex in zip (actual_blocks, expected_blocks)]
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'Delta E': 1.0e0, 'gf': 1.0e-4, 'rate': 1.0e4,
'multipole': 1.0e-5},
rtols={'Delta E': 0.01, 'gf': 0.02, 'rate': 0.05,
'multipole': 0.05})
def check_ce(actual_file, expected_file):
actual_header, actual_blocks = read_ce(actual_file)
expected_header, expected_blocks = read_ce(expected_file)
# we need to sort arrays, because with openmp, the order may differ
actual_blocks = [_sort_array(ac, ex, 'lower_index', 'upper_index')
for ac, ex in zip (actual_blocks, expected_blocks)]
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'Delta E': 1.0e0, 'bethe': 1.0e0, 'born': 1.0,
'collision strength': 0.001, 'crosssection': 0.01,
'TEGRID': 1.0, 'EGRID': 1.0, 'USR': 1.0,
'TE0': 1.0, 'ratio collision strength': 0.1},
rtols={'Delta E': 0.01, 'bethe': 0.05, 'born': 0.05,
'collision strength': 0.05, 'crosssection': 0.1,
'TEGRID': 0.01, 'EGRID': 0.01, 'USR': 0.01,
'TE0': 0.01, 'ratio collision strenth': 0.05})
def check_ci(actual_file, expected_file):
actual_header, actual_blocks = read_ci(actual_file)
expected_header, expected_blocks = read_ci(expected_file)
# we need to sort arrays, because with openmp, the order may differ
actual_blocks = [_sort_array(ac, ex, 'bound_index', 'free_index')
for ac, ex in zip (actual_blocks, expected_blocks)]
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'Delta E': 1.0e0, 'bethe': 1.0e0, 'born': 1.0,
'collision strength': 0.001, 'crosssection': 0.01,
'TEGRID': 1.0, 'EGRID': 1.0, 'USR': 1.0,
'TE0': 1.0},
rtols={'Delta E': 0.01, 'bethe': 0.01, 'born': 0.01,
'collision strength': 0.02, 'crosssection': 0.1,
'TEGRID': 0.01, 'EGRID': 0.01, 'USR': 0.01,
'TE0': 0.01, 'parameters': 0.01})
def check_rr(actual_file, expected_file):
actual_header, actual_blocks = read_rr(actual_file)
expected_header, expected_blocks = read_rr(expected_file)
# we need to sort arrays, because with openmp, the order may differ
actual_blocks = [_sort_array(ac, ex, 'bound_index', 'free_index')
for ac, ex in zip (actual_blocks, expected_blocks)]
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'Delta E': 1.0e0, 'bethe': 1.0e0, 'born': 1.0,
'TEGRID': 1.0, 'EGRID': 1.0, 'USR': 1.0,
'TE0': 1.0},
rtols={'Delta E': 0.01, 'bethe': 0.01, 'born': 0.01,
'RR crosssection': 0.01, 'PI crosssection': 0.01,
'TEGRID': 0.01, 'EGRID': 0.01, 'USR': 0.01,
'TE0': 0.01, 'parameters': 0.01, 'gf': 0.01})
def check_sp(actual_file, expected_file):
actual_header, actual_blocks = read_sp(actual_file)
expected_header, expected_blocks = read_sp(expected_file)
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'Delta E': 1.0e0,
'emissivity': 1.0e-10,
'population': 1.0e-30},
rtols={'Delta E': 0.01,
'emissivity': 0.05,
'population': 0.05})
def check_rt(actual_file, expected_file):
actual_header, actual_blocks = read_rt(actual_file)
expected_header, expected_blocks = read_rt(expected_file)
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks)
def check_ham(actual_file, expected_file):
actual_header, actual_blocks = read_ham(actual_file)
expected_header, expected_blocks = read_ham(expected_file)
_check_header(actual_header, expected_header)
_check_block(actual_blocks, expected_blocks,
atols={'value': 1e-5},
rtols={'value': 1e-3})
def check(actual_file, expected_file):
if actual_file[-3:] in ['.en', 'lev']:
check_en(actual_file, expected_file)
elif actual_file[-3:] == '.ai':
check_ai(actual_file, expected_file)
elif actual_file[-3:] == '.tr':
check_tr(actual_file, expected_file)
elif actual_file[-3:] in ['.ce', 'ceM']:
check_ce(actual_file, expected_file)
elif actual_file[-3:] == '.ci':
check_ci(actual_file, expected_file)
elif actual_file[-3:] == '.rr':
check_rr(actual_file, expected_file)
elif actual_file[-3:] == '.sp':
check_sp(actual_file, expected_file)
elif actual_file[-3:] == '.rt':
check_rt(actual_file, expected_file)
elif actual_file[-4:] == '.ham':
check_ham(actual_file, expected_file)
else:
raise TypeError('Unknown file extension: {}'.format(actual_file))
if __name__ == '__main__':
args = sys.argv
if len(args) != 3:
raise ValueError('Usage: python check.py file1 file2')
print(args[1])
check(args[1], args[2])