SimiLie
Loading...
Searching...
No Matches
antisymmetric_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 an antisymmetric 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 if constexpr (sizeof...(TensorIndex) == 0) {
38 return 0;
39 } else {
40 return (TensorIndex::rank() + ...);
41 }
42 }
43
44 static constexpr std::size_t size()
45 {
46 if constexpr (rank() == 0) {
47 return 1;
48 } else {
49 return (TensorIndex::size() * ...);
50 }
51 }
52
53 static constexpr std::size_t mem_size()
54 {
55 static_assert([] {
56 std::array<std::size_t, sizeof...(TensorIndex)> const sizes {TensorIndex::size()...};
57 for (std::size_t i = 1; i < sizes.size(); ++i) {
58 if (sizes[i] != sizes[0]) {
59 return false;
60 }
61 }
62 return true;
63 }());
64 if constexpr (rank() == 0) {
65 return 1;
66 } else {
68 ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size(),
69 rank());
70 }
71 }
72
73 static constexpr std::size_t access_size()
74 {
75 if constexpr (rank() <= 1) {
76 return mem_size();
77 } else {
78 return 2 * mem_size() + 1;
79 }
80 }
81
82 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
83 std::array<std::size_t, rank()> const& natural_ids)
84 {
85 std::array<std::size_t, rank()> sorted_ids(natural_ids);
86 misc::detail::sort(sorted_ids.begin(), sorted_ids.end());
87 for (std::size_t i = 1; i < rank(); ++i) {
88 assert(sorted_ids[i - 1] != sorted_ids[i]);
89 }
90 return mem_size()
91 - (0 + ...
92 + (sorted_ids[ddc::type_seq_rank_v<TensorIndex, ddc::TypeSeq<TensorIndex...>>]
93 == TensorIndex::mem_size() - rank()
94 + ddc::type_seq_rank_v<
96 ddc::TypeSeq<TensorIndex...>>
97 ? 0
99 TensorIndex::mem_size()
100 - sorted_ids[ddc::type_seq_rank_v<
102 ddc::TypeSeq<TensorIndex...>>]
103 - 1,
104 rank()
105 - ddc::type_seq_rank_v<
107 ddc::TypeSeq<TensorIndex...>>)))
108 - 1;
109 }
110
111private:
112 KOKKOS_FUNCTION static constexpr bool permutation_parity(
113 std::array<std::size_t, rank()> const& ids)
114 {
115 bool cnt = false;
116 for (std::size_t i = 0; i < rank(); i++)
117 for (std::size_t j = i + 1; j < rank(); j++)
118 if (ids[i] > ids[j])
119 cnt = !cnt;
120 return cnt;
121 }
122
123public:
124 KOKKOS_FUNCTION static constexpr std::size_t access_id(
125 std::array<std::size_t, rank()> const& natural_ids)
126 {
127 if constexpr (rank() <= 1) {
128 return mem_id(natural_ids);
129 } else {
130 for (std::size_t i = 0; i < rank(); ++i) {
131 for (std::size_t j = i + 1; j < rank(); ++j) {
132 if (natural_ids[i] == natural_ids[j]) {
133 return 0;
134 }
135 }
136 }
137 if (!permutation_parity(natural_ids)) {
138 return 1 + mem_id(natural_ids);
139 } else {
140 return 1 + mem_size() + mem_id(natural_ids);
141 }
142 }
143 }
144
145 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
146 {
147 if constexpr (rank() <= 1) {
148 return access_id;
149 } else {
150 if (access_id != 0) {
151 return (access_id - 1) % mem_size();
152 } else {
153 return std::numeric_limits<std::size_t>::max();
154 }
155 }
156 }
157
158 template <class Tensor, class Elem, class Id, class FunctorType>
159 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
160 const FunctorType& access,
161 Tensor const& tensor,
162 Elem const& elem)
163 {
164 if constexpr (rank() <= 1) {
165 return access(tensor, elem);
166 } else {
167 if (elem.template uid<Id>() == 0) {
168 return 0.;
169 } else if (elem.template uid<Id>() <= mem_size()) {
170 return access(tensor, elem);
171 } else {
172 return -access(tensor, elem);
173 }
174 }
175 }
176
177 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
179 {
180 assert(mem_id < mem_size());
181 if constexpr (rank() == 0) {
182 return std::array<std::size_t, rank()> {};
183 } else {
184 std::array<std::size_t, rank()> ids;
185 std::size_t d = ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size();
186 std::size_t r = rank();
187 for (std::size_t i = 0; i < rank(); ++i) {
188 const std::size_t triangle_size = misc::binomial_coefficient(d, r - i);
189 for (std::size_t j = 0; j < d; ++j) {
190 const std::size_t subtriangle_size
191 = misc::binomial_coefficient(d - j - 1, r - i);
192 if (triangle_size - subtriangle_size > mem_id) {
193 ids[i] = ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::
194 mem_size()
195 - d + j;
196 mem_id -= triangle_size - misc::binomial_coefficient(d - j, r - i);
197 d -= j + 1;
198 break;
199 }
200 ids[i] = 0;
201 }
202 }
203 return ids;
204 }
205 }
206};
207
208static_assert(
209 TensorIndexConcept<TensorAntisymmetricIndex<TensorNaturalIndex<detail::DimensionConcept>>>);
210
211namespace detail {
212
213template <class T>
214struct ToTensorAntisymmetricIndex;
215
216template <tensor::TensorNatIndex Index>
217 requires(Index::rank() == 0)
218struct ToTensorAntisymmetricIndex<Index>
219{
220 using type = TensorAntisymmetricIndex<>;
221};
222
223template <tensor::TensorNatIndex Index>
224 requires(Index::rank() > 0)
225struct ToTensorAntisymmetricIndex<Index>
226{
227 using type = TensorAntisymmetricIndex<Index>;
228};
229
230template <tensor::TensorNatIndex... Index>
231struct ToTensorAntisymmetricIndex<TensorAntisymmetricIndex<Index...>>
232{
233 using type = TensorAntisymmetricIndex<Index...>;
234};
235
236} // namespace detail
237
238template <class T>
239using to_tensor_antisymmetric_index_t = typename detail::ToTensorAntisymmetricIndex<T>::type;
240
241} // namespace tensor
242
243} // namespace sil
KOKKOS_FUNCTION constexpr std::size_t binomial_coefficient(std::size_t n, std::size_t k) noexcept
typename detail::ToTensorAntisymmetricIndex< T >::type to_tensor_antisymmetric_index_t
The top-level namespace of SimiLie.
Definition csr.hpp:15
static constexpr std::size_t access_size()
static constexpr std::size_t mem_size()
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 Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)
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 mem_id(std::array< std::size_t, rank()> const &natural_ids)
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, rank()> const &natural_ids)