Debian Codegraph @

packages/muelu/research/regionMG/test/structured/compare_residual_history.py

raw
1 import os
2 import sys
3
4 def check_residual(goldResidual, testResidual, maxDiff, relDiff=1e-7, precisionThreshold=1e-7):
5 res=False
6 if(abs(goldResidual) < precisionThreshold):
7 print("Using absolute error")
8 # Check residual using absolute error
9 # as values are to small to check reliably
10 # with relative error due to machine precision
11 if(abs(goldResidual - testResidual) < maxDiff):
12 res=True
13 else:
14 print("Using relative error")
15 if(abs(goldResidual - testResidual) / abs(goldResidual) < relDiff):
16 res=True
17
18 return res
19
20 numIterPassed = False
21 goldConvergenceHistoryPassed = True
22
23 goldFileName = sys.argv[1]
24 testLogFileName = sys.argv[2]
25 maxDiff = float(sys.argv[3])
26
27 ## Read gold file and extract pure convergence history
28 goldFile = open(goldFileName, 'rt')
29 goldFileContent = goldFile.readlines()
30 goldConvergenceHistory = []
31 for line in goldFileContent:
32 if not line.startswith('#'):
33 goldConvergenceHistory.append(line)
34 goldFile.close()
35
36 ## Read current log file and extract pure convergence history
37 testLogFile = open(testLogFileName, 'rt')
38 testLogFileContent = testLogFile.readlines()
39 testLogConvergenceHistory = []
40 for line in testLogFileContent:
41 if not line.startswith('#'):
42 testLogConvergenceHistory.append(line)
43 testLogFile.close()
44
45 # Check for same number of iterations
46 if len(goldConvergenceHistory) == len(testLogConvergenceHistory):
47 print('Number of iterations: OK')
48 numIterPassed = True
49 else:
50 print('Number of iterations: WRONG -- deviation from value in gold file.')
51
52 # Check residual in each iteration
53 if numIterPassed:
54 for iteration in range(len(goldConvergenceHistory)):
55 goldResidual = float((goldConvergenceHistory[iteration].split())[1])
56 testResidual = float((testLogConvergenceHistory[iteration].split())[1])
57
58 if check_residual(goldResidual, testResidual, maxDiff):
59 print(('Residual in iteration {}: OK').format(str(iteration)))
60 else:
61 print(('Residual in iteration {}: WRONG -- deviation from value in gold file.').format(str(iteration)))
62 goldConvergenceHistoryPassed = False
63 break
64
65 # Print final result to be processed by Tribits
66 if numIterPassed and goldConvergenceHistoryPassed:
67 print('End Result: TEST PASSED')
68 else:
69 print('End Result: TEST FAILED')