SimiLie
Loading...
Searching...
No Matches
topology.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 <cmath>
9#include <cstddef>
10#include <limits>
11#include <stdexcept>
12#include <vector>
13
14#include <similie/tensor/external_domain_extrapolation_rule.hpp>
15
16namespace sil::multidomains {
17
24{
25 enum class Kind { Clamp, Zero, Natural, Prescribed, Flux };
26 Kind m_kind = Kind::Clamp;
27 double m_value = 0.0;
28
29public:
30 BoundaryRule() = default;
35 : m_kind(Kind::Prescribed)
36 , m_value(rule.prescribed_value)
37 {
38 }
40 : m_kind(Kind::Flux)
41 , m_value(rule.exterior_value)
42 {
43 }
44
45 KOKKOS_FUNCTION bool is_dirichlet() const
46 {
47 return m_kind == Kind::Prescribed;
48 }
49
50 template <class Domain, class Element>
51 KOKKOS_FUNCTION auto value(Domain const& domain, Element elem) const
52 {
53 switch (m_kind) {
54 case Kind::Zero:
55 return tensor::ZeroExtrapolationRule {}.value(domain, elem);
56 case Kind::Natural:
57 return tensor::NaturalScalarExtrapolationRule {}.value(domain, elem);
58 case Kind::Flux:
59 return tensor::ConstantScalarExtrapolationRule {m_value}.value(domain, elem);
60 case Kind::Prescribed:
61 return tensor::PrescribedScalarExtrapolationRule {m_value}.value(domain, elem);
62 default:
63 return tensor::ClampExtrapolationRule {}.value(domain, elem);
64 }
65 }
66 KOKKOS_FUNCTION double source_value() const
67 {
68 return m_value;
69 }
70 template <class Field, class Element>
71 KOKKOS_FUNCTION auto operator()(Field const& field, Element elem) const
72 {
73 auto const basis = value(field.non_indices_domain(), elem);
74 return tensor::detail::pointwise_tensor<Field>([&](auto component) {
75 return tensor::detail::contract_extrapolation_basis(
76 basis,
77 [&](auto sample) { return field.mem(sample, component); },
78 [&](std::size_t) { return m_value; });
79 });
80 }
81};
82
89
90struct Face
91{
92 std::size_t domain;
93 std::size_t axis;
95 bool operator==(Face const&) const = default;
96};
97
99{
100 static constexpr std::size_t UNCONNECTED = std::numeric_limits<std::size_t>::max();
101 std::size_t domain = UNCONNECTED;
103 bool shared_boundary = true;
104 double orientation = 1.0;
105 double jump = 0.0;
106};
107
118template <std::size_t N>
120{
121 static_assert(N > 0);
122 std::vector<std::array<Connection, 2 * N>> m_faces;
123 std::vector<BoundaryDomain> m_boundaries;
124
125 Connection& slot(Face face)
126 {
127 if (face.domain >= m_faces.size() || face.axis >= N)
128 throw std::invalid_argument("invalid physical face");
129 return m_faces[face.domain][2 * face.axis + (face.side == tensor::BoundarySide::Upper)];
130 }
131
132public:
133 explicit Topology(std::size_t domains) : m_faces(domains)
134 {
135 if (domains == 0)
136 throw std::invalid_argument("a multidomain needs a physical domain");
137 }
138 std::size_t domain_count() const
139 {
140 return m_faces.size();
141 }
143 {
144 m_boundaries.push_back(boundary);
145 return domain_count() + m_boundaries.size() - 1;
146 }
147 BoundaryDomain const& boundary(std::size_t id) const
148 {
149 return m_boundaries.at(id - domain_count());
150 }
152 Face first,
153 Face second,
154 bool shared_boundary = true,
155 double orientation = 1.0,
156 double jump = 0.0)
157 {
158 Connection& a = slot(first);
159 Connection& b = slot(second);
160 if (first == second || first.axis != second.axis || !std::isfinite(orientation)
161 || orientation == 0.0 || !std::isfinite(jump) || !std::isfinite(1.0 / orientation)
162 || !std::isfinite(jump / orientation) || a.domain != Connection::UNCONNECTED
164 throw std::invalid_argument("invalid or duplicate interface connection");
165 a = {second.domain, second.side, shared_boundary, orientation, jump};
166 b = {first.domain, first.side, shared_boundary, 1.0 / orientation, -jump / orientation};
167 }
168 void connect_boundary(Face face, std::size_t id)
169 {
170 Connection& entry = slot(face);
171 if (id < domain_count() || id - domain_count() >= m_boundaries.size()
173 throw std::invalid_argument("invalid or duplicate boundary connection");
174 entry.domain = id;
175 }
176 Connection const& connection(Face face) const
177 {
178 if (face.axis >= N)
179 throw std::invalid_argument("invalid face axis");
180 return m_faces.at(face.domain)[2 * face.axis + (face.side == tensor::BoundarySide::Upper)];
181 }
182 void validate() const
183 {
184 std::vector<bool> used(m_boundaries.size(), false);
185 for (std::array<Connection, 2 * N> const& faces : m_faces)
186 for (Connection const& edge : faces) {
187 if (edge.domain == Connection::UNCONNECTED)
188 throw std::invalid_argument("every physical face needs a connection");
189 if (edge.domain >= domain_count())
190 used.at(edge.domain - domain_count()) = true;
191 }
192 for (bool active : used)
193 if (!active)
194 throw std::invalid_argument("unconnected boundary node");
195 }
196};
197} // namespace sil::multidomains
A device-copyable choice of the scalar boundary policies.
Definition topology.hpp:24
BoundaryRule(tensor::PrescribedScalarExtrapolationRule rule)
Definition topology.hpp:34
BoundaryRule(tensor::ClampExtrapolationRule)
Definition topology.hpp:31
KOKKOS_FUNCTION auto operator()(Field const &field, Element elem) const
Definition topology.hpp:71
KOKKOS_FUNCTION double source_value() const
Definition topology.hpp:66
BoundaryRule(tensor::NaturalScalarExtrapolationRule)
Definition topology.hpp:33
BoundaryRule(tensor::ConstantScalarExtrapolationRule rule)
Definition topology.hpp:39
BoundaryRule(tensor::ZeroExtrapolationRule)
Definition topology.hpp:32
KOKKOS_FUNCTION bool is_dirichlet() const
Definition topology.hpp:45
KOKKOS_FUNCTION auto value(Domain const &domain, Element elem) const
Definition topology.hpp:51
Runtime adjacency of aligned tensor domains of dimension N.
Definition topology.hpp:120
void connect_boundary(Face face, std::size_t id)
Definition topology.hpp:168
std::size_t add_boundary(BoundaryDomain boundary)
Definition topology.hpp:142
std::size_t domain_count() const
Definition topology.hpp:138
void connect(Face first, Face second, bool shared_boundary=true, double orientation=1.0, double jump=0.0)
Definition topology.hpp:151
Topology(std::size_t domains)
Definition topology.hpp:133
Connection const & connection(Face face) const
Definition topology.hpp:176
BoundaryDomain const & boundary(std::size_t id) const
Definition topology.hpp:147
tensor::BoundarySide side
Definition topology.hpp:102
static constexpr std::size_t UNCONNECTED
Definition topology.hpp:100
bool operator==(Face const &) const =default
tensor::BoundarySide side
Definition topology.hpp:94
Spatial closure evaluated independently of the stored field values.
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const
KOKKOS_FUNCTION auto value(Domain const &domain, Element element) const