SimiLie
Loading...
Searching...
No Matches
symmetric_tensor.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/binomial_coefficient.hpp>
9#include <similie/misc/portable_stl.hpp>
10#include <similie/tensor/tensor_impl.hpp>
11
12namespace sil {
13
14namespace tensor {
15
16// struct representing an abstract unique index sweeping on all possible combination of natural indices, for a summetric tensor.
17template <TensorNatIndex... TensorIndex>
19{
20 static_assert(((TensorIndex::rank() == 1) && ...));
21
22 static constexpr bool is_tensor_index = true;
23 static constexpr bool is_explicitely_stored_tensor = true;
24
25 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
26
27 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
28 {
29 return ddc::DiscreteDomain<TensorIndex...>(
30 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
31 ddc::DiscreteVector<TensorIndex...>(
32 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
33 }
34
35 static constexpr std::size_t rank()
36 {
37 return (0 + ... + TensorIndex::rank());
38 }
39
40 static constexpr std::size_t size()
41 {
42 return (1 * ... * TensorIndex::size());
43 }
44
45 static constexpr std::size_t mem_size()
46 {
47 static_assert([] {
48 std::array<std::size_t, sizeof...(TensorIndex)> const sizes {TensorIndex::size()...};
49 for (std::size_t i = 1; i < sizes.size(); ++i) {
50 if (sizes[i] != sizes[0]) {
51 return false;
52 }
53 }
54 return true;
55 }());
56 if constexpr (rank() == 0) {
57 return 1;
58 } else {
60 ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size() + rank()
61 - 1,
62 rank());
63 }
64 }
65
66 static constexpr std::size_t access_size()
67 {
68 return mem_size();
69 }
70
71 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
72 std::array<std::size_t, rank()> const& natural_ids)
73 {
74 if constexpr (rank() == 0) {
75 return 0;
76 } else {
77 std::array<std::size_t, rank()> sorted_ids(natural_ids);
78 misc::detail::sort(sorted_ids.begin(), sorted_ids.end());
80 ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size()
81 + rank() - 1,
82 rank())
83 - ((sorted_ids[ddc::type_seq_rank_v<TensorIndex, ddc::TypeSeq<TensorIndex...>>]
84 == TensorIndex::mem_size() - 1
85 ? 0
87 TensorIndex::mem_size()
88 - sorted_ids[ddc::type_seq_rank_v<
90 ddc::TypeSeq<TensorIndex...>>]
91 + rank()
92 - ddc::type_seq_rank_v<
94 ddc::TypeSeq<TensorIndex...>>
95 - 2,
96 rank()
97 - ddc::type_seq_rank_v<
99 ddc::TypeSeq<TensorIndex...>>))
100 + ...)
101 - 1;
102 }
103 }
104
105 KOKKOS_FUNCTION static constexpr std::size_t access_id(
106 std::array<std::size_t, rank()> const& natural_ids)
107 {
108 return mem_id(natural_ids);
109 }
110
111 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
112 {
113 return access_id;
114 }
115
116 template <class Tensor, class Elem, class Id, class FunctorType>
117 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
118 const FunctorType& access,
119 Tensor const& tensor,
120 Elem const& elem)
121 {
122 return access(tensor, elem);
123 }
124
125 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
127 {
128 assert(mem_id < mem_size());
129 if constexpr (rank() == 0) {
130 return std::array<std::size_t, rank()> {};
131 } else {
132 std::array<std::size_t, rank()> ids;
133 std::size_t d = ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size();
134 std::size_t r = rank();
135 for (std::size_t i = 0; i < rank(); ++i) {
136 const std::size_t triangle_size = misc::binomial_coefficient(d + r - i - 1, r - i);
137 for (std::size_t j = 0; j < d; ++j) {
138 const std::size_t subtriangle_size
139 = misc::binomial_coefficient(d - j + r - i - 2, r - i);
140 if (triangle_size - subtriangle_size > mem_id) {
141 ids[i] = ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::
142 mem_size()
143 - d + j;
144 mem_id -= triangle_size
145 - misc::binomial_coefficient(d - j + r - i - 1, r - i);
146 d -= j;
147 break;
148 }
149 ids[i] = 0;
150 }
151 }
152 return ids;
153 }
154 }
155};
156
157static_assert(
158 TensorIndexConcept<TensorSymmetricIndex<TensorNaturalIndex<detail::DimensionConcept>>>);
159
160} // namespace tensor
161
162} // namespace sil
KOKKOS_FUNCTION constexpr std::size_t binomial_coefficient(std::size_t n, std::size_t k) noexcept
The top-level namespace of SimiLie.
Definition csr.hpp:15
static constexpr bool is_explicitely_stored_tensor
ddc::DiscreteDomain< TensorIndex... > subindices_domain_t
static KOKKOS_FUNCTION constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static constexpr std::size_t rank()
static constexpr std::size_t access_size()
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t mem_id)
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, rank()> const &natural_ids)
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)
static constexpr std::size_t mem_size()
static constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr std::size_t mem_id(std::array< std::size_t, rank()> const &natural_ids)