Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/NCXYZTDataLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ template<typename T> class XYZTDataLayer : public DataLayer<T> {
dy = extent.getY()/ny;
dz = extent.getZ()/nz;
dt = 0;
if((timespan < 3600*24)&& (nt>1)){
if(nt>1){
dt = timespan/(nt-1);
}

Expand Down Expand Up @@ -356,7 +356,13 @@ T XYZTDataLayer<T>::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);
Expand All @@ -372,8 +378,8 @@ T XYZTDataLayer<T>::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 ){
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" "domain ownership")
foreach(_suite "model registry" "propagation models" "flux models" "http server" "topology" "fire front" "timetable" "quit command" "commands" "data broker" "data layers" "domain ownership")
string(REPLACE " " "_" _suite_id "${_suite}")
add_test(NAME "unit.${_suite_id}"
COMMAND forefire_unit_tests --test-suite=${_suite} --no-skipped-summary)
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/test_data_layers.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <vector>

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<double>& slices, double t0, double timespan, double t) {
std::size_t nx = 2, ny = 2, nz = 1, nt = slices.size();
std::vector<double> 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<double> 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<double> 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<double> 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<double> 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<double> ramp = {0., 8.};
CHECK(valueAt(ramp, 1000., 2000., 0.) == doctest::Approx(0.));
CHECK(valueAt(ramp, 1000., 2000., 5000.) == doctest::Approx(8.));
}

}
Loading