This repository was archived by the owner on Jan 4, 2024. It is now read-only.
forked from athokk/MFC-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
606 lines (445 loc) · 22.6 KB
/
test.py
File metadata and controls
606 lines (445 loc) · 22.6 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/env python3
import os
import re
import copy
import binascii
import subprocess
import dataclasses
import rich
import rich.progress
from pathlib import Path
from collections import ChainMap
import internal.common as common
@dataclasses.dataclass
class Case:
name: str
parameters: list
def __init__(self, data: dict) -> None:
self.name = data.get("name")
self.parameters = {}
for p in list(data.get("parameters").items()):
self.parameters[p[0]] = p[1]
def get_keys(self):
keys = []
for param in self.parameters:
keys.append(param.name)
return keys
def has_parameter(self, key: str):
return key in self.get_keys()
def __getitem__(self, key: str) -> str:
if key not in self.parameters:
raise common.MFCException(f"Case: Parameter {key} does not exist.")
return self.parameters[key]
def __setitem__(self, key: str, val: str):
self.parameters[key] = val
def create_case_dict_str(self) -> str:
result: str = "{\n"
for key,val in self.parameters.items():
result = f'{result}\t"{key}": "{val}",\n'
return result + "}"
@dataclasses.dataclass
class Test:
case: Case
def __init__(self, data: dict) -> None:
self.case = data.get("case", {})
Tend = 0.25
Nt = 50
mydt = 0.0005
BASE_CASE = Case({
"name": "Base Case",
"parameters": {
'case_dir' : '\'.\'',
'run_time_info' : 'F',
'nodes' : 1,
'ppn' : 1,
'queue' : 'normal',
'walltime' : '24:00:00',
'mail_list' : '',
'm' : 0,
'n' : 0,
'p' : 0,
'dt' : mydt,
't_step_start' : 0,
't_step_stop' : int(Nt+1),
't_step_save' : int(Nt),
'num_patches' : 3,
'model_eqns' : 2,
'alt_soundspeed' : 'F',
'num_fluids' : 1,
'adv_alphan' : 'T',
'mpp_lim' : 'F',
'mixture_err' : 'F',
'time_stepper' : 3,
'weno_vars' : 2,
'weno_order' : 5,
'weno_eps' : 1.E-16,
'mapped_weno' : 'F',
'null_weights' : 'F',
'mp_weno' : 'F',
'riemann_solver' : 2,
'wave_speeds' : 1,
'avg_state' : 2,
'format' : 1,
'precision' : 2,
'prim_vars_wrt' :'T',
'parallel_io' :'F',
'patch_icpp(1)%pres' : 1.0,
'patch_icpp(1)%alpha_rho(1)' : 1.E+00,
'patch_icpp(1)%alpha(1)' : 1.,
'patch_icpp(2)%pres' : 0.5,
'patch_icpp(2)%alpha_rho(1)' : 0.5,
'patch_icpp(2)%alpha(1)' : 1.,
'patch_icpp(3)%pres' : 0.1,
'patch_icpp(3)%alpha_rho(1)' : 0.125,
'patch_icpp(3)%alpha(1)' : 1.,
'fluid_pp(1)%gamma' : 1.E+00/(1.4-1.E+00),
'fluid_pp(1)%pi_inf' : 0.0,
'bubbles' : 'F',
'Ca' : 0.9769178386380458,
'Web' : 13.927835051546392,
'Re_inv' : 0.009954269975623245,
'pref' : 101325.0,
'rhoref' : 1000.0,
'bubble_model' : 3,
'polytropic' : 'T',
'polydisperse' : 'F',
'thermal' : 3,
'R0ref' : 1e-05,
'patch_icpp(1)%r0' : 1,
'patch_icpp(1)%v0' : 0,
'patch_icpp(2)%r0' : 1,
'patch_icpp(2)%v0' : 0,
'patch_icpp(3)%r0' : 1,
'patch_icpp(3)%v0' : 0,
'qbmm' : 'F',
'dist_type' : 2,
'poly_sigma' : 0.3,
'R0_type' : 1,
'nnode' : 4,
'sigR' : 0.1,
'sigV' : 0.1,
'rhoRV' : 0.0,
'Monopole' : 'F',
'num_mono' : 1,
'Mono(1)%loc(1)' : 0.5,
'Mono(1)%mag' : 1.0,
'Mono(1)%length' : 0.25,
'Mono(1)%dir' : 1.0,
'Mono(1)%npulse' : 1,
'Mono(1)%pulse' : 1,
}
})
class TestCaseConfiguration:
parameters: dict = {}
traceback: str = ""
def __init__(self, parameters: list, traceback: list) -> None:
self.parameters = {}
for e in parameters:
self.parameters.update(e)
self.traceback = ' -> '.join(traceback)
class MFCTest:
def __init__(self, bootstrap):
self.bootstrap = bootstrap
# Aliases
self.console = self.bootstrap.console
self.args = self.bootstrap.args
def get_test_params(self):
tests = []
traceback = []
parameters = []
for dimInfo in [ (["x"], {'m': 299, 'n': 0, 'p': 0}, {"geometry": 1}),
(["x", "y"], {'m': 49, 'n': 39, 'p': 0}, {"geometry": 3}),
(["x", "y", "z"], {'m': 24, 'n': 24, 'p': 24}, {"geometry": 9}) ]:
dimParams = {**dimInfo[1]}
for dimCmp in dimInfo[0]:
dimParams[f"{dimCmp}_domain%beg"] = 0.E+00
dimParams[f"{dimCmp}_domain%end"] = 1.E+00
for patchID in range(1, 3+1):
dimParams[f"patch_icpp({patchID})%geometry"] = dimInfo[2].get("geometry")
if "z" in dimInfo[0]:
dimParams[f"patch_icpp({1})%z_centroid"] = 0.05
dimParams[f"patch_icpp({1})%length_z"] = 0.1
dimParams[f"patch_icpp({2})%z_centroid"] = 0.45
dimParams[f"patch_icpp({2})%length_z"] = 0.7
dimParams[f"patch_icpp({3})%z_centroid"] = 0.9
dimParams[f"patch_icpp({3})%length_z"] = 0.2
dimParams[f"patch_icpp({patchID})%y_centroid"] = 0.5
dimParams[f"patch_icpp({patchID})%length_y"] = 1
dimParams[f"patch_icpp({patchID})%x_centroid"] = 0.5
dimParams[f"patch_icpp({patchID})%length_x"] = 1
elif "y" in dimInfo[0]:
dimParams[f"patch_icpp({1})%y_centroid"] = 0.05
dimParams[f"patch_icpp({1})%length_y"] = 0.1
dimParams[f"patch_icpp({2})%y_centroid"] = 0.45
dimParams[f"patch_icpp({2})%length_y"] = 0.7
dimParams[f"patch_icpp({3})%y_centroid"] = 0.9
dimParams[f"patch_icpp({3})%length_y"] = 0.2
dimParams[f"patch_icpp({patchID})%x_centroid"] = 0.5
dimParams[f"patch_icpp({patchID})%length_x"] = 1
else:
dimParams[f"patch_icpp({1})%x_centroid"] = 0.05
dimParams[f"patch_icpp({1})%length_x"] = 0.1
dimParams[f"patch_icpp({2})%x_centroid"] = 0.45
dimParams[f"patch_icpp({2})%length_x"] = 0.7
dimParams[f"patch_icpp({3})%x_centroid"] = 0.9
dimParams[f"patch_icpp({3})%length_x"] = 0.2
if "x" in dimInfo[0]:
dimParams[f"patch_icpp({patchID})%vel(1)"] = 0.0
if "y" in dimInfo[0]:
dimParams[f"patch_icpp({patchID})%vel(2)"] = 0.0
if "z" in dimInfo[0]:
dimParams[f"patch_icpp({patchID})%vel(3)"] = 0.0
traceback.append (f"{len(dimInfo[0])}D (m={dimInfo[1].get('m')},n={dimInfo[1].get('n')},p={dimInfo[1].get('p')})")
parameters.append(dimParams)
for bc in [ -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12]:
params = {}
for dimCmp in dimInfo[0]:
params = {**params, **{f'bc_{dimCmp}%beg': bc, f'bc_{dimCmp}%end': bc}}
trace = f"bc={bc}"
tests.append(TestCaseConfiguration(parameters + [params], traceback + [trace]))
if bc == -3:
parameters.append(params)
traceback.append(trace)
for weno_order in [3, 5]:
traceback.append (f"weno_order={weno_order}")
parameters.append({'weno_order': weno_order})
for mapped_weno, mp_weno in [('F', 'F'), ('T', 'F'), ('F', 'T')]:
traceback.append (f"(mapped_weno={mapped_weno},mp_weno={mp_weno})")
parameters.append({'mapped_weno': mapped_weno, 'mp_weno': mp_weno})
if not (mp_weno == 'T' and weno_order != 5):
tests.append(TestCaseConfiguration(parameters, traceback))
traceback.pop()
parameters.pop()
traceback.pop()
parameters.pop()
for num_fluids in [1, 2]:
traceback.append(f"num_fluids={num_fluids}")
parameters.append({"num_fluids": num_fluids})
if num_fluids == 2:
parameters.append({'fluid_pp(2)%gamma': 2.5, 'fluid_pp(2)%pi_inf': 0.0,'patch_icpp(1)%alpha_rho(1)': 0.81, 'patch_icpp(1)%alpha(1)': 0.9, 'patch_icpp(1)%alpha_rho(2)': 0.19, 'patch_icpp(1)%alpha(2)': 0.1, 'patch_icpp(2)%alpha_rho(1)': 0.25, 'patch_icpp(2)%alpha(1)': 0.5, 'patch_icpp(2)%alpha_rho(2)': 0.25, 'patch_icpp(2)%alpha(2)': 0.5, 'patch_icpp(3)%alpha_rho(1)': 0.08, 'patch_icpp(3)%alpha(1)': 0.2, 'patch_icpp(3)%alpha_rho(2)': 0.0225, 'patch_icpp(3)%alpha(2)': 0.8,})
for riemann_solver in [1, 2]:
traceback.append(f"riemann_solver={riemann_solver}")
parameters.append({'riemann_solver': riemann_solver})
tests.append(TestCaseConfiguration(parameters + [{'mixture_err': 'T'}], traceback + ['mixture_err=T']))
tests.append(TestCaseConfiguration(parameters + [{'avg_state': '1'}], traceback + ['avg_state=1']))
tests.append(TestCaseConfiguration(parameters + [{'wave_speeds': '2'}], traceback + ['wave_speeds=2']))
if num_fluids == 2:
if riemann_solver == 2:
tests.append(TestCaseConfiguration(parameters + [{'alt_soundspeed': 'T'}], traceback + ['alt_soundspeed=T']))
tests.append(TestCaseConfiguration(parameters + [{'avg_state': 1}], traceback + ['avg_state=1']))
tests.append(TestCaseConfiguration(parameters + [{'wave_speeds': 2}], traceback + ['wave_speeds=2']))
tests.append(TestCaseConfiguration(parameters + [{'mpp_lim': 'T'}], traceback + ['mpp_lim=T']))
traceback.pop()
parameters.pop()
if num_fluids == 2:
parameters.pop()
traceback.pop()
parameters.pop()
if len(dimInfo[0]) == 3:
tests.append(TestCaseConfiguration(parameters + [{'ppn': 2, 'm': 29, 'n': 29, 'p': 49}], traceback + [f'ppn=2,m=29,n=29,p=49']))
else:
tests.append(TestCaseConfiguration(parameters + [{'ppn': 2}], traceback + [f'ppn=2']))
if len(dimInfo[0]) == 1:
parameters.append({'dt': 1e-07})
elif len(dimInfo[0]) == 2:
parameters.append({'dt': 1e-06})
else:
parameters.append({'dt':1e-06})
if len(dimInfo[0]) > 0:
traceback.append(f"bubbles={'T'}")
parameters.append({"bubbles": 'T'})
parameters.append({'nb' : 3, 'fluid_pp(1)%gamma' : 0.16, 'fluid_pp(1)%pi_inf': 3515.0, 'fluid_pp(2)%gamma': 2.5, 'fluid_pp(2)%pi_inf': 0.0, 'fluid_pp(1)%mul0' : 0.001002, 'fluid_pp(1)%ss' : 0.07275,'fluid_pp(1)%pv' : 2338.8,'fluid_pp(1)%gamma_v' : 1.33,'fluid_pp(1)%M_v' : 18.02,'fluid_pp(1)%mu_v' : 8.816e-06,'fluid_pp(1)%k_v' : 0.019426,'fluid_pp(2)%gamma_v' : 1.4,'fluid_pp(2)%M_v' : 28.97,'fluid_pp(2)%mu_v' : 1.8e-05, 'fluid_pp(2)%k_v' : 0.02556, 'patch_icpp(1)%alpha_rho(1)': 0.999999999999, 'patch_icpp(1)%alpha(1)': 1e-12, 'patch_icpp(2)%alpha_rho(1)': 0.96, 'patch_icpp(2)%alpha(1)': 4e-02, 'patch_icpp(3)%alpha_rho(1)': 0.999999999999, 'patch_icpp(3)%alpha(1)': 1e-12, 'patch_icpp(1)%pres': 1.0, 'patch_icpp(2)%pres': 1.0, 'patch_icpp(3)%pres': 1.0 })
traceback.append(f"Monopole={'T'}")
parameters.append({"Monopole": 'T'})
if len(dimInfo[0]) >= 2:
parameters.append({'Mono(1)%loc(2)': 0.5})
if len(dimInfo[0]) >= 3:
parameters.append({'Mono(1)%loc(3)': 0.5, 'Mono(1)%support': 3})
for polytropic in ['T', 'F']:
for bubble_model in [3, 2]:
traceback.append(f"polytropic={polytropic}")
parameters.append({'polytropic' : polytropic})
traceback.append(f"bubble_model={bubble_model}")
parameters.append({'bubble_model' : bubble_model})
if not (polytropic == 'F' and bubble_model == 3):
tests.append(TestCaseConfiguration(parameters, traceback))
traceback.pop()
parameters.pop()
traceback.pop()
parameters.pop()
parameters.append({'polytropic' : 'T'})
parameters.append({'bubble_model' : 2})
parameters.append({'nb':1})
traceback.append(f"nb={'1'}")
tests.append(TestCaseConfiguration(parameters, traceback))
traceback.pop()
parameters.pop()
traceback.append(f"qbmm={'T'}")
parameters.append({'qbmm': 'T'})
tests.append(TestCaseConfiguration(parameters, traceback))
parameters.append({'bubble_model' : 3})
tests.append(TestCaseConfiguration(parameters, traceback))
traceback.pop()
for i in range(4):
parameters.pop()
if len(dimInfo[0]) >= 2:
parameters.pop()
if len(dimInfo[0]) >= 3:
parameters.pop()
parameters.pop()
traceback.pop()
parameters.pop()
parameters.pop()
traceback.pop()
parameters.pop()
for i in range(2):
traceback.pop()
parameters.pop()
return tests
def filter_tests(self, tests: list):
if len(self.args["only"]) > 0:
for i, test in enumerate(tests[:]):
doKeep = False
for o in self.args["only"]:
if str(o).isnumeric():
testID = i+1
if testID == int(o):
doKeep = True
break
testHash = self.get_case_dir_name(test.parameters)
if str(o) == testHash:
doKeep = True
break
if not doKeep:
tests.remove(test)
return tests
def test(self):
if self.args["generate"]:
common.delete_directory_recursive(common.MFC_TESTDIR)
common.create_directory(common.MFC_TESTDIR)
for target in ["pre_process", "simulation"]:
if not self.bootstrap.is_build_satisfied(target):
self.console.print(f"> {target} needs (re)building...")
self.bootstrap.build_target(f"{target}", "> ")
tests = self.filter_tests(self.get_test_params())
for i, test in enumerate(rich.progress.track(tests, "Testing [bold blue]mfc[/bold blue]...", console=self.console, )):
test: TestCaseConfiguration
testID = i+1
if len(self.args["only"]):
testID = self.args["only"][i]
self.handle_case(testID, test)
self.console.print(f"> Tested [bold green]✓[/bold green]")
def get_case_dir_name(self, mods: dict):
return hex(binascii.crc32(str(mods.items()).encode()))[2:]
def get_case_dir(self, mods: dict):
return f"{common.MFC_TESTDIR}/{self.get_case_dir_name(mods)}"
def get_case_from_mods(self, mods: dict):
case = copy.deepcopy(BASE_CASE)
for key, val in mods.items():
case[key] = val
return case
def create_case_dir(self, mods: dict):
case = self.get_case_from_mods(mods)
case_dir = self.get_case_dir(mods)
content = f"""\
#!/usr/bin/env python3
from pathlib import Path
from sys import path
path.insert(0, f"{{Path(__file__).parent.resolve()}}/../../src/master_scripts")
# Let Python find MFC's module
from m_python_proxy import f_execute_mfc_component
case_dict = {case.create_case_dict_str()}
f_execute_mfc_component('pre_process', case_dict, '..', 'serial')
f_execute_mfc_component('simulation', case_dict, '..', 'serial')
"""
common.create_directory(case_dir)
common.file_write(f"{case_dir}/input.py", content)
def golden_file_compare_match(self, truth: str, candidate: str, tol):
if truth.count('\n') != candidate.count('\n'):
return (False, "Line count didn't match.")
if "NaN" in truth:
return (False, "NaN in golden file")
if "NaN" in candidate:
return (False, "NaN in packed file")
for candidate_line in candidate.splitlines():
if candidate_line == "":
continue
file_subpath: str = candidate_line.split(' ')[0]
line_trusted: str = ""
for l in truth.splitlines():
if l.startswith(file_subpath):
line_trusted = l
break
if len(line_trusted) == 0:
continue
numbers_cand = [ float(x) for x in candidate_line.strip().split(' ')[1:] ]
numbers_trust = [ float(x) for x in line_trusted.strip().split(' ')[1:] ]
# Different amount of spaces, means that there are more entires in one than in the other
if len(numbers_cand) != len(numbers_trust):
return (False, "Variable count didn't match.")
# check values one by one
for i in range(len(numbers_cand)):
abs_delta = abs(numbers_cand[i]-numbers_trust[i])
rel_diff = abs(abs_delta/numbers_trust[i]) if numbers_trust[i] != 0 else 0
if (abs_delta > tol and rel_diff > tol):
percent_diff = rel_diff*100
return (False, f"Error margin is too high for the value #{i+1} in {file_subpath}: ~{round(percent_diff, 5)}% (~{round(abs_delta, 5)}).")
# Both tests gave the same results within an acceptable tolerance
return (True, "")
def get_test_summary(self, mods: dict):
return "".join([f"{str(x[0]).split('_')[0][:4]}-{str(x[1])[:4]}_" for x in mods.items()])[:-1]
def handle_case(self, testID, test: TestCaseConfiguration):
self.create_case_dir(test.parameters)
if('qbmm' in test.parameters):
tol = 1e-7
elif('bubbles' in test.parameters):
tol = 1e-10
else:
tol = 1e-12
def on_test_errror(msg: str = "", term_out: str = ""):
common.clear_line()
self.console.print(f"> Test #{testID}: Failed! [bold green]✓[/bold green]")
if msg != "":
self.console.print(f"> {msg}")
common.file_write(f"{common.MFC_TESTDIR}/failed_test.txt", f"""\
(1/3) Test #{testID}:
- Test ID: {testID}
- Summary: {test.traceback}
- Location: {self.get_case_dir(test.parameters)}
- Error: {msg}
- When: {common.get_datetime_str()}
(2/3) Test case:
{self.get_case_from_mods(test.parameters).create_case_dict_str()}
(3/3) Terminal output:
{term_out}
""")
self.console.print(f"> Please read {common.MFC_TESTDIR}/failed_test.txt for more information.")
raise common.MFCException("Testing failed (view above).")
cmd = subprocess.run(f"cd '{self.get_case_dir(test.parameters)}' && python3 input.py 2>&1",
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, shell=True)
common.file_write(f"{self.get_case_dir(test.parameters)}/out.txt", cmd.stdout)
if cmd.returncode != 0:
on_test_errror("MFC Execution Failed.", cmd.stdout)
pack = self.pack_case_output(test.parameters)
common.file_write(f"{self.get_case_dir(test.parameters)}/pack.txt", pack)
golden_filepath = f"{self.get_case_dir(test.parameters)}/golden.txt"
if self.args["generate"]:
common.delete_file(golden_filepath)
common.file_write(golden_filepath, pack)
if not os.path.isfile(golden_filepath):
common.clear_line()
on_test_errror("Golden file doesn't exist! To generate golden files, use the '-g' flag.", cmd.stdout)
golden_file_content = common.file_read(golden_filepath)
bSuccess, errorMsg = self.golden_file_compare_match(golden_file_content, pack, tol)
if not bSuccess:
on_test_errror(errorMsg, cmd.stdout)
def pack_case_output(self, params: dict):
result: str = ""
case_dir = self.get_case_dir(params)
D_dir = f"{case_dir}/D/"
for filepath in list(Path(D_dir).rglob("*.dat")):
file_content = common.file_read(filepath)
short_filepath = str(filepath).replace(f'{case_dir}/', '')
result += f"{short_filepath} " + re.sub(r' +', ' ', file_content.replace('\n', ' ')).strip() + '\n'
return result