Skip to content

Commit

Permalink
Improve test error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrp089 committed Jan 25, 2024
1 parent 6b841e8 commit 2de6136
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
42 changes: 32 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ def run_with_reference(

# check results
for f in fields:
# extract field
if f not in res.point_data.keys():
raise ValueError("Field " + f + " not in simulation result")
a = res.point_data[f]

if f not in ref.point_data.keys():
raise ValueError("Field " + f + " not in reference result")
b = ref.point_data[f]

# truncate last dimension if solution is 2D but reference is 3D
Expand All @@ -99,19 +105,35 @@ def run_with_reference(
assert not np.any(b[:, 2])
b = b[:, :2]

# compare solution to reference
# pick tolerance for current field
if f in RTOL:
rtol = RTOL[f]
else:
rtol = DEFAULT_TOL

# throw error if not all results are within relative tolerance
close = np.isclose(a, b, rtol=rtol)
if np.all(close):
return
else:
msg = "Test failed!"
msg += "\nResults in field " + f + " differ by more than rtol=" + str(rtol)
msg += (
" in " + str(np.sum(close)) + " out of " + str(close.size) + " results."
)
msg += " Max. abs. difference is " + "{:.1e}".format(np.max(np.abs(a - b)))
if not np.all(close):
# portion of individual results that are above the tolerance
wrong = 1 - np.sum(close) / close.size

# location of absolute maximum difference
a_fl = a.flatten()
b_fl = b.flatten()
i_max = np.argmax(np.abs(a_fl - b_fl))

# maximum absolute difference
max_abs = np.abs(a_fl[i_max] - a_fl[i_max])

# relative difference at same location
max_rel = np.abs(a_fl[i_max] / a_fl[i_max] - 1)

# throw error message for pytest
msg = "Test failed in field " + f + "."
msg += " Results differ by more than rtol=" + str(rtol)
msg += " in {:.0%}".format(wrong)
msg += " out of results."
msg += " Max. abs. differences is"
msg += " {:.1e}".format(max_abs)
msg += " (rel. {:.1e}".format(max_rel) + ")"
raise ValueError(msg)
18 changes: 4 additions & 14 deletions tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,7 @@
"Jacobian",
"Stress",
"Strain",
"Caucy_stress",
"Def_grad",
"VonMises_stress",
]

# Fields to test
fields = [
"Displacement",
"Velocity",
"Jacobian",
"Stress",
"Strain",
"Caucy_stress",
"Cauchy_stress",
"Def_grad",
"VonMises_stress",
]
Expand All @@ -32,10 +20,12 @@ def test_LV_Guccione_passive(n_proc):
test_folder = "LV_Guccione_passive"
run_with_reference(base_folder, test_folder, fields, n_proc)


def test_LV_Holzapfel_passive(n_proc):
test_folder = "LV_Holzapfel_passive"
run_with_reference(base_folder, test_folder, fields, n_proc)



def test_block_compression(n_proc):
test_folder = "block_compression"
run_with_reference(base_folder, test_folder, fields, n_proc)
Expand Down

0 comments on commit 2de6136

Please sign in to comment.