SimiLie
Loading...
Searching...
No Matches
reduction_and_reconstruction.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7
8#include <ddc/ddc.hpp>
9
10#include <similie/exterior/cochain_indices.hpp>
11#include <similie/exterior/concepts.hpp>
12#include <similie/exterior/staged_operator/staged_operator.hpp>
13#include <similie/exterior/volume.hpp>
14#include <similie/misc/clamp_to_domain.hpp>
15#include <similie/misc/factorial.hpp>
16#include <similie/misc/small_matrix.hpp>
17#include <similie/misc/specialization.hpp>
18#include <similie/misc/type_seq_conversion.hpp>
19#include <similie/tensor/tensor_impl.hpp>
20#include <similie/tensor/tensor_indices/antisymmetric_tensor.hpp>
21#include <similie/tensor/tensor_indices/character.hpp>
22#include <similie/tensor/tensor_indices/full_tensor.hpp>
23#include <similie/tensor/tensor_indices/identity_tensor.hpp>
24#include <similie/tensor/tensor_indices/prime.hpp>
25#include <similie/tensor/tensor_prod.hpp>
26
27
28namespace sil {
29
30namespace exterior {
31
32namespace detail {
33
34template <misc::Specialization<ddc::TypeSeq> Indices>
35struct ReductionTargetIndex;
36
37template <>
38struct ReductionTargetIndex<ddc::TypeSeq<>>
39{
40 using type = tensor::Covariant<tensor::ScalarIndex>;
41};
42
43template <tensor::TensorNatIndex Index>
44struct ReductionTargetIndex<ddc::TypeSeq<Index>>
45{
46 using type = Index;
47};
48
49template <tensor::TensorNatIndex... Index>
50 requires(sizeof...(Index) > 1)
51struct ReductionTargetIndex<ddc::TypeSeq<Index...>>
52{
53 using type = tensor::TensorAntisymmetricIndex<Index...>;
54};
55
56template <std::size_t N>
57KOKKOS_FUNCTION bool has_unique_reduction_ids(std::array<std::size_t, N> const& ids)
58{
59 for (std::size_t i = 0; i < N; ++i) {
60 for (std::size_t j = i + 1; j < N; ++j) {
61 if (ids[i] == ids[j]) {
62 return false;
63 }
64 }
65 }
66 return true;
67}
68
69template <std::size_t N>
70KOKKOS_FUNCTION bool have_same_reduction_ids(
71 std::array<std::size_t, N> const& lhs,
72 std::array<std::size_t, N> const& rhs)
73{
74 for (std::size_t i = 0; i < N; ++i) {
75 if (lhs[i] != rhs[i]) {
76 return false;
77 }
78 }
79 return true;
80}
81
82template <std::size_t K>
83KOKKOS_FUNCTION double reduction_determinant(std::array<double, K * K> const& matrix)
84{
85 if constexpr (K == 0) {
86 return 1.;
87 } else if constexpr (K == 1) {
88 return matrix[0];
89 } else {
90 double det = 0.;
91 for (std::size_t col = 0; col < K; ++col) {
92 std::array<double, (K - 1) * (K - 1)> minor {};
93 for (std::size_t row = 1; row < K; ++row) {
94 std::size_t minor_col = 0;
95 for (std::size_t source_col = 0; source_col < K; ++source_col) {
96 if (source_col == col) {
97 continue;
98 }
99 minor[(row - 1) * (K - 1) + minor_col] = matrix[row * K + source_col];
100 ++minor_col;
101 }
102 }
103 double const sign = (col % 2 == 0) ? 1. : -1.;
104 det += sign * matrix[col] * reduction_determinant<K - 1>(minor);
105 }
106 return det;
107 }
108}
109
110template <std::size_t N, std::size_t K>
111KOKKOS_FUNCTION std::array<std::size_t, K> combination_from_rank(std::size_t rank)
112{
113 std::array<std::size_t, K> ids {};
114 if constexpr (K == 0) {
115 return ids;
116 } else {
117 std::size_t next_candidate = 0;
118 for (std::size_t i = 0; i < K; ++i) {
119 for (std::size_t candidate = next_candidate; candidate < N; ++candidate) {
120 std::size_t const remaining = K - i - 1;
121 std::size_t const remaining_space = N - candidate - 1;
122 std::size_t const count
123 = (remaining == 0) ? 1
124 : misc::binomial_coefficient(remaining_space, remaining);
125 if (rank < count) {
126 ids[i] = candidate;
127 next_candidate = candidate + 1;
128 break;
129 }
130 rank -= count;
131 }
132 }
133 return ids;
134 }
135}
136
137template <std::size_t N, std::size_t K>
138KOKKOS_FUNCTION std::size_t combination_rank(std::array<std::size_t, K> const& ids)
139{
140 if constexpr (K == 0) {
141 return 0;
142 } else {
143 std::size_t rank = 0;
144 std::size_t next_candidate = 0;
145 for (std::size_t i = 0; i < K; ++i) {
146 for (std::size_t candidate = next_candidate; candidate < ids[i]; ++candidate) {
147 std::size_t const remaining = K - i - 1;
148 std::size_t const remaining_space = N - candidate - 1;
149 rank += (remaining == 0) ? 1
150 : misc::binomial_coefficient(remaining_space, remaining);
151 }
152 next_candidate = ids[i] + 1;
153 }
154 return rank;
155 }
156}
157
158template <std::size_t N, std::size_t M>
159KOKKOS_FUNCTION bool hodge_has_unique_ids(std::array<std::size_t, M> const& ids)
160{
161 if constexpr (M > 0) {
162 for (std::size_t i = 0; i < M; ++i) {
163 if (ids[i] >= N) {
164 return false;
165 }
166 for (std::size_t j = i + 1; j < M; ++j) {
167 if (ids[i] == ids[j]) {
168 return false;
169 }
170 }
171 }
172 }
173 return true;
174}
175
176template <std::size_t N, std::size_t M1, std::size_t M2>
177KOKKOS_FUNCTION bool hodge_is_complete_permutation(
178 std::array<std::size_t, M1> const& source_ids,
179 std::array<std::size_t, M2> const& target_ids)
180{
181 std::array<int, N> counts {};
182 for (std::size_t id : source_ids) {
183 if (id >= N) {
184 return false;
185 }
186 counts[id]++;
187 }
188 for (std::size_t id : target_ids) {
189 if (id >= N) {
190 return false;
191 }
192 counts[id]++;
193 }
194 for (int count : counts) {
195 if (count != 1) {
196 return false;
197 }
198 }
199 return true;
200}
201
202template <std::size_t N, std::size_t M1, std::size_t M2>
203KOKKOS_FUNCTION int hodge_permutation_sign(
204 std::array<std::size_t, M1> const& source_ids,
205 std::array<std::size_t, M2> const& target_ids)
206{
207 std::array<std::size_t, N> permutation {};
208 for (std::size_t i = 0; i < M1; ++i) {
209 permutation[i] = source_ids[i];
210 }
211 if constexpr (M2 > 0) {
212 for (std::size_t i = 0; i < M2; ++i) {
213 permutation[M1 + i] = target_ids[i];
214 }
215 }
216
217 bool odd = false;
218 for (std::size_t i = 0; i < N; ++i) {
219 for (std::size_t j = i + 1; j < N; ++j) {
220 odd = (permutation[i] > permutation[j]) != odd;
221 }
222 }
223 return odd ? -1 : 1;
224}
225
226template <std::size_t N, std::size_t M>
227KOKKOS_FUNCTION std::array<std::size_t, N - M> hodge_complement_ids(
228 std::array<std::size_t, M> const& ids)
229{
230 std::array<std::size_t, N - M> complement {};
231 std::size_t complement_id = 0;
232 for (std::size_t i = 0; i < N; ++i) {
233 bool found = false;
234 for (std::size_t id : ids) {
235 found = found || (id == i);
236 }
237 if (!found) {
238 complement[complement_id++] = i;
239 }
240 }
241 return complement;
242}
243
244template <std::size_t N, std::size_t K, class MetricType, class BatchElem>
245KOKKOS_FUNCTION double continuous_hodge_value_from_ids(
246 MetricType const& metric,
247 BatchElem elem,
248 std::array<std::size_t, K> const& source_ids,
249 std::array<std::size_t, N - K> const& target_ids)
250{
251 if (!hodge_has_unique_ids<N>(source_ids) || !hodge_has_unique_ids<N>(target_ids)) {
252 return 0.;
253 }
254
255 std::array<std::size_t, K> const complement = hodge_complement_ids<N>(target_ids);
256 using MetricIndex
257 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename MetricType::indices_domain_t>>;
258 if constexpr (misc::Specialization<MetricIndex, tensor::TensorIdentityIndex>) {
259 return hodge_is_complete_permutation<N>(source_ids, target_ids)
260 ? static_cast<double>(hodge_permutation_sign<N>(source_ids, target_ids))
261 / misc::factorial(K)
262 : 0.;
263 }
264 using MetricIndex1 = tensor::metric_index_1<MetricIndex>;
265 using MetricIndex2 = tensor::metric_index_2<MetricIndex>;
266 std::array<double, N * N> metric_alloc {};
267 std::array<double, N * N> determinant_metric_alloc {};
268 std::array<double, N * N> inverse_metric_alloc {};
269 std::array<double, N * N> workspace_alloc {};
270 std::array<double, K * K> submatrix_alloc {};
271 for (std::size_t i = 0; i < N; ++i) {
272 for (std::size_t j = 0; j < N; ++j) {
273 metric_alloc[i * N + j] = metric.get(metric.access_element(
274 elem,
275 ddc::DiscreteElement<MetricIndex1, MetricIndex2>(i, j)));
276 }
277 }
278 determinant_metric_alloc = metric_alloc;
279
280 auto determinant_metric_view = misc::math::matrix_view<
281 double,
282 typename MetricType::memory_space>(determinant_metric_alloc.data(), N, N);
283 auto metric_view = misc::math::
285 double const determinant = misc::math::determinant(determinant_metric_view);
286 auto inverse_metric_view = misc::math::matrix_view<
287 double,
288 typename MetricType::memory_space>(inverse_metric_alloc.data(), N, N);
289 auto workspace = misc::math::
291 if (!misc::math::invert(inverse_metric_view, metric_view, workspace)) {
292 return 0.;
293 }
294
295 return Kokkos::sqrt(Kokkos::abs(determinant))
296 * static_cast<double>(hodge_permutation_sign<N>(complement, target_ids))
298 K>(inverse_metric_view, source_ids, complement, submatrix_alloc)
299 / misc::factorial(K);
300}
301
302template <
303 CellComplex Complex,
304 std::size_t N,
305 std::size_t K,
306 class MetricType,
307 class PositionType,
308 class BatchElem>
309KOKKOS_FUNCTION double legacy_discrete_hodge_value_from_ids(
310 MetricType const& metric,
311 PositionType const& position,
312 BatchElem elem,
313 std::array<std::size_t, K> const& source_ids,
314 std::array<std::size_t, N - K> const& target_ids)
315{
316 if (!hodge_has_unique_ids<N>(source_ids) || !hodge_has_unique_ids<N>(target_ids)
317 || !hodge_is_complete_permutation<N>(source_ids, target_ids)) {
318 return 0.;
319 }
320 double const primal_volume
321 = SimplexVolume<CellComplex::Primal, N, MetricType, PositionType, BatchElem>::template
322 operator()<K>(metric, position, elem, source_ids);
323 if (primal_volume == 0.) {
324 return 0.;
325 }
326
327 return static_cast<double>(hodge_permutation_sign<N>(source_ids, target_ids))
328 * DualSimplexVolume<Complex, N, MetricType, PositionType, BatchElem>::template
329 operator()<K>(metric, position, elem, source_ids)
330 / (primal_volume * misc::factorial(K));
331}
332
333template <class ReductionNaturalElemType, std::size_t N1, std::size_t N2>
334KOKKOS_FUNCTION ReductionNaturalElemType merge_reduction_natural_elems(
335 std::array<std::size_t, N1> const& first_ids,
336 std::array<std::size_t, N2> const& second_ids)
337{
338 std::array<std::size_t, N1 + N2> reduction_ids {};
339 for (std::size_t i = 0; i < N1; ++i) {
340 reduction_ids[i] = first_ids[i];
341 }
342 for (std::size_t i = 0; i < N2; ++i) {
343 reduction_ids[N1 + i] = second_ids[i];
344 }
345 return natural_elem_from_flat_ids<ReductionNaturalElemType>(reduction_ids);
346}
347
348
349} // namespace detail
350
351template <misc::Specialization<ddc::TypeSeq> Indices>
352using reduction_index_t = typename detail::ReductionTargetIndex<Indices>::type;
353
354template <misc::Specialization<ddc::TypeSeq> Indices>
355using reduction_domain_t = ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_merge_t<
356 ddc::TypeSeq<misc::convert_type_seq_to_t<
359 ddc::TypeSeq<reduction_index_t<Indices>>>>;
360
361template <misc::Specialization<ddc::TypeSeq> Indices>
363 = ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_merge_t<
364 ddc::TypeSeq<reduction_index_t<Indices>>,
365 ddc::TypeSeq<reduction_index_t<tensor::primes<Indices>>>>>;
366
367template <
370 class BatchElem,
372 template <class...> class Coefficients = tensor::TensorFullIndex>
373struct Reduction
374{
375private:
377 using CoefficientIndex = detail::
378 LocalCoefficientIndex<ComponentSource, reduction_index_t<Indices>, Coefficients>;
379
380public:
381 static constexpr bool input_is_form = true;
382 static constexpr bool output_is_form = false;
384 using staged_storage_type = detail::LinearLocalOperatorStorage<
385 Reduction,
389 typename PositionType::memory_space,
390 typename PositionType::memory_space::execution_space,
391 Coefficients>;
401 using position_index_type = ddc::
402 type_seq_element_t<0, ddc::to_type_seq_t<typename PositionType::indices_domain_t>>;
403
404 template <CochainType ReductionTensorType, FormType FormTensorType>
405 KOKKOS_FUNCTION static void operator()(
406 ReductionTensorType const& reduced_tensor,
407 FormTensorType const& form_tensor,
408 PositionType const& position,
409 BatchElem elem)
410 {
411 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
412#ifndef NDEBUG
413 detail::assert_local_diagonal<Coefficients>(
415#endif
416 detail::apply_local_diagonal<
417 ComponentSource,
419 Coefficients>(reduced_tensor, form_tensor, diagonal_weights(position, elem));
420 return;
421 }
422 using reduction_accessor_t
424 using reduction_natural_elem_type =
425 typename reduction_accessor_t::natural_domain_t::discrete_element_type;
426 using form_natural_elem_type =
427 typename FormTensorType::accessor_t::natural_domain_t::discrete_element_type;
428 if constexpr (target_index_type::rank() == 0) {
429 reduced_tensor.mem(reduced_tensor.domain().front())
430 = form_tensor.get(form_tensor.access_element(form_natural_elem_type()));
431 } else {
432 [[maybe_unused]] sil::tensor::TensorAccessor<source_index_type> source_accessor;
433 ddc::device_for_each(reduced_tensor.domain(), [&](auto target_mem_elem) {
434 auto const target_natural_elem
435 = reduced_tensor.canonical_natural_element(target_mem_elem);
436 double reduced_value = 0.;
437 ddc::device_for_each(
438 source_accessor.natural_domain(),
439 [&](auto source_natural_elem) {
440 reduction_natural_elem_type reduction_natural_elem;
441 auto const source_ids = ddc::detail::array(source_natural_elem);
442 auto const target_ids = ddc::detail::array(target_natural_elem);
443 reduction_natural_elem = detail::merge_reduction_natural_elems<
444 reduction_natural_elem_type>(source_ids, target_ids);
445
446 form_natural_elem_type form_natural_elem;
447 ddc::detail::array(form_natural_elem)
448 = ddc::detail::array(source_natural_elem);
449
450 reduced_value += value(position, elem, reduction_natural_elem)
451 * form_tensor.get(
452 form_tensor.access_element(form_natural_elem));
453 });
454 reduced_tensor.mem(target_mem_elem) = reduced_value;
455 });
456 }
457 }
458
459 template <
460 CochainType ReductionTensorType,
461 FormType FormTensorType,
463 KOKKOS_FUNCTION static void operator()(
464 ReductionTensorType const& reduced_tensor,
465 FormTensorType const& form_tensor,
466 MetricType const& metric,
467 PositionType const& position,
468 BatchElem elem)
469 {
470 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
471#ifndef NDEBUG
472 detail::assert_local_diagonal<Coefficients>(
474 value(metric, position, elem));
475 detail::assert_local_diagonal<Coefficients>(
477 value(position, elem));
478#endif
479 detail::apply_local_diagonal<ComponentSource, target_index_type, Coefficients>(
480 reduced_tensor,
481 form_tensor,
482 diagonal_weights(metric, position, elem));
483 return;
484 }
485 if constexpr (Complex == CellComplex::Primal) {
486 // Reduce the physical form on its full tangent basis: diagonal
487 // volume ratios alone lose the cross terms of an oblique chart.
488 operator()(reduced_tensor, form_tensor, position, elem);
489 } else {
490 static_assert(
492 "Metric-aware reduction is only implemented for the circumcentric dual "
493 "complex.");
494
495 constexpr std::size_t N = position_index_type::size();
496 constexpr std::size_t Q = target_index_type::rank();
497 constexpr std::size_t K = N - Q;
498 constexpr std::size_t NBASIS = misc::binomial_coefficient(N, Q);
499 static_assert(
500 NBASIS == misc::binomial_coefficient(N, K),
501 "Complementary basis sizes must match.");
502
503 std::array<double, NBASIS * NBASIS> coeff_from_primal_alloc {};
504 std::array<double, NBASIS * NBASIS> coeff_from_primal_inverse_alloc {};
505 std::array<double, NBASIS * NBASIS> old_hodge_alloc {};
506 std::array<double, NBASIS * NBASIS> dual_reduction_alloc {};
507
508 // Use the same full primal reduction matrix as Reconstruction.
509 // Diagonal Cartesian projections vanish under rotations and do not
510 // invert the reconstruction on an oblique mapped cell.
511 std::array<double, NBASIS * NBASIS> primal_reduction_alloc {};
512 std::array<double, NBASIS * NBASIS> primal_inverse_alloc {};
513 std::array<double, NBASIS * NBASIS> primal_workspace_alloc {};
514 for (std::size_t logical = 0; logical < NBASIS; ++logical) {
515 std::array<std::size_t, K> const logical_ids
516 = detail::combination_from_rank<N, K>(logical);
517 for (std::size_t physical = 0; physical < NBASIS; ++physical) {
518 std::array<std::size_t, K> const physical_ids
519 = detail::combination_from_rank<N, K>(physical);
520 std::array<double, K * K> minor {};
521 for (std::size_t col = 0; col < K; ++col) {
522 std::array<double, N> const edge = detail::edge_vector<
523 position_index_type>(position, elem, logical_ids[col]);
524 for (std::size_t row = 0; row < K; ++row)
525 minor[row * K + col] = edge[physical_ids[row]];
526 }
527 primal_reduction_alloc[logical * NBASIS + physical]
528 = detail::reduction_determinant<K>(minor) / misc::factorial(K);
529 }
530 }
531 bool const primal_invertible = misc::math::invert(
533 double,
534 typename MetricType::
535 memory_space>(primal_inverse_alloc.data(), NBASIS, NBASIS),
537 double,
538 typename MetricType::
539 memory_space>(primal_reduction_alloc.data(), NBASIS, NBASIS),
541 double,
542 typename MetricType::
543 memory_space>(primal_workspace_alloc.data(), NBASIS * NBASIS));
544
545 for (std::size_t primal_id = 0; primal_id < NBASIS; ++primal_id) {
546 std::array<std::size_t, K> const primal_ids
547 = detail::combination_from_rank<N, K>(primal_id);
548 for (std::size_t source_id = 0; source_id < NBASIS; ++source_id) {
549 std::array<std::size_t, Q> const source_ids
550 = detail::combination_from_rank<N, Q>(source_id);
551 for (std::size_t physical = 0; physical < NBASIS; ++physical) {
552 std::array<std::size_t, K> const physical_ids
553 = detail::combination_from_rank<N, K>(physical);
554 coeff_from_primal_alloc[source_id * NBASIS + primal_id]
555 += detail::continuous_hodge_value_from_ids<
556 N,
557 K>(metric, elem, physical_ids, source_ids)
558 * primal_inverse_alloc[physical * NBASIS + primal_id]
559 / misc::factorial(K);
560 }
561 }
562 for (std::size_t target_id = 0; target_id < NBASIS; ++target_id) {
563 std::array<std::size_t, Q> const target_ids
564 = detail::combination_from_rank<N, Q>(target_id);
565 old_hodge_alloc[target_id * NBASIS + primal_id]
566 = detail::legacy_discrete_hodge_value_from_ids<
567 Complex,
568 N,
569 K>(metric, position, elem, primal_ids, target_ids);
570 }
571 }
572
573 std::array<double, NBASIS * NBASIS> workspace_alloc {};
574 auto coeff_from_primal_view = misc::math::matrix_view<
575 double,
576 typename MetricType::
577 memory_space>(coeff_from_primal_alloc.data(), NBASIS, NBASIS);
578 auto coeff_from_primal_inverse_view = misc::math::matrix_view<
579 double,
580 typename MetricType::
581 memory_space>(coeff_from_primal_inverse_alloc.data(), NBASIS, NBASIS);
582 auto workspace = misc::math::vector_view<
583 double,
584 typename MetricType::memory_space>(workspace_alloc.data(), NBASIS * NBASIS);
585 bool const invertible = primal_invertible
586 && misc::math::
587 invert(coeff_from_primal_inverse_view,
588 coeff_from_primal_view,
589 workspace);
590
591 ddc::device_for_each(reduced_tensor.domain(), [&](auto target_mem_elem) {
592 auto const target_natural_elem
593 = reduced_tensor.canonical_natural_element(target_mem_elem);
594 std::size_t const target_id = [&]() {
595 if constexpr (Q == 0) {
596 return std::size_t(0);
597 } else {
598 std::array<std::size_t, Q> const target_ids
599 = ddc::detail::array(target_natural_elem);
600 return detail::combination_rank<N, Q>(target_ids);
601 }
602 }();
603
604 double reduced_value = 0.;
605 ddc::device_for_each(form_tensor.domain(), [&](auto source_mem_elem) {
606 auto const source_natural_elem
607 = form_tensor.canonical_natural_element(source_mem_elem);
608 std::size_t const source_id = [&]() {
609 if constexpr (Q == 0) {
610 return std::size_t(0);
611 } else {
612 std::array<std::size_t, Q> const source_ids
613 = ddc::detail::array(source_natural_elem);
614 return detail::combination_rank<N, Q>(source_ids);
615 }
616 }();
617 double dual_reduction_value = 0.;
618 if (invertible) {
619 for (std::size_t primal_id = 0; primal_id < NBASIS; ++primal_id) {
620 dual_reduction_value += old_hodge_alloc[target_id * NBASIS + primal_id]
621 * coeff_from_primal_inverse_alloc
622 [primal_id * NBASIS + source_id];
623 }
624 }
625
626 using form_natural_elem_type = typename FormTensorType::accessor_t::
627 natural_domain_t::discrete_element_type;
628 form_natural_elem_type form_natural_elem;
629 ddc::detail::array(form_natural_elem) = ddc::detail::array(source_natural_elem);
630 reduced_value += dual_reduction_value * form_tensor.mem(source_mem_elem);
631 });
632 reduced_tensor.mem(target_mem_elem) = reduced_value;
633 });
634 }
635 }
637 KOKKOS_FUNCTION static double value(
638 PositionType const& position,
639 BatchElem elem,
640 auto natural_elem)
641 {
643 return detail::local_stencil_coefficient<ComponentSource, target_index_type>(
644 value(position, elem),
645 ddc::detail::array(source_natural_elem_type(natural_elem)),
646 ddc::detail::array(target_natural_elem_type(natural_elem)));
647 }
648 constexpr std::size_t K = target_index_type::rank();
649 if constexpr (K == 0) {
650 return 1.;
651 } else {
652 std::array const source_ids
653 = ddc::detail::array(source_natural_elem_type(natural_elem));
654 std::array const target_ids
655 = ddc::detail::array(target_natural_elem_type(natural_elem));
656 if (!detail::has_unique_reduction_ids<K>(source_ids)
657 || !detail::has_unique_reduction_ids<K>(target_ids)) {
658 return 0.;
659 }
660
661 std::array<double, K * K> jacobian_matrix {};
662 for (std::size_t row = 0; row < K; ++row) {
663 for (std::size_t col = 0; col < K; ++col) {
664 std::array<double, position_index_type::size()> const edge
665 = sil::exterior::detail::edge_vector<
666 position_index_type>(position, elem, target_ids[col]);
667 jacobian_matrix[row * K + col] = edge[source_ids[row]];
668 }
669 }
670 return detail::reduction_determinant<K>(jacobian_matrix)
671 * detail::complex_volume_factor<Complex, source_index_type::size()>(K)
672 / misc::factorial(K);
673 }
674 }
675
676 KOKKOS_FUNCTION static detail::LocalOperatorStencil<
679 value(PositionType const& position, BatchElem element)
680 {
682 detail::LocalOperatorStencil<Source, target_index_type> result;
683 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
684#ifndef NDEBUG
685 detail::assert_local_diagonal<Coefficients>(
687#endif
688 std::array<double, CoefficientIndex::mem_size()> const coefficients
689 = diagonal_weights(position, element);
690 return detail::local_diagonal_stencil<ComponentSource, target_index_type, Coefficients>(
691 coefficients);
692 }
693 auto unit = detail::make_stencil<typename PositionType::memory_space, Source>(
694 ddc::DiscreteElement<>());
695 auto output = detail::make_stencil<typename PositionType::memory_space, target_index_type>(
696 ddc::DiscreteElement<>());
697 for (std::size_t source = 0; source < Source::mem_size(); ++source) {
698 unit.mem(ddc::DiscreteElement<Source>(source)) = 1.0;
699 operator()(
700 Cochain(tensor::Tensor(output.span_view())),
701 Form(tensor::Tensor(unit.span_view())),
702 position,
703 element);
704 for (std::size_t target = 0; target < target_index_type::mem_size(); ++target)
705 result.weights[target * Source::mem_size() + source]
706 = output.mem(ddc::DiscreteElement<target_index_type>(target));
707 unit.mem(ddc::DiscreteElement<Source>(source)) = 0.0;
708 }
709 return result;
710 }
711
712 template <misc::Specialization<tensor::Tensor> MetricType>
713 KOKKOS_FUNCTION static detail::LocalOperatorStencil<
716 value(MetricType const& metric, PositionType const& position, BatchElem element)
717 {
719 detail::LocalOperatorStencil<Source, target_index_type> result;
720 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
721#ifndef NDEBUG
722 detail::assert_local_diagonal<Coefficients>(
724 value(metric, position, element));
725#endif
726 std::array<double, CoefficientIndex::mem_size()> const coefficients
727 = diagonal_weights(metric, position, element);
728 return detail::local_diagonal_stencil<ComponentSource, target_index_type, Coefficients>(
729 coefficients);
730 }
731 auto unit = detail::make_stencil<typename PositionType::memory_space, Source>(
732 ddc::DiscreteElement<>());
733 auto output = detail::make_stencil<typename PositionType::memory_space, target_index_type>(
734 ddc::DiscreteElement<>());
735 for (std::size_t source = 0; source < Source::mem_size(); ++source) {
736 unit.mem(ddc::DiscreteElement<Source>(source)) = 1.0;
737 operator()(
738 Cochain(tensor::Tensor(output.span_view())),
739 Form(tensor::Tensor(unit.span_view())),
740 metric,
741 position,
742 element);
743 for (std::size_t target = 0; target < target_index_type::mem_size(); ++target)
744 result.weights[target * Source::mem_size() + source]
745 = output.mem(ddc::DiscreteElement<target_index_type>(target));
746 unit.mem(ddc::DiscreteElement<Source>(source)) = 0.0;
747 }
748 return result;
749 }
750
751 template <misc::Specialization<tensor::Tensor> MetricType>
752 KOKKOS_FUNCTION static double value(
753 MetricType const& metric,
754 PositionType const& position,
755 BatchElem elem,
756 auto natural_elem)
757 {
759 return detail::local_stencil_coefficient<ComponentSource, target_index_type>(
760 value(metric, position, elem),
761 ddc::detail::array(source_natural_elem_type(natural_elem)),
762 ddc::detail::array(target_natural_elem_type(natural_elem)));
763 }
764 if constexpr (Complex == CellComplex::Primal) {
765 return value(position, elem, natural_elem);
766 } else {
767 [[maybe_unused]] sil::tensor::TensorAccessor<source_index_type> source_accessor;
768 std::array<double, source_index_type::mem_size()> source_alloc {};
769 ddc::ChunkSpan<
770 double,
771 ddc::DiscreteDomain<source_index_type>,
772 Kokkos::layout_right,
773 typename MetricType::memory_space>
774 source_span(source_alloc.data(), source_accessor.domain());
775 sil::tensor::Tensor source_tensor(source_span);
776 ddc::device_for_each(source_tensor.domain(), [&](auto source_mem_elem) {
777 source_tensor.mem(source_mem_elem) = 0.;
778 });
779
780 [[maybe_unused]] sil::tensor::TensorAccessor<target_index_type> target_accessor;
781 std::array<double, target_index_type::mem_size()> target_alloc {};
782 ddc::ChunkSpan<
783 double,
784 ddc::DiscreteDomain<target_index_type>,
785 Kokkos::layout_right,
786 typename MetricType::memory_space>
787 target_span(target_alloc.data(), target_accessor.domain());
788 sil::tensor::Tensor target_tensor(target_span);
789 ddc::device_for_each(target_tensor.domain(), [&](auto target_mem_elem) {
790 target_tensor.mem(target_mem_elem) = 0.;
791 });
792
793 source_natural_elem_type source_natural_elem;
794 ddc::detail::array(source_natural_elem)
795 = ddc::detail::array(source_natural_elem_type(natural_elem));
796 if constexpr (source_index_type::rank() > 1) {
797 if (!detail::has_unique_reduction_ids(ddc::detail::array(source_natural_elem))) {
798 return 0.;
799 }
800 }
801 source_tensor(source_tensor.accessor().access_element(source_natural_elem)) = 1.;
802
803 operator()(target_tensor, source_tensor, metric, position, elem);
804
805 if constexpr (target_index_type::rank() == 0) {
806 return target_tensor.get(
807 target_tensor.accessor().access_element(target_natural_elem_type()));
808 } else {
809 target_natural_elem_type target_natural_elem;
810 ddc::detail::array(target_natural_elem)
811 = ddc::detail::array(target_natural_elem_type(natural_elem));
812 return target_tensor.get(
813 target_tensor.accessor().access_element(target_natural_elem));
814 }
815 }
816 }
817
818private:
819 KOKKOS_FUNCTION static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
820 PositionType const& position,
821 BatchElem elem)
822 {
823 std::array<double, CoefficientIndex::mem_size()> weights {};
824 if constexpr (target_index_type::rank() == 0) {
825 weights[0] = 1.;
826 } else {
827 for (std::size_t component = 0; component < weights.size(); ++component) {
828 weights[component]
829 = misc::factorial(target_index_type::rank())
830 * Reduction<Indices, PositionType, BatchElem, Complex>::
831 value(position,
832 elem,
833 detail::merge_reduction_natural_elems<
834 typename tensor::tensor_accessor_for_domain_t<
835 reduction_domain_t<Indices>>::
836 natural_domain_t::discrete_element_type>(
837 ComponentSource::mem_id_to_canonical_natural_ids(
838 component),
839 target_index_type::mem_id_to_canonical_natural_ids(
840 component)));
841 }
842 }
843 return weights;
844 }
845
846 template <class Metric>
847 KOKKOS_FUNCTION static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
848 Metric const& metric,
849 PositionType const& position,
850 BatchElem elem)
851 {
852 if constexpr (Complex == CellComplex::Primal) {
853 return diagonal_weights(position, elem);
854 } else {
855 static_assert(Complex == CellComplex::CircumcentricDual);
856 constexpr std::size_t N = position_index_type::size();
857 constexpr std::size_t Q = target_index_type::rank();
858 constexpr std::size_t K = N - Q;
859 std::array<double, CoefficientIndex::mem_size()> weights {};
860 for (std::size_t component = 0; component < weights.size(); ++component) {
861 std::array<std::size_t, Q> const target_ids
862 = target_index_type::mem_id_to_canonical_natural_ids(component);
863 std::array<std::size_t, K> const primal_ids
864 = detail::hodge_complement_ids<N>(target_ids);
865 std::array<double, K * K> minor {};
866 for (std::size_t col = 0; col < K; ++col) {
867 std::array<double, N> const edge = detail::edge_vector<
868 position_index_type>(position, elem, primal_ids[col]);
869 for (std::size_t row = 0; row < K; ++row)
870 minor[row * K + col] = edge[primal_ids[row]];
871 }
872 // H_discrete * inverse(H_continuous * inverse(R_primal) / K!)
873 // reduces to scalar division for a diagonal reduction.
874 double const hodge = detail::
875 continuous_hodge_value_from_ids<N, K>(metric, elem, primal_ids, target_ids);
876 if (hodge != 0.)
877 weights[component] = detail::legacy_discrete_hodge_value_from_ids<
878 Complex,
879 N,
880 K>(metric, position, elem, primal_ids, target_ids)
881 * detail::reduction_determinant<K>(minor) / hodge;
882 }
883 return weights;
884 }
885 }
886};
887
888template <
889 misc::Specialization<ddc::TypeSeq> Indices,
890 misc::Specialization<tensor::Tensor> ReductionTensorType,
891 misc::Specialization<tensor::Tensor> PositionType,
892 CellComplex Complex,
893 class BatchElem>
896 ReductionTensorType m_reduction_tensor;
897 PositionType m_position;
898 BatchElem m_elem;
899
900 template <class MemElem>
901 KOKKOS_FUNCTION void operator()(MemElem mem_elem) const
902 {
903 m_reduction_tensor.mem(
904 typename ReductionTensorType::discrete_element_type(m_elem, mem_elem))
906 value(m_position,
907 m_elem,
908 m_reduction_tensor.accessor().canonical_natural_element(mem_elem));
909 }
910};
911
912template <
914 misc::Specialization<tensor::Tensor> ReductionTensorType,
916 class ExecSpace,
917 CellComplex Complex = CellComplex::Primal>
918ReductionTensorType fill_reduction_operator(
919 ExecSpace const& exec_space,
920 ReductionTensorType const& reduction_tensor,
921 PositionType const& position)
922{
923 SIMILIE_DEBUG_LOG("similie_compute_reduction_operator");
924 ddc::parallel_for_each(
925 "similie_compute_reduction_operator",
926 exec_space,
927 reduction_tensor.non_indices_domain(),
928 KOKKOS_LAMBDA(
929 typename ReductionTensorType::non_indices_domain_t::discrete_element_type
930 elem) {
931 ddc::device_for_each(
932 reduction_tensor.accessor().domain(),
934 Indices,
935 ReductionTensorType,
936 PositionType,
937 Complex,
938 typename ReductionTensorType::non_indices_domain_t::
939 discrete_element_type> {reduction_tensor, position, elem});
940 });
941 return reduction_tensor;
942}
943
944namespace detail {
945
946template <class Indices, class PositionType, class BatchElem, CellComplex Complex>
947class ReconstructionMatrix
948{
949 using SourceIndex = reduction_index_t<Indices>;
950 using TargetIndex = reduction_index_t<tensor::primes<Indices>>;
951 static constexpr std::size_t matrix_size = SourceIndex::mem_size();
952 std::array<double, matrix_size * matrix_size> m_inverse {};
953 bool m_valid = false;
954
955public:
956 KOKKOS_FUNCTION ReconstructionMatrix(PositionType const& position, BatchElem elem)
957 {
958 static_assert(
959 Complex != CellComplex::BarycentricDual,
960 "Reconstruction is not implemented for the barycentric dual complex.");
961 if constexpr (SourceIndex::rank() == 0) {
962 m_inverse[0] = 1.;
963 m_valid = true;
964 } else {
965 [[maybe_unused]] tensor::TensorAccessor<SourceIndex> source_accessor;
966 [[maybe_unused]] tensor::TensorAccessor<TargetIndex> target_accessor;
967 std::array<double, matrix_size * matrix_size> reduction_alloc {};
968 std::array<double, matrix_size * matrix_size> workspace_alloc {};
969 misc::math::unmanaged_matrix_view_t<double, typename PositionType::memory_space> const
970 reduction_matrix
971 = misc::math::matrix_view<
972 double,
973 typename PositionType::
974 memory_space>(reduction_alloc.data(), matrix_size, matrix_size),
975 inverse_matrix = misc::math::matrix_view<
976 double,
977 typename PositionType::
978 memory_space>(m_inverse.data(), matrix_size, matrix_size);
979 misc::math::unmanaged_vector_view_t<double, typename PositionType::memory_space> const
980 workspace
981 = misc::math::vector_view<double, typename PositionType::memory_space>(
982 workspace_alloc.data(),
983 matrix_size * matrix_size);
984 ddc::device_for_each(source_accessor.domain(), [&](auto source_mem_elem) {
985 auto const source_natural_elem
986 = source_accessor.canonical_natural_element(source_mem_elem);
987 ddc::device_for_each(target_accessor.domain(), [&](auto target_mem_elem) {
988 auto const target_natural_elem
989 = target_accessor.canonical_natural_element(target_mem_elem);
990 reduction_matrix(
991 source_mem_elem.template uid<SourceIndex>(),
992 target_mem_elem.template uid<TargetIndex>())
993 = Reduction<Indices, PositionType, BatchElem, Complex>::
994 value(position,
995 elem,
996 merge_reduction_natural_elems<
997 typename tensor::tensor_accessor_for_domain_t<
998 reduction_domain_t<Indices>>::
999 natural_domain_t::discrete_element_type>(
1000 ddc::detail::array(source_natural_elem),
1001 ddc::detail::array(target_natural_elem)));
1002 });
1003 });
1004 m_valid = misc::math::invert(inverse_matrix, reduction_matrix, workspace);
1005 }
1006 }
1007
1008 KOKKOS_FUNCTION double value(auto natural_elem) const
1009 {
1010 if constexpr (SourceIndex::rank() == 0) {
1011 return 1.;
1012 } else {
1013 if (!m_valid)
1014 return 0.;
1015 std::array const source_ids = ddc::detail::array(
1016 typename tensor::natural_domain_t<SourceIndex>::discrete_element_type(
1017 natural_elem));
1018 std::array const target_ids = ddc::detail::array(
1019 typename tensor::natural_domain_t<TargetIndex>::discrete_element_type(
1020 natural_elem));
1021 if (!has_unique_reduction_ids(source_ids) || !has_unique_reduction_ids(target_ids))
1022 return 0.;
1023 [[maybe_unused]] tensor::TensorAccessor<SourceIndex> source_accessor;
1024 [[maybe_unused]] tensor::TensorAccessor<TargetIndex> target_accessor;
1025 std::size_t const source = SourceIndex::access_id_to_mem_id(
1026 source_accessor
1027 .access_element(
1028 typename tensor::natural_domain_t<
1029 SourceIndex>::discrete_element_type(natural_elem))
1030 .template uid<SourceIndex>());
1031 std::size_t const target = TargetIndex::access_id_to_mem_id(
1032 target_accessor
1033 .access_element(
1034 typename tensor::natural_domain_t<
1035 TargetIndex>::discrete_element_type(natural_elem))
1036 .template uid<TargetIndex>());
1037 // Restore the orientation of each requested wedge multi-index.
1038 int sign = 1;
1039 for (std::size_t i = 0; i < SourceIndex::rank(); ++i)
1040 for (std::size_t j = i + 1; j < SourceIndex::rank(); ++j) {
1041 if (source_ids[i] > source_ids[j])
1042 sign = -sign;
1043 if (target_ids[i] > target_ids[j])
1044 sign = -sign;
1045 }
1046 return sign * m_inverse[source * matrix_size + target]
1047 / misc::factorial(SourceIndex::rank());
1048 }
1049 }
1050};
1051
1052} // namespace detail
1053
1054template <
1055 misc::Specialization<ddc::TypeSeq> Indices,
1056 misc::Specialization<tensor::Tensor> PositionType,
1057 class BatchElem,
1058 CellComplex Complex = CellComplex::Primal,
1059 template <class...> class Coefficients = tensor::TensorFullIndex>
1060struct Reconstruction
1061{
1062private:
1063 using CoefficientIndex = detail::LocalCoefficientIndex<
1066 Coefficients>;
1067
1068public:
1069 static constexpr bool input_is_form = false;
1070 static constexpr bool output_is_form = true;
1072 using staged_storage_type = detail::LinearLocalOperatorStorage<
1077 typename PositionType::memory_space,
1078 typename PositionType::memory_space::execution_space,
1079 Coefficients,
1080 typename tensor::natural_domain_t<reduction_index_t<Indices>>::discrete_element_type,
1081 typename tensor::natural_domain_t<
1082 reduction_index_t<tensor::primes<Indices>>>::discrete_element_type,
1083 false>;
1091
1092 template <FormType ReconstructedTensorType, CochainType CochainTensorType>
1093 KOKKOS_FUNCTION static void operator()(
1094 ReconstructedTensorType const& reconstructed_tensor,
1095 CochainTensorType const& cochain_tensor,
1096 PositionType const& position,
1097 BatchElem elem)
1098 {
1099 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1100#ifndef NDEBUG
1101 detail::assert_local_diagonal<Coefficients>(
1103#endif
1104 detail::apply_local_diagonal<source_index_type, target_index_type, Coefficients>(
1105 reconstructed_tensor,
1106 cochain_tensor,
1107 diagonal_weights(position, elem));
1108 return;
1109 }
1110 using reconstruction_accessor_t
1112 using reconstruction_natural_elem_type =
1113 typename reconstruction_accessor_t::natural_domain_t::discrete_element_type;
1114 using cochain_natural_elem_type =
1115 typename CochainTensorType::accessor_t::natural_domain_t::discrete_element_type;
1116 if constexpr (source_index_type::rank() == 0) {
1117 reconstructed_tensor.mem(ddc::DiscreteElement<source_index_type>(0))
1118 = cochain_tensor.get(
1119 cochain_tensor.access_element(cochain_natural_elem_type()));
1120 } else {
1121 detail::ReconstructionMatrix<Indices, PositionType, BatchElem, Complex> const
1122 reconstruction(position, elem);
1123 [[maybe_unused]] sil::tensor::TensorAccessor<source_index_type> source_accessor;
1124 ddc::device_for_each(reconstructed_tensor.domain(), [&](auto target_mem_elem) {
1125 auto const target_natural_elem
1126 = reconstructed_tensor.canonical_natural_element(target_mem_elem);
1127 double reconstructed_value = 0.;
1128 ddc::device_for_each(source_accessor.domain(), [&](auto source_mem_elem) {
1129 auto const source_natural_elem
1130 = source_accessor.canonical_natural_element(source_mem_elem);
1131 reconstruction_natural_elem_type reconstruction_natural_elem;
1132 reconstruction_natural_elem = detail::merge_reduction_natural_elems<
1133 reconstruction_natural_elem_type>(
1134 ddc::detail::array(source_natural_elem),
1135 ddc::detail::array(target_natural_elem));
1136
1137 cochain_natural_elem_type cochain_natural_elem;
1138 ddc::detail::array(cochain_natural_elem)
1139 = ddc::detail::array(source_natural_elem);
1140
1141 reconstructed_value += reconstruction.value(reconstruction_natural_elem)
1142 * cochain_tensor.get(cochain_tensor.access_element(
1143 cochain_natural_elem));
1144 });
1145 reconstructed_tensor.mem(target_mem_elem) = reconstructed_value;
1146 });
1147 }
1148 }
1150 KOKKOS_FUNCTION static detail::LocalOperatorStencil<source_index_type, target_index_type> value(
1151 PositionType const& position,
1152 BatchElem element)
1153 {
1154 detail::LocalOperatorStencil<source_index_type, target_index_type> stencil;
1155 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1156#ifndef NDEBUG
1157 detail::assert_local_diagonal<Coefficients>(
1159#endif
1160 std::array<double, CoefficientIndex::mem_size()> const weights
1161 = diagonal_weights(position, element);
1162 return detail::
1163 local_diagonal_stencil<source_index_type, target_index_type, Coefficients>(
1164 weights);
1165 }
1166 if constexpr (source_index_type::rank() == 0) {
1167 stencil.weights[0] = 1.0;
1168 } else {
1169 detail::ReconstructionMatrix<Indices, PositionType, BatchElem, Complex> const
1170 matrix(position, element);
1171 for (std::size_t source = 0; source < source_index_type::mem_size(); ++source)
1172 for (std::size_t target = 0; target < target_index_type::mem_size(); ++target)
1173 stencil.weights[target * source_index_type::mem_size() + source] = matrix.value(
1174 detail::merge_reduction_natural_elems<
1176 reconstruction_domain_t<Indices>>::natural_domain_t::
1177 discrete_element_type>(
1178 source_index_type::mem_id_to_canonical_natural_ids(source),
1179 target_index_type::mem_id_to_canonical_natural_ids(target)));
1180 }
1181 return stencil;
1182 }
1184 KOKKOS_FUNCTION static double value(
1185 PositionType const& position,
1186 BatchElem elem,
1187 auto natural_elem)
1188 {
1189 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1190 return detail::local_stencil_coefficient<source_index_type, target_index_type, false>(
1191 value(position, elem),
1192 ddc::detail::array(source_natural_elem_type(natural_elem)),
1193 ddc::detail::array(target_natural_elem_type(natural_elem)));
1194 } else {
1195 return detail::
1196 ReconstructionMatrix<Indices, PositionType, BatchElem, Complex>(position, elem)
1197 .value(natural_elem);
1198 }
1199 }
1200
1201private:
1202 KOKKOS_FUNCTION static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
1203 PositionType const& position,
1204 BatchElem elem)
1205 {
1206 std::array<double, CoefficientIndex::mem_size()> weights {};
1207 bool invertible = true;
1208 if constexpr (source_index_type::rank() == 0) {
1209 weights[0] = 1.;
1210 } else {
1211 for (std::size_t component = 0; component < weights.size(); ++component) {
1212 double const coefficient
1213 = misc::factorial(source_index_type::rank())
1214 * Reduction<Indices, PositionType, BatchElem, Complex>::
1215 value(position,
1216 elem,
1217 detail::merge_reduction_natural_elems<
1218 typename tensor::tensor_accessor_for_domain_t<
1219 reduction_domain_t<Indices>>::
1220 natural_domain_t::discrete_element_type>(
1221 source_index_type::mem_id_to_canonical_natural_ids(
1222 component),
1223 target_index_type::mem_id_to_canonical_natural_ids(
1224 component)));
1225 invertible = invertible && coefficient != 0.;
1226 weights[component] = coefficient != 0. ? 1. / coefficient : 0.;
1227 }
1228 if (!invertible)
1229 weights.fill(0.);
1230 }
1231 return weights;
1232 }
1233};
1234
1235template <
1236 misc::Specialization<ddc::TypeSeq> Indices,
1237 misc::Specialization<tensor::Tensor> ReconstructionTensorType,
1238 misc::Specialization<tensor::Tensor> PositionType,
1239 CellComplex Complex,
1240 class BatchElem>
1243 ReconstructionTensorType m_reconstruction_tensor;
1244 PositionType m_position;
1245 BatchElem m_elem;
1246
1247 template <class MemElem>
1248 KOKKOS_FUNCTION void operator()(MemElem mem_elem) const
1249 {
1250 m_reconstruction_tensor.mem(
1251 typename ReconstructionTensorType::discrete_element_type(m_elem, mem_elem))
1253 value(m_position,
1254 m_elem,
1255 m_reconstruction_tensor.accessor().canonical_natural_element(
1256 mem_elem));
1257 }
1258};
1259
1260template <
1262 misc::Specialization<tensor::Tensor> ReconstructionTensorType,
1264 class ExecSpace,
1265 CellComplex Complex = CellComplex::Primal>
1266ReconstructionTensorType fill_reconstruction_operator(
1267 ExecSpace const& exec_space,
1268 ReconstructionTensorType const& reconstruction_tensor,
1269 PositionType const& position)
1270{
1271 SIMILIE_DEBUG_LOG("similie_compute_reconstruction_operator");
1272 ddc::parallel_for_each(
1273 "similie_compute_reconstruction_operator",
1274 exec_space,
1275 reconstruction_tensor.non_indices_domain(),
1276 KOKKOS_LAMBDA(
1277 typename ReconstructionTensorType::non_indices_domain_t::discrete_element_type
1278 elem) {
1279 auto const stencil = Reconstruction<
1280 Indices,
1281 PositionType,
1282 typename ReconstructionTensorType::non_indices_domain_t::
1283 discrete_element_type,
1284 Complex>::value(position, elem);
1285 ddc::device_for_each(
1286 reconstruction_tensor.accessor().domain(),
1287 [&](auto component) {
1288 using Source = reduction_index_t<Indices>;
1289 using Target = reduction_index_t<tensor::primes<Indices>>;
1290 auto const natural
1291 = reconstruction_tensor.accessor().canonical_natural_element(
1292 component);
1293 reconstruction_tensor.mem(elem, component)
1294 = detail::local_stencil_coefficient<Source, Target, false>(
1295 stencil,
1296 ddc::detail::array(
1297 typename tensor::natural_domain_t<Source>::
1298 discrete_element_type(natural)),
1299 ddc::detail::array(
1300 typename tensor::natural_domain_t<Target>::
1301 discrete_element_type(natural)));
1302 });
1303 });
1304 return reconstruction_tensor;
1305}
1306
1307} // namespace exterior
1308
1309} // namespace sil
Cochain class.
Definition cochain.hpp:32
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
ddc::detail::convert_type_seq_to_discrete_domain_t< ddc::type_seq_merge_t< ddc::TypeSeq< misc::convert_type_seq_to_t< tensor::TensorFullIndex, tensor::primes< tensor::upper_t< Indices > > > >, ddc::TypeSeq< reduction_index_t< Indices > > > > reduction_domain_t
ReductionTensorType fill_reduction_operator(ExecSpace const &exec_space, ReductionTensorType const &reduction_tensor, PositionType const &position)
typename detail::ReductionTargetIndex< Indices >::type reduction_index_t
ReconstructionTensorType fill_reconstruction_operator(ExecSpace const &exec_space, ReconstructionTensorType const &reconstruction_tensor, PositionType const &position)
ddc::detail::convert_type_seq_to_discrete_domain_t< ddc::type_seq_merge_t< ddc::TypeSeq< reduction_index_t< Indices > >, ddc::TypeSeq< reduction_index_t< tensor::primes< Indices > > > > > reconstruction_domain_t
KOKKOS_FUNCTION bool invert(InverseView const &inverse, MatrixView const &matrix, WorkspaceView const &workspace)
KOKKOS_FUNCTION unmanaged_vector_view_t< Scalar, MemorySpace > vector_view(Scalar *data, std::size_t n)
KOKKOS_FUNCTION MatrixView::non_const_value_type determinant(MatrixView const &matrix)
KOKKOS_FUNCTION unmanaged_matrix_view_t< Scalar, MemorySpace > matrix_view(Scalar *data, std::size_t rows, std::size_t cols)
KOKKOS_FUNCTION double submatrix_determinant(MatrixView const &matrix, RowIds const &row_ids, ColIds const &col_ids, std::array< double, N *N > &submatrix_alloc)
KOKKOS_FUNCTION constexpr std::size_t binomial_coefficient(std::size_t n, std::size_t k) noexcept
KOKKOS_FUNCTION constexpr std::size_t factorial(std::size_t k) noexcept
Definition factorial.hpp:13
typename detail::ConvertTypeSeqTo< T, Seq >::type convert_type_seq_to_t
ddc::type_seq_element_t< 0, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_1
Definition metric.hpp:33
detail::Primes< Indices, I >::type primes
Definition prime.hpp:55
ddc::type_seq_element_t< 1, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_2
Definition metric.hpp:41
typename detail::NaturalDomainType< Index >::type natural_domain_t
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
The top-level namespace of SimiLie.
Definition csr.hpp:15
reduction_index_t< Indices > source_index_type
typename tensor::natural_domain_t< target_index_type >::discrete_element_type target_natural_elem_type
detail::LinearLocalOperatorStorage< Reconstruction, reduction_index_t< Indices >, reduction_index_t< tensor::primes< Indices > >, misc::convert_type_seq_to_t< ddc::DiscreteDomain, ddc::to_type_seq_t< BatchElem > >, typename PositionType::memory_space, typename PositionType::memory_space::execution_space, Coefficients, typename tensor::natural_domain_t< reduction_index_t< Indices > >::discrete_element_type, typename tensor::natural_domain_t< reduction_index_t< tensor::primes< Indices > > >::discrete_element_type, false > staged_storage_type
reduction_index_t< tensor::primes< Indices > > target_index_type
typename tensor::natural_domain_t< source_index_type >::discrete_element_type source_natural_elem_type
misc::convert_type_seq_to_t< tensor::TensorFullIndex, tensor::primes< tensor::upper_t< Indices > > > source_index_type
typename tensor::natural_domain_t< target_index_type >::discrete_element_type target_natural_elem_type
typename tensor::natural_domain_t< source_index_type >::discrete_element_type source_natural_elem_type
static KOKKOS_FUNCTION void operator()(ReductionTensorType const &reduced_tensor, FormTensorType const &form_tensor, PositionType const &position, BatchElem elem)
detail::LinearLocalOperatorStorage< Reduction, reduction_index_t< tensor::primes< tensor::upper_t< Indices > > >, reduction_index_t< Indices >, misc::convert_type_seq_to_t< ddc::DiscreteDomain, ddc::to_type_seq_t< BatchElem > >, typename PositionType::memory_space, typename PositionType::memory_space::execution_space, Coefficients > staged_storage_type
ddc:: type_seq_element_t< 0, ddc::to_type_seq_t< typename PositionType::indices_domain_t > > position_index_type
reduction_index_t< Indices > target_index_type