8#include <similie/misc/macros.hpp>
9#include <similie/misc/small_matrix.hpp>
10#include <similie/misc/type_seq_ext.hpp>
11#include <similie/misc/unsecure_parallel_deepcopy.hpp>
12#include <similie/tensor/tensor_indices/character.hpp>
13#include <similie/tensor/tensor_indices/diagonal_tensor.hpp>
14#include <similie/tensor/tensor_indices/identity_tensor.hpp>
15#include <similie/tensor/tensor_indices/lorentzian_sign_tensor.hpp>
16#include <similie/tensor/tensor_indices/prime.hpp>
17#include <similie/tensor/tensor_indices/relabelization.hpp>
18#include <similie/tensor/tensor_indices/symmetric_tensor.hpp>
19#include <similie/tensor/tensor_prod.hpp>
27template <
class... CDim>
32template <
class MetricIndex>
34 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename MetricIndex::subindices_domain_t>>;
36template <
class... CDim>
41template <
class MetricIndex>
43 = ddc::type_seq_element_t<1, ddc::to_type_seq_t<typename MetricIndex::subindices_domain_t>>;
47template <
class TypeSeqCDim>
48struct ConvertTypeSeqToMetricIndex1;
50template <
class... CDim>
51struct ConvertTypeSeqToMetricIndex1<
ddc::TypeSeq<CDim...>>
56template <
class TypeSeqCDim>
57struct ConvertTypeSeqToMetricIndex2;
59template <
class... CDim>
60struct ConvertTypeSeqToMetricIndex2<
ddc::TypeSeq<CDim...>>
75 typename detail::ConvertTypeSeqToMetricIndex1<
76 typename Index1::type_seq_dimensions>::type,
77 typename detail::ConvertTypeSeqToMetricIndex2<
78 typename Index2::type_seq_dimensions>::type>,
81template <TensorNatIndex Index1, TensorNatIndex Index2,
class Dom>
87 typename detail::ConvertTypeSeqToMetricIndex1<
88 typename Index1::type_seq_dimensions>::type,
89 typename detail::ConvertTypeSeqToMetricIndex2<
90 typename Index2::type_seq_dimensions>::type>,
94template <misc::Specialization<Tensor> TensorType, TensorNatIndex Index1, TensorNatIndex Index2>
98 typename detail::ConvertTypeSeqToMetricIndex1<
99 typename Index1::type_seq_dimensions>::type,
100 typename detail::ConvertTypeSeqToMetricIndex2<
101 typename Index2::type_seq_dimensions>::type>,
104template <TensorNatIndex Index1, TensorNatIndex Index2, misc::Specialization<Tensor> TensorType>
106 TensorType
const& tensor)
110 typename detail::ConvertTypeSeqToMetricIndex1<
111 typename Index1::type_seq_dimensions>::type,
112 typename detail::ConvertTypeSeqToMetricIndex2<
113 typename Index2::type_seq_dimensions>::type>,
120template <
class MetricIndex,
class Indices1,
class Indices2>
121struct MetricProdDomainType;
123template <
class MetricIndex,
class... Index1,
class... Index2>
124struct MetricProdDomainType<MetricIndex,
ddc::TypeSeq<Index1...>, ddc::TypeSeq<Index2...>>
126 static_assert(
sizeof...(Index1) ==
sizeof...(Index2));
128 ddc::DiscreteDomain<MetricIndex>,
130 ddc::type_seq_element_t<
131 ddc::type_seq_rank_v<Index1, ddc::TypeSeq<Index1...>>,
132 ddc::TypeSeq<Index2...>>>...>;
138 TensorIndex MetricIndex,
142 typename detail::MetricProdDomainType<MetricIndex, Indices1, Indices2>::type;
152 class LayoutStridedPolicy,
154struct MetricProdType;
161 class LayoutStridedPolicy,
163struct MetricProdType<
166 ddc::TypeSeq<Index1...>,
167 ddc::TypeSeq<Index2...>,
177 ddc::TypeSeq<Index1...>,
178 ddc::TypeSeq<Index2...>>>,
183template <
class MetricType,
class BatchElem,
class Indices1,
class Indices2>
184struct MetricProdValue;
186template <
class MetricType,
class BatchElem>
187struct MetricProdValue<MetricType, BatchElem,
ddc::TypeSeq<>, ddc::TypeSeq<>>
189 KOKKOS_FUNCTION
static double operator()(
190 [[maybe_unused]] MetricType metric,
191 [[maybe_unused]] BatchElem elem,
192 [[maybe_unused]]
auto natural_elem)
205struct MetricProdValue<
208 ddc::TypeSeq<HeadIndex1, TailIndex1...>,
209 ddc::TypeSeq<HeadIndex2, TailIndex2...>>
211 KOKKOS_FUNCTION
static double operator()(
212 MetricType
const& metric,
217 using metric_natural_elem_type =
typename decltype(relabeled_metric)::accessor_t::
218 natural_domain_t::discrete_element_type;
219 metric_natural_elem_type
const metric_natural_elem(natural_elem);
220 return relabeled_metric.get(relabeled_metric.access_element(elem, metric_natural_elem))
224 ddc::TypeSeq<TailIndex1...>,
225 ddc::TypeSeq<TailIndex2...>>::operator()(metric, elem, natural_elem);
233 TensorIndex MetricIndex,
236 class LayoutStridedPolicy,
254 template <misc::Specialization<Tensor> MetricProdType_>
256 MetricProdType_ metric_prod,
257 MetricType
const& metric,
260 ddc::device_for_each(metric_prod.domain(), [&](
auto mem_elem) {
261 metric_prod.mem(mem_elem)
262 = value(metric, elem, metric_prod.canonical_natural_element(mem_elem));
266 KOKKOS_FUNCTION
static double value(MetricType
const& metric, BatchElem elem,
auto natural_elem)
268 return detail::MetricProdValue<MetricType, BatchElem, Indices1, Indices2>::
269 operator()(metric, elem, natural_elem);
274 TensorIndex MetricIndex,
275 misc::Specialization<ddc::TypeSeq> Indices1,
276 misc::Specialization<ddc::TypeSeq> Indices2,
277 misc::Specialization<Tensor> MetricType,
280 typename MetricType::non_indices_domain_t,
284 typename MetricType::layout_type,
285 typename MetricType::memory_space>
287 ExecSpace
const& exec_space,
289 typename MetricType::non_indices_domain_t,
293 typename MetricType::layout_type,
294 typename MetricType::memory_space> metric_prod,
295 MetricType
const& metric)
297 SIMILIE_DEBUG_LOG(
"similie_compute_metric_prod");
298 ddc::parallel_for_each(
299 "similie_compute_metric_prod",
301 metric_prod.non_indices_domain(),
303 typename decltype(metric_prod)::non_indices_domain_t::discrete_element_type
305 MetricProd<MetricIndex, Indices1, Indices2, MetricType,
decltype(elem)>::
306 operator()(metric_prod[elem], metric, elem);
320 misc::Specialization<Tensor> MetricType,
321 misc::Specialization<Tensor> TensorType,
326 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>
328 ExecSpace
const& exec_space,
329 TensorType
const& tensor,
330 MetricType
const& metric_prod)
332 ddc::Chunk result_alloc(
335 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>,
336 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>(
338 ddc::KokkosAllocator<double, typename ExecSpace::memory_space>());
342 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>
343 result(result_alloc);
344 SIMILIE_DEBUG_LOG(
"similie_inplace_apply_metric");
345 ddc::parallel_for_each(
346 "similie_inplace_apply_metric",
348 tensor.non_indices_domain(),
349 KOKKOS_LAMBDA(
typename TensorType::non_indices_domain_t::discrete_element_type elem) {
355 typename MetricType::accessor_t::natural_domain_t>>,
357 typename MetricType::accessor_t::natural_domain_t>>>>(
360 misc::detail::unsecure_parallel_deepcopy(exec_space, tensor, result);
364 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>(tensor);
368 TensorIndex MetricIndex,
369 TensorNatIndex... Index1,
376 ddc::TypeSeq<Index1...>>
378 ExecSpace
const& exec_space,
379 TensorType
const& tensor,
380 MetricType
const& metric)
384 ddc::TypeSeq<Index1...>,
385 primes<ddc::TypeSeq<Index1...>>>>
386 metric_prod_accessor;
387 ddc::Chunk metric_prod_alloc(
389 typename TensorType::non_indices_domain_t,
392 ddc::TypeSeq<Index1...>,
393 primes<ddc::TypeSeq<Index1...>>>>(
394 tensor.non_indices_domain(),
395 metric_prod_accessor.domain()),
396 ddc::KokkosAllocator<double, typename ExecSpace::memory_space>());
401 ddc::TypeSeq<Index1...>,
402 primes<ddc::TypeSeq<Index1...>>>(exec_space, metric_prod, metric);
407template <misc::Specialization<Tensor> MetricType>
410 ddc::to_type_seq_t<typename MetricType::accessor_t::natural_domain_t>,
413template <TensorIndex MetricIndex, misc::Specialization<Tensor> MetricType,
class BatchElem>
418 template <misc::Specialization<Tensor> OutputTensorType>
420 OutputTensorType inv_metric,
421 MetricType
const& metric,
427 ddc::device_for_each(inv_metric.domain(), [&](
auto mem_elem) {
428 inv_metric.mem(mem_elem)
429 = value(metric, elem, inv_metric.canonical_natural_element(mem_elem));
432 ddc::device_for_each(inv_metric.domain(), [&](
auto mem_elem) {
433 inv_metric.mem(mem_elem)
434 = value(metric, elem, inv_metric.canonical_natural_element(mem_elem));
438 using memory_space =
typename MetricType::memory_space;
442 std::array<double, N * N> matrix_alloc {};
443 std::array<double, N * N> inverse_alloc {};
444 std::array<double, N * N> workspace_alloc {};
452 for (std::size_t i = 0; i < N; ++i) {
453 for (std::size_t j = 0; j < N; ++j) {
454 matrix_view(i, j) = metric.get(metric.access_element(
456 ddc::DiscreteElement<metric_index_1_t, metric_index_2_t>(i, j)));
461 assert(success &&
"Kokkos-kernels failed at inverting metric tensor");
463 ddc::device_for_each(inv_metric.domain(), [&](
auto mem_elem) {
464 auto const natural_elem = inv_metric.canonical_natural_element(mem_elem);
465 auto const ids = ddc::detail::array(natural_elem);
466 inv_metric.mem(mem_elem) = inverse_view(ids[0], ids[1]);
471 KOKKOS_FUNCTION
static double value(MetricType
const& metric, BatchElem elem,
auto natural_elem)
474 auto const ids = ddc::detail::array(natural_elem);
475 return ids[0] == ids[1] ? 1. : 0.;
477 return metric.get(metric.access_element(elem, natural_elem));
478 }
else if constexpr (misc::Specialization<MetricIndex, TensorDiagonalIndex>) {
483 typename MetricType::accessor_t::natural_domain_t>>,
485 typename MetricType::accessor_t::natural_domain_t>>(
487 .access_element(elem, natural_elem));
488 }
else if constexpr (misc::Specialization<MetricIndex, TensorSymmetricIndex>) {
489 [[maybe_unused]]
typename output_tensor_type::accessor_t accessor;
490 std::array<double, output_tensor_type::accessor_t::domain().size()> alloc {};
493 typename output_tensor_type::indices_domain_t,
494 Kokkos::layout_right,
495 typename MetricType::memory_space>
496 span(alloc.data(), accessor.domain());
499 return local_inverse.get(local_inverse.access_element(natural_elem));
507template <TensorIndex MetricIndex, misc::Specialization<Tensor> MetricType,
class ExecSpace>
509 ExecSpace
const& exec_space,
511 MetricType
const& metric)
513 SIMILIE_DEBUG_LOG(
"similie_invert_metric");
514 ddc::parallel_for_each(
515 "similie_invert_metric",
517 inv_metric.non_indices_domain(),
520 MetricType>::non_indices_domain_t::discrete_element_type elem) {
522 operator()(inv_metric[elem], metric, elem);
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 unmanaged_matrix_view_t< Scalar, MemorySpace > matrix_view(Scalar *data, std::size_t rows, std::size_t cols)
convert_type_seq_to_t< ddc::DiscreteDomain, typename detail::CartesianProd< Domains... >::type > cartesian_prod_t
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
relabelize_indices_of_t< MetricType, ddc::to_type_seq_t< typename MetricType::accessor_t::natural_domain_t >, swap_character_t< ddc::to_type_seq_t< typename MetricType::accessor_t::natural_domain_t > > > invert_metric_t
typename detail::MetricProdType< NonMetricDom, MetricIndex, Indices1, Indices2, LayoutStridedPolicy, MemorySpace >::type metric_prod_t
detail::RelabelizeIndicesOfType< TensorType, OldIndices, NewIndices >::type relabelize_indices_of_t
ddc::type_seq_element_t< 0, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_1
KOKKOS_FUNCTION constexpr relabelize_indices_in_t< T, OldIndices, NewIndices > relabelize_indices_in(T t)
detail::Primes< Indices, I >::type primes
relabelize_indices_of_t< TensorType, ddc::TypeSeq< typename detail::ConvertTypeSeqToMetricIndex1< typename Index1::type_seq_dimensions >::type, typename detail::ConvertTypeSeqToMetricIndex2< typename Index2::type_seq_dimensions >::type >, ddc::TypeSeq< uncharacterize_t< Index1 >, uncharacterize_t< Index2 > > > relabelize_metric_t
metric_prod_t< typename MetricType::non_indices_domain_t, MetricIndex, Indices1, Indices2, typename MetricType::layout_type, typename MetricType::memory_space > fill_metric_prod(ExecSpace const &exec_space, metric_prod_t< typename MetricType::non_indices_domain_t, MetricIndex, Indices1, Indices2, typename MetricType::layout_type, typename MetricType::memory_space > metric_prod, MetricType const &metric)
relabelize_indices_of_t< TensorType, swap_character_t< detail::non_primes< typename MetricType::accessor_t::natural_domain_t > >, detail::non_primes< typename MetricType::accessor_t::natural_domain_t > > inplace_apply_metric(ExecSpace const &exec_space, TensorType const &tensor, MetricType const &metric_prod)
KOKKOS_FUNCTION constexpr relabelize_metric_in_domain_t< Dom, Index1, Index2 > relabelize_metric_in_domain(Dom metric_dom)
ddc::type_seq_element_t< 1, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_2
typename detail::MetricProdDomainType< MetricIndex, Indices1, Indices2 >::type metric_prod_domain_t
Tensor< ElementType, ddc::DiscreteDomain< ProdDDim... >, LayoutStridedPolicy, MemorySpace > tensor_prod(Tensor< ElementType, ddc::DiscreteDomain< ProdDDim... >, LayoutStridedPolicy, MemorySpace > const &prod_tensor, Tensor< ElementType, ddc::DiscreteDomain< DDim1... >, LayoutStridedPolicy, MemorySpace > const &tensor1, Tensor< ElementType, ddc::DiscreteDomain< DDim2... >, LayoutStridedPolicy, MemorySpace > const &tensor2)
KOKKOS_FUNCTION constexpr relabelize_metric_t< TensorType, Index1, Index2 > relabelize_metric(TensorType const &tensor)
detail::SwapCharacter< T >::type swap_character_t
KOKKOS_FUNCTION constexpr relabelize_indices_of_t< Tensor, OldIndices, NewIndices > relabelize_indices_of(Tensor const &tensor)
detail::Uncharacterize< Index >::type uncharacterize_t
invert_metric_t< MetricType > fill_inverse_metric(ExecSpace const &exec_space, invert_metric_t< MetricType > inv_metric, MetricType const &metric)
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
relabelize_indices_in_t< Dom, ddc::TypeSeq< typename detail::ConvertTypeSeqToMetricIndex1< typename Index1::type_seq_dimensions >::type, typename detail::ConvertTypeSeqToMetricIndex2< typename Index2::type_seq_dimensions >::type >, ddc::TypeSeq< uncharacterize_t< Index1 >, uncharacterize_t< Index2 > > > relabelize_metric_in_domain_t
typename detail::RelabelizeIndicesInType< T, OldIndices, NewIndices >::type relabelize_indices_in_t
The top-level namespace of SimiLie.
invert_metric_t< MetricType > output_tensor_type
static KOKKOS_FUNCTION double value(MetricType const &metric, BatchElem elem, auto natural_elem)
static KOKKOS_FUNCTION void operator()(OutputTensorType inv_metric, MetricType const &metric, BatchElem elem)
static KOKKOS_FUNCTION double value(MetricType const &metric, BatchElem elem, auto natural_elem)
static KOKKOS_FUNCTION void operator()(MetricProdType_ metric_prod, MetricType const &metric, BatchElem elem)