ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_GroundAverageDown.H
Go to the documentation of this file.
1 #ifndef ERF_GROUND_AVERAGE_DOWN_H_
2 #define ERF_GROUND_AVERAGE_DOWN_H_
3 
4 #include <AMReX_MultiFab.H>
5 
6 /**
7  * Restriction of a quantity held in the ground layer of cells (a surface accumulation) from a
8  * fine level to the coarse level below it. Each coarse ground cell under a fine box that stands
9  * on the ground gets a scale times the sum of the rr[0]*rr[1] fine ground cells above it: the
10  * inverse area for a depth, 1 for an amount per cell. The coarse ground boxes, distributed with
11  * the fine boxes they lie under, are built once and serve every quantity restricted with them.
12  */
14 {
15 public:
16  /**
17  * @param a_fba, a_fdm BoxArray and DistributionMapping of the fine data
18  * @param a_rr refinement ratio between the two levels
19  * @param a_kf, a_kc index of the ground layer on the fine and on the coarse level
20  */
21  GroundAverageDown (const amrex::BoxArray& a_fba, const amrex::DistributionMapping& a_fdm,
22  const amrex::IntVect& a_rr, int a_kf, int a_kc)
23  : m_fba(a_fba), m_fdm(a_fdm), m_rr(a_rr), m_kf(a_kf)
24  {
25  amrex::BoxList bl;
26  amrex::Vector<int> pmap;
27  for (int ib = 0; ib < a_fba.size(); ++ib) {
28  if (a_fba[ib].smallEnd(2) != a_kf) { continue; }
29  amrex::Box s = amrex::coarsen(a_fba[ib], a_rr);
30  s.setRange(2, a_kc);
31  bl.push_back(s);
32  m_fine_box.push_back(ib);
33  pmap.push_back(a_fdm[ib]);
34  }
35  if (!bl.isEmpty()) {
36  m_tmp.define(amrex::BoxArray(std::move(bl)), amrex::DistributionMapping(std::move(pmap)), 1, 0);
37  }
38  }
39 
40  /**
41  * Component a_ccomp of a_crse = a_scale times the sum of the fine ground cells of component
42  * a_fcomp of a_fine, in the coarse ground cells under the fine level. a_fine must be defined
43  * on the fine BoxArray and DistributionMapping given to the constructor.
44  */
45  void apply (const amrex::MultiFab& a_fine, amrex::MultiFab& a_crse, const amrex::Real a_scale,
46  const int a_fcomp = 0, const int a_ccomp = 0)
47  {
48  AMREX_ASSERT(a_fine.boxArray() == m_fba && a_fine.DistributionMap() == m_fdm);
49  if (m_fine_box.empty()) { return; }
50  const amrex::IntVect rr = m_rr;
51  const int kf = m_kf;
52  for (amrex::MFIter mfi(m_tmp); mfi.isValid(); ++mfi) {
53  const auto f = a_fine.const_array(m_fine_box[mfi.index()], a_fcomp);
54  const auto c = m_tmp.array(mfi);
55  amrex::ParallelFor(mfi.validbox(), [=] AMREX_GPU_DEVICE (int ic, int jc, int kc) noexcept
56  {
57  amrex::Real sum = 0.0;
58  for (int j = jc*rr[1]; j < (jc+1)*rr[1]; ++j) {
59  for (int i = ic*rr[0]; i < (ic+1)*rr[0]; ++i) {
60  sum += f(i,j,kf);
61  }
62  }
63  c(ic,jc,kc) = sum * a_scale;
64  });
65  }
66  a_crse.ParallelCopy(m_tmp, 0, a_ccomp, 1);
67  }
68 
69 private:
70  amrex::BoxArray m_fba;
71  amrex::DistributionMapping m_fdm;
72  amrex::IntVect m_rr;
73  int m_kf;
74  amrex::Vector<int> m_fine_box;
75  amrex::MultiFab m_tmp;
76 };
77 
78 /**
79  * The reverse of GroundAverageDown: each ground cell of a_fine (component a_fcomp) gets a_scale
80  * times the coarse ground cell of a_crse (component a_ccomp) it lies over -- 1/(rr[0]*rr[1]) for
81  * an amount per cell, which keeps the amount under each coarse cell. a_crse must cover the fine
82  * ground boxes, coarsened.
83  */
84 inline void
85 groundFillFromCoarse (amrex::MultiFab& a_fine, const amrex::MultiFab& a_crse, const amrex::IntVect& a_rr,
86  const int a_kf, const int a_kc, const amrex::Real a_scale,
87  const int a_fcomp = 0, const int a_ccomp = 0)
88 {
89  const amrex::BoxArray& fba = a_fine.boxArray();
90  amrex::BoxList bl;
91  amrex::Vector<int> fine_box, pmap;
92  for (int ib = 0; ib < fba.size(); ++ib) {
93  if (fba[ib].smallEnd(2) != a_kf) { continue; }
94  amrex::Box s = amrex::coarsen(fba[ib], a_rr);
95  s.setRange(2, a_kc);
96  bl.push_back(s);
97  fine_box.push_back(ib);
98  pmap.push_back(a_fine.DistributionMap()[ib]);
99  }
100  if (bl.isEmpty()) { return; }
101  amrex::MultiFab tmp(amrex::BoxArray(std::move(bl)), amrex::DistributionMapping(std::move(pmap)), 1, 0);
102  tmp.setVal(0.0);
103  tmp.ParallelCopy(a_crse, a_ccomp, 0, 1);
104  const amrex::IntVect rr = a_rr;
105  for (amrex::MFIter mfi(tmp); mfi.isValid(); ++mfi) {
106  const int ib = fine_box[mfi.index()];
107  const auto c = tmp.const_array(mfi);
108  const auto f = a_fine.array(ib, a_fcomp);
109  amrex::Box gbx = fba[ib];
110  gbx.setRange(2, a_kf);
111  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
112  {
113  // floor division: the coarse cell over fine cell (i, j)
114  const int ic = (i >= 0) ? i / rr[0] : -((-i - 1) / rr[0]) - 1;
115  const int jc = (j >= 0) ? j / rr[1] : -((-j - 1) / rr[1]) - 1;
116  f(i,j,k) = a_scale * c(ic,jc,a_kc);
117  });
118  }
119 }
120 
121 #endif
void groundFillFromCoarse(amrex::MultiFab &a_fine, const amrex::MultiFab &a_crse, const amrex::IntVect &a_rr, const int a_kf, const int a_kc, const amrex::Real a_scale, const int a_fcomp=0, const int a_ccomp=0)
Definition: ERF_GroundAverageDown.H:85
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
Definition: ERF_GroundAverageDown.H:14
amrex::IntVect m_rr
Definition: ERF_GroundAverageDown.H:72
void apply(const amrex::MultiFab &a_fine, amrex::MultiFab &a_crse, const amrex::Real a_scale, const int a_fcomp=0, const int a_ccomp=0)
Definition: ERF_GroundAverageDown.H:45
GroundAverageDown(const amrex::BoxArray &a_fba, const amrex::DistributionMapping &a_fdm, const amrex::IntVect &a_rr, int a_kf, int a_kc)
Definition: ERF_GroundAverageDown.H:21
amrex::BoxArray m_fba
Definition: ERF_GroundAverageDown.H:70
amrex::Vector< int > m_fine_box
Definition: ERF_GroundAverageDown.H:74
amrex::MultiFab m_tmp
Definition: ERF_GroundAverageDown.H:75
int m_kf
Definition: ERF_GroundAverageDown.H:73
amrex::DistributionMapping m_fdm
Definition: ERF_GroundAverageDown.H:71
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
real(c_double), private rr
Definition: ERF_module_mp_morr_two_moment.F90:224