SimiLie
Loading...
Searching...
No Matches
weighted_levi_civita_discrete_hodge_star.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 <similie/exterior/discrete_hodge_star/discrete_hodge_star.hpp>
8#include <similie/tensor/tensor_indices/weighted_levi_civita_tensor.hpp>
9
10namespace sil::exterior {
11namespace detail {
12
13template <class SourceIndices, class TargetIndices>
14using WeightedHodgeIndex = tensor::TensorWeightedLeviCivitaIndex<
17
18template <class SourceIndices, class TargetIndices>
19KOKKOS_FUNCTION std::array<std::size_t, 2> weighted_hodge_components(std::size_t entry)
20{
21 using Index = WeightedHodgeIndex<SourceIndices, TargetIndices>;
23 std::array<std::size_t, Target::rank()> target {};
24 std::array const ids = Index::mem_id_to_canonical_natural_ids(entry);
25 for (std::size_t i = 0; i < target.size(); ++i)
26 target[i] = ids[ddc::type_seq_size_v<SourceIndices> + i];
27 return {entry, Target::mem_id(target)};
28}
29
30template <class SourceIndices, class TargetIndices, class Output, class Input>
31KOKKOS_FUNCTION void apply_weighted_hodge(
32 Output const& output,
33 Input const& input,
35 weights)
36{
37 using Index = WeightedHodgeIndex<SourceIndices, TargetIndices>;
38 tensor::Tensor coefficients(
39 ddc::ChunkSpan<
40 double const,
41 ddc::DiscreteDomain<Index>,
42 Kokkos::layout_right,
43 typename Input::
44 memory_space>(weights.data(), tensor::TensorAccessor<Index>::domain()));
45 if constexpr (ddc::type_seq_size_v<SourceIndices> == 0) {
46 tensor::Tensor scalar(
47 ddc::ChunkSpan<
48 typename Input::element_type,
49 ddc::DiscreteDomain<>,
50 Kokkos::layout_right,
51 typename Input::
52 memory_space>(input.data_handle(), ddc::DiscreteDomain<>()));
53 tensor::tensor_prod(output, coefficients, scalar);
54 } else {
55 tensor::tensor_prod(output, coefficients, input);
56 }
57 ddc::device_for_each(output.domain(), [&](auto component) {
58 output.mem(component) /= misc::factorial(ddc::type_seq_size_v<SourceIndices>);
59 });
60}
61
62template <class Operator, class SourceIndices, class TargetIndices, class Metric>
63class WeightedHodgeStorage
64{
65 using Index = WeightedHodgeIndex<SourceIndices, TargetIndices>;
68 using Domain = typename Metric::non_indices_domain_t;
69 using Element = typename Domain::discrete_element_type;
70 Domain m_domain;
71 Kokkos::View<double**, Kokkos::LayoutLeft, typename Metric::memory_space> m_weights;
72
73 KOKKOS_FUNCTION std::array<double, Index::mem_size()> weights(Element element) const
74 {
75 std::array<double, Index::mem_size()> result {};
76 std::size_t const node = stencil_node(m_domain, element);
77 for (std::size_t entry = 0; entry < result.size(); ++entry)
78 result[entry] = m_weights(node, entry);
79 return result;
80 }
81
82public:
83 using execution_space = typename Metric::memory_space::execution_space;
84
85 template <class Position>
86 static WeightedHodgeStorage build(
87 execution_space const& exec,
88 Metric const& metric,
89 Position const& position)
90 {
91 WeightedHodgeStorage result;
92 result.m_domain = metric.non_indices_domain();
93 result.m_weights
94 = Kokkos::View<double**, Kokkos::LayoutLeft, typename Metric::memory_space>(
95 "weighted_levi_civita_hodge_coefficients",
96 result.m_domain.size(),
98 WeightedHodgeStorage const storage = result;
99 ddc::parallel_for_each(
100 "cache_weighted_levi_civita_hodge",
101 exec,
102 result.m_domain,
103 KOKKOS_LAMBDA(Element element) {
104 auto const stencil = Operator::value(metric, position, element);
105 std::size_t const node = stencil_node(storage.m_domain, element);
106 for (std::size_t entry = 0; entry < Index::mem_size(); ++entry) {
107 std::array const components
108 = weighted_hodge_components<SourceIndices, TargetIndices>(entry);
109 storage.m_weights(node, entry)
110 = stencil.weights
111 [components[1] * Source::mem_size() + components[0]];
112 }
113 });
114 return result;
115 }
116
117 template <class Output, class Input, class Position>
118 KOKKOS_FUNCTION void apply_pointwise(
119 Output const& output,
120 Input const& input,
121 Metric const&,
122 Position const&,
123 Element element) const
124 {
125 apply_weighted_hodge<SourceIndices, TargetIndices>(output, input, weights(element));
126 }
127 template <class Output, class Input>
128 Output apply(execution_space const& exec, Output const& output, Input const& input) const
129 {
130 WeightedHodgeStorage const storage = *this;
131 ddc::parallel_for_each(
132 "apply_weighted_levi_civita_hodge",
133 exec,
134 output.non_indices_domain(),
135 KOKKOS_LAMBDA(Element element) {
136 apply_weighted_hodge<SourceIndices, TargetIndices>(
137 output[element],
138 input[element],
139 storage.weights(element));
140 });
141 return output;
142 }
143 template <class Position>
144 KOKKOS_FUNCTION LocalOperatorStencil<Source, Target> value(
145 Metric const&,
146 Position const&,
147 Element element) const
148 {
149 LocalOperatorStencil<Source, Target> result;
150 std::array const coefficients = weights(element);
151 for (std::size_t entry = 0; entry < coefficients.size(); ++entry) {
152 std::array const components
153 = weighted_hodge_components<SourceIndices, TargetIndices>(entry);
154 result.weights[components[1] * Source::mem_size() + components[0]]
155 = coefficients[entry];
156 }
157 return result;
158 }
159 template <class Position, class Natural>
160 KOKKOS_FUNCTION double value(
161 Metric const& metric,
162 Position const& position,
163 Element element,
164 Natural natural) const
165 {
168 return local_stencil_coefficient<Source, Target>(
169 value(metric, position, element),
170 ddc::detail::array(SourceElement(natural)),
171 ddc::detail::array(TargetElement(natural)));
172 }
173};
174} // namespace detail
175
181template <
182 CellComplex Complex,
183 class Indices1,
184 class Indices2,
185 class MetricType,
186 class PositionType,
187 class BatchElem>
189{
191 using Target = reduction_index_t<Indices2>;
193 using MetricIndex
194 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename MetricType::indices_domain_t>>;
197
198 KOKKOS_FUNCTION static std::array<double, Index::mem_size()> weights(
199 MetricType const& metric,
200 PositionType const& position,
201 BatchElem element)
202 {
203 auto const reconstruction = Reconstruction<
205 PositionType,
206 BatchElem,
208 tensor::TensorDiagonalIndex>::value(position, element);
209 auto const reduction = Reduction<
210 Indices2,
211 PositionType,
212 BatchElem,
213 Complex,
214 tensor::TensorDiagonalIndex>::value(metric, position, element);
215 std::array<double, Index::mem_size()> result {};
216 for (std::size_t entry = 0; entry < result.size(); ++entry) {
217 std::array const components
218 = detail::weighted_hodge_components<Indices1, Indices2>(entry);
219 result[entry]
220 = reconstruction.weights[components[0] * Source::mem_size() + components[0]]
221 * reduction.weights[components[1] * Target::mem_size() + components[1]]
223 }
224 return result;
225 }
226
227public:
228 using staged_storage_type = detail::WeightedHodgeStorage<
230 Indices1,
231 Indices2,
232 MetricType>;
233
234 template <CochainType Output, CochainType Input>
235 KOKKOS_FUNCTION static void operator()(
236 Output const& output,
237 Input const& input,
238 MetricType const& metric,
239 PositionType const& position,
240 BatchElem element)
241 {
242 detail::apply_weighted_hodge<
243 Indices1,
244 Indices2>(output, input, weights(metric, position, element));
246 KOKKOS_FUNCTION static detail::LocalOperatorStencil<Source, Target> value(
247 MetricType const& metric,
248 PositionType const& position,
249 BatchElem element)
250 {
251 detail::LocalOperatorStencil<Source, Target> result;
252 std::array const coefficients = weights(metric, position, element);
253 for (std::size_t entry = 0; entry < coefficients.size(); ++entry) {
254 std::array const components
255 = detail::weighted_hodge_components<Indices1, Indices2>(entry);
256 result.weights[components[1] * Source::mem_size() + components[0]]
257 = coefficients[entry];
258 }
259 return result;
260 }
261 template <class Natural>
262 KOKKOS_FUNCTION static double value(
263 MetricType const& metric,
264 PositionType const& position,
265 BatchElem element,
266 Natural natural)
267 {
270 return detail::local_stencil_coefficient<Source, Target>(
271 value(metric, position, element),
272 ddc::detail::array(SourceElement(natural)),
273 ddc::detail::array(TargetElement(natural)));
274 }
275};
276
277
278} // namespace sil::exterior
Discrete Hodge star for an identity metric and coordinate-aligned orthogonal cells.
detail::WeightedHodgeStorage< WeightedLeviCivitaDiscreteHodgeStar, Indices1, Indices2, MetricType > staged_storage_type
static KOKKOS_FUNCTION void operator()(Output const &output, Input const &input, MetricType const &metric, PositionType const &position, BatchElem element)
static KOKKOS_FUNCTION detail::LocalOperatorStencil< Source, Target > value(MetricType const &metric, PositionType const &position, BatchElem element)
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
typename detail::ReductionTargetIndex< Indices >::type reduction_index_t
KOKKOS_FUNCTION constexpr int permutation_parity(std::array< std::size_t, N > const &lst)
typename detail::ConvertTypeSeqTo< T, Seq >::type convert_type_seq_to_t
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
ddc::type_seq_element_t< 0, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_1
Definition metric.hpp:33
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)
detail::Lower< T >::type lower_t
Definition character.hpp:79
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t id)