Skip to content

Commit

Permalink
GRIDEDIT-1548 Added unit test for the sample averaging interpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed Jan 9, 2025
1 parent 9abd348 commit 54965fc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace meshkernel
/// @brief Which averaging method should be used
AveragingInterpolation::Method m_method = AveragingInterpolation::Method::SimpleAveraging;

/// @brief The absolute search radius
double m_absoluteSearchRadius = 100.0;

/// @brief The relative search radius
double m_relativeSearchRadius = 1.0;

Expand Down
2 changes: 1 addition & 1 deletion libs/MeshKernel/src/SampleAveragingInterpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void meshkernel::SampleAveragingInterpolator::Interpolate(const int propertyId,
std::vector<Sample> sampleCache;
sampleCache.reserve(100);

double searchRadiusSquared = 1.0e5;
double searchRadiusSquared = m_interpolationParameters.m_absoluteSearchRadius * m_interpolationParameters.m_absoluteSearchRadius;

for (size_t i = 0; i < interpolationNodes.size(); ++i)
{
Expand Down
1 change: 1 addition & 0 deletions libs/MeshKernel/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(
${SRC_DIR}/PolygonalEnclosureTests.cpp
${SRC_DIR}/PolygonsTests.cpp
${SRC_DIR}/RangeCheckTests.cpp
${SRC_DIR}/SampleInterpolationTests.cpp
${SRC_DIR}/SpatialTreesTests.cpp
${SRC_DIR}/SplineAlgorithmTests.cpp
${SRC_DIR}/SplineTests.cpp
Expand Down
67 changes: 67 additions & 0 deletions libs/MeshKernel/tests/src/SampleInterpolationTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <vector>

#include "MeshKernel/SampleAveragingInterpolator.hpp"
#include "TestUtils/Definitions.hpp"
#include "TestUtils/MakeMeshes.hpp"
#include "TestUtils/SampleFileReader.hpp"

namespace mk = meshkernel;

TEST(SampleInterpolationTests, AveragingInterpolation)
{
const mk::UInt numberOfPointsX = 11;
const mk::UInt numberOfPointsY = 11;
const mk::UInt numberOfPoints = numberOfPointsX * numberOfPointsY;

std::vector<double> xPoints(numberOfPoints);
std::vector<double> yPoints(numberOfPoints);
std::vector<double> data(numberOfPoints);

// Generate sample data points
double delta = 1000.0;
double x = 0.0;
double y = 0.0;
size_t count = 0;

for (size_t i = 0; i < numberOfPointsY; ++i)
{
x = 0.0;

for (size_t j = 0; j < numberOfPointsX; ++j)
{
xPoints[count] = x;
yPoints[count] = y;
data[count] = x;
x += delta;
++count;
}

y += delta;
}

mk::InterpolationParameters params{.m_absoluteSearchRadius = 3.0 * delta};
mk::SampleAveragingInterpolator interpolator(xPoints, yPoints, mk::Projection::cartesian, params);

int propertyId = 1;
interpolator.SetData(propertyId, data);

// Execute
ASSERT_EQ(interpolator.Size(), numberOfPoints);

std::vector<mk::Point> interpolationPoints{{0.5 * delta, 0.5 * delta}, {9.5 * delta, 9.5 * delta}};
const double initialValue = -1.0e20;
std::vector<double> interpolationResult(interpolationPoints.size(), initialValue);
std::vector<double> expectedResult{1400.0, 8600.0};

interpolator.Interpolate(propertyId, interpolationPoints, interpolationResult);

const double tolerance = 1.0e-8;

for (size_t i = 0; i < expectedResult.size(); ++i)
{
EXPECT_NEAR(expectedResult[i], interpolationResult[i], tolerance);
}
}

0 comments on commit 54965fc

Please sign in to comment.