DDC 0.12.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 */
248 KOKKOS_INLINE_FUNCTION discrete_element_type eval_basis_and_n_derivs(
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
407namespace concepts {
408
409template <class DDim>
410concept non_uniform_bsplines = is_non_uniform_bsplines_v<DDim>;
411
412}
413
414template <class CDim, std::size_t D>
415template <class DDim, class MemorySpace>
416template <class RandomIt>
417NonUniformBSplines<CDim, D>::Impl<DDim, MemorySpace>::Impl(
418 RandomIt const breaks_begin,
419 RandomIt const breaks_end)
420 : m_knot_domain(
421 ddc::DiscreteElement<knot_discrete_dimension_type>(0),
422 ddc::DiscreteVector<knot_discrete_dimension_type>(
423 (breaks_end - breaks_begin)
424 + 2 * degree())) // Create a mesh of knots including the eventual periodic point
425 , m_break_point_domain(
426 ddc::DiscreteElement<knot_discrete_dimension_type>(degree()),
427 ddc::DiscreteVector<knot_discrete_dimension_type>(
428 (breaks_end - breaks_begin))) // Create a mesh of break points
429 , m_reference(ddc::create_reference_discrete_element<DDim>())
430{
431 std::vector<ddc::Coordinate<CDim>> knots((breaks_end - breaks_begin) + 2 * degree());
432 // Fill the provided knots
433 int ii = 0;
434 for (RandomIt it = breaks_begin; it < breaks_end; ++it) {
435 knots[degree() + ii] = *it;
436 ++ii;
437 }
438 ddc::Coordinate<CDim> const rmin = knots[degree()];
439 ddc::Coordinate<CDim> const rmax = knots[(breaks_end - breaks_begin) + degree() - 1];
440 assert(rmin < rmax);
441
442 // Fill out the extra knots
443 if constexpr (is_periodic()) {
444 double const period = rmax - rmin;
445 for (std::size_t i = 1; i < degree() + 1; ++i) {
446 knots[degree() + -i] = knots[degree() + ncells() - i] - period;
447 knots[degree() + ncells() + i] = knots[degree() + i] + period;
448 }
449 } else // open
450 {
451 for (std::size_t i = 1; i < degree() + 1; ++i) {
452 knots[degree() + -i] = rmin;
453 knots[degree() + npoints() - 1 + i] = rmax;
454 }
455 }
456 ddc::init_discrete_space<knot_discrete_dimension_type>(knots);
457}
458
459template <class CDim, std::size_t D>
460template <class DDim, class MemorySpace>
461KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
462 Impl<DDim, MemorySpace>::eval_basis(DSpan1D values, ddc::Coordinate<CDim> const& x) const
463{
464 KOKKOS_ASSERT(values.size() == D + 1)
465
466 std::array<double, degree()> left;
467 std::array<double, degree()> right;
468
469 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
470 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
471 KOKKOS_ASSERT(values.size() == degree() + 1)
472
473 // 1. Compute cell index 'icell'
474 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
475
476 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
477 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
478 KOKKOS_ASSERT(ddc::coordinate(icell) - x <= length() * 1e-14)
479 KOKKOS_ASSERT(x - ddc::coordinate(icell + 1) <= length() * 1e-14)
480
481 // 2. Compute values of B-splines with support over cell 'icell'
482 double temp;
483 values[0] = 1.0;
484 for (std::size_t j = 0; j < degree(); ++j) {
485 left[j] = x - ddc::coordinate(icell - j);
486 right[j] = ddc::coordinate(icell + j + 1) - x;
487 double saved = 0.0;
488 for (std::size_t r = 0; r < j + 1; ++r) {
489 temp = values[r] / (right[r] + left[j - r]);
490 values[r] = saved + right[r] * temp;
491 saved = left[j - r] * temp;
492 }
493 values[j + 1] = saved;
494 }
495
496 return get_first_bspline_in_cell(icell);
497}
498
499template <class CDim, std::size_t D>
500template <class DDim, class MemorySpace>
501KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
502 Impl<DDim, MemorySpace>::eval_deriv(DSpan1D derivs, ddc::Coordinate<CDim> const& x) const
503{
504 std::array<double, degree()> left;
505 std::array<double, degree()> right;
506
507 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
508 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
509 KOKKOS_ASSERT(derivs.size() == degree() + 1)
510
511 // 1. Compute cell index 'icell'
512 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
513
514 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
515 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
516 KOKKOS_ASSERT(ddc::coordinate(icell) <= x)
517 KOKKOS_ASSERT(ddc::coordinate(icell + 1) >= x)
518
519 // 2. Compute values of derivatives of B-splines with support over cell 'icell'
520
521 /*
522 * Compute nonzero basis functions and knot differences
523 * for splines up to degree degree-1 which are needed to compute derivative
524 * First part of Algorithm A3.2 of NURBS book
525 */
526 double saved;
527 double temp;
528 derivs[0] = 1.0;
529 for (std::size_t j = 0; j < degree() - 1; ++j) {
530 left[j] = x - ddc::coordinate(icell - j);
531 right[j] = ddc::coordinate(icell + j + 1) - x;
532 saved = 0.0;
533 for (std::size_t r = 0; r < j + 1; ++r) {
534 temp = derivs[r] / (right[r] + left[j - r]);
535 derivs[r] = saved + right[r] * temp;
536 saved = left[j - r] * temp;
537 }
538 derivs[j + 1] = saved;
539 }
540
541 /*
542 * Compute derivatives at x using values stored in bsdx and formula
543 * for spline derivative based on difference of splines of degree degree-1
544 */
545 saved = degree() * derivs[0]
546 / (ddc::coordinate(icell + 1) - ddc::coordinate(icell + 1 - degree()));
547 derivs[0] = -saved;
548 for (std::size_t j = 1; j < degree(); ++j) {
549 temp = saved;
550 saved = degree() * derivs[j]
551 / (ddc::coordinate(icell + j + 1) - ddc::coordinate(icell + j + 1 - degree()));
552 derivs[j] = temp - saved;
553 }
554 derivs[degree()] = saved;
555
556 return get_first_bspline_in_cell(icell);
557}
558
559template <class CDim, std::size_t D>
560template <class DDim, class MemorySpace>
561KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<DDim> NonUniformBSplines<CDim, D>::
562 Impl<DDim, MemorySpace>::eval_basis_and_n_derivs(
563 ddc::DSpan2D const derivs,
564 ddc::Coordinate<CDim> const& x,
565 std::size_t const n) const
566{
567 std::array<double, degree()> left;
568 std::array<double, degree()> right;
569
570 std::array<double, 2 * (degree() + 1)> a_ptr;
571 Kokkos::mdspan<double, Kokkos::extents<std::size_t, degree() + 1, 2>> const a(a_ptr.data());
572
573 std::array<double, (degree() + 1) * (degree() + 1)> ndu_ptr;
574 Kokkos::mdspan<double, Kokkos::extents<std::size_t, degree() + 1, degree() + 1>> const ndu(
575 ndu_ptr.data());
576
577 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
578 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
579 // KOKKOS_ASSERT(n >= 0) as long as n is unsigned
580 KOKKOS_ASSERT(n <= degree())
581 KOKKOS_ASSERT(derivs.extent(0) == 1 + degree())
582 KOKKOS_ASSERT(derivs.extent(1) == 1 + n)
583
584 // 1. Compute cell index 'icell' and x_offset
585 ddc::DiscreteElement<knot_discrete_dimension_type> const icell = find_cell_start(x);
586
587 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
588 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
589 KOKKOS_ASSERT(ddc::coordinate(icell) <= x)
590 KOKKOS_ASSERT(ddc::coordinate(icell + 1) >= x)
591
592 // 2. Compute nonzero basis functions and knot differences for splines
593 // up to degree (degree-1) which are needed to compute derivative
594 // Algorithm A2.3 of NURBS book
595 //
596 // 21.08.2017: save inverse of knot differences to avoid unnecessary
597 // divisions
598 // [Yaman Güçlü, Edoardo Zoni]
599
600 double saved;
601 double temp;
602 DDC_MDSPAN_ACCESS_OP(ndu, 0, 0) = 1.0;
603 for (std::size_t j = 0; j < degree(); ++j) {
604 left[j] = x - ddc::coordinate(icell - j);
605 right[j] = ddc::coordinate(icell + j + 1) - x;
606 saved = 0.0;
607 for (std::size_t r = 0; r < j + 1; ++r) {
608 // compute inverse of knot differences and save them into lower
609 // triangular part of ndu
610 DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1) = 1.0 / (right[r] + left[j - r]);
611 // compute basis functions and save them into upper triangular part
612 // of ndu
613 temp = DDC_MDSPAN_ACCESS_OP(ndu, j, r) * DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1);
614 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, r) = saved + right[r] * temp;
615 saved = left[j - r] * temp;
616 }
617 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, j + 1) = saved;
618 }
619 // Save 0-th derivative
620 for (std::size_t j = 0; j < degree() + 1; ++j) {
621 DDC_MDSPAN_ACCESS_OP(derivs, j, 0) = DDC_MDSPAN_ACCESS_OP(ndu, degree(), j);
622 }
623
624 for (int r = 0; r < static_cast<int>(degree() + 1); ++r) {
625 int s1 = 0;
626 int s2 = 1;
627 DDC_MDSPAN_ACCESS_OP(a, 0, 0) = 1.0;
628 for (int k = 1; k < static_cast<int>(n + 1); ++k) {
629 double d = 0.0;
630 int const rk = r - k;
631 int const pk = degree() - k;
632 if (r >= k) {
633 DDC_MDSPAN_ACCESS_OP(a, 0, s2)
634 = DDC_MDSPAN_ACCESS_OP(a, 0, s1) * DDC_MDSPAN_ACCESS_OP(ndu, rk, pk + 1);
635 d = DDC_MDSPAN_ACCESS_OP(a, 0, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk);
636 }
637 int const j1 = rk > -1 ? 1 : (-rk);
638 int const j2 = (r - 1) <= pk ? k : (degree() - r + 1);
639 for (int j = j1; j < j2; ++j) {
640 DDC_MDSPAN_ACCESS_OP(a, j, s2)
641 = (DDC_MDSPAN_ACCESS_OP(a, j, s1) - DDC_MDSPAN_ACCESS_OP(a, j - 1, s1))
642 * DDC_MDSPAN_ACCESS_OP(ndu, rk + j, pk + 1);
643 d += DDC_MDSPAN_ACCESS_OP(a, j, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk + j);
644 }
645 if (r <= pk) {
646 DDC_MDSPAN_ACCESS_OP(a, k, s2) = -DDC_MDSPAN_ACCESS_OP(a, k - 1, s1)
647 * DDC_MDSPAN_ACCESS_OP(ndu, r, pk + 1);
648 d += DDC_MDSPAN_ACCESS_OP(a, k, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, r);
649 }
650 DDC_MDSPAN_ACCESS_OP(derivs, r, k) = d;
651 Kokkos::kokkos_swap(s1, s2);
652 }
653 }
654
655 int r = degree();
656 for (int k = 1; k < static_cast<int>(n + 1); ++k) {
657 for (std::size_t i = 0; i < derivs.extent(0); ++i) {
658 DDC_MDSPAN_ACCESS_OP(derivs, i, k) *= r;
659 }
660 r *= degree() - k;
661 }
662
663 return get_first_bspline_in_cell(icell);
664}
665
666template <class CDim, std::size_t D>
667template <class DDim, class MemorySpace>
668KOKKOS_INLINE_FUNCTION ddc::DiscreteElement<NonUniformBsplinesKnots<DDim>> NonUniformBSplines<
669 CDim,
670 D>::Impl<DDim, MemorySpace>::find_cell_start(ddc::Coordinate<CDim> const& x) const
671{
672 KOKKOS_ASSERT(x - rmin() >= -length() * 1e-14)
673 KOKKOS_ASSERT(rmax() - x >= -length() * 1e-14)
674
675 if (x <= rmin()) {
676 return m_break_point_domain.front();
677 }
678 if (x >= rmax()) {
679 return m_break_point_domain.back() - 1;
680 }
681
682 // Binary search
683 ddc::DiscreteElement<knot_discrete_dimension_type> low = m_break_point_domain.front();
684 ddc::DiscreteElement<knot_discrete_dimension_type> high = m_break_point_domain.back();
685 ddc::DiscreteElement<knot_discrete_dimension_type> icell = low + (high - low) / 2;
686 while (x < ddc::coordinate(icell) || x >= ddc::coordinate(icell + 1)) {
687 if (x < ddc::coordinate(icell)) {
688 high = icell;
689 } else {
690 low = icell;
691 }
692 icell = low + (high - low) / 2;
693 }
694 return icell;
695}
696
697} // 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.