ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SBMInventory.H
Go to the documentation of this file.
1 #ifndef ERF_SBM_INVENTORY_H_
2 #define ERF_SBM_INVENTORY_H_
3 
4 #include <AMReX.H>
5 #include <AMReX_IntVect.H>
6 #include <AMReX_ParallelDescriptor.H>
7 #include <AMReX_REAL.H>
8 
9 #include <cstdint>
10 #include <limits>
11 
12 namespace erf_sbm {
13 
15 {
16  std::uint64_t leaf_cell_terms{0};
17  std::uint64_t operation_count{0};
19  amrex::Real rounding_factor{0};
20 };
21 
22 /**
23  * Operation-count bound on the difference of two composite leaf inventories,
24  * shared by the per-step, regrid and boundary-budget conservation checks.
25  * Each positive leaf term uses up to three multiplies and one local
26  * accumulation; bound both inventories, the final subtraction, and a pairwise
27  * MPI reduction with eight operations per leaf plus two reduction-tree levels
28  * per depth. A regrid adds the prolongation of a new fine cell from its
29  * parent, every leaf update between the two inventories re-forms each leaf,
30  * and each physical-face term of the boundary ledger takes a product, a mask
31  * and two sums.
32  */
33 inline InventoryRoundoff
34 composite_inventory_roundoff (const std::uint64_t coarse_cells,
35  const std::uint64_t fine_cells,
36  const amrex::IntVect& ratio,
37  const bool with_prolongation,
38  const std::uint64_t leaf_updates = 0,
39  const std::uint64_t boundary_face_terms = 0)
40 {
41  std::uint64_t refinement_volume = 1U;
42  for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
43  refinement_volume *= static_cast<std::uint64_t>(ratio[dir]);
44  }
45  AMREX_ALWAYS_ASSERT(refinement_volume > 0U &&
46  fine_cells % refinement_volume == 0U &&
47  coarse_cells >= fine_cells / refinement_volume);
48  InventoryRoundoff roundoff;
49  roundoff.leaf_cell_terms = coarse_cells - fine_cells / refinement_volume + fine_cells;
50  for (int ranks = amrex::ParallelDescriptor::NProcs(); ranks > 1;
51  ranks = (ranks + 1) / 2) {
52  ++roundoff.reduction_depth;
53  }
54  roundoff.operation_count =
55  8U * roundoff.leaf_cell_terms * (1U + leaf_updates) + 6U * boundary_face_terms +
56  2U * static_cast<std::uint64_t>(roundoff.reduction_depth) + 1U +
57  (with_prolongation ? refinement_volume + 6U : 0U);
58  const amrex::Real q = static_cast<amrex::Real>(roundoff.operation_count) *
60  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(q < amrex::Real(1.0),
61  "SBM composite inventory check: operation count times machine epsilon "
62  "reaches one, so this grid cannot be checked at this precision");
63  roundoff.rounding_factor = q / (amrex::Real(1.0) - q);
64  return roundoff;
65 }
66 
67 } // namespace erf_sbm
68 
69 #endif
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
@ U
Definition: ERF_IndexDefines.H:178
@ q
Definition: ERF_WSM6.H:296
Definition: ERF.H:128
InventoryRoundoff composite_inventory_roundoff(const std::uint64_t coarse_cells, const std::uint64_t fine_cells, const amrex::IntVect &ratio, const bool with_prolongation, const std::uint64_t leaf_updates=0, const std::uint64_t boundary_face_terms=0)
Definition: ERF_SBMInventory.H:34
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
Definition: ERF_SBMInventory.H:15
std::uint64_t leaf_cell_terms
Definition: ERF_SBMInventory.H:16
amrex::Real rounding_factor
Definition: ERF_SBMInventory.H:19
int reduction_depth
Definition: ERF_SBMInventory.H:18
std::uint64_t operation_count
Definition: ERF_SBMInventory.H:17