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 <cmath>
9 #include <vector>
10 #include <AMReX_REAL.H>
11 #include <AMReX_Array.H>
12 #include <ERF_Constants.H>
13 
14 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
15 amrex::Real erf_gammafff (amrex::Real x){
16  return std::exp(lgamma(x));
17 }
18 
19 // From Flatau et al. (1992):
20 // https://doi.org/10.1175/1520-0450(1992)031<1507:PFTSVP>2.0.CO;2
21 // Coefficients come from Table 4 and the data is valid over a
22 // temperature range of [-90 0] C. Return 0 if above this temp range.
23 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
24 amrex::Real erf_esati (amrex::Real t) {
25  amrex::Real const a0 = 6.11147274;
26  amrex::Real const a1 = 0.503160820;
27  amrex::Real const a2 = 0.188439774e-1;
28  amrex::Real const a3 = 0.420895665e-3;
29  amrex::Real const a4 = 0.615021634e-5;
30  amrex::Real const a5 = 0.602588177e-7;
31  amrex::Real const a6 = 0.385852041e-9;
32  amrex::Real const a7 = 0.146898966e-11;
33  amrex::Real const a8 = 0.252751365e-14;
34 
35  amrex::Real dtt = t-273.16;
36  AMREX_ALWAYS_ASSERT(dtt>-85);
37 
38  amrex::Real esati;
39  if (dtt > 0.0) {
40  esati = 0.0;
41  } else {
42  esati = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
43  }
44  return esati;
45 }
46 
47 // From Clausius-Clapeyron
48 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
49 amrex::Real erf_esatw_cc (amrex::Real t) {
50  constexpr amrex::Real svp1 = 0.6112;
51  constexpr amrex::Real svp2 = 17.67;
52  constexpr amrex::Real svp3 = 29.65;
53  constexpr amrex::Real svpt0 = 273.15;
54  // NOTE: units of
55  amrex::Real esatw = 10.0 * svp1 * std::exp(svp2 * (t - svpt0) / (t - svp3));
56  return esatw;
57 }
58 
59 // From Flatau et al. (1992):
60 // https://doi.org/10.1175/1520-0450(1992)031<1507:PFTSVP>2.0.CO;2
61 // Coefficients come from Table 4 and the data is valid over a
62 // temperature range of [-85 70] C. Assert we are in this temp range.
63 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
64 amrex::Real erf_esatw (amrex::Real t) {
65  amrex::Real const a0 = 6.11239921;
66  amrex::Real const a1 = 0.443987641;
67  amrex::Real const a2 = 0.142986287e-1;
68  amrex::Real const a3 = 0.264847430e-3;
69  amrex::Real const a4 = 0.302950461e-5;
70  amrex::Real const a5 = 0.206739458e-7;
71  amrex::Real const a6 = 0.640689451e-10;
72  amrex::Real const a7 = -0.952447341e-13;
73  amrex::Real const a8 = -0.976195544e-15;
74 
75  amrex::Real dtt = t-273.16;
76  amrex::Real esatw;
77  if (dtt>-85 && dtt<70.0) {
78  esatw = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
79  } else {
80  esatw = erf_esatw_cc(t);
81  }
82  return esatw;
83 }
84 
85 // From Flatau et al. (1992):
86 // https://doi.org/10.1175/1520-0450(1992)031<1507:PFTSVP>2.0.CO;2
87 // Coefficients come from Table 4 and the data is valid over a
88 // temperature range of [-90 0] C. Return 0 if above this temp range.
89 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
90 amrex::Real erf_dtesati (amrex::Real t) {
91  amrex::Real const a0 = 0.503223089;
92  amrex::Real const a1 = 0.377174432e-1;
93  amrex::Real const a2 = 0.126710138e-2;
94  amrex::Real const a3 = 0.249065913e-4;
95  amrex::Real const a4 = 0.312668753e-6;
96  amrex::Real const a5 = 0.255653718e-8;
97  amrex::Real const a6 = 0.132073448e-10;
98  amrex::Real const a7 = 0.390204672e-13;
99  amrex::Real const a8 = 0.497275778e-16;
100 
101  amrex::Real dtt = t-273.16;
102  AMREX_ALWAYS_ASSERT(dtt>-85);
103  amrex::Real dtesati;
104  if (dtt > 0.0) {
105  dtesati = 0.0;
106  } else {
107  dtesati = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
108  }
109  return dtesati;
110 }
111 
112 // From Clausius-Clapeyron
113 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
114 amrex::Real erf_dtesatw_cc (amrex::Real t) {
115  constexpr amrex::Real svp1 = 0.6112;
116  constexpr amrex::Real svp2 = 17.67;
117  constexpr amrex::Real svp3 = 29.65;
118  constexpr amrex::Real svpt0 = 273.15;
119  amrex::Real dtesatw = 10.0 * svp1 * svp2 * std::exp(svp2 * (t - svpt0) / (t - svp3))
120  * (svpt0 - svp3) / ((t - svp3) * (t - svp3));
121  return dtesatw;
122 }
123 
124 // From Flatau et al. (1992):
125 // https://doi.org/10.1175/1520-0450(1992)031<1507:PFTSVP>2.0.CO;2
126 // Coefficients come from Table 4 and the data is valid over a
127 // temperature range of [-85 70] C. Assert we are in this temp range.
128 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
129 amrex::Real erf_dtesatw (amrex::Real t) {
130  amrex::Real const a0 = 0.443956472;
131  amrex::Real const a1 = 0.285976452e-1;
132  amrex::Real const a2 = 0.794747212e-3;
133  amrex::Real const a3 = 0.121167162e-4;
134  amrex::Real const a4 = 0.103167413e-6;
135  amrex::Real const a5 = 0.385208005e-9;
136  amrex::Real const a6 = -0.604119582e-12;
137  amrex::Real const a7 = -0.792933209e-14;
138  amrex::Real const a8 = -0.599634321e-17;
139 
140  amrex::Real dtt = t-273.16;
141  amrex::Real dtesatw;
142  if (dtt>-85.0 && dtt<70.) {
143  dtesatw = a0 + dtt*(a1+dtt*(a2+dtt*(a3+dtt*(a4+dtt*(a5+dtt*(a6+dtt*(a7+a8*dtt)))))));
144  } else {
145  dtesatw = erf_dtesatw_cc(t);
146  }
147  return dtesatw;
148 }
149 
150 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
151 void erf_qsati (amrex::Real t, amrex::Real p, amrex::Real &qsati) {
152  amrex::Real esati;
153  esati = erf_esati(t);
154  qsati = Rd_on_Rv*esati/std::max(esati,p-esati);
155 }
156 
157 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
158 void erf_qsatw (amrex::Real t, amrex::Real p, amrex::Real &qsatw) {
159  amrex::Real esatw;
160  esatw = erf_esatw(t);
161  qsatw = Rd_on_Rv*esatw/std::max(esatw,p-esatw);
162 }
163 
164 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
165 void erf_dtqsati (amrex::Real t, amrex::Real p, amrex::Real &dtqsati) {
166  dtqsati = Rd_on_Rv*erf_dtesati(t)/p;
167 }
168 
169 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
170 void erf_dtqsatw (amrex::Real t, amrex::Real p, amrex::Real &dtqsatw) {
171  dtqsatw = Rd_on_Rv*erf_dtesatw(t)/p;
172 }
173 
174 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
175 void z0_est (amrex::Real z, amrex::Real bflx, amrex::Real wnd, amrex::Real ustar, amrex::Real &z0) {
176  amrex::Real vonk = 0.4;
177  amrex::Real eps = 1.0e-10;
178  amrex::Real am = 4.8;
179  amrex::Real bm = 19.3;
180  amrex::Real c1 = 3.14159/2.0 - 3.0*log(2.0);
181  amrex::Real rlmo = -bflx*vonk/(ustar*ustar*ustar+eps);
182  amrex::Real zeta = std::min(1.0,z*rlmo);
183  amrex::Real x;
184  amrex::Real psi1;
185  if(zeta >= 0.0) {
186  psi1 = -am*zeta;
187  }
188  else {
189  x = std::sqrt(sqrt(1.0-bm*zeta));
190  psi1 = 2.0*std::log(1.0+x) + std::log(1.0+x*x) -2.0*std::atan(x) + c1;
191  }
192  amrex::Real lnz = std::max(0.0, vonk*wnd/(ustar+eps) +psi1);
193  z0 = z*std::exp(-lnz);
194 }
195 
196 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
197 amrex::Real term_vel_qp (amrex::Real qploc,
198  amrex::Real vrain,
199  amrex::Real vsnow,
200  amrex::Real vgrau,
201  amrex::Real rho,
202  amrex::Real tabs)
203 {
204  amrex::Real term_vel = 0.0;
205  if(qploc > qp_threshold) {
206  amrex::Real omp = std::max(0.0,std::min(1.0,(tabs-tprmin)*a_pr));
207  amrex::Real omg = std::max(0.0,std::min(1.0,(tabs-tgrmin)*a_gr));
208  amrex::Real qrr = omp*qploc;
209  amrex::Real qss = (1.0-omp)*(1.0-omg)*qploc;
210  amrex::Real qgg = (1.0-omp)*(omg)*qploc;
211  term_vel = omp*vrain*std::pow(rho*qrr,crain)
212  + (1.0-omp)*( (1.0-omg)*vsnow*std::pow(rho*qss,csnow)
213  + omg *vgrau*std::pow(rho*qgg,cgrau) );
214  }
215  return term_vel;
216 }
217 
218 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
219 amrex::Real pp (amrex::Real y) {
220  return std::max(0.0,y);
221 }
222 
223 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
224 amrex::Real pn (amrex::Real y) {
225  return -std::min(0.0,y);
226 }
227 #endif
constexpr amrex::Real csnow
Definition: ERF_Constants.H:82
constexpr amrex::Real a_gr
Definition: ERF_Constants.H:79
constexpr amrex::Real Rd_on_Rv
Definition: ERF_Constants.H:87
constexpr amrex::Real cgrau
Definition: ERF_Constants.H:83
constexpr amrex::Real tprmin
Definition: ERF_Constants.H:33
constexpr amrex::Real qp_threshold
Definition: ERF_Constants.H:60
constexpr amrex::Real crain
Definition: ERF_Constants.H:81
constexpr amrex::Real tgrmin
Definition: ERF_Constants.H:35
constexpr amrex::Real a_pr
Definition: ERF_Constants.H:78
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:64
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesatw(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:129
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_dtqsatw(amrex::Real t, amrex::Real p, amrex::Real &dtqsatw)
Definition: ERF_MicrophysicsUtils.H:170
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesati(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:90
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esati(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:24
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pn(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:224
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void z0_est(amrex::Real z, amrex::Real bflx, amrex::Real wnd, amrex::Real ustar, amrex::Real &z0)
Definition: ERF_MicrophysicsUtils.H:175
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_dtesatw_cc(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:114
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_dtqsati(amrex::Real t, amrex::Real p, amrex::Real &dtqsati)
Definition: ERF_MicrophysicsUtils.H:165
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real term_vel_qp(amrex::Real qploc, amrex::Real vrain, amrex::Real vsnow, amrex::Real vgrau, amrex::Real rho, amrex::Real tabs)
Definition: ERF_MicrophysicsUtils.H:197
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_qsatw(amrex::Real t, amrex::Real p, amrex::Real &qsatw)
Definition: ERF_MicrophysicsUtils.H:158
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_gammafff(amrex::Real x)
Definition: ERF_MicrophysicsUtils.H:15
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void erf_qsati(amrex::Real t, amrex::Real p, amrex::Real &qsati)
Definition: ERF_MicrophysicsUtils.H:151
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pp(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:219
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw_cc(amrex::Real t)
Definition: ERF_MicrophysicsUtils.H:49
@ tabs
Definition: ERF_Kessler.H:32
@ rho
Definition: ERF_Kessler.H:30