SimiLie
Loading...
Searching...
No Matches
staged_vertex_form_composition.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4#pragma once
5
6#include <similie/exterior/interior_product/interior_product.hpp>
7#include <similie/exterior/reduction_and_reconstruction/vertex_form_transfer.hpp>
8#include <similie/exterior/staged_operator/staged_operator.hpp>
9#include <similie/tensor/periodic_extrapolation_rule.hpp>
10
11namespace sil::exterior {
12namespace detail {
13// Each source k-face contributes through its 2^k vertices. Retain those
14// coefficients rather than expanding the product of the two spatial supports.
15template <class Index, class Domain, class Memory>
16struct VertexTransferFactors
17{
18 static constexpr std::size_t corners = std::size_t(1) << Index::rank();
19 using Element = typename Domain::discrete_element_type;
20 using Local = tensor::Tensor<double, ddc::DiscreteDomain<Index>, Kokkos::layout_right, Memory>;
21 Kokkos::View<
22 double * [Index::mem_size() * Index::mem_size() * corners],
23 Kokkos::LayoutLeft,
24 Memory>
25 weights;
26
27 KOKKOS_FUNCTION static Element sample(
28 Element node,
29 std::size_t component,
30 std::size_t corner,
31 int sign)
32 {
33 tensor::TensorAccessor<Index> accessor;
34 auto const natural
35 = accessor.canonical_natural_element(ddc::DiscreteElement<Index>(component));
36 auto const axes = flat_natural_elem_ids(natural);
37 for (std::size_t axis = 0; axis < axes.size(); ++axis)
38 ddc::detail::array(node)[axes[axis]] += sign * int((corner >> axis) & 1);
39 return node;
40 }
41
42 struct UnitSample
43 {
44 Element node;
45 std::size_t component;
46 template <class Field, class Point>
47 KOKKOS_FUNCTION auto operator()(Field const&, Point point) const
48 {
49 return tensor::detail::pointwise_tensor<ddc::DiscreteDomain<Index>, Memory>(
50 [&](auto c) {
51 return point == node && c.template uid<Index>() == component ? 1.0 : 0.0;
52 });
53 }
54 };
55
56 template <bool Projection, class Exec, class Field, class Position>
57 static VertexTransferFactors build(
58 Exec const& exec,
59 Field const& field,
60 Position const& position)
61 {
62 VertexTransferFactors result;
63 Domain const domain = field.non_indices_domain();
64 result.weights = decltype(weights)(
65 Kokkos::view_alloc(Kokkos::WithoutInitializing, "staged_vertex_transfer_factors"),
66 domain.size());
67 auto const weights = result.weights;
68 using Indices = ddc::to_type_seq_t<tensor::natural_domain_t<Index>>;
69 Kokkos::parallel_for(
70 "similie_cache_vertex_transfer_factors",
71 Kokkos::RangePolicy<Exec>(exec, 0, domain.size()),
72 KOKKOS_LAMBDA(std::size_t node) {
73 (void)weights;
74 (void)field;
75 (void)position;
76 Element const element = stencil_element(domain, node);
77 std::array<double, Index::mem_size()> buffer {};
78 Local local(
79 ddc::ChunkSpan<
80 double,
81 ddc::DiscreteDomain<Index>,
82 Kokkos::layout_right,
83 Memory>(
84 buffer.data(),
86 for (std::size_t source = 0; source < Index::mem_size(); ++source)
87 for (std::size_t corner = 0; corner < corners; ++corner) {
88 // Projection support follows the output face; reconstruction
89 // support follows the source face. Probe the canonical operators.
90 if constexpr (!Projection) {
92 Form(local),
93 field,
94 position,
95 element,
96 UnitSample {sample(element, source, corner, -1), source});
97 for (std::size_t target = 0; target < Index::mem_size(); ++target)
98 weights(node,
99 (target * Index::mem_size() + source) * corners
100 + corner)
101 = local.mem(ddc::DiscreteElement<Index>(target));
102 } else {
103 for (std::size_t target = 0; target < Index::mem_size(); ++target) {
105 Cochain(local),
106 field,
107 position,
108 element,
109 UnitSample {
110 sample(element, target, corner, 1),
111 source});
112 weights(node,
113 (target * Index::mem_size() + source) * corners
114 + corner)
115 = local.mem(ddc::DiscreteElement<Index>(target));
116 }
117 }
118 }
119 });
120 return result;
121 }
122
123 template <class Output, class Field>
124 KOKKOS_FUNCTION void reconstruct(
125 Output const& output,
126 Field const& field,
127 Domain const& domain,
128 Element element) const
129 {
130 std::size_t const node = stencil_node(domain, element);
131 for (std::size_t target = 0; target < Index::mem_size(); ++target) {
132 double value = 0;
133 for (std::size_t source = 0; source < Index::mem_size(); ++source)
134 for (std::size_t corner = 0; corner < corners; ++corner) {
135 auto const point = tensor::PeriodicExtrapolationRule {}(
136 field,
137 sample(element, source, corner, -1));
138 value += weights(node, (target * Index::mem_size() + source) * corners + corner)
139 * point.mem(ddc::DiscreteElement<Index>(source));
140 }
141 output.mem(ddc::DiscreteElement<Index>(target)) = value;
142 }
143 }
144};
145} // namespace detail
146
153template <
154 class Operator,
155 class First,
156 class Second,
157 class Target,
158 class Domain,
159 class Memory,
160 class InverseMetric>
162{
163 struct Storage
164 {
165 using execution_space = typename Memory::execution_space;
166 Domain domain;
167 InverseMetric inverse_metric;
168 detail::VertexTransferFactors<First, Domain, Memory> first_factors;
169 detail::VertexTransferFactors<Second, Domain, Memory> second_factors;
170 detail::VertexTransferFactors<Target, Domain, Memory> projection;
171
172 template <class Output, class A, class B, class Position>
174 execution_space const& exec,
175 Output const& output,
176 A const& a,
177 B const& b,
178 Position const& position,
179 InverseMetric const& inverse_metric)
180 {
181 if (a.non_indices_domain() != b.non_indices_domain())
182 throw std::invalid_argument("Vertex composition requires equal input domains");
183 return {a.non_indices_domain(),
185 detail::VertexTransferFactors<First, Domain, Memory>::template build<
186 false>(exec, a, position),
187 detail::VertexTransferFactors<Second, Domain, Memory>::template build<
188 false>(exec, b, position),
189 detail::VertexTransferFactors<Target, Domain, Memory>::template build<
190 true>(exec, Form(output.coefficients()), position)};
191 }
192
193 template <class Output, class A, class B>
194 Output apply(execution_space const& exec, Output const& output, A const& a, B const& b)
195 const
196 {
197 if (a.non_indices_domain() != domain || b.non_indices_domain() != domain
198 || output.non_indices_domain() != domain)
199 throw std::invalid_argument("Staged vertex composition domain changed");
200 Storage const storage = *this;
201 Kokkos::parallel_for(
202 "similie_interior_product",
203 Kokkos::RangePolicy<execution_space>(exec, 0, domain.size()),
204 KOKKOS_LAMBDA(std::size_t node) {
205 auto const element = detail::stencil_element(storage.domain, node);
206 std::array<double, First::mem_size()> av {};
207 std::array<double, Second::mem_size()> bv {};
208 std::array<double, Target::mem_size()> ov {};
210 ddc::ChunkSpan<
211 double,
212 ddc::DiscreteDomain<First>,
213 Kokkos::layout_right,
214 Memory>(
215 av.data(),
218 second(ddc::ChunkSpan<
219 double,
220 ddc::DiscreteDomain<Second>,
221 Kokkos::layout_right,
222 Memory>(
223 bv.data(),
226 transformed(
227 ddc::ChunkSpan<
228 double,
229 ddc::DiscreteDomain<Target>,
230 Kokkos::layout_right,
231 Memory>(
232 ov.data(),
234 for (std::size_t target = 0; target < Target::mem_size(); ++target) {
235 double value = 0;
236 for (std::size_t corner = 0;
237 corner < decltype(storage.projection)::corners;
238 ++corner) {
239 auto const sample = decltype(storage.projection)::
240 sample(element, target, corner, 1);
241 // Apply the same periodic observation rule as the field adapter.
242 auto wrapped = sample;
243 for (std::size_t axis = 0; axis < Domain::rank(); ++axis) {
244 std::ptrdiff_t const front
245 = ddc::detail::array(storage.domain.front())[axis];
246 std::ptrdiff_t const count
247 = ddc::detail::array(storage.domain.extents())[axis];
248 std::ptrdiff_t const relative
249 = ddc::detail::array(sample)[axis] - front;
250 ddc::detail::array(wrapped)[axis]
251 = front + (relative % count + count) % count;
252 }
253 storage.first_factors
254 .reconstruct(first, a, storage.domain, wrapped);
255 storage.second_factors
256 .reconstruct(second, b, storage.domain, wrapped);
257 Operator {}(
258 Form(transformed),
259 Form(first),
260 Form(second),
261 storage.inverse_metric,
262 wrapped);
263 for (std::size_t source = 0; source < Target::mem_size(); ++source)
264 value += storage.projection.weights(
265 node,
266 (target * Target::mem_size() + source)
267 * decltype(storage.projection)::
268 corners
269 + corner)
270 * transformed.mem(
271 ddc::DiscreteElement<Target>(source));
272 }
273 output.mem(element, ddc::DiscreteElement<Target>(target)) = value;
274 }
275 });
276 return output;
277 }
278 };
280};
281} // namespace sil::exterior
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
Cochain(Tensor const &) -> Cochain< Tensor >
Form(Coefficients const &) -> Form< Coefficients >
Periodic vertex reconstruction, bilinear form operator, and projection.
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
detail::VertexTransferFactors< First, Domain, Memory > first_factors
detail::VertexTransferFactors< Second, Domain, Memory > second_factors
Output apply(execution_space const &exec, Output const &output, A const &a, B const &b) const
detail::VertexTransferFactors< Target, Domain, Memory > projection
static Storage build(execution_space const &exec, Output const &output, A const &a, B const &b, Position const &position, InverseMetric const &inverse_metric)
static KOKKOS_FUNCTION void operator()(Output const &output, Input const &input, Position const &position, Element const &element, Extrapolation const &extrapolation={})
static KOKKOS_FUNCTION void operator()(Output const &output, Input const &input, Position const &position, Element const &element, Extrapolation const &extrapolation={})