ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InitDensityHSE.H
Go to the documentation of this file.
1 /**
2  * Initialize hydrostatically balanced density
3  *
4  * Calls init_isentropic_hse_terrain() or init_isentropic_hse() for cases with
5  * and without terrain, respectively. Hydrostatic equilibrium (HSE) is satisfied discretely.
6  * Note that these routines presume that qv==0 when evaluating the EOS. Both
7  * density and pressure in HSE are calculated but only the density is used at
8  * this point.
9 */
10 
11 #include "ERF_HSEUtils.H"
12 
13 /**
14  * @brief Hydrostatic columns of a terrain-fitted mesh, integrated upward from the bottom of each box.
15  *
16  * One column array per (i,j) thread, of NZMAX cells: 256 (the arrays ERF always had) for a domain of
17  * up to 256 cells in z, otherwise 512.
18  *
19  * @param[out] rho_hse MultiFab to be filled with HSE density.
20  * @param[in] z_phys_cc Cell-centered physical height MultiFab.
21  * @param[in] geom Geometry defining the domain.
22  * @param[in] rho_sfc Density at the surface.
23  * @param[in] Thetabar Potential temperature of the isentropic column.
24  */
25 template <int NZMAX>
26 static void
27 hse_terrain_columns (amrex::MultiFab& rho_hse,
28  const amrex::MultiFab& z_phys_cc,
29  amrex::Geometry const& geom,
30  amrex::Real rho_sfc, amrex::Real Thetabar)
31 {
32  const int domlo_z = geom.Domain().smallEnd(2);
33  const int domhi_z = geom.Domain().bigEnd(2);
34  if (domhi_z > NZMAX-1) {
35  amrex::Abort("1D Arrays are hard-wired to only " + std::to_string(NZMAX) + " high");
36  }
37 
38  //
39  // A column is integrated upward from the bottom of its box. A box stacked on another box
40  // of this level (the BoxArray is split in z, e.g. amr.max_grid_size below the number of
41  // cells in z) must continue from the density that box reached, not start afresh from
42  // the ghost cell below it. The boxes are therefore integrated in bands of equal lowest
43  // index, bottom up, and before each band the cells just below it are filled from the
44  // bands already done. With a single band nothing is filled.
45  //
46  const amrex::Vector<int> bands = column_bands(rho_hse.boxArray());
47 
48  for (const int klo_band : bands)
49  {
50  if (klo_band != bands[0]) {
51  fill_below_band(rho_hse, 0, 1, klo_band, amrex::IntVect(1,1,0), geom);
52  }
53 
54  for ( amrex::MFIter mfi(rho_hse, TileNoZ()); mfi.isValid(); ++mfi )
55  {
56  amrex::Array4<amrex::Real > rho_arr = rho_hse.array(mfi);
57  amrex::Array4<amrex::Real const> z_cc_arr = z_phys_cc.const_array(mfi);
58 
59  // Create a flat box with same horizontal extent but only one cell in vertical
60  const amrex::Box& tbz = mfi.nodaltilebox(2);
61  amrex::Box b2d = tbz; // Copy constructor
62  b2d.grow(0,1); b2d.grow(1,1); // Grow by one in the lateral directions
63  b2d.setRange(2,0);
64 
65  const int klo = tbz.smallEnd(2);
66  const int khi = tbz.bigEnd(2)-1;
67 
68  if (klo != klo_band) { continue; }
69 
70  amrex::ParallelFor(b2d, [=] AMREX_GPU_DEVICE (int i, int j, int)
71  {
72  //
73  // The density we start each column's integration from. If this box does
74  // not reach the bottom of the domain then that is the density in the
75  // ghost cell below the box: the density the box of this level below it
76  // reached or, where there is no such box, the one interpolated from the
77  // coarser level.
78  //
79  // NOTE: we must do this lookup here, inside the kernel, for two reasons:
80  // rho_arr is device memory, so reading it on the host is invalid in
81  // a GPU build, and -- more importantly -- each column has its own
82  // surface density, so we must not use the value at one corner of
83  // the box for every column.
84  //
85  const amrex::Real rho_local_sfc = (klo == 0) ? rho_sfc : rho_arr(i,j,klo-1);
86 
87  amrex::Array1D<amrex::Real,0,NZMAX-1> r;
88  amrex::Array1D<amrex::Real,0,NZMAX-1> p;
89  HSEutils::init_isentropic_hse_terrain(i,j,rho_local_sfc,Thetabar,&(r(0)),&(p(0)),z_cc_arr,klo,khi);
90 
91  for (int k = klo; k <= khi; k++) {
92  rho_arr(i,j,k) = r(k);
93  }
94 
95  // Impose Neumann conditions at bottom and top of domain boundary
96  if (klo == domlo_z) {
97  rho_arr(i,j,domlo_z-1) = rho_arr(i,j,domlo_z);
98  }
99  if (khi == domhi_z) {
100  rho_arr(i,j,domhi_z+1) = rho_arr(i,j,domhi_z);
101  }
102  });
103  } // mfi
104  } // band
105 }
106 
107 /**
108  * @brief Initialize density in hydrostatic equilibrium for dry cases.
109  *
110  * @param[out] rho_hse MultiFab to be filled with HSE density.
111  * @param z_phys_nd Unused nodal physical height MultiFab.
112  * @param[in] z_phys_cc Cell-centered physical height MultiFab.
113  * @param[in] geom Geometry defining the domain.
114  * @param[in] stretched_dz_h Vector of stretched grid spacings.
115  * @param[in] is_constant_dz Whether the grid spacing is constant.
116  * @param[in] is_stretched_dz Whether the grid spacing is stretched.
117  */
118 void
119 erf_init_dens_hse_dry (amrex::MultiFab& rho_hse,
120  std::unique_ptr<amrex::MultiFab>& /*z_phys_nd*/,
121  std::unique_ptr<amrex::MultiFab>& z_phys_cc,
122  amrex::Geometry const& geom,
123  const amrex::Vector<amrex::Real>& stretched_dz_h,
124  bool is_constant_dz, bool is_stretched_dz) override
125 {
126  const amrex::Real T_sfc = amrex::Real(300.);
127  const amrex::Real rho_sfc = p_0 / (R_d*T_sfc);
128  const amrex::Real Thetabar = T_sfc;
129 
130  if (!is_constant_dz && !is_stretched_dz) {
131 
132  // Column arrays sized from the height of the domain, so a tall column on any mesh fits
133  if (geom.Domain().bigEnd(2) > 255) {
134  hse_terrain_columns<512>(rho_hse, *z_phys_cc, geom, rho_sfc, Thetabar);
135  } else {
136  hse_terrain_columns<256>(rho_hse, *z_phys_cc, geom, rho_sfc, Thetabar);
137  }
138 
139 
140  } else {
141 
142 
143  // Note that this integrates over the entire domain height,
144  // but only copies into each box as appropriate.
145  // There is no assumption that any one box spans the whole vertical height.
146  const int klo = geom.Domain().smallEnd(2);
147  const int khi = geom.Domain().bigEnd(2);
148 
149  // These are at cell centers (unstaggered)
150  amrex::Vector<amrex::Real> h_r(khi+2);
151  amrex::Vector<amrex::Real> h_p(khi+2);
152 
153  amrex::Gpu::DeviceVector<amrex::Real> d_r(khi+2);
154  amrex::Gpu::DeviceVector<amrex::Real> d_p(khi+2);
155 
156  if (is_constant_dz) {
157  const amrex::Real dz = geom.CellSize()[2];
158  HSEutils::init_isentropic_hse_constant_dz(rho_sfc,Thetabar,h_r.data(),h_p.data(),dz,geom.ProbLo(2),klo,khi);
159  } else { // stretched_dz
160  HSEutils::init_isentropic_hse_stretched_dz(rho_sfc,Thetabar,h_r.data(),h_p.data(),stretched_dz_h.data(),geom.ProbLo(2),klo,khi);
161  }
162 
163  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, h_r.begin(), h_r.end(), d_r.begin());
164  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, h_p.begin(), h_p.end(), d_p.begin());
165 
166  amrex::Real* r = d_r.data();
167 
168 #ifdef _OPENMP
169 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
170 #endif
171  for ( amrex::MFIter mfi(rho_hse, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
172  {
173  const amrex::Box& bx = mfi.growntilebox(1);
174  const amrex::Array4<amrex::Real> rho_hse_arr = rho_hse[mfi].array();
175  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
176  {
177  int kk = std::max(k,0);
178  rho_hse_arr(i,j,k) = r[kk];
179  });
180  } // mfi
181 
182  // The kernels above read d_r; wait for them before d_r is freed
183  amrex::Gpu::streamSynchronize();
184  }
185 }
186 
187 /**
188  * @brief Initialize density in hydrostatic equilibrium for moist cases.
189  *
190  * @param[out] rho_hse MultiFab to be filled with HSE density.
191  * @param z_phys_nd Unused nodal physical height MultiFab.
192  * @param[in] geom Geometry defining the domain.
193  * @param[in] zlevels_stag Staggered vertical levels of this level; the heights on a mesh
194  * other than ConstantDz, where CellSize(2) is not physical.
195  */
196 void
197 erf_init_dens_hse_moist (amrex::MultiFab& rho_hse,
198  std::unique_ptr<amrex::MultiFab>& /*z_phys_nd*/,
199  amrex::Geometry const& geom,
200  const amrex::Vector<amrex::Real>& zlevels_stag) override
201 {
202  const amrex::Real dz = geom.CellSize()[2];
203  const int khi = geom.Domain().bigEnd()[2];
204 
205  // These are at cell centers (unstaggered)
206  amrex::Vector<amrex::Real> h_r(khi+2);
207  amrex::Vector<amrex::Real> h_p(khi+2);
208  amrex::Vector<amrex::Real> h_t(khi+2);
209  amrex::Vector<amrex::Real> h_q_v(khi+2);
210 
211  amrex::ParmParse pp("prob");
212  amrex::Real q_t = amrex::Real(0.02);
213  pp.query("qt_init",q_t);
214 
215  amrex::Real eq_pot_temp = amrex::Real(320.0);
216  pp.query("eq_pot_temp",eq_pot_temp);
217 
218  bool use_empirical = false;
219  pp.query("use_empirical_psat",use_empirical);
220 
221  amrex::Real z_tr_1 = -one;
222  amrex::Real z_tr_2 = -one;
223  pp.query("height", z_tr_1);
224  pp.query("z_tr", z_tr_2);
225 
226  // Used only with T_from_theta_in_moist_init, but kept at physical values:
227  // zeros raise an FPE in the speculatively evaluated compute_theta arm.
228  amrex::Real T_tr = amrex::Real(300), theta_tr = amrex::Real(300), theta_0 = amrex::Real(300);
229 
230  bool T_from_theta = false;
231  pp.query("T_from_theta_in_moist_init", T_from_theta);
232 
233  if (T_from_theta) {
234  pp.get("theta_tr",theta_tr); pp.get("theta_0",theta_0);
235  pp.get("T_tr",T_tr);
236  }
237 
238  if (use_physical_z()) {
239  HSEutils::init_isentropic_hse_no_terrain( h_t.data(), h_r.data(), h_p.data(),
240  h_q_v.data(), zlevels_stag.data(), khi, q_t, eq_pot_temp,
241  use_empirical, T_from_theta, z_tr_1, z_tr_2,
242  theta_0, theta_tr, T_tr);
243  } else {
244  HSEutils::init_isentropic_hse_no_terrain( h_t.data(), h_r.data(), h_p.data(),
245  h_q_v.data(), dz, geom.ProbLo(2), khi, q_t, eq_pot_temp, use_empirical,
246  T_from_theta, z_tr_1, z_tr_2,
247  theta_0, theta_tr, T_tr);
248  }
249 
250  amrex::Gpu::DeviceVector<amrex::Real> d_r(khi+2);
251  amrex::Gpu::copy(amrex::Gpu::hostToDevice, h_r.begin(), h_r.end(), d_r.begin());
252  amrex::Real* r = d_r.data();
253 
254 #ifdef _OPENMP
255 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
256 #endif
257  for ( amrex::MFIter mfi(rho_hse,amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
258  {
259  const amrex::Box& bx = mfi.growntilebox(1);
260  const amrex::Array4<amrex::Real> rho_hse_arr = rho_hse[mfi].array();
261  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
262  {
263  int kk = std::max(k,0);
264  rho_hse_arr(i,j,k) = r[kk];
265  });
266  } // mfi
267 
268  // The kernels above read d_r; wait for them before d_r is freed
269  amrex::Gpu::streamSynchronize();
270 }
271 
272 /**
273  * @brief Initialize HSE density as a constant value.
274  *
275  * @param[out] rho_hse MultiFab to be filled with constant HSE density.
276  */
277 void
278 erf_init_const_dens_hse (amrex::MultiFab& rho_hse) override
279 {
280  amrex::Real rho_0 = base_parms.rho_0;
281  for ( amrex::MFIter mfi(rho_hse, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
282  {
283  amrex::Array4<amrex::Real> rho_hse_arr = rho_hse.array(mfi);
284  const amrex::Box& gbx = mfi.growntilebox(1);
285  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
286  {
287  rho_hse_arr(i,j,k) = rho_0;
288  });
289  }
290 }
291 /**
292  * @brief Initialize constant density and potential temperature in HSE.
293  *
294  * @param[out] rho_hse MultiFab for HSE density.
295  * @param[out] p_hse MultiFab for HSE pressure.
296  * @param[out] pi_hse MultiFab for HSE internal energy.
297  * @param[out] th_hse MultiFab for HSE potential temperature.
298  * @param[out] qv_hse MultiFab for HSE water vapor mixing ratio.
299  * @param[in] l_rdOcp Constant R_d/Cp.
300  */
301 void
302 erf_init_const_dens_and_th_hse (amrex::MultiFab& rho_hse, amrex::MultiFab& p_hse,
303  amrex::MultiFab& pi_hse, amrex::MultiFab& th_hse,
304  amrex::MultiFab& qv_hse, amrex::Real l_rdOcp) override
305 {
306  amrex::Real rho_0 = base_parms.rho_0;
307  amrex::Real T_0 = base_parms.T_0;
308 
309  amrex::ParmParse pp_prob("prob");
310  amrex::Real dtheta_dz = zero; pp_prob.query("dtheta_dz",dtheta_dz);
311 
312  rho_hse.setVal(rho_0);
313  th_hse.setVal(T_0);
314 
315  amrex::Real rt0 = rho_0 * T_0;
316 
317  amrex::Real p0 = getPgivenRTh(rt0);
318  p_hse.setVal(p0);
319 
320  amrex::Real pi0 = getExnergivenRTh(rt0, l_rdOcp);
321 
322  pi_hse.setVal(pi0);
323 
324  // Default to zero background moisture
325  qv_hse.setVal(0.0);
326 }
327 
328 /**
329  * @brief Initialize constant density and linearly varying potential temperature in HSE.
330  *
331  * @param[out] rho_hse MultiFab for HSE density.
332  * @param[out] p_hse MultiFab for HSE pressure.
333  * @param[out] pi_hse MultiFab for HSE internal energy.
334  * @param[out] th_hse MultiFab for HSE potential temperature.
335  * @param[out] qv_hse MultiFab for HSE water vapor mixing ratio.
336  * @param[in] l_rdOcp Constant R_d/Cp.
337  * @param[in] z_phys_cc Cell-centered physical height MultiFab.
338  */
339 void
340 erf_init_const_dens_and_linear_th_hse (amrex::MultiFab& rho_hse, amrex::MultiFab& p_hse,
341  amrex::MultiFab& pi_hse, amrex::MultiFab& th_hse,
342  amrex::MultiFab& qv_hse, amrex::Real l_rdOcp,
343  std::unique_ptr<amrex::MultiFab>& z_phys_cc) override
344 {
345  amrex::Real rho_0 = base_parms.rho_0;
346  amrex::Real T_0 = base_parms.T_0;
347 
348  amrex::ParmParse pp_prob("prob");
349  amrex::Real dtheta_dz = zero; pp_prob.query("dtheta_dz",dtheta_dz);
350 
351  rho_hse.setVal(rho_0);
352 
353  for ( amrex::MFIter mfi(th_hse, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi )
354  {
355  amrex::Array4<amrex::Real> th_hse_arr = th_hse.array(mfi);
356  amrex::Array4<amrex::Real> p_hse_arr = p_hse.array(mfi);
357  amrex::Array4<amrex::Real> pi_hse_arr = pi_hse.array(mfi);
358  amrex::Array4<amrex::Real const> z_arr = z_phys_cc->array(mfi);
359  const amrex::Box& gbx = mfi.growntilebox(1);
360  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
361  {
362  th_hse_arr(i,j,k) = T_0 + dtheta_dz * (z_arr(i,j,k)- z_arr(i,j,0));
363  p_hse_arr(i,j,k) = getPgivenRTh(rho_0*th_hse_arr(i,j,k));
364  pi_hse_arr(i,j,k) = getExnergivenRTh(rho_0*th_hse_arr(i,j,k), l_rdOcp);
365  });
366  } // mfi
367 
368  // Default to zero background moisture
369  qv_hse.setVal(0.0);
370 }
Vector< int > column_bands(const BoxArray &ba)
Definition: ERF_ColumnBands.cpp:13
void fill_below_band(MultiFab &mf, int icomp, int ncomp, int klo_band, const IntVect &lateral_ng, const Geometry &geom)
Definition: ERF_ColumnBands.cpp:23
constexpr amrex::Real p_0
Definition: ERF_Constants.H:69
constexpr amrex::Real R_d
Definition: ERF_Constants.H:49
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getExnergivenRTh(const amrex::Real rhotheta, const amrex::Real rdOcp, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:156
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getPgivenRTh(const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:81
Real rho_0
Definition: ERF_InitCustomPert_ABL.H:18
ParmParse pp_prob("prob")
Real T_0
Definition: ERF_InitCustomPert_ABL.H:19
ParmParse pp("prob")
const int khi
Definition: ERF_InitCustomPert_Bubble.H:25
const int klo
Definition: ERF_InitCustomPert_KE.H:9
Real eq_pot_temp
Definition: ERF_InitCustomPert_MultiSpeciesBubble.H:27
bool use_empirical
Definition: ERF_InitCustomPert_MultiSpeciesBubble.H:29
Vector< Real > h_t(khi+2)
Real T_tr
Definition: ERF_InitCustomPert_SquallLine.H:47
Real q_t
Definition: ERF_InitCustomPert_SquallLine.H:28
Vector< Real > h_q_v(khi+2)
Gpu::DeviceVector< Real > d_p(khi+2)
Vector< Real > h_r(khi+2)
Real theta_tr
Definition: ERF_InitCustomPert_SquallLine.H:51
Vector< Real > h_p(khi+2)
Gpu::DeviceVector< Real > d_r(khi+2)
Real theta_0
Definition: ERF_InitCustomPert_SquallLine.H:50
void erf_init_const_dens_hse(amrex::MultiFab &rho_hse) override
Initialize HSE density as a constant value.
Definition: ERF_InitDensityHSE.H:278
void erf_init_dens_hse_moist(amrex::MultiFab &rho_hse, std::unique_ptr< amrex::MultiFab > &, amrex::Geometry const &geom, const amrex::Vector< amrex::Real > &zlevels_stag) override
Initialize density in hydrostatic equilibrium for moist cases.
Definition: ERF_InitDensityHSE.H:197
void erf_init_const_dens_and_linear_th_hse(amrex::MultiFab &rho_hse, amrex::MultiFab &p_hse, amrex::MultiFab &pi_hse, amrex::MultiFab &th_hse, amrex::MultiFab &qv_hse, amrex::Real l_rdOcp, std::unique_ptr< amrex::MultiFab > &z_phys_cc) override
Initialize constant density and linearly varying potential temperature in HSE.
Definition: ERF_InitDensityHSE.H:340
static void hse_terrain_columns(amrex::MultiFab &rho_hse, const amrex::MultiFab &z_phys_cc, amrex::Geometry const &geom, amrex::Real rho_sfc, amrex::Real Thetabar)
Hydrostatic columns of a terrain-fitted mesh, integrated upward from the bottom of each box.
Definition: ERF_InitDensityHSE.H:27
void erf_init_const_dens_and_th_hse(amrex::MultiFab &rho_hse, amrex::MultiFab &p_hse, amrex::MultiFab &pi_hse, amrex::MultiFab &th_hse, amrex::MultiFab &qv_hse, amrex::Real l_rdOcp) override
Initialize constant density and potential temperature in HSE.
Definition: ERF_InitDensityHSE.H:302
void erf_init_dens_hse_dry(amrex::MultiFab &rho_hse, std::unique_ptr< amrex::MultiFab > &, std::unique_ptr< amrex::MultiFab > &z_phys_cc, amrex::Geometry const &geom, const amrex::Vector< amrex::Real > &stretched_dz_h, bool is_constant_dz, bool is_stretched_dz) override
Initialize density in hydrostatic equilibrium for dry cases.
Definition: ERF_InitDensityHSE.H:119
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);})
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
AMREX_FORCE_INLINE amrex::IntVect TileNoZ()
Definition: ERF_TileNoZ.H:11
auto rho_arr
Definition: ERF_UpdateWSubsidence_SineMassFlux.H:3
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void init_isentropic_hse_constant_dz(const amrex::Real &r_sfc, const amrex::Real &theta, amrex::Real *r, amrex::Real *p, const amrex::Real &dz, const amrex::Real &z_bot, const int klo, const int khi)
Definition: ERF_HSEUtils.H:112
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void init_isentropic_hse_stretched_dz(const amrex::Real &r_sfc, const amrex::Real &theta, amrex::Real *r, amrex::Real *p, const amrex::Real *stretched_dz, const amrex::Real &z_bot, const int klo, const int khi)
Definition: ERF_HSEUtils.H:226
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void init_isentropic_hse_terrain(int i, int j, const amrex::Real &r_sfc, const amrex::Real &theta, amrex::Real *r, amrex::Real *p, const amrex::Array4< amrex::Real const > z_cc, const int &klo, const int &khi)
Definition: ERF_HSEUtils.H:342
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE void init_isentropic_hse_no_terrain(Real *theta, Real *r, Real *p, Real *q_v, const Real &dz, const Real &z_bot, const int &khi, const Real q_t, const Real eq_pot_temp, const bool use_empirical, const bool T_from_theta=false, const Real z_tr_1=-one, const Real z_tr_2=-one, const Real theta_0=amrex::Real(300), const Real theta_tr=amrex::Real(300), const Real T_tr=amrex::Real(300))
Definition: ERF_HSEUtils.H:769
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
@ p
Definition: ERF_WSM6.H:303
real(c_double), parameter p0
Definition: ERF_module_model_constants.F90:40