forked from SAP-archive/fedem-solvers
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_utils.py
More file actions
72 lines (62 loc) · 1.66 KB
/
test_utils.py
File metadata and controls
72 lines (62 loc) · 1.66 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
# SPDX-FileCopyrightText: 2023 SAP SE
#
# SPDX-License-Identifier: Apache-2.0
#
# This file is part of FEDEM - https://openfedem.org
"""
Utility functions to facilitate regression testing of the Python wrapper.
"""
import argparse as ap
import numpy as np
def diff_values(v1, v2, eps):
"""
Compares two float values with some tolerance.
"""
diff = abs(v1 - v2)
refv = abs(v1 + v2) * eps * 0.5
if diff > refv and diff > eps:
return diff
return 0.0
# pylint: disable=invalid-name, consider-using-enumerate
def compare_lists(t, l1, l2, eps=1.0e-12):
"""
Compares two lists of real values with some tolerance.
"""
ndiff = 0
for i in range(len(l1)):
diff = diff_values(l1[i], l2[i], eps)
if diff > 0.0:
print(
"t =",
t,
": Value",
i + 1,
"does not match",
l1[i],
l2[i],
"diff =",
diff,
)
ndiff += 1
return ndiff
def read_reference_data(file_name):
"""
Reads time history reference data from a file.
"""
refsd = []
rfile = open(file_name, "r")
for line in rfile:
if line[0:5] == "#DESC":
count = 0
for head in line.split():
count += 1
if count == 1:
print("#Reference quantities:")
else:
print(count, head)
elif line[0] == "#":
print(line.strip()) # comment line
else:
refsd.append([float(x) for x in line.split()])
rfile.close()
return refsd