Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Dec 2, 2024
1 parent eaf94d3 commit fdc6c0b
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 8 deletions.
14 changes: 7 additions & 7 deletions include/ddc/kernels/splines/spline_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ class SplineBuilder
private:
batched_interpolation_domain_type m_batched_interpolation_domain;

int m_offset;
int m_offset = 0;

double m_dx; // average cell size for normalization of derivatives

// interpolator specific
std::unique_ptr<ddc::detail::SplinesLinearProblem<exec_space>> matrix;

/// Calculate offset so that the matrix is diagonally dominant
int compute_offset(interpolation_domain_type const& interpolation_domain);
void compute_offset(interpolation_domain_type const& interpolation_domain, int& offset);

public:
/**
Expand All @@ -197,7 +197,6 @@ class SplineBuilder
std::optional<std::size_t> cols_per_chunk = std::nullopt,
std::optional<unsigned int> preconditioner_max_block_size = std::nullopt)
: m_batched_interpolation_domain(batched_interpolation_domain)
, m_offset(compute_offset(interpolation_domain()))
, m_dx((ddc::discrete_space<BSplines>().rmax() - ddc::discrete_space<BSplines>().rmin())
/ ddc::discrete_space<BSplines>().ncells())
{
Expand All @@ -206,6 +205,8 @@ class SplineBuilder
"Incompatible boundary conditions");
check_valid_grid();

compute_offset(interpolation_domain(), m_offset);

// Calculate block sizes
int lower_block_size;
int upper_block_size;
Expand Down Expand Up @@ -484,17 +485,17 @@ template <
ddc::BoundCond BcUpper,
SplineSolver Solver,
class... IDimX>
int SplineBuilder<
void SplineBuilder<
ExecSpace,
MemorySpace,
BSplines,
InterpolationDDim,
BcLower,
BcUpper,
Solver,
IDimX...>::compute_offset(interpolation_domain_type const& interpolation_domain)
IDimX...>::
compute_offset(interpolation_domain_type const& interpolation_domain, int& offset)
{
int offset;
if constexpr (bsplines_type::is_periodic()) {
// Calculate offset so that the matrix is diagonally dominant
std::array<double, bsplines_type::degree() + 1> values_ptr;
Expand All @@ -517,7 +518,6 @@ int SplineBuilder<
} else {
offset = 0;
}
return offset;
}

template <
Expand Down
8 changes: 7 additions & 1 deletion tests/splines/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ include(GoogleTest)
set(SPLINES_TEST_DEGREE_MIN 3 CACHE STRING "Minimum degree to test splines.")
set(SPLINES_TEST_DEGREE_MAX 3 CACHE STRING "Maximum degree to test splines.")

add_executable(splines_tests ../main.cpp knots_as_interpolation_points.cpp view.cpp)
add_executable(
splines_tests
../main.cpp
knots_as_interpolation_points.cpp
spline_builder.cpp
view.cpp
)

target_compile_features(splines_tests PUBLIC cxx_std_17)
target_link_libraries(splines_tests PUBLIC DDC::core DDC::splines GTest::gtest)
Expand Down
159 changes: 159 additions & 0 deletions tests/splines/spline_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (C) The DDC development team, see COPYRIGHT.md file
//
// SPDX-License-Identifier: MIT

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <stdexcept>
#include <tuple>

#include "ddc/discrete_space.hpp"
#include "ddc/non_uniform_point_sampling.hpp"
#if defined(BSPLINES_TYPE_NON_UNIFORM)
#include <vector>
#endif

#include <ddc/ddc.hpp>
#include <ddc/kernels/splines.hpp>

#include <gtest/gtest.h>

#include <Kokkos_Core.hpp>

#include "cosine_evaluator.hpp"
#include "spline_error_bounds.hpp"

struct DimX
{
static constexpr bool PERIODIC = true;
};

using CoordX = ddc::Coordinate<DimX>;

static constexpr std::size_t s_degree_x = 2;

struct BSplinesX : ddc::UniformBSplines<DimX, s_degree_x>
{
};

struct IDimX : ddc::NonUniformPointSampling<DimX>
{
};

TEST(SplineBuilder, ShortInterpolationGrid)
{
using execution_space = Kokkos::DefaultExecutionSpace;
using memory_space = Kokkos::HostSpace;

CoordX constexpr x0(0.);
CoordX constexpr xN(1.);
std::size_t constexpr ncells = 5;

ddc::init_discrete_space<BSplinesX>(x0, xN, ncells);

// One point missing
std::vector<double> const range {0.1, 0.3, 0.5, 0.7};

ddc::DiscreteDomain<IDimX> const interpolation_domain
= ddc::init_discrete_space<IDimX>(IDimX::init<IDimX>(range));

EXPECT_THROW(
(ddc::SplineBuilder<
execution_space,
memory_space,
BSplinesX,
IDimX,
ddc::BoundCond::PERIODIC,
ddc::BoundCond::PERIODIC,
ddc::SplineSolver::GINKGO,
IDimX>(interpolation_domain)),
std::runtime_error);
}

TEST(SplineBuilder, LongInterpolationGrid)
{
using execution_space = Kokkos::DefaultExecutionSpace;
using memory_space = Kokkos::HostSpace;

CoordX constexpr x0(0.);
CoordX constexpr xN(1.);
std::size_t constexpr ncells = 5;

ddc::init_discrete_space<BSplinesX>(x0, xN, ncells);

// One point too much
std::vector<double> const range {0.1, 0.3, 0.5, 0.7, 0.9, 0.95};

ddc::DiscreteDomain<IDimX> const interpolation_domain
= ddc::init_discrete_space<IDimX>(IDimX::init<IDimX>(range));

EXPECT_THROW(
(ddc::SplineBuilder<
execution_space,
memory_space,
BSplinesX,
IDimX,
ddc::BoundCond::PERIODIC,
ddc::BoundCond::PERIODIC,
ddc::SplineSolver::GINKGO,
IDimX>(interpolation_domain)),
std::runtime_error);
}

TEST(SplineBuilder, BadShapeInterpolationGrid)
{
using execution_space = Kokkos::DefaultExecutionSpace;
using memory_space = Kokkos::HostSpace;

CoordX constexpr x0(0.);
CoordX constexpr xN(1.);
std::size_t constexpr ncells = 5;

ddc::init_discrete_space<BSplinesX>(x0, xN, ncells);

// All points end up in the first cell ]0, 0.2[
std::vector<double> const range {0.1, 0.11, 0.12, 0.13, 0.14};

ddc::DiscreteDomain<IDimX> const interpolation_domain
= ddc::init_discrete_space<IDimX>(IDimX::init<IDimX>(range));

EXPECT_THROW(
(ddc::SplineBuilder<
execution_space,
memory_space,
BSplinesX,
IDimX,
ddc::BoundCond::PERIODIC,
ddc::BoundCond::PERIODIC,
ddc::SplineSolver::GINKGO,
IDimX>(interpolation_domain)),
std::runtime_error);
}

TEST(SplineBuilder, CorrectInterpolationGrid)
{
using execution_space = Kokkos::DefaultExecutionSpace;
using memory_space = Kokkos::HostSpace;

CoordX constexpr x0(0.);
CoordX constexpr xN(1.);
std::size_t constexpr ncells = 5;

ddc::init_discrete_space<BSplinesX>(x0, xN, ncells);

std::vector<double> const range {0.1, 0.11, 0.25, 0.23, 0.24};

ddc::DiscreteDomain<IDimX> const interpolation_domain
= ddc::init_discrete_space<IDimX>(IDimX::init<IDimX>(range));

EXPECT_NO_THROW((ddc::SplineBuilder<
execution_space,
memory_space,
BSplinesX,
IDimX,
ddc::BoundCond::PERIODIC,
ddc::BoundCond::PERIODIC,
ddc::SplineSolver::GINKGO,
IDimX>(interpolation_domain)));
}

0 comments on commit fdc6c0b

Please sign in to comment.