include/pops/amr/tagging/tag_box.hpp Source FileΒΆ

adc_cpp: include/pops/amr/tagging/tag_box.hpp Source File
adc_cpp 0.3.0
Model-free C++23 core for coupled hyperbolic-elliptic systems on adaptive (AMR) meshes, with MPI and GPU (Kokkos) backends
tag_box.hpp
Go to the documentation of this file.
1
16
17#pragma once
18
20
21#include <algorithm>
22#include <cstddef>
23#include <cstdint>
24#include <stdexcept>
25#include <vector>
26
27namespace pops {
28
35struct TagBox {
37 std::vector<char> t{};
38
39 TagBox() = default;
41 explicit TagBox(const Box2D& b)
42 : box(b), t(static_cast<std::size_t>(std::max<std::int64_t>(0, b.num_cells())), 0) {}
43
45 char& operator()(int i, int j) { return t[idx(i, j)]; }
47 char operator()(int i, int j) const { return t[idx(i, j)]; }
49 bool tagged(int i, int j) const { return box.contains(i, j) && t[idx(i, j)] != 0; }
50
52 std::int64_t count() const {
53 std::int64_t c = 0;
54 for (char x : t)
55 c += x;
56 return c;
57 }
58
59 private:
60 // i-fast linear index of marker (i, j) in `t`; assumes (i, j) in box.
61 std::size_t idx(int i, int j) const {
62 return static_cast<std::size_t>(j - box.lo[1]) * box.nx() + (i - box.lo[0]);
63 }
64};
65
73// No physics dependency (a few lines): i-fast storage -> simple |= over the buffer.
74inline TagBox tag_union(const std::vector<TagBox>& parts) {
75 if (parts.empty())
76 return TagBox{};
77 TagBox out(parts[0].box);
78 for (const TagBox& tb : parts) {
79 if (tb.box.lo[0] != out.box.lo[0] || tb.box.lo[1] != out.box.lo[1] ||
80 tb.box.hi[0] != out.box.hi[0] || tb.box.hi[1] != out.box.hi[1])
81 throw std::runtime_error(
82 "tag_union: all TagBox must share EXACTLY the same box (same parent "
83 "domain) for cell-by-cell union");
84 const std::size_t n = std::min(out.t.size(), tb.t.size());
85 for (std::size_t k = 0; k < n; ++k)
86 out.t[k] |= tb.t[k];
87 }
88 return out;
89}
90
91} // namespace pops
Box2D: the integer index space of a 2D cell-centered Cartesian grid.
Definition amr_hierarchy.hpp:29
TagBox tag_union(const std::vector< TagBox > &parts)
Union (cell-by-cell logical OR) of several TagBox sharing EXACTLY the same box.
Definition tag_box.hpp:74
2D integer index space, cell-centered.
Definition box2d.hpp:37
POPS_HD int nx() const
Width (direction 0). POPS_HD (called from Geometry::dx() in a device kernel).
Definition box2d.hpp:50
int hi[2]
Definition box2d.hpp:39
bool contains(int i, int j) const
true if cell (i, j) is inside the box (lo/hi bounds inclusive).
Definition box2d.hpp:61
int lo[2]
Definition box2d.hpp:38
Dense grid of 0/1 markers over a box, input to Berger-Rigoutsos clustering.
Definition tag_box.hpp:35
Box2D box
Definition tag_box.hpp:36
TagBox(const Box2D &b)
Build a TagBox covering b, all markers at 0 (buffer sized on b.num_cells()).
Definition tag_box.hpp:41
std::int64_t count() const
Number of tagged cells (sum of markers).
Definition tag_box.hpp:52
std::vector< char > t
Definition tag_box.hpp:37
bool tagged(int i, int j) const
true if (i, j) is in box AND tagged; safe (bound check included, unlike operator()).
Definition tag_box.hpp:49
char & operator()(int i, int j)
Write access to marker (i, j); (i, j) MUST be in box (no bound check).
Definition tag_box.hpp:45
TagBox()=default
char operator()(int i, int j) const
Read access to marker (i, j); (i, j) MUST be in box (no bound check).
Definition tag_box.hpp:47