SimiLie
Loading...
Searching...
No Matches
covariant_derivative.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4
5#pragma once
6
7#include <array>
8
9#include <ddc/ddc.hpp>
10
11#include <similie/exterior/coboundary/coboundary.hpp>
12
13namespace sil::exterior {
14
19template <std::size_t Rank>
21{
22 [[nodiscard]] KOKKOS_FUNCTION auto operator()(std::size_t, std::size_t) const
23 {
24 std::array<std::array<double, Rank>, Rank> result {};
25 for (std::size_t i = 0; i < Rank; ++i)
26 result[i][i] = 1;
27 return result;
28 }
29};
30
31template <class... SpatialIndex>
33{
34public:
44 template <
45 std::size_t Degree,
46 std::size_t Rank,
47 class Sampler,
48 class Transport = IdentityTransport<Rank>>
49 [[nodiscard]] KOKKOS_FUNCTION static auto cochain_value(
50 std::array<std::size_t, Degree + 1> const& directions,
51 std::size_t vertex,
52 Sampler sampler,
53 Transport transport = {})
54 {
55 // Local arrays live in the calling execution space. In a CUDA build,
56 // host-side assembly must not label its stack storage as CudaSpace.
57 KOKKOS_IF_ON_HOST(
58 (return cochain_value_impl<
59 Degree,
60 Rank,
61 Kokkos::HostSpace>(directions, vertex, sampler, transport);))
62 KOKKOS_IF_ON_DEVICE(
63 (return cochain_value_impl<
64 Degree,
65 Rank,
66 typename Kokkos::DefaultExecutionSpace::
67 memory_space>(directions, vertex, sampler, transport);))
68 }
69
70private:
71 template <
72 std::size_t Degree,
73 std::size_t Rank,
74 class MemorySpace,
75 class Sampler,
76 class Transport>
77 [[nodiscard]] KOKKOS_FUNCTION static auto cochain_value_impl(
78 std::array<std::size_t, Degree + 1> const& directions,
79 std::size_t vertex,
80 Sampler& sampler,
81 Transport& transport)
82 {
83 static_assert(Degree < sizeof...(SpatialIndex));
84 static_assert(Rank > 0);
85 std::array<double, Rank> result {};
86 std::size_t mask = 0;
87 for (std::size_t j = 0; j <= Degree; ++j) {
88 assert(directions[j] < sizeof...(SpatialIndex));
89 assert(j == 0 || directions[j - 1] < directions[j]);
90 mask |= std::size_t(1) << directions[j];
91 }
92 assert((vertex & mask) == 0);
93 // Vertex masks only adapt the public cell-local API to the existing
94 // simplex/boundary algebra. Boundary owns all incidence signs.
95 ddc::DiscreteElement<SpatialIndex...> base;
96 ddc::DiscreteVector<SpatialIndex...> vector;
97 for (std::size_t d = 0; d < sizeof...(SpatialIndex); ++d) {
98 ddc::detail::array(base)[d] = (vertex >> d) & 1;
99 ddc::detail::array(vector)[d] = (mask >> d) & 1;
100 }
101 Simplex<Degree + 1, SpatialIndex...> const
102 cell(std::integral_constant<std::size_t, Degree + 1> {}, base, vector);
103 auto const faces = boundary<MemorySpace>(cell);
104 std::array<std::array<double, Rank>, 2 * (Degree + 1)> values {};
105 std::size_t face_id = 0;
106 for (auto face = faces.begin(); face < faces.end(); ++face, ++face_id) {
107 std::size_t face_mask = 0;
108 std::size_t face_vertex = 0;
109 for (std::size_t d = 0; d < sizeof...(SpatialIndex); ++d) {
110 face_mask |= std::size_t(ddc::detail::array(face->discrete_vector())[d]) << d;
111 face_vertex |= ddc::detail::array(face->discrete_element())[d] << d;
112 }
113 auto const sample = sampler(face_mask, face_vertex);
114 auto const sampled = [&]() {
115 if constexpr (misc::Specialization<std::remove_cv_t<decltype(sample)>, Cosimplex>)
116 return sample.value();
117 else
118 return sample;
119 }();
120 if (face_vertex == vertex) {
121 values[face_id] = sampled;
122 } else {
123 auto const parallel = transport(vertex, face_vertex);
124 for (std::size_t a = 0; a < Rank; ++a)
125 for (std::size_t b = 0; b < Rank; ++b)
126 values[face_id][a] += parallel[a][b] * sampled[b];
127 }
128 }
129 for (std::size_t a = 0; a < Rank; ++a) {
130 std::array<double, 2 * (Degree + 1)> component {};
131 for (std::size_t f = 0; f < component.size(); ++f)
132 component[f] = values[f][a];
133 Kokkos::View<
134 double*,
135 Kokkos::LayoutRight,
136 MemorySpace,
137 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
138 span(component.data(), component.size());
139 Cochain<LocalChain<Simplex<Degree, SpatialIndex...>, Kokkos::LayoutRight, MemorySpace>>
140 cochain(faces, span);
141 result[a] = cochain.integrate();
142 }
143 return Cosimplex(cell, result);
144 }
145};
146
147} // namespace sil::exterior
static KOKKOS_FUNCTION auto cochain_value(std::array< std::size_t, Degree+1 > const &directions, std::size_t vertex, Sampler sampler, Transport transport={})
Exterior covariant coboundary of a bundle-valued k-cochain on a cube.
Simplex(ddc::DiscreteElement< Tag... >, ddc::DiscreteVector< T... >) -> Simplex< sizeof...(T), Tag... >
Cochain(Tensor const &) -> Cochain< Tensor >
KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > boundary(AllocationType allocation, SimplexType const &simplex)
Definition boundary.hpp:170
LocalChain(AllocationType, SimplexType, T...) -> LocalChain< std::remove_cvref_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space >
Parallel transport from the second vertex's fibre to the first one's.
KOKKOS_FUNCTION auto operator()(std::size_t, std::size_t) const