From 0cd08f709fc7583b4be949aaeafa16bd13eaf96d Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 12:31:45 +0100 Subject: [PATCH 1/6] Extract generate_random_sample_data function to a test_tools.h file to avoid code duplication --- tests/DirectSolver/directSolver.cpp | 18 ++---------------- tests/DirectSolver/directSolverNoMumps.cpp | 18 ++---------------- .../extrapolated_smoother.cpp | 18 ++---------------- .../extrapolated_prolongation.cpp | 19 ++----------------- .../extrapolated_restriction.cpp | 14 ++------------ tests/Interpolation/prolongation.cpp | 18 ++---------------- tests/Interpolation/restriction.cpp | 13 ++----------- tests/Residual/residual.cpp | 18 ++---------------- tests/Smoother/smoother.cpp | 18 ++---------------- tests/test_tools.h | 17 +++++++++++++++++ 10 files changed, 35 insertions(+), 136 deletions(-) create mode 100644 tests/test_tools.h diff --git a/tests/DirectSolver/directSolver.cpp b/tests/DirectSolver/directSolver.cpp index 84e8c5a3..06e7e5ff 100644 --- a/tests/DirectSolver/directSolver.cpp +++ b/tests/DirectSolver/directSolver.cpp @@ -3,6 +3,8 @@ #include #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Residual/ResidualGive/residualGive.h" @@ -44,22 +46,6 @@ #include "../include/InputFunctions/DensityProfileCoefficients/zoniShiftedGyroCoefficients.h" #include "../include/InputFunctions/SourceTerms/refined_ZoniShiftedGyro_CulhamGeometry.h" -namespace DirectSolverTest -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector x("x", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < x.size(); ++i) { - x(i) = dist(gen); - } - return x; -} -} // namespace DirectSolverTest - -using namespace DirectSolverTest; - #ifdef GMGPOLAR_USE_MUMPS /* Test 1/2: */ diff --git a/tests/DirectSolver/directSolverNoMumps.cpp b/tests/DirectSolver/directSolverNoMumps.cpp index 49207283..7ae3eec7 100644 --- a/tests/DirectSolver/directSolverNoMumps.cpp +++ b/tests/DirectSolver/directSolverNoMumps.cpp @@ -3,6 +3,8 @@ #include #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Residual/ResidualGive/residualGive.h" @@ -44,22 +46,6 @@ #include "../include/InputFunctions/DensityProfileCoefficients/zoniShiftedGyroCoefficients.h" #include "../include/InputFunctions/SourceTerms/refined_ZoniShiftedGyro_CulhamGeometry.h" -namespace DirectSolverTestNoMumps -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector x("x", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < x.size(); ++i) { - x(i) = dist(gen); - } - return x; -} -} // namespace DirectSolverTestNoMumps - -using namespace DirectSolverTestNoMumps; - /* Test 1/2: */ /* Does the Take and Give Implementation match up? */ diff --git a/tests/ExtrapolatedSmoother/extrapolated_smoother.cpp b/tests/ExtrapolatedSmoother/extrapolated_smoother.cpp index 90729ec6..714403a5 100644 --- a/tests/ExtrapolatedSmoother/extrapolated_smoother.cpp +++ b/tests/ExtrapolatedSmoother/extrapolated_smoother.cpp @@ -3,6 +3,8 @@ #include #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Residual/ResidualGive/residualGive.h" @@ -26,22 +28,6 @@ #include -namespace ExtrapolatedExtrapolatedSmootherTest -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector x("x", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < x.size(); ++i) { - x(i) = dist(gen); - } - return x; -} -} // namespace ExtrapolatedExtrapolatedSmootherTest - -using namespace ExtrapolatedExtrapolatedSmootherTest; - /* Test 1/2: */ /* Does the Take and Give Implementation match up? */ diff --git a/tests/Interpolation/extrapolated_prolongation.cpp b/tests/Interpolation/extrapolated_prolongation.cpp index 0e749047..e02b86e4 100644 --- a/tests/Interpolation/extrapolated_prolongation.cpp +++ b/tests/Interpolation/extrapolated_prolongation.cpp @@ -2,6 +2,8 @@ #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Interpolation/interpolation.h" #include "../../include/InputFunctions/domainGeometry.h" @@ -10,23 +12,6 @@ #include "../../include/InputFunctions/DomainGeometry/circularGeometry.h" #include "../../include/InputFunctions/DensityProfileCoefficients/poissonCoefficients.h" -namespace ExtrapolatedProlongationTest -{ -// Function to generate sample data for vector x using random values with seed -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector vector("vector", grid.numberOfNodes()); - std::mt19937 gen(seed); // Standard mersenne_twister_engine seeded with seed - std::uniform_real_distribution dist(0.0, 1.0); // Generate random double between 0 and 1 - for (uint i = 0; i < vector.size(); ++i) { - vector[i] = dist(gen); - } - return vector; -} -} // namespace ExtrapolatedProlongationTest - -using namespace ExtrapolatedProlongationTest; - TEST(ExtrapolatedProlongationTest, ExtrapolatedProlongationSmoothingRadius) { std::vector fine_radii = {0.1, 0.2, 0.25, 0.5, 0.8, 0.9, 1.3, 1.4, 2.0}; diff --git a/tests/Interpolation/extrapolated_restriction.cpp b/tests/Interpolation/extrapolated_restriction.cpp index 6d619a7b..8eee7e8a 100644 --- a/tests/Interpolation/extrapolated_restriction.cpp +++ b/tests/Interpolation/extrapolated_restriction.cpp @@ -2,6 +2,8 @@ #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Interpolation/interpolation.h" #include "../../include/InputFunctions/domainGeometry.h" @@ -12,18 +14,6 @@ namespace ExtrapolatedRestrictionTest { -// Function to generate sample data for vector x using random values with seed -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector vector("vector", grid.numberOfNodes()); - std::mt19937 gen(seed); // Standard mersenne_twister_engine seeded with seed - std::uniform_real_distribution dist(0.0, 1.0); // Generate random double between 0 and 1 - for (uint i = 0; i < vector.size(); ++i) { - vector[i] = dist(gen); - } - return vector; -} - /* In src/Interpolation/restriction.cpp the Restriction Operator is implemented with "Take". */ /* Here we test against the "Give" version. */ diff --git a/tests/Interpolation/prolongation.cpp b/tests/Interpolation/prolongation.cpp index 01655b11..3eb46183 100644 --- a/tests/Interpolation/prolongation.cpp +++ b/tests/Interpolation/prolongation.cpp @@ -2,26 +2,12 @@ #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Interpolation/interpolation.h" #include "../../include/InputFunctions/DensityProfileCoefficients/poissonCoefficients.h" -namespace ProlongationTest -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector vector("vector", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < vector.size(); ++i) { - vector[i] = dist(gen); - } - return vector; -} -} // namespace ProlongationTest - -using namespace ProlongationTest; - TEST(ProlongationTest, ProlongationTest) { std::vector fine_radii = {0.1, 0.2, 0.25, 0.5, 0.8, 0.9, 1.3, 1.4, 2.0}; diff --git a/tests/Interpolation/restriction.cpp b/tests/Interpolation/restriction.cpp index 006f8c75..6adf8856 100644 --- a/tests/Interpolation/restriction.cpp +++ b/tests/Interpolation/restriction.cpp @@ -2,23 +2,14 @@ #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Interpolation/interpolation.h" #include "../../include/InputFunctions/DensityProfileCoefficients/poissonCoefficients.h" namespace RestrictionTest { -// Function to generate sample data for vector x using random values with seed -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector vector("vector", grid.numberOfNodes()); - std::mt19937 gen(seed); // Standard mersenne_twister_engine seeded with seed - std::uniform_real_distribution dist(0.0, 1.0); // Generate random double between 0 and 1 - for (uint i = 0; i < vector.size(); ++i) { - vector[i] = dist(gen); - } - return vector; -} /* In src/Interpolation/restriction.cpp the Restriction Operator is implemented with "Take". */ /* Here we test against the "Give" version. */ diff --git a/tests/Residual/residual.cpp b/tests/Residual/residual.cpp index 7522f8c3..70ec6449 100644 --- a/tests/Residual/residual.cpp +++ b/tests/Residual/residual.cpp @@ -3,6 +3,8 @@ #include #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Residual/ResidualGive/residualGive.h" @@ -20,22 +22,6 @@ #include "../include/InputFunctions/DensityProfileCoefficients/zoniShiftedCoefficients.h" #include "../include/InputFunctions/SourceTerms/polarR6_ZoniShifted_CzarnyGeometry.h" -namespace ResidualTest -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector x("x", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < x.size(); ++i) { - x(i) = dist(gen); - } - return x; -} -} // namespace ResidualTest - -using namespace ResidualTest; - /* Test 1/1: */ /* Does the Take and Give Implementation match up? */ diff --git a/tests/Smoother/smoother.cpp b/tests/Smoother/smoother.cpp index 40ea3003..8fad3f54 100644 --- a/tests/Smoother/smoother.cpp +++ b/tests/Smoother/smoother.cpp @@ -3,6 +3,8 @@ #include #include +#include "../test_tools.h" + #include "../../include/GMGPolar/gmgpolar.h" #include "../../include/Residual/ResidualGive/residualGive.h" @@ -24,22 +26,6 @@ #include -namespace SmootherTest -{ -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) -{ - Vector x("x", grid.numberOfNodes()); - std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); - for (uint i = 0; i < x.size(); ++i) { - x(i) = dist(gen); - } - return x; -} -} // namespace SmootherTest - -using namespace SmootherTest; - /* Test 1/2: */ /* Does the Take and Give Implementation match up? */ diff --git a/tests/test_tools.h b/tests/test_tools.h new file mode 100644 index 00000000..e13dc577 --- /dev/null +++ b/tests/test_tools.h @@ -0,0 +1,17 @@ +#pragma once +#include + +#include "../include/PolarGrid/polargrid.h" +#include "../include/LinearAlgebra/vector.h" + +Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) +{ + Vector x("x", grid.numberOfNodes()); + std::mt19937 gen(seed); + std::uniform_real_distribution dist(-100.0, 100.0); + for (uint i = 0; i < x.size(); ++i) { + x(i) = dist(gen); + } + return x; +} + From 4eed006f89f7309725ac2e89d78b93969551ce42 Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 12:34:14 +0100 Subject: [PATCH 2/6] Clang --- tests/test_tools.h | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_tools.h b/tests/test_tools.h index e13dc577..b221e959 100644 --- a/tests/test_tools.h +++ b/tests/test_tools.h @@ -14,4 +14,3 @@ Vector generate_random_sample_data(const PolarGrid& grid, unsigned int s } return x; } - From 0d16382c6cbd1f056a4da1a1e81e4a1bb12e7eea Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 12:41:03 +0100 Subject: [PATCH 3/6] Inline for file in header --- tests/test_tools.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tools.h b/tests/test_tools.h index b221e959..a9a73993 100644 --- a/tests/test_tools.h +++ b/tests/test_tools.h @@ -4,7 +4,7 @@ #include "../include/PolarGrid/polargrid.h" #include "../include/LinearAlgebra/vector.h" -Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) +inline Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) { Vector x("x", grid.numberOfNodes()); std::mt19937 gen(seed); From 67afb50cdc23e325edbc1b7557222258a33cd7c9 Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 12:49:41 +0100 Subject: [PATCH 4/6] Allow min/max to be passed as arguments --- tests/test_tools.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_tools.h b/tests/test_tools.h index a9a73993..31c58c98 100644 --- a/tests/test_tools.h +++ b/tests/test_tools.h @@ -4,7 +4,8 @@ #include "../include/PolarGrid/polargrid.h" #include "../include/LinearAlgebra/vector.h" -inline Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed) +inline Vector generate_random_sample_data(const PolarGrid& grid, unsigned int seed, double min_val = -100.0, + double max_val = 100.0) { Vector x("x", grid.numberOfNodes()); std::mt19937 gen(seed); From ebfcfd7b1d6b71922aba80ddcf5b1bc88e5a3325 Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 12:51:18 +0100 Subject: [PATCH 5/6] Put back bounds in Interpolation tests --- tests/Interpolation/extrapolated_prolongation.cpp | 2 +- tests/Interpolation/extrapolated_restriction.cpp | 2 +- tests/Interpolation/restriction.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Interpolation/extrapolated_prolongation.cpp b/tests/Interpolation/extrapolated_prolongation.cpp index e02b86e4..b5a26db0 100644 --- a/tests/Interpolation/extrapolated_prolongation.cpp +++ b/tests/Interpolation/extrapolated_prolongation.cpp @@ -27,7 +27,7 @@ TEST(ExtrapolatedProlongationTest, ExtrapolatedProlongationSmoothingRadius) Interpolation interpolation_operator(maxOpenMPThreads, DirBC_Interior); unsigned int seed = 42; - Vector x = generate_random_sample_data(coarse_grid, seed); + Vector x = generate_random_sample_data(coarse_grid, seed, 0.0, 1.0); // Apply prolongation to both functions Vector result1("result1", finest_grid.numberOfNodes()); diff --git a/tests/Interpolation/extrapolated_restriction.cpp b/tests/Interpolation/extrapolated_restriction.cpp index 8eee7e8a..f988fda3 100644 --- a/tests/Interpolation/extrapolated_restriction.cpp +++ b/tests/Interpolation/extrapolated_restriction.cpp @@ -107,7 +107,7 @@ TEST(ExtrapolatedRestrictionTest, applyExtrapolatedRestriction) Interpolation interpolation_operator(maxOpenMPThreads, DirBC_Interior); unsigned int seed = 42; - Vector x = generate_random_sample_data(finest_grid, seed); + Vector x = generate_random_sample_data(finest_grid, seed, 0.0, 1.0); // Apply prolongation to both functions Vector result1("result1", coarse_grid.numberOfNodes()); diff --git a/tests/Interpolation/restriction.cpp b/tests/Interpolation/restriction.cpp index 6adf8856..fa7c5e1b 100644 --- a/tests/Interpolation/restriction.cpp +++ b/tests/Interpolation/restriction.cpp @@ -109,7 +109,7 @@ TEST(RestrictionTest, applyRestriction) Interpolation interpolation_operator(maxOpenMPThreads, DirBC_Interior); - Vector x = generate_random_sample_data(finest_grid, 42); + Vector x = generate_random_sample_data(finest_grid, 42, 0.0, 1.0); // Apply prolongation to both functions Vector result1("result1", coarse_grid.numberOfNodes()); From 16d254e31d28c899c2c9b2e06cf0d4665e987ec7 Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Thu, 15 Jan 2026 13:00:33 +0100 Subject: [PATCH 6/6] Correct bounds --- tests/test_tools.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tools.h b/tests/test_tools.h index 31c58c98..75d7e6bd 100644 --- a/tests/test_tools.h +++ b/tests/test_tools.h @@ -9,7 +9,7 @@ inline Vector generate_random_sample_data(const PolarGrid& grid, unsigne { Vector x("x", grid.numberOfNodes()); std::mt19937 gen(seed); - std::uniform_real_distribution dist(-100.0, 100.0); + std::uniform_real_distribution dist(min_val, max_val); for (uint i = 0; i < x.size(); ++i) { x(i) = dist(gen); }