From eebc24bfeedc823456de66284424efedf190b38b Mon Sep 17 00:00:00 2001 From: program-- Date: Fri, 9 Aug 2024 08:20:46 -0700 Subject: [PATCH] fix(ForcingsEngineLumpedDataProvider): handle divide_index and fix get_value[s] to use beginning time as epoch --- .../ForcingsEngineLumpedDataProvider.cpp | 10 +++++--- .../ForcingsEngineLumpedDataProvider_Test.cpp | 25 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/forcing/ForcingsEngineLumpedDataProvider.cpp b/src/forcing/ForcingsEngineLumpedDataProvider.cpp index d5ea92b14e..f22e89ec25 100644 --- a/src/forcing/ForcingsEngineLumpedDataProvider.cpp +++ b/src/forcing/ForcingsEngineLumpedDataProvider.cpp @@ -54,6 +54,8 @@ Provider::ForcingsEngineLumpedDataProvider( if (divide_id_pos == cat_id_span.end()) { // throw std::runtime_error{"Unable to find divide ID `" + divide_id + "` in given Forcings Engine domain"}; divide_idx_ = static_cast(-1); + } else { + divide_idx_ = std::distance(cat_id_span.begin(), divide_id_pos); } } @@ -87,8 +89,8 @@ Provider::data_type Provider::get_value( auto current = start; while (current < end) { current += time_step_; - bmi_->UpdateUntil(std::chrono::duration_cast(current - start).count()); - acc += *static_cast(bmi_->GetValuePtr(variable)); + bmi_->UpdateUntil(std::chrono::duration_cast(current - time_begin_).count()); + acc += static_cast(bmi_->GetValuePtr(variable))[divide_idx_]; } if (m == ReSampleMethod::MEAN) { @@ -122,8 +124,8 @@ std::vector Provider::get_values( auto current = start; while (current < end) { current += time_step_; - bmi_->UpdateUntil(std::chrono::duration_cast(current - start).count()); - values.push_back(*static_cast(bmi_->GetValuePtr(variable))); + bmi_->UpdateUntil(std::chrono::duration_cast(current - time_begin_).count()); + values.push_back(static_cast(bmi_->GetValuePtr(variable))[divide_idx_]); } return values; diff --git a/test/forcing/ForcingsEngineLumpedDataProvider_Test.cpp b/test/forcing/ForcingsEngineLumpedDataProvider_Test.cpp index c311e36fc7..bb6e0293ea 100644 --- a/test/forcing/ForcingsEngineLumpedDataProvider_Test.cpp +++ b/test/forcing/ForcingsEngineLumpedDataProvider_Test.cpp @@ -1,5 +1,3 @@ -#include "DataProviderSelectors.hpp" -#include "ForcingsEngineDataProvider.hpp" #include #include @@ -41,8 +39,8 @@ struct ForcingsEngineLumpedDataProviderTest using TestFixture = ForcingsEngineLumpedDataProviderTest; constexpr const char* TestFixture::config_file; -const std::time_t TestFixture::time_start = data_access::detail::parse_time("2024-01-17 01:00:00"); -const std::time_t TestFixture::time_end = TestFixture::time_start + 3600; +const std::time_t TestFixture::time_start = data_access::detail::parse_time("2023-01-17 01:00:00"); +const std::time_t TestFixture::time_end = TestFixture::time_start + 3600 + 3600; std::shared_ptr TestFixture::gil_ = nullptr; std::unique_ptr TestFixture::provider_ = nullptr; @@ -78,7 +76,8 @@ void TestFixture::TearDownTestSuite() /** * Tests for the flyweight-like design of provider storage by getting * a new instance of the forcings engine and verifying that it points - * to the same address as the static initialized `provider_` member. + * to the same address as the static initialized `provider_` memberm + * based on matching `init`, and shared over distinct `divide_id`. */ TEST_F(ForcingsEngineLumpedDataProviderTest, Storage) { @@ -94,6 +93,9 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, Storage) TEST_F(ForcingsEngineLumpedDataProviderTest, VariableAccess) { + ASSERT_EQ(provider_->divide(), 11223UL); + ASSERT_EQ(provider_->divide_index(), 0); + constexpr std::array expected_variables = { "U2D_ELEMENT", "V2D_ELEMENT", @@ -115,11 +117,12 @@ TEST_F(ForcingsEngineLumpedDataProviderTest, VariableAccess) ); } - const auto selector = CatchmentAggrDataSelector{"cat-11223", "LWDOWN", time_start, 3600, "seconds"}; - const auto result = provider_->get_values(selector, data_access::ReSampleMethod::SUM); + auto selector = CatchmentAggrDataSelector{"cat-11223", "PSFC", time_start, 3600, "seconds"}; + auto result = provider_->get_value(selector, data_access::ReSampleMethod::SUM); + EXPECT_NEAR(result, 99580.52, 1e-2); - ASSERT_GT(result.size(), 0); - for (const auto& r : result) { - EXPECT_NEAR(r, 164.45626831054688, 1e6); - } + selector = CatchmentAggrDataSelector{"cat-11223", "LWDOWN", time_start + 3600, 3600, "seconds"}; + auto result2 = provider_->get_values(selector, data_access::ReSampleMethod::SUM); + ASSERT_GT(result2.size(), 0); + EXPECT_NEAR(result2[0], 0, 1e-6); }