DDC 0.10.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
17template <class DimI, class... Dim>
19{
20};
21
22/**
23 * @brief A functor for describing a spline boundary value by a constant extrapolation for 1D evaluator.
24 *
25 * To define the value of a function on B-splines out of the domain, we here use a constant
26 * extrapolation on the edge.
27 */
28template <class DimI>
30{
31private:
32 ddc::Coordinate<DimI> m_eval_pos;
33
34public:
35 /**
36 * @brief Instantiate a ConstantExtrapolationRule.
37 *
38 * The boundary value will be the same as at the coordinate eval_pos given.
39 *
40 * @param[in] eval_pos Coordinate inside the domain where we will evaluate each points outside the domain.
41 */
42 explicit ConstantExtrapolationRule(ddc::Coordinate<DimI> eval_pos) : m_eval_pos(eval_pos) {}
43
44 /**
45 * @brief Get the value of the function on B-splines at a coordinate outside the domain.
46 *
47 * @param[in] pos The coordinate where we want to evaluate the function on B-splines.
48 * @param[in] spline_coef The coefficients of the function on B-splines.
49 *
50 * @return A double with the value of the function on B-splines evaluated at the coordinate.
51 */
52 template <class CoordType, class BSplines, class Layout, class MemorySpace>
53 KOKKOS_FUNCTION double operator()(
54 [[maybe_unused]] CoordType pos,
55 ddc::ChunkSpan<double const, ddc::DiscreteDomain<BSplines>, Layout, MemorySpace> const
56 spline_coef) const
57 {
58 // `pos` is always unused, but needed for Doxygen
59 static_assert(in_tags_v<DimI, to_type_seq_t<CoordType>>);
60
61 std::array<double, BSplines::degree() + 1> vals_ptr;
62 Kokkos::mdspan<double, Kokkos::extents<std::size_t, BSplines::degree() + 1>> const vals(
63 vals_ptr.data());
64
65 ddc::DiscreteElement<BSplines> const idx
66 = ddc::discrete_space<BSplines>().eval_basis(vals, m_eval_pos);
67
68 double y = 0.0;
69 for (std::size_t i = 0; i < BSplines::degree() + 1; ++i) {
70 y += spline_coef(idx + i) * vals[i];
71 }
72 return y;
73 }
74};
75
76/**
77 * @brief A functor for describing a spline boundary value by a constant extrapolation for 2D evaluator.
78 *
79 * To define the value of a function on B-splines out of the domain, we here use a constant
80 * extrapolation on the edge.
81 */
82template <class DimI, class DimNI>
83struct ConstantExtrapolationRule<DimI, DimNI>
84{
85private:
86 ddc::Coordinate<DimI> m_eval_pos;
87 ddc::Coordinate<DimNI> m_eval_pos_not_interest_min;
88 ddc::Coordinate<DimNI> m_eval_pos_not_interest_max;
89
90public:
91 /**
92 * @brief Instantiate a ConstantExtrapolationRule.
93 *
94 * The boundary value will be the same as at the coordinate given in a dimension given.
95 * The dimension of the input defines the dimension of the boundary condition.
96 * The second and the third parameters are needed in case of non-periodic splines on the
97 * dimension off-interest (the complementary dimension of the boundary condition),
98 * because the evaluator can receive coordinates outside the domain in both dimension.
99 *
100 * @param[in] eval_pos Coordinate in the dimension given inside the domain where we will evaluate each points outside the domain.
101 * @param[in] eval_pos_not_interest_min The minimum coordinate inside the domain on the complementary dimension of the boundary condition.
102 * @param[in] eval_pos_not_interest_max The maximum coordinate inside the domain on the complementary dimension of the boundary condition.
103 */
105 ddc::Coordinate<DimI> eval_pos,
106 ddc::Coordinate<DimNI> eval_pos_not_interest_min,
107 ddc::Coordinate<DimNI> eval_pos_not_interest_max)
108 : m_eval_pos(eval_pos)
109 , m_eval_pos_not_interest_min(eval_pos_not_interest_min)
110 , m_eval_pos_not_interest_max(eval_pos_not_interest_max)
111 {
112 }
113
114 /**
115 * @brief Instantiate a ConstantExtrapolationRule.
116 *
117 * The boundary value will be the same as at the coordinate given in a dimension given.
118 * The dimension of the input defines the dimension of the boundary condition.
119 * No second and third parameters are needed in case of periodic splines on the
120 * dimension off-interest (the complementary dimension of the boundary condition).
121 *
122 * @param[in] eval_pos Coordinate in the dimension given inside the domain where we will evaluate each points outside the domain.
123 */
124 template <class SFINAEDimNI = DimNI, std::enable_if_t<SFINAEDimNI::PERIODIC, int> = 0>
125 explicit ConstantExtrapolationRule(ddc::Coordinate<DimI> eval_pos)
126 : m_eval_pos(eval_pos)
127 , m_eval_pos_not_interest_min(0.)
128 , m_eval_pos_not_interest_max(0.)
129 {
130 }
131
132 /**
133 * @brief Get the value of the function on B-splines at a coordinate outside the domain.
134 *
135 * In the dimension defined in the constructor Dim1 (or Dim2), it sets the coordinate pos_1 (or pos_2)
136 * given at the m_eval_pos coordinate if it is outside the domain.
137 * If the coordinate on the complementary dimension of the boundary condition dimension ddc::Coordinate<DimNI>(coord_extrap) is
138 * outside the domain, then it also sets the coordinate at eval_pos_not_interest_min
139 * (if ddc::Coordinate<DimNI>(coord_extrap) @f$ < @f$ eval_pos_not_interest_min) or
140 * at eval_pos_not_interest_max (if ddc::Coordinate<DimNI>(coord_extrap) @f$ > @f$ eval_pos_not_interest_max).
141 *
142 * @param[in] coord_extrap The coordinates where we want to evaluate the function on B-splines
143 * @param[in] spline_coef The coefficients of the function on B-splines.
144 *
145 *@return A double with the value of the function on B-splines evaluated at the coordinate.
146 */
147 template <class CoordType, class BSplines1, class BSplines2, class Layout, class MemorySpace>
148 KOKKOS_FUNCTION double operator()(
149 CoordType coord_extrap,
150 ddc::ChunkSpan<
151 double const,
152 ddc::DiscreteDomain<BSplines1, BSplines2>,
153 Layout,
154 MemorySpace> const spline_coef) const
155 {
156 static_assert(
157 in_tags_v<DimI, to_type_seq_t<CoordType>>
158 && in_tags_v<DimNI, to_type_seq_t<CoordType>>);
159
160 ddc::Coordinate<DimI, DimNI> eval_pos;
161 if constexpr (DimNI::PERIODIC) {
162 eval_pos = ddc::
163 Coordinate<DimI, DimNI>(m_eval_pos, ddc::Coordinate<DimNI>(coord_extrap));
164 } else {
165 eval_pos = ddc::Coordinate<DimI, DimNI>(
166 m_eval_pos,
167 Kokkos::
168 clamp(ddc::Coordinate<DimNI>(coord_extrap),
169 m_eval_pos_not_interest_min,
170 m_eval_pos_not_interest_max));
171 }
172
173 std::array<double, BSplines1::degree() + 1> vals1_ptr;
174 Kokkos::mdspan<double, Kokkos::extents<std::size_t, BSplines1::degree() + 1>> const vals1(
175 vals1_ptr.data());
176 std::array<double, BSplines2::degree() + 1> vals2_ptr;
177 Kokkos::mdspan<double, Kokkos::extents<std::size_t, BSplines2::degree() + 1>> const vals2(
178 vals2_ptr.data());
179
180 ddc::DiscreteElement<BSplines1> const idx1 = ddc::discrete_space<BSplines1>().eval_basis(
181 vals1,
182 ddc::Coordinate<typename BSplines1::continuous_dimension_type>(eval_pos));
183 ddc::DiscreteElement<BSplines2> const idx2 = ddc::discrete_space<BSplines2>().eval_basis(
184 vals2,
185 ddc::Coordinate<typename BSplines2::continuous_dimension_type>(eval_pos));
186
187 double y = 0.0;
188 for (std::size_t i = 0; i < BSplines1::degree() + 1; ++i) {
189 for (std::size_t j = 0; j < BSplines2::degree() + 1; ++j) {
190 y += spline_coef(idx1 + i, idx2 + j) * vals1[i] * vals2[j];
191 }
192 }
193
194 return y;
195 }
196};
197
198} // 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.
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 .
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 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.
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.