diff --git a/src/FireDomain.cpp b/src/FireDomain.cpp index 1f068e6c..44267abe 100644 --- a/src/FireDomain.cpp +++ b/src/FireDomain.cpp @@ -1125,7 +1125,7 @@ size_t maxI = (size_t) ((nec.getX() - SWCornerX())/burningMatrixResX) + 1; if ( maxI > globalBMapSizeX - 1 ) maxI = globalBMapSizeX - 1; size_t minJ = 1; - if ( swc.getY() > SWCornerY()/burningMatrixResY ) + if ( swc.getY() > SWCornerY()+burningMatrixResY ) minJ = (size_t) ((swc.getY() - SWCornerY())/burningMatrixResY); size_t maxJ = (size_t) ((nec.getY() - SWCornerY())/burningMatrixResY) + 1; if ( maxJ > globalBMapSizeY - 1 ) maxJ = globalBMapSizeY - 1; diff --git a/src/FireNode.cpp b/src/FireNode.cpp index 00d15b15..ee0b4a25 100644 --- a/src/FireNode.cpp +++ b/src/FireNode.cpp @@ -185,6 +185,8 @@ void FireNode::timeAdvance(){ } // obtaining the speed from the propagation model double localSpeed = domain->getPropagationSpeed(this); + // No data here (outside a layer, no fuel): the node stops + if ( std::isnan(localSpeed) ) localSpeed = 0.; double newSpeed = localSpeed; /*if(newSpeed > minSpeed){ double prevSpeed, nextSpeed; diff --git a/src/GradientDataLayer.h b/src/GradientDataLayer.h index cbd0e802..6b3c25ac 100644 --- a/src/GradientDataLayer.h +++ b/src/GradientDataLayer.h @@ -80,6 +80,11 @@ T GradientDataLayer::getValueAt(FireNode* fn){ T nextValue; FFPoint nextLoc = fn->getLoc() + dx*(fn->getNormal().toPoint()); nextValue = parent->getValueAt(nextLoc,fn->getUpdateTime()); + if ( std::isnan(nextValue) ){ + // Ahead is outside the data (domain edge): look behind instead + FFPoint prevLoc = fn->getLoc() - dx*(fn->getNormal().toPoint()); + return (currentValue - parent->getValueAt(prevLoc,fn->getUpdateTime()))/dx; + } return (nextValue - currentValue)/dx; } diff --git a/src/NCXYZTDataLayer.h b/src/NCXYZTDataLayer.h index 57dafd0f..92228575 100644 --- a/src/NCXYZTDataLayer.h +++ b/src/NCXYZTDataLayer.h @@ -320,21 +320,23 @@ T XYZTDataLayer::getValueAt(FFPoint loc, const double& t){ - double ud = indices.getX() + EPSILONX; - double vd = indices.getY() + EPSILONX; + // No data outside the layer. Values sit at cell centres: between + // the layer edge and the outermost centres, the outermost cells hold. + if ( nx < 2 or ny < 2 ) return NAN; + if ( indices.getX() < -0.5 - EPSILONX ) return NAN; + if ( indices.getX() > nx - 0.5 + EPSILONX ) return NAN; + if ( indices.getY() < -0.5 - EPSILONX ) return NAN; + if ( indices.getY() > ny - 0.5 + EPSILONX ) return NAN; + + double ud = min(max(indices.getX() + EPSILONX, 0.), nx - 1.); + double vd = min(max(indices.getY() + EPSILONX, 0.), ny - 1.); int uu = (int) ceil(ud-1); int vv = (int) ceil(vd-1); -/* if ( uu < 0 ) uu = 0; if ( uu > ((int) nx) - 2 ) uu = ((int) nx) - 2; if ( vv < 0 ) vv = 0; if ( vv > ((int) ny) - 2 ) vv = ((int) ny) - 2; - */ - if (uu < 0) return NAN; - if (uu > ((int) nx) - 2) return NAN; - if (vv < 0) return NAN; - if (vv > ((int) ny) - 2) return NAN; double udif = ud - ((double) uu); double vdif = vd - ((double) vv); diff --git a/src/propagation/FarsitePropagationModel.cpp b/src/propagation/FarsitePropagationModel.cpp index 64c17541..cb3b0c24 100644 --- a/src/propagation/FarsitePropagationModel.cpp +++ b/src/propagation/FarsitePropagationModel.cpp @@ -237,6 +237,10 @@ namespace libforefire if (fuel_lw > 0.0) nlive++; + // No fuel (non-burnable cell): no spread, rather than 0/0 below + if (ndead == 0 && nlive == 0) + return 0.0; + if (nlive == 0) fuel_dynamic = false; diff --git a/tests/unit/test_commands.cpp b/tests/unit/test_commands.cpp index f31ffc35..66b56492 100644 --- a/tests/unit/test_commands.cpp +++ b/tests/unit/test_commands.cpp @@ -15,6 +15,10 @@ * The arrival times can be read two ways, and both used to be wrong: the BMap * layer was never filled, and plot[...;filename=*.asc] wrote one row per x, * at six significant digits, instead of an ASCII grid. + * + * A front reaching the domain edge over a gridded altitude used to fill the + * whole arrival map at one time: the slope past the edge was NaN, and so + * were the node positions it moved. */ #include "doctest/doctest.h" @@ -175,4 +179,37 @@ TEST_CASE("plot writes arrival times as an ASCII grid, north row first") { CHECK(inexact == 0); } +TEST_CASE("a front that reaches the edge on sloping ground leaves the far side unburnt") { + Command executor; + run(executor, "clear[]"); + run(executor, "FireDomain[sw=(0,0,0);ne=(1000,1000,0);t=0]"); + auto* domain = Command::getDomain(); + REQUIRE(domain != nullptr); + REQUIRE(domain->addPropagativeLayer("Iso")); + + /* 2 x 2 altitude rising 100 m to the east, cell centres at 250 and 750 */ + std::vector altitude = {0., 0., 100., 100.}; + double x0 = 0, y0 = 0, t0 = 0, width = 1000, height = 1000, timespan = 0; + size_t nx = 2, ny = 2, nz = 1, nt = 1; + REQUIRE(domain->addScalarLayer("data", "altitude", x0, y0, t0, width, height, timespan, + nx, ny, nz, nt, altitude.data())); + + run(executor, "startFire[loc=(900,500,0)]"); + run(executor, "goTo[t=200]"); + + /* Iso spreads at 1 m/s: the front is past the east edge, 300 m short of + * the west third. */ + auto matrix = domain->getDataMatrix("arrival_time"); + size_t burnt = 0, burntInWestThird = 0; + for (size_t i = 0; i < matrix.size(); i++) { + for (size_t j = 0; j < matrix[i].size(); j++) { + if (!std::isfinite(matrix[i][j])) continue; + burnt++; + if (i < matrix.size() / 3) burntInWestThird++; + } + } + CHECK(burnt > 0); + CHECK(burntInWestThird == 0); +} + } /* TEST_SUITE */ diff --git a/tests/unit/test_data_layers.cpp b/tests/unit/test_data_layers.cpp index 1d2f0a53..bb6fdefd 100644 --- a/tests/unit/test_data_layers.cpp +++ b/tests/unit/test_data_layers.cpp @@ -1,6 +1,6 @@ /** * @file test_data_layers.cpp - * @brief XYZTDataLayer interpolation in time. + * @brief XYZTDataLayer interpolation in time and near its edges. * @copyright Copyright (C) 2025 ForeFire, Fire Team, SPE, CNRS/Universita di Corsica. * @license This program is free software; See LICENSE file for details. (See LICENSE file). */ @@ -10,6 +10,7 @@ #include "FFPoint.h" #include "NCXYZTDataLayer.h" +#include #include #include @@ -30,6 +31,17 @@ double valueAt(const std::vector& slices, double t0, double timespan, do return layer.getValueAt(FFPoint(500., 500., 0.), t); } +// A 2x2 layer over 2 km, 0 in the west cells and 10 in the east ones. +// Values sit at the cell centres, x = 500 and x = 1500. +double eastRampAt(double x, double y) { + std::vector vals = {0., 0., 10., 10.}; // x is the slowest index + FFPoint sw(0., 0., 0.), extent(2000., 2000., 0.); + double t0 = 0., timespan = 0.; + std::size_t nx = 2, ny = 2, nz = 1, nt = 1; + XYZTDataLayer layer("altitude", sw, t0, extent, timespan, nx, ny, nz, nt, vals.data()); + return layer.getValueAt(FFPoint(x, y, 0.), 0.); +} + } TEST_SUITE("data layers") { @@ -58,4 +70,21 @@ TEST_CASE("outside its time axis a layer holds the first or last slice") { CHECK(valueAt(ramp, 1000., 2000., 5000.) == doctest::Approx(8.)); } +TEST_CASE("between its edge and the outermost cell centres a layer holds the edge cells") { + // This band used to return NaN: half a cell along every edge, a quarter + // of a 2x2 layer. A slope taken there turned the front geometry to NaN. + // Lookups are nudged by EPSILONX, hence the tolerance mid-ramp. + CHECK(eastRampAt(1000., 1000.) == doctest::Approx(5.).epsilon(0.01)); + CHECK(eastRampAt(1900., 1000.) == doctest::Approx(10.)); + CHECK(eastRampAt(2000., 1000.) == doctest::Approx(10.)); + CHECK(eastRampAt(100., 1900.) == doctest::Approx(0.)); + CHECK(eastRampAt(0., 0.) == doctest::Approx(0.)); +} + +TEST_CASE("outside its extent a layer has no data") { + CHECK(std::isnan(eastRampAt(2100., 1000.))); + CHECK(std::isnan(eastRampAt(-100., 1000.))); + CHECK(std::isnan(eastRampAt(1000., 2100.))); +} + } diff --git a/tests/unit/test_propagation_models.cpp b/tests/unit/test_propagation_models.cpp index 19e45967..30b30a2e 100644 --- a/tests/unit/test_propagation_models.cpp +++ b/tests/unit/test_propagation_models.cpp @@ -204,6 +204,24 @@ TEST_CASE("Farsite reads the wind in m/s") { CHECK(oneMetrePerSecond / calm == doctest::Approx(1.0 + phiw).epsilon(1e-6)); } +TEST_CASE("Farsite does not spread over a cell with no fuel") { + // A non-burnable cell has no fuel load at all, and Farsite divided zero + // by zero for it. The NaN stopped the node for good, even if fuel was + // one step further on. + ModelSandbox sandbox; + PropagationModel* model = sandbox.propagation("Farsite"); + REQUIRE(model != 0); + + Inputs inputs(model); + inputs.set("normalWind", 1.0); + inputs.set("slope", 0.2); + inputs.set("moist.ones", 0.05); + + const double ros = speedOf(model, inputs); + CHECK(std::isfinite(ros)); + CHECK(ros == doctest::Approx(0.0)); +} + TEST_CASE("spread is finite and non-negative over a wind and slope sweep") { ModelSandbox sandbox; const std::vector& names = standardFuelModels();