EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_DomainSetup.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_DOMAINSETUP_HPP
12#define EBGEOMETRY_DOMAINSETUP_HPP
13
14// Std includes
15#include <algorithm>
16#include <cstdint>
17#include <vector>
18
19// Our includes
20#include "EBGeometry_Macros.hpp"
21#include "EBGeometry_Math.hpp"
22#include "EBGeometry_Pool.hpp"
26
27namespace EBGeometry {
28
39{
40public:
51 uint32_t
52 addSubdomain(std::vector<uint32_t> a_regions)
53 {
54 EBGEOMETRY_REQUIRE(!a_regions.empty(), "DomainSetup: a subdomain needs at least one region");
55
56 std::sort(a_regions.begin(), a_regions.end());
57
58 a_regions.erase(std::unique(a_regions.begin(), a_regions.end()), a_regions.end());
59
60 for (const uint32_t r : a_regions) {
61 EBGEOMETRY_REQUIRE(r != Subdomains::None, "DomainSetup: no region number %u", unsigned(r));
63 "DomainSetup: region %u is already in subdomain %u",
64 unsigned(r),
65 unsigned(this->subdomainOf(r)));
66 }
67
68 const uint32_t subdomain = m_numSubdomains++;
69
70 for (const uint32_t r : a_regions) {
71 if (r >= m_subdomainOf.size()) {
72 m_subdomainOf.resize(r + 1U, Subdomains::None);
73 }
74
75 m_subdomainOf[r] = subdomain;
76 }
77
78 return subdomain;
79 }
80
85 [[nodiscard]] EBGEOMETRY_HOST
86 uint32_t
87 getNumSubdomains() const noexcept
88 {
89 return m_numSubdomains;
90 }
91
97 [[nodiscard]] EBGEOMETRY_HOST
98 uint32_t
99 subdomainOf(const uint32_t a_region) const noexcept
100 {
101 return (a_region < m_subdomainOf.size()) ? m_subdomainOf[a_region] : Subdomains::None;
102 }
103
109 [[nodiscard]] EBGEOMETRY_HOST
110 bool
111 computesIn(const uint32_t a_region) const noexcept
112 {
113 return this->subdomainOf(a_region) != Subdomains::None;
114 }
115
123 [[nodiscard]] EBGEOMETRY_HOST
124 bool
125 touches(const uint32_t a_first, const uint32_t a_second) const noexcept
126 {
127 return this->computesIn(a_first) || this->computesIn(a_second);
128 }
129
135 [[nodiscard]] EBGEOMETRY_HOST
136 std::vector<uint32_t>
137 getRegions(const uint32_t a_subdomain) const
138 {
139 std::vector<uint32_t> regions;
140
141 for (uint32_t r = 0; r < m_subdomainOf.size(); r++) {
142 if (m_subdomainOf[r] == a_subdomain) {
143 regions.push_back(r);
144 }
145 }
146
147 return regions;
148 }
149
154 [[nodiscard]] EBGEOMETRY_HOST
155 uint32_t
156 getRegionsNeeded() const noexcept
157 {
158 return static_cast<uint32_t>(m_subdomainOf.size());
159 }
160
167 template <class T>
168 [[nodiscard]] EBGEOMETRY_HOST
170 role(const SurfaceRef<T>& a_surface) const noexcept
171 {
172 const uint32_t minus =
173 (a_surface.m_ownerRegion != SurfaceRef<T>::None) ? this->subdomainOf(a_surface.m_ownerRegion) : Subdomains::None;
174 const uint32_t plus = (a_surface.m_neighborRegion != SurfaceRef<T>::None)
175 ? this->subdomainOf(a_surface.m_neighborRegion)
177
178 if (minus == Subdomains::None && plus == Subdomains::None) {
179 return SurfaceRole::None;
180 }
181 if (minus == Subdomains::None || plus == Subdomains::None) {
183 }
184
185 return (minus == plus) ? SurfaceRole::Internal : SurfaceRole::Interface;
186 }
187
193 uint32_t
195 {
196 return m_numSurfaceGroups++;
197 }
198
206 void
207 add(const uint32_t a_group, const SurfaceSelector& a_selector)
208 {
209 EBGEOMETRY_REQUIRE(a_group < m_numSurfaceGroups,
210 "DomainSetup: surface group %u of %u",
211 unsigned(a_group),
212 unsigned(m_numSurfaceGroups));
213
214 m_selectors.push_back(a_selector);
215 m_surfaceGroups.push_back(a_group);
216 }
217
224 void
225 removeSurfaceGroup(const uint32_t a_group)
226 {
227 EBGEOMETRY_REQUIRE(a_group < m_numSurfaceGroups,
228 "DomainSetup: surface group %u of %u",
229 unsigned(a_group),
230 unsigned(m_numSurfaceGroups));
231
232 std::size_t kept = 0;
233
234 for (std::size_t i = 0; i < m_selectors.size(); i++) {
235 if (m_surfaceGroups[i] != a_group) {
236 m_selectors[kept] = m_selectors[i];
237 m_surfaceGroups[kept] = (m_surfaceGroups[i] > a_group) ? m_surfaceGroups[i] - 1U : m_surfaceGroups[i];
238
239 kept++;
240 }
241 }
242
243 m_selectors.resize(kept);
244 m_surfaceGroups.resize(kept);
245
246 m_numSurfaceGroups--;
247 }
248
255 void
256 remove(const uint32_t a_group, const SurfaceSelector& a_selector)
257 {
258 std::size_t kept = 0;
259
260 for (std::size_t i = 0; i < m_selectors.size(); i++) {
261 if (m_surfaceGroups[i] != a_group || !same(m_selectors[i], a_selector)) {
262 m_selectors[kept] = m_selectors[i];
263 m_surfaceGroups[kept] = m_surfaceGroups[i];
264
265 kept++;
266 }
267 }
268
269 m_selectors.resize(kept);
270 m_surfaceGroups.resize(kept);
271 }
272
277 [[nodiscard]] EBGEOMETRY_HOST
278 uint32_t
279 getNumSurfaceGroups() const noexcept
280 {
281 return m_numSurfaceGroups;
282 }
283
288 [[nodiscard]] EBGEOMETRY_HOST
289 uint32_t
290 getNumSelectors() const noexcept
291 {
292 return static_cast<uint32_t>(m_selectors.size());
293 }
294
300 [[nodiscard]] EBGEOMETRY_HOST
301 const SurfaceSelector&
302 getSelector(const uint32_t a_selector) const
303 {
304 return m_selectors.at(a_selector);
305 }
306
312 [[nodiscard]] EBGEOMETRY_HOST
313 uint32_t
314 getSurfaceGroupOfSelector(const uint32_t a_selector) const
315 {
316 return m_surfaceGroups.at(a_selector);
317 }
318
325 template <class T>
326 [[nodiscard]] EBGEOMETRY_HOST
327 uint32_t
328 surfaceGroupOf(const SurfaceRef<T>& a_surface) const noexcept
329 {
330 for (int pass = 0; pass < 2; pass++) {
331 for (std::size_t i = 0; i < m_selectors.size(); i++) {
332 if (m_selectors[i].m_rest == (pass == 1) && m_selectors[i].matches(a_surface)) {
333 return m_surfaceGroups[i];
334 }
335 }
336 }
337
338 return SurfaceGroups::None;
339 }
340
348 [[nodiscard]] EBGEOMETRY_HOST
350 buildSubdomains(Pool& a_pool, const uint32_t a_numRegions) const
351 {
352 EBGEOMETRY_REQUIRE(this->getRegionsNeeded() <= a_numRegions,
353 "DomainSetup: a subdomain holds region %u, the tape has %u regions",
354 unsigned(this->getRegionsNeeded() - 1U),
355 unsigned(a_numRegions));
356
357 std::vector<uint32_t> table(a_numRegions, Subdomains::None);
358
359 std::copy(m_subdomainOf.begin(), m_subdomainOf.end(), table.begin());
360
361 return Subdomains(a_pool, table, m_numSubdomains);
362 }
363
369 [[nodiscard]] EBGEOMETRY_HOST
372 {
373 return SurfaceGroups(a_pool, m_selectors, m_surfaceGroups, m_numSurfaceGroups);
374 }
375
382 [[nodiscard]] EBGEOMETRY_HOST
383 bool
384 operator==(const DomainSetup& a_other) const noexcept
385 {
386 if (m_numSubdomains != a_other.m_numSubdomains || m_numSurfaceGroups != a_other.m_numSurfaceGroups ||
387 m_selectors.size() != a_other.m_selectors.size() || m_surfaceGroups != a_other.m_surfaceGroups) {
388 return false;
389 }
390
391 const std::size_t regions = Math::max(m_subdomainOf.size(), a_other.m_subdomainOf.size());
392
393 for (std::size_t r = 0; r < regions; r++) {
394 if (this->subdomainOf(static_cast<uint32_t>(r)) != a_other.subdomainOf(static_cast<uint32_t>(r))) {
395 return false;
396 }
397 }
398
399 for (std::size_t i = 0; i < m_selectors.size(); i++) {
400 if (!same(m_selectors[i], a_other.m_selectors[i])) {
401 return false;
402 }
403 }
404
405 return true;
406 }
407
413 [[nodiscard]] EBGEOMETRY_HOST
414 bool
415 operator!=(const DomainSetup& a_other) const noexcept
416 {
417 return !(*this == a_other);
418 }
419
420private:
427 [[nodiscard]] EBGEOMETRY_HOST
428 static bool
429 same(const SurfaceSelector& a_first, const SurfaceSelector& a_second) noexcept
430 {
431 return a_first.m_surface == a_second.m_surface && a_first.m_part == a_second.m_part &&
432 a_first.m_regionA == a_second.m_regionA && a_first.m_regionB == a_second.m_regionB &&
433 a_first.m_label == a_second.m_label && a_first.m_materialA == a_second.m_materialA &&
434 a_first.m_materialB == a_second.m_materialB && a_first.m_rest == a_second.m_rest &&
435 (a_first.m_surface == SurfaceSelector::Any || a_first.m_numSurfaces == a_second.m_numSurfaces);
436 }
437
441 std::vector<uint32_t> m_subdomainOf;
442
446 uint32_t m_numSubdomains = 0;
447
451 std::vector<SurfaceSelector> m_selectors;
452
456 std::vector<uint32_t> m_surfaceGroups;
457
461 uint32_t m_numSurfaceGroups = 0;
462};
463
464} // namespace EBGeometry
465
466#endif
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200
Scalar helpers and numeric limits that compile for both host and device.
Growable bump arena for the GPU memory foundation.
Subdomains: the subdomain of each region of a tape, read on the host or a device.
SurfaceSelector and SurfaceGroups: which surfaces a downstream code groups under one meaning.
SurfaceRef: which surface of a shape decides its value at a point, and the container protocol that na...
How a domain is made: its subdomains (the regions each computes in) and its surface groups (the selec...
Definition EBGeometry_DomainSetup.hpp:39
uint32_t getNumSelectors() const noexcept
How many selectors there are, over all surface groups.
Definition EBGeometry_DomainSetup.hpp:290
std::vector< uint32_t > getRegions(const uint32_t a_subdomain) const
The regions of a subdomain.
Definition EBGeometry_DomainSetup.hpp:137
void remove(const uint32_t a_group, const SurfaceSelector &a_selector)
Remove the selectors of a surface group that equal one (to undo a click).
Definition EBGeometry_DomainSetup.hpp:256
uint32_t getRegionsNeeded() const noexcept
One past the largest region in a subdomain: the fewest regions a tape must have for the setup.
Definition EBGeometry_DomainSetup.hpp:156
const SurfaceSelector & getSelector(const uint32_t a_selector) const
A selector.
Definition EBGeometry_DomainSetup.hpp:302
uint32_t getNumSubdomains() const noexcept
How many subdomains there are.
Definition EBGeometry_DomainSetup.hpp:87
bool touches(const uint32_t a_first, const uint32_t a_second) const noexcept
Whether a surface between two regions is one the domain must give a condition: one side or both in it...
Definition EBGeometry_DomainSetup.hpp:125
SurfaceGroups buildSurfaceGroups(Pool &a_pool) const
Build the surface groups.
Definition EBGeometry_DomainSetup.hpp:371
SurfaceRole role(const SurfaceRef< T > &a_surface) const noexcept
What a surface is to the subdomains (as Subdomains::role()).
Definition EBGeometry_DomainSetup.hpp:170
void removeSurfaceGroup(const uint32_t a_group)
Remove a surface group and its selectors; the surface groups after it move down by one.
Definition EBGeometry_DomainSetup.hpp:225
void add(const uint32_t a_group, const SurfaceSelector &a_selector)
Add a selector to a surface group.
Definition EBGeometry_DomainSetup.hpp:207
uint32_t surfaceGroupOf(const SurfaceRef< T > &a_surface) const noexcept
The surface group a surface point is in (as SurfaceGroups::surfaceGroupOf()).
Definition EBGeometry_DomainSetup.hpp:328
uint32_t getSurfaceGroupOfSelector(const uint32_t a_selector) const
The surface group of a selector.
Definition EBGeometry_DomainSetup.hpp:314
uint32_t getNumSurfaceGroups() const noexcept
How many surface groups there are.
Definition EBGeometry_DomainSetup.hpp:279
bool operator!=(const DomainSetup &a_other) const noexcept
Whether two setups make different domains.
Definition EBGeometry_DomainSetup.hpp:415
uint32_t addSurfaceGroup() noexcept
Add a surface group.
Definition EBGeometry_DomainSetup.hpp:194
Subdomains buildSubdomains(Pool &a_pool, const uint32_t a_numRegions) const
Build the subdomains for a tape of a number of regions.
Definition EBGeometry_DomainSetup.hpp:350
bool computesIn(const uint32_t a_region) const noexcept
Whether the domain computes in a region.
Definition EBGeometry_DomainSetup.hpp:111
uint32_t addSubdomain(std::vector< uint32_t > a_regions)
Add a subdomain.
Definition EBGeometry_DomainSetup.hpp:52
uint32_t subdomainOf(const uint32_t a_region) const noexcept
The subdomain a region is in.
Definition EBGeometry_DomainSetup.hpp:99
bool operator==(const DomainSetup &a_other) const noexcept
Whether two setups make the same domain: the same subdomains, and the same selectors in the same grou...
Definition EBGeometry_DomainSetup.hpp:384
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
The subdomains of a domain (see Domain), read on the host or a device: the subdomain of each region o...
Definition EBGeometry_Subdomains.hpp:51
static constexpr uint32_t None
No subdomain, or a region in no subdomain.
Definition EBGeometry_Subdomains.hpp:56
A domain's surface groups, read on the host or a device: each group a set of surfaces,...
Definition EBGeometry_SurfaceGroups.hpp:296
static constexpr uint32_t None
No surface group: a surface no selector matches.
Definition EBGeometry_SurfaceGroups.hpp:301
constexpr T max(const T &a_a, const T &a_b) noexcept
The larger of two values.
Definition EBGeometry_Math.hpp:61
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
SurfaceRole
What a surface is to a domain, at a point, from the regions on its two sides (see Subdomains::role())...
Definition EBGeometry_Subdomains.hpp:32
@ Interface
The two sides are in two different subdomains: one surface the solver couples across.
@ None
Neither side is in a subdomain: the solver does not cut it.
@ Internal
Both sides are in the same subdomain (two of its regions touching): the solver decides; usually not c...
@ Boundary
One side is in a subdomain: cut once, from that side; its surface group gives its meaning (a boundary...
The surface that decides a shape's value at a point, as closestSurface() reports it.
Definition EBGeometry_SurfaceRef.hpp:39
Conditions on a point of a surface, all of which must hold; an unset one (Any) matches anything.
Definition EBGeometry_SurfaceGroups.hpp:40
static constexpr uint32_t Any
An unset condition: matches anything.
Definition EBGeometry_SurfaceGroups.hpp:44