ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_LogShear.H
Go to the documentation of this file.
1 #ifndef ERF_LOG_SHEAR_H_
2 #define ERF_LOG_SHEAR_H_
3 
4 #include <cmath>
5 
6 #include <AMReX_Array4.H>
7 #include <AMReX_Box.H>
8 #include <AMReX_GpuLaunch.H>
9 #include <AMReX_GpuQualifiers.H>
10 #include <AMReX_REAL.H>
11 
12 #include "ERF_NumericalConstants.H"
13 
14 /**
15  * erf.rans_shear_stencil = log_law (k-eqn RANS, issue 5261): the vertical gradients of u and v in
16  * the strain invariant S_mn S_mn, exact for a logarithmic profile over the ground.
17  *
18  * ComputeSmnSmn averages the strains of the faces below and above a cell, which for du/dz is the
19  * central difference (u_{k+1} - u_{k-1}) / (z_{k+1} - z_{k-1}) on uniform layers. For the profile
20  * u = (u_* / kappa) ln((d + z0) / z0) that difference is larger than du/dz at the cell center by
21  * the factor (d_k + z0) ln((d_{k+1} + z0) / (d_{k-1} + z0)) / (d_{k+1} - d_{k-1}): 1.21 in the
22  * second cell of uniform layers, 1.06 in the third and 1.03 in the fourth (z0 << dz), and
23  * 1 + dz^2 / (3 d^2) far from the ground. The shear production of k, 2 nu_t S_mn S_mn, is then
24  * 46 %, 12 % and 6 % high there (45 %, 12 % and 6 % with z0 = 0.1 m and dz = 15.625 m).
25  *
26  * Here, in a cell k above the wall cell and below the top cell,
27  *
28  * du/dz = (u_{k+1} - u_{k-1}) / [ (d_k + z0) ln((d_{k+1} + z0) / (d_{k-1} + z0)) ]
29  *
30  * and the same for v, with u and v averaged to the cell centers, d_k the closure's wall distance,
31  * d_{k-1} and d_{k+1} that distance less and plus the height differences of the cell centers, and
32  * z0 the surface layer's roughness length of the column. S_mn S_mn changes by the difference of
33  * its (1,3) and (2,3) terms; dw/dx, dw/dy and the other components stay as ComputeStrain_* formed
34  * them. A cell whose d_{k-1} + z0 would not be positive is left as it is.
35  *
36  * The stencil is exact for the log profile only. For a profile that is linear in d it returns the
37  * gradient divided by the factor above (17 %, 6 % and 3 % low in the cells 2, 3 and 4), so it is
38  * less accurate than the central difference where the profile is closer to linear than to
39  * logarithmic: in a stable layer above about L / 5 (L the Obukhov length), and well above the
40  * ground, where both errors are below 1 %. On sloping ground d is the distance to the wall and
41  * the height differences are vertical, so the form is exact to leading order in the slope only.
42  */
43 namespace log_shear {
44 
45 /**
46  * Inverse of the vertical distance over which ComputeStrain_{N,S,T} difference u on the x-z edge
47  * (i,j,k), the edge below u(i,j,k): the height difference of the u points k and k-1.
48  * It is dz on uniform layers, (dz_k + dz_{k-1}) / 2 on stretched layers and
49  * dz / h_zeta (Compute_h_zeta_AtEdgeCenterJ) on a terrain-fitted mesh.
50  *
51  * @param[in] i x index of the edge
52  * @param[in] j y index of the edge
53  * @param[in] k z index of the edge
54  * @param[in] z_nd nodal physical heights
55  * @return inverse spacing [1/m]
56  */
57 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
58 amrex::Real
59 inv_dz_xedge (int i, int j, int k, const amrex::Array4<amrex::Real const>& z_nd) noexcept
60 {
61  return amrex::Real(4) / ( z_nd(i,j,k+1) + z_nd(i,j+1,k+1) - z_nd(i,j,k-1) - z_nd(i,j+1,k-1) );
62 }
63 
64 /**
65  * As inv_dz_xedge for v on the y-z edge (i,j,k) (Compute_h_zeta_AtEdgeCenterI).
66  *
67  * @param[in] i x index of the edge
68  * @param[in] j y index of the edge
69  * @param[in] k z index of the edge
70  * @param[in] z_nd nodal physical heights
71  * @return inverse spacing [1/m]
72  */
73 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
74 amrex::Real
75 inv_dz_yedge (int i, int j, int k, const amrex::Array4<amrex::Real const>& z_nd) noexcept
76 {
77  return amrex::Real(4) / ( z_nd(i,j,k+1) + z_nd(i+1,j,k+1) - z_nd(i,j,k-1) - z_nd(i+1,j,k-1) );
78 }
79 
80 /**
81  * S_mn S_mn of cell (i,j,k) with the log-consistent vertical gradients of u and v.
82  *
83  * The caller keeps k above the wall cell and below the top cell of the domain: the stencil reads
84  * the velocities of the cells k-1 and k+1, the edges k and k+1 (none of them a boundary edge)
85  * and the nodes k-1 to k+2.
86  *
87  * @param[in] i cell index in x
88  * @param[in] j cell index in y
89  * @param[in] k cell index in z
90  * @param[in] SmnSmn S_mn S_mn of the cell from ComputeSmnSmn
91  * @param[in] u x velocity (x faces)
92  * @param[in] v y velocity (y faces)
93  * @param[in] s13 (1,3) strain on the x-z edges, as ComputeSmnSmn read it
94  * @param[in] s23 (2,3) strain on the y-z edges, as ComputeSmnSmn read it
95  * @param[in] d wall distance of the cells [m], read in the cell (i,j,k) only
96  * @param[in] z0 roughness length of the column [m]
97  * @param[in] z_nd nodal physical heights
98  * @return the corrected S_mn S_mn, not below zero
99  */
100 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
101 amrex::Real
102 corrected_SmnSmn (int i, int j, int k, amrex::Real SmnSmn,
103  const amrex::Array4<amrex::Real const>& u,
104  const amrex::Array4<amrex::Real const>& v,
105  const amrex::Array4<amrex::Real const>& s13,
106  const amrex::Array4<amrex::Real const>& s23,
107  const amrex::Array4<amrex::Real const>& d,
108  amrex::Real z0,
109  const amrex::Array4<amrex::Real const>& z_nd) noexcept
110 {
111  // Heights of the cell centers k-1 and k+1 relative to the center k, from the nodes: the wall
112  // distance is read in the cell itself only, so the result does not depend on how the boxes
113  // are cut or on ghost cells of the distance.
114  const amrex::Real dz_lo = amrex::Real(0.125) * ( z_nd(i,j,k+1) + z_nd(i+1,j,k+1) + z_nd(i,j+1,k+1) + z_nd(i+1,j+1,k+1)
115  - z_nd(i,j,k-1) - z_nd(i+1,j,k-1) - z_nd(i,j+1,k-1) - z_nd(i+1,j+1,k-1) );
116  const amrex::Real dz_hi = amrex::Real(0.125) * ( z_nd(i,j,k+2) + z_nd(i+1,j,k+2) + z_nd(i,j+1,k+2) + z_nd(i+1,j+1,k+2)
117  - z_nd(i,j,k ) - z_nd(i+1,j,k ) - z_nd(i,j+1,k ) - z_nd(i+1,j+1,k ) );
118  const amrex::Real dc = d(i,j,k) + z0;
119  const amrex::Real dm = dc - dz_lo;
120  const amrex::Real dp = dc + dz_hi;
121  // Not where the cell below would lie at or under the roughness height (a wall distance
122  // shorter than the height above the ground). The negated form also leaves a cell with a NaN
123  // distance as it is.
124  if (!(dm > zero && dc > dm && dp > dc)) { return SmnSmn; }
125  const amrex::Real inv_lg = one / (dc * std::log(dp / dm));
126 
127  // The log-consistent gradients, from the velocities averaged to the cell centers
128  const amrex::Real gu_new = myhalf * ( u(i,j,k+1) + u(i+1,j,k+1) - u(i,j,k-1) - u(i+1,j,k-1) ) * inv_lg;
129  const amrex::Real gv_new = myhalf * ( v(i,j,k+1) + v(i,j+1,k+1) - v(i,j,k-1) - v(i,j+1,k-1) ) * inv_lg;
130 
131  // The gradients the four edges of ComputeSmnSmn hold: du/dz of s13, dv/dz of s23
132  const amrex::Real gu_old = fourth * ( (u(i ,j,k ) - u(i ,j,k-1)) * inv_dz_xedge(i ,j,k ,z_nd)
133  + (u(i ,j,k+1) - u(i ,j,k )) * inv_dz_xedge(i ,j,k+1,z_nd)
134  + (u(i+1,j,k ) - u(i+1,j,k-1)) * inv_dz_xedge(i+1,j,k ,z_nd)
135  + (u(i+1,j,k+1) - u(i+1,j,k )) * inv_dz_xedge(i+1,j,k+1,z_nd) );
136  const amrex::Real gv_old = fourth * ( (v(i,j ,k ) - v(i,j ,k-1)) * inv_dz_yedge(i,j ,k ,z_nd)
137  + (v(i,j ,k+1) - v(i,j ,k )) * inv_dz_yedge(i,j ,k+1,z_nd)
138  + (v(i,j+1,k ) - v(i,j+1,k-1)) * inv_dz_yedge(i,j+1,k ,z_nd)
139  + (v(i,j+1,k+1) - v(i,j+1,k )) * inv_dz_yedge(i,j+1,k+1,z_nd) );
140 
141  // The cell-centered strains of ComputeSmnSmn, and the same with the new gradients
142  const amrex::Real s13_old = fourth * ( s13(i,j,k) + s13(i,j,k+1) + s13(i+1,j,k) + s13(i+1,j,k+1) );
143  const amrex::Real s23_old = fourth * ( s23(i,j,k) + s23(i,j,k+1) + s23(i,j+1,k) + s23(i,j+1,k+1) );
144  const amrex::Real s13_new = s13_old + myhalf * (gu_new - gu_old);
145  const amrex::Real s23_new = s23_old + myhalf * (gv_new - gv_old);
146 
147  // Not below zero: SmnSmn holds the two old terms to round-off only
148  return amrex::max(amrex::Real(zero), SmnSmn + two * (s13_new*s13_new - s13_old*s13_old)
149  + two * (s23_new*s23_new - s23_old*s23_old));
150 }
151 
152 /**
153  * Replace S_mn S_mn by corrected_SmnSmn in the cells of bx above the wall cell and below the top
154  * cell of the domain. The wall cell keeps the strain of its faces (the lower one is the one-sided
155  * difference to the ghost value under the surface layer) and the top cell that of the top boundary.
156  *
157  * @param[in] bx cells ComputeSmnSmn filled
158  * @param[in] domain domain of the level
159  * @param[in,out] SmnSmn S_mn S_mn of the cells
160  * @param[in] u x velocity the strains were formed from
161  * @param[in] v y velocity the strains were formed from
162  * @param[in] s13 (1,3) strain on the x-z edges
163  * @param[in] s23 (2,3) strain on the y-z edges
164  * @param[in] d wall distance of the cells [m]
165  * @param[in] z0 roughness length of the surface layer (one layer, k = 0) [m]
166  * @param[in] z_nd nodal physical heights
167  */
168 inline void
169 correct_box (const amrex::Box& bx, const amrex::Box& domain,
170  const amrex::Array4<amrex::Real>& SmnSmn,
171  const amrex::Array4<amrex::Real const>& u,
172  const amrex::Array4<amrex::Real const>& v,
173  const amrex::Array4<amrex::Real const>& s13,
174  const amrex::Array4<amrex::Real const>& s23,
175  const amrex::Array4<amrex::Real const>& d,
176  const amrex::Array4<amrex::Real const>& z0,
177  const amrex::Array4<amrex::Real const>& z_nd)
178 {
179  amrex::Box cbx = bx;
180  cbx.setSmall(2, amrex::max(bx.smallEnd(2), domain.smallEnd(2) + 1));
181  cbx.setBig (2, amrex::min(bx.bigEnd(2) , domain.bigEnd(2) - 1));
182  if (!cbx.ok()) { return; }
183  amrex::ParallelFor(cbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
184  {
185  SmnSmn(i,j,k) = corrected_SmnSmn(i, j, k, SmnSmn(i,j,k), u, v, s13, s23, d, z0(i,j,0), z_nd);
186  });
187 }
188 
189 } // namespace log_shear
190 
191 #endif
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
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);})
Dimensionless numeric literals and pure mathematical constants.
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
Definition: ERF_LogShear.H:43
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real corrected_SmnSmn(int i, int j, int k, amrex::Real SmnSmn, const amrex::Array4< amrex::Real const > &u, const amrex::Array4< amrex::Real const > &v, const amrex::Array4< amrex::Real const > &s13, const amrex::Array4< amrex::Real const > &s23, const amrex::Array4< amrex::Real const > &d, amrex::Real z0, const amrex::Array4< amrex::Real const > &z_nd) noexcept
Definition: ERF_LogShear.H:102
void correct_box(const amrex::Box &bx, const amrex::Box &domain, const amrex::Array4< amrex::Real > &SmnSmn, const amrex::Array4< amrex::Real const > &u, const amrex::Array4< amrex::Real const > &v, const amrex::Array4< amrex::Real const > &s13, const amrex::Array4< amrex::Real const > &s23, const amrex::Array4< amrex::Real const > &d, const amrex::Array4< amrex::Real const > &z0, const amrex::Array4< amrex::Real const > &z_nd)
Definition: ERF_LogShear.H:169
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real inv_dz_yedge(int i, int j, int k, const amrex::Array4< amrex::Real const > &z_nd) noexcept
Definition: ERF_LogShear.H:75
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real inv_dz_xedge(int i, int j, int k, const amrex::Array4< amrex::Real const > &z_nd) noexcept
Definition: ERF_LogShear.H:59