SimiLie
Loading...
Searching...
No Matches
external_domain_extrapolation_rule.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 <cassert>
8#include <cstddef>
9#include <type_traits>
10
11#include <ddc/ddc.hpp>
12
13#include "scalar_extrapolation_rules.hpp"
14
15namespace sil::tensor {
16
17enum class BoundarySide { Lower, Upper };
18
37template <class Dimension>
39{
42 bool shared_boundary = true;
43
44private:
45 template <class DDim, class SourceDomain, class TargetDomain, class Element>
46 KOKKOS_FUNCTION void map_coordinate(
47 SourceDomain source,
48 TargetDomain const& target,
49 Element sampled,
50 Element& mapped) const
51 {
52 if constexpr (std::is_same_v<DDim, Dimension>) {
53 ddc::DiscreteElement<Dimension> const source_boundary(
54 source_side == BoundarySide::Lower ? source.front() : source.back());
55 ddc::DiscreteElement<Dimension> const target_boundary(
56 target_side == BoundarySide::Lower ? target.front() : target.back());
57 std::ptrdiff_t const offset
58 = static_cast<std::ptrdiff_t>(sampled.template uid<Dimension>())
59 - static_cast<std::ptrdiff_t>(source_boundary.template uid<Dimension>());
60 // Signed differences also handle unsigned elements wrapping below zero.
61 std::ptrdiff_t const distance = source_side == BoundarySide::Lower ? -offset : offset;
62 assert(distance >= 0);
63 std::ptrdiff_t const inward = distance - (shared_boundary ? 0 : 1);
64 assert(inward >= 0);
65 mapped.template uid<Dimension>()
66 = static_cast<std::ptrdiff_t>(target_boundary.template uid<Dimension>())
67 + (target_side == BoundarySide::Lower ? inward : -inward);
68 } else {
69 assert(source.template extent<DDim>() == target.template extent<DDim>());
70 std::ptrdiff_t const offset
71 = static_cast<std::ptrdiff_t>(sampled.template uid<DDim>())
72 - static_cast<std::ptrdiff_t>(source.front().template uid<DDim>());
73 mapped.template uid<DDim>()
74 = static_cast<std::ptrdiff_t>(target.front().template uid<DDim>()) + offset;
75 }
76 }
77
78public:
79 template <class... DDim, class TargetDomain, class Element>
80 KOKKOS_FUNCTION Element operator()(
81 ddc::DiscreteDomain<DDim...> const& source,
82 TargetDomain const& target,
83 Element sampled) const
84 {
85 static_assert((std::is_same_v<Dimension, DDim> || ...));
86 static_assert(std::is_same_v<ddc::TypeSeq<DDim...>, ddc::to_type_seq_t<TargetDomain>>);
87 Element mapped = sampled;
88 (map_coordinate<DDim>(source, target, sampled, mapped), ...);
89 return mapped;
90 }
91
92 template <class SourceDomain, class TargetDomain, class Element>
93 KOKKOS_FUNCTION Element
94 map_boundary(SourceDomain source, TargetDomain const& target, Element sampled) const
95 {
96 assert(shared_boundary);
97 assert(sampled.template uid<Dimension>()
98 == (source_side == BoundarySide::Lower ? source.front() : source.back())
99 .template uid<Dimension>());
100 return (*this)(source, target, sampled);
101 }
102};
103
120template <class Dimension, class NeighborTensor, class TargetRule = ClampExtrapolationRule>
122{
123 static constexpr bool IS_INTERFACE = true;
124 NeighborTensor neighbor;
126 double jump = 0.0;
127 double orientation = 1.0;
128 TargetRule target_rule = {};
129
130 template <class Domain, class Element>
131 KOKKOS_FUNCTION auto value(Domain const& domain, Element element) const
132 {
133 auto result = detail::make_extrapolation_basis(domain, element);
134 result
135 .mem(detail::extrapolation_sample(domain, element),
136 ddc::DiscreteElement<detail::ExtrapolationSource>(
137 misc::domain_contains(domain, element) ? 0 : 1))
138 = 1.0;
139 return result;
140 }
141
142 template <class Field, class Element>
143 KOKKOS_FUNCTION auto operator()(Field const& field, Element element) const
144 {
145 if (misc::domain_contains(field.non_indices_domain(), element))
146 return detail::pointwise_tensor<Field>([&](auto component) {
147 return field.mem(detail::storage_element(element), component);
148 });
149 auto const donor = target_rule(
150 neighbor,
151 boundary_map(field.non_indices_domain(), neighbor.non_indices_domain(), element));
152 return detail::pointwise_tensor<Field>(
153 [&](auto component) { return orientation * donor.mem(component) + jump; });
154 }
155};
156
157static_assert(ExtrapolationRuleConcept<
159 detail::GridConcept,
160 detail::FieldConcept<ScalarIndex>>,
161 detail::FieldConcept<ScalarIndex>,
162 ddc::DiscreteElement<detail::GridConcept>>);
163
164template <class NeighborTensor, class Dimension>
166 NeighborTensor,
168 double = 0.0,
170
171template <class NeighborTensor, class Dimension, class TargetRule>
173 NeighborTensor,
175 double,
176 double,
178
179} // namespace sil::tensor
KOKKOS_FUNCTION bool domain_contains(ddc::DiscreteDomain< DDim... > const &dom, ddc::DiscreteElement< ODDim... > elem)
ExternalDomainExtrapolationRule(NeighborTensor, ExternalDomainBoundaryMap< Dimension >, double=0.0, double=1.0) -> ExternalDomainExtrapolationRule< Dimension, NeighborTensor >
Map samples through an aligned interface, in any number of dimensions.
KOKKOS_FUNCTION Element operator()(ddc::DiscreteDomain< DDim... > const &source, TargetDomain const &target, Element sampled) const
KOKKOS_FUNCTION Element map_boundary(SourceDomain source, TargetDomain const &target, Element sampled) const
Continue tensor values into a specified boundary of another tensor domain.
KOKKOS_FUNCTION auto operator()(Field const &field, Element element) const
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const