DDC 0.0.0

a discrete domain computation library

spline_evaluator.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
9#include <ddc/ddc.hpp>
10
11#include "Kokkos_Macros.hpp"
12#include "view.hpp"
13
14namespace ddc {
15
16template <
17 class ExecSpace,
18 class MemorySpace,
19 class BSplinesType,
23 class... IDimX>
25{
26private:
27 // Tags to determine what to evaluate
28 struct eval_type
29 {
30 };
31
32 struct eval_deriv_type
33 {
34 };
35
36 using tag_type = typename BSplinesType::tag_type;
37
38public:
40
42
44
47
49
51
53
55
57 typename ddc::detail::convert_type_seq_to_discrete_domain<ddc::type_seq_remove_t<
58 ddc::detail::TypeSeq<IDimX...>,
59 ddc::detail::TypeSeq<interpolation_mesh_type>>>;
60
62 typename ddc::detail::convert_type_seq_to_discrete_domain<ddc::type_seq_replace_t<
63 ddc::detail::TypeSeq<IDimX...>,
64 ddc::detail::TypeSeq<interpolation_mesh_type>,
65 ddc::detail::TypeSeq<bsplines_type>>>;
66
67
68private:
69 LeftExtrapolationRule m_left_extrap_rule;
70
71 RightExtrapolationRule m_right_extrap_rule;
72
73public:
74 static_assert(
75 std::is_same_v<LeftExtrapolationRule,
77 tag_type>> == bsplines_type::is_periodic()
78 && std::is_same_v<
81 tag_type>> == bsplines_type::is_periodic(),
82 "PeriodicExtrapolationRule has to be used if and only if dimension is periodic");
83 static_assert(
84 std::is_invocable_r_v<
85 double,
89 double const,
91 std::experimental::layout_right,
93 "LeftExtrapolationRule::operator() has to be callable with usual arguments.");
94 static_assert(
95 std::is_invocable_r_v<
96 double,
100 double const,
102 std::experimental::layout_right,
103 memory_space>>,
104 "RightExtrapolationRule::operator() has to be callable with usual arguments.");
105
109 : m_left_extrap_rule(left_extrap_rule)
110 , m_right_extrap_rule(right_extrap_rule)
111 {
112 }
113
115
117
118 ~SplineEvaluator() = default;
119
121
123
125 {
126 return m_left_extrap_rule;
127 }
128
130 {
131 return m_right_extrap_rule;
132 }
133
134 template <class Layout, class... CoordsDims>
142
143 template <class Layout1, class Layout2, class Layout3, class... CoordsDims>
150 Layout2,
153 spline_coef) const
154 {
155 interpolation_domain_type const interpolation_domain(spline_eval.domain());
156 batch_domain_type const batch_domain(spline_eval.domain());
157
159 exec_space(),
160 batch_domain,
161 KOKKOS_CLASS_LAMBDA(typename batch_domain_type::discrete_element_type const j) {
162 const auto spline_eval_1D = spline_eval[j];
163 const auto coords_eval_1D = coords_eval[j];
164 const auto spline_coef_1D = spline_coef[j];
165 for (auto const i : interpolation_domain) {
167 }
168 });
169 }
170
171 template <class Layout, class... CoordsDims>
179
180 template <class Layout1, class Layout2, class Layout3, class... CoordsDims>
181 void deriv(
187 Layout2,
190 spline_coef) const
191 {
192 interpolation_domain_type const interpolation_domain(spline_eval.domain());
193 batch_domain_type const batch_domain(spline_eval.domain());
194
196 exec_space(),
197 batch_domain,
198 KOKKOS_CLASS_LAMBDA(typename batch_domain_type::discrete_element_type const j) {
199 const auto spline_eval_1D = spline_eval[j];
200 const auto coords_eval_1D = coords_eval[j];
201 const auto spline_coef_1D = spline_coef[j];
202 for (auto const i : interpolation_domain) {
205 }
206 });
207 }
208
209 template <class Layout1, class Layout2>
213 spline_coef) const
214 {
215 batch_domain_type const batch_domain(integrals.domain());
219 ddc::ChunkSpan values = values_alloc.span_view();
220 Kokkos::parallel_for(
221 Kokkos::RangePolicy<exec_space>(0, 1),
223
225 exec_space(),
226 batch_domain,
227 KOKKOS_LAMBDA(typename batch_domain_type::discrete_element_type const j) {
228 integrals(j) = 0;
229 for (typename spline_domain_type::discrete_element_type const i :
230 values.domain()) {
231 integrals(j) += spline_coef(i, j) * values(i);
232 }
233 });
234 }
235
236private:
237 template <class Layout, class... CoordsDims>
238 KOKKOS_INLINE_FUNCTION double eval(
241 spline_coef) const
242 {
246 coord_eval);
247 if constexpr (bsplines_type::is_periodic()) {
250 coord_eval_interpolation -= Kokkos::floor(
255 }
256 } else {
258 return m_left_extrap_rule(coord_eval_interpolation, spline_coef);
259 }
261 return m_right_extrap_rule(coord_eval_interpolation, spline_coef);
262 }
263 }
265 }
266
267 template <class EvalType, class Layout, class... CoordsDims>
268 KOKKOS_INLINE_FUNCTION double eval_no_bc(
271 spline_coef) const
272 {
273 static_assert(
274 std::is_same_v<EvalType, eval_type> || std::is_same_v<EvalType, eval_deriv_type>);
276 std::array<double, bsplines_type::degree() + 1> vals_ptr;
277 std::experimental::mdspan<
278 double,
279 std::experimental::extents<std::size_t, bsplines_type::degree() + 1>> const
280 vals(vals_ptr.data());
284 coord_eval);
285 if constexpr (std::is_same_v<EvalType, eval_type>) {
287 } else if constexpr (std::is_same_v<EvalType, eval_deriv_type>) {
289 }
290 double y = 0.0;
291 for (std::size_t i = 0; i < bsplines_type::degree() + 1; ++i) {
293 }
294 return y;
295 }
296};
297} // namespace ddc
Definition discrete_domain.hpp:51
Definition kokkos_allocator.hpp:17
Definition spline_evaluator.hpp:25
~SplineEvaluator()=default
MemorySpace memory_space
Definition spline_evaluator.hpp:41
KOKKOS_FUNCTION double operator()(ddc::Coordinate< CoordsDims... > const &coord_eval, ddc::ChunkSpan< double const, spline_domain_type, Layout, memory_space > const spline_coef) const
Definition spline_evaluator.hpp:135
KOKKOS_FUNCTION double deriv(ddc::Coordinate< CoordsDims... > const &coord_eval, ddc::ChunkSpan< double const, spline_domain_type, Layout, memory_space > const spline_coef) const
Definition spline_evaluator.hpp:172
SplineEvaluator & operator=(SplineEvaluator &&x)=default
RightExtrapolationRule right_extrapolation_rule_type
Definition spline_evaluator.hpp:46
SplineEvaluator & operator=(SplineEvaluator const &x)=default
typename ddc::detail::convert_type_seq_to_discrete_domain< ddc::type_seq_remove_t< ddc::detail::TypeSeq< IDimX... >, ddc::detail::TypeSeq< interpolation_mesh_type > > > batch_domain_type
Definition spline_evaluator.hpp:56
ddc::DiscreteDomain< bsplines_type > spline_domain_type
Definition spline_evaluator.hpp:54
void integrate(ddc::ChunkSpan< double, batch_domain_type, Layout1, memory_space > const integrals, ddc::ChunkSpan< double const, batched_spline_domain_type, Layout2, memory_space > const spline_coef) const
Definition spline_evaluator.hpp:210
SplineEvaluator(SplineEvaluator &&x)=default
void deriv(ddc::ChunkSpan< double, batched_interpolation_domain_type, Layout1, memory_space > const spline_eval, ddc::ChunkSpan< ddc::Coordinate< CoordsDims... > const, batched_interpolation_domain_type, Layout2, memory_space > const coords_eval, ddc::ChunkSpan< double const, batched_spline_domain_type, Layout3, memory_space > const spline_coef) const
Definition spline_evaluator.hpp:181
ExecSpace exec_space
Definition spline_evaluator.hpp:39
right_extrapolation_rule_type right_extrapolation_rule() const
Definition spline_evaluator.hpp:129
LeftExtrapolationRule left_extrapolation_rule_type
Definition spline_evaluator.hpp:45
SplineEvaluator(LeftExtrapolationRule const &left_extrap_rule, RightExtrapolationRule const &right_extrap_rule)
Definition spline_evaluator.hpp:106
void operator()(ddc::ChunkSpan< double, batched_interpolation_domain_type, Layout1, memory_space > const spline_eval, ddc::ChunkSpan< ddc::Coordinate< CoordsDims... > const, batched_interpolation_domain_type, Layout2, memory_space > const coords_eval, ddc::ChunkSpan< double const, batched_spline_domain_type, Layout3, memory_space > const spline_coef) const
Definition spline_evaluator.hpp:144
BSplinesType bsplines_type
Definition spline_evaluator.hpp:43
InterpolationMesh interpolation_mesh_type
Definition spline_evaluator.hpp:48
typename ddc::detail::convert_type_seq_to_discrete_domain< ddc::type_seq_replace_t< ddc::detail::TypeSeq< IDimX... >, ddc::detail::TypeSeq< interpolation_mesh_type >, ddc::detail::TypeSeq< bsplines_type > > > batched_spline_domain_type
Definition spline_evaluator.hpp:61
SplineEvaluator(SplineEvaluator const &x)=default
left_extrapolation_rule_type left_extrapolation_rule() const
Definition spline_evaluator.hpp:124
The top-level namespace of DDC.
Definition aligned_allocator.hpp:11
constexpr bool enable_chunk
Definition chunk_traits.hpp:16
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmax(DiscreteDomain< DDim > const &d)
Definition non_uniform_point_sampling.hpp:182
void parallel_for_each(std::string const &label, ExecSpace const &execution_space, DiscreteDomain< DDims... > const &domain, Functor &&f) noexcept
iterates over a nD domain using a given Kokkos execution space
Definition parallel_for_each.hpp:155
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmin(DiscreteDomain< DDim > const &d)
Definition non_uniform_point_sampling.hpp:175
detail::TaggedVector< CoordinateElement, CDims... > Coordinate
A Coordinate represents a coordinate in the continuous space.
Definition coordinate.hpp:26
Definition chunk.hpp:21
Definition chunk_span.hpp:30
Definition periodic_extrapolation_rule.hpp:13