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
3 changes: 1 addition & 2 deletions tests/GMGPolar/pcg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ void run_gmgpolar()
solver.solve(boundary_conditions, source_term);

// --- Retrieve solution and associated grid --- //
Vector<double> solution = solver.solution();
const PolarGrid<Kokkos::HostSpace>& solution_grid = solver.grid();
Vector<double> solution = solver.solution();

if (TestFixture::verbose > 0) {
solver.printTimings();
Expand Down
3 changes: 1 addition & 2 deletions tests/GMGPolar/solve_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,7 @@ void run_gmgpolar()
solver.solve(boundary_conditions, source_term);

// --- Retrieve solution and associated grid --- //
Vector<double> solution = solver.solution();
const PolarGrid<Kokkos::HostSpace>& solution_grid = solver.grid();
Vector<double> solution = solver.solution();

if (TestFixture::verbose > 0) {
solver.printTimings();
Expand Down
64 changes: 34 additions & 30 deletions tests/Interpolation/extrapolated_prolongation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using namespace gmgpolar;

// Helper that computes the mathematically expected extrapolated prolongation value
static double expected_extrapolated_value(const PolarGrid<Kokkos::HostSpace>& coarse,
const PolarGrid<Kokkos::HostSpace>& fine, HostConstVector<double> coarse_vals,
static double expected_extrapolated_value(const PolarGrid<DefaultMemorySpace>& coarse,
const PolarGrid<DefaultMemorySpace>& fine, ConstVector<double> coarse_vals,
int i_r, int i_theta)
{
int i_r_coarse = i_r / 2;
Expand All @@ -19,26 +19,35 @@ static double expected_extrapolated_value(const PolarGrid<Kokkos::HostSpace>& co
bool r_even = (i_r % 2 == 0);
bool t_even = (i_theta % 2 == 0);

if (r_even && t_even) {
// Node coincides with a coarse node
return coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)];
}

if (!r_even && t_even) {
// Radial midpoint - arithmetic mean of left and right
return 0.5 * (coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)]);
}

if (r_even && !t_even) {
// Angular midpoint - arithmetic mean of bottom and top
return 0.5 * (coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]);
}

// Center of coarse cell - arithmetic mean of diagonal nodes (bottom-right + top-left)
return 0.5 * (coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]);
double result = 0;
Kokkos::parallel_reduce(
"extrap_prolongation_test", Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(0, 1),
KOKKOS_LAMBDA(int idx, double& local_result) {
if (r_even && t_even) {
// Node coincides with a coarse node
local_result = coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)];
}

else if (!r_even && t_even) {
// Radial midpoint - arithmetic mean of left and right
local_result = 0.5 * (coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)]);
}

else if (r_even && !t_even) {
// Angular midpoint - arithmetic mean of bottom and top
local_result = 0.5 * (coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]);
}
else {
// Center of coarse cell - arithmetic mean of diagonal nodes (bottom-right + top-left)
local_result = 0.5 * (coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)] +
coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]);
}
},
result);

return result;
}

TEST(ExtrapolatedProlongationTest, ExtrapolatedProlongationMatchesStencil)
Expand All @@ -50,24 +59,19 @@ TEST(ExtrapolatedProlongationTest, ExtrapolatedProlongationMatchesStencil)
PolarGrid<DefaultMemorySpace> fine_grid(fine_radii, fine_angles);
PolarGrid<DefaultMemorySpace> coarse_grid = coarseningGrid(fine_grid);

PolarGrid<Kokkos::HostSpace> h_fine_grid(fine_grid);
PolarGrid<Kokkos::HostSpace> h_coarse_grid(coarse_grid);

Interpolation I(/*DirBC*/ true);

HostVector<double> h_coarse_values = generate_random_sample_data(h_coarse_grid, 1234, 0.0, 1.0);
Vector<double> coarse_values = generate_random_sample_data(coarse_grid, 1234, 0.0, 1.0);
Vector<double> fine_result("fine_result", fine_grid.numberOfNodes());

auto coarse_values = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), h_coarse_values);

I.applyExtrapolatedProlongation(coarse_grid, fine_grid, fine_result, coarse_values);

auto h_fine_result = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), fine_result);

for (int i_r = 0; i_r < fine_grid.nr(); ++i_r) {
for (int i_theta = 0; i_theta < fine_grid.ntheta(); ++i_theta) {
double expected = expected_extrapolated_value(h_coarse_grid, h_fine_grid, h_coarse_values, i_r, i_theta);
double got = h_fine_result[h_fine_grid.index(i_r, i_theta)];
double expected = expected_extrapolated_value(coarse_grid, fine_grid, coarse_values, i_r, i_theta);
double got = h_fine_result[fine_grid.index(i_r, i_theta)];
Comment thread
EmilyBourne marked this conversation as resolved.
ASSERT_NEAR(expected, got, 1e-10) << "Mismatch at (" << i_r << ", " << i_theta << ")";
}
}
Expand Down
68 changes: 34 additions & 34 deletions tests/Interpolation/extrapolated_restriction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,40 @@
using namespace gmgpolar;

// Helper that computes the mathematically expected extrapolated restriction value
static double expected_extrapolated_restriction_value(const PolarGrid<Kokkos::HostSpace>& fine,
const PolarGrid<Kokkos::HostSpace>& coarse,
HostConstVector<double> fine_vals, int i_r_coarse,
int i_theta_coarse)
static double expected_extrapolated_restriction_value(const PolarGrid<DefaultMemorySpace>& fine,
const PolarGrid<DefaultMemorySpace>& coarse,
ConstVector<double> fine_vals, int i_r_coarse, int i_theta_coarse)
{
int i_r = i_r_coarse * 2;
int i_theta = i_theta_coarse * 2;

// Angular indices with periodic wrapping
int i_theta_M1 = fine.wrapThetaIndex(i_theta - 1);
int i_theta_P1 = fine.wrapThetaIndex(i_theta + 1);

// Center + Angular contributions (always present)
double value = fine_vals[fine.index(i_r, i_theta)] + 0.5 * fine_vals[fine.index(i_r, i_theta_M1)] +
0.5 * fine_vals[fine.index(i_r, i_theta_P1)];

// Left contributions (if not at inner boundary)
if (i_r_coarse > 0) {
value += 0.5 * fine_vals[fine.index(i_r - 1, i_theta)] +
0.5 * fine_vals[fine.index(i_r - 1, i_theta_P1)]; // Top-Left diagonal
}

// Right contributions (if not at outer boundary)
if (i_r_coarse < coarse.nr() - 1) {
value += 0.5 * fine_vals[fine.index(i_r + 1, i_theta)] +
0.5 * fine_vals[fine.index(i_r + 1, i_theta_M1)]; // Bottom-Right diagonal
}

return value;
double result = 0;
Kokkos::parallel_reduce(
"extrap_restriction_test", Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(0, 1),
KOKKOS_LAMBDA(const int idx, double& local_result) {
// Angular indices with periodic wrapping
int i_theta_M1 = fine.wrapThetaIndex(i_theta - 1);
int i_theta_P1 = fine.wrapThetaIndex(i_theta + 1);

// Center + Angular contributions (always present)
local_result = fine_vals[fine.index(i_r, i_theta)] + 0.5 * fine_vals[fine.index(i_r, i_theta_M1)] +
0.5 * fine_vals[fine.index(i_r, i_theta_P1)];

// Left contributions (if not at inner boundary)
if (i_r_coarse > 0) {
local_result += 0.5 * fine_vals[fine.index(i_r - 1, i_theta)] +
0.5 * fine_vals[fine.index(i_r - 1, i_theta_P1)]; // Top-Left diagonal
}

// Right contributions (if not at outer boundary)
if (i_r_coarse < coarse.nr() - 1) {
local_result += 0.5 * fine_vals[fine.index(i_r + 1, i_theta)] +
0.5 * fine_vals[fine.index(i_r + 1, i_theta_M1)]; // Bottom-Right diagonal
}
},
result);

return result;
}

TEST(ExtrapolatedRestrictionTest, ExtrapolatedRestrictionMatchesStencil)
Expand All @@ -49,25 +54,20 @@ TEST(ExtrapolatedRestrictionTest, ExtrapolatedRestrictionMatchesStencil)
PolarGrid<DefaultMemorySpace> fine_grid(fine_radii, fine_angles);
PolarGrid<DefaultMemorySpace> coarse_grid = coarseningGrid(fine_grid);

PolarGrid<Kokkos::HostSpace> h_fine_grid(fine_grid);
PolarGrid<Kokkos::HostSpace> h_coarse_grid(coarse_grid);

Interpolation I(/*DirBC*/ true);

HostVector<double> h_fine_values = generate_random_sample_data(h_fine_grid, 9012, 0.0, 1.0);
Vector<double> fine_values = generate_random_sample_data(fine_grid, 9012, 0.0, 1.0);
Vector<double> coarse_result("coarse_result", coarse_grid.numberOfNodes());

auto fine_values = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), h_fine_values);

I.applyExtrapolatedRestriction(fine_grid, coarse_grid, coarse_result, fine_values);

auto h_coarse_result = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), coarse_result);

for (int i_r_coarse = 0; i_r_coarse < coarse_grid.nr(); ++i_r_coarse) {
for (int i_theta_coarse = 0; i_theta_coarse < coarse_grid.ntheta(); ++i_theta_coarse) {
double expected = expected_extrapolated_restriction_value(h_fine_grid, h_coarse_grid, h_fine_values,
i_r_coarse, i_theta_coarse);
double got = h_coarse_result[h_coarse_grid.index(i_r_coarse, i_theta_coarse)];
double expected = expected_extrapolated_restriction_value(fine_grid, coarse_grid, fine_values, i_r_coarse,
i_theta_coarse);
double got = h_coarse_result[coarse_grid.index(i_r_coarse, i_theta_coarse)];
Comment thread
EmilyBourne marked this conversation as resolved.
ASSERT_NEAR(expected, got, 1e-10) << "Mismatch at (" << i_r_coarse << ", " << i_theta_coarse << ")";
}
}
Expand Down
95 changes: 50 additions & 45 deletions tests/Interpolation/prolongation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,60 @@
using namespace gmgpolar;

// Helper that computes the mathematically expected prolongation value
static double expected_value(const PolarGrid<Kokkos::HostSpace>& coarse, const PolarGrid<Kokkos::HostSpace>& fine,
HostConstVector<double> coarse_vals, int i_r, int i_theta)
static double expected_value(const PolarGrid<DefaultMemorySpace>& coarse, const PolarGrid<DefaultMemorySpace>& fine,
ConstVector<double> coarse_vals, int i_r, int i_theta)
{
int i_r_coarse = i_r / 2;
int i_theta_coarse = i_theta / 2;

bool r_even = (i_r % 2 == 0);
bool t_even = (i_theta % 2 == 0);

if (r_even && t_even) {
// Node coincides with a coarse node
return coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)];
}

if (!r_even && t_even) {
// Radial midpoint
double h1 = fine.radialSpacing(i_r - 1);
double h2 = fine.radialSpacing(i_r);

return (h1 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
h2 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)]) /
(h1 + h2);
}

if (r_even && !t_even) {
// Angular midpoint
double k1 = fine.angularSpacing(i_theta - 1);
double k2 = fine.angularSpacing(i_theta);

return (k1 * coarse_vals[coarse.index(i_r_coarse, t_even ? i_theta_coarse : i_theta_coarse)] +
k2 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]) /
(k1 + k2);
}

// Center of coarse cell
double h1 = fine.radialSpacing(i_r - 1);
double h2 = fine.radialSpacing(i_r);
double k1 = fine.angularSpacing(i_theta - 1);
double k2 = fine.angularSpacing(i_theta);

return (h1 * k1 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
h2 * k1 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)] +
h1 * k2 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)] +
h2 * k2 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse + 1)]) /
((h1 + h2) * (k1 + k2));
double result = 0;
Kokkos::parallel_reduce(
"prolongation_test", Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(0, 1),
KOKKOS_LAMBDA(int idx, double& local_result) {
if (r_even && t_even) {
// Node coincides with a coarse node
local_result = coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)];
}

else if (!r_even && t_even) {
// Radial midpoint
double h1 = fine.radialSpacing(i_r - 1);
double h2 = fine.radialSpacing(i_r);

local_result = (h1 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
h2 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)]) /
(h1 + h2);
}

else if (r_even && !t_even) {
// Angular midpoint
double k1 = fine.angularSpacing(i_theta - 1);
double k2 = fine.angularSpacing(i_theta);

local_result = (k1 * coarse_vals[coarse.index(i_r_coarse, t_even ? i_theta_coarse : i_theta_coarse)] +
k2 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)]) /
(k1 + k2);
}
else {
// Center of coarse cell
double h1 = fine.radialSpacing(i_r - 1);
double h2 = fine.radialSpacing(i_r);
double k1 = fine.angularSpacing(i_theta - 1);
double k2 = fine.angularSpacing(i_theta);

local_result = (h1 * k1 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse)] +
h2 * k1 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse)] +
h1 * k2 * coarse_vals[coarse.index(i_r_coarse, i_theta_coarse + 1)] +
h2 * k2 * coarse_vals[coarse.index(i_r_coarse + 1, i_theta_coarse + 1)]) /
((h1 + h2) * (k1 + k2));
}
},
result);

return result;
}

TEST(ProlongationTest, ProlongationMatchesStencil)
Expand All @@ -71,16 +80,12 @@ TEST(ProlongationTest, ProlongationMatchesStencil)

I.applyProlongation(coarse_grid, fine_grid, fine_result, coarse_values);

PolarGrid<Kokkos::HostSpace> h_fine_grid(fine_grid);
PolarGrid<Kokkos::HostSpace> h_coarse_grid(coarse_grid);

auto h_coarse_values = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, coarse_values);
auto h_fine_result = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, fine_result);
auto h_fine_result = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, fine_result);

for (int i_r = 0; i_r < fine_grid.nr(); ++i_r) {
for (int i_theta = 0; i_theta < fine_grid.ntheta(); ++i_theta) {
double expected = expected_value(h_coarse_grid, h_fine_grid, h_coarse_values, i_r, i_theta);
double got = h_fine_result[h_fine_grid.index(i_r, i_theta)];
double expected = expected_value(coarse_grid, fine_grid, coarse_values, i_r, i_theta);
double got = h_fine_result[fine_grid.index(i_r, i_theta)];
Comment thread
EmilyBourne marked this conversation as resolved.
ASSERT_NEAR(expected, got, 1e-10) << "Mismatch at (" << i_r << ", " << i_theta << ")";
}
}
Expand Down
Loading
Loading