Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: compare the relative difference for Onsager.txt #4378

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions tests/integrate/tools/CompareFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import os,sys

usage = '''
python CompareFile.py file1 file2 [accuracy] [-abs 0]
python CompareFile.py file1 file2 [accuracy] [-abs 0] [-com_type 1]

accuracy - the accuracy of two float value.
default value is 8, which means
pass when the difference of two
value is smaller than 1e-8.
-abs - if compare the absolute value, default is False
-com_type - 0: assure the relative difference of two float value is smaller than accuracy
- 1: assure the absolute difference of two float value is smaller than accuracy
- Default is 1

This script is used to compare whether two files are
same.
Expand All @@ -21,23 +24,35 @@
'''

def ReadParam():
if len(sys.argv) not in [3,4,5,6]:
if len(sys.argv) not in [3,4,5,6,7,8]:
print(usage)
sys.exit(1)
file1 = sys.argv[1]
file2 = sys.argv[2]
accur = 8
absolute = False
if len(sys.argv) == 4:
accur = int(sys.argv[3])
elif len(sys.argv) == 5 and sys.argv[3] == '-abs':
absolute = bool(sys.argv[4])
elif len(sys.argv) == 6:
accur = int(sys.argv[3])
absolute = bool(sys.argv[5])

useAbsoluteVal = False
compareAbsoluteDiff = True
argi = 3
while argi < len(sys.argv):
if sys.argv[argi] == '-abs':
argi += 1
useAbsoluteVal = bool(int(sys.argv[argi]))
elif sys.argv[argi] == '-com_type':
argi += 1
compareAbsoluteDiff = bool(int(sys.argv[argi]))
else:
accur = int(sys.argv[argi])
argi += 1
epsilon = 0.1**accur
return file1,file2,epsilon,absolute
return file1,file2,epsilon,useAbsoluteVal,compareAbsoluteDiff

def is_two_data_diff(data1,data2,epsilon,compareAbsoluteDiff):
if compareAbsoluteDiff:
if abs(data1-data2) > epsilon: return True
else:
if abs(data1) < 1e-8 and abs(data2) < 1e-8: return False
if abs(data1-data2)/max(abs(data1),abs(data2)) > epsilon: return True
return False

def ReadFile(file1,lines):
if os.path.isfile(file1):
Expand Down Expand Up @@ -78,7 +93,7 @@ def ExitError(iline,line1,line2,jnumber=-1):


if __name__ == "__main__":
file1,file2,epsilon,absolute = ReadParam()
file1,file2,epsilon,useAbsoluteVal,compareAbsoluteDiff = ReadParam()
lines1 = []
lines2 = []
ReadFile(file1,lines1)
Expand All @@ -95,19 +110,19 @@ def ExitError(iline,line1,line2,jnumber=-1):
x1 = IsComplex(sline1[j])
x2 = IsComplex(sline2[j])
if x1 and x2:
if absolute:
if abs((x1[0]**2+x1[1]**2)**0.5 - (x2[0]**2+x2[1]**2)**0.5) > epsilon:
if useAbsoluteVal:
if is_two_data_diff((x1[0]**2+x1[1]**2)**0.5, (x2[0]**2+x2[1]**2)**0.5, epsilon, compareAbsoluteDiff):
ExitError(i,sline1[j],sline2[j],j)
else:
if abs(x1[0] - x2[0]) > epsilon:
if is_two_data_diff(x1[0], x2[0], epsilon, compareAbsoluteDiff):
ExitError(i,sline1[j],sline2[j],j)
if abs(x1[1] - x2[1]) > epsilon:
if is_two_data_diff(x1[1], x2[1], epsilon, compareAbsoluteDiff):
ExitError(i,sline1[j],sline2[j],j)
elif IsFloat(sline1[j]) and IsFloat(sline2[j]):
if absolute:
if abs(abs(float(sline1[j])) - abs(float(sline2[j]))) > epsilon:
if useAbsoluteVal:
if is_two_data_diff(abs(float(sline1[j])), abs(float(sline2[j])), epsilon, compareAbsoluteDiff):
ExitError(i,sline1[j],sline2[j],j)
else:
if abs(float(sline1[j]) - float(sline2[j])) > epsilon:
if is_two_data_diff(float(sline1[j]), float(sline2[j]), epsilon, compareAbsoluteDiff):
ExitError(i,sline1[j],sline2[j],j)
elif sline1[j] != sline2[j]: ExitError(i,sline1[j],sline2[j],j)
2 changes: 1 addition & 1 deletion tests/integrate/tools/catch_properties.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fi
if ! test -z "$has_cond" && [ $has_cond == 1 ]; then
onref=refOnsager.txt
oncal=Onsager.txt
python3 ../tools/CompareFile.py $onref $oncal 2
python3 ../tools/CompareFile.py $onref $oncal 3 -com_type 0
echo "CompareH_Failed $?" >>$1
rm -f je-je.txt Chebycoef
fi
Expand Down
Loading