include/pops/amr/hierarchy/amr_hierarchy.hpp Source FileΒΆ

adc_cpp: include/pops/amr/hierarchy/amr_hierarchy.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
amr_hierarchy.hpp
Go to the documentation of this file.
1
15
16#pragma once
17
24
25#include <cassert>
26#include <utility>
27#include <vector>
28
29namespace pops {
30
39 public:
47 AmrHierarchy(const Box2D& coarse_domain, int max_grid_size, int ncomp, int ngrow,
49 : ref_ratio_(ref_ratio), ncomp_(ncomp), ngrow_(ngrow) {
51 BoxArray ba = BoxArray::from_domain(coarse_domain, max_grid_size);
52 domain_.push_back(coarse_domain);
53 ba_.push_back(ba);
54 data_.emplace_back(ba, DistributionMapping(ba.size(), n_ranks()), ncomp, ngrow);
55 }
56
60 void add_level(const BoxArray& fine_ba) {
61 const int lev = num_levels();
62 domain_.push_back(domain_[lev - 1].refine(ref_ratio_));
63 ba_.push_back(fine_ba);
64 data_.emplace_back(fine_ba, DistributionMapping(fine_ba.size(), n_ranks()), ncomp_, ngrow_);
65 }
66
72 void install_level(int lev, const BoxArray& fine_ba, MultiFab data) {
73 assert(lev >= 1 && lev <= num_levels());
74 const Box2D dom = domain_[lev - 1].refine(ref_ratio_);
75 if (lev == num_levels()) {
76 domain_.push_back(dom);
77 ba_.push_back(fine_ba);
78 data_.push_back(std::move(data));
79 } else {
80 domain_[lev] = dom;
81 ba_[lev] = fine_ba;
82 data_[lev] = std::move(data);
83 domain_.resize(lev + 1);
84 ba_.resize(lev + 1);
85 data_.resize(lev + 1);
86 }
87 }
88
90 void clear_above(int lev) {
91 if (lev + 1 < num_levels()) {
92 domain_.resize(lev + 1);
93 ba_.resize(lev + 1);
94 data_.resize(lev + 1);
95 }
96 }
97
99 int num_levels() const { return static_cast<int>(data_.size()); }
101 int ref_ratio() const { return ref_ratio_; }
103 int ncomp() const { return ncomp_; }
105 int n_grow() const { return ngrow_; }
106
108 const Box2D& domain(int lev) const { return domain_[lev]; }
110 const BoxArray& boxes(int lev) const { return ba_[lev]; }
112 MultiFab& data(int lev) { return data_[lev]; }
114 const MultiFab& data(int lev) const { return data_[lev]; }
115
116 private:
117 int ref_ratio_;
118 int ncomp_;
119 int ngrow_;
120 std::vector<Box2D> domain_{};
121 std::vector<BoxArray> ba_{};
122 std::vector<MultiFab> data_{};
123};
124
125} // namespace pops
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
const MultiFab & data(int lev) const
Field of level lev (const access).
Definition amr_hierarchy.hpp:114
MultiFab & data(int lev)
Field of level lev (mutable access).
Definition amr_hierarchy.hpp:112
AmrHierarchy(const Box2D &coarse_domain, int max_grid_size, int ncomp, int ngrow, int ref_ratio=kAmrRefRatio)
Builds the hierarchy with only its level 0 (coarse).
Definition amr_hierarchy.hpp:47
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
const BoxArray & boxes(int lev) const
BoxArray (split into boxes) of level lev.
Definition amr_hierarchy.hpp:110
void add_level(const BoxArray &fine_ba)
Adds a fine level defined by its BoxArray (in fine index space).
Definition amr_hierarchy.hpp:60
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
Ordered list of boxes tiling a level.
Definition box_array.hpp:22
int size() const
Number of boxes in the tiling.
Definition box_array.hpp:42
static BoxArray from_domain(const Box2D &domain, int max_grid_size)
Tile the domain into tiles of at most max_grid_size per direction, distributed evenly.
Definition box_array.hpp:30
Owning MPI rank of each box, indexed by GLOBAL box index (parallel to a BoxArray).
Definition distribution_mapping.hpp:19
Field distributed over a level: decomposition (BoxArray) + distribution (DistributionMapping) + ncomp...
Definition multifab.hpp:33
Parallel seam: minimal MPI abstraction (rank/size + collectives) with serial fallback.
DistributionMapping: maps each box (by global index) to its owning MPI rank.
MultiFab: a field DISTRIBUTED over a level (equivalent of AMReX's MultiFab).
Definition amr_hierarchy.hpp:29
void require_supported_ref_ratio(int ratio)
Validates a requested AMR refinement ratio at the hierarchy boundary.
Definition refinement_ratio.hpp:35
int n_ranks()
Definition comm.hpp:139
constexpr int kAmrRefRatio
The native AMR refinement ratio between two consecutive levels.
Definition refinement_ratio.hpp:30
Single source of truth for the native AMR refinement ratio.
2D integer index space, cell-centered.
Definition box2d.hpp:37