17#include <Kokkos_Core.hpp>
20#include "integrals.hpp"
21#include "math_tools.hpp"
22#include "spline_boundary_conditions.hpp"
23#include "splines_linear_problem_maker.hpp"
28
29
30
31
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
58 class InterpolationDDim,
73 using exec_space = ExecSpace;
76 using memory_space = MemorySpace;
79 using continuous_dimension_type =
typename InterpolationDDim::continuous_dimension_type;
82 using interpolation_discrete_dimension_type = InterpolationDDim;
85 using bsplines_type = BSplines;
88 using deriv_type =
ddc::
Deriv<continuous_dimension_type>;
91 using interpolation_domain_type =
ddc::
DiscreteDomain<interpolation_discrete_dimension_type>;
97
98
99
100
101
102
103 using batch_domain_type =
ddc::remove_dims_of_t<
104 batched_interpolation_domain_type,
105 interpolation_discrete_dimension_type>;
108
109
110
111
112
113
114 using batched_spline_domain_type =
ddc::replace_dim_of_t<
115 batched_interpolation_domain_type,
116 interpolation_discrete_dimension_type,
121
122
123
124
125
126
127 using batched_spline_tr_domain_type =
128 typename ddc::detail::convert_type_seq_to_discrete_domain_t<
ddc::type_seq_merge_t<
129 ddc::detail::TypeSeq<bsplines_type>,
130 ddc::type_seq_remove_t<
131 ddc::detail::TypeSeq<IDimX...>,
132 ddc::detail::TypeSeq<interpolation_discrete_dimension_type>>>>;
136
137
138
139
140
141
142 using batched_derivs_domain_type =
ddc::replace_dim_of_t<
143 batched_interpolation_domain_type,
144 interpolation_discrete_dimension_type,
148 static constexpr bool s_odd = BSplines::degree() % 2;
151 static constexpr int s_nbc_xmin = n_boundary_equations(BcLower, BSplines::degree());
154 static constexpr int s_nbc_xmax = n_boundary_equations(BcUpper, BSplines::degree());
166 batched_interpolation_domain_type m_batched_interpolation_domain;
173 std::unique_ptr<
ddc::detail::SplinesLinearProblem<exec_space>> matrix;
176 int compute_offset(interpolation_domain_type
const& interpolation_domain);
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
196 batched_interpolation_domain_type
const& batched_interpolation_domain,
197 std::optional<std::size_t> cols_per_chunk = std::nullopt,
198 std::optional<
unsigned int> preconditioner_max_block_size = std::nullopt)
199 : m_batched_interpolation_domain(batched_interpolation_domain)
201 , m_dx((
ddc::discrete_space<BSplines>().rmax() -
ddc::discrete_space<BSplines>().rmin())
202 /
ddc::discrete_space<BSplines>().ncells())
206 "Incompatible boundary conditions");
209 int lower_block_size;
210 int upper_block_size;
211 if constexpr (bsplines_type::is_uniform()) {
212 upper_block_size = compute_block_sizes_uniform(BcLower,
s_nbc_xmin);
213 lower_block_size = compute_block_sizes_uniform(BcUpper,
s_nbc_xmax);
215 upper_block_size = compute_block_sizes_non_uniform(BcLower,
s_nbc_xmin);
216 lower_block_size = compute_block_sizes_non_uniform(BcUpper,
s_nbc_xmax);
222 preconditioner_max_block_size);
229
230
231
241
242
243
244
248
249
250
251
252
253
256 return interpolation_domain_type(m_batched_interpolation_domain);
260
261
262
263
264
265
266
269 return m_batched_interpolation_domain;
273
274
275
276
277
278
285
286
287
288
289
290
293 return ddc::discrete_space<bsplines_type>().full_domain();
297
298
299
300
301
302
305 return ddc::replace_dim_of<
306 interpolation_discrete_dimension_type,
312
313
314
315
316
317
318 batched_spline_tr_domain_type batched_spline_tr_domain()
const noexcept
320 return batched_spline_tr_domain_type(
ddc::replace_dim_of<bsplines_type, bsplines_type>(
323 ddc::DiscreteElement<bsplines_type>(0),
325 matrix->required_number_of_rhs_rows()))));
330
331
332
333
334
335
338 return ddc::replace_dim_of<interpolation_discrete_dimension_type, deriv_type>(
341 ddc::DiscreteElement<deriv_type>(1),
346
347
348
349
350
351
354 return ddc::replace_dim_of<interpolation_discrete_dimension_type, deriv_type>(
357 ddc::DiscreteElement<deriv_type>(1),
362
363
364
365
366
367
368
369
370
371
372
373
374
375 [[
deprecated(
"Use quadrature_coefficients() instead.")]]
const ddc::detail::
376 SplinesLinearProblem<exec_space>&
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401 template <
class Layout>
403 ddc::
ChunkSpan<
double, batched_spline_domain_type, Layout, memory_space> spline,
404 ddc::
ChunkSpan<
double const, batched_interpolation_domain_type, Layout, memory_space>
407 ddc::
ChunkSpan<
double const, batched_derivs_domain_type, Layout, memory_space>>
411 ddc::
ChunkSpan<
double const, batched_derivs_domain_type, Layout, memory_space>>
413 = std::nullopt)
const;
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438 template <
class OutMemorySpace = MemorySpace>
457 static int compute_block_sizes_uniform(
ddc::
BoundCond bound_cond,
int nbc);
459 static int compute_block_sizes_non_uniform(
ddc::
BoundCond bound_cond,
int nbc);
461 void allocate_matrix(
462 int lower_block_size,
463 int upper_block_size,
464 std::optional<std::size_t> cols_per_chunk = std::nullopt,
465 std::optional<
unsigned int> preconditioner_max_block_size = std::nullopt);
467 void build_matrix_system();
474 class InterpolationDDim,
487 IDimX...>::compute_offset(interpolation_domain_type
const& interpolation_domain)
490 if constexpr (bsplines_type::is_periodic()) {
492 std::array<
double, bsplines_type::degree() + 1> values_ptr;
493 Kokkos::mdspan<
double, Kokkos::extents<std::size_t, bsplines_type::degree() + 1>>
const
494 values(values_ptr.data());
495 ddc::DiscreteElement<interpolation_discrete_dimension_type> start(
496 interpolation_domain.front());
497 auto jmin =
ddc::discrete_space<BSplines>()
498 .eval_basis(values,
ddc::coordinate(start + BSplines::degree()));
499 if constexpr (bsplines_type::degree() % 2 == 0) {
500 offset = jmin.uid() - start.uid() + bsplines_type::degree() / 2 - BSplines::degree();
502 int const mid = bsplines_type::degree() / 2;
503 offset = jmin.uid() - start.uid()
504 + (DDC_MDSPAN_ACCESS_OP(values, mid) > DDC_MDSPAN_ACCESS_OP(values, mid + 1)
507 - BSplines::degree();
519 class InterpolationDDim,
532 IDimX...>::compute_block_sizes_uniform(
ddc::
BoundCond const bound_cond,
int const nbc)
535 return static_cast<
int>(bsplines_type::degree()) / 2;
543 return static_cast<
int>(bsplines_type::degree()) - 1;
546 throw std::runtime_error(
"ddc::BoundCond not handled");
553 class InterpolationDDim,
566 IDimX...>::compute_block_sizes_non_uniform(
ddc::
BoundCond const bound_cond,
int const nbc)
569 return static_cast<
int>(bsplines_type::degree()) - 1;
576 throw std::runtime_error(
"ddc::BoundCond not handled");
583 class InterpolationDDim,
598 [[maybe_unused]]
int lower_block_size,
599 [[maybe_unused]]
int upper_block_size,
600 std::optional<std::size_t> cols_per_chunk,
601 std::optional<
unsigned int> preconditioner_max_block_size)
607
608
609
612 int upper_band_width;
613 if (bsplines_type::is_uniform()) {
614 upper_band_width = bsplines_type::degree() / 2;
616 upper_band_width = bsplines_type::degree() - 1;
618 if constexpr (bsplines_type::is_periodic()) {
619 matrix =
ddc::detail::SplinesLinearProblemMaker::make_new_periodic_band_matrix<
621 ddc::discrete_space<BSplines>().nbasis(),
624 bsplines_type::is_uniform());
626 matrix =
ddc::detail::SplinesLinearProblemMaker::
627 make_new_block_matrix_with_band_main_block<ExecSpace>(
628 ddc::discrete_space<BSplines>().nbasis(),
631 bsplines_type::is_uniform(),
636 matrix =
ddc::detail::SplinesLinearProblemMaker::make_new_sparse<ExecSpace>(
637 ddc::discrete_space<BSplines>().nbasis(),
639 preconditioner_max_block_size);
642 build_matrix_system();
644 matrix->setup_solver();
651 class InterpolationDDim,
664 IDimX...>::build_matrix_system()
668 std::array<
double, (bsplines_type::degree() / 2 + 1) * (bsplines_type::degree() + 1)>
671 derivs(derivs_ptr.data(),
672 bsplines_type::degree() + 1,
673 bsplines_type::degree() / 2 + 1);
674 ddc::discrete_space<BSplines>().eval_basis_and_n_derivs(
676 ddc::discrete_space<BSplines>().rmin(),
681 for (std::size_t i = 0; i < bsplines_type::degree() + 1; ++i) {
682 for (std::size_t j = 1; j < bsplines_type::degree() / 2 + 1; ++j) {
683 DDC_MDSPAN_ACCESS_OP(derivs, i, j) *=
ddc::detail::ipow(m_dx, j);
688 for (std::size_t i = 0; i <
s_nbc_xmin; ++i) {
689 for (std::size_t j = 0; j < bsplines_type::degree(); ++j) {
693 DDC_MDSPAN_ACCESS_OP(derivs, j, s_nbc_xmin - i - 1 + s_odd));
699 std::array<
double, bsplines_type::degree() + 1> values_ptr;
700 Kokkos::mdspan<
double, Kokkos::extents<std::size_t, bsplines_type::degree() + 1>>
const values(
705 auto jmin =
ddc::discrete_space<BSplines>().eval_basis(
707 ddc::coordinate(
ddc::DiscreteElement<interpolation_discrete_dimension_type>(ix)));
708 for (std::size_t s = 0; s < bsplines_type::degree() + 1; ++s) {
709 int const j =
ddc::detail::
710 modulo(
int(jmin.uid() - m_offset + s),
711 static_cast<
int>(
ddc::discrete_space<BSplines>().nbasis()));
712 matrix->set_element(ix.uid() - start +
s_nbc_xmin, j, DDC_MDSPAN_ACCESS_OP(values, s));
718 std::array<
double, (bsplines_type::degree() / 2 + 1) * (bsplines_type::degree() + 1)>
724 bsplines_type::degree() + 1,
725 bsplines_type::degree() / 2 + 1>>
const derivs(derivs_ptr.data());
727 ddc::discrete_space<BSplines>().eval_basis_and_n_derivs(
729 ddc::discrete_space<BSplines>().rmax(),
734 for (std::size_t i = 0; i < bsplines_type::degree() + 1; ++i) {
735 for (std::size_t j = 1; j < bsplines_type::degree() / 2 + 1; ++j) {
736 DDC_MDSPAN_ACCESS_OP(derivs, i, j) *=
ddc::detail::ipow(m_dx, j);
740 int const i0 =
ddc::discrete_space<BSplines>().nbasis() -
s_nbc_xmax;
741 int const j0 =
ddc::discrete_space<BSplines>().nbasis() - bsplines_type::degree();
742 for (std::size_t j = 0; j < bsplines_type::degree(); ++j) {
743 for (std::size_t i = 0; i <
s_nbc_xmax; ++i) {
744 matrix->set_element(i0 + i, j0 + j, DDC_MDSPAN_ACCESS_OP(derivs, j + 1, i + s_odd));
759template <
class Layout>
770 ddc::
ChunkSpan<
double, batched_spline_domain_type, Layout, memory_space> spline,
771 ddc::
ChunkSpan<
double const, batched_interpolation_domain_type, Layout, memory_space> vals,
774 batched_derivs_domain_type,
776 memory_space>>
const derivs_xmin,
779 batched_derivs_domain_type,
781 memory_space>>
const derivs_xmax)
const
783 assert(vals.
template extent<interpolation_discrete_dimension_type>()
784 == ddc::discrete_space<bsplines_type>().nbasis() - s_nbc_xmin - s_nbc_xmax);
786 assert((BcLower == ddc::BoundCond::HERMITE)
787 != (!derivs_xmin.has_value() || derivs_xmin->
template extent<deriv_type>() == 0));
788 assert((BcUpper == ddc::BoundCond::HERMITE)
789 != (!derivs_xmax.has_value() || derivs_xmax->
template extent<deriv_type>() == 0));
791 assert(ddc::DiscreteElement<deriv_type>(derivs_xmin->domain().front()).uid() == 1);
794 assert(ddc::DiscreteElement<deriv_type>(derivs_xmax->domain().front()).uid() == 1);
801 assert(derivs_xmin->
template extent<deriv_type>() == s_nbc_xmin);
802 auto derivs_xmin_values = *derivs_xmin;
803 auto const dx_proxy = m_dx;
804 ddc::parallel_for_each(
805 "ddc_splines_hermite_compute_lower_coefficients",
808 KOKKOS_LAMBDA(
typename batch_domain_type::discrete_element_type j) {
810 spline(
ddc::DiscreteElement<bsplines_type>(
s_nbc_xmin - i), j)
811 = derivs_xmin_values(
ddc::DiscreteElement<deriv_type>(i), j)
812 *
ddc::detail::ipow(dx_proxy, i +
s_odd - 1);
833 interpolation_discrete_dimension_type>())))]
834 .allocation_kokkos_view(),
835 vals.allocation_kokkos_view());
842 auto const& nbasis_proxy =
ddc::discrete_space<bsplines_type>().nbasis();
844 assert(derivs_xmax->
template extent<deriv_type>() == s_nbc_xmax);
845 auto derivs_xmax_values = *derivs_xmax;
846 auto const dx_proxy = m_dx;
847 ddc::parallel_for_each(
848 "ddc_splines_hermite_compute_upper_coefficients",
851 KOKKOS_LAMBDA(
typename batch_domain_type::discrete_element_type j) {
853 spline(
ddc::DiscreteElement<bsplines_type>(nbasis_proxy -
s_nbc_xmax - i),
855 = derivs_xmax_values(
ddc::DiscreteElement<deriv_type>(i + 1), j)
856 *
ddc::detail::ipow(dx_proxy, i +
s_odd);
862 auto const& offset_proxy = m_offset;
864 batched_spline_tr_domain(),
866 ddc::
ChunkSpan const spline_tr = spline_tr_alloc.span_view();
867 ddc::parallel_for_each(
868 "ddc_splines_transpose_rhs",
871 KOKKOS_LAMBDA(
typename batch_domain_type::discrete_element_type
const j) {
872 for (std::size_t i = 0; i < nbasis_proxy; ++i) {
873 spline_tr(
ddc::DiscreteElement<bsplines_type>(i), j)
874 = spline(
ddc::DiscreteElement<bsplines_type>(i + offset_proxy), j);
878 Kokkos::View<
double**, Kokkos::LayoutRight, exec_space>
const bcoef_section(
879 spline_tr.data_handle(),
880 static_cast<std::size_t>(spline_tr.
template extent<bsplines_type>()),
883 matrix->solve(bcoef_section,
false);
885 ddc::parallel_for_each(
886 "ddc_splines_transpose_back_rhs",
889 KOKKOS_LAMBDA(
typename batch_domain_type::discrete_element_type
const j) {
890 for (std::size_t i = 0; i < nbasis_proxy; ++i) {
891 spline(
ddc::DiscreteElement<bsplines_type>(i + offset_proxy), j)
892 = spline_tr(
ddc::DiscreteElement<bsplines_type>(i), j);
897 if (bsplines_type::is_periodic()) {
898 ddc::parallel_for_each(
899 "ddc_splines_periodic_rows_duplicate_rhs",
902 KOKKOS_LAMBDA(
typename batch_domain_type::discrete_element_type
const j) {
903 if (offset_proxy != 0) {
904 for (
int i = 0; i < offset_proxy; ++i) {
905 spline(
ddc::DiscreteElement<bsplines_type>(i), j) = spline(
906 ddc::DiscreteElement<bsplines_type>(nbasis_proxy + i),
909 for (std::size_t i = offset_proxy; i < bsplines_type::degree(); ++i) {
910 spline(
ddc::DiscreteElement<bsplines_type>(nbasis_proxy + i), j)
911 = spline(
ddc::DiscreteElement<bsplines_type>(i), j);
914 for (std::size_t i(0); i < bsplines_type::degree(); ++i) {
915 const ddc::DiscreteElement<bsplines_type> i_start(i);
916 const ddc::DiscreteElement<bsplines_type> i_end(nbasis_proxy + i);
918 spline(i_end, j) = spline(i_start, j);
933template <
class OutMemorySpace>
961 ddc::integrals(ExecSpace(), integral_bsplines.span_view());
964 ddc::
ChunkSpan const integral_bsplines_without_periodic_additional_bsplines
969 Kokkos::View<
double**, Kokkos::LayoutRight, MemorySpace>
const
970 integral_bsplines_mirror_with_additional_allocation(
971 "integral_bsplines_mirror_with_additional_allocation",
972 matrix->required_number_of_rhs_rows(),
976 Kokkos::View<
double*, Kokkos::LayoutRight, MemorySpace>
const integral_bsplines_mirror
978 subview(integral_bsplines_mirror_with_additional_allocation,
980 pair {
static_cast<std::size_t>(0),
981 integral_bsplines_without_periodic_additional_bsplines
987 integral_bsplines_mirror,
988 integral_bsplines_without_periodic_additional_bsplines.allocation_kokkos_view());
989 matrix->solve(integral_bsplines_mirror_with_additional_allocation,
true);
991 integral_bsplines_without_periodic_additional_bsplines.allocation_kokkos_view(),
992 integral_bsplines_mirror);
996 = integral_bsplines_without_periodic_additional_bsplines[
spline_domain().take_first(
998 ddc::
ChunkSpan const coefficients = integral_bsplines_without_periodic_additional_bsplines
1005 = integral_bsplines_without_periodic_additional_bsplines
1013 ddc::parallel_for_each(
1015 coefficients_derivs_xmin.domain(),
1016 KOKKOS_LAMBDA(
ddc::DiscreteElement<bsplines_type> i) {
1017 ddc::Coordinate<continuous_dimension_type>
const dx
1018 =
ddc::distance_at_right(interpolation_domain_proxy.front() + 1);
1019 coefficients_derivs_xmin(i) *=
ddc::detail::
1021 static_cast<std::size_t>(get<bsplines_type>(
1022 i - coefficients_derivs_xmin.domain().front() + 1)));
1024 ddc::parallel_for_each(
1026 coefficients_derivs_xmax.domain(),
1027 KOKKOS_LAMBDA(
ddc::DiscreteElement<bsplines_type> i) {
1028 ddc::Coordinate<continuous_dimension_type>
const dx
1029 =
ddc::distance_at_left(interpolation_domain_proxy.back() - 1);
1030 coefficients_derivs_xmax(i) *=
ddc::detail::
1032 static_cast<std::size_t>(get<bsplines_type>(
1033 i - coefficients_derivs_xmax.domain().front() + 1)));
1037 ddc::
Chunk coefficients_derivs_xmin_out(
1039 ddc::DiscreteElement<deriv_type>(1),
1045 coefficients.size())),
1047 ddc::
Chunk coefficients_derivs_xmax_out(
1049 ddc::DiscreteElement<deriv_type>(1),
1053 coefficients_derivs_xmin_out.allocation_kokkos_view(),
1054 coefficients_derivs_xmin.allocation_kokkos_view());
1056 coefficients_out.allocation_kokkos_view(),
1057 coefficients.allocation_kokkos_view());
1059 coefficients_derivs_xmax_out.allocation_kokkos_view(),
1060 coefficients_derivs_xmax.allocation_kokkos_view());
1061 return std::make_tuple(
1062 std::move(coefficients_derivs_xmin_out),
1063 std::move(coefficients_out),
1064 std::move(coefficients_derivs_xmax_out));
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
A class for creating a spline approximation of a function.
batched_spline_domain_type batched_spline_domain() const noexcept
Get the whole domain on which spline coefficients are defined.
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.
static constexpr bool s_odd
Indicates if the degree of the splines is odd or even.
void operator()(ddc::ChunkSpan< double, batched_spline_domain_type, Layout, memory_space > spline, ddc::ChunkSpan< double const, batched_interpolation_domain_type, Layout, memory_space > vals, std::optional< ddc::ChunkSpan< double const, batched_derivs_domain_type, Layout, memory_space > > derivs_xmin=std::nullopt, std::optional< ddc::ChunkSpan< double const, batched_derivs_domain_type, Layout, memory_space > > derivs_xmax=std::nullopt) const
Compute a spline approximation of a function.
batched_interpolation_domain_type batched_interpolation_domain() const noexcept
Get the whole domain representing interpolation points.
static constexpr int s_nbc_xmax
The number of equations defining the boundary condition at the upper bound.
~SplineBuilder()=default
Destructs.
SplineBuilder(SplineBuilder &&x)=default
Move-constructs.
static constexpr ddc::BoundCond s_bc_xmax
The boundary condition implemented at the upper bound.
SplineBuilder & operator=(SplineBuilder const &x)=delete
Copy-assignment is deleted.
SplineBuilder(batched_interpolation_domain_type 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 batched_interpolation_domain.
ddc::DiscreteDomain< bsplines_type > spline_domain() const noexcept
Get the 1D domain on which spline coefficients are defined.
interpolation_domain_type interpolation_domain() const noexcept
Get the domain for the 1D interpolation mesh used by this class.
static constexpr ddc::BoundCond s_bc_xmin
The boundary condition implemented at the lower bound.
batched_derivs_domain_type batched_derivs_xmax_domain() const noexcept
Get the whole domain on which derivatives on upper boundary are defined.
const ddc::detail::SplinesLinearProblem< exec_space > & get_interpolation_matrix() const noexcept
Get the interpolation matrix.
static constexpr int s_nbc_xmin
The number of equations defining the boundary condition at the lower bound.
SplineBuilder(SplineBuilder const &x)=delete
Copy-constructor is deleted.
batch_domain_type batch_domain() const noexcept
Get the batch domain.
static constexpr SplineSolver s_spline_solver
The SplineSolver giving the backend used to perform the spline approximation.
SplineBuilder & operator=(SplineBuilder &&x)=default
Move-assigns.
batched_derivs_domain_type batched_derivs_xmin_domain() const noexcept
Get the whole domain on which derivatives on lower boundary are defined.
The top-level namespace of DDC.
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)
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)
A templated struct representing a discrete dimension storing the derivatives of a function along a co...