SimiLie
Loading...
Searching...
No Matches
metric.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/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>
20
21namespace sil {
22
23namespace tensor {
24
25// Abstract indices used to characterize a generic metric
26
27template <class... CDim>
29{
30};
31
32template <class MetricIndex>
34 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename MetricIndex::subindices_domain_t>>;
35
36template <class... CDim>
37struct MetricIndex2 : TensorNaturalIndex<CDim...>
38{
39};
40
41template <class MetricIndex>
43 = ddc::type_seq_element_t<1, ddc::to_type_seq_t<typename MetricIndex::subindices_domain_t>>;
44
45namespace detail {
46
47template <class TypeSeqCDim>
48struct ConvertTypeSeqToMetricIndex1;
49
50template <class... CDim>
51struct ConvertTypeSeqToMetricIndex1<ddc::TypeSeq<CDim...>>
52{
53 using type = MetricIndex1<CDim...>;
54};
55
56template <class TypeSeqCDim>
57struct ConvertTypeSeqToMetricIndex2;
58
59template <class... CDim>
60struct ConvertTypeSeqToMetricIndex2<ddc::TypeSeq<CDim...>>
61{
62 using type = MetricIndex2<CDim...>;
63};
64
65} // namespace detail
66
67// Relabelize metric
68template <
71 TensorNatIndex Index2>
73 Dom,
74 ddc::TypeSeq<
75 typename detail::ConvertTypeSeqToMetricIndex1<
76 typename Index1::type_seq_dimensions>::type,
77 typename detail::ConvertTypeSeqToMetricIndex2<
78 typename Index2::type_seq_dimensions>::type>,
79 ddc::TypeSeq<uncharacterize_t<Index1>, uncharacterize_t<Index2>>>;
80
81template <TensorNatIndex Index1, TensorNatIndex Index2, class Dom>
83relabelize_metric_in_domain(Dom metric_dom)
84{
86 ddc::TypeSeq<
87 typename detail::ConvertTypeSeqToMetricIndex1<
88 typename Index1::type_seq_dimensions>::type,
89 typename detail::ConvertTypeSeqToMetricIndex2<
90 typename Index2::type_seq_dimensions>::type>,
91 ddc::TypeSeq<uncharacterize_t<Index1>, uncharacterize_t<Index2>>>(metric_dom);
92}
94template <misc::Specialization<Tensor> TensorType, TensorNatIndex Index1, TensorNatIndex Index2>
96 TensorType,
97 ddc::TypeSeq<
98 typename detail::ConvertTypeSeqToMetricIndex1<
99 typename Index1::type_seq_dimensions>::type,
100 typename detail::ConvertTypeSeqToMetricIndex2<
101 typename Index2::type_seq_dimensions>::type>,
102 ddc::TypeSeq<uncharacterize_t<Index1>, uncharacterize_t<Index2>>>;
104template <TensorNatIndex Index1, TensorNatIndex Index2, misc::Specialization<Tensor> TensorType>
106 TensorType const& tensor)
107{
109 ddc::TypeSeq<
110 typename detail::ConvertTypeSeqToMetricIndex1<
111 typename Index1::type_seq_dimensions>::type,
112 typename detail::ConvertTypeSeqToMetricIndex2<
113 typename Index2::type_seq_dimensions>::type>,
114 ddc::TypeSeq<uncharacterize_t<Index1>, uncharacterize_t<Index2>>>(tensor);
115}
116
117// Compute domain for a tensor product of metrics (ie. g_mu_muprime*g_nu_nuprime*...)
118namespace detail {
119
120template <class MetricIndex, class Indices1, class Indices2>
121struct MetricProdDomainType;
122
123template <class MetricIndex, class... Index1, class... Index2>
124struct MetricProdDomainType<MetricIndex, ddc::TypeSeq<Index1...>, ddc::TypeSeq<Index2...>>
125{
126 static_assert(sizeof...(Index1) == sizeof...(Index2));
128 ddc::DiscreteDomain<MetricIndex>,
129 Index1,
130 ddc::type_seq_element_t<
131 ddc::type_seq_rank_v<Index1, ddc::TypeSeq<Index1...>>,
132 ddc::TypeSeq<Index2...>>>...>;
133};
134
135} // namespace detail
136
137template <
138 TensorIndex MetricIndex,
142 typename detail::MetricProdDomainType<MetricIndex, Indices1, Indices2>::type;
143
144namespace detail {
145
146// Compute tensor product of metrics (ie. g_mu_muprime*g_nu_nuprime*...)
147template <
148 class NonMetricDom,
149 class MetricIndex,
150 class Indices1,
151 class Indices2,
152 class LayoutStridedPolicy,
153 class MemorySpace>
154struct MetricProdType;
155
156template <
157 class NonMetricDom,
158 class MetricIndex,
159 class... Index1,
160 class... Index2,
161 class LayoutStridedPolicy,
162 class MemorySpace>
163struct MetricProdType<
164 NonMetricDom,
165 MetricIndex,
166 ddc::TypeSeq<Index1...>,
167 ddc::TypeSeq<Index2...>,
168 LayoutStridedPolicy,
169 MemorySpace>
170{
171 using type = tensor::Tensor<
172 double,
174 NonMetricDom,
176 MetricIndex,
177 ddc::TypeSeq<Index1...>,
178 ddc::TypeSeq<Index2...>>>,
179 LayoutStridedPolicy,
180 MemorySpace>;
181};
182
183template <class MetricType, class BatchElem, class Indices1, class Indices2>
184struct MetricProdValue;
185
186template <class MetricType, class BatchElem>
187struct MetricProdValue<MetricType, BatchElem, ddc::TypeSeq<>, ddc::TypeSeq<>>
188{
189 KOKKOS_FUNCTION static double operator()(
190 [[maybe_unused]] MetricType metric,
191 [[maybe_unused]] BatchElem elem,
192 [[maybe_unused]] auto natural_elem)
193 {
194 return 1.;
195 }
196};
197
198template <
199 class MetricType,
200 class BatchElem,
201 class HeadIndex1,
202 class... TailIndex1,
203 class HeadIndex2,
204 class... TailIndex2>
205struct MetricProdValue<
206 MetricType,
207 BatchElem,
208 ddc::TypeSeq<HeadIndex1, TailIndex1...>,
209 ddc::TypeSeq<HeadIndex2, TailIndex2...>>
210{
211 KOKKOS_FUNCTION static double operator()(
212 MetricType const& metric,
213 BatchElem elem,
214 auto natural_elem)
215 {
216 auto const relabeled_metric = relabelize_metric<HeadIndex1, HeadIndex2>(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))
221 * MetricProdValue<
222 MetricType,
223 BatchElem,
224 ddc::TypeSeq<TailIndex1...>,
225 ddc::TypeSeq<TailIndex2...>>::operator()(metric, elem, natural_elem);
226 }
227};
228
229} // namespace detail
230
231template <
233 TensorIndex MetricIndex,
236 class LayoutStridedPolicy,
237 class MemorySpace>
238using metric_prod_t = typename detail::MetricProdType<
239 NonMetricDom,
240 MetricIndex,
241 Indices1,
242 Indices2,
243 LayoutStridedPolicy,
244 MemorySpace>::type;
245
246template <
247 TensorIndex MetricIndex,
251 class BatchElem>
252struct MetricProd
254 template <misc::Specialization<Tensor> MetricProdType_>
255 KOKKOS_FUNCTION static void operator()(
256 MetricProdType_ metric_prod,
257 MetricType const& metric,
258 BatchElem elem)
259 {
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));
263 });
265
266 KOKKOS_FUNCTION static double value(MetricType const& metric, BatchElem elem, auto natural_elem)
267 {
268 return detail::MetricProdValue<MetricType, BatchElem, Indices1, Indices2>::
269 operator()(metric, elem, natural_elem);
270 }
271};
272
273template <
274 TensorIndex MetricIndex,
275 misc::Specialization<ddc::TypeSeq> Indices1,
276 misc::Specialization<ddc::TypeSeq> Indices2,
277 misc::Specialization<Tensor> MetricType,
278 class ExecSpace>
280 typename MetricType::non_indices_domain_t,
281 MetricIndex,
282 Indices1,
283 Indices2,
284 typename MetricType::layout_type,
285 typename MetricType::memory_space>
287 ExecSpace const& exec_space,
289 typename MetricType::non_indices_domain_t,
290 MetricIndex,
291 Indices1,
292 Indices2,
293 typename MetricType::layout_type,
294 typename MetricType::memory_space> metric_prod,
295 MetricType const& metric)
296{
297 SIMILIE_DEBUG_LOG("similie_compute_metric_prod");
298 ddc::parallel_for_each(
299 "similie_compute_metric_prod",
300 exec_space,
301 metric_prod.non_indices_domain(),
302 KOKKOS_LAMBDA(
303 typename decltype(metric_prod)::non_indices_domain_t::discrete_element_type
304 elem) {
305 MetricProd<MetricIndex, Indices1, Indices2, MetricType, decltype(elem)>::
306 operator()(metric_prod[elem], metric, elem);
307 });
308 return metric_prod;
309}
310
311namespace detail {
312
313template <class Dom>
314using non_primes = ddc::type_seq_remove_t<ddc::to_type_seq_t<Dom>, primes<ddc::to_type_seq_t<Dom>>>;
315
316} // namespace detail
317
318// Apply metrics inplace (like g_mu_muprime*T^muprime^nu)
319template <
320 misc::Specialization<Tensor> MetricType,
321 misc::Specialization<Tensor> TensorType,
322 class ExecSpace>
324 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)
331{
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>>(
337 tensor.domain()),
338 ddc::KokkosAllocator<double, typename ExecSpace::memory_space>());
340 TensorType,
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",
347 exec_space,
348 tensor.non_indices_domain(),
349 KOKKOS_LAMBDA(typename TensorType::non_indices_domain_t::discrete_element_type elem) {
351 result[elem],
352 metric_prod[elem],
354 swap_character_t<detail::non_primes<
355 typename MetricType::accessor_t::natural_domain_t>>,
356 primes<swap_character_t<detail::non_primes<
357 typename MetricType::accessor_t::natural_domain_t>>>>(
358 tensor)[elem]);
359 });
360 misc::detail::unsecure_parallel_deepcopy(exec_space, tensor, result);
361
364 detail::non_primes<typename MetricType::accessor_t::natural_domain_t>>(tensor);
365}
366
367template <
368 TensorIndex MetricIndex,
369 TensorNatIndex... Index1,
372 class ExecSpace>
374 TensorType,
375 swap_character_t<ddc::TypeSeq<Index1...>>,
376 ddc::TypeSeq<Index1...>>
378 ExecSpace const& exec_space,
379 TensorType const& tensor,
380 MetricType const& metric)
381{
383 MetricIndex,
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,
391 MetricIndex,
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>());
397 tensor::Tensor metric_prod(metric_prod_alloc);
398
400 MetricIndex,
401 ddc::TypeSeq<Index1...>,
402 primes<ddc::TypeSeq<Index1...>>>(exec_space, metric_prod, metric);
403
404 return inplace_apply_metric(exec_space, tensor, metric_prod);
405}
407template <misc::Specialization<Tensor> MetricType>
409 MetricType,
410 ddc::to_type_seq_t<typename MetricType::accessor_t::natural_domain_t>,
413template <TensorIndex MetricIndex, misc::Specialization<Tensor> MetricType, class BatchElem>
415{
418 template <misc::Specialization<Tensor> OutputTensorType>
419 KOKKOS_FUNCTION static void operator()(
420 OutputTensorType inv_metric,
421 MetricType const& metric,
422 BatchElem elem)
423 {
424 if constexpr (
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));
430 });
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));
435 });
437 constexpr std::size_t N = metric_index_1<MetricIndex>::size();
438 using memory_space = typename MetricType::memory_space;
439 using metric_index_1_t = metric_index_1<MetricIndex>;
440 using metric_index_2_t = metric_index_2<MetricIndex>;
441
442 std::array<double, N * N> matrix_alloc {};
443 std::array<double, N * N> inverse_alloc {};
444 std::array<double, N * N> workspace_alloc {};
445 auto matrix_view
446 = misc::math::matrix_view<double, memory_space>(matrix_alloc.data(), N, N);
447 auto inverse_view
448 = misc::math::matrix_view<double, memory_space>(inverse_alloc.data(), N, N);
449 auto workspace
450 = misc::math::vector_view<double, memory_space>(workspace_alloc.data(), N * N);
451
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(
455 elem,
456 ddc::DiscreteElement<metric_index_1_t, metric_index_2_t>(i, j)));
457 }
458 }
459
460 bool const success = misc::math::invert(inverse_view, matrix_view, workspace);
461 assert(success && "Kokkos-kernels failed at inverting metric tensor");
462
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]);
467 });
468 }
470
471 KOKKOS_FUNCTION static double value(MetricType const& metric, BatchElem elem, auto natural_elem)
472 {
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>) {
479 return 1.
480 / metric.get(
482 swap_character_t<ddc::to_type_seq_t<
483 typename MetricType::accessor_t::natural_domain_t>>,
484 ddc::to_type_seq_t<
485 typename MetricType::accessor_t::natural_domain_t>>(
486 metric)
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 {};
491 ddc::ChunkSpan<
492 double,
493 typename output_tensor_type::indices_domain_t,
494 Kokkos::layout_right,
495 typename MetricType::memory_space>
496 span(alloc.data(), accessor.domain());
497 tensor::Tensor local_inverse(span);
498 operator()(local_inverse, metric, elem);
499 return local_inverse.get(local_inverse.access_element(natural_elem));
500 } else {
501 return 0.;
502 }
503 }
504};
505
506// Compute invert metric (g_mu_nu for gmunu or gmunu for g_mu_nu)
507template <TensorIndex MetricIndex, misc::Specialization<Tensor> MetricType, class ExecSpace>
509 ExecSpace const& exec_space,
511 MetricType const& metric)
512{
513 SIMILIE_DEBUG_LOG("similie_invert_metric");
514 ddc::parallel_for_each(
515 "similie_invert_metric",
516 exec_space,
517 inv_metric.non_indices_domain(),
518 KOKKOS_LAMBDA(
519 typename invert_metric_t<
520 MetricType>::non_indices_domain_t::discrete_element_type elem) {
521 InverseMetric<MetricIndex, MetricType, decltype(elem)>::
522 operator()(inv_metric[elem], metric, elem);
523 });
524
525 return inv_metric;
526}
527
528} // namespace tensor
529
530} // namespace sil
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
Definition metric.hpp:406
typename detail::MetricProdType< NonMetricDom, MetricIndex, Indices1, Indices2, LayoutStridedPolicy, MemorySpace >::type metric_prod_t
Definition metric.hpp:236
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
Definition metric.hpp:33
KOKKOS_FUNCTION constexpr relabelize_indices_in_t< T, OldIndices, NewIndices > relabelize_indices_in(T t)
detail::Primes< Indices, I >::type primes
Definition prime.hpp:55
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
Definition metric.hpp:93
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)
Definition metric.hpp:284
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)
Definition metric.hpp:325
KOKKOS_FUNCTION constexpr relabelize_metric_in_domain_t< Dom, Index1, Index2 > relabelize_metric_in_domain(Dom metric_dom)
Definition metric.hpp:81
ddc::type_seq_element_t< 1, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_2
Definition metric.hpp:41
typename detail::MetricProdDomainType< MetricIndex, Indices1, Indices2 >::type metric_prod_domain_t
Definition metric.hpp:139
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)
Definition metric.hpp:103
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)
Definition metric.hpp:506
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
Definition metric.hpp:70
typename detail::RelabelizeIndicesInType< T, OldIndices, NewIndices >::type relabelize_indices_in_t
The top-level namespace of SimiLie.
Definition csr.hpp:15
invert_metric_t< MetricType > output_tensor_type
Definition metric.hpp:414
static KOKKOS_FUNCTION double value(MetricType const &metric, BatchElem elem, auto natural_elem)
Definition metric.hpp:469
static KOKKOS_FUNCTION void operator()(OutputTensorType inv_metric, MetricType const &metric, BatchElem elem)
Definition metric.hpp:417
static KOKKOS_FUNCTION double value(MetricType const &metric, BatchElem elem, auto natural_elem)
Definition metric.hpp:264
static KOKKOS_FUNCTION void operator()(MetricProdType_ metric_prod, MetricType const &metric, BatchElem elem)
Definition metric.hpp:253