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