-
Notifications
You must be signed in to change notification settings - Fork 11
/
turbulence_functions.pyx
377 lines (292 loc) · 11.9 KB
/
turbulence_functions.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import numpy as np
cimport numpy as np
from libc.math cimport cbrt, sqrt, log, fabs,atan, exp, fmax, pow, fmin, tanh
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
include "parameters.pxi"
from thermodynamic_functions cimport *
# Entrainment Rates
cdef entr_struct entr_detr_dry(entr_in_struct entr_in)nogil:
cdef entr_struct _ret
cdef double eps = 1.0 # to avoid division by zero when z = 0 or z_i
# Following Soares 2004
_ret.entr_sc = 0.5*(1.0/entr_in.z + 1.0/fmax(entr_in.zi - entr_in.z, 10.0)) #vkb/(z + 1.0e-3)
_ret.detr_sc = 0.0
return _ret
cdef entr_struct entr_detr_inverse_z(entr_in_struct entr_in) nogil:
cdef:
entr_struct _ret
_ret.entr_sc = vkb/entr_in.z
_ret.detr_sc= 0.0
return _ret
cdef entr_struct entr_detr_inverse_w(entr_in_struct entr_in) nogil:
cdef:
entr_struct _ret
eps_w = 1.0/(fmax(fabs(entr_in.w),1.0)* 500)
if entr_in.af>0.0:
partiation_func = entr_detr_buoyancy_sorting(entr_in)
_ret.entr_sc = partiation_func*eps_w/2.0
_ret.detr_sc = (1.0-partiation_func/2.0)*eps_w
else:
_ret.entr_sc = 0.0
_ret.detr_sc = 0.0
return _ret
cdef double entr_detr_buoyancy_sorting(entr_in_struct entr_in) nogil:
cdef:
Py_ssize_t m_q, m_h
#double[:] inner
int i_b
double h_hat, qt_hat, sd_h, sd_q, corr, mu_h_star, sigma_h_star, qt_var
double sqpi_inv = 1.0/sqrt(pi)
double sqrt2 = sqrt(2.0)
double sd_q_lim, bmix, qv_
double partiation_func = 0.0
double inner_partiation_func = 0.0
eos_struct sa
double [:] weights
double [:] abscissas
with gil:
abscissas, weights = np.polynomial.hermite.hermgauss(entr_in.quadrature_order)
if entr_in.env_QTvar != 0.0 and entr_in.env_Hvar != 0.0:
sd_q = sqrt(entr_in.env_QTvar)
sd_h = sqrt(entr_in.env_Hvar)
corr = fmax(fmin(entr_in.env_HQTcov/fmax(sd_h*sd_q, 1e-13),1.0),-1.0)
# limit sd_q to prevent negative qt_hat
sd_q_lim = (1e-10 - entr_in.qt_env)/(sqrt2 * abscissas[0])
sd_q = fmin(sd_q, sd_q_lim)
qt_var = sd_q * sd_q
sigma_h_star = sqrt(fmax(1.0-corr*corr,0.0)) * sd_h
for m_q in xrange(entr_in.quadrature_order):
qt_hat = (entr_in.qt_env + sqrt2 * sd_q * abscissas[m_q] + entr_in.qt_up)/2.0
mu_h_star = entr_in.H_env + sqrt2 * corr * sd_h * abscissas[m_q]
inner_partiation_func = 0.0
for m_h in xrange(entr_in.quadrature_order):
h_hat = (sqrt2 * sigma_h_star * abscissas[m_h] + mu_h_star + entr_in.H_up)/2.0
# condensation
sa = eos(t_to_thetali_c, eos_first_guess_thetal, entr_in.p0, qt_hat, h_hat)
# calcualte buoyancy
qv_ = qt_hat - sa.ql
alpha_mix = alpha_c(entr_in.p0, sa.T, qt_hat, qv_)
bmix = buoyancy_c(entr_in.alpha0, alpha_mix) - entr_in.b_mean
# sum only the points with positive buoyancy to get the buoyant fraction
if bmix >0.0:
inner_partiation_func += weights[m_h] * sqpi_inv
partiation_func += inner_partiation_func * weights[m_q] * sqpi_inv
else:
h_hat = ( entr_in.H_env + entr_in.H_up)/2.0
qt_hat = ( entr_in.qt_env + entr_in.qt_up)/2.0
# condensation
sa = eos(t_to_thetali_c, eos_first_guess_thetal, entr_in.p0, qt_hat, h_hat)
# calcualte buoyancy
alpha_mix = alpha_c(entr_in.p0, sa.T, qt_hat, qt_hat - sa.ql)
bmix = buoyancy_c(entr_in.alpha0, alpha_mix) - entr_in.b_mean
return partiation_func
cdef entr_struct entr_detr_tke2(entr_in_struct entr_in) nogil:
cdef entr_struct _ret
# in cloud portion from Soares 2004
if entr_in.z >= entr_in.zi :
_ret.detr_sc= 3.0e-3
else:
_ret.detr_sc = 0.0
_ret.entr_sc = (0.05 * sqrt(entr_in.tke) / fmax(entr_in.w, 0.01) / fmax(entr_in.af, 0.001) / fmax(entr_in.z, 1.0))
return _ret
# yair - this is a new entr-detr function that takes entr as proportional to TKE/w and detr ~ b/w2
cdef entr_struct entr_detr_tke(entr_in_struct entr_in) nogil:
cdef entr_struct _ret
_ret.detr_sc = fabs(entr_in.b)/ fmax(entr_in.w * entr_in.w, 1e-3)
_ret.entr_sc = sqrt(entr_in.tke) / fmax(entr_in.w, 0.01) / fmax(sqrt(entr_in.af), 0.001) / 50000.0
return _ret
cdef entr_struct entr_detr_b_w2(entr_in_struct entr_in) nogil:
cdef :
entr_struct _ret
double effective_buoyancy
# in cloud portion from Soares 2004
if entr_in.z >= entr_in.zi :
_ret.detr_sc= 4.0e-3 + 0.12 *fabs(fmin(entr_in.b,0.0)) / fmax(entr_in.w * entr_in.w, 1e-2)
else:
_ret.detr_sc = 0.0
_ret.entr_sc = 0.12 * fmax(entr_in.b,0.0) / fmax(entr_in.w * entr_in.w, 1e-2)
return _ret
cdef entr_struct entr_detr_suselj(entr_in_struct entr_in) nogil:
cdef:
entr_struct _ret
double entr_dry = 2.5e-3
double l0
l0 = (entr_in.zbl - entr_in.zi)/10.0
if entr_in.z >= entr_in.zi :
_ret.detr_sc= 4.0e-3 + 0.12* fabs(fmin(entr_in.b,0.0)) / fmax(entr_in.w * entr_in.w, 1e-2)
_ret.entr_sc = 0.1 / entr_in.dz * entr_in.poisson
else:
_ret.detr_sc = 0.0
_ret.entr_sc = 0.0 #entr_dry # Very low entrainment rate needed for Dycoms to work
return _ret
cdef entr_struct entr_detr_none(entr_in_struct entr_in)nogil:
cdef entr_struct _ret
_ret.entr_sc = 0.0
_ret.detr_sc = 0.0
return _ret
cdef evap_struct evap_sat_adjust(double p0, double thetal_, double qt_mix) nogil:
cdef:
evap_struct evap
double ql_1, T_2, ql_2, f_1, f_2, qv_mix, T_1
qv_mix = qt_mix
ql = 0.0
pv_1 = pv_c(p0,qt_mix,qt_mix)
pd_1 = p0 - pv_1
# evaporate and cool
T_1 = eos_first_guess_thetal(thetal_, pd_1, pv_1, qt_mix)
pv_star_1 = pv_star(T_1)
qv_star_1 = qv_star_c(p0,qt_mix,pv_star_1)
if(qt_mix <= qv_star_1):
evap.T = T_1
evap.ql = 0.0
else:
ql_1 = qt_mix - qv_star_1
prog_1 = t_to_thetali_c(p0, T_1, qt_mix, ql_1, 0.0)
f_1 = thetal_ - prog_1
T_2 = T_1 + ql_1 * latent_heat(T_1) /((1.0 - qt_mix)*cpd + qv_star_1 * cpv)
delta_T = fabs(T_2 - T_1)
while delta_T > 1.0e-3 or ql_2 < 0.0:
pv_star_2 = pv_star(T_2)
qv_star_2 = qv_star_c(p0,qt_mix,pv_star_2)
pv_2 = pv_c(p0, qt_mix, qv_star_2)
pd_2 = p0 - pv_2
ql_2 = qt_mix - qv_star_2
prog_2 = t_to_thetali_c(p0,T_2,qt_mix, ql_2, 0.0)
f_2 = thetal_ - prog_2
T_n = T_2 - f_2*(T_2 - T_1)/(f_2 - f_1)
T_1 = T_2
T_2 = T_n
f_1 = f_2
delta_T = fabs(T_2 - T_1)
evap.T = T_2
qv = qv_star_2
evap.ql = ql_2
return evap
# convective velocity scale
cdef double get_wstar(double bflux, double zi ):
return cbrt(fmax(bflux * zi, 0.0))
# BL height
cdef double get_inversion(double *theta_rho, double *u, double *v, double *z_half,
Py_ssize_t kmin, Py_ssize_t kmax, double Ri_bulk_crit):
cdef:
double theta_rho_b = theta_rho[kmin]
double h, Ri_bulk=0.0, Ri_bulk_low = 0.0
Py_ssize_t k = kmin
# test if we need to look at the free convective limit
if (u[kmin] * u[kmin] + v[kmin] * v[kmin]) <= 0.01:
with nogil:
for k in xrange(kmin,kmax):
if theta_rho[k] > theta_rho_b:
break
h = (z_half[k] - z_half[k-1])/(theta_rho[k] - theta_rho[k-1]) * (theta_rho_b - theta_rho[k-1]) + z_half[k-1]
else:
with nogil:
for k in xrange(kmin,kmax):
Ri_bulk_low = Ri_bulk
Ri_bulk = g * (theta_rho[k] - theta_rho_b) * z_half[k]/theta_rho_b / (u[k] * u[k] + v[k] * v[k])
if Ri_bulk > Ri_bulk_crit:
break
h = (z_half[k] - z_half[k-1])/(Ri_bulk - Ri_bulk_low) * (Ri_bulk_crit - Ri_bulk_low) + z_half[k-1]
return h
# Teixiera convective tau
cdef double get_mixing_tau(double zi, double wstar) nogil:
# return 0.5 * zi / wstar
#return zi / (fmax(wstar, 1e-5))
return zi / (wstar + 0.001)
# MO scaling of near surface tke and scalar variance
cdef double get_surface_tke(double ustar, double wstar, double zLL, double oblength) nogil:
if oblength < 0.0:
return ((3.75 + cbrt(zLL/oblength * zLL/oblength)) * ustar * ustar + 0.2 * wstar * wstar)
else:
return (3.75 * ustar * ustar)
cdef double get_surface_variance(double flux1, double flux2, double ustar, double zLL, double oblength) nogil:
cdef:
double c_star1 = -flux1/ustar
double c_star2 = -flux2/ustar
if oblength < 0.0:
return 4.0 * c_star1 * c_star2 * pow(1.0 - 8.3 * zLL/oblength, -2.0/3.0)
else:
return 4.0 * c_star1 * c_star2
# Math-y stuff
cdef void construct_tridiag_diffusion(Py_ssize_t nzg, Py_ssize_t gw, double dzi, double dt,
double *rho_ae_K_m, double *rho, double *ae, double *a, double *b, double *c):
cdef:
Py_ssize_t k
double X, Y, Z #
Py_ssize_t nz = nzg - 2* gw
with nogil:
for k in xrange(gw,nzg-gw):
X = rho[k] * ae[k]/dt
Y = rho_ae_K_m[k] * dzi * dzi
Z = rho_ae_K_m[k-1] * dzi * dzi
if k == gw:
Z = 0.0
elif k == nzg-gw-1:
Y = 0.0
a[k-gw] = - Z/X
b[k-gw] = 1.0 + Y/X + Z/X
c[k-gw] = -Y/X
return
cdef void construct_tridiag_diffusion_implicitMF(Py_ssize_t nzg, Py_ssize_t gw, double dzi, double dt,
double *rho_ae_K_m, double *massflux, double *rho, double *alpha, double *ae, double *a, double *b, double *c):
cdef:
Py_ssize_t k
double X, Y, Z #
Py_ssize_t nz = nzg - 2* gw
with nogil:
for k in xrange(gw,nzg-gw):
X = rho[k] * ae[k]/dt
Y = rho_ae_K_m[k] * dzi * dzi
Z = rho_ae_K_m[k-1] * dzi * dzi
if k == gw:
Z = 0.0
elif k == nzg-gw-1:
Y = 0.0
a[k-gw] = - Z/X + 0.5 * massflux[k-1] * dt * dzi/rho[k]
b[k-gw] = 1.0 + Y/X + Z/X + 0.5 * dt * dzi * (massflux[k-1]-massflux[k])/rho[k]
c[k-gw] = -Y/X - 0.5 * dt * dzi * massflux[k]/rho[k]
return
cdef void construct_tridiag_diffusion_dirichlet(Py_ssize_t nzg, Py_ssize_t gw, double dzi, double dt,
double *rho_ae_K_m, double *rho, double *ae, double *a, double *b, double *c):
cdef:
Py_ssize_t k
double X, Y, Z #
Py_ssize_t nz = nzg - 2* gw
with nogil:
for k in xrange(gw,nzg-gw):
X = rho[k] * ae[k]/dt
Y = rho_ae_K_m[k] * dzi * dzi
Z = rho_ae_K_m[k-1] * dzi * dzi
if k == gw:
Z = 0.0
Y = 0.0
elif k == nzg-gw-1:
Y = 0.0
a[k-gw] = - Z/X
b[k-gw] = 1.0 + Y/X + Z/X
c[k-gw] = -Y/X
return
cdef void tridiag_solve(Py_ssize_t nz, double *x, double *a, double *b, double *c):
cdef:
double * scratch = <double*> PyMem_Malloc(nz * sizeof(double))
Py_ssize_t i
double m
scratch[0] = c[0]/b[0]
x[0] = x[0]/b[0]
with nogil:
for i in xrange(1,nz):
m = 1.0/(b[i] - a[i] * scratch[i-1])
scratch[i] = c[i] * m
x[i] = (x[i] - a[i] * x[i-1])*m
for i in xrange(nz-2,-1,-1):
x[i] = x[i] - scratch[i] * x[i+1]
PyMem_Free(scratch)
return
# Dustbin
cdef bint set_cloudbase_flag(double ql, bint current_flag) nogil:
cdef bint new_flag
if ql > 1.0e-8:
new_flag = True
else:
new_flag = current_flag
return new_flag