DDC 0.10.0
Loading...
Searching...
No Matches
splines_linear_problem_maker.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <algorithm>
8#include <cassert>
9#include <cstddef>
10#include <memory>
11#include <optional>
12#include <utility>
13
22
23namespace ddc::detail {
24
25/**
26 * @brief A static factory for SplinesLinearProblem classes.
27 */
28class SplinesLinearProblemMaker
29{
30public:
31 /**
32 * @brief Construct a dense matrix
33 *
34 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
35 * @param n The size of one of the dimensions of the square matrix.
36 *
37 * @return The SplinesLinearProblem instance.
38 */
39 template <typename ExecSpace>
40 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_dense(int const n)
41 {
42 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
43 }
44
45 /**
46 * @brief Construct a band matrix
47 *
48 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
49 * @param n The size of one of the dimensions of the square matrix.
50 * @param kl The number of subdiagonals.
51 * @param ku The number of superdiagonals.
52 * @param pds A boolean indicating if the matrix is positive-definite symmetric or not.
53 *
54 * @return The SplinesLinearProblem instance.
55 */
56 template <typename ExecSpace>
57 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_band(
58 int const n,
59 int const kl,
60 int const ku,
61 bool const pds)
62 {
63 if (kl == ku && kl == 1 && pds) {
64 return std::make_unique<SplinesLinearProblemPDSTridiag<ExecSpace>>(n);
65 }
66
67 if (kl == ku && pds) {
68 return std::make_unique<SplinesLinearProblemPDSBand<ExecSpace>>(n, kl);
69 }
70
71 if (2 * kl + ku + 1 >= n) {
72 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
73 }
74
75 return std::make_unique<SplinesLinearProblemBand<ExecSpace>>(n, kl, ku);
76 }
77
78 /**
79 * @brief Construct a 2x2-blocks or 3x3-blocks linear problem with band "main" block (the one called
80 * Q in SplinesLinearProblem2x2Blocks and SplinesLinearProblem3x3Blocks).
81 *
82 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
83 * @param n The size of one of the dimensions of the whole square matrix.
84 * @param kl The number of subdiagonals in the band block.
85 * @param ku The number of superdiagonals in the band block.
86 * @param pds A boolean indicating if the band block is positive-definite symmetric or not.
87 * @param bottom_right_size The size of one of the dimensions of the bottom-right square block.
88 * @param top_left_size The size of one of the dimensions of the top-left square block.
89 *
90 * @return The SplinesLinearProblem instance.
91 */
92 template <typename ExecSpace>
93 static std::unique_ptr<SplinesLinearProblem<ExecSpace>>
94 make_new_block_matrix_with_band_main_block(
95 int const n,
96 int const kl,
97 int const ku,
98 bool const pds,
99 int const bottom_right_size,
100 int const top_left_size = 0)
101 {
102 int const main_size = n - top_left_size - bottom_right_size;
103 std::unique_ptr<SplinesLinearProblem<ExecSpace>> main_block
104 = make_new_band<ExecSpace>(main_size, kl, ku, pds);
105 if (top_left_size == 0) {
106 return std::make_unique<
107 SplinesLinearProblem2x2Blocks<ExecSpace>>(n, std::move(main_block));
108 }
109 return std::make_unique<
110 SplinesLinearProblem3x3Blocks<ExecSpace>>(n, top_left_size, std::move(main_block));
111 }
112
113 /**
114 * @brief Construct a 2x2-blocks linear problem with band "main" block (the one called
115 * Q in SplinesLinearProblem2x2Blocks) and other blocks containing the "periodic parts" of
116 * a periodic band matrix.
117 *
118 * It simply calls make_new_block_matrix_with_band_main_block with bottom_size being
119 * max(kl, ku) (except if the allocation would be higher than instantiating a SplinesLinearProblemDense).
120 *
121 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
122 * @param n The size of one of the dimensions of the whole square matrix.
123 * @param kl The number of subdiagonals in the band block.
124 * @param ku The number of superdiagonals in the band block.
125 * @param pds A boolean indicating if the band block is positive-definite symmetric or not.
126 *
127 * @return The SplinesLinearProblem instance.
128 */
129 template <typename ExecSpace>
130 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_periodic_band_matrix(
131 int const n,
132 int const kl,
133 int const ku,
134 bool const pds)
135 {
136 assert(kl < n);
137 assert(ku < n);
138 int const bottom_size = std::max(kl, ku);
139 int const top_size = n - bottom_size;
140
141 if (bottom_size * (n + top_size) + (2 * kl + ku + 1) * top_size >= n * n) {
142 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
143 }
144
145 return make_new_block_matrix_with_band_main_block<ExecSpace>(n, kl, ku, pds, bottom_size);
146 }
147
148 /**
149 * @brief Construct a sparse matrix
150 *
151 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
152 * @param n The size of one of the dimensions of the square matrix.
153 * @param cols_per_chunk A parameter used by the slicer (internal to the solver) to define the size
154 * of a chunk of right-hand sides of the linear problem to be computed in parallel (chunks are treated
155 * by the linear solver one-after-the-other).
156 * This value is optional. If no value is provided then the default value is chosen by the requested solver.
157 *
158 * @param preconditioner_max_block_size A parameter used by the slicer (internal to the solver) to
159 * define the size of a block used by the Block-Jacobi preconditioner.
160 * This value is optional. If no value is provided then the default value is chosen by the requested solver.
161 *
162 * @return The SplinesLinearProblem instance.
163 */
164 template <typename ExecSpace>
165 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_sparse(
166 int const n,
167 std::optional<std::size_t> cols_per_chunk = std::nullopt,
168 std::optional<unsigned int> preconditioner_max_block_size = std::nullopt)
169 {
170 return std::make_unique<SplinesLinearProblemSparse<
171 ExecSpace>>(n, cols_per_chunk, preconditioner_max_block_size);
172 }
173};
174
175} // namespace ddc::detail
friend class ChunkSpan
friend class Chunk
Definition chunk.hpp:83
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
A class which provides helper functions to initialise the Greville points from a B-Spline definition.
static ddc::DiscreteDomain< Sampling > get_domain()
Get the domain which gives us access to all of the Greville points.
static auto get_sampling()
Get the UniformPointSampling defining the Greville points.
Helper class for the initialisation of the mesh of interpolation points.
static auto get_sampling()
Get the sampling of interpolation points.
static ddc::DiscreteDomain< Sampling > get_domain()
Get the domain which can be used to access the interpolation points in the sampling.
Storage class of the static attributes of the discrete dimension.
Impl & operator=(Impl &&x)=default
Move-assigns.
Impl(RandomIt breaks_begin, RandomIt breaks_end)
Constructs an Impl by iterating over a range of break points from begin to end.
KOKKOS_INLINE_FUNCTION ddc::Coordinate< CDim > rmin() const noexcept
Returns the coordinate of the first break point of the domain on which the B-splines are defined.
Impl(std::vector< ddc::Coordinate< CDim > > const &breaks)
Constructs an Impl using a std::vector.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis(DSpan1D values, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-splines at a given coordinate.
KOKKOS_INLINE_FUNCTION std::size_t size() const noexcept
Returns the number of elements necessary to construct a spline representation of a function.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Copy-constructs from another Impl with a different Kokkos memory space.
~Impl()=default
Destructs.
KOKKOS_INLINE_FUNCTION ddc::DiscreteDomain< knot_discrete_dimension_type > break_point_domain() const
Returns the discrete domain which describes the break points.
KOKKOS_INLINE_FUNCTION ddc::DiscreteElement< knot_discrete_dimension_type > get_last_support_knot(discrete_element_type const &ix) const
Returns the coordinate of the last support knot associated to a DiscreteElement identifying a B-splin...
Impl(Impl &&x)=default
Move-constructs.
Impl(std::initializer_list< ddc::Coordinate< CDim > > breaks)
Constructs an Impl using a brace-list, i.e.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis_and_n_derivs(ddc::DSpan2D derivs, ddc::Coordinate< CDim > const &x, std::size_t n) const
Evaluates non-zero B-spline values and derivatives at a given coordinate.
KOKKOS_INLINE_FUNCTION std::size_t ncells() const noexcept
Returns the number of cells over which the B-splines are defined.
KOKKOS_INLINE_FUNCTION discrete_domain_type full_domain() const
Returns the discrete domain including eventual additional B-splines in the periodic case.
KOKKOS_INLINE_FUNCTION ddc::DiscreteElement< knot_discrete_dimension_type > get_first_support_knot(discrete_element_type const &ix) const
Returns the coordinate of the first support knot associated to a DiscreteElement identifying a B-spli...
KOKKOS_INLINE_FUNCTION std::size_t npoints() const noexcept
The number of break points.
KOKKOS_INLINE_FUNCTION std::size_t nbasis() const noexcept
Returns the number of basis functions.
Impl(Impl const &x)=default
Copy-constructs.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_deriv(DSpan1D derivs, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-spline derivatives at a given coordinate.
KOKKOS_INLINE_FUNCTION double length() const noexcept
Returns the length of the domain.
Impl & operator=(Impl const &x)=default
Copy-assigns.
KOKKOS_INLINE_FUNCTION ddc::Coordinate< CDim > rmax() const noexcept
Returns the coordinate of the last break point of the domain on which the B-splines are defined.
The type of a non-uniform 1D spline basis (B-spline).
static constexpr std::size_t degree() noexcept
The degree of B-splines.
static constexpr bool is_periodic() noexcept
Indicates if the B-splines are periodic or not.
static constexpr bool is_uniform() noexcept
Indicates if the B-splines are uniform or not (this is not the case here).
NonUniformPointSampling models a non-uniform discretization of the CDim segment .
A class for creating a spline approximation of a function.
batched_derivs_domain_type< BatchedInterpolationDDom > batched_derivs_xmax_domain(BatchedInterpolationDDom const &batched_interpolation_domain) const noexcept
Get the whole domain on which derivatives on upper boundary are defined.
static constexpr SplineSolver s_spline_solver
The SplineSolver giving the backend used to perform the spline approximation.
batch_domain_type< BatchedInterpolationDDom > batch_domain(BatchedInterpolationDDom const &batched_interpolation_domain) const noexcept
Get the batch domain.
std::tuple< ddc::Chunk< double, ddc::DiscreteDomain< ddc::Deriv< typename InterpolationDDim::continuous_dimension_type > >, ddc::KokkosAllocator< double, OutMemorySpace > >, ddc::Chunk< double, ddc::DiscreteDomain< InterpolationDDim >, ddc::KokkosAllocator< double, OutMemorySpace > >, ddc::Chunk< double, ddc::DiscreteDomain< ddc::Deriv< typename InterpolationDDim::continuous_dimension_type > >, ddc::KokkosAllocator< double, OutMemorySpace > > > quadrature_coefficients() const
Compute the quadrature coefficients associated to the b-splines used by this SplineBuilder.
SplineBuilder & operator=(SplineBuilder const &x)=delete
Copy-assignment is deleted.
SplineBuilder(SplineBuilder &&x)=default
Move-constructs.
SplineBuilder(interpolation_domain_type const &interpolation_domain, std::optional< std::size_t > cols_per_chunk=std::nullopt, std::optional< unsigned int > preconditioner_max_block_size=std::nullopt)
Build a SplineBuilder acting on interpolation_domain.
static constexpr ddc::BoundCond s_bc_xmin
The boundary condition implemented at the lower bound.
static constexpr int s_nbc_xmin
The number of equations defining the boundary condition at the lower bound.
SplineBuilder & operator=(SplineBuilder &&x)=default
Move-assigns.
BatchedInterpolationDDom batched_interpolation_domain(BatchedInterpolationDDom const &batched_interpolation_domain) const noexcept
Get the whole domain representing interpolation points.
SplineBuilder(SplineBuilder const &x)=delete
Copy-constructor is deleted.
SplineBuilder(BatchedInterpolationDDom const &batched_interpolation_domain, std::optional< std::size_t > cols_per_chunk=std::nullopt, std::optional< unsigned int > preconditioner_max_block_size=std::nullopt)
Build a SplineBuilder acting on the interpolation domain contained by batched_interpolation_domain.
static constexpr bool s_odd
Indicates if the degree of the splines is odd or even.
interpolation_domain_type interpolation_domain() const noexcept
Get the domain for the 1D interpolation mesh used by this class.
batched_derivs_domain_type< BatchedInterpolationDDom > batched_derivs_xmin_domain(BatchedInterpolationDDom const &batched_interpolation_domain) const noexcept
Get the whole domain on which derivatives on lower boundary are defined.
static constexpr ddc::BoundCond s_bc_xmax
The boundary condition implemented at the upper bound.
static constexpr int s_nbc_xmax
The number of equations defining the boundary condition at the upper bound.
void operator()(ddc::ChunkSpan< double, batched_spline_domain_type< BatchedInterpolationDDom >, Layout, memory_space > spline, ddc::ChunkSpan< double const, BatchedInterpolationDDom, Layout, memory_space > vals, std::optional< ddc::ChunkSpan< double const, batched_derivs_domain_type< BatchedInterpolationDDom >, Layout, memory_space > > derivs_xmin=std::nullopt, std::optional< ddc::ChunkSpan< double const, batched_derivs_domain_type< BatchedInterpolationDDom >, Layout, memory_space > > derivs_xmax=std::nullopt) const
Compute a spline approximation of a function.
batched_spline_domain_type< BatchedInterpolationDDom > batched_spline_domain(BatchedInterpolationDDom const &batched_interpolation_domain) const noexcept
Get the whole domain on which spline coefficients are defined.
ddc::DiscreteDomain< bsplines_type > spline_domain() const noexcept
Get the 1D domain on which spline coefficients are defined.
~SplineBuilder()=default
Destructs.
Storage class of the static attributes of the discrete dimension.
KOKKOS_INLINE_FUNCTION ddc::DiscreteElement< knot_discrete_dimension_type > get_last_support_knot(discrete_element_type const &ix) const
Returns the coordinate of the last support knot associated to a DiscreteElement identifying a B-splin...
Impl(ddc::Coordinate< CDim > rmin, ddc::Coordinate< CDim > rmax, std::size_t ncells)
Constructs a spline basis (B-splines) with n equidistant knots over .
KOKKOS_INLINE_FUNCTION ddc::Coordinate< CDim > rmax() const noexcept
Returns the coordinate of the upper bound of the domain on which the B-splines are defined.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis(DSpan1D values, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-splines at a given coordinate.
KOKKOS_INLINE_FUNCTION ddc::DiscreteDomain< knot_discrete_dimension_type > break_point_domain() const
Returns the discrete domain which describes the break points.
KOKKOS_INLINE_FUNCTION ddc::Coordinate< CDim > rmin() const noexcept
Returns the coordinate of the lower bound of the domain on which the B-splines are defined.
~Impl()=default
Destructs.
KOKKOS_INLINE_FUNCTION std::size_t nbasis() const noexcept
Returns the number of basis functions.
Impl(Impl const &x)=default
Copy-constructs.
KOKKOS_INLINE_FUNCTION std::size_t size() const noexcept
Returns the number of elements necessary to construct a spline representation of a function.
Impl(Impl &&x)=default
Move-constructs.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis_and_n_derivs(ddc::DSpan2D derivs, ddc::Coordinate< CDim > const &x, std::size_t n) const
Evaluates non-zero B-spline values and derivatives at a given coordinate.
KOKKOS_INLINE_FUNCTION ddc::DiscreteElement< knot_discrete_dimension_type > get_first_support_knot(discrete_element_type const &ix) const
Returns the coordinate of the first support knot associated to a DiscreteElement identifying a B-spli...
KOKKOS_INLINE_FUNCTION double length() const noexcept
Returns the length of the domain.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Copy-constructs from another Impl with a different Kokkos memory space.
KOKKOS_INLINE_FUNCTION std::size_t ncells() const noexcept
Returns the number of cells over which the B-splines are defined.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_deriv(DSpan1D derivs, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-spline derivatives at a given coordinate.
Impl & operator=(Impl &&x)=default
Move-assigns.
KOKKOS_INLINE_FUNCTION discrete_domain_type full_domain() const
Returns the discrete domain including eventual additional B-splines in the periodic case.
Impl & operator=(Impl const &x)=default
Copy-assigns.
The type of a uniform 1D spline basis (B-spline).
static constexpr bool is_uniform() noexcept
Indicates if the B-splines are uniform or not (this is the case here).
static constexpr bool is_periodic() noexcept
Indicates if the B-splines are periodic or not.
static constexpr std::size_t degree() noexcept
The degree of B-splines.
UniformPointSampling models a uniform discretization of the provided continuous dimension.
The top-level namespace of DDC.
constexpr int n_boundary_equations(ddc::BoundCond const bc, std::size_t const degree)
Return the number of equations needed to describe a given boundary condition.
constexpr bool is_uniform_bsplines_v
Indicates if a tag corresponds to uniform B-splines or not.
BoundCond
An enum representing a spline boundary condition.
@ GREVILLE
Use Greville points instead of conditions on derivative for B-Spline interpolation.
@ HERMITE
Hermite boundary condition.
@ PERIODIC
Periodic boundary condition u(1)=u(n)
ddc::ChunkSpan< double, ddc::DiscreteDomain< DDim >, Layout, MemorySpace > integrals(ExecSpace const &execution_space, ddc::ChunkSpan< double, ddc::DiscreteDomain< DDim >, Layout, MemorySpace > int_vals)
Compute the integrals of the B-splines.
SplineSolver
An enum determining the backend solver of a SplineBuilder or SplineBuilder2d.
@ LAPACK
Enum member to identify the LAPACK-based solver (direct method)
@ GINKGO
Enum member to identify the Ginkgo-based solver (iterative method)
constexpr bool is_non_uniform_bsplines_v
Indicates if a tag corresponds to non-uniform B-splines or not.
A templated struct representing a discrete dimension storing the derivatives of a function along a co...
Definition deriv.hpp:15
If the type DDim is a B-spline, defines type to the discrete dimension of the associated knots.
ConstantExtrapolationRule(ddc::Coordinate< DimI > eval_pos, ddc::Coordinate< DimNI > eval_pos_not_interest_min, ddc::Coordinate< DimNI > eval_pos_not_interest_max)
Instantiate a ConstantExtrapolationRule.
KOKKOS_FUNCTION double operator()(CoordType coord_extrap, ddc::ChunkSpan< double const, ddc::DiscreteDomain< BSplines1, BSplines2 >, Layout, MemorySpace > const spline_coef) const
Get the value of the function on B-splines at a coordinate outside the domain.
ConstantExtrapolationRule(ddc::Coordinate< DimI > eval_pos)
Instantiate a ConstantExtrapolationRule.
KOKKOS_FUNCTION double operator()(CoordType pos, ddc::ChunkSpan< double const, ddc::DiscreteDomain< BSplines >, Layout, MemorySpace > const spline_coef) const
Get the value of the function on B-splines at a coordinate outside the domain.
ConstantExtrapolationRule(ddc::Coordinate< DimI > eval_pos)
Instantiate a ConstantExtrapolationRule.
A functor describing a null extrapolation boundary value for 1D spline evaluator.
KOKKOS_FUNCTION double operator()(CoordType, ChunkSpan) const
Evaluates the spline at a coordinate outside of the domain.
KOKKOS_FUNCTION double operator()(CoordType, ChunkSpan) const