From ff4d365c6cd040974069bbabda9b45678344c39b Mon Sep 17 00:00:00 2001 From: Jonathan Maddock <78556175+jonmaddock@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:20:52 +0100 Subject: [PATCH 1/2] Use full double precision in MFILE output --- source/fortran/output.f90 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/fortran/output.f90 b/source/fortran/output.f90 index f97e6b1d9c..47313ab38e 100755 --- a/source/fortran/output.f90 +++ b/source/fortran/output.f90 @@ -485,7 +485,11 @@ subroutine ovarre(file,descr,varnam,value,output_flag) call underscore(dum20) write(mfile,10) dum72, dum20, value, flag -10 format(1x,a,t75,a,t110,1pe11.4," ",a,t10) + ! MFILE.DAT format + ! Machine epsilon for double ~2.22e-16, hence require 17 sig figs in significand + ! for full precision of a double float +10 format(1x,a,t75,a,t110,ES23.16e2," ",a,t10) + ! OUT.DAT format 20 format(1x,a,t75,a,t100,1pe10.3, t112, a) end subroutine ovarre From 7996f5558357704a2645468ca0e66b74c3a16f78 Mon Sep 17 00:00:00 2001 From: Jonathan Maddock <78556175+jonmaddock@users.noreply.github.com> Date: Wed, 24 Apr 2024 16:35:38 +0100 Subject: [PATCH 2/2] Avoid 0% tolerance regression tests Causes failures due to floating-point discrepancies between local and CI. Use pytest.approx for sensible relative and absolute difference assertions. --- tests/regression/test_process_input_files.py | 54 +++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/tests/regression/test_process_input_files.py b/tests/regression/test_process_input_files.py index a454014a6b..6104c17799 100644 --- a/tests/regression/test_process_input_files.py +++ b/tests/regression/test_process_input_files.py @@ -167,11 +167,8 @@ def mfile_value_changes( if re.match(exclusions, key) is not None: continue - # no need to carry on if the variables are exactly the same - if (ref_value := ref.data[key].get_scan(-1)) == ( - new_value := new.data[key].get_scan(-1) - ): - continue + ref_value = ref.data[key].get_scan(-1) + new_value = new.data[key].get_scan(-1) try: ref_value = float(ref_value) @@ -180,28 +177,37 @@ def mfile_value_changes( # only compare float-able values continue - # NOTE: the percentage change for a value that was - # originally 0 is 100% NOT 0% because this allows it - # to get picked up by various checks below - percentage_change = ( - 100.0 * (new_value - ref_value) / abs(ref_value) - if ref_value != 0 - else 100.0 - ) + # Define relative tolerance + if tolerance == 0: + # Use pytest's default relative tolerance (1e-6) + # 0 tolerance causes floating-point discrepancies + # between local and CI runs + rel_tolerance = None + else: + # tolerance is a percentage, rel arg takes a fraction + rel_tolerance = tolerance / 100 - # ignore the difference between the variables if its below - # tolerance - if percentage_change <= tolerance: - continue + try: + # Use pytest.approx for relative and absolute comparisons: + # handles values close to 0 + assert new_value == pytest.approx(ref_value, rel=rel_tolerance) + except AssertionError: + # NOTE: the percentage change for a value that was originally 0 + # is 100% NOT 0% because this indicates the change better + percentage_change = ( + 100.0 * (new_value - ref_value) / abs(ref_value) + if ref_value != 0 + else 100.0 + ) - diffs.append( - MFileVariableDifference( - key, - ref_value, - new_value, - percentage_change, + diffs.append( + MFileVariableDifference( + key, + ref_value, + new_value, + percentage_change, + ) ) - ) return diffs