DDC 0.10.0
Loading...
Searching...
No Matches
bsplines_non_uniform.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 <cassert>
9#include <cstddef>
10#include <initializer_list>
11#include <type_traits>
12#include <vector>
13
14#include <ddc/ddc.hpp>
15
16#include <Kokkos_Core.hpp>
17
18#include "view.hpp"
19
20namespace ddc {
21
22namespace detail {
23
24struct NonUniformBSplinesBase
25{
26};
27
28} // namespace detail
29
30template <class T>
32{
33};
34
35/**
36 * The type of a non-uniform 1D spline basis (B-spline).
37 *
38 * Knots for non-uniform B-splines are non-uniformly distributed (no assumption is made on the uniformity of their distribution,
39 * the associated discrete dimension is a NonUniformPointSampling).
40 *
41 * @tparam CDim The tag identifying the continuous dimension on which the support of the B-spline functions are defined.
42 * @tparam D The degree of the B-splines.
43 */
44template <class CDim, std::size_t D>
45class NonUniformBSplines : detail::NonUniformBSplinesBase
46{
47 static_assert(D > 0, "Parameter `D` must be positive");
48
49public:
50 /// @brief The tag identifying the continuous dimension on which the support of the B-splines are defined.
51 using continuous_dimension_type = CDim;
52
53 /// @brief The discrete dimension identifying B-splines.
54 using discrete_dimension_type = NonUniformBSplines;
55
56 /** @brief The degree of B-splines.
57 *
58 * @return The degree.
59 */
60 static constexpr std::size_t degree() noexcept
61 {
62 return D;
63 }
64
65 /** @brief Indicates if the B-splines are periodic or not.
66 *
67 * @return A boolean indicating if the B-splines are periodic or not.
68 */
69 static constexpr bool is_periodic() noexcept
70 {
71 return CDim::PERIODIC;
72 }
73
74 /** @brief Indicates if the B-splines are uniform or not (this is not the case here).
75 *
76 * @return A boolean indicating if the B-splines are uniform or not.
77 */
78 static constexpr bool is_uniform() noexcept
79 {
80 return false;
81 }
82
83 /** @brief Storage class of the static attributes of the discrete dimension.
84 *
85 * @tparam DDim The name of the discrete dimension.
86 * @tparam MemorySpace The Kokkos memory space where the attributes are being stored.
87 */
88 template <class DDim, class MemorySpace>
89 class Impl
90 {
91 template <class ODDim, class OMemorySpace>
92 friend class Impl;
93
94 public:
95 /// @brief The type of the knots defining the B-splines.
96 using knot_discrete_dimension_type = NonUniformBsplinesKnots<DDim>;
97
98 /// @brief The type of the discrete dimension representing the B-splines.
99 using discrete_dimension_type = NonUniformBSplines;
100
101 /// @brief The type of a DiscreteDomain whose elements identify the B-splines.
102 using discrete_domain_type = DiscreteDomain<DDim>;
103
104 /// @brief The type of a DiscreteElement identifying a B-spline.
105 using discrete_element_type = DiscreteElement<DDim>;
106
107 /// @brief The type of a DiscreteVector representing an "index displacement" between two B-splines.
108 using discrete_vector_type = DiscreteVector<DDim>;
109
110 private:
111 ddc::DiscreteDomain<knot_discrete_dimension_type> m_knot_domain;
112 ddc::DiscreteDomain<knot_discrete_dimension_type> m_break_point_domain;
113
114 ddc::DiscreteElement<DDim> m_reference;
115
116 public:
117 Impl() = default;
118
119 /** @brief Constructs an Impl using a brace-list, i.e. `Impl bsplines({0., 1.})`
120 *
121 * Constructs an Impl by iterating over a list of break points. Internally this constructor calls the constructor
122 * Impl(RandomIt breaks_begin, RandomIt breaks_end).
123 *
124 * @param breaks The std::initializer_list of the coordinates of break points.
125 */
126 Impl(std::initializer_list<ddc::Coordinate<CDim>> breaks)
127 : Impl(breaks.begin(), breaks.end())
128 {
129 }
130
131 /** @brief Constructs an Impl using a std::vector.
132 *
133 * Constructs an Impl by iterating over a list of break points. Internally this constructor calls the constructor
134 * Impl(RandomIt breaks_begin, RandomIt breaks_end).
135 *
136 * @param breaks The std::vector of the coordinates of break points.
137 */
138 explicit Impl(std::vector<ddc::Coordinate<CDim>> const& breaks)
139 : Impl(breaks.begin(), breaks.end())
140 {
141 }
142
143 /** @brief Constructs an Impl by iterating over a range of break points from begin to end.
144 *
145 * The provided break points describe the separation between the cells on which the polynomials
146 * comprising a spline are defined. They are used to build a set of knots. There are 2*degree more
147 * knots than break points. In the non-periodic case the knots are defined as follows:
148 * \f$ k_i = b_0 \forall 0 \leq i < d \f$
149 * \f$ k_{i+d} = b_i \forall 0 \leq i < n_b \f$
150 * \f$ k_{i+d+n_b} = b_{n_b-1} \forall 0 \leq i < d \f$
151 * where \f$d\f$ is the degree of the polynomials, and \f$n_b\f$ is the number of break points in the input pair of iterators. And in the periodic case:
152 * \f$ k_i = b_{n_b-1-d+i} \forall 0 \leq i < d \f$
153 * \f$ k_{i+d} = b_i \forall 0 \leq i \leq n_b \f$
154 * \f$ k_{i+d+n_b} = b_{i+1} \forall 0 \leq i < d \f$
155 *
156 * This constructor makes the knots accessible via a DiscreteSpace.
157 *
158 * @param breaks_begin The iterator which points at the beginning of the break points.
159 * @param breaks_end The iterator which points at the end of the break points.
160 */
161 template <class RandomIt>
162 Impl(RandomIt breaks_begin, RandomIt breaks_end);
163
164 /** @brief Copy-constructs from another Impl with a different Kokkos memory space.
165 *
166 * @param impl A reference to the other Impl.
167 */
168 template <class OriginMemorySpace>
169 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
170 : m_knot_domain(impl.m_knot_domain)
171 , m_break_point_domain(impl.m_break_point_domain)
172 , m_reference(impl.m_reference)
173 {
174 }
175
176 /** @brief Copy-constructs.
177 *
178 * @param x A reference to another Impl.
179 */
180 Impl(Impl const& x) = default;
181
182 /** @brief Move-constructs.
183 *
184 * @param x An rvalue to another Impl.
185 */
186 Impl(Impl&& x) = default;
187
188 /// @brief Destructs.
189 ~Impl() = default;
190
191 /** @brief Copy-assigns.
192 *
193 * @param x A reference to another Impl.
194 * @return A reference to the copied Impl.
195 */
196 Impl& operator=(Impl const& x) = default;
197
198 /** @brief Move-assigns.
199 *
200 * @param x An rvalue to another Impl.
201 * @return A reference to this object.
202 */
203 Impl& operator=(Impl&& x) = default;
204
205 /** @brief Evaluates non-zero B-splines at a given coordinate.
206 *
207 * The values are computed for every B-spline with support at the given coordinate x. There are only (degree+1)
208 * B-splines which are non-zero at any given point. It is these B-splines which are evaluated.
209 * This can be useful to calculate a spline approximation of a function. A spline approximation at coordinate x
210 * is a linear combination of these B-spline evaluations weighted with the spline coefficients of the spline-transformed
211 * initial discrete function.
212 *
213 * @param[out] values The values of the B-splines evaluated at coordinate x. It has to be a 1D mdspan with (degree+1) elements.
214 * @param[in] x The coordinate where B-splines are evaluated. It has to be in the range of break points coordinates.
215 * @return The index of the first B-spline which is evaluated.
216 */
217 KOKKOS_INLINE_FUNCTION discrete_element_type
218 eval_basis(DSpan1D values, ddc::Coordinate<CDim> const& x) const;
219
220 /** @brief Evaluates non-zero B-spline derivatives at a given coordinate
221 *
222 * The derivatives are computed for every B-spline with support at the given coordinate x. There are only (degree+1)
223 * B-splines which are non-zero at any given point. It is these B-splines which are differentiated.
224 * A spline approximation of a derivative at coordinate x is a linear
225 * combination of those B-spline derivatives weighted with the spline coefficients of the spline-transformed
226 * initial discrete function.
227 *
228 * @param[out] derivs The derivatives of the B-splines evaluated at coordinate x. It has to be a 1D mdspan with (degree+1) elements.
229 * @param[in] x The coordinate where B-spline derivatives are evaluated. It has to be in the range of break points coordinates.
230 * @return The index of the first B-spline which is differentiated.
231 */
232 KOKKOS_INLINE_FUNCTION discrete_element_type
233 eval_deriv(DSpan1D derivs, ddc::Coordinate<CDim> const& x) const;
234
235 /** @brief Evaluates non-zero B-spline values and \f$n\f$ derivatives at a given coordinate
236 *
237 * The values and derivatives are computed for every B-spline with support at the given coordinate x. There are only (degree+1)
238 * B-splines which are non-zero at any given point. It is these B-splines which are evaluated and differentiated.
239 * A spline approximation of a derivative at coordinate x is a linear
240 * combination of those B-spline derivatives weighted with spline coefficients of the spline-transformed
241 * initial discrete function.
242 *
243 * @param[out] derivs The values and \f$n\f$ derivatives of the B-splines evaluated at coordinate x. It has to be a 2D mdspan of sizes (degree+1, n+1).
244 * @param[in] x The coordinate where B-spline derivatives are evaluated. It has to be in the range of break points coordinates.
245 * @param[in] n The number of derivatives to evaluate (in addition to the B-spline values themselves).
246 * @return The index of the first B-spline which is evaluated/derivated.
247 */
249 ddc::DSpan2D derivs,
250 ddc::Coordinate<CDim> const& x,
251 std::size_t n) const;
252
253 /** @brief Returns the coordinate of the first support knot associated to a DiscreteElement identifying a B-spline.
254 *
255 * Each B-spline has a support defined over (degree+2) knots. For a B-spline identified by the
256 * provided DiscreteElement, this function returns the first knot in the support of the B-spline.
257 * In other words it returns the lower bound of the support.
258 *
259 * @param[in] ix DiscreteElement identifying the B-spline.
260 * @return DiscreteElement of the lower bound of the support of the B-spline.
261 */
262 KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<knot_discrete_dimension_type>
263 get_first_support_knot(discrete_element_type const& ix) const
264 {
265 return m_knot_domain.front() + (ix - m_reference).value();
266 }
267
268 /** @brief Returns the coordinate of the last support knot associated to a DiscreteElement identifying a B-spline.
269 *
270 * Each B-spline has a support defined over (degree+2) knots. For a B-spline identified by the
271 * provided DiscreteElement, this function returns the last knot in the support of the B-spline.
272 * In other words it returns the upper bound of the support.
273 *
274 * @param[in] ix DiscreteElement identifying the B-spline.
275 * @return DiscreteElement of the upper bound of the support of the B-spline.
276 */
277 KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<knot_discrete_dimension_type>
278 get_last_support_knot(discrete_element_type const& ix) const
279 {
281 + ddc::DiscreteVector<knot_discrete_dimension_type>(degree() + 1);
282 }
283
284 /** @brief Returns the coordinate of the first break point of the domain on which the B-splines are defined.
285 *
286 * @return Coordinate of the lower bound of the domain.
287 */
288 KOKKOS_INLINE_FUNCTION ddc::Coordinate<CDim> rmin() const noexcept
289 {
290 return ddc::coordinate(m_break_point_domain.front());
291 }
292
293 /** @brief Returns the coordinate of the last break point of the domain on which the B-splines are defined.
294 *
295 * @return Coordinate of the upper bound of the domain.
296 */
297 KOKKOS_INLINE_FUNCTION ddc::Coordinate<CDim> rmax() const noexcept
298 {
299 return ddc::coordinate(m_break_point_domain.back());
300 }
301
302 /** @brief Returns the length of the domain.
303 *
304 * @return The length of the domain.
305 */
306 KOKKOS_INLINE_FUNCTION double length() const noexcept
307 {
308 return rmax() - rmin();
309 }
310
311 /** @brief Returns the number of elements necessary to construct a spline representation of a function.
312 *
313 * For a non-periodic domain the number of elements necessary to construct a spline representation of a function
314 * is equal to the number of basis functions. However in the periodic case it additionally includes degree additional elements
315 * which allow the first B-splines to be evaluated close to rmax (where they also appear due to the periodicity).
316 *
317 * @return The number of elements necessary to construct a spline representation of a function.
318 */
319 KOKKOS_INLINE_FUNCTION std::size_t size() const noexcept
320 {
321 return degree() + ncells();
322 }
323
324 /** @brief Returns the discrete domain including eventual additional B-splines in the periodic case. See size().
325 *
326 * @return The discrete domain including eventual additional B-splines.
327 */
328 KOKKOS_INLINE_FUNCTION discrete_domain_type full_domain() const
329 {
330 return discrete_domain_type(m_reference, discrete_vector_type(size()));
331 }
332
333 /** @brief Returns the discrete domain which describes the break points.
334 *
335 * @return The discrete domain describing the break points.
336 */
337 KOKKOS_INLINE_FUNCTION ddc::DiscreteDomain<knot_discrete_dimension_type>
338 break_point_domain() const
339 {
340 return m_break_point_domain;
341 }
342
343 /** @brief The number of break points
344 *
345 * The number of break points or cell boundaries.
346 *
347 * @return The number of break points
348 */
349 KOKKOS_INLINE_FUNCTION std::size_t npoints() const noexcept
350 {
351 return m_knot_domain.size() - 2 * degree();
352 }
353
354 /** @brief Returns the number of basis functions.
355 *
356 * The number of functions in the spline basis.
357 *
358 * @return The number of basis functions.
359 */
360 KOKKOS_INLINE_FUNCTION std::size_t nbasis() const noexcept
361 {
362 return ncells() + !is_periodic() * degree();
363 }
364
365 /** @brief Returns the number of cells over which the B-splines are defined.
366 *
367 * The number of cells over which the B-splines and any spline representation are defined.
368 * In other words the number of polynomials that comprise a spline representation on the domain where the basis is defined.
369 *
370 * @return The number of cells over which the B-splines are defined.
371 */
372 KOKKOS_INLINE_FUNCTION std::size_t ncells() const noexcept
373 {
374 return npoints() - 1;
375 }
376
377 private:
378 KOKKOS_INLINE_FUNCTION discrete_element_type get_first_bspline_in_cell(
379 ddc::DiscreteElement<knot_discrete_dimension_type> const& ic) const
380 {
381 return m_reference + (ic - m_break_point_domain.front()).value();
382 }
383
384 /**
385 * @brief Get the DiscreteElement describing the knot at the start of the cell where x is found.
386 * @param x The point whose location must be determined.
387 * @returns The DiscreteElement describing the knot at the lower bound of the cell of interest.
388 */
389 KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<knot_discrete_dimension_type> find_cell_start(
390 ddc::Coordinate<CDim> const& x) const;
391 };
392};
393
394template <class DDim>
395struct is_non_uniform_bsplines : public std::is_base_of<detail::NonUniformBSplinesBase, DDim>::type
396{
397};
398
399/**
400 * @brief Indicates if a tag corresponds to non-uniform B-splines or not.
401 *
402 * @tparam The presumed non-uniform B-splines.
403 */
404template <class DDim>
406
407template <class CDim, std::size_t D>
408template <class DDim, class MemorySpace>
409template <class RandomIt>
410NonUniformBSplines<CDim, D>::Impl<DDim, MemorySpace>::Impl(
411 RandomIt const breaks_begin,
412 RandomIt const breaks_end)
413 : m_knot_domain(
414 ddc::DiscreteElement<knot_discrete_dimension_type>(0),
415 ddc::DiscreteVector<knot_discrete_dimension_type>(
416 (breaks_end - breaks_begin)
417 + 2 * degree())) // Create a mesh of knots including the eventual periodic point
418 , m_break_point_domain(
419 ddc::DiscreteElement<knot_discrete_dimension_type>(degree()),
420 ddc::DiscreteVector<knot_discrete_dimension_type>(
421 (breaks_end - breaks_begin))) // Create a mesh of break points
422 , m_reference(ddc::create_reference_discrete_element<DDim>())
423{
424 std::vector<ddc::Coordinate<CDim>> knots((breaks_end - breaks_begin) + 2 * degree());
425 // Fill the provided knots
426 int ii = 0;
427 for (RandomIt it = breaks_begin; it < breaks_end; ++it) {
428 knots[degree() + ii] = *it;
429 ++ii;
430 }
431 ddc::Coordinate<CDim> const rmin = knots[degree()];
432 ddc::Coordinate<CDim> const rmax = knots[(breaks_end - breaks_begin) + degree() - 1];
433 assert(rmin < rmax);
434
435 // Fill out the extra knots
436 if constexpr (is_periodic()) {
437 double const period = rmax - rmin;
438 for (std::size_t i = 1; i < degree() + 1; ++i) {
439 knots[degree() + -i] = knots[degree() + ncells() - i] - period;
440 knots[degree() + ncells() + i] = knots[degree() + i] + period;
441 }
442 } else // open
443 {
444 for (std::size_t i = 1; i < degree() + 1; ++i) {
445 knots[degree() + -i] = rmin;
446 knots[degree() + npoints() - 1 + i] = rmax;
447 }
448 }
449 ddc::init_discrete_space<knot_discrete_dimension_type>(knots);
450}
451
452template <class CDim, std::size_t D>
453template <class DDim, class MemorySpace>
454KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
455 Impl<DDim, MemorySpace>::eval_basis(DSpan1D values, ddc::Coordinate<CDim> const& x) const
456{
457 KOKKOS_ASSERT(values.size() == D + 1)
458
459 std::array<double, degree()> left;
460 std::array<double, degree()> right;
461
462 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
463 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
464 KOKKOS_ASSERT(values.size() == degree() + 1)
465
466 // 1. Compute cell index 'icell'
467 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
468
469 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
470 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
471 KOKKOS_ASSERT(ddc::coordinate(icell) - x <= length() * 1e-14)
472 KOKKOS_ASSERT(x - ddc::coordinate(icell + 1) <= length() * 1e-14)
473
474 // 2. Compute values of B-splines with support over cell 'icell'
475 double temp;
476 values[0] = 1.0;
477 for (std::size_t j = 0; j < degree(); ++j) {
478 left[j] = x - ddc::coordinate(icell - j);
479 right[j] = ddc::coordinate(icell + j + 1) - x;
480 double saved = 0.0;
481 for (std::size_t r = 0; r < j + 1; ++r) {
482 temp = values[r] / (right[r] + left[j - r]);
483 values[r] = saved + right[r] * temp;
484 saved = left[j - r] * temp;
485 }
486 values[j + 1] = saved;
487 }
488
489 return get_first_bspline_in_cell(icell);
490}
491
492template <class CDim, std::size_t D>
493template <class DDim, class MemorySpace>
494KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
495 Impl<DDim, MemorySpace>::eval_deriv(DSpan1D derivs, ddc::Coordinate<CDim> const& x) const
496{
497 std::array<double, degree()> left;
498 std::array<double, degree()> right;
499
500 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
501 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
502 KOKKOS_ASSERT(derivs.size() == degree() + 1)
503
504 // 1. Compute cell index 'icell'
505 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
506
507 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
508 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
509 KOKKOS_ASSERT(ddc::coordinate(icell) <= x)
510 KOKKOS_ASSERT(ddc::coordinate(icell + 1) >= x)
511
512 // 2. Compute values of derivatives of B-splines with support over cell 'icell'
513
514 /*
515 * Compute nonzero basis functions and knot differences
516 * for splines up to degree degree-1 which are needed to compute derivative
517 * First part of Algorithm A3.2 of NURBS book
518 */
519 double saved;
520 double temp;
521 derivs[0] = 1.0;
522 for (std::size_t j = 0; j < degree() - 1; ++j) {
523 left[j] = x - ddc::coordinate(icell - j);
524 right[j] = ddc::coordinate(icell + j + 1) - x;
525 saved = 0.0;
526 for (std::size_t r = 0; r < j + 1; ++r) {
527 temp = derivs[r] / (right[r] + left[j - r]);
528 derivs[r] = saved + right[r] * temp;
529 saved = left[j - r] * temp;
530 }
531 derivs[j + 1] = saved;
532 }
533
534 /*
535 * Compute derivatives at x using values stored in bsdx and formula
536 * for spline derivative based on difference of splines of degree degree-1
537 */
538 saved = degree() * derivs[0]
539 / (ddc::coordinate(icell + 1) - ddc::coordinate(icell + 1 - degree()));
540 derivs[0] = -saved;
541 for (std::size_t j = 1; j < degree(); ++j) {
542 temp = saved;
543 saved = degree() * derivs[j]
544 / (ddc::coordinate(icell + j + 1) - ddc::coordinate(icell + j + 1 - degree()));
545 derivs[j] = temp - saved;
546 }
547 derivs[degree()] = saved;
548
549 return get_first_bspline_in_cell(icell);
550}
551
552template <class CDim, std::size_t D>
553template <class DDim, class MemorySpace>
554KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
555 Impl<DDim, MemorySpace>::eval_basis_and_n_derivs(
556 ddc::DSpan2D const derivs,
557 ddc::Coordinate<CDim> const& x,
558 std::size_t const n) const
559{
560 std::array<double, degree()> left;
561 std::array<double, degree()> right;
562
563 std::array<double, 2 * (degree() + 1)> a_ptr;
564 Kokkos::mdspan<double, Kokkos::extents<std::size_t, degree() + 1, 2>> const a(a_ptr.data());
565
566 std::array<double, (degree() + 1) * (degree() + 1)> ndu_ptr;
567 Kokkos::mdspan<double, Kokkos::extents<std::size_t, degree() + 1, degree() + 1>> const ndu(
568 ndu_ptr.data());
569
570 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
571 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
572 // KOKKOS_ASSERT(n >= 0) as long as n is unsigned
573 KOKKOS_ASSERT(n <= degree())
574 KOKKOS_ASSERT(derivs.extent(0) == 1 + degree())
575 KOKKOS_ASSERT(derivs.extent(1) == 1 + n)
576
577 // 1. Compute cell index 'icell' and x_offset
578 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
579
580 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
581 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
582 KOKKOS_ASSERT(ddc::coordinate(icell) <= x)
583 KOKKOS_ASSERT(ddc::coordinate(icell + 1) >= x)
584
585 // 2. Compute nonzero basis functions and knot differences for splines
586 // up to degree (degree-1) which are needed to compute derivative
587 // Algorithm A2.3 of NURBS book
588 //
589 // 21.08.2017: save inverse of knot differences to avoid unnecessary
590 // divisions
591 // [Yaman Güçlü, Edoardo Zoni]
592
593 double saved;
594 double temp;
595 DDC_MDSPAN_ACCESS_OP(ndu, 0, 0) = 1.0;
596 for (std::size_t j = 0; j < degree(); ++j) {
597 left[j] = x - ddc::coordinate(icell - j);
598 right[j] = ddc::coordinate(icell + j + 1) - x;
599 saved = 0.0;
600 for (std::size_t r = 0; r < j + 1; ++r) {
601 // compute inverse of knot differences and save them into lower
602 // triangular part of ndu
603 DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1) = 1.0 / (right[r] + left[j - r]);
604 // compute basis functions and save them into upper triangular part
605 // of ndu
606 temp = DDC_MDSPAN_ACCESS_OP(ndu, j, r) * DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1);
607 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, r) = saved + right[r] * temp;
608 saved = left[j - r] * temp;
609 }
610 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, j + 1) = saved;
611 }
612 // Save 0-th derivative
613 for (std::size_t j = 0; j < degree() + 1; ++j) {
614 DDC_MDSPAN_ACCESS_OP(derivs, j, 0) = DDC_MDSPAN_ACCESS_OP(ndu, degree(), j);
615 }
616
617 for (int r = 0; r < static_cast<int>(degree() + 1); ++r) {
618 int s1 = 0;
619 int s2 = 1;
620 DDC_MDSPAN_ACCESS_OP(a, 0, 0) = 1.0;
621 for (int k = 1; k < static_cast<int>(n + 1); ++k) {
622 double d = 0.0;
623 int const rk = r - k;
624 int const pk = degree() - k;
625 if (r >= k) {
626 DDC_MDSPAN_ACCESS_OP(a, 0, s2)
627 = DDC_MDSPAN_ACCESS_OP(a, 0, s1) * DDC_MDSPAN_ACCESS_OP(ndu, rk, pk + 1);
628 d = DDC_MDSPAN_ACCESS_OP(a, 0, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk);
629 }
630 int const j1 = rk > -1 ? 1 : (-rk);
631 int const j2 = (r - 1) <= pk ? k : (degree() - r + 1);
632 for (int j = j1; j < j2; ++j) {
633 DDC_MDSPAN_ACCESS_OP(a, j, s2)
634 = (DDC_MDSPAN_ACCESS_OP(a, j, s1) - DDC_MDSPAN_ACCESS_OP(a, j - 1, s1))
635 * DDC_MDSPAN_ACCESS_OP(ndu, rk + j, pk + 1);
636 d += DDC_MDSPAN_ACCESS_OP(a, j, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk + j);
637 }
638 if (r <= pk) {
639 DDC_MDSPAN_ACCESS_OP(a, k, s2) = -DDC_MDSPAN_ACCESS_OP(a, k - 1, s1)
640 * DDC_MDSPAN_ACCESS_OP(ndu, r, pk + 1);
641 d += DDC_MDSPAN_ACCESS_OP(a, k, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, r);
642 }
643 DDC_MDSPAN_ACCESS_OP(derivs, r, k) = d;
644 Kokkos::kokkos_swap(s1, s2);
645 }
646 }
647
648 int r = degree();
649 for (int k = 1; k < static_cast<int>(n + 1); ++k) {
650 for (std::size_t i = 0; i < derivs.extent(0); ++i) {
651 DDC_MDSPAN_ACCESS_OP(derivs, i, k) *= r;
652 }
653 r *= degree() - k;
654 }
655
656 return get_first_bspline_in_cell(icell);
657}
658
659template <class CDim, std::size_t D>
660template <class DDim, class MemorySpace>
661KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<NonUniformBsplinesKnots<DDim>> NonUniformBSplines<
662 CDim,
663 D>::Impl<DDim, MemorySpace>::find_cell_start(ddc::Coordinate<CDim> const& x) const
664{
665 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
666 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
667
668 if (x <= rmin()) {
669 return m_break_point_domain.front();
670 }
671 if (x >= rmax()) {
672 return m_break_point_domain.back() - 1;
673 }
674
675 // Binary search
676 ddc::DiscreteElement<knot_discrete_dimension_type> low = m_break_point_domain.front();
677 ddc::DiscreteElement<knot_discrete_dimension_type> high = m_break_point_domain.back();
678 ddc::DiscreteElement<knot_discrete_dimension_type> icell = low + (high - low) / 2;
679 while (x < ddc::coordinate(icell) || x >= ddc::coordinate(icell + 1)) {
680 if (x < ddc::coordinate(icell)) {
681 high = icell;
682 } else {
683 low = icell;
684 }
685 icell = low + (high - low) / 2;
686 }
687 return icell;
688}
689
690} // namespace ddc
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 .
The top-level namespace of DDC.
constexpr bool is_non_uniform_bsplines_v
Indicates if a tag corresponds to non-uniform B-splines or not.