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