From 06aa0db142edabe103306f014e156b781ddf3f44 Mon Sep 17 00:00:00 2001 From: BillSenior Date: Thu, 9 Jan 2025 16:33:46 +0100 Subject: [PATCH] GRIDEDIT-1548 extended and added extra tests to improve code coverage --- .../tests/src/SampleInterpolationTests.cpp | 128 ++++++++++++++++-- 1 file changed, 115 insertions(+), 13 deletions(-) diff --git a/libs/MeshKernel/tests/src/SampleInterpolationTests.cpp b/libs/MeshKernel/tests/src/SampleInterpolationTests.cpp index 8b3a75892..ed1f4d914 100644 --- a/libs/MeshKernel/tests/src/SampleInterpolationTests.cpp +++ b/libs/MeshKernel/tests/src/SampleInterpolationTests.cpp @@ -4,6 +4,7 @@ #include #include "MeshKernel/SampleAveragingInterpolator.hpp" +#include "MeshKernel/SampleTriangulationInterpolator.hpp" #include "TestUtils/Definitions.hpp" #include "TestUtils/MakeMeshes.hpp" #include "TestUtils/SampleFileReader.hpp" @@ -45,6 +46,8 @@ TEST(SampleInterpolationTests, AveragingInterpolationWithPoints) mk::InterpolationParameters params{.m_absoluteSearchRadius = 3.0 * delta}; mk::SampleAveragingInterpolator interpolator(xPoints, yPoints, mk::Projection::cartesian, params); + ASSERT_EQ(static_cast(interpolator.Size()), xPoints.size()); + int propertyId = 1; interpolator.SetData(propertyId, data); @@ -72,8 +75,7 @@ TEST(SampleInterpolationTests, AveragingInterpolationWithMesh) const mk::UInt numberOfPointsY = 11; const mk::UInt numberOfPoints = numberOfPointsX * numberOfPointsY; - std::vector xPoints(numberOfPoints); - std::vector yPoints(numberOfPoints); + std::vector samplePoints(numberOfPoints); std::vector data(numberOfPoints); // Generate sample data points @@ -88,8 +90,8 @@ TEST(SampleInterpolationTests, AveragingInterpolationWithMesh) for (size_t j = 0; j < numberOfPointsX; ++j) { - xPoints[count] = x; - yPoints[count] = y; + samplePoints[i].x = x; + samplePoints[i].y = y; data[count] = x; x += delta; ++count; @@ -99,17 +101,19 @@ TEST(SampleInterpolationTests, AveragingInterpolationWithMesh) } mk::InterpolationParameters params{.m_absoluteSearchRadius = 3.0 * delta, .m_minimumNumberOfSamples = 1}; - mk::SampleAveragingInterpolator interpolator(xPoints, yPoints, mk::Projection::cartesian, params); + mk::SampleAveragingInterpolator interpolator(samplePoints, mk::Projection::cartesian, params); + + ASSERT_EQ(static_cast(interpolator.Size()), samplePoints.size()); //-------------------------------- - const mk::UInt meshPointsX = 3; - const mk::UInt meshPointsY = 3; + const mk::UInt meshPointsX = 5; + const mk::UInt meshPointsY = 5; const auto mesh = MakeRectangularMeshForTesting(meshPointsX, meshPointsY, - 3.0 * delta, - 3.0 * delta, + 8.0 * delta, + 8.0 * delta, mk::Projection::cartesian, {0.5 * delta, 0.5 * delta}); @@ -123,15 +127,113 @@ TEST(SampleInterpolationTests, AveragingInterpolationWithMesh) // Execute ASSERT_EQ(interpolator.Size(), numberOfPoints); + // Test node data + const double initialValue = -1.0e20; - std::vector interpolationResult(mesh->GetNumNodes(), initialValue); - std::vector expectedResult{1000.0, 1000.0, 1000.0, 2000.0, 2000.0, 2000.0, 3000.0, 3000.0, 3000.0}; + std::vector interpolationNodeResult(mesh->GetNumNodes(), initialValue); + std::vector expectedNodeResult{0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0}; - interpolator.Interpolate(propertyId, *mesh, mk::Location::Nodes, interpolationResult); + interpolator.Interpolate(propertyId, *mesh, mk::Location::Nodes, interpolationNodeResult); const double tolerance = 1.0e-8; - for (size_t i = 0; i < expectedResult.size(); ++i) + for (size_t i = 0; i < interpolationNodeResult.size(); ++i) + { + EXPECT_NEAR(expectedNodeResult[i], interpolationNodeResult[i], tolerance); + } + + // Test edge data + + std::vector interpolationEdgeResult(mesh->GetNumEdges(), initialValue); + std::vector expectedEdgeResult{0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 0.0, 2000.0, 4000.0, 6000.0, 8000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0}; + + interpolator.Interpolate(propertyId, *mesh, mk::Location::Edges, interpolationEdgeResult); + + for (size_t i = 0; i < interpolationEdgeResult.size(); ++i) + { + EXPECT_NEAR(expectedEdgeResult[i], interpolationEdgeResult[i], tolerance); + } + + // Test face data + + std::vector interpolationFaceResult(mesh->GetNumFaces(), initialValue); + std::vector expectedFaceResult{1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0, + 1000.0, 3000.0, 5000.0, 7000.0}; + + interpolator.Interpolate(propertyId, *mesh, mk::Location::Faces, interpolationFaceResult); + + for (size_t i = 0; i < interpolationFaceResult.size(); ++i) + { + EXPECT_NEAR(expectedFaceResult[i], interpolationFaceResult[i], tolerance); + } +} + +TEST(SampleInterpolationTests, TriangulationInterpolationWithPoints) +{ + const mk::UInt numberOfPointsX = 11; + const mk::UInt numberOfPointsY = 11; + const mk::UInt numberOfPoints = numberOfPointsX * numberOfPointsY; + + std::vector xPoints(numberOfPoints); + std::vector yPoints(numberOfPoints); + std::vector 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::SampleTriangulationInterpolator interpolator(xPoints, yPoints, mk::Projection::cartesian); + + ASSERT_EQ(static_cast(interpolator.Size()), xPoints.size()); + + int propertyId = 1; + interpolator.SetData(propertyId, data); + + // Execute + ASSERT_EQ(interpolator.Size(), numberOfPoints); + + std::vector interpolationPoints{{0.5 * delta, 0.5 * delta}, {9.5 * delta, 9.5 * delta}, {11.0 * delta, 11.0 * delta}}; + const double initialValue = -1.0e20; + std::vector interpolationResult(interpolationPoints.size(), initialValue); + std::vector expectedResult{500.0, 9500.0, mk::constants::missing::doubleValue}; + + interpolator.Interpolate(propertyId, interpolationPoints, interpolationResult); + + const double tolerance = 1.0e-8; + + for (size_t i = 0; i < interpolationResult.size(); ++i) { EXPECT_NEAR(expectedResult[i], interpolationResult[i], tolerance); }