10#include <initializer_list>
16#include <Kokkos_Core.hpp>
22struct NonUniformBSplinesBase
34
35
36
37
38
39
40
41
42template <
class CDim, std::size_t D,
bool Periodic = CDim::PERIODIC>
45 static_assert(D > 0,
"Parameter `D` must be positive");
49 using continuous_dimension_type = CDim;
55
56
57
58 static constexpr std::size_t
degree()
noexcept
64
65
66
73
74
75
82
83
84
85
86 template <
class DDim,
class MemorySpace>
89 template <
class ODDim,
class OMemorySpace>
103 using discrete_element_type = DiscreteElement<DDim>;
112 ddc::DiscreteElement<DDim> m_reference;
118
119
120
121
122
123
124 Impl(std::initializer_list<
ddc::Coordinate<CDim>> breaks)
125 :
Impl(breaks.begin(), breaks.end())
130
131
132
133
134
135
136 explicit Impl(std::vector<
ddc::Coordinate<CDim>>
const& breaks)
137 :
Impl(breaks.begin(), breaks.end())
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 template <
class RandomIt>
160 Impl(RandomIt breaks_begin, RandomIt breaks_end);
163
164
165
166 template <
class OriginMemorySpace>
167 explicit Impl(
Impl<DDim, OriginMemorySpace>
const& impl)
168 : m_knot_domain(impl.m_knot_domain)
169 , m_break_point_domain(impl.m_break_point_domain)
170 , m_reference(impl.m_reference)
175
176
177
181
182
183
190
191
192
193
197
198
199
200
204
205
206
207
208
209
210
211
212
213
214
215 KOKKOS_INLINE_FUNCTION discrete_element_type
eval_basis(
216 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 1>> values,
217 ddc::Coordinate<CDim>
const& x)
const;
220
221
222
223
224
225
226
227
228
229
230
231 KOKKOS_INLINE_FUNCTION discrete_element_type
eval_deriv(
232 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 1>> derivs,
233 ddc::Coordinate<CDim>
const& x)
const;
236
237
238
239
240
241
242
243
244
245
246
247
249 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 2>> derivs,
250 ddc::Coordinate<CDim>
const& x,
251 std::size_t n)
const;
254
255
256
257
258
259
260
261
262 KOKKOS_INLINE_FUNCTION
ddc::DiscreteElement<knot_discrete_dimension_type>
265 return m_knot_domain.front() + (ix - m_reference).value();
269
270
271
272
273
274
275
276
277 KOKKOS_INLINE_FUNCTION
ddc::DiscreteElement<knot_discrete_dimension_type>
285
286
287
288 KOKKOS_INLINE_FUNCTION
ddc::Coordinate<CDim>
rmin()
const noexcept
290 return ddc::coordinate(m_break_point_domain.front());
294
295
296
297 KOKKOS_INLINE_FUNCTION
ddc::Coordinate<CDim>
rmax()
const noexcept
299 return ddc::coordinate(m_break_point_domain.back());
303
304
305
306 KOKKOS_INLINE_FUNCTION
double length()
const noexcept
312
313
314
315
316
317
318
319 KOKKOS_INLINE_FUNCTION std::size_t
size()
const noexcept
325
326
327
328 KOKKOS_INLINE_FUNCTION discrete_domain_type
full_domain()
const
330 return discrete_domain_type(m_reference, discrete_vector_type(
size()));
334
335
336
340 return m_break_point_domain;
344
345
346
347
348
349 KOKKOS_INLINE_FUNCTION std::size_t
npoints()
const noexcept
351 return m_knot_domain.size() - 2 *
degree();
355
356
357
358
359
360 KOKKOS_INLINE_FUNCTION std::size_t
nbasis()
const noexcept
366
367
368
369
370
371
372 KOKKOS_INLINE_FUNCTION std::size_t
ncells()
const noexcept
378 KOKKOS_INLINE_FUNCTION discrete_element_type get_first_bspline_in_cell(
379 ddc::DiscreteElement<knot_discrete_dimension_type>
const& ic)
const
381 return m_reference + (ic - m_break_point_domain.front()).value();
385
386
387
388
389 KOKKOS_INLINE_FUNCTION
ddc::DiscreteElement<knot_discrete_dimension_type> find_cell_start(
390 ddc::Coordinate<CDim>
const& x)
const;
400
401
402
403
410concept non_uniform_bsplines = is_non_uniform_bsplines_v<DDim>;
416template <
class RandomIt>
418 RandomIt
const breaks_begin,
419 RandomIt
const breaks_end)
421 ddc::DiscreteElement<knot_discrete_dimension_type>(0),
423 (breaks_end - breaks_begin)
425 , m_break_point_domain(
426 ddc::DiscreteElement<knot_discrete_dimension_type>(
degree()),
428 (breaks_end - breaks_begin)))
429 , m_reference(
ddc::create_reference_discrete_element<DDim>())
431 std::vector<
ddc::Coordinate<CDim>> knots((breaks_end - breaks_begin) + 2 *
degree());
434 for (RandomIt it = breaks_begin; it < breaks_end; ++it) {
438 ddc::Coordinate<CDim>
const rmin = knots[
degree()];
439 ddc::Coordinate<CDim>
const rmax = knots[(breaks_end - breaks_begin) +
degree() - 1];
444 double const period = rmax - rmin;
445 for (std::size_t i = 1; i <
degree() + 1; ++i) {
451 for (std::size_t i = 1; i <
degree() + 1; ++i) {
456 ddc::init_discrete_space<knot_discrete_dimension_type>(knots);
459template <
class CDim, std::size_t D,
bool Periodic>
460template <
class DDim,
class MemorySpace>
463 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 1>> values,
464 ddc::Coordinate<CDim>
const& x)
const
466 KOKKOS_ASSERT(values.size() == D + 1)
473 KOKKOS_ASSERT(values.size() ==
degree() + 1)
476 ddc::DiscreteElement<knot_discrete_dimension_type>
const icell = find_cell_start(x);
478 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
479 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
480 KOKKOS_ASSERT(
ddc::coordinate(icell) - x <=
length() * 1e-14)
481 KOKKOS_ASSERT(x -
ddc::coordinate(icell + 1) <=
length() * 1e-14)
486 for (std::size_t j = 0; j <
degree(); ++j) {
487 left[j] = x -
ddc::coordinate(icell - j);
488 right[j] =
ddc::coordinate(icell + j + 1) - x;
490 for (std::size_t r = 0; r < j + 1; ++r) {
491 temp = values[r] / (right[r] + left[j - r]);
492 values[r] = saved + right[r] * temp;
493 saved = left[j - r] * temp;
495 values[j + 1] = saved;
498 return get_first_bspline_in_cell(icell);
501template <
class CDim, std::size_t D,
bool Periodic>
502template <
class DDim,
class MemorySpace>
505 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 1>> derivs,
506 ddc::Coordinate<CDim>
const& x)
const
513 KOKKOS_ASSERT(derivs.size() ==
degree() + 1)
516 ddc::DiscreteElement<knot_discrete_dimension_type>
const icell = find_cell_start(x);
518 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
519 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
520 KOKKOS_ASSERT(
ddc::coordinate(icell) - x <=
length() * 1e-14)
521 KOKKOS_ASSERT(x -
ddc::coordinate(icell + 1) <=
length() * 1e-14)
526
527
528
529
533 for (std::size_t j = 0; j <
degree() - 1; ++j) {
534 left[j] = x -
ddc::coordinate(icell - j);
535 right[j] =
ddc::coordinate(icell + j + 1) - x;
537 for (std::size_t r = 0; r < j + 1; ++r) {
538 temp = derivs[r] / (right[r] + left[j - r]);
539 derivs[r] = saved + right[r] * temp;
540 saved = left[j - r] * temp;
542 derivs[j + 1] = saved;
546
547
548
550 / (
ddc::coordinate(icell + 1) -
ddc::coordinate(icell + 1 -
degree()));
552 for (std::size_t j = 1; j <
degree(); ++j) {
555 / (
ddc::coordinate(icell + j + 1) -
ddc::coordinate(icell + j + 1 -
degree()));
556 derivs[j] = temp - saved;
560 return get_first_bspline_in_cell(icell);
563template <
class CDim, std::size_t D,
bool Periodic>
564template <
class DDim,
class MemorySpace>
566 Impl<DDim, MemorySpace>::eval_basis_and_n_derivs(
567 Kokkos::mdspan<
double, Kokkos::dextents<std::size_t, 2>>
const derivs,
568 ddc::Coordinate<CDim>
const& x,
569 std::size_t
const n)
const
574 std::array<
double, 2 * (
degree() + 1)> a_ptr;
575 Kokkos::mdspan<
double, Kokkos::extents<std::size_t,
degree() + 1, 2>>
const a(a_ptr.data());
578 Kokkos::mdspan<
double, Kokkos::extents<std::size_t,
degree() + 1,
degree() + 1>>
const ndu(
585 KOKKOS_ASSERT(derivs.extent(0) == 1 +
degree())
586 KOKKOS_ASSERT(derivs.extent(1) == 1 + n)
589 ddc::DiscreteElement<knot_discrete_dimension_type>
const icell = find_cell_start(x);
591 KOKKOS_ASSERT(icell >= m_break_point_domain.front())
592 KOKKOS_ASSERT(icell <= m_break_point_domain.back())
593 KOKKOS_ASSERT(
ddc::coordinate(icell) - x <=
length() * 1e-14)
594 KOKKOS_ASSERT(x -
ddc::coordinate(icell + 1) <=
length() * 1e-14)
606 DDC_MDSPAN_ACCESS_OP(ndu, 0, 0) = 1.0;
607 for (std::size_t j = 0; j <
degree(); ++j) {
608 left[j] = x -
ddc::coordinate(icell - j);
609 right[j] =
ddc::coordinate(icell + j + 1) - x;
611 for (std::size_t r = 0; r < j + 1; ++r) {
614 DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1) = 1.0 / (right[r] + left[j - r]);
617 temp = DDC_MDSPAN_ACCESS_OP(ndu, j, r) * DDC_MDSPAN_ACCESS_OP(ndu, r, j + 1);
618 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, r) = saved + right[r] * temp;
619 saved = left[j - r] * temp;
621 DDC_MDSPAN_ACCESS_OP(ndu, j + 1, j + 1) = saved;
624 for (std::size_t j = 0; j <
degree() + 1; ++j) {
625 DDC_MDSPAN_ACCESS_OP(derivs, j, 0) = DDC_MDSPAN_ACCESS_OP(ndu,
degree(), j);
628 for (
int r = 0; r <
static_cast<
int>(
degree() + 1); ++r) {
631 DDC_MDSPAN_ACCESS_OP(a, 0, 0) = 1.0;
632 for (
int k = 1; k <
static_cast<
int>(n + 1); ++k) {
634 int const rk = r - k;
637 DDC_MDSPAN_ACCESS_OP(a, 0, s2)
638 = DDC_MDSPAN_ACCESS_OP(a, 0, s1) * DDC_MDSPAN_ACCESS_OP(ndu, rk, pk + 1);
639 d = DDC_MDSPAN_ACCESS_OP(a, 0, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk);
641 int const j1 = rk > -1 ? 1 : (-rk);
642 int const j2 = (r - 1) <= pk ? k : (
degree() - r + 1);
643 for (
int j = j1; j < j2; ++j) {
644 DDC_MDSPAN_ACCESS_OP(a, j, s2)
645 = (DDC_MDSPAN_ACCESS_OP(a, j, s1) - DDC_MDSPAN_ACCESS_OP(a, j - 1, s1))
646 * DDC_MDSPAN_ACCESS_OP(ndu, rk + j, pk + 1);
647 d += DDC_MDSPAN_ACCESS_OP(a, j, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, rk + j);
650 DDC_MDSPAN_ACCESS_OP(a, k, s2) = -DDC_MDSPAN_ACCESS_OP(a, k - 1, s1)
651 * DDC_MDSPAN_ACCESS_OP(ndu, r, pk + 1);
652 d += DDC_MDSPAN_ACCESS_OP(a, k, s2) * DDC_MDSPAN_ACCESS_OP(ndu, pk, r);
654 DDC_MDSPAN_ACCESS_OP(derivs, r, k) = d;
655 Kokkos::kokkos_swap(s1, s2);
660 for (
int k = 1; k <
static_cast<
int>(n + 1); ++k) {
661 for (std::size_t i = 0; i < derivs.extent(0); ++i) {
662 DDC_MDSPAN_ACCESS_OP(derivs, i, k) *= r;
667 return get_first_bspline_in_cell(icell);
670template <
class CDim, std::size_t D,
bool Periodic>
671template <
class DDim,
class MemorySpace>
675 Periodic>::
Impl<DDim, MemorySpace>::find_cell_start(
ddc::Coordinate<CDim>
const& x)
const
681 return m_break_point_domain.front();
684 return m_break_point_domain.back() - 1;
688 ddc::DiscreteElement<knot_discrete_dimension_type> low = m_break_point_domain.front();
689 ddc::DiscreteElement<knot_discrete_dimension_type> high = m_break_point_domain.back();
690 ddc::DiscreteElement<knot_discrete_dimension_type> icell = low + (high - low) / 2;
691 while (x <
ddc::coordinate(icell) || x >=
ddc::coordinate(icell + 1)) {
692 if (x <
ddc::coordinate(icell)) {
697 icell = low + (high - low) / 2;
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
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.