SimiLie
Loading...
Searching...
No Matches
stencil_composition.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/tensor/extrapolation_basis.hpp>
8
9namespace sil::multidomains::detail {
10// Expand native coefficient tensors into stored samples and independent sources.
11// Donor maps and affine jumps remain properties of the extrapolation policies.
12template <class Field, class Element, class Component, class Sample, class Constant>
13KOKKOS_FUNCTION void visit_tensor_basis(
14 Field const& field,
15 Element element,
16 Component component,
17 double weight,
18 Sample sample,
19 Constant constant);
20
21template <class Rule, class Field, class Element, class Component, class Sample, class Constant>
22KOKKOS_FUNCTION void visit_rule_basis(
23 Rule const& rule,
24 Field const& field,
25 Element element,
26 Component component,
27 double weight,
28 Sample sample,
29 Constant constant);
30
31template <class Rule, class Field, class Element, class Component, class Sample, class Constant>
32KOKKOS_FUNCTION void visit_rule_source(
33 Rule const& rule,
34 Field const& field,
35 Element element,
36 Component component,
37 double weight,
38 Sample sample,
39 Constant constant)
40{
41 if constexpr (requires {
42 rule.interface;
43 rule.external;
44 rule.boundary;
45 }) {
46 if (rule.interface)
47 visit_rule_source(rule.external, field, element, component, weight, sample, constant);
48 else
49 visit_rule_source(rule.boundary, field, element, component, weight, sample, constant);
50 } else if constexpr (requires {
51 rule.neighbor;
52 rule.boundary_map;
53 rule.target_rule;
54 }) {
55 visit_rule_basis(
56 rule.target_rule,
57 rule.neighbor,
58 rule.boundary_map(
59 field.non_indices_domain(),
60 rule.neighbor.non_indices_domain(),
61 element),
62 component,
63 weight * rule.orientation,
64 sample,
65 constant);
66 constant(weight * rule.jump);
67 } else
68 constant(
69 weight
70 * sil::tensor::detail::extrapolation_source_value(rule, field, element, component));
71}
72
73template <
74 class Rule,
75 class Field,
76 class Element,
77 class Component,
78 class Sample,
79 class Constant,
80 std::size_t... Axis>
81KOKKOS_FUNCTION void visit_rule_faces(
82 Rule const& rule,
83 Field const& field,
84 Element element,
85 Component component,
86 std::size_t source,
87 double coefficient,
88 Sample sample,
89 Constant constant,
90 std::index_sequence<Axis...>)
91{
92 ((source == 1 + 2 * Axis ? visit_rule_source(
93 rule.template boundary_rule<Axis, false>(),
94 field,
95 element,
96 component,
97 coefficient,
98 sample,
99 constant)
100 : void()),
101 ...);
102 ((source == 2 + 2 * Axis ? visit_rule_source(
103 rule.template boundary_rule<Axis, true>(),
104 field,
105 element,
106 component,
107 coefficient,
108 sample,
109 constant)
110 : void()),
111 ...);
112}
113
114template <class Rule, class Field, class Element, class Component, class Sample, class Constant>
115KOKKOS_FUNCTION void visit_rule_basis(
116 Rule const& rule,
117 Field const& field,
118 Element element,
119 Component component,
120 double weight,
121 Sample sample,
122 Constant constant)
123{
124 if constexpr (requires {
125 rule.interface;
126 rule.external;
127 rule.boundary;
128 }) {
129 if (rule.interface)
130 visit_rule_basis(rule.external, field, element, component, weight, sample, constant);
131 else
132 visit_rule_basis(rule.boundary, field, element, component, weight, sample, constant);
133 } else {
134 auto const coefficients = rule.value(field.non_indices_domain(), element);
135 ddc::device_for_each(coefficients.domain(), [&](auto entry) {
136 double const coefficient = weight * coefficients.mem(entry);
137 if (coefficient == 0.0)
138 return;
139 std::size_t const source
140 = entry.template uid<sil::tensor::detail::ExtrapolationSource>();
141 if (source == 0)
142 visit_tensor_basis(
143 field,
144 typename Field::non_indices_domain_t::discrete_element_type(entry),
145 component,
146 coefficient,
147 sample,
148 constant);
149 else if constexpr (requires { rule.template boundary_rule<0, false>(); }) {
150 visit_rule_faces(
151 rule,
152 field,
153 element,
154 component,
155 source,
156 coefficient,
157 sample,
158 constant,
159 std::make_index_sequence<ddc::type_seq_size_v<
160 ddc::to_type_seq_t<typename Field::non_indices_domain_t>>> {});
161 } else
162 visit_rule_source(rule, field, element, component, coefficient, sample, constant);
163 });
164 }
165}
166
167template <class Field, class Element, class Component, class Sample, class Constant>
168KOKKOS_FUNCTION void visit_tensor_basis(
169 Field const& field,
170 Element element,
171 Component component,
172 double weight,
173 Sample sample,
174 Constant constant)
175{
176 if constexpr (requires { *field.field; })
177 visit_tensor_basis(*field.field, element, component, weight, sample, constant);
178 else if constexpr (requires {
179 field.rules;
180 field.field;
181 field.fixed;
182 }) {
185 ddc::to_type_seq_t<typename std::remove_cvref_t<decltype(field.field)>::
186 non_indices_domain_t>> const point(field.fixed, element);
187 visit_rule_basis(field.rules, field.field, point, component, weight, sample, constant);
188 } else if constexpr (requires {
189 field.cochain;
190 field.prescribed_flux(element, component, std::declval<double&>());
191 }) {
192 double prescribed = 0.0;
193 if (field.prescribed_flux(element, component, prescribed)) {
194 constant(weight * prescribed);
195 return;
196 }
197 auto const coefficients = field.value(element, component);
198 ddc::device_for_each(coefficients.domain(), [&](auto entry) {
199 double const coefficient = weight * coefficients.mem(entry);
200 if (coefficient != 0.0)
201 visit_tensor_basis(
202 field.cochain,
203 sil::tensor::detail::stencil_sample(
204 sil::tensor::detail::storage_element(element),
205 entry),
206 typename std::remove_cvref_t<decltype(coefficients)>::indices_domain_t::
207 discrete_element_type(entry),
208 coefficient,
209 sample,
210 constant);
211 });
212 } else
213 sample(field, sil::tensor::detail::storage_element(element), component, weight);
214}
215} // namespace sil::multidomains::detail
Absolute logical coordinates with signed storage, including exterior nodes.
typename detail::ConvertTypeSeqTo< T, Seq >::type convert_type_seq_to_t