DDC 0.12.0
Loading...
Searching...
No Matches
greville_interpolation_points.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 <cassert>
8#include <cstddef>
9#include <type_traits>
10#include <vector>
11
12#include <ddc/ddc.hpp>
13
14#include <Kokkos_Core.hpp>
15
19
20namespace ddc {
21
22/**
23 * A class which provides helper functions to initialise the Greville points from a B-Spline definition.
24 *
25 * @tparam BSplines The bspline class relative to which the Greville points will be calculated.
26 * @tparam BcLower The lower boundary condition that will be used to build the splines.
27 * @tparam BcUpper The upper boundary condition that will be used to build the splines.
28 */
29template <class BSplines, ddc::BoundCond BcLower, ddc::BoundCond BcUpper>
31{
32 using continuous_dimension_type = BSplines::continuous_dimension_type;
33
34 template <class Sampling>
35 struct IntermediateUniformSampling
37 {
38 };
39
40 template <class Sampling>
41 struct IntermediateNonUniformSampling
43 {
44 };
45
46 template <class Sampling>
47 static auto uniform_greville_points()
48 requires(BSplines::is_uniform())
49 {
50 using SamplingImpl = Sampling::template Impl<Sampling, Kokkos::HostSpace>;
51
52 double constexpr shift = (BSplines::degree() % 2 == 0) ? 0.5 : 0.0;
53 double const dx
54 = (ddc::discrete_space<BSplines>().rmax() - ddc::discrete_space<BSplines>().rmin())
55 / ddc::discrete_space<BSplines>().ncells();
56 return SamplingImpl(
57 ddc::Coordinate<continuous_dimension_type>(
58 ddc::discrete_space<BSplines>().rmin() + shift * dx),
59 ddc::Coordinate<continuous_dimension_type>(dx));
60 }
61
62 template <class Sampling>
63 static auto non_uniform_greville_points()
64 requires(!BSplines::is_uniform())
65 {
66 using SamplingImpl = Sampling::template Impl<Sampling, Kokkos::HostSpace>;
67
68 std::size_t n_greville_points = 0;
69 if constexpr (BSplines::is_periodic()) {
70 n_greville_points = ddc::discrete_space<BSplines>().nbasis() + 1;
71 } else {
72 n_greville_points = ddc::discrete_space<BSplines>().nbasis();
73 }
74
75 std::vector<double> greville_points(n_greville_points);
76 ddc::DiscreteDomain<BSplines> const bspline_domain
77 = ddc::discrete_space<BSplines>().full_domain().take_first(
78 ddc::DiscreteVector<BSplines>(ddc::discrete_space<BSplines>().nbasis()));
79
80 ddc::DiscreteVector<NonUniformBsplinesKnots<BSplines>> n_points_in_average(
81 BSplines::degree());
82
83 ddc::DiscreteElement<BSplines> ib0(bspline_domain.front());
84
85 ddc::host_for_each(bspline_domain, [&](ddc::DiscreteElement<BSplines> ib) {
86 // Define the Greville points from the bspline knots
87 greville_points[ib - ib0] = 0.0;
88 ddc::DiscreteDomain<NonUniformBsplinesKnots<BSplines>> const sub_domain(
89 ddc::discrete_space<BSplines>().get_first_support_knot(ib) + 1,
90 n_points_in_average);
91 ddc::host_for_each(sub_domain, [&](auto ik) {
92 greville_points[ib - ib0] += ddc::coordinate(ik);
93 });
94 greville_points[ib - ib0] /= n_points_in_average.value();
95 });
96
97 // Use periodicity to ensure all points are in the domain
98 if constexpr (BSplines::is_periodic()) {
99 std::vector<double> temp_knots(BSplines::degree());
100 std::size_t npoints = 0;
101 // Count the number of interpolation points that need shifting to preserve the ordering
102 while (greville_points[npoints] < ddc::discrete_space<BSplines>().rmin()) {
103 assert(npoints < BSplines::degree());
104 temp_knots[npoints]
105 = greville_points[npoints] + ddc::discrete_space<BSplines>().length();
106 ++npoints;
107 }
108 // Shift the points
109 for (std::size_t i = 0; i < ddc::discrete_space<BSplines>().nbasis() - npoints; ++i) {
110 greville_points[i] = greville_points[i + npoints];
111 }
112 for (std::size_t i = 0; i < npoints; ++i) {
113 greville_points[ddc::discrete_space<BSplines>().nbasis() - npoints + i]
114 = temp_knots[i];
115 }
116
117 // Save a periodic point to initialise the domain size
118 greville_points[n_greville_points - 1]
119 = greville_points[0] + ddc::discrete_space<BSplines>().length();
120 }
121
122 return SamplingImpl(greville_points);
123 }
124
125 static constexpr std::size_t N_BE_MIN = n_boundary_equations(BcLower, BSplines::degree());
126 static constexpr std::size_t N_BE_MAX = n_boundary_equations(BcUpper, BSplines::degree());
127 static constexpr std::size_t N_BE = N_BE_MIN + N_BE_MAX;
128 template <class U>
129 static constexpr bool is_uniform_discrete_dimension_v
130 = U::is_uniform() && ((N_BE_MIN != 0 && N_BE_MAX != 0) || U::is_periodic());
131
132public:
133 /**
134 * Get the UniformPointSampling defining the Greville points.
135 *
136 * This function is called when the result is a UniformPointSampling. This is the case
137 * when uniform splines are used with an odd degree and with boundary conditions which
138 * do not introduce additional interpolation points.
139 *
140 * @tparam Sampling The discrete dimension supporting the Greville points.
141 *
142 * @returns The mesh of uniform Greville points.
143 */
144 template <class Sampling>
145 static auto get_sampling()
146 requires(is_uniform_discrete_dimension_v<BSplines>)
147 {
148 return uniform_greville_points<Sampling>();
149 }
150
151 /**
152 * Get the NonUniformPointSampling defining the Greville points.
153 *
154 * @tparam Sampling The discrete dimension supporting the Greville points.
155 *
156 * @returns The mesh of non-uniform Greville points.
157 */
158 template <class Sampling>
159 static auto get_sampling()
160 requires(!is_uniform_discrete_dimension_v<BSplines>)
161 {
162 using SamplingImpl = Sampling::template Impl<Sampling, Kokkos::HostSpace>;
163 if constexpr (BSplines::is_uniform()) {
164 using IntermediateSampling = IntermediateUniformSampling<Sampling>;
165 auto points_wo_bcs = uniform_greville_points<IntermediateSampling>();
166 std::size_t const n_break_points = ddc::discrete_space<BSplines>().ncells() + 1;
167 if constexpr (N_BE > 0) {
168 assert(ddc::discrete_space<BSplines>().nbasis() >= N_BE);
169 }
170 std::size_t const npoints = ddc::discrete_space<BSplines>().nbasis() - N_BE;
171 std::vector<double> points_with_bcs(npoints);
172
173 // Construct Greville-like points at the edge
174 if constexpr (BcLower == ddc::BoundCond::GREVILLE) {
175 for (std::size_t i(0); i < BSplines::degree() / 2 + 1; ++i) {
176 points_with_bcs[i]
177 = (BSplines::degree() - i) * ddc::discrete_space<BSplines>().rmin();
178 ddc::DiscreteElement<BSplines> const spline_idx(i);
179 ddc::DiscreteVector<UniformBsplinesKnots<BSplines>> const n_knots_in_domain(i);
180 ddc::DiscreteDomain<UniformBsplinesKnots<BSplines>> const sub_domain(
181 ddc::discrete_space<BSplines>().get_last_support_knot(spline_idx)
182 - n_knots_in_domain,
183 n_knots_in_domain);
184 ddc::host_for_each(
185 sub_domain,
186 [&](ddc::DiscreteElement<UniformBsplinesKnots<BSplines>> ik) {
187 points_with_bcs[i] += ddc::coordinate(ik);
188 });
189 points_with_bcs[i] /= BSplines::degree();
190 }
191 } else {
192 points_with_bcs[0]
193 = points_wo_bcs.coordinate(ddc::DiscreteElement<IntermediateSampling>(0));
194 }
195
196 std::size_t const n_start
197 = (BcLower == ddc::BoundCond::GREVILLE) ? BSplines::degree() / 2 + 1 : 1;
198 std::size_t const domain_size = n_break_points - 2;
199 ddc::DiscreteElement<IntermediateSampling> domain_start(1);
200 ddc::DiscreteDomain<IntermediateSampling> const
201 domain(domain_start, ddc::DiscreteVector<IntermediateSampling>(domain_size));
202
203 // Copy central points
204 ddc::host_for_each(domain, [&](auto ip) {
205 points_with_bcs[ip - domain_start + n_start] = points_wo_bcs.coordinate(ip);
206 });
207
208 // Construct Greville-like points at the edge
209 if constexpr (BcUpper == ddc::BoundCond::GREVILLE) {
210 for (std::size_t i(0); i < BSplines::degree() / 2 + 1; ++i) {
211 points_with_bcs[npoints - 1 - i]
212 = (BSplines::degree() - i) * ddc::discrete_space<BSplines>().rmax();
213 ddc::DiscreteElement<BSplines> const spline_idx(
214 ddc::discrete_space<BSplines>().nbasis() - 1 - i);
215 ddc::DiscreteVector<UniformBsplinesKnots<BSplines>> const n_knots_in_domain(i);
216 ddc::DiscreteDomain<UniformBsplinesKnots<BSplines>> const sub_domain(
217 ddc::discrete_space<BSplines>().get_first_support_knot(spline_idx) + 1,
218 n_knots_in_domain);
219 ddc::host_for_each(
220 sub_domain,
221 [&](ddc::DiscreteElement<UniformBsplinesKnots<BSplines>> ik) {
222 points_with_bcs[npoints - 1 - i] += ddc::coordinate(ik);
223 });
224 points_with_bcs[npoints - 1 - i] /= BSplines::degree();
225 }
226 } else {
227 points_with_bcs[npoints - 1] = points_wo_bcs.coordinate(
228 ddc::DiscreteElement<IntermediateSampling>(
229 ddc::discrete_space<BSplines>().ncells() - 1
230 + BSplines::degree() % 2));
231 }
232 return SamplingImpl(points_with_bcs);
233 } else {
234 using IntermediateSampling = IntermediateNonUniformSampling<Sampling>;
235 if constexpr (N_BE == 0) {
236 return non_uniform_greville_points<Sampling>();
237 } else {
238 auto points_wo_bcs = non_uniform_greville_points<IntermediateSampling>();
239 // All points are Greville points. Extract unnecessary points near the boundary
240 std::vector<double> points_with_bcs(points_wo_bcs.size() - N_BE);
241 std::size_t constexpr n_start = N_BE_MIN;
242
243 using length = ddc::DiscreteVector<IntermediateSampling>;
244
245 ddc::DiscreteElement<IntermediateSampling> domain_start(n_start);
246 ddc::DiscreteDomain<IntermediateSampling> const
247 domain(domain_start, length(points_with_bcs.size()));
248
249 points_with_bcs[0] = points_wo_bcs.coordinate(domain.front());
250 ddc::host_for_each(domain.remove(length(1), length(1)), [&](auto ip) {
251 points_with_bcs[ip - domain_start] = points_wo_bcs.coordinate(ip);
252 });
253 points_with_bcs[points_with_bcs.size() - 1]
254 = points_wo_bcs.coordinate(domain.back());
255
256 return SamplingImpl(points_with_bcs);
257 }
258 }
259 }
260
261 /**
262 * The type of the mesh.
263 *
264 * This is either NonUniformPointSampling or UniformPointSampling.
265 */
266 using interpolation_discrete_dimension_type = std::conditional_t<
267 is_uniform_discrete_dimension_v<BSplines>,
268 ddc::UniformPointSampling<continuous_dimension_type>,
269 ddc::NonUniformPointSampling<continuous_dimension_type>>;
270
271 /**
272 * Get the domain which gives us access to all of the Greville points.
273 *
274 * @tparam Sampling The discrete dimension supporting the Greville points.
275 *
276 * @returns The domain of the Greville points.
277 */
278 template <class Sampling>
279 static ddc::DiscreteDomain<Sampling> get_domain()
280 {
281 std::size_t const npoints = ddc::discrete_space<BSplines>().nbasis() - N_BE;
282 return ddc::DiscreteDomain<Sampling>(
283 ddc::DiscreteElement<Sampling>(0),
284 ddc::DiscreteVector<Sampling>(npoints));
285 }
286};
287
288} // namespace ddc
friend class ChunkSpan
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.
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.
BoundCond
An enum representing a spline boundary condition.
@ GREVILLE
Use Greville points instead of conditions on derivative for B-Spline interpolation.
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
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.