From b74a0d697eafc8be1b3c97581cde6f95b0e39094 Mon Sep 17 00:00:00 2001 From: HugoFara Date: Thu, 29 Oct 2026 13:06:19 +0100 Subject: [PATCH] fix(core): interpolate time-varying data layers forwards, past 24 h too (#232) A data layer with several time slices weighted them the wrong way round: just after slice k it returned slice k+1. It also ignored startTime in the weight, read one slice past the array at the last slice, and left dt at 0 when the timespan was 24 h or more, so multi-day layers returned NaN. Weight slices by their distance from t, measured from startTime, and hold the first or last slice outside the time axis. New "data layers" unit suite. --- src/NCXYZTDataLayer.h | 14 +++++--- tests/unit/CMakeLists.txt | 5 +-- tests/unit/test_data_layers.cpp | 61 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_data_layers.cpp diff --git a/src/NCXYZTDataLayer.h b/src/NCXYZTDataLayer.h index 15d3c509..57dafd0f 100644 --- a/src/NCXYZTDataLayer.h +++ b/src/NCXYZTDataLayer.h @@ -153,7 +153,7 @@ template class XYZTDataLayer : public DataLayer { dy = extent.getY()/ny; dz = extent.getZ()/nz; dt = 0; - if((timespan < 3600*24)&& (nt>1)){ + if(nt>1){ dt = timespan/(nt-1); } @@ -356,7 +356,13 @@ T XYZTDataLayer::getValueAt(FFPoint loc, const double& t){ } if ( interp == InterpolationBilinearT ){ - int it = (int) (t-startTime)/dt; + // Slices sit at startTime + k*dt; before the first or after + // the last, the nearest slice holds. + double tt = dt > 0 ? (t-startTime)/dt : 0.; + if ( tt < 0. ) tt = 0.; + if ( tt > nt-1. ) tt = nt-1.; + int it = (int) tt; + if ( it > ((int) nt) - 2 ) it = ((int) nt) - 2; T tsw1 = getVal(uu,vv,0,it); T tnw1 = getVal(uu,vv+1,0,it); @@ -372,8 +378,8 @@ T XYZTDataLayer::getValueAt(FFPoint loc, const double& t){ T val2 = csw*tsw2 + cse*tse2 + cnw*tnw2 + cne*tne2; /* interpolation in time */ - double at = ( t - it*dt )/dt; - return at*val1 + (1.-at)*val2; + double at = tt - it; + return (1.-at)*val1 + at*val2; } if ( interp == InterpolationBilinearCoeff ){ diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 765845b7..974829e1 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -19,7 +19,8 @@ add_executable(forefire_unit_tests test_timetable.cpp test_quit_command.cpp test_commands.cpp - test_data_broker.cpp) + test_data_broker.cpp + test_data_layers.cpp) target_link_libraries(forefire_unit_tests PRIVATE forefireL) target_include_directories(forefire_unit_tests PRIVATE @@ -34,7 +35,7 @@ endif() # doctest groups its cases into suites; registering one CTest entry per suite # keeps `ctest` output useful without needing doctest's CMake integration. -foreach(_suite "model registry" "propagation models" "flux models" "http server" "topology" "fire front" "timetable" "quit command" "commands" "data broker") +foreach(_suite "model registry" "propagation models" "flux models" "http server" "topology" "fire front" "timetable" "quit command" "commands" "data broker" "data layers") string(REPLACE " " "_" _suite_id "${_suite}") add_test(NAME "unit.${_suite_id}" COMMAND forefire_unit_tests --test-suite=${_suite} --no-skipped-summary) diff --git a/tests/unit/test_data_layers.cpp b/tests/unit/test_data_layers.cpp new file mode 100644 index 00000000..1d2f0a53 --- /dev/null +++ b/tests/unit/test_data_layers.cpp @@ -0,0 +1,61 @@ +/** + * @file test_data_layers.cpp + * @brief XYZTDataLayer interpolation in time. + * @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). + */ + +#include "doctest/doctest.h" + +#include "FFPoint.h" +#include "NCXYZTDataLayer.h" + +#include +#include + +using libforefire::FFPoint; +using libforefire::XYZTDataLayer; + +namespace { + +// A 2x2 layer over 1 km, uniform in space, one value per time slice. +double valueAt(const std::vector& slices, double t0, double timespan, double t) { + std::size_t nx = 2, ny = 2, nz = 1, nt = slices.size(); + std::vector vals(nx * ny * nz * nt); + for (std::size_t c = 0; c < nx * ny; ++c) + for (std::size_t k = 0; k < nt; ++k) + vals[c * nt + k] = slices[k]; // time is the fastest index + FFPoint sw(0., 0., 0.), extent(1000., 1000., 0.); + XYZTDataLayer layer("windU", sw, t0, extent, timespan, nx, ny, nz, nt, vals.data()); + return layer.getValueAt(FFPoint(500., 500., 0.), t); +} + +} + +TEST_SUITE("data layers") { + +TEST_CASE("a layer with a time axis returns the slice it is closest to") { + const std::vector ramp = {0., 8.}; + CHECK(valueAt(ramp, 0., 2000., 0.) == doctest::Approx(0.)); + CHECK(valueAt(ramp, 0., 2000., 500.) == doctest::Approx(2.)); + CHECK(valueAt(ramp, 0., 2000., 2000.) == doctest::Approx(8.)); +} + +TEST_CASE("the time axis starts at the layer's start time") { + const std::vector ramp = {0., 8.}; + CHECK(valueAt(ramp, 1000., 2000., 1250.) == doctest::Approx(1.)); +} + +TEST_CASE("a time axis longer than a day still interpolates") { + const std::vector hourly = {0., 4., 8.}; + CHECK(valueAt(hourly, 0., 2 * 86400., 86400.) == doctest::Approx(4.)); + CHECK(valueAt(hourly, 0., 2 * 86400., 1.5 * 86400.) == doctest::Approx(6.)); +} + +TEST_CASE("outside its time axis a layer holds the first or last slice") { + const std::vector ramp = {0., 8.}; + CHECK(valueAt(ramp, 1000., 2000., 0.) == doctest::Approx(0.)); + CHECK(valueAt(ramp, 1000., 2000., 5000.) == doctest::Approx(8.)); +} + +}