ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_KesslerUtils.H
Go to the documentation of this file.
1 #ifndef ERF_KESSLER_UTILS_H_
2 #define ERF_KESSLER_UTILS_H_
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <limits>
7 
8 #include <AMReX_GpuQualifiers.H>
9 #include <AMReX_REAL.H>
10 
11 #include <ERF_NumericalConstants.H>
13 #include <ERF_MicrophysicsUtils.H>
14 
16  amrex::Real dq_vapor_to_cloud;
17  amrex::Real dq_cloud_to_vapor;
18  amrex::Real dq_cloud_to_rain;
19  amrex::Real dq_rain_to_vapor;
20 };
21 
23  amrex::Real dq_vapor_to_cloud;
24  amrex::Real dq_cloud_to_vapor;
25 };
26 
28  amrex::Real rho;
29  amrex::Real qp;
30 };
31 
32 static constexpr amrex::Real kessler_sedimentation_cfl_max = myhalf;
33 // More rain substeps than any healthy state asks for (AdvanceKessler aborts above this)
34 static constexpr int kessler_max_sedimentation_substeps = 1000;
35 static constexpr amrex::Real kessler_water_depth_m_per_kg_m2 = amrex::Real(1.0) / rhor;
36 static constexpr amrex::Real kessler_millimeters_per_meter = amrex::Real(1000.0);
37 // Klemp & Wilhelmson (1978) rain fall speed, as in WRF module_mp_kessler.F and DCMIP2016 kessler.f90:
38 // V [m/s] = 36.34 * (0.001*rho*qp)^0.1364 * (rho/1.16)^(-1/2), rho in kg/m^3 (0.001*rho is in g/cm^3).
39 static constexpr amrex::Real kessler_vt_coeff = amrex::Real(36.34);
40 static constexpr amrex::Real kessler_vt_rho_to_cgs = amrex::Real(0.001);
41 static constexpr amrex::Real kessler_vt_exponent = amrex::Real(0.1364);
42 static constexpr amrex::Real kessler_vt_rho0 = amrex::Real(1.16);
43 static constexpr amrex::Real kessler_vt_density_exponent = -myhalf;
44 
45 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
46 int kessler_face_donor_k (const int k,
47  const int k_hi) noexcept
48 {
49  return (k == k_hi + 1) ? k - 1 : k;
50 }
51 
52 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
53 amrex::Real kessler_terminal_velocity (const amrex::Real rho,
54  const amrex::Real qp) noexcept
55 {
56  // This function, with the kessler_vt_* constants, defines the Kessler rain fall speed.
57  return kessler_vt_coeff
60 }
61 
62 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
63 amrex::Real kessler_precip_flux (const amrex::Real rho,
64  const amrex::Real terminal_velocity,
65  const amrex::Real qp) noexcept
66 {
67  return rho * terminal_velocity * qp;
68 }
69 
70 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
71 amrex::Real kessler_limit_precip_flux (const amrex::Real flux,
72  const amrex::Real donor_rho,
73  const amrex::Real donor_qp,
74  const amrex::Real donor_detJ,
75  const amrex::Real coef) noexcept
76 {
77  // The donor cap: a face cannot export more rain in one substep than its donor cell
78  // holds, F * dt / dz <= rho_donor * qp_donor * detJ_donor, with coef = dt / dz.
79  return amrex::min(flux, donor_rho * donor_qp * donor_detJ / coef);
80 }
81 
82 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
83 int kessler_num_sedimentation_substeps (const amrex::Real max_courant) noexcept
84 {
85  // max_courant is the largest fall Courant number over the faces for the full step,
86  // V*dt over the height of the cell the face drains: enough substeps that rain falls
87  // at most CFL_MAX of that cell per substep, and always at least one. A NaN, infinite
88  // or huge max_courant returns kessler_max_sedimentation_substeps + 1 rather than casting
89  // it to an int; AdvanceKessler aborts before it gets here.
90  const amrex::Real ratio = max_courant / kessler_sedimentation_cfl_max;
91  if (!(ratio <= amrex::Real(kessler_max_sedimentation_substeps))) {
93  }
94  return amrex::max(1, static_cast<int>(std::ceil(ratio)));
95 }
96 
97 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
98 amrex::Real kessler_fall_courant (const amrex::Real terminal_velocity,
99  const amrex::Real dt,
100  const amrex::Real dz,
101  const amrex::Real detJ_donor) noexcept
102 {
103  // The fall Courant number at a face: the distance fallen in dt over the physical height
104  // dz*detJ of the donor cell, the one the face drains, as the donor-cap limiter uses.
105  return terminal_velocity * dt / (dz * detJ_donor);
106 }
107 
108 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
109 amrex::Real kessler_sizing_detJ (const int k,
110  const int k_lo,
111  const int k_hi,
112  const amrex::Real detJ_donor,
113  const amrex::Real detJ_below) noexcept
114 {
115  // The detJ that sizes the substeps at face k: the smaller of the donor's (the cell the
116  // face drains) and that of the cell below it (the one its rain falls into), for faces
117  // inside the domain. The substep count is fixed at the start of the step, while the
118  // cells below the rain front are still dry; looking one cell down covers the first cell
119  // the front enters. The surface face drains out of the domain and the top face carries
120  // no rain, so both use the donor alone.
121  return (k > k_lo && k <= k_hi) ? amrex::min(detJ_donor, detJ_below) : detJ_donor;
122 }
123 
124 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
125 amrex::Real kessler_rain_accumulation_increment (const amrex::Real precip_mass_per_area) noexcept
126 {
127  // Convert precipitation mass per unit area [kg m^-2] to liquid-water depth
128  // [mm]: divide by water density [kg m^-3], then multiply by mm per meter.
129  return precip_mass_per_area
132 }
133 
134 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
135 bool kessler_is_small_sedimentation_value (const amrex::Real value) noexcept
136 {
137  // MUST MATCH: current AdvanceKessler sedimentation zero threshold literal.
138  return std::fabs(value) < 1e-14;
139 }
140 
141 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
143  const amrex::Real qc,
144  const amrex::Real qsat,
145  const amrex::Real dtqsat,
146  const bool do_cond,
147  const amrex::Real latent_over_cp) noexcept
148 {
149  KesslerSaturationAdjustment source_terms{amrex::Real(0), amrex::Real(0)};
150 
151  const amrex::Real fac = latent_over_cp * dtqsat;
152 
153  // MUST MATCH: current AdvanceKessler condensation branch behavior.
154  if ((qv > qsat) && do_cond) {
155  source_terms.dq_vapor_to_cloud = std::min(qv, (qv - qsat) / (amrex::Real(1) + fac));
156  }
157 
158  // MUST MATCH: current AdvanceKessler cloud-evaporation branch behavior.
159  if ((qv < qsat) && (qc > amrex::Real(0)) && do_cond) {
160  source_terms.dq_cloud_to_vapor = std::min(qc, (qsat - qv) / (amrex::Real(1) + fac));
161  }
162 
163  return source_terms;
164 }
165 
166 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
168  const int k_hi,
169  const amrex::Real rho_km1,
170  const amrex::Real rho_k,
171  const amrex::Real /*qp_km1*/,
172  const amrex::Real qp_k) noexcept
173 {
174  KesslerFaceState face_state{amrex::Real(0), amrex::Real(0)};
175 
176  // Sedimentation is one-way downward transport. Use a face-centered density
177  // for the z-face flux, but upwind the transported rain mixing ratio from the
178  // donor cell. A centered qp can create flux from downstream rain when the
179  // donor cell is dry. The top boundary face carries no rain: nothing lies
180  // above the domain, so its donor is empty. It keeps the top cell's rho so
181  // the fall speed stays defined.
182  if (k == k_hi + 1) {
183  face_state.rho = rho_km1;
184  face_state.qp = amrex::Real(0);
185  } else if (k == 0) {
186  face_state.rho = rho_k;
187  face_state.qp = qp_k;
188  } else {
189  face_state.rho = amrex::Real(0.5) * (rho_km1 + rho_k);
190  face_state.qp = qp_k;
191  }
192 
193  // MUST MATCH: current AdvanceKessler nonnegative donor-qp clip before flux formation.
194  face_state.qp = std::max(amrex::Real(0), face_state.qp);
195  return face_state;
196 }
197 
198 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
199 amrex::Real kessler_sedimentation_tendency (const amrex::Real fz_hi,
200  const amrex::Real fz_lo,
201  const amrex::Real rho,
202  const amrex::Real dJinv,
203  const amrex::Real coef) noexcept
204 {
205  // MUST MATCH: current AdvanceKessler sedimentation tendency arithmetic order.
206  return dJinv * (amrex::Real(1) / rho) * (fz_hi - fz_lo) * coef;
207 }
208 
209 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
211  const amrex::Real qc,
212  const amrex::Real qp,
213  const amrex::Real rho,
214  const amrex::Real pressure_current_units,
215  const amrex::Real qsat,
216  const amrex::Real dtqsat,
217  const amrex::Real dt,
218  const bool do_cond,
219  const amrex::Real latent_over_cp) noexcept
220 {
221  // Units must match current AdvanceKessler: q* are mass fractions, rho is in
222  // the production density units currently carried by Kessler, pressure is in
223  // Kessler's mbar / hPa path for qsat helpers, and dt is in seconds.
224  const KesslerSaturationAdjustment saturation_adjustment =
225  kessler_saturation_adjustment(qv, qc, qsat, dtqsat, do_cond, latent_over_cp);
226  KesslerSourceTerms source_terms{
227  saturation_adjustment.dq_vapor_to_cloud,
228  saturation_adjustment.dq_cloud_to_vapor,
229  amrex::Real(0),
230  amrex::Real(0)};
231 
232  const amrex::Real capacity = amrex::max(
233  amrex::Real(0), (qsat - qv) / (amrex::Real(1) + latent_over_cp * dtqsat));
234  const amrex::Real remaining_capacity = amrex::max(
235  amrex::Real(0), capacity - saturation_adjustment.dq_cloud_to_vapor);
236  // Cloud evaporation gets first claim on the linearized subsaturation
237  // capacity. Rain evaporation can use only the remaining capacity.
238  const amrex::Real qv_eff = qv
239  - saturation_adjustment.dq_vapor_to_cloud
240  + saturation_adjustment.dq_cloud_to_vapor;
241 
242  if ((qp > amrex::Real(0)) && (qsat > amrex::Real(0))
243  && (pressure_current_units > amrex::Real(0))
244  && (qv_eff < qsat) && (remaining_capacity > amrex::Real(0))) {
245  // MUST MATCH: current AdvanceKessler rain-evaporation coefficients and exponents.
246  const amrex::Real coeff = amrex::Real(1.6)
247  + amrex::Real(124.9) * std::pow(amrex::Real(0.001) * rho * qp, amrex::Real(0.2046));
248  const amrex::Real raw_rain_to_vapor = amrex::Real(1) / (amrex::Real(0.001) * rho)
249  * (amrex::Real(1) - qv_eff / qsat)
250  * coeff
251  * std::pow(amrex::Real(0.001) * rho * qp, amrex::Real(0.525))
252  / (amrex::Real(5.4e5) + amrex::Real(2.55e6) / (pressure_current_units * qsat))
253  * dt;
254  source_terms.dq_rain_to_vapor = amrex::min(qp, amrex::min(raw_rain_to_vapor, remaining_capacity));
255  }
256 
257  if (qc > amrex::Real(0)) {
258  const amrex::Real qcc = qc;
259  // Cloud-to-rain conversion is capped by cloud water available after
260  // saturation adjustment, not by the original qc.
261  const amrex::Real available_cloud = amrex::max(
262  amrex::Real(0), qc + saturation_adjustment.dq_vapor_to_cloud - saturation_adjustment.dq_cloud_to_vapor);
263  amrex::Real auto_r = amrex::Real(0);
264  if (qcc > qcw0) {
265  // MUST MATCH: current AdvanceKessler autoconversion trigger.
266  auto_r = alphaelq;
267  }
268 
269  // MUST MATCH: current AdvanceKessler accretion coefficient and exponent.
270  amrex::Real accrr = amrex::Real(0);
271  accrr = amrex::Real(2.2) * std::pow(qp, amrex::Real(0.875));
272  source_terms.dq_cloud_to_rain = dt * (accrr * qcc + auto_r * (qcc - qcw0));
273  source_terms.dq_cloud_to_rain = amrex::min(source_terms.dq_cloud_to_rain, available_cloud);
274  }
275 
276  return source_terms;
277 }
278 
279 #endif
static constexpr int kessler_max_sedimentation_substeps
Definition: ERF_KesslerUtils.H:34
static constexpr amrex::Real kessler_sedimentation_cfl_max
Definition: ERF_KesslerUtils.H:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE KesslerSaturationAdjustment kessler_saturation_adjustment(const amrex::Real qv, const amrex::Real qc, const amrex::Real qsat, const amrex::Real dtqsat, const bool do_cond, const amrex::Real latent_over_cp) noexcept
Definition: ERF_KesslerUtils.H:142
static constexpr amrex::Real kessler_water_depth_m_per_kg_m2
Definition: ERF_KesslerUtils.H:35
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_sizing_detJ(const int k, const int k_lo, const int k_hi, const amrex::Real detJ_donor, const amrex::Real detJ_below) noexcept
Definition: ERF_KesslerUtils.H:109
static constexpr amrex::Real kessler_millimeters_per_meter
Definition: ERF_KesslerUtils.H:36
static constexpr amrex::Real kessler_vt_density_exponent
Definition: ERF_KesslerUtils.H:43
static constexpr amrex::Real kessler_vt_rho_to_cgs
Definition: ERF_KesslerUtils.H:40
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE KesslerSourceTerms kessler_warm_rain_sources(const amrex::Real qv, const amrex::Real qc, const amrex::Real qp, const amrex::Real rho, const amrex::Real pressure_current_units, const amrex::Real qsat, const amrex::Real dtqsat, const amrex::Real dt, const bool do_cond, const amrex::Real latent_over_cp) noexcept
Definition: ERF_KesslerUtils.H:210
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_fall_courant(const amrex::Real terminal_velocity, const amrex::Real dt, const amrex::Real dz, const amrex::Real detJ_donor) noexcept
Definition: ERF_KesslerUtils.H:98
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_limit_precip_flux(const amrex::Real flux, const amrex::Real donor_rho, const amrex::Real donor_qp, const amrex::Real donor_detJ, const amrex::Real coef) noexcept
Definition: ERF_KesslerUtils.H:71
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_sedimentation_tendency(const amrex::Real fz_hi, const amrex::Real fz_lo, const amrex::Real rho, const amrex::Real dJinv, const amrex::Real coef) noexcept
Definition: ERF_KesslerUtils.H:199
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_terminal_velocity(const amrex::Real rho, const amrex::Real qp) noexcept
Definition: ERF_KesslerUtils.H:53
static constexpr amrex::Real kessler_vt_coeff
Definition: ERF_KesslerUtils.H:39
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_rain_accumulation_increment(const amrex::Real precip_mass_per_area) noexcept
Definition: ERF_KesslerUtils.H:125
static constexpr amrex::Real kessler_vt_exponent
Definition: ERF_KesslerUtils.H:41
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE KesslerFaceState kessler_face_state(const int k, const int k_hi, const amrex::Real rho_km1, const amrex::Real rho_k, const amrex::Real, const amrex::Real qp_k) noexcept
Definition: ERF_KesslerUtils.H:167
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real kessler_precip_flux(const amrex::Real rho, const amrex::Real terminal_velocity, const amrex::Real qp) noexcept
Definition: ERF_KesslerUtils.H:63
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int kessler_num_sedimentation_substeps(const amrex::Real max_courant) noexcept
Definition: ERF_KesslerUtils.H:83
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool kessler_is_small_sedimentation_value(const amrex::Real value) noexcept
Definition: ERF_KesslerUtils.H:135
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int kessler_face_donor_k(const int k, const int k_hi) noexcept
Definition: ERF_KesslerUtils.H:46
static constexpr amrex::Real kessler_vt_rho0
Definition: ERF_KesslerUtils.H:42
Physical constants and tuning parameters used only by the moisture and cloud-physics code.
constexpr amrex::Real alphaelq
Definition: ERF_MicrophysicsConstants.H:78
constexpr amrex::Real qcw0
Definition: ERF_MicrophysicsConstants.H:76
constexpr amrex::Real rhor
Definition: ERF_MicrophysicsConstants.H:39
Dimensionless numeric literals and pure mathematical constants.
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
@ qp
Definition: ERF_Kessler.H:34
@ rho
Definition: ERF_Kessler.H:25
@ qv
Definition: ERF_Kessler.H:31
@ qc
Definition: ERF_Morrison.H:110
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
Definition: ERF_KesslerUtils.H:27
amrex::Real rho
Definition: ERF_KesslerUtils.H:28
amrex::Real qp
Definition: ERF_KesslerUtils.H:29
Definition: ERF_KesslerUtils.H:22
amrex::Real dq_cloud_to_vapor
Definition: ERF_KesslerUtils.H:24
amrex::Real dq_vapor_to_cloud
Definition: ERF_KesslerUtils.H:23
Definition: ERF_KesslerUtils.H:15
amrex::Real dq_rain_to_vapor
Definition: ERF_KesslerUtils.H:19
amrex::Real dq_cloud_to_vapor
Definition: ERF_KesslerUtils.H:17
amrex::Real dq_cloud_to_rain
Definition: ERF_KesslerUtils.H:18
amrex::Real dq_vapor_to_cloud
Definition: ERF_KesslerUtils.H:16