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>
34template <misc::Specialization<ddc::TypeSeq> Indices>
35struct ReductionTargetIndex;
38struct ReductionTargetIndex<
ddc::TypeSeq<>>
40 using type = tensor::Covariant<tensor::ScalarIndex>;
43template <tensor::TensorNatIndex Index>
44struct ReductionTargetIndex<
ddc::TypeSeq<Index>>
49template <tensor::TensorNatIndex... Index>
50 requires(
sizeof...(Index) > 1)
51struct ReductionTargetIndex<ddc::TypeSeq<Index...>>
53 using type = tensor::TensorAntisymmetricIndex<Index...>;
56template <std::
size_t N>
57KOKKOS_FUNCTION
bool has_unique_reduction_ids(std::array<std::size_t, N>
const& ids)
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]) {
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)
74 for (std::size_t i = 0; i < N; ++i) {
75 if (lhs[i] != rhs[i]) {
82template <std::
size_t K>
83KOKKOS_FUNCTION
double reduction_determinant(std::array<double, K * K>
const& matrix)
85 if constexpr (K == 0) {
87 }
else if constexpr (K == 1) {
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) {
99 minor[(row - 1) * (K - 1) + minor_col] = matrix[row * K + source_col];
103 double const sign = (col % 2 == 0) ? 1. : -1.;
104 det += sign * matrix[col] * reduction_determinant<K - 1>(minor);
110template <std::
size_t N, std::
size_t K>
111KOKKOS_FUNCTION std::array<std::size_t, K> combination_from_rank(std::size_t rank)
113 std::array<std::size_t, K> ids {};
114 if constexpr (K == 0) {
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
127 next_candidate = candidate + 1;
137template <std::
size_t N, std::
size_t K>
138KOKKOS_FUNCTION std::size_t combination_rank(std::array<std::size_t, K>
const& ids)
140 if constexpr (K == 0) {
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
152 next_candidate = ids[i] + 1;
158template <std::
size_t N, std::
size_t M>
159KOKKOS_FUNCTION
bool hodge_has_unique_ids(std::array<std::size_t, M>
const& ids)
161 if constexpr (M > 0) {
162 for (std::size_t i = 0; i < M; ++i) {
166 for (std::size_t j = i + 1; j < M; ++j) {
167 if (ids[i] == ids[j]) {
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)
181 std::array<int, N> counts {};
182 for (std::size_t
id : source_ids) {
188 for (std::size_t
id : target_ids) {
194 for (
int count : counts) {
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)
207 std::array<std::size_t, N> permutation {};
208 for (std::size_t i = 0; i < M1; ++i) {
209 permutation[i] = source_ids[i];
211 if constexpr (M2 > 0) {
212 for (std::size_t i = 0; i < M2; ++i) {
213 permutation[M1 + i] = target_ids[i];
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;
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)
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) {
234 for (std::size_t
id : ids) {
235 found = found || (
id == i);
238 complement[complement_id++] = i;
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,
248 std::array<std::size_t, K>
const& source_ids,
249 std::array<std::size_t, N - K>
const& target_ids)
251 if (!hodge_has_unique_ids<N>(source_ids) || !hodge_has_unique_ids<N>(target_ids)) {
255 std::array<std::size_t, K>
const complement = hodge_complement_ids<N>(target_ids);
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))
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(
275 ddc::DiscreteElement<MetricIndex1, MetricIndex2>(i, j)));
278 determinant_metric_alloc = metric_alloc;
282 typename MetricType::memory_space>(determinant_metric_alloc.data(), N, N);
288 typename MetricType::memory_space>(inverse_metric_alloc.data(), N, N);
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)
309KOKKOS_FUNCTION
double legacy_discrete_hodge_value_from_ids(
310 MetricType
const& metric,
311 PositionType
const& position,
313 std::array<std::size_t, K>
const& source_ids,
314 std::array<std::size_t, N - K>
const& target_ids)
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)) {
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.) {
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)
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)
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];
342 for (std::size_t i = 0; i < N2; ++i) {
343 reduction_ids[N1 + i] = second_ids[i];
345 return natural_elem_from_flat_ids<ReductionNaturalElemType>(reduction_ids);
351template <misc::Specialization<ddc::TypeSeq> Indices>
354template <misc::Specialization<ddc::TypeSeq> Indices>
359 ddc::TypeSeq<reduction_index_t<Indices>>>>;
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>>>>>;
377 using CoefficientIndex = detail::
378 LocalCoefficientIndex<ComponentSource, reduction_index_t<Indices>, Coefficients>;
389 typename PositionType::memory_space,
390 typename PositionType::memory_space::execution_space,
402 type_seq_element_t<0, ddc::to_type_seq_t<typename PositionType::indices_domain_t>>;
404 template <CochainType ReductionTensorType, FormType FormTensorType>
406 ReductionTensorType
const& reduced_tensor,
407 FormTensorType
const& form_tensor,
408 PositionType
const& position,
411 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
413 detail::assert_local_diagonal<Coefficients>(
416 detail::apply_local_diagonal<
419 Coefficients>(reduced_tensor, form_tensor, diagonal_weights(position, elem));
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()));
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);
446 form_natural_elem_type form_natural_elem;
447 ddc::detail::array(form_natural_elem)
448 = ddc::detail::array(source_natural_elem);
450 reduced_value += value(position, elem, reduction_natural_elem)
452 form_tensor.access_element(form_natural_elem));
454 reduced_tensor.mem(target_mem_elem) = reduced_value;
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,
470 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
472 detail::assert_local_diagonal<Coefficients>(
474 value(metric, position, elem));
475 detail::assert_local_diagonal<Coefficients>(
477 value(position, elem));
479 detail::apply_local_diagonal<ComponentSource, target_index_type, Coefficients>(
482 diagonal_weights(metric, position, elem));
488 operator()(reduced_tensor, form_tensor, position, elem);
492 "Metric-aware reduction is only implemented for the circumcentric dual "
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;
501 "Complementary basis sizes must match.");
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 {};
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<
524 for (std::size_t row = 0; row < K; ++row)
525 minor[row * K + col] = edge[physical_ids[row]];
527 primal_reduction_alloc[logical * NBASIS + physical]
534 typename MetricType::
535 memory_space>(primal_inverse_alloc.data(), NBASIS, NBASIS),
538 typename MetricType::
539 memory_space>(primal_reduction_alloc.data(), NBASIS, NBASIS),
542 typename MetricType::
543 memory_space>(primal_workspace_alloc.data(), NBASIS * NBASIS));
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<
557 K>(metric, elem, physical_ids, source_ids)
558 * primal_inverse_alloc[physical * NBASIS + primal_id]
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<
569 K>(metric, position, elem, primal_ids, target_ids);
573 std::array<double, NBASIS * NBASIS> workspace_alloc {};
576 typename MetricType::
577 memory_space>(coeff_from_primal_alloc.data(), NBASIS, NBASIS);
580 typename MetricType::
581 memory_space>(coeff_from_primal_inverse_alloc.data(), NBASIS, NBASIS);
584 typename MetricType::memory_space>(workspace_alloc.data(), NBASIS * NBASIS);
585 bool const invertible = primal_invertible
587 invert(coeff_from_primal_inverse_view,
588 coeff_from_primal_view,
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);
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);
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);
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);
617 double dual_reduction_value = 0.;
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];
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);
632 reduced_tensor.mem(target_mem_elem) = reduced_value;
637 KOKKOS_FUNCTION
static double value(
638 PositionType
const& position,
643 return detail::local_stencil_coefficient<ComponentSource, target_index_type>(
644 value(position, elem),
648 constexpr std::size_t K = target_index_type::rank();
649 if constexpr (K == 0) {
652 std::array
const source_ids
654 std::array
const target_ids
656 if (!detail::has_unique_reduction_ids<K>(source_ids)
657 || !detail::has_unique_reduction_ids<K>(target_ids)) {
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<
667 jacobian_matrix[row * K + col] = edge[source_ids[row]];
670 return detail::reduction_determinant<K>(jacobian_matrix)
671 * detail::complex_volume_factor<Complex, source_index_type::size()>(K)
672 / misc::factorial(K);
676 KOKKOS_FUNCTION
static detail::LocalOperatorStencil<
679 value(PositionType
const& position, BatchElem element)
682 detail::LocalOperatorStencil<Source, target_index_type> result;
683 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
685 detail::assert_local_diagonal<Coefficients>(
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>(
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;
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;
712 template <misc::Specialization<tensor::Tensor> MetricType>
713 KOKKOS_FUNCTION
static detail::LocalOperatorStencil<
716 value(MetricType
const& metric, PositionType
const& position, BatchElem element)
719 detail::LocalOperatorStencil<Source, target_index_type> result;
720 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
722 detail::assert_local_diagonal<Coefficients>(
724 value(metric, position, element));
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>(
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;
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;
751 template <misc::Specialization<tensor::Tensor> MetricType>
752 KOKKOS_FUNCTION
static double value(
753 MetricType
const& metric,
754 PositionType
const& position,
759 return detail::local_stencil_coefficient<ComponentSource, target_index_type>(
760 value(metric, position, elem),
764 if constexpr (Complex == CellComplex::Primal) {
765 return value(position, elem, natural_elem);
768 std::array<double, source_index_type::mem_size()> source_alloc {};
771 ddc::DiscreteDomain<source_index_type>,
772 Kokkos::layout_right,
773 typename MetricType::memory_space>
774 source_span(source_alloc.data(), source_accessor.
domain());
776 ddc::device_for_each(source_tensor.domain(), [&](
auto source_mem_elem) {
777 source_tensor.mem(source_mem_elem) = 0.;
781 std::array<double, target_index_type::mem_size()> target_alloc {};
784 ddc::DiscreteDomain<target_index_type>,
785 Kokkos::layout_right,
786 typename MetricType::memory_space>
787 target_span(target_alloc.data(), target_accessor.
domain());
789 ddc::device_for_each(target_tensor.domain(), [&](
auto target_mem_elem) {
790 target_tensor.mem(target_mem_elem) = 0.;
794 ddc::detail::array(source_natural_elem)
796 if constexpr (source_index_type::rank() > 1) {
797 if (!detail::has_unique_reduction_ids(ddc::detail::array(source_natural_elem))) {
801 source_tensor(source_tensor.accessor().access_element(source_natural_elem)) = 1.;
803 operator()(target_tensor, source_tensor, metric, position, elem);
805 if constexpr (target_index_type::rank() == 0) {
806 return target_tensor.get(
810 ddc::detail::array(target_natural_elem)
812 return target_tensor.get(
813 target_tensor.accessor().access_element(target_natural_elem));
819 KOKKOS_FUNCTION
static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
820 PositionType
const& position,
823 std::array<double, CoefficientIndex::mem_size()> weights {};
824 if constexpr (target_index_type::rank() == 0) {
827 for (std::size_t component = 0; component < weights.size(); ++component) {
829 = misc::factorial(target_index_type::rank())
830 * Reduction<Indices, PositionType, BatchElem, Complex>::
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(
839 target_index_type::mem_id_to_canonical_natural_ids(
846 template <
class Metric>
847 KOKKOS_FUNCTION
static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
848 Metric
const& metric,
849 PositionType
const& position,
852 if constexpr (Complex == CellComplex::Primal) {
853 return diagonal_weights(position, elem);
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]];
874 double const hodge = detail::
875 continuous_hodge_value_from_ids<N, K>(metric, elem, primal_ids, target_ids);
877 weights[component] = detail::legacy_discrete_hodge_value_from_ids<
880 K>(metric, position, elem, primal_ids, target_ids)
881 * detail::reduction_determinant<K>(minor) / hodge;
889 misc::Specialization<ddc::TypeSeq> Indices,
890 misc::Specialization<tensor::Tensor> ReductionTensorType,
891 misc::Specialization<tensor::Tensor> PositionType,
896 ReductionTensorType m_reduction_tensor;
897 PositionType m_position;
900 template <
class MemElem>
901 KOKKOS_FUNCTION
void operator()(MemElem mem_elem)
const
903 m_reduction_tensor.mem(
904 typename ReductionTensorType::discrete_element_type(m_elem, mem_elem))
908 m_reduction_tensor.accessor().canonical_natural_element(mem_elem));
919 ExecSpace
const& exec_space,
920 ReductionTensorType
const& reduction_tensor,
921 PositionType
const& position)
923 SIMILIE_DEBUG_LOG(
"similie_compute_reduction_operator");
924 ddc::parallel_for_each(
925 "similie_compute_reduction_operator",
927 reduction_tensor.non_indices_domain(),
929 typename ReductionTensorType::non_indices_domain_t::discrete_element_type
931 ddc::device_for_each(
932 reduction_tensor.accessor().domain(),
938 typename ReductionTensorType::non_indices_domain_t::
939 discrete_element_type> {reduction_tensor, position, elem});
941 return reduction_tensor;
946template <
class Indices,
class PositionType,
class BatchElem, CellComplex Complex>
947class ReconstructionMatrix
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;
956 KOKKOS_FUNCTION ReconstructionMatrix(PositionType
const& position, BatchElem elem)
959 Complex != CellComplex::BarycentricDual,
960 "Reconstruction is not implemented for the barycentric dual complex.");
961 if constexpr (SourceIndex::rank() == 0) {
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
971 = misc::math::matrix_view<
973 typename PositionType::
974 memory_space>(reduction_alloc.data(), matrix_size, matrix_size),
975 inverse_matrix = misc::math::matrix_view<
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
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);
991 source_mem_elem.template uid<SourceIndex>(),
992 target_mem_elem.template uid<TargetIndex>())
993 = Reduction<Indices, PositionType, BatchElem, Complex>::
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)));
1004 m_valid = misc::math::invert(inverse_matrix, reduction_matrix, workspace);
1008 KOKKOS_FUNCTION
double value(
auto natural_elem)
const
1010 if constexpr (SourceIndex::rank() == 0) {
1015 std::array
const source_ids = ddc::detail::array(
1016 typename tensor::natural_domain_t<SourceIndex>::discrete_element_type(
1018 std::array
const target_ids = ddc::detail::array(
1019 typename tensor::natural_domain_t<TargetIndex>::discrete_element_type(
1021 if (!has_unique_reduction_ids(source_ids) || !has_unique_reduction_ids(target_ids))
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(
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(
1034 typename tensor::natural_domain_t<
1035 TargetIndex>::discrete_element_type(natural_elem))
1036 .
template uid<TargetIndex>());
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])
1043 if (target_ids[i] > target_ids[j])
1046 return sign * m_inverse[source * matrix_size + target]
1047 / misc::factorial(SourceIndex::rank());
1055 misc::Specialization<ddc::TypeSeq> Indices,
1056 misc::Specialization<tensor::Tensor> PositionType,
1063 using CoefficientIndex = detail::LocalCoefficientIndex<
1069 static constexpr bool input_is_form =
false;
1070 static constexpr bool output_is_form =
true;
1077 typename PositionType::memory_space,
1078 typename PositionType::memory_space::execution_space,
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,
1099 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1101 detail::assert_local_diagonal<Coefficients>(
1104 detail::apply_local_diagonal<source_index_type, target_index_type, Coefficients>(
1105 reconstructed_tensor,
1107 diagonal_weights(position, elem));
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()));
1121 detail::ReconstructionMatrix<Indices, PositionType, BatchElem, Complex>
const
1122 reconstruction(position, elem);
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));
1137 cochain_natural_elem_type cochain_natural_elem;
1138 ddc::detail::array(cochain_natural_elem)
1139 = ddc::detail::array(source_natural_elem);
1141 reconstructed_value += reconstruction.value(reconstruction_natural_elem)
1142 * cochain_tensor.get(cochain_tensor.access_element(
1143 cochain_natural_elem));
1145 reconstructed_tensor.mem(target_mem_elem) = reconstructed_value;
1150 KOKKOS_FUNCTION
static detail::LocalOperatorStencil<source_index_type, target_index_type> value(
1151 PositionType
const& position,
1154 detail::LocalOperatorStencil<source_index_type, target_index_type> stencil;
1155 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1157 detail::assert_local_diagonal<Coefficients>(
1160 std::array<double, CoefficientIndex::mem_size()>
const weights
1161 = diagonal_weights(position, element);
1163 local_diagonal_stencil<source_index_type, target_index_type, Coefficients>(
1166 if constexpr (source_index_type::rank() == 0) {
1167 stencil.weights[0] = 1.0;
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<
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)));
1184 KOKKOS_FUNCTION
static double value(
1185 PositionType
const& position,
1189 if constexpr (detail::LocalDiagonalCoefficientIndex<CoefficientIndex>) {
1190 return detail::local_stencil_coefficient<source_index_type, target_index_type, false>(
1191 value(position, elem),
1196 ReconstructionMatrix<Indices, PositionType, BatchElem, Complex>(position, elem)
1197 .value(natural_elem);
1202 KOKKOS_FUNCTION
static std::array<double, CoefficientIndex::mem_size()> diagonal_weights(
1203 PositionType
const& position,
1206 std::array<double, CoefficientIndex::mem_size()> weights {};
1207 bool invertible =
true;
1208 if constexpr (source_index_type::rank() == 0) {
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>::
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(
1223 target_index_type::mem_id_to_canonical_natural_ids(
1225 invertible = invertible && coefficient != 0.;
1226 weights[component] = coefficient != 0. ? 1. / coefficient : 0.;
1236 misc::Specialization<ddc::TypeSeq> Indices,
1237 misc::Specialization<tensor::Tensor> ReconstructionTensorType,
1238 misc::Specialization<tensor::Tensor> PositionType,
1243 ReconstructionTensorType m_reconstruction_tensor;
1244 PositionType m_position;
1247 template <
class MemElem>
1248 KOKKOS_FUNCTION
void operator()(MemElem mem_elem)
const
1250 m_reconstruction_tensor.mem(
1251 typename ReconstructionTensorType::discrete_element_type(m_elem, mem_elem))
1255 m_reconstruction_tensor.accessor().canonical_natural_element(
1267 ExecSpace
const& exec_space,
1268 ReconstructionTensorType
const& reconstruction_tensor,
1269 PositionType
const& position)
1271 SIMILIE_DEBUG_LOG(
"similie_compute_reconstruction_operator");
1272 ddc::parallel_for_each(
1273 "similie_compute_reconstruction_operator",
1275 reconstruction_tensor.non_indices_domain(),
1277 typename ReconstructionTensorType::non_indices_domain_t::discrete_element_type
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>>;
1291 = reconstruction_tensor.accessor().canonical_natural_element(
1293 reconstruction_tensor.mem(elem, component)
1294 = detail::local_stencil_coefficient<Source, Target, false>(
1297 typename tensor::natural_domain_t<Source>::
1298 discrete_element_type(natural)),
1300 typename tensor::natural_domain_t<Target>::
1301 discrete_element_type(natural)));
1304 return reconstruction_tensor;
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
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
detail::Primes< Indices, I >::type primes
ddc::type_seq_element_t< 1, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_2
typename detail::NaturalDomainType< Index >::type natural_domain_t
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
The top-level namespace of SimiLie.
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
static constexpr bool input_is_form
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
static constexpr bool output_is_form