SimiLie
Loading...
Searching...
No Matches
levi_civita_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/permutation_parity.hpp>
9#include <similie/tensor/tensor_impl.hpp>
10
11namespace sil {
12
13namespace tensor {
14
15// struct representing an identity tensor (no storage).
16template <TensorNatIndex... TensorIndex>
18{
19 static constexpr bool is_tensor_index = true;
20 static constexpr bool is_explicitely_stored_tensor = true;
21
22 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
23
24 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
25 {
26 return ddc::DiscreteDomain<TensorIndex...>(
27 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
28 ddc::DiscreteVector<TensorIndex...>(
29 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
30 }
31
32 static constexpr std::size_t rank()
33 {
34 return (TensorIndex::rank() + ...);
35 }
36
37 static_assert(((TensorIndex::size() == rank()) && ...));
38
39 static constexpr std::size_t size()
40 {
41 return (TensorIndex::size() * ...);
42 }
43
44 static constexpr std::size_t mem_size()
45 {
46 return 0;
47 }
48
49 static constexpr std::size_t access_size()
50 {
51 return 3;
52 }
53
54 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
55 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
56 {
57 return std::numeric_limits<std::size_t>::max();
58 }
59
60 KOKKOS_FUNCTION static constexpr std::size_t access_id(
61 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
62 {
63 if constexpr (rank() == 1) {
64 return 1;
65 } else {
66 int const parity = misc::permutation_parity(natural_ids);
67 if (parity == 0) {
68 return 0;
69 } else if (parity == 1) {
70 return 1;
71 } else {
72 return 2;
73 }
74 }
75 }
76
77 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(
78 [[maybe_unused]] std::size_t access_id)
79 {
80 return std::numeric_limits<std::size_t>::max();
81 }
82
83 template <class Tensor, class Elem, class Id, class FunctorType>
84 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
85 const FunctorType& access,
86 Tensor const& tensor,
87 Elem const& elem)
88 {
89 if (elem.template uid<Id>() == 0) {
90 return 0;
91 } else if (elem.template uid<Id>() == 1) {
92 return access(tensor, elem);
93 } else {
94 return -access(tensor, elem);
95 }
96 }
97
98 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
100 {
101 assert(false);
102 std::array<std::size_t, rank()> ids;
103 for (auto i = ids.begin(); i < ids.end(); ++i) {
104 *i = 0;
105 }
106 return ids;
107 }
108};
109
110static_assert(
111 TensorIndexConcept<TensorLeviCivitaIndex<TensorNaturalIndex<detail::DimensionConcept>>>);
112
113} // namespace tensor
114
115} // namespace sil
KOKKOS_FUNCTION constexpr int permutation_parity(std::array< std::size_t, N > const &lst)
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION constexpr std::size_t mem_id(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static constexpr std::size_t access_size()
static KOKKOS_FUNCTION constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
static constexpr std::size_t mem_size()
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)
static constexpr std::size_t rank()
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
static constexpr std::size_t size()
static constexpr bool is_explicitely_stored_tensor
ddc::DiscreteDomain< TensorIndex... > subindices_domain_t
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t mem_id)