SimiLie
Loading...
Searching...
No Matches
stencil_indices.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#include <cstddef>
9
10#include <Kokkos_Macros.hpp>
11
12namespace sil::exterior {
13
14template <std::size_t Dimensions>
16{
17 std::size_t output_component;
18 std::array<int, Dimensions> spatial_offset;
19 std::size_t input_component;
20};
21
22namespace detail {
23
24// One incidence changes one coordinate. Two incidences have Manhattan radius two.
25// Both signs are retained to cover reflected physical boundary closures as well.
26template <class Index, std::size_t Radius>
27struct StencilShape
28{
29 static_assert(Radius <= 2);
30 static constexpr std::size_t size = Radius == 0 ? 1
31 : Radius == 1 ? 1 + 2 * Index::size()
32 : 1 + 2 * Index::size() * (Index::size() + 1);
33 KOKKOS_FUNCTION static constexpr std::array<int, Index::size()> offset(std::size_t point)
34 {
35 std::array<int, Index::size()> result {};
36 if (point == 0)
37 return result;
38 --point;
39 if (point < 2 * Index::size()) {
40 result[point / 2] = point % 2 == 0 ? -1 : 1;
41 return result;
42 }
43 point -= 2 * Index::size();
44 if constexpr (Radius == 2) {
45 if (point < 2 * Index::size()) {
46 result[point / 2] = point % 2 == 0 ? -2 : 2;
47 return result;
48 }
49 point -= 2 * Index::size();
50 for (std::size_t first = 0; first < Index::size(); ++first)
51 for (std::size_t second = first + 1; second < Index::size(); ++second) {
52 if (point < 4) {
53 result[first] = point % 2 == 0 ? -1 : 1;
54 result[second] = point / 2 == 0 ? -1 : 1;
55 return result;
56 }
57 point -= 4;
58 }
59 }
60 return result;
61 }
62};
63
64
65} // namespace detail
66
71template <class Index, class Source, class Target, std::size_t Radius>
72KOKKOS_FUNCTION constexpr StencilIndex<Index::size()> stencil_reindex(std::size_t flat)
73{
74 return {flat / (detail::StencilShape<Index, Radius>::size * Source::mem_size()),
75 detail::StencilShape<Index, Radius>::offset(
76 (flat / Source::mem_size()) % detail::StencilShape<Index, Radius>::size),
77 flat % Source::mem_size()};
78}
79
80template <class Index, class Source, class Target, std::size_t Radius>
81KOKKOS_FUNCTION constexpr std::size_t stencil_flatten(StencilIndex<Index::size()> index)
82{
83 for (std::size_t point = 0; point < detail::StencilShape<Index, Radius>::size; ++point)
84 if (detail::StencilShape<Index, Radius>::offset(point) == index.spatial_offset)
85 return (index.output_component * detail::StencilShape<Index, Radius>::size + point)
86 * Source::mem_size()
87 + index.input_component;
88 return Target::mem_size() * detail::StencilShape<Index, Radius>::size * Source::mem_size();
89}
90
91namespace detail {
92
93template <class Index, std::size_t Count>
94KOKKOS_FUNCTION constexpr std::size_t stencil_component_index(
95 std::array<std::size_t, Count> const& ids)
96{
97 if constexpr (Index::rank() == 0)
98 return 0;
99 else if constexpr (Index::rank() == 1)
100 return ids[0];
101 else
102 return Index::mem_id(ids);
103}
104
105} // namespace detail
106} // namespace sil::exterior
KOKKOS_FUNCTION constexpr StencilIndex< Index::size()> stencil_reindex(std::size_t flat)
Decode the standard ordering: output component, spatial point, input component.
KOKKOS_FUNCTION constexpr std::size_t stencil_flatten(StencilIndex< Index::size()> index)
std::array< int, Dimensions > spatial_offset