ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_NearSurfaceDiagnostics.H
Go to the documentation of this file.
1 #ifndef ERF_NEAR_SURFACE_DIAGNOSTICS_H_
2 #define ERF_NEAR_SURFACE_DIAGNOSTICS_H_
3 
4 #include <AMReX_GpuQualifiers.H>
5 #include <AMReX_Math.H>
6 #include <AMReX_MultiFab.H>
7 #include <AMReX_iMultiFab.H>
8 #include <cmath>
9 
10 #include "ERF_Constants.H"
11 #include "ERF_MOSTStress.H"
13 
15 {
16 
18 {
19  amrex::Real temperature_vegetated;
20  amrex::Real temperature_bare;
22  amrex::Real mixing_ratio_bare;
23  amrex::Real vegetation_fraction;
24 };
25 
27 {
28  amrex::Real temperature;
29  amrex::Real mixing_ratio;
30 };
31 
32 struct MostState
33 {
34  amrex::Real theta_surface;
35  amrex::Real theta_star;
36  amrex::Real mixing_ratio_surface;
37  amrex::Real mixing_ratio_star;
38  amrex::Real roughness_height;
39  amrex::Real obukhov_length;
40 };
41 
43 {
44  amrex::Real potential_temperature;
45  amrex::Real mixing_ratio;
46 };
47 
48 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
49 bool valid_real (amrex::Real value) noexcept
50 {
51  return amrex::Math::isfinite(value);
52 }
53 
54 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
55 bool valid_noahmp_component (amrex::Real value) noexcept
56 {
57  return valid_real(value) && value > amrex::Real(-9990.0) &&
58  value < amrex::Real(0.5) * lsm_undefined;
59 }
60 
61 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
62 bool native_bundle_temperature_is_valid (const NativeBundle& bundle) noexcept;
63 
64 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
65 bool native_bundle_mixing_ratio_is_valid (const NativeBundle& bundle) noexcept;
66 
67 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
68 bool native_bundle_vegetation_fraction_is_valid (const NativeBundle& bundle) noexcept;
69 
70 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
71 bool native_bundle_is_valid (const NativeBundle& bundle) noexcept
72 {
73  return native_bundle_temperature_is_valid(bundle) &&
76 }
77 
78 // A tile with zero weight does not enter the aggregate, so its value is not
79 // checked: Noah-MP writes T2MVXY = 0 where FVEGXY = 0 (and the bare tile is
80 // likewise unused where FVEGXY = 1). Any fraction strictly between 0 and 1
81 // still requires both tiles to be valid.
82 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
83 bool native_vegetated_tile_is_used (amrex::Real vegetation_fraction) noexcept
84 {
85  return vegetation_fraction > amrex::Real(0.0);
86 }
87 
88 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
89 bool native_bare_tile_is_used (amrex::Real vegetation_fraction) noexcept
90 {
91  return vegetation_fraction < amrex::Real(1.0);
92 }
93 
94 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
95 bool native_bundle_temperature_is_valid (const NativeBundle& bundle) noexcept
96 {
97  if (!native_bundle_vegetation_fraction_is_valid(bundle)) { return false; }
98  const amrex::Real f = bundle.vegetation_fraction;
99  const bool vegetated_ok = !native_vegetated_tile_is_used(f) ||
100  (valid_noahmp_component(bundle.temperature_vegetated) &&
101  bundle.temperature_vegetated > amrex::Real(0.0));
102  const bool bare_ok = !native_bare_tile_is_used(f) ||
103  (valid_noahmp_component(bundle.temperature_bare) &&
104  bundle.temperature_bare > amrex::Real(0.0));
105  return vegetated_ok && bare_ok;
106 }
107 
108 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
110 {
111  if (!native_bundle_vegetation_fraction_is_valid(bundle)) { return false; }
112  const amrex::Real f = bundle.vegetation_fraction;
113  const bool vegetated_ok = !native_vegetated_tile_is_used(f) ||
114  (valid_noahmp_component(bundle.mixing_ratio_vegetated) &&
115  bundle.mixing_ratio_vegetated >= amrex::Real(0.0));
116  const bool bare_ok = !native_bare_tile_is_used(f) ||
117  (valid_noahmp_component(bundle.mixing_ratio_bare) &&
118  bundle.mixing_ratio_bare >= amrex::Real(0.0));
119  return vegetated_ok && bare_ok;
120 }
121 
122 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
124 {
125  return valid_noahmp_component(bundle.vegetation_fraction) &&
126  bundle.vegetation_fraction >= amrex::Real(0.0) &&
127  bundle.vegetation_fraction <= amrex::Real(1.0);
128 }
129 
130 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
132 {
133  const amrex::Real f = bundle.vegetation_fraction;
134  // At the endpoints take the one tile in use, so that an unused tile's
135  // placeholder (which may be a sentinel) cannot leak in through 0 * value.
137  return {bundle.temperature_bare, bundle.mixing_ratio_bare};
138  }
139  if (!native_bare_tile_is_used(f)) {
140  return {bundle.temperature_vegetated, bundle.mixing_ratio_vegetated};
141  }
142  return {
143  f * bundle.temperature_vegetated + (amrex::Real(1.0)-f) * bundle.temperature_bare,
144  f * bundle.mixing_ratio_vegetated + (amrex::Real(1.0)-f) * bundle.mixing_ratio_bare
145  };
146 }
147 
148 // Largest stability coordinate z/L used by the 2 m and 10 m profiles. The
149 // Businger-Dyer stable correction is linear in z/L and is calibrated for z/L of
150 // order one; with a very small positive L it would otherwise grow without bound.
151 // The QNSE correction grows only as ln(1 + b z/L), and its flux kernels run up to
152 // the bound of the kernels, most_zeta_lim (their fold can sit above 1), so the
153 // profiles take the same bound and are evaluated at the z/L the flux was.
154 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
155 amrex::Real most_profile_zeta_max (const similarity_funs& sfuns) noexcept
156 {
157  const amrex::Real zlim = most_zeta_lim; // by value: no reference to a namespace constant in device code
158  return sfuns.uses_qnse() ? zlim : amrex::Real(1.0);
159 }
160 
161 // The scalar MOST factor ln(z/z0) - psi_h(z/L) of the surface layer's similarity
162 // functions sfuns (most.stable_functions; the callers pass the surface layer's own,
163 // so that a caller cannot fall back to Businger-Dyer by leaving them out).
164 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
165 bool evaluate_most_factor (const MostState& state,
166  amrex::Real height,
167  amrex::Real& factor,
168  const similarity_funs& sfuns) noexcept
169 {
170  if (!valid_real(state.roughness_height) ||
171  !valid_real(state.obukhov_length) ||
172  state.roughness_height <= amrex::Real(0.0) ||
173  height <= state.roughness_height ||
174  state.obukhov_length == amrex::Real(0.0)) {
175  return false;
176  }
177  const amrex::Real zeta = amrex::min(height / state.obukhov_length,
178  most_profile_zeta_max(sfuns));
179  factor = std::log(height / state.roughness_height) - sfuns.calc_psi_h(zeta);
180  return valid_real(factor);
181 }
182 
183 // The momentum counterpart of evaluate_most_factor: ln(z/z0) - psi_m(z/L), so that
184 // the wind speed at height z is u_star / KAPPA times it.
185 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
186 bool evaluate_most_momentum_factor (amrex::Real roughness_height,
187  amrex::Real obukhov_length,
188  amrex::Real height,
189  amrex::Real& factor,
190  const similarity_funs& sfuns) noexcept
191 {
192  if (!valid_real(roughness_height) || !valid_real(obukhov_length) ||
193  roughness_height <= amrex::Real(0.0) ||
194  height <= roughness_height ||
195  obukhov_length == amrex::Real(0.0)) {
196  return false;
197  }
198  const amrex::Real zeta = amrex::min(height / obukhov_length,
199  most_profile_zeta_max(sfuns));
200  factor = std::log(height / roughness_height) - sfuns.calc_psi_m(zeta);
201  return valid_real(factor);
202 }
203 
204 // Evaluate ERF's integrated scalar profile without changing the production
205 // surface-layer state. The inputs use theta and mixing-ratio scales.
206 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
207 bool evaluate_most_profile (const MostState& state,
208  amrex::Real height,
209  MostProfile& profile,
210  const similarity_funs& sfuns) noexcept
211 {
212  if (!valid_real(state.theta_surface) || !valid_real(state.theta_star) ||
213  !valid_real(state.mixing_ratio_surface) || !valid_real(state.mixing_ratio_star)) {
214  return false;
215  }
216 
217  amrex::Real profile_factor = amrex::Real(0.0);
218  if (!evaluate_most_factor(state, height, profile_factor, sfuns)) {
219  return false;
220  }
221 
222  profile.potential_temperature = state.theta_surface +
223  state.theta_star / KAPPA * profile_factor;
224  profile.mixing_ratio = state.mixing_ratio_surface +
225  state.mixing_ratio_star / KAPPA * profile_factor;
226  return valid_real(profile.potential_temperature) &&
227  valid_real(profile.mixing_ratio) &&
228  profile.potential_temperature > amrex::Real(0.0) &&
229  profile.mixing_ratio >= amrex::Real(0.0);
230 }
231 
232 struct Sources
233 {
234  const amrex::MultiFab* native_temperature_vegetated = nullptr;
235  const amrex::MultiFab* native_temperature_bare = nullptr;
236  const amrex::MultiFab* native_mixing_ratio_vegetated = nullptr;
237  const amrex::MultiFab* native_mixing_ratio_bare = nullptr;
238  const amrex::MultiFab* native_vegetation_fraction = nullptr;
239 
240  const amrex::MultiFab* theta_surface = nullptr;
241  const amrex::MultiFab* theta_star = nullptr;
242  const amrex::MultiFab* mixing_ratio_surface = nullptr;
243  const amrex::MultiFab* mixing_ratio_star = nullptr;
244  const amrex::MultiFab* roughness_height = nullptr;
245  const amrex::MultiFab* obukhov_length = nullptr;
246  const amrex::MultiFab* source_mask = nullptr;
247  const amrex::iMultiFab* land_mask = nullptr;
248  const amrex::MultiFab* cons = nullptr;
249  const amrex::MultiFab* z_phys_nd = nullptr;
250  const amrex::MultiFab* z_sfc_nd = nullptr; ///< ground heights at the nodes; needed with z_phys_nd
251  similarity_funs sfuns; ///< the surface layer's similarity functions (most.stable_functions)
252 
253  amrex::Real dz = amrex::Real(0.0);
254  int klo = 0;
255  bool moist = false;
256  bool has_lsm = false;
257 };
258 
259 // Fill any requested unified 2-m components. A component index below zero
260 // means that the caller did not select that field. A source-only request uses
261 // the temperature path; provenance paired with humidity describes humidity
262 // without adding a temperature prerequisite. Dry runs retain a meaningful
263 // temperature source while humidity remains unavailable.
264 void fill (amrex::MultiFab& dst,
265  int temperature_comp,
266  int mixing_ratio_comp,
267  int source_comp,
268  const Sources& sources,
269  amrex::Real missing_value = amrex::Real(-999.0));
270 
271 // Inputs to the 10 m wind: the surface layer's friction velocity, roughness height
272 // and Obukhov length set the speed; the grid-relative wind at the center of the first
273 // cell sets the direction; cos_alpha and sin_alpha, where given, turn it earth-relative.
274 // Over a moving surface (current_x and current_y given, on the x- and y-faces of the
275 // plane k = klo) the speed and direction are those of the wind relative to the surface,
276 // and the current is added back.
278 {
279  const amrex::MultiFab* u_star = nullptr;
280  const amrex::MultiFab* roughness_height = nullptr;
281  const amrex::MultiFab* obukhov_length = nullptr;
282  const amrex::MultiFab* xvel = nullptr;
283  const amrex::MultiFab* yvel = nullptr;
284  const amrex::MultiFab* current_x = nullptr;
285  const amrex::MultiFab* current_y = nullptr;
286  const amrex::MultiFab* cos_alpha = nullptr;
287  const amrex::MultiFab* sin_alpha = nullptr;
288  similarity_funs sfuns; ///< the surface layer's similarity functions (most.stable_functions)
289  int klo = 0;
290 };
291 
292 // Height of the wind diagnostics above the local surface
293 constexpr amrex::Real wind_height = amrex::Real(10.0);
294 
295 // Fill any requested 10 m wind components: grid-relative u and v, and earth-relative
296 // u_east and v_north. A component index below zero means that field was not
297 // requested. Where the surface layer is absent or its state cannot be evaluated at
298 // 10 m (for example z0 >= 10 m), every requested component is missing_value.
299 void fill_wind_10m (amrex::MultiFab& dst,
300  int u_comp,
301  int v_comp,
302  int u_east_comp,
303  int v_north_comp,
304  const WindSources& sources,
305  amrex::Real missing_value = amrex::Real(-999.0));
306 
307 } // namespace near_surface_diagnostics
308 
309 #endif
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:71
constexpr amrex::Real lsm_undefined
Definition: ERF_Constants.H:28
Real height
Definition: ERF_InitCustomPert_SquallLine.H:37
constexpr amrex::Real most_zeta_lim
Definition: ERF_MOSTUtils.H:71
Definition: ERF_NearSurfaceDiagnostics.cpp:15
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool evaluate_most_factor(const MostState &state, amrex::Real height, amrex::Real &factor, const similarity_funs &sfuns) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:165
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_vegetated_tile_is_used(amrex::Real vegetation_fraction) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:83
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool evaluate_most_profile(const MostState &state, amrex::Real height, MostProfile &profile, const similarity_funs &sfuns) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:207
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_bundle_vegetation_fraction_is_valid(const NativeBundle &bundle) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:123
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_bundle_temperature_is_valid(const NativeBundle &bundle) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:95
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE NativeAggregate aggregate_native_bundle(const NativeBundle &bundle) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:131
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_bundle_is_valid(const NativeBundle &bundle) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:71
constexpr amrex::Real wind_height
Definition: ERF_NearSurfaceDiagnostics.H:293
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_bare_tile_is_used(amrex::Real vegetation_fraction) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:89
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real most_profile_zeta_max(const similarity_funs &sfuns) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:155
void fill(MultiFab &dst, int temperature_comp, int mixing_ratio_comp, int source_comp, const Sources &sources, Real missing_value)
Definition: ERF_NearSurfaceDiagnostics.cpp:42
void fill_wind_10m(MultiFab &dst, int u_comp, int v_comp, int u_east_comp, int v_north_comp, const WindSources &sources, Real missing_value)
Definition: ERF_NearSurfaceDiagnostics.cpp:226
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool native_bundle_mixing_ratio_is_valid(const NativeBundle &bundle) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:109
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_noahmp_component(amrex::Real value) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:55
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_real(amrex::Real value) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:49
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool evaluate_most_momentum_factor(amrex::Real roughness_height, amrex::Real obukhov_length, amrex::Real height, amrex::Real &factor, const similarity_funs &sfuns) noexcept
Definition: ERF_NearSurfaceDiagnostics.H:186
Definition: ERF_NearSurfaceDiagnostics.H:43
amrex::Real potential_temperature
Definition: ERF_NearSurfaceDiagnostics.H:44
amrex::Real mixing_ratio
Definition: ERF_NearSurfaceDiagnostics.H:45
Definition: ERF_NearSurfaceDiagnostics.H:33
amrex::Real mixing_ratio_star
Definition: ERF_NearSurfaceDiagnostics.H:37
amrex::Real theta_star
Definition: ERF_NearSurfaceDiagnostics.H:35
amrex::Real theta_surface
Definition: ERF_NearSurfaceDiagnostics.H:34
amrex::Real obukhov_length
Definition: ERF_NearSurfaceDiagnostics.H:39
amrex::Real mixing_ratio_surface
Definition: ERF_NearSurfaceDiagnostics.H:36
amrex::Real roughness_height
Definition: ERF_NearSurfaceDiagnostics.H:38
Definition: ERF_NearSurfaceDiagnostics.H:27
amrex::Real temperature
Definition: ERF_NearSurfaceDiagnostics.H:28
amrex::Real mixing_ratio
Definition: ERF_NearSurfaceDiagnostics.H:29
Definition: ERF_NearSurfaceDiagnostics.H:18
amrex::Real mixing_ratio_vegetated
Definition: ERF_NearSurfaceDiagnostics.H:21
amrex::Real temperature_vegetated
Definition: ERF_NearSurfaceDiagnostics.H:19
amrex::Real vegetation_fraction
Definition: ERF_NearSurfaceDiagnostics.H:23
amrex::Real mixing_ratio_bare
Definition: ERF_NearSurfaceDiagnostics.H:22
amrex::Real temperature_bare
Definition: ERF_NearSurfaceDiagnostics.H:20
Definition: ERF_NearSurfaceDiagnostics.H:233
const amrex::MultiFab * z_sfc_nd
ground heights at the nodes; needed with z_phys_nd
Definition: ERF_NearSurfaceDiagnostics.H:250
const amrex::MultiFab * theta_surface
Definition: ERF_NearSurfaceDiagnostics.H:240
const amrex::MultiFab * z_phys_nd
Definition: ERF_NearSurfaceDiagnostics.H:249
bool moist
Definition: ERF_NearSurfaceDiagnostics.H:255
bool has_lsm
Definition: ERF_NearSurfaceDiagnostics.H:256
int klo
Definition: ERF_NearSurfaceDiagnostics.H:254
const amrex::iMultiFab * land_mask
Definition: ERF_NearSurfaceDiagnostics.H:247
const amrex::MultiFab * obukhov_length
Definition: ERF_NearSurfaceDiagnostics.H:245
const amrex::MultiFab * roughness_height
Definition: ERF_NearSurfaceDiagnostics.H:244
const amrex::MultiFab * native_temperature_bare
Definition: ERF_NearSurfaceDiagnostics.H:235
const amrex::MultiFab * cons
Definition: ERF_NearSurfaceDiagnostics.H:248
const amrex::MultiFab * theta_star
Definition: ERF_NearSurfaceDiagnostics.H:241
similarity_funs sfuns
the surface layer's similarity functions (most.stable_functions)
Definition: ERF_NearSurfaceDiagnostics.H:251
const amrex::MultiFab * native_temperature_vegetated
Definition: ERF_NearSurfaceDiagnostics.H:234
const amrex::MultiFab * mixing_ratio_surface
Definition: ERF_NearSurfaceDiagnostics.H:242
const amrex::MultiFab * native_mixing_ratio_bare
Definition: ERF_NearSurfaceDiagnostics.H:237
amrex::Real dz
Definition: ERF_NearSurfaceDiagnostics.H:253
const amrex::MultiFab * source_mask
Definition: ERF_NearSurfaceDiagnostics.H:246
const amrex::MultiFab * mixing_ratio_star
Definition: ERF_NearSurfaceDiagnostics.H:243
const amrex::MultiFab * native_mixing_ratio_vegetated
Definition: ERF_NearSurfaceDiagnostics.H:236
const amrex::MultiFab * native_vegetation_fraction
Definition: ERF_NearSurfaceDiagnostics.H:238
Definition: ERF_NearSurfaceDiagnostics.H:278
const amrex::MultiFab * current_y
Definition: ERF_NearSurfaceDiagnostics.H:285
const amrex::MultiFab * u_star
Definition: ERF_NearSurfaceDiagnostics.H:279
const amrex::MultiFab * yvel
Definition: ERF_NearSurfaceDiagnostics.H:283
const amrex::MultiFab * cos_alpha
Definition: ERF_NearSurfaceDiagnostics.H:286
const amrex::MultiFab * roughness_height
Definition: ERF_NearSurfaceDiagnostics.H:280
const amrex::MultiFab * xvel
Definition: ERF_NearSurfaceDiagnostics.H:282
similarity_funs sfuns
the surface layer's similarity functions (most.stable_functions)
Definition: ERF_NearSurfaceDiagnostics.H:288
const amrex::MultiFab * sin_alpha
Definition: ERF_NearSurfaceDiagnostics.H:287
const amrex::MultiFab * current_x
Definition: ERF_NearSurfaceDiagnostics.H:284
const amrex::MultiFab * obukhov_length
Definition: ERF_NearSurfaceDiagnostics.H:281
int klo
Definition: ERF_NearSurfaceDiagnostics.H:289
Definition: ERF_MOSTUtils.H:84