include/pops/coupling/amr/amr_diagnostics.hpp Source FileΒΆ

adc_cpp: include/pops/coupling/amr/amr_diagnostics.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_diagnostics.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
16#include <pops/mesh/execution/for_each.hpp> // device_fence
18
19#include <algorithm>
20#include <cmath>
21
22namespace pops {
23
24// --- MULTI-BOX form (canonical): sum/max over the valid cells of ALL local fabs,
25// WITHOUT MPI reduction (the coupler decides whether to all_reduce according to its
26// ownership policy). This is the single implementation; the mono-box variants below reduce
27// to it (a single fab whose box equals the domain -> bit for bit identical). This removes
28// the duplication between AmrCoupler (mono-box) and AmrCouplerMP (multi-box / distributed).
29
30// local sum of u(.,.,0) * dV over the valid cells. dV multiplied INSIDE the kernel.
33inline Real amr_mass_mb(const MultiFab& coarse, Real dx, Real dy) {
34 const Real dV = dx * dy;
35 Real M = 0;
36 for (int li = 0; li < coarse.local_size(); ++li) {
37 const ConstArray4 u = coarse.fab(li).const_array();
38 M += for_each_cell_reduce_sum(coarse.box(li),
39 [u, dV] POPS_HD(int i, int j) { return u(i, j, 0) * dV; });
40 }
41 return M;
42}
43
44// local max of |grad phi| / B0 (aux comp 1,2 = grad phi). Host loop (std::hypot not
45// confirmed device: see the header). WITHOUT floor (applied by the caller).
48inline Real amr_max_drift_speed_mb(const MultiFab& aux0, Real B0) {
50 Real v = 0;
51 for (int li = 0; li < aux0.local_size(); ++li) {
52 const ConstArray4 a = aux0.fab(li).const_array();
53 const Box2D b = aux0.box(li);
54 for (int j = b.lo[1]; j <= b.hi[1]; ++j)
55 for (int i = b.lo[0]; i <= b.hi[0]; ++i)
56 v = std::max(v, std::hypot(a(i, j, 1), a(i, j, 2)) / B0);
57 }
58 return v;
59}
60
61// mass of component 0 on the coarse level (single box): degenerate case of
62// amr_mass_mb (one fab covering the domain), bit for bit identical. dom kept for the API.
64inline Real amr_mass(const MultiFab& coarse, const Box2D& dom, Real dx, Real dy) {
65 (void)dom;
66 return amr_mass_mb(coarse, dx, dy);
67}
68
69// max drift speed on the coarse level (single box) + floor 1e-12 (CFL guard).
71inline Real amr_max_drift_speed(const MultiFab& aux0, const Box2D& dom, Real B0) {
72 (void)dom;
73 return std::max(amr_max_drift_speed_mb(aux0, B0), Real(1e-12));
74}
75
76} // namespace pops
Box2D: the integer index space of a 2D cell-centered Cartesian grid.
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
const Box2D & box(int li) const
VALID box of local fab li.
Definition multifab.hpp:71
int local_size() const
Number of fabs OWNED by this rank (bound on local indices).
Definition multifab.hpp:65
for_each_cell and reductions: the parallelism SEAM over the cells of a Box2D; sync_host / sync_device...
MultiFab: a field DISTRIBUTED over a level (equivalent of AMReX's MultiFab).
Definition amr_hierarchy.hpp:29
Real for_each_cell_reduce_sum(const Box2D &b, F f)
SUM reduction of f(i, j) over box b.
Definition for_each.hpp:199
double Real
Definition types.hpp:30
Real amr_mass(const MultiFab &coarse, const Box2D &dom, Real dx, Real dy)
Mono-box mass: degenerate case of amr_mass_mb (bit for bit). dom is ignored (kept for the API).
Definition amr_diagnostics.hpp:64
void device_fence()
Device barrier: waits for in-flight kernels to finish before a HOST access to unified memory.
Definition kokkos_env.hpp:43
Real amr_max_drift_speed_mb(const MultiFab &aux0, Real B0)
LOCAL max drift speed: max of |grad phi| / B0 (aux comp 1, 2 = grad phi) over the valid cells,...
Definition amr_diagnostics.hpp:48
Real amr_mass_mb(const MultiFab &coarse, Real dx, Real dy)
LOCAL mass: sum of u(.,.,0) * dx * dy over the valid cells of ALL local fabs, WITHOUT MPI reduction (...
Definition amr_diagnostics.hpp:33
Real amr_max_drift_speed(const MultiFab &aux0, const Box2D &dom, Real B0)
Mono-box max drift speed + floor 1e-12 (CFL guard). dom ignored (kept for the API).
Definition amr_diagnostics.hpp:71
2D integer index space, cell-centered.
Definition box2d.hpp:37
int hi[2]
Definition box2d.hpp:39
int lo[2]
Definition box2d.hpp:38
READ-only handle (const counterpart of Array4): same layout and same contract (POD device-copyable,...
Definition fab2d.hpp:44
Base scalar types and the POPS_HD macro (host+device portability).
#define POPS_HD
Definition types.hpp:25