From 4b754e3c75fe737c3e3ee62ab7dbc065634daf9e Mon Sep 17 00:00:00 2001 From: Antonio Leblanc <56365887+antonio-leblanc@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:02:02 -0300 Subject: [PATCH 1/6] fix: use 3.59e-4 in the Rothermel wind exponent 10E-4 is 1e-3 in C++. Regenerates the runff references and the unit test pin. --- CHANGELOG.md | 6 ++++++ src/propagation/Rothermel.cpp | 2 +- tests/runff/ForeFire.0.nc.ref | 4 ++-- tests/runff/real_case.kml.ref | 4 ++-- tests/unit/test_propagation_models.cpp | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88bd1c31..834571ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,6 +102,11 @@ Merged since v2.5.0, not yet released. dividing by it in eq. C7, which drove the radiant fraction to zero. It now matches `Balbi2020.py` in `wildfire_ROS_models`, and its rate of spread is floored at zero on a downslope with no wind. ([#196]) +- `Rothermel` used `10E-4` (that is, 1e-3) instead of 3.59e-4 in the wind + exponent E, so wind raised the rate of spread too little. Results with wind + change: +6% on the `runff` fuel at 5 m/s, several times more in loose fuel + beds such as sparse shrub. The `runff` references and the unit test pin are + regenerated. ([#195]) ### Changed @@ -255,6 +260,7 @@ repository stays easy to return to. [#197]: https://github.com/forefireAPI/forefire/pull/197 [#194]: https://github.com/forefireAPI/forefire/pull/194 [#196]: https://github.com/forefireAPI/forefire/issues/196 +[#195]: https://github.com/forefireAPI/forefire/pull/195 [#202]: https://github.com/forefireAPI/forefire/pull/202 [#199]: https://github.com/forefireAPI/forefire/pull/199 [#179]: https://github.com/forefireAPI/forefire/pull/179 diff --git a/src/propagation/Rothermel.cpp b/src/propagation/Rothermel.cpp index 517d68ab..8fe8faae 100644 --- a/src/propagation/Rothermel.cpp +++ b/src/propagation/Rothermel.cpp @@ -208,7 +208,7 @@ double Rothermel::getSpeed(double* valueOf){ double B = 0.02526*pow(lsd, 0.54); - double E = 0.715* exp(-3.59*(10E-4 * lsd)); + double E = 0.715* exp(-3.59e-4 * lsd); double Ir = Rprime*Wn*lDeltaH*Etam*Etas; double Uf = 0.9*Ir; diff --git a/tests/runff/ForeFire.0.nc.ref b/tests/runff/ForeFire.0.nc.ref index 9083f602..43100b88 100644 --- a/tests/runff/ForeFire.0.nc.ref +++ b/tests/runff/ForeFire.0.nc.ref @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:45a2b159caad744d15d8cdd1b43d738a6626259ebc8542944d5daa06a2fad42a -size 99203 +oid sha256:666b4d1f9bbcff131aa6188741455ce2eeb4ebe2640f985ec57a82b7a00f8462 +size 134658 diff --git a/tests/runff/real_case.kml.ref b/tests/runff/real_case.kml.ref index bf5c8b20..49f4dc34 100644 --- a/tests/runff/real_case.kml.ref +++ b/tests/runff/real_case.kml.ref @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5124aba342990b9af402a2960c6ff1cc5bc36f91c19004d56dbfce1e03d80021 -size 8171 +oid sha256:b3d483a73911521aaf1eeca7005dcf8ef093a00176ecf16938c88be2ef3dec89 +size 12661 diff --git a/tests/unit/test_propagation_models.cpp b/tests/unit/test_propagation_models.cpp index ce1f9725..46cc3349 100644 --- a/tests/unit/test_propagation_models.cpp +++ b/tests/unit/test_propagation_models.cpp @@ -249,7 +249,7 @@ TEST_CASE("recorded rates of spread for the standard fuel") { double ros; }; const Expectation expected[] = { - {"Rothermel", 0.369131}, + {"Rothermel", 0.392218}, {"Balbi2015", 0.069659}, {"Balbi2020", 0.0823161}, {"BalbiNov2011", 8.16139e-05}, From ddeb3e7cbc4e9016b7083d0252b61bd2cf015033 Mon Sep 17 00:00:00 2001 From: Antonio Leblanc <56365887+antonio-leblanc@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:44:04 -0300 Subject: [PATCH 2/6] test: compare runff fronts by shape, not node by node The front now has a different node count on each CPU (630 on the Linux runner, 636 on macOS, 637 elsewhere). Both comparisons run on failure, and the outputs are uploaded. --- .github/workflows/macos.yml | 11 +++- .github/workflows/main.yml | 9 +++ tests/runff/compare_kml.py | 127 +++++++++++++++++++++++------------- tests/runff/compare_nc.py | 70 +++++++++----------- tests/runff/ff-run.bash | 9 +-- 5 files changed, 137 insertions(+), 89 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 2d6fb4e5..6bcbe3f3 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -57,4 +57,13 @@ jobs: - name: Run 'runff' Test Script run: | cd tests/runff - bash ff-run.bash \ No newline at end of file + bash ff-run.bash + + - name: Upload runff outputs + if: failure() + uses: actions/upload-artifact@v7 + with: + name: runff-outputs-${{ runner.os }} + path: | + tests/runff/real_case.kml + tests/runff/ForeFire.0.nc diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index efcca169..2a822cf8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,6 +81,15 @@ jobs: cd tests/runff bash ff-run.bash # Execute the complete test logic script + - name: Upload runff outputs + if: failure() + uses: actions/upload-artifact@v7 + with: + name: runff-outputs-${{ runner.os }} + path: | + tests/runff/real_case.kml + tests/runff/ForeFire.0.nc + # The only coverage for ANNPropagationModel and BMapLoggerForANNTraining: # both read a .ffann network in their constructor and abort without one, # so the unit suite cannot reach them. ANN_test is built by the step diff --git a/tests/runff/compare_kml.py b/tests/runff/compare_kml.py index ff69011c..29ef19b9 100644 --- a/tests/runff/compare_kml.py +++ b/tests/runff/compare_kml.py @@ -1,38 +1,69 @@ import sys import math -import xml.etree.ElementTree as ET import re +import xml.etree.ElementTree as ET +# The front is compared by shape, not node by node: the number of nodes +# changes with the CPU (630 on the Linux runner, 636 on macOS, 637 elsewhere +# for the same case), while the shape stays put. # --- Configuration --- -RELATIVE_TOLERANCE = 1e-5 -ABSOLUTE_TOLERANCE = 1e-8 +MAX_DISTANCE_M = 15.0 # symmetric Hausdorff distance between the fronts +MAX_AREA_REL_DIFF = 0.01 # relative difference of the enclosed area # --- -def parse_coordinates(coord_string): - """Parses the KML coordinate string into a list of [lon, lat, alt] floats.""" - points = [] - raw_points = [p for p in re.split(r'\s+', coord_string.strip()) if p] - for point_str in raw_points: - try: - coords = [float(c) for c in point_str.split(',')] - if len(coords) == 2: coords.append(0.0) - if len(coords) == 3: points.append(coords) - else: print(f"Warning: Skipping invalid coordinate tuple: {point_str}", file=sys.stderr) - except ValueError: print(f"Warning: Skipping non-numeric coordinate data: {point_str}", file=sys.stderr) - return points - -def compare_coordinate_lists(list1, list2, rel_tol, abs_tol): - """Compares two lists of coordinates point by point with tolerance.""" - if len(list1) != len(list2): - print(f"Error: Coordinate lists have different lengths ({len(list1)} vs {len(list2)})", file=sys.stderr) - return False - for i, (p1, p2) in enumerate(zip(list1, list2)): - if not math.isclose(p1[0], p2[0], rel_tol=rel_tol, abs_tol=abs_tol) or \ - not math.isclose(p1[1], p2[1], rel_tol=rel_tol, abs_tol=abs_tol) or \ - not math.isclose(p1[2], p2[2], rel_tol=rel_tol, abs_tol=abs_tol): - print(f"Error: Mismatch at coordinate point {i+1}:\n Got: {p1}\n Expected: {p2}", file=sys.stderr) - return False - return True +EARTH_RADIUS_M = 6371000.0 + + +def read_rings(path): + """Returns each element of the KML as a list of (lon, lat).""" + root = ET.parse(path).getroot() + elements = root.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root.findall('.//coordinates') + rings = [] + for element in elements: + ring = [] + for point in re.split(r'\s+', (element.text or '').strip()): + if point: + coords = [float(c) for c in point.split(',')] + ring.append((coords[0], coords[1])) + if ring: + rings.append(ring) + return rings + + +def to_metres(rings, lon0, lat0): + """Local equirectangular projection around (lon0, lat0).""" + kx = math.radians(1) * EARTH_RADIUS_M * math.cos(math.radians(lat0)) + ky = math.radians(1) * EARTH_RADIUS_M + return [[((lon - lon0) * kx, (lat - lat0) * ky) for lon, lat in ring] for ring in rings] + + +def area(rings): + total = 0.0 + for ring in rings: + s = 0.0 + for (x1, y1), (x2, y2) in zip(ring, ring[1:] + ring[:1]): + s += x1 * y2 - x2 * y1 + total += abs(s) / 2 + return total + + +def segments(rings): + return [seg for ring in rings for seg in zip(ring, ring[1:] + ring[:1])] + + +def point_segment_distance(p, a, b): + (px, py), (ax, ay), (bx, by) = p, a, b + dx, dy = bx - ax, by - ay + length2 = dx * dx + dy * dy + t = 0.0 if length2 == 0 else max(0.0, min(1.0, ((px - ax) * dx + (py - ay) * dy) / length2)) + return math.hypot(px - ax - t * dx, py - ay - t * dy) + + +def directed_distance(rings_from, rings_to): + segs = segments(rings_to) + return max(min(point_segment_distance(p, a, b) for a, b in segs) + for ring in rings_from for p in ring) + if __name__ == "__main__": if len(sys.argv) != 3: @@ -40,21 +71,25 @@ def compare_coordinate_lists(list1, list2, rel_tol, abs_tol): sys.exit(2) file1_path, file2_path = sys.argv[1], sys.argv[2] try: - tree1 = ET.parse(file1_path); root1 = tree1.getroot() - coords1_elements = root1.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root1.findall('.//coordinates') - coords1_text = "".join([c.text for c in coords1_elements if c.text]) - points1 = parse_coordinates(coords1_text) - - tree2 = ET.parse(file2_path); root2 = tree2.getroot() - coords2_elements = root2.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root2.findall('.//coordinates') - coords2_text = "".join([c.text for c in coords2_elements if c.text]) - points2 = parse_coordinates(coords2_text) - - if not points1 or not points2: print("Error: Could not extract coordinates.", file=sys.stderr); sys.exit(1) - if compare_coordinate_lists(points1, points2, RELATIVE_TOLERANCE, ABSOLUTE_TOLERANCE): - print(f"KML comparison successful: {file1_path} matches {file2_path} within tolerance.") - sys.exit(0) - else: print(f"KML comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr); sys.exit(1) - except ET.ParseError as e: print(f"Error parsing XML: {e}", file=sys.stderr); sys.exit(1) - except FileNotFoundError as e: print(f"Error: File not found - {e}", file=sys.stderr); sys.exit(1) - except Exception as e: print(f"An unexpected error occurred: {e}", file=sys.stderr); sys.exit(1) \ No newline at end of file + rings1, rings2 = read_rings(file1_path), read_rings(file2_path) + except (ET.ParseError, ValueError, FileNotFoundError) as e: + print(f"Error reading KML: {e}", file=sys.stderr) + sys.exit(1) + if not rings1 or not rings2: + print("Error: Could not extract coordinates.", file=sys.stderr) + sys.exit(1) + + lon0, lat0 = rings2[0][0] + m1, m2 = to_metres(rings1, lon0, lat0), to_metres(rings2, lon0, lat0) + distance = max(directed_distance(m1, m2), directed_distance(m2, m1)) + area1, area2 = area(m1), area(m2) + area_diff = abs(area1 - area2) / area2 + + print(f"Fronts: {sum(map(len, rings1))} vs {sum(map(len, rings2))} nodes, " + f"Hausdorff distance {distance:.2f} m (max {MAX_DISTANCE_M}), " + f"area {area1:.0f} vs {area2:.0f} m2, {area_diff:.2%} apart (max {MAX_AREA_REL_DIFF:.0%})") + if distance <= MAX_DISTANCE_M and area_diff <= MAX_AREA_REL_DIFF: + print(f"KML comparison successful: {file1_path} matches {file2_path} within tolerance.") + sys.exit(0) + print(f"KML comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) + sys.exit(1) diff --git a/tests/runff/compare_nc.py b/tests/runff/compare_nc.py index 5b74273d..bb75da8a 100644 --- a/tests/runff/compare_nc.py +++ b/tests/runff/compare_nc.py @@ -1,52 +1,46 @@ # tests/runff/compare_nc.py import sys +import numpy as np import xarray as xr +# Arrival times are compared cell by cell, but a few cells along the front may +# burn on one CPU and not on another, or burn a little earlier, as the front +# itself differs slightly between machines (see compare_kml.py). # --- Configuration --- -# Adjust tolerance as needed. Start with something reasonable. -RELATIVE_TOLERANCE = 1e-5 -ABSOLUTE_TOLERANCE = 1e-8 +VARIABLE = "arrival_time_of_front" +UNBURNT = -9999 +MAX_TIME_DIFF_S = 30.0 # a burnt cell differs if its arrival time moves more +MAX_DIFFERING_FRACTION = 0.01 # share of burnt cells allowed to differ # --- if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python compare_nc.py ", file=sys.stderr) - sys.exit(2) # Exit code for usage error - - file1_path = sys.argv[1] - file2_path = sys.argv[2] - - ds1 = None # Initialize to None - ds2 = None # Initialize to None + sys.exit(2) + file1_path, file2_path = sys.argv[1], sys.argv[2] try: - # Open datasets using xarray. Ensure they are closed afterwards. - ds1 = xr.open_dataset(file1_path, cache=False) - ds2 = xr.open_dataset(file2_path, cache=False) + with xr.open_dataset(file1_path, cache=False) as ds1, xr.open_dataset(file2_path, cache=False) as ds2: + t1, t2 = ds1[VARIABLE].values, ds2[VARIABLE].values + except Exception as e: + print(f"Error reading NetCDF: {e}", file=sys.stderr) + sys.exit(1) + if t1.shape != t2.shape: + print(f"Error: {VARIABLE} has shape {t1.shape} vs {t2.shape}", file=sys.stderr) + sys.exit(1) - # Compare the datasets numerically with tolerance - # This compares all data variables by default. - # Use `equal = ds1.equals(ds2)` for exact bit-wise comparison (usually too strict) - # Use `identical = ds1.identical(ds2)` for exact comparison including attributes etc. (also often too strict) - xr.testing.assert_allclose(ds1, ds2, rtol=RELATIVE_TOLERANCE, atol=ABSOLUTE_TOLERANCE) + burnt1, burnt2 = t1 != UNBURNT, t2 != UNBURNT + both = burnt1 & burnt2 + only_one = int((burnt1 ^ burnt2).sum()) + time_diff = np.abs(t1[both] - t2[both]) + late = int((time_diff > MAX_TIME_DIFF_S).sum()) + burnt = int((burnt1 | burnt2).sum()) + fraction = (only_one + late) / burnt if burnt else 0.0 + print(f"Burnt cells: {int(burnt1.sum())} vs {int(burnt2.sum())}, {only_one} burnt in only one, " + f"{late} more than {MAX_TIME_DIFF_S:.0f} s apart (max diff {time_diff.max() if time_diff.size else 0:.1f} s); " + f"{fraction:.2%} of burnt cells differ (max {MAX_DIFFERING_FRACTION:.0%})") + if fraction <= MAX_DIFFERING_FRACTION: print(f"NetCDF comparison successful: {file1_path} matches {file2_path} within tolerance.") - sys.exit(0) # Success - - except FileNotFoundError as e: - print(f"Error: NetCDF file not found - {e}", file=sys.stderr) - sys.exit(1) - except AssertionError as e: - # This exception is raised by xr.testing.assert_allclose on failure - print(f"NetCDF comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) - print(f"Details from xarray: {e}", file=sys.stderr) - sys.exit(1) # Failure - Datasets are different - except Exception as e: - # Catch other potential errors (e.g., invalid NetCDF format) - print(f"An unexpected error occurred during NetCDF comparison: {e}", file=sys.stderr) - sys.exit(1) # Failure - finally: - # Ensure datasets are closed to release file handles - if ds1 is not None: - ds1.close() - if ds2 is not None: - ds2.close() \ No newline at end of file + sys.exit(0) + print(f"NetCDF comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) + sys.exit(1) diff --git a/tests/runff/ff-run.bash b/tests/runff/ff-run.bash index 7d8b40ce..627bde4b 100644 --- a/tests/runff/ff-run.bash +++ b/tests/runff/ff-run.bash @@ -33,16 +33,17 @@ echo "Verifying KML output..." if ! command -v python3 &> /dev/null; then echo "Error: python3 required."; exit 1; fi if [ ! -f compare_kml.py ]; then echo "Error: compare_kml.py not found."; exit 1; fi -python3 compare_kml.py "$GENERATED_KML" "$REFERENCE_KML" -echo "KML verification passed." +# Both comparisons run even if the first fails, so a failing run shows both. +STATUS=0 +python3 compare_kml.py "$GENERATED_KML" "$REFERENCE_KML" || STATUS=1 echo "Verifying NetCDF output..." if [ ! -f compare_nc.py ]; then echo "Error: compare_nc.py not found."; exit 1; fi # Check if the NetCDF file was created (likely by the first run) if [ ! -f "$GENERATED_NC" ]; then echo "Error: Expected NetCDF output file '$GENERATED_NC' not found."; exit 1; fi -python3 compare_nc.py "$GENERATED_NC" "$REFERENCE_NC" -echo "NetCDF verification passed." +python3 compare_nc.py "$GENERATED_NC" "$REFERENCE_NC" || STATUS=1 +if [ "$STATUS" -ne 0 ]; then echo "runff verification failed."; exit 1; fi echo "All KML and NetCDF verifications passed for runff test." exit 0 \ No newline at end of file From 3dca2e16355bf633f4cb16c77ec0c0a922d25ace Mon Sep 17 00:00:00 2001 From: Antonio Leblanc <56365887+antonio-leblanc@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:47:04 -0300 Subject: [PATCH 3/6] test: set the runff front tolerance from the CI runners --- tests/runff/compare_kml.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/runff/compare_kml.py b/tests/runff/compare_kml.py index 29ef19b9..f08497a8 100644 --- a/tests/runff/compare_kml.py +++ b/tests/runff/compare_kml.py @@ -5,9 +5,10 @@ # The front is compared by shape, not node by node: the number of nodes # changes with the CPU (630 on the Linux runner, 636 on macOS, 637 elsewhere -# for the same case), while the shape stays put. +# for the same case), while the shape stays put: the runners land 16-30 m and +# 0.01-0.34% of area from the reference, the pre-#195 front 416 m and 37%. # --- Configuration --- -MAX_DISTANCE_M = 15.0 # symmetric Hausdorff distance between the fronts +MAX_DISTANCE_M = 50.0 # symmetric Hausdorff distance, about two cells of ForeFire.0.nc MAX_AREA_REL_DIFF = 0.01 # relative difference of the enclosed area # --- From 68d58fc57d6dc84868b647bcdb6ab584662d9926 Mon Sep 17 00:00:00 2001 From: Antonio Leblanc <56365887+antonio-leblanc@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:47:52 -0300 Subject: [PATCH 4/6] test: reword the tolerance comment --- tests/runff/compare_kml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/runff/compare_kml.py b/tests/runff/compare_kml.py index f08497a8..768d2ab1 100644 --- a/tests/runff/compare_kml.py +++ b/tests/runff/compare_kml.py @@ -6,7 +6,7 @@ # The front is compared by shape, not node by node: the number of nodes # changes with the CPU (630 on the Linux runner, 636 on macOS, 637 elsewhere # for the same case), while the shape stays put: the runners land 16-30 m and -# 0.01-0.34% of area from the reference, the pre-#195 front 416 m and 37%. +# 0.01-0.34% of area from it, the front before the Rothermel fix 416 m and 37%. # --- Configuration --- MAX_DISTANCE_M = 50.0 # symmetric Hausdorff distance, about two cells of ForeFire.0.nc MAX_AREA_REL_DIFF = 0.01 # relative difference of the enclosed area From 4aa69df8cba5c99fb8e16ad761519bf7d7f2ea55 Mon Sep 17 00:00:00 2001 From: Antonio Leblanc Date: Tue, 27 Oct 2026 15:01:06 -0300 Subject: [PATCH 5/6] build: -ffp-contract=off so every platform gives the same front; runff back to exact comparison --- .github/workflows/macos.yml | 11 +--- .github/workflows/main.yml | 9 --- CHANGELOG.md | 3 + CMakeLists.txt | 2 +- tests/runff/compare_kml.py | 128 +++++++++++++----------------------- tests/runff/compare_nc.py | 70 +++++++++++--------- tests/runff/ff-run.bash | 9 ++- 7 files changed, 93 insertions(+), 139 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 6bcbe3f3..2d6fb4e5 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -57,13 +57,4 @@ jobs: - name: Run 'runff' Test Script run: | cd tests/runff - bash ff-run.bash - - - name: Upload runff outputs - if: failure() - uses: actions/upload-artifact@v7 - with: - name: runff-outputs-${{ runner.os }} - path: | - tests/runff/real_case.kml - tests/runff/ForeFire.0.nc + bash ff-run.bash \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2a822cf8..efcca169 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -81,15 +81,6 @@ jobs: cd tests/runff bash ff-run.bash # Execute the complete test logic script - - name: Upload runff outputs - if: failure() - uses: actions/upload-artifact@v7 - with: - name: runff-outputs-${{ runner.os }} - path: | - tests/runff/real_case.kml - tests/runff/ForeFire.0.nc - # The only coverage for ANNPropagationModel and BMapLoggerForANNTraining: # both read a .ffann network in their constructor and abort without one, # so the unit suite cannot reach them. ANN_test is built by the step diff --git a/CHANGELOG.md b/CHANGELOG.md index 834571ec..4518d6dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,9 @@ Merged since v2.5.0, not yet released. regenerated. ([#195]) ### Changed +- Release builds compile with `-ffp-contract=off`, so the compiler no longer + fuses multiply-adds (FMA) where the CPU allows it. `-march=native` and ARM + builds now give the same simulation results as x86 builds without it. - Linux wheels are built against a NetCDF without DAP and HDF4, roughly halving the wheel. ([#154]) diff --git a/CMakeLists.txt b/CMakeLists.txt index 871674c8..d7ea5206 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,7 +256,7 @@ elseif(FOREFIRE_SANITIZE) else() # uncomment if want to debug # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto -fomit-frame-pointer -finline-functions -funroll-loops -ftree-vectorize -fno-strict-aliasing") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -ffp-contract=off -flto -fomit-frame-pointer -finline-functions -funroll-loops -ftree-vectorize -fno-strict-aliasing") if(FOREFIRE_NATIVE_ARCH) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") endif() diff --git a/tests/runff/compare_kml.py b/tests/runff/compare_kml.py index 768d2ab1..ff69011c 100644 --- a/tests/runff/compare_kml.py +++ b/tests/runff/compare_kml.py @@ -1,70 +1,38 @@ import sys import math -import re import xml.etree.ElementTree as ET +import re -# The front is compared by shape, not node by node: the number of nodes -# changes with the CPU (630 on the Linux runner, 636 on macOS, 637 elsewhere -# for the same case), while the shape stays put: the runners land 16-30 m and -# 0.01-0.34% of area from it, the front before the Rothermel fix 416 m and 37%. # --- Configuration --- -MAX_DISTANCE_M = 50.0 # symmetric Hausdorff distance, about two cells of ForeFire.0.nc -MAX_AREA_REL_DIFF = 0.01 # relative difference of the enclosed area +RELATIVE_TOLERANCE = 1e-5 +ABSOLUTE_TOLERANCE = 1e-8 # --- -EARTH_RADIUS_M = 6371000.0 - - -def read_rings(path): - """Returns each element of the KML as a list of (lon, lat).""" - root = ET.parse(path).getroot() - elements = root.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root.findall('.//coordinates') - rings = [] - for element in elements: - ring = [] - for point in re.split(r'\s+', (element.text or '').strip()): - if point: - coords = [float(c) for c in point.split(',')] - ring.append((coords[0], coords[1])) - if ring: - rings.append(ring) - return rings - - -def to_metres(rings, lon0, lat0): - """Local equirectangular projection around (lon0, lat0).""" - kx = math.radians(1) * EARTH_RADIUS_M * math.cos(math.radians(lat0)) - ky = math.radians(1) * EARTH_RADIUS_M - return [[((lon - lon0) * kx, (lat - lat0) * ky) for lon, lat in ring] for ring in rings] - - -def area(rings): - total = 0.0 - for ring in rings: - s = 0.0 - for (x1, y1), (x2, y2) in zip(ring, ring[1:] + ring[:1]): - s += x1 * y2 - x2 * y1 - total += abs(s) / 2 - return total - - -def segments(rings): - return [seg for ring in rings for seg in zip(ring, ring[1:] + ring[:1])] - - -def point_segment_distance(p, a, b): - (px, py), (ax, ay), (bx, by) = p, a, b - dx, dy = bx - ax, by - ay - length2 = dx * dx + dy * dy - t = 0.0 if length2 == 0 else max(0.0, min(1.0, ((px - ax) * dx + (py - ay) * dy) / length2)) - return math.hypot(px - ax - t * dx, py - ay - t * dy) - - -def directed_distance(rings_from, rings_to): - segs = segments(rings_to) - return max(min(point_segment_distance(p, a, b) for a, b in segs) - for ring in rings_from for p in ring) - +def parse_coordinates(coord_string): + """Parses the KML coordinate string into a list of [lon, lat, alt] floats.""" + points = [] + raw_points = [p for p in re.split(r'\s+', coord_string.strip()) if p] + for point_str in raw_points: + try: + coords = [float(c) for c in point_str.split(',')] + if len(coords) == 2: coords.append(0.0) + if len(coords) == 3: points.append(coords) + else: print(f"Warning: Skipping invalid coordinate tuple: {point_str}", file=sys.stderr) + except ValueError: print(f"Warning: Skipping non-numeric coordinate data: {point_str}", file=sys.stderr) + return points + +def compare_coordinate_lists(list1, list2, rel_tol, abs_tol): + """Compares two lists of coordinates point by point with tolerance.""" + if len(list1) != len(list2): + print(f"Error: Coordinate lists have different lengths ({len(list1)} vs {len(list2)})", file=sys.stderr) + return False + for i, (p1, p2) in enumerate(zip(list1, list2)): + if not math.isclose(p1[0], p2[0], rel_tol=rel_tol, abs_tol=abs_tol) or \ + not math.isclose(p1[1], p2[1], rel_tol=rel_tol, abs_tol=abs_tol) or \ + not math.isclose(p1[2], p2[2], rel_tol=rel_tol, abs_tol=abs_tol): + print(f"Error: Mismatch at coordinate point {i+1}:\n Got: {p1}\n Expected: {p2}", file=sys.stderr) + return False + return True if __name__ == "__main__": if len(sys.argv) != 3: @@ -72,25 +40,21 @@ def directed_distance(rings_from, rings_to): sys.exit(2) file1_path, file2_path = sys.argv[1], sys.argv[2] try: - rings1, rings2 = read_rings(file1_path), read_rings(file2_path) - except (ET.ParseError, ValueError, FileNotFoundError) as e: - print(f"Error reading KML: {e}", file=sys.stderr) - sys.exit(1) - if not rings1 or not rings2: - print("Error: Could not extract coordinates.", file=sys.stderr) - sys.exit(1) - - lon0, lat0 = rings2[0][0] - m1, m2 = to_metres(rings1, lon0, lat0), to_metres(rings2, lon0, lat0) - distance = max(directed_distance(m1, m2), directed_distance(m2, m1)) - area1, area2 = area(m1), area(m2) - area_diff = abs(area1 - area2) / area2 - - print(f"Fronts: {sum(map(len, rings1))} vs {sum(map(len, rings2))} nodes, " - f"Hausdorff distance {distance:.2f} m (max {MAX_DISTANCE_M}), " - f"area {area1:.0f} vs {area2:.0f} m2, {area_diff:.2%} apart (max {MAX_AREA_REL_DIFF:.0%})") - if distance <= MAX_DISTANCE_M and area_diff <= MAX_AREA_REL_DIFF: - print(f"KML comparison successful: {file1_path} matches {file2_path} within tolerance.") - sys.exit(0) - print(f"KML comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) - sys.exit(1) + tree1 = ET.parse(file1_path); root1 = tree1.getroot() + coords1_elements = root1.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root1.findall('.//coordinates') + coords1_text = "".join([c.text for c in coords1_elements if c.text]) + points1 = parse_coordinates(coords1_text) + + tree2 = ET.parse(file2_path); root2 = tree2.getroot() + coords2_elements = root2.findall('.//{http://www.opengis.net/kml/2.2}coordinates') or root2.findall('.//coordinates') + coords2_text = "".join([c.text for c in coords2_elements if c.text]) + points2 = parse_coordinates(coords2_text) + + if not points1 or not points2: print("Error: Could not extract coordinates.", file=sys.stderr); sys.exit(1) + if compare_coordinate_lists(points1, points2, RELATIVE_TOLERANCE, ABSOLUTE_TOLERANCE): + print(f"KML comparison successful: {file1_path} matches {file2_path} within tolerance.") + sys.exit(0) + else: print(f"KML comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr); sys.exit(1) + except ET.ParseError as e: print(f"Error parsing XML: {e}", file=sys.stderr); sys.exit(1) + except FileNotFoundError as e: print(f"Error: File not found - {e}", file=sys.stderr); sys.exit(1) + except Exception as e: print(f"An unexpected error occurred: {e}", file=sys.stderr); sys.exit(1) \ No newline at end of file diff --git a/tests/runff/compare_nc.py b/tests/runff/compare_nc.py index bb75da8a..5b74273d 100644 --- a/tests/runff/compare_nc.py +++ b/tests/runff/compare_nc.py @@ -1,46 +1,52 @@ # tests/runff/compare_nc.py import sys -import numpy as np import xarray as xr -# Arrival times are compared cell by cell, but a few cells along the front may -# burn on one CPU and not on another, or burn a little earlier, as the front -# itself differs slightly between machines (see compare_kml.py). # --- Configuration --- -VARIABLE = "arrival_time_of_front" -UNBURNT = -9999 -MAX_TIME_DIFF_S = 30.0 # a burnt cell differs if its arrival time moves more -MAX_DIFFERING_FRACTION = 0.01 # share of burnt cells allowed to differ +# Adjust tolerance as needed. Start with something reasonable. +RELATIVE_TOLERANCE = 1e-5 +ABSOLUTE_TOLERANCE = 1e-8 # --- if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python compare_nc.py ", file=sys.stderr) - sys.exit(2) - file1_path, file2_path = sys.argv[1], sys.argv[2] + sys.exit(2) # Exit code for usage error + + file1_path = sys.argv[1] + file2_path = sys.argv[2] + + ds1 = None # Initialize to None + ds2 = None # Initialize to None try: - with xr.open_dataset(file1_path, cache=False) as ds1, xr.open_dataset(file2_path, cache=False) as ds2: - t1, t2 = ds1[VARIABLE].values, ds2[VARIABLE].values - except Exception as e: - print(f"Error reading NetCDF: {e}", file=sys.stderr) - sys.exit(1) - if t1.shape != t2.shape: - print(f"Error: {VARIABLE} has shape {t1.shape} vs {t2.shape}", file=sys.stderr) - sys.exit(1) + # Open datasets using xarray. Ensure they are closed afterwards. + ds1 = xr.open_dataset(file1_path, cache=False) + ds2 = xr.open_dataset(file2_path, cache=False) - burnt1, burnt2 = t1 != UNBURNT, t2 != UNBURNT - both = burnt1 & burnt2 - only_one = int((burnt1 ^ burnt2).sum()) - time_diff = np.abs(t1[both] - t2[both]) - late = int((time_diff > MAX_TIME_DIFF_S).sum()) - burnt = int((burnt1 | burnt2).sum()) - fraction = (only_one + late) / burnt if burnt else 0.0 + # Compare the datasets numerically with tolerance + # This compares all data variables by default. + # Use `equal = ds1.equals(ds2)` for exact bit-wise comparison (usually too strict) + # Use `identical = ds1.identical(ds2)` for exact comparison including attributes etc. (also often too strict) + xr.testing.assert_allclose(ds1, ds2, rtol=RELATIVE_TOLERANCE, atol=ABSOLUTE_TOLERANCE) - print(f"Burnt cells: {int(burnt1.sum())} vs {int(burnt2.sum())}, {only_one} burnt in only one, " - f"{late} more than {MAX_TIME_DIFF_S:.0f} s apart (max diff {time_diff.max() if time_diff.size else 0:.1f} s); " - f"{fraction:.2%} of burnt cells differ (max {MAX_DIFFERING_FRACTION:.0%})") - if fraction <= MAX_DIFFERING_FRACTION: print(f"NetCDF comparison successful: {file1_path} matches {file2_path} within tolerance.") - sys.exit(0) - print(f"NetCDF comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) - sys.exit(1) + sys.exit(0) # Success + + except FileNotFoundError as e: + print(f"Error: NetCDF file not found - {e}", file=sys.stderr) + sys.exit(1) + except AssertionError as e: + # This exception is raised by xr.testing.assert_allclose on failure + print(f"NetCDF comparison failed: {file1_path} differs from {file2_path}.", file=sys.stderr) + print(f"Details from xarray: {e}", file=sys.stderr) + sys.exit(1) # Failure - Datasets are different + except Exception as e: + # Catch other potential errors (e.g., invalid NetCDF format) + print(f"An unexpected error occurred during NetCDF comparison: {e}", file=sys.stderr) + sys.exit(1) # Failure + finally: + # Ensure datasets are closed to release file handles + if ds1 is not None: + ds1.close() + if ds2 is not None: + ds2.close() \ No newline at end of file diff --git a/tests/runff/ff-run.bash b/tests/runff/ff-run.bash index 627bde4b..7d8b40ce 100644 --- a/tests/runff/ff-run.bash +++ b/tests/runff/ff-run.bash @@ -33,17 +33,16 @@ echo "Verifying KML output..." if ! command -v python3 &> /dev/null; then echo "Error: python3 required."; exit 1; fi if [ ! -f compare_kml.py ]; then echo "Error: compare_kml.py not found."; exit 1; fi -# Both comparisons run even if the first fails, so a failing run shows both. -STATUS=0 -python3 compare_kml.py "$GENERATED_KML" "$REFERENCE_KML" || STATUS=1 +python3 compare_kml.py "$GENERATED_KML" "$REFERENCE_KML" +echo "KML verification passed." echo "Verifying NetCDF output..." if [ ! -f compare_nc.py ]; then echo "Error: compare_nc.py not found."; exit 1; fi # Check if the NetCDF file was created (likely by the first run) if [ ! -f "$GENERATED_NC" ]; then echo "Error: Expected NetCDF output file '$GENERATED_NC' not found."; exit 1; fi -python3 compare_nc.py "$GENERATED_NC" "$REFERENCE_NC" || STATUS=1 +python3 compare_nc.py "$GENERATED_NC" "$REFERENCE_NC" +echo "NetCDF verification passed." -if [ "$STATUS" -ne 0 ]; then echo "runff verification failed."; exit 1; fi echo "All KML and NetCDF verifications passed for runff test." exit 0 \ No newline at end of file From 5f4da0eeb3610005ac56943c7a6a2852c3adca37 Mon Sep 17 00:00:00 2001 From: Antonio Leblanc Date: Tue, 27 Oct 2026 15:15:05 -0300 Subject: [PATCH 6/6] changelog: the old Rothermel coefficient was 3.59e-3, not 1e-3; one list under Changed --- CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4518d6dc..3fd571e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,17 +102,18 @@ Merged since v2.5.0, not yet released. dividing by it in eq. C7, which drove the radiant fraction to zero. It now matches `Balbi2020.py` in `wildfire_ROS_models`, and its rate of spread is floored at zero on a downslope with no wind. ([#196]) -- `Rothermel` used `10E-4` (that is, 1e-3) instead of 3.59e-4 in the wind - exponent E, so wind raised the rate of spread too little. Results with wind +- `Rothermel` wrote the wind exponent coefficient as `3.59*10E-4`, which is + 3.59e-3 in C++, ten times the 3.59e-4 of Rothermel (1972), so wind raised + the rate of spread too little. Results with wind change: +6% on the `runff` fuel at 5 m/s, several times more in loose fuel beds such as sparse shrub. The `runff` references and the unit test pin are regenerated. ([#195]) ### Changed + - Release builds compile with `-ffp-contract=off`, so the compiler no longer fuses multiply-adds (FMA) where the CPU allows it. `-march=native` and ARM builds now give the same simulation results as x86 builds without it. - - Linux wheels are built against a NetCDF without DAP and HDF4, roughly halving the wheel. ([#154]) - Free-threaded (`cp314t`) wheels are no longer published, because the core is