include/pops/amr/regridding/regrid.hpp Source FileΒΆ

adc_cpp: include/pops/amr/regridding/regrid.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
regrid.hpp
Go to the documentation of this file.
1
15
16#pragma once
17
28
29#include <utility>
30#include <vector>
31
32namespace pops {
33
39template <class Crit>
40TagBox tag_cells(const MultiFab& mf, const Box2D& domain, Crit crit) {
41 TagBox tb(domain);
42 for (int li = 0; li < mf.local_size(); ++li) {
43 const Fab2D& f = mf.fab(li);
44 const ConstArray4 a = f.const_array();
45 const Box2D v = f.box();
46 for (int j = v.lo[1]; j <= v.hi[1]; ++j)
47 for (int i = v.lo[0]; i <= v.hi[0]; ++i)
48 if (crit(a, i, j))
49 tb(i, j) = 1;
50 }
51 return tb;
52}
53
58inline TagBox grow_tags(const TagBox& in, int n, const Box2D& domain) {
59 TagBox out(in.box);
60 const Box2D& b = in.box;
61 for (int j = b.lo[1]; j <= b.hi[1]; ++j)
62 for (int i = b.lo[0]; i <= b.hi[0]; ++i)
63 if (in(i, j))
64 for (int dj = -n; dj <= n; ++dj)
65 for (int di = -n; di <= n; ++di) {
66 const int ii = i + di, jj = j + dj;
67 if (b.contains(ii, jj) && domain.contains(ii, jj))
68 out(ii, jj) = 1;
69 }
70 return out;
71}
72
78
86template <class Crit>
87void regrid_level(AmrHierarchy& h, int coarse_lev, Crit crit, const RegridParams& rp = {}) {
88 const Box2D cdom = h.domain(coarse_lev);
89 TagBox tags = tag_cells(h.data(coarse_lev), cdom, crit);
90 TagBox grown = grow_tags(tags, rp.n_buffer, cdom);
91
92 if (grown.count() == 0) { // nothing left to refine
93 h.clear_above(coarse_lev);
94 return;
95 }
96
97 // coarse boxes -> fine boxes (index space of level coarse_lev+1)
98 std::vector<Box2D> cboxes = berger_rigoutsos(grown, rp.cluster);
99 std::vector<Box2D> fboxes;
100 fboxes.reserve(cboxes.size());
101 for (const auto& b : cboxes)
102 fboxes.push_back(b.refine(h.ref_ratio()));
103 BoxArray fba(std::move(fboxes));
104
105 MultiFab newfine(fba, DistributionMapping(fba.size(), n_ranks()), h.ncomp(), h.n_grow());
106 interpolate(h.data(coarse_lev), newfine, h.ref_ratio());
107 if (h.num_levels() > coarse_lev + 1)
108 parallel_copy(newfine, h.data(coarse_lev + 1)); // preserve the old fine level
109
110 h.install_level(coarse_lev + 1, fba, std::move(newfine));
111}
112
113} // namespace pops
AmrHierarchy: the stack of refinement levels (container of the AMR hierarchy).
Box2D: the integer index space of a 2D cell-centered Cartesian grid.
BoxArray: the set of boxes tiling a level (disjoint, covering).
Stack of refined levels (domain + BoxArray + MultiFab per level), level 0 the coarsest.
Definition amr_hierarchy.hpp:38
void clear_above(int lev)
Removes all levels strictly finer than lev (no-op if lev is already the finest).
Definition amr_hierarchy.hpp:90
MultiFab & data(int lev)
Field of level lev (mutable access).
Definition amr_hierarchy.hpp:112
int ncomp() const
Number of field components per cell.
Definition amr_hierarchy.hpp:103
int n_grow() const
Number of ghost layers of the MultiFab.
Definition amr_hierarchy.hpp:105
int ref_ratio() const
Integer refinement ratio between consecutive levels.
Definition amr_hierarchy.hpp:101
const Box2D & domain(int lev) const
Domain of level lev in index space (INCLUSIVE lo/hi corners).
Definition amr_hierarchy.hpp:108
int num_levels() const
Number of levels present (>= 1: level 0 always exists).
Definition amr_hierarchy.hpp:99
void install_level(int lev, const BoxArray &fine_ba, MultiFab data)
Installs (adds or replaces) a fine level at index lev.
Definition amr_hierarchy.hpp:72
Single-grid data on a Box2D: VALID box + ng ghost layers, ncomp components, component-slow layout.
Definition fab2d.hpp:59
const Box2D & box() const
VALID box (without ghosts).
Definition fab2d.hpp:74
ConstArray4 const_array() const
READ handle (POD device-copyable) over this Fab. Valid as long as the Fab lives.
Definition fab2d.hpp:96
Field distributed over a level: decomposition (BoxArray) + distribution (DistributionMapping) + ncomp...
Definition multifab.hpp:33
Fab2D & fab(int li)
Local fab at index li (0 <= li < local_size()), for writing.
Definition multifab.hpp:67
int local_size() const
Number of fabs OWNED by this rank (bound on local indices).
Definition multifab.hpp:65
Berger-Rigoutsos clustering: tag grid -> small set of boxes covering the tagged cells.
Parallel seam: minimal MPI abstraction (rank/size + collectives) with serial fallback.
DistributionMapping: maps each box (by global index) to its owning MPI rank.
Fab2D: single-grid data on a Box2D (in-house equivalent of AMReX's FArrayBox); Array4 / ConstArray4: ...
MultiFab: a field DISTRIBUTED over a level (equivalent of AMReX's MultiFab).
Definition amr_hierarchy.hpp:29
void regrid_level(AmrHierarchy &h, int coarse_lev, Crit crit, const RegridParams &rp={})
(Re)builds level coarse_lev+1 from the tagging of level coarse_lev.
Definition regrid.hpp:87
int n_ranks()
Definition comm.hpp:139
void interpolate(const MultiFab &coarse, MultiFab &fine, int r, MultiFab &cfine)
Interpolation coarse -> fine (ratio r) by piecewise-CONSTANT injection: each fine cell (including the...
Definition refinement.hpp:220
void parallel_copy(MultiFab &dst, const MultiFab &src)
Copies the valid regions that OVERLAP from src to dst (same indices, no shift).
Definition refinement.hpp:50
std::vector< Box2D > berger_rigoutsos(const TagBox &tags, const ClusterParams &p={})
Cluster a TagBox into boxes covering the tagged cells (Berger-Rigoutsos), then final chop.
Definition cluster.hpp:185
TagBox grow_tags(const TagBox &in, int n, const Box2D &domain)
Grows the tags by n cells (square neighborhood), staying within the domain.
Definition regrid.hpp:58
TagBox tag_cells(const MultiFab &mf, const Box2D &domain, Crit crit)
Marks the valid cells where the predicate is true, on a TagBox covering the domain.
Definition regrid.hpp:40
AMR inter-level transfer operators (integer ratio r) + parallel_copy.
2D integer index space, cell-centered.
Definition box2d.hpp:37
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
Berger-Rigoutsos clustering parameters (configuration object).
Definition cluster.hpp:31
READ-only handle (const counterpart of Array4): same layout and same contract (POD device-copyable,...
Definition fab2d.hpp:44
Regrid parameters (configuration object).
Definition regrid.hpp:74
ClusterParams cluster
Berger-Rigoutsos clustering parameters.
Definition regrid.hpp:76
int n_buffer
tag dilation radius (grow_tags) before clustering.
Definition regrid.hpp:75
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: dense grid of markers (0/1) over a region, input to Berger-Rigoutsos clustering.