ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MicrophysicsUtils.H
Go to the documentation of this file.
1 /*
2  * utility tools for microphysics
3  *
4  */
5 #ifndef ERF_Microphysics_Utils_H
6 #define ERF_Microphysics_Utils_H
7 
8 #include <algorithm>
9 #include <cmath>
10 #include <limits>
11 #include <vector>
12 #include <AMReX_REAL.H>
13 #include <AMReX_Array.H>
14 #include <ERF_Constants.H>
15 #include "ERF_EOS.H"
16 
17 /**
18  * Thermodynamic state passed from ERF into a microphysics scheme.
19  * Pressure is always in Pa here; schemes with legacy mbar storage convert it
20  * once at their copy-in boundary.
21  */
23  amrex::Real pressure_pa;
24  amrex::Real temperature;
25 };
26 
27 /**
28  * Diagnose the state used by one microphysics cell.
29  *
30  * In anelastic and pseudo-incompressible mode, which get their pressure from a
31  * projection rather than from the EOS, the conserved state supplies theta while the
32  * hydrostatic reference pressure supplied by BaseState supplies the thermodynamic pressure.
33  * In compressible mode, retain ERF's existing local-EOS diagnosis.
34  */
35 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
37  const amrex::Real rho,
38  const amrex::Real rho_theta,
39  const amrex::Real qv,
40  const amrex::Real rdOcp,
41  const bool use_anelastic_reference_pressure,
42  const amrex::Real p0) noexcept
43 {
44  if (use_anelastic_reference_pressure) {
45  const amrex::Real theta = rho_theta / rho;
46  return {p0, getTgivenPandTh(p0, theta, rdOcp)};
47  }
48 
49  return {getPgivenRTh(rho_theta, qv),
50  getTgivenRandRTh(rho, rho_theta, qv)};
51 }
52 
53 // Positive-argument gamma wrapper. std::lgamma returns log(abs(Gamma(x))) for
54 // negative non-integers, so ERF keeps a positive-only contract here.
55 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
56 amrex::Real erf_gammafff (amrex::Real x){
57  AMREX_ALWAYS_ASSERT(x > amrex::Real(0));
58  return std::exp(std::lgamma(x));
59 }
60 
61 // Saturation vapor pressure of ice [mbar / hPa].
62 //
63 // Flatau et al. (1992), Polynomial Fits to Saturation Vapor Pressure,
64 // J. Appl. Meteorol. Coefficients come from Table 4 and the value fit is valid
65 // over [-90, 0] C, i.e. down to about 183.16 K. ERF intentionally clamps the
66 // above-freezing branch to 0.
67 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
68 amrex::Real erf_esati (amrex::Real t) {
69  amrex::Real const a0 = amrex::Real(6.11147274);
70  amrex::Real const a1 = amrex::Real(0.503160820);
71  amrex::Real const a2 = amrex::Real(0.188439774e-1);
72  amrex::Real const a3 = amrex::Real(0.420895665e-3);
73  amrex::Real const a4 = amrex::Real(0.615021634e-5);
74  amrex::Real const a5 = amrex::Real(0.602588177e-7);
75  amrex::Real const a6 = amrex::Real(0.385852041e-9);
76  amrex::Real const a7 = amrex::Real(0.146898966e-11);
77  amrex::Real const a8 = amrex::Real(0.252751365e-14);
78 
79  amrex::Real dtt = t-amrex::Real(273.16);
80  const amrex::Real tol = amrex::Real(16.0) * std::numeric_limits<amrex::Real>::epsilon();
81  AMREX_ALWAYS_ASSERT(dtt >= -amrex::Real(90.0) - tol);
82 
83  amrex::Real esati;
84  if (dtt > amrex::Real(0)) {
85  esati = amrex::Real(0);
86  } else {
87  esati = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
88  }
89  return esati;
90 }
91 
92 // Magnus-style exponential saturation-pressure approximation over water
93 // [mbar / hPa]. This closed-form formula is commonly motivated by
94 // simplified integrations of the Clausius-Clapeyron relation, but ERF
95 // evaluates the empirical exponential expression below rather than
96 // performing a direct Clausius-Clapeyron integration. The `_cc` suffix is
97 // retained for API compatibility.
98 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
99 amrex::Real erf_esatw_cc (amrex::Real t) {
100  constexpr amrex::Real svp1 = amrex::Real(0.6112);
101  constexpr amrex::Real svp2 = amrex::Real(17.67);
102  constexpr amrex::Real svp3 = amrex::Real(29.65);
103  constexpr amrex::Real svpt0 = amrex::Real(273.15);
104  amrex::Real esatw = amrex::Real(10.0) * svp1 * std::exp(svp2 * (t - svpt0) / (t - svp3));
105  return esatw;
106 }
107 
108 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
109 amrex::Real erf_esatw_flatau_poly (amrex::Real dtt)
110 {
111  amrex::Real const a0 = amrex::Real(6.11239921);
112  amrex::Real const a1 = amrex::Real(0.443987641);
113  amrex::Real const a2 = amrex::Real(0.142986287e-1);
114  amrex::Real const a3 = amrex::Real(0.264847430e-3);
115  amrex::Real const a4 = amrex::Real(0.302950461e-5);
116  amrex::Real const a5 = amrex::Real(0.206739458e-7);
117  amrex::Real const a6 = amrex::Real(0.640689451e-10);
118  amrex::Real const a7 = -amrex::Real(0.952447341e-13);
119  amrex::Real const a8 = -amrex::Real(0.976195544e-15);
120 
121  return a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
122 }
123 
124 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
125 amrex::Real erf_dtesatw_flatau_poly (amrex::Real dtt)
126 {
127  amrex::Real const a0 = amrex::Real(0.443956472);
128  amrex::Real const a1 = amrex::Real(0.285976452e-1);
129  amrex::Real const a2 = amrex::Real(0.794747212e-3);
130  amrex::Real const a3 = amrex::Real(0.121167162e-4);
131  amrex::Real const a4 = amrex::Real(0.103167413e-6);
132  amrex::Real const a5 = amrex::Real(0.385208005e-9);
133  amrex::Real const a6 = -amrex::Real(0.604119582e-12);
134  amrex::Real const a7 = -amrex::Real(0.792933209e-14);
135  amrex::Real const a8 = -amrex::Real(0.599634321e-17);
136 
137  return a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
138 }
139 
140 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
141 bool erf_use_positive_esatw_poly (amrex::Real t)
142 {
143  amrex::Real const dtt = t - amrex::Real(273.16);
144  amrex::Real const poly_min_dtt = -amrex::Real(70.0);
145  return dtt > poly_min_dtt && dtt < amrex::Real(70.0) &&
146  erf_esatw_flatau_poly(dtt) > amrex::Real(0.0);
147 }
148 
149 // Saturation vapor pressure of water vapor [mbar / hPa].
150 //
151 // Flatau et al. (1992), Polynomial Fits to Saturation Vapor Pressure,
152 // J. Appl. Meteorol. The default branch uses the Flatau polynomial only on the
153 // current positive in-range interval selected by erf_use_positive_esatw_poly,
154 // currently about [-70, 70] C. Outside that interval, or whenever the
155 // polynomial would be non-positive, it falls back to the Magnus-style
156 // exponential approximation. The optional empirical fit is originally in Pa;
157 // ERF converts it to mbar / hPa before returning so both warm-water branches
158 // use the same units.
159 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
160 amrex::Real erf_esatw (amrex::Real t, bool use_empirical=false)
161 {
162  if (use_empirical) {
163  return amrex::Real(0.01) *
164  std::exp(amrex::Real(34.494) - amrex::Real(4924.99)/(t - amrex::Real(273.15) + amrex::Real(237.1))) /
165  std::pow(t - amrex::Real(273.15) + amrex::Real(105.0), amrex::Real(1.57));
166  }
167 
169  return erf_esatw_flatau_poly(t - amrex::Real(273.16));
170  }
171 
172  return erf_esatw_cc(t);
173 }
174 
175 // Flatau et al. (1992), Polynomial Fits to Saturation Vapor Pressure,
176 // J. Appl. Meteorol. The derivative fit is valid over the narrower
177 // [-85, 0] C interval, i.e. down to about 188.16 K. ERF intentionally returns
178 // 0 above freezing to match erf_esati.
179 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
180 amrex::Real erf_dtesati (amrex::Real t) {
181  amrex::Real const a0 = amrex::Real(0.503223089);
182  amrex::Real const a1 = amrex::Real(0.377174432e-1);
183  amrex::Real const a2 = amrex::Real(0.126710138e-2);
184  amrex::Real const a3 = amrex::Real(0.249065913e-4);
185  amrex::Real const a4 = amrex::Real(0.312668753e-6);
186  amrex::Real const a5 = amrex::Real(0.255653718e-8);
187  amrex::Real const a6 = amrex::Real(0.132073448e-10);
188  amrex::Real const a7 = amrex::Real(0.390204672e-13);
189  amrex::Real const a8 = amrex::Real(0.497275778e-16);
190 
191  amrex::Real dtt = t-amrex::Real(273.16);
192  const amrex::Real tol = amrex::Real(16.0) * std::numeric_limits<amrex::Real>::epsilon();
193  AMREX_ALWAYS_ASSERT(dtt >= -amrex::Real(85.0) - tol);
194  amrex::Real dtesati;
195  if (dtt > amrex::Real(0)) {
196  dtesati = amrex::Real(0);
197  } else {
198  dtesati = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
199  }
200  return dtesati;
201 }
202 
203 // Temperature derivative of the Magnus-style exponential water
204 // saturation-pressure approximation [mbar / hPa / K]. This differentiates
205 // the closed-form formula in erf_esatw_cc.
206 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
207 amrex::Real erf_dtesatw_cc (amrex::Real t) {
208  constexpr amrex::Real svp1 = amrex::Real(0.6112);
209  constexpr amrex::Real svp2 = amrex::Real(17.67);
210  constexpr amrex::Real svp3 = amrex::Real(29.65);
211  constexpr amrex::Real svpt0 = amrex::Real(273.15);
212  amrex::Real dtesatw = amrex::Real(10.0) * svp1 * svp2 * std::exp(svp2 * (t - svpt0) / (t - svp3))
213  * (svpt0 - svp3) / ((t - svp3) * (t - svp3));
214  return dtesatw;
215 }
216 
217 // Flatau et al. (1992), Polynomial Fits to Saturation Vapor Pressure,
218 // J. Appl. Meteorol. Use the same branch selection as erf_esatw so the value
219 // and derivative stay on the same function. Small derivative jumps can remain
220 // at the switch because the Flatau polynomial and Magnus-style exponential
221 // approximation are independent fits rather than a matched composite model.
222 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
223 amrex::Real erf_dtesatw (amrex::Real t) {
225  return erf_dtesatw_flatau_poly(t - amrex::Real(273.16));
226  }
227 
228  return erf_dtesatw_cc(t);
229 }
230 
231 // Saturation mixing-ratio helper for a precomputed saturation pressure. qsat is
232 // capped at RdoRv whenever esat > p/2, i.e. when max(esat, p - esat) = esat.
233 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
234 amrex::Real erf_qsat_from_esat (amrex::Real esat, amrex::Real p)
235 {
236  return RdoRv * esat / std::max(esat, p - esat);
237 }
238 
239 // Temperature derivative of the capped saturation-mixing-ratio helper. The
240 // derivative is zero on the capped branch where qsat == RdoRv.
241 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
242 amrex::Real erf_dtqsat_from_esat (amrex::Real esat, amrex::Real dtesat, amrex::Real p)
243 {
244  if ((p - esat) >= esat) {
245  amrex::Real const denom = p - esat;
246  return RdoRv * dtesat * p / (denom * denom);
247  }
248 
249  return amrex::Real(0);
250 }
251 
252 // Saturated ice vapor mixing ratio [-]. Input temperature is in K and input
253 // pressure is in mbar. The result is capped at RdoRv when esat > p/2.
254 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
255 void erf_qsati (amrex::Real t, amrex::Real p, amrex::Real &qsati) {
257 }
258 
259 /* saturated water vapor mixing ratio
260  * t: temperature [K]
261  * p: pressure [mbar]
262  * return value is capped at RdoRv when esat > p/2.
263  */
264 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
265 void erf_qsatw (amrex::Real t, amrex::Real p, amrex::Real &qsatw) {
267 }
268 
269 // Temperature derivative of the capped erf_qsati relation. This is zero on the
270 // capped branch where qsat == RdoRv.
271 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
272 void erf_dtqsati (amrex::Real t, amrex::Real p, amrex::Real &dtqsati) {
273  amrex::Real esati = erf_esati(t);
274  amrex::Real dtesati = erf_dtesati(t);
275  dtqsati = erf_dtqsat_from_esat(esati, dtesati, p);
276 }
277 
278 // Temperature derivative of the capped erf_qsatw relation. This is zero on the
279 // capped branch where qsat == RdoRv.
280 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
281 void erf_dtqsatw (amrex::Real t, amrex::Real p, amrex::Real &dtqsatw) {
282  amrex::Real esatw = erf_esatw(t);
283  amrex::Real dtesatw = erf_dtesatw(t);
284  dtqsatw = erf_dtqsat_from_esat(esatw, dtesatw, p);
285 }
286 #endif
constexpr amrex::Real RdoRv
Definition: ERF_Constants.H:60
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getTgivenRandRTh(const amrex::Real rho, const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:46
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getTgivenPandTh(const amrex::Real P, const amrex::Real th, const amrex::Real rdOcp)
Definition: ERF_EOS.H:32
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
bool use_empirical
Definition: ERF_InitCustomPert_MultiSpeciesBubble.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool erf_use_positive_esatw_poly(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:141
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesatw(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:223
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_dtqsatw(amrex::Real t, amrex::Real p, amrex::Real &dtqsatw)
Definition: ERF_MicrophysicsUtils.H:281
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesati(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:180
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw_flatau_poly(amrex::Real dtt)
Definition: ERF_MicrophysicsUtils.H:109
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesatw_flatau_poly(amrex::Real dtt)
Definition: ERF_MicrophysicsUtils.H:125
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esati(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:68
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE MicrophysicsThermoState diagnose_microphysics_thermo_state(const amrex::Real rho, const amrex::Real rho_theta, const amrex::Real qv, const amrex::Real rdOcp, const bool use_anelastic_reference_pressure, const amrex::Real p0) noexcept
Definition: ERF_MicrophysicsUtils.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesatw_cc(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:207
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_qsat_from_esat(amrex::Real esat, amrex::Real p)
Definition: ERF_MicrophysicsUtils.H:234
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_dtqsati(amrex::Real t, amrex::Real p, amrex::Real &dtqsati)
Definition: ERF_MicrophysicsUtils.H:272
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_qsatw(amrex::Real t, amrex::Real p, amrex::Real &qsatw)
Definition: ERF_MicrophysicsUtils.H:265
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_gammafff(amrex::Real x)
Definition: ERF_MicrophysicsUtils.H:56
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtqsat_from_esat(amrex::Real esat, amrex::Real dtesat, amrex::Real p)
Definition: ERF_MicrophysicsUtils.H:242
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_qsati(amrex::Real t, amrex::Real p, amrex::Real &qsati)
Definition: ERF_MicrophysicsUtils.H:255
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw(amrex::Real t, bool use_empirical=false)
Definition: ERF_MicrophysicsUtils.H:160
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw_cc(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:99
@ rho
Definition: ERF_Kessler.H:25
@ theta
Definition: ERF_Kessler.H:26
@ qv
Definition: ERF_Kessler.H:31
@ qsatw
Definition: ERF_WSM6.H:363
@ qsati
Definition: ERF_WSM6.H:363
@ t
Definition: ERF_WSM6.H:295
@ p
Definition: ERF_WSM6.H:303
real(c_double), parameter svp1
Definition: ERF_module_model_constants.F90:78
real(c_double), parameter p0
Definition: ERF_module_model_constants.F90:40
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
real(c_double), parameter a2
Definition: ERF_module_model_constants.F90:95
real(c_double), parameter a3
Definition: ERF_module_model_constants.F90:96
real(c_double), parameter svp3
Definition: ERF_module_model_constants.F90:80
real(c_double), parameter svp2
Definition: ERF_module_model_constants.F90:79
real(c_double), parameter a4
Definition: ERF_module_model_constants.F90:97
real(c_double), parameter svpt0
Definition: ERF_module_model_constants.F90:81
Definition: ERF_MicrophysicsUtils.H:22
amrex::Real pressure_pa
Definition: ERF_MicrophysicsUtils.H:23
amrex::Real temperature
Definition: ERF_MicrophysicsUtils.H:24