DDC 0.17.0
Loading...
Searching...
No Matches
constant_extrapolation_rule.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 <array>
8#include <cstddef>
9#include <type_traits>
10
11#include <ddc/ddc.hpp>
12
13#include <Kokkos_Core.hpp>
14
15namespace ddc {
16
17/**
18 * @brief A functor for describing a spline boundary value by a constant extrapolation for 2D evaluator.
19 *
20 * To define the value of a function on B-splines out of the domain, we here use a constant
21 * extrapolation on the edge.
22 */
23template <class DimI>
25{
26private:
27 ddc::Coordinate<DimI> m_eval_pos;
28
29public:
30 /**
31 * @brief Instantiate a ConstantExtrapolationRule.
32 *
33 * The boundary value will be the same as at the coordinate given in a dimension given.
34 * The dimension of the input defines the dimension of the boundary condition.
35 * No second and third parameters are needed in case of periodic splines on the
36 * dimension off-interest (the complementary dimension of the boundary condition).
37 *
38 * @param[in] eval_pos Coordinate in the dimension given inside the domain where we will evaluate each points outside the domain.
39 */
40 explicit ConstantExtrapolationRule(ddc::Coordinate<DimI> eval_pos) : m_eval_pos(eval_pos) {}
41
42 /**
43 * @brief Get the value of the function on B-splines at a coordinate outside the domain.
44 *
45 * In the dimension defined in the constructor Dim1 (or Dim2), it sets the coordinate pos_1 (or pos_2)
46 * given at the m_eval_pos coordinate if it is outside the domain.
47 * If the coordinate on the complementary dimension of the boundary condition dimension ddc::Coordinate<DimNI>(coord_extrap) is
48 * outside the domain, then it also sets the coordinate at eval_pos_not_interest_min
49 * (if ddc::Coordinate<DimNI>(coord_extrap) @f$ < @f$ eval_pos_not_interest_min) or
50 * at eval_pos_not_interest_max (if ddc::Coordinate<DimNI>(coord_extrap) @f$ > @f$ eval_pos_not_interest_max).
51 *
52 * @param[in] coord_extrap The coordinates where we want to evaluate the function on B-splines
53 * @param[in] spline_coef The coefficients of the function on B-splines.
54 *
55 *@return A Real with the value of the function on B-splines evaluated at the coordinate.
56 */
57 template <class CoordType, class... BSplines, class Layout, class MemorySpace>
58 KOKKOS_FUNCTION Real operator()(
59 CoordType coord_extrap,
60 ddc::ChunkSpan<Real const, ddc::DiscreteDomain<BSplines...>, Layout, MemorySpace> const
61 spline_coef) const
62 {
63 static_assert(in_tags_v<DimI, to_type_seq_t<CoordType>>);
64 using TypeSeqBSplines = ddc::TypeSeq<BSplines...>;
65
66 ddc::Coordinate<DimI, typename BSplines::continuous_dimension_type...> const coord_eval(
67 get_eval_pos<BSplines>(ddc::select<typename BSplines::continuous_dimension_type>(
68 coord_extrap))...);
69
70 auto vals_ptr = cexa::make_tuple(std::array<Real, BSplines::degree() + 1> {}...);
71 auto const vals = cexa::make_tuple(
72 Kokkos::mdspan<Real, Kokkos::extents<std::size_t, BSplines::degree() + 1>>(
73 cexa::get<ddc::type_seq_rank_v<BSplines, TypeSeqBSplines>>(vals_ptr)
74 .data())...);
75
76 auto const jmin = cexa::make_tuple(
77 ddc::discrete_space<BSplines>().eval_basis(
78 cexa::get<ddc::type_seq_rank_v<BSplines, TypeSeqBSplines>>(vals),
79 ddc::Coordinate<typename BSplines::continuous_dimension_type>(
80 coord_eval))...);
81
82 static constexpr std::size_t dimension = sizeof...(BSplines);
83
84 Real y = 0.0;
85 for_each(
86 std::array<std::size_t, dimension> {(BSplines::degree() + 1)...},
87 [&](std::array<std::size_t, dimension> idx) {
88 y += spline_coef(
89 ddc::DiscreteElement<BSplines...>((
90 cexa::get<ddc::type_seq_rank_v<BSplines, TypeSeqBSplines>>(
91 jmin)
92 + idx[ddc::type_seq_rank_v<
93 BSplines,
94 TypeSeqBSplines>])...))
95 * (cexa::get<ddc::type_seq_rank_v<BSplines, TypeSeqBSplines>>(
96 vals)[idx[ddc::type_seq_rank_v<BSplines, TypeSeqBSplines>]]
97 * ...);
98 });
99
100 return y;
101 }
102
103private:
104 template <class BSplinesNI>
105 KOKKOS_FUNCTION ddc::Coordinate<typename BSplinesNI::continuous_dimension_type> get_eval_pos(
106 ddc::Coordinate<typename BSplinesNI::continuous_dimension_type> coord_extrap) const
107 {
108 if constexpr (std::is_same_v<typename BSplinesNI::continuous_dimension_type, DimI>) {
109 return m_eval_pos;
110 } else {
111 if constexpr (BSplinesNI::is_periodic()) {
112 return ddc::Coordinate<typename BSplinesNI::continuous_dimension_type>(
113 coord_extrap);
114 } else {
115 return Kokkos::
116 clamp(ddc::Coordinate<typename BSplinesNI::continuous_dimension_type>(
117 coord_extrap),
118 ddc::discrete_space<BSplinesNI>().rmin(),
119 ddc::discrete_space<BSplinesNI>().rmax());
120 }
121 }
122 }
123
124 template <std::size_t N, class Functor, class... Is>
125 KOKKOS_FUNCTION static void for_each(
126 std::array<std::size_t, N> const& bounds,
127 Functor const& f,
128 Is... is)
129 {
130 static constexpr std::size_t I = sizeof...(Is);
131 if constexpr (I == N) {
132 f(std::array<std::size_t, N> {is...});
133 } else {
134 for (std::size_t i = 0; i < bounds[I]; ++i) {
135 for_each(bounds, f, is..., i);
136 }
137 }
138 }
139};
140
141} // namespace ddc
friend class ChunkSpan
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
Storage class of the static attributes of the discrete dimension.
KOKKOS_INLINE_FUNCTION std::size_t ncells() const noexcept
Returns the number of cells over which the B-splines are defined.
Impl(Impl &&x)=default
Move-constructs.
KOKKOS_INLINE_FUNCTION std::size_t nbasis() const noexcept
Returns the number of basis functions.
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...
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis_and_n_derivs(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 2 > > derivs, ddc::Coordinate< CDim > const &x, std::size_t n) const
Evaluates non-zero B-spline values and derivatives at a given coordinate.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Copy-constructs from another Impl with a different Kokkos memory space.
Impl(Impl const &x)=default
Copy-constructs.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_deriv(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 1 > > derivs, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-spline derivatives 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.
Impl & operator=(Impl const &x)=default
Copy-assigns.
KOKKOS_INLINE_FUNCTION Real length() const noexcept
Returns the length of the domain.
KOKKOS_INLINE_FUNCTION std::size_t npoints() const noexcept
The number of break points.
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...
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 discrete_domain_type full_domain() const
Returns the discrete domain including eventual additional B-splines in the periodic case.
KOKKOS_INLINE_FUNCTION std::size_t size() const noexcept
Returns the number of elements necessary to construct a spline representation of a function.
Impl(std::initializer_list< ddc::Coordinate< CDim > > breaks)
Constructs an Impl using a brace-list, i.e.
Impl & operator=(Impl &&x)=default
Move-assigns.
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.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 1 > > values, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-splines at a given coordinate.
~Impl()=default
Destructs.
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.
Impl(std::vector< ddc::Coordinate< CDim > > const &breaks)
Constructs an Impl using a std::vector.
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 .
Storage class of the static attributes of the discrete dimension.
KOKKOS_INLINE_FUNCTION std::size_t ncells() const noexcept
Returns the number of cells over which the B-splines are defined.
KOKKOS_INLINE_FUNCTION Real length() const noexcept
Returns the length of the domain.
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(Impl const &x)=default
Copy-constructs.
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 discrete_element_type eval_deriv(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 1 > > derivs, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-spline derivatives at a given coordinate.
~Impl()=default
Destructs.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Copy-constructs from another Impl with a different Kokkos memory space.
KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis_and_n_derivs(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 2 > > derivs, ddc::Coordinate< CDim > const &x, std::size_t n) const
Evaluates non-zero B-spline values and derivatives at a given coordinate.
Impl & operator=(Impl &&x)=default
Move-assigns.
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 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...
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 discrete_element_type eval_basis(Kokkos::mdspan< double, Kokkos::dextents< std::size_t, 1 > > values, ddc::Coordinate< CDim > const &x) const
Evaluates non-zero B-splines 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 std::size_t nbasis() const noexcept
Returns the number of basis functions.
Impl & operator=(Impl const &x)=default
Copy-assigns.
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.
Impl(ddc::Coordinate< CDim > rmin, ddc::Coordinate< CDim > rmax, std::size_t ncells)
Constructs a spline basis (B-splines) with n equidistant knots over .
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 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.
UniformPointSampling models a uniform discretization of the provided continuous dimension.
The top-level namespace of DDC.
constexpr bool is_uniform_bsplines_v
Indicates if a tag corresponds to uniform B-splines or not.
constexpr bool is_non_uniform_bsplines_v
Indicates if a tag corresponds to non-uniform B-splines or not.
A compile-time sequence of types.
Definition type_seq.hpp:30
A functor for describing a spline boundary value by a constant extrapolation for 2D evaluator.
KOKKOS_FUNCTION Real operator()(CoordType coord_extrap, ddc::ChunkSpan< Real 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.