53 def _k_diff(self, theta, rv, u, v, ctx, sfth, sfrv, sfu, sfv, timestep):
55 K-gradient diffusion tendencies for all variables in one pass.
56 Uses an implicit tridiagonal solver following PHYEX's TRIDIAG_THERMO.
60 theta : ndarray (nz,) — potential temperature
61 rv : ndarray (nz,) — water vapour mixing ratio
62 u, v : ndarray (nz,) — wind components
63 ctx : dict — context from _preparation
64 sfth, sfrv, sfu, sfv : float — surface fluxes
65 timestep : float — time step (s)
69 tuple of ndarray — (tend_theta, tend_rv, tend_u, tend_v)
72 dz_mass = ctx[
'dz_mass']
73 z_flux = ctx[
'z_flux']
77 if self.
grid.ascending:
81 height = numpy.abs(z_flux - z_ground)
85 l = self.
cst.Karman * height[1:-1] / (1. + self.
cst.Karman * height[1:-1] / lasymp)
88 du_dz = ((u[1:] - u[:-1]) / dz_mass) ** 2 + \
89 ((v[1:] - v[:-1]) / dz_mass) ** 2
90 du_dz = numpy.sqrt(du_dz)
93 theta_v = theta * (1.0 + (self.
cst.Rv / self.
cst.Rd - 1.0) * rv)
94 theta_v_flux = 0.5 * (theta_v[1:] + theta_v[:-1])
97 N2 = (self.
cst.g / theta_v_flux) * (theta_v[1:] - theta_v[:-1]) / dz_mass
100 Ri = numpy.maximum(N2, 0.0) / (du_dz**2 + 1e-10)
103 f_Ri = 1.0 / (1.0 + 10.0 * Ri)**2
105 K = l**2 * numpy.abs(du_dz) * f_Ri
106 K = numpy.maximum(K, 1e-4)
107 K = numpy.minimum(K, 200)
109 rhodJ = ctx[
'rhod'] * dzz
112 MZM_RHODJ = numpy.zeros(nz + 1)
113 MZM_RHODJ[1:-1] = 0.5 * (rhodJ[1:] + rhodJ[:-1])
114 MZM_RHODJ[0] = rhodJ[0]
115 MZM_RHODJ[-1] = rhodJ[-1]
118 PDZZ = numpy.ones(nz + 1)
121 PDZZ[-1] = dz_mass[-1]
124 PDFDDTDZ = numpy.zeros(nz + 1)
127 PDFDDTDZ[-1] = -K[-1]
129 ZRHODJ_DF = MZM_RHODJ * PDFDDTDZ / (PDZZ ** 2)
133 def _compute_fluxes(field, surface_flux):
134 PF = numpy.zeros(nz + 1)
137 PF[1:-1] = - K * (field[1:] - field[:-1]) / dz_mass
140 def _tridiag_solver_thermo(PVARM, PF):
147 Y[0] = rhodJ[0] * PVARM[0] / timestep \
148 - MZM_RHODJ[1] * PF[1] / PDZZ[1] \
149 + MZM_RHODJ[0] * PF[0] / PDZZ[0] \
150 + ZRHODJ_DF[1] * PIMPL * PVARM[1] \
151 - ZRHODJ_DF[1] * PIMPL * PVARM[0]
153 B[0] = rhodJ[0] / timestep - ZRHODJ_DF[1] * PIMPL
154 C[0] = ZRHODJ_DF[1] * PIMPL
157 for k
in range(1, nz - 1):
158 Y[k] = rhodJ[k] * PVARM[k] / timestep \
159 - MZM_RHODJ[k + 1] * PF[k + 1] / PDZZ[k + 1] \
160 + MZM_RHODJ[k] * PF[k] / PDZZ[k] \
161 + ZRHODJ_DF[k + 1] * PIMPL * PVARM[k + 1] \
162 - ZRHODJ_DF[k + 1] * PIMPL * PVARM[k] \
163 - ZRHODJ_DF[k] * PIMPL * PVARM[k] \
164 + ZRHODJ_DF[k] * PIMPL * PVARM[k - 1]
165 A[k] = ZRHODJ_DF[k] * PIMPL
166 B[k] = rhodJ[k] / timestep \
167 - ZRHODJ_DF[k + 1] * PIMPL - ZRHODJ_DF[k] * PIMPL
168 C[k] = ZRHODJ_DF[k + 1] * PIMPL
171 Y[-1] = rhodJ[-1] * PVARM[-1] / timestep \
172 - MZM_RHODJ[-1] * PF[-1] / PDZZ[-1] \
173 + MZM_RHODJ[-2] * PF[-2] / PDZZ[-2] \
174 - ZRHODJ_DF[-2] * PIMPL * PVARM[-1] \
175 + ZRHODJ_DF[-2] * PIMPL * PVARM[-2]
176 A[-1] = ZRHODJ_DF[-2] * PIMPL
177 B[-1] = rhodJ[-1] / timestep - ZRHODJ_DF[-2] * PIMPL
180 gam = numpy.zeros(nz)
182 PVARP = numpy.zeros(nz)
183 PVARP[0] = Y[0] / bet
185 for k
in range(1, nz):
186 gam[k] = C[k - 1] / bet
187 bet = B[k] - A[k] * gam[k]
188 PVARP[k] = (Y[k] - A[k] * PVARP[k - 1]) / bet
190 for k
in range(nz - 2, -1, -1):
191 PVARP[k] = PVARP[k] - gam[k + 1] * PVARP[k + 1]
193 return (PVARP - PVARM) / timestep
195 PF_theta = _compute_fluxes(theta, sfth)
196 PF_rv = _compute_fluxes(rv, sfrv)
197 PF_u = _compute_fluxes(u, sfu)
198 PF_v = _compute_fluxes(v, sfv)
201 _tridiag_solver_thermo(theta, PF_theta),
202 _tridiag_solver_thermo(rv, PF_rv),
203 _tridiag_solver_thermo(u, PF_u),
204 _tridiag_solver_thermo(v, PF_v),
279 """Compute surface fluxes, return (sfu, sfv, sfth, sfrv)."""
280 klevgrd = 0
if self.
grid.ascending
else -1
282 if self.
case.surface_forcing_temp
in (
'none',
'ts')
or \
283 self.
case.surface_forcing_moisture
in (
'none',
'beta',
'mrsos')
or \
284 self.
case.surface_forcing_wind
in (
'none',):
289 def ustar2fluxes(ustar, u, v):
290 alpha = numpy.arctan2(v, u)
291 return -ustar ** 2 * numpy.cos(alpha), \
292 -ustar ** 2 * numpy.sin(alpha)
295 surface_temperature = state[
'Ts']
297 if self.
grid.ascending:
298 surface_temperature = ctx[
'temperature'][0]
300 surface_temperature = ctx[
'temperature'][-1]
301 if surface_temperature > self.
cst.Tt:
302 latent_heat = (self.
cst.LvTt
303 - (self.
cst.Cpv - self.
cst.Cl)
304 * (surface_temperature - self.
cst.Tt))
306 latent_heat = (self.
cst.LsTt
307 - (self.
cst.Cpv - self.
cst.Ci)
308 * (surface_temperature - self.
cst.Tt))
317 if self.
case.surface_forcing_temp ==
'none':
318 from .
import Phyex1DError
320 'surface_forcing_temp == none not implemented')
321 if self.
case.surface_forcing_moisture
in (
'beta',
'mrsos'):
322 from .
import Phyex1DError
324 'surface_forcing_moisture == beta or mrsos '
326 if self.
case.surface_forcing_wind !=
'none':
327 from .
import Phyex1DError
329 'surface_forcing_wind != none not implemented')
330 windgrd = numpy.sqrt(state[
'u'][klevgrd] ** 2
331 + state[
'v'][klevgrd] ** 2)
332 exner_surf = (state[
'Ps'] / 1.E5) ** (self.
cst.Rd / self.
cst.Cpd)
334 qv_grd = state[
'qv'][klevgrd]
336 qv_grd = state[
'rv'][klevgrd] * ctx[
'qdm'][klevgrd]
339 wave_peak_period = 0.
340 result = wasp.WASP_FLUX(
341 ctx[
'temperature'][klevgrd], qv_grd,
342 ctx[
'exner'][klevgrd], ctx[
'rho'][klevgrd],
343 windgrd, ctx[
'z_mass'][klevgrd], ctx[
'z_mass'][klevgrd],
344 state[
'Ts' if self.
case.surface_type ==
'ocean' else 'Tskin'],
345 exner_surf, state[
'Ps'], precip,
346 wave_height, wave_peak_period)
347 sshf_scheme, swf_scheme, ustar = result
348 sfu_scheme, sfv_scheme = ustar2fluxes(
349 ustar, state[
'u'][klevgrd], state[
'v'][klevgrd])
351 if self.
case.surface_forcing_temp ==
'none':
352 if sshf_scheme
is None:
353 from .
import Phyex1DError
354 raise Phyex1DError(
"Surface scheme didn't compute sshf")
355 elif self.
case.surface_forcing_temp ==
'kinematic':
356 raise NotImplementedError(
'surface_forcing_temp=kinematic')
357 elif self.
case.surface_forcing_temp ==
'surface_flux':
359 elif self.
case.surface_forcing_temp ==
'ts':
362 from .
import Phyex1DError
365 if self.
case.surface_forcing_moisture ==
'none':
366 if swf_scheme
is None:
367 from .
import Phyex1DError
370 elif self.
case.surface_forcing_moisture ==
'kinematic':
371 raise NotImplementedError(
'surface_forcing_moisture=kinematic')
372 elif self.
case.surface_forcing_moisture ==
'surface_flux':
373 swf = state[
'slhf'] / latent_heat
374 elif self.
case.surface_forcing_moisture ==
'beta':
375 raise NotImplementedError(
'surface_forcing_moisture=beta')
376 elif self.
case.surface_forcing_moisture ==
'mrsos':
377 raise NotImplementedError(
'surface_forcing_moisture=mrsos')
379 from .
import Phyex1DError
380 raise Phyex1DError(
'Wrong surface_forcing_moisture option')
382 if self.
case.surface_forcing_wind ==
'none':
385 elif self.
case.surface_forcing_wind ==
'z0':
386 ustar = (numpy.sqrt(state[
'u'][klevgrd] ** 2
387 + state[
'v'][klevgrd] ** 2)
389 / numpy.log(ctx[
'z_mass'][klevgrd] / state[
'z0']))
390 sfu, sfv = ustar2fluxes(
391 ustar, state[
'u'][klevgrd], state[
'v'][klevgrd])
392 elif self.
case.surface_forcing_wind ==
'ustar':
393 sfu, sfv = ustar2fluxes(
394 state[
'ustar'], state[
'u'][klevgrd], state[
'v'][klevgrd])
396 from .
import Phyex1DError
399 sfrv = swf / ctx[
'rhod'][0
if self.
grid.ascending
else -1]
400 sfth = sshf / (self.
cst.Cpd * ctx[
'rho'][klevgrd])
404 state[
'slhf'] = swf * latent_heat
408 return sfu, sfv, sfth, sfrv
412 Mass-flux shallow convection scheme with implicit tridiagonal solver
413 following PHYEX's TRIDIAG_MASSFLUX.
415 Updraft properties equal near-surface values (no perturbation).
416 Cloud top is the first flux level above ground where environmental
417 theta_l equals the updraft theta_l. The mass-flux profile is a
418 half-sine (0 at surface, 0 at cloud top, max in the middle).
422 timestep : float — time step (s)
426 tend_theta_l : ndarray (nkt,) — theta_l tendency (K/s)
427 tend_rt : ndarray (nkt,) — total-water tendency (kg/kg/s)
428 tend_u : ndarray (nkt,) — u-wind tendency (m/s²)
429 tend_v : ndarray (nkt,) — v-wind tendency (m/s²)
434 if self.
grid.ascending:
435 z_ground = ctx[
'z_flux'][0]
438 z_ground = ctx[
'z_flux'][-1]
440 height = numpy.abs(ctx[
'z_flux'] - z_ground)
444 rv = state[
'qv'] / ctx[
'qdm']
445 rc = state[
'qc'] / ctx[
'qdm']
452 + (self.
cst.Cpv - self.
cst.Cl) * (ctx[
'temperature'] - self.
cst.Tt))
453 cp = ctx[
'qdm'] * (self.
cst.Cpd + self.
cst.Cpv * rv + self.
cst.Cl * rc)
454 theta_l = ctx[
'theta'] - lv / (cp * ctx[
'exner']) * rc
460 theta_l_up = theta_l[klev] + self.
mf_dtheta
462 u_up = state[
'u'][klev]
463 v_up = state[
'v'][klev]
467 vf = numpy.zeros(nkt + 1)
468 vf[1:-1] = 0.5 * (var[:-1] + var[1:])
470 vf[-1] = var[-1
if self.
grid.ascending
else 0]
473 theta_l_env = to_flux(theta_l)
475 u_env = to_flux(state[
'u'])
476 v_env = to_flux(state[
'v'])
479 if self.
grid.ascending:
481 for k
in range(1, nkt + 1):
482 if theta_l_env[k] >= theta_l_up:
487 for k
in range(nkt - 1, -1, -1):
488 if theta_l_env[k] >= theta_l_up:
491 pbl_height = height[ktop]
494 mf = numpy.zeros(nkt + 1)
495 mask = (height > 0.) & (height < pbl_height)
496 mf[mask] = self.
mf_max * numpy.sin(
497 numpy.pi * height[mask] / pbl_height)
500 PF_theta_l = mf * (theta_l_up - theta_l_env)
501 PF_rt = mf * (rt_up - rt_env)
502 PF_u = mf * (u_up - u_env)
503 PF_v = mf * (v_up - v_env)
510 rhodJ = ctx[
'rhod'] * dzz
512 MZM_RHODJ = numpy.zeros(nkt + 1)
513 MZM_RHODJ[1:-1] = 0.5 * (rhodJ[1:] + rhodJ[:-1])
514 MZM_RHODJ[0] = rhodJ[klev]
515 MZM_RHODJ[-1] = rhodJ[-1
if self.
grid.ascending
else 0]
517 PDZZ = numpy.ones(nkt + 1)
518 PDZZ[1:-1] = ctx[
'dz_mass']
519 PDZZ[0] = ctx[
'dz_mass'][0]
520 PDZZ[-1] = ctx[
'dz_mass'][-1]
522 ZRHODJ_DFDT_O_DZ = MZM_RHODJ * PDFDT / PDZZ
526 def _tridiag_solver(PVARM, PF):
533 Y[0] = rhodJ[0] * PVARM[0] / timestep \
534 - MZM_RHODJ[1] * PF[1] / PDZZ[1] \
535 + MZM_RHODJ[0] * PF[0] / PDZZ[0] \
536 + ZRHODJ_DFDT_O_DZ[1] * 0.5 * PIMPL * PVARM[1] \
537 + ZRHODJ_DFDT_O_DZ[1] * 0.5 * PIMPL * PVARM[0]
539 B[0] = rhodJ[0] / timestep \
540 + ZRHODJ_DFDT_O_DZ[1] * 0.5 * PIMPL
541 C[0] = ZRHODJ_DFDT_O_DZ[1] * 0.5 * PIMPL
544 for k
in range(1, nkt - 1):
545 Y[k] = rhodJ[k] * PVARM[k] / timestep \
546 - MZM_RHODJ[k + 1] * PF[k + 1] / PDZZ[k + 1] \
547 + MZM_RHODJ[k] * PF[k] / PDZZ[k] \
548 + ZRHODJ_DFDT_O_DZ[k + 1] * 0.5 * PIMPL * PVARM[k + 1] \
549 + ZRHODJ_DFDT_O_DZ[k + 1] * 0.5 * PIMPL * PVARM[k] \
550 - ZRHODJ_DFDT_O_DZ[k] * 0.5 * PIMPL * PVARM[k] \
551 - ZRHODJ_DFDT_O_DZ[k] * 0.5 * PIMPL * PVARM[k - 1]
552 A[k] = -ZRHODJ_DFDT_O_DZ[k] * 0.5 * PIMPL
553 B[k] = rhodJ[k] / timestep \
554 + ZRHODJ_DFDT_O_DZ[k + 1] * 0.5 * PIMPL \
555 - ZRHODJ_DFDT_O_DZ[k] * 0.5 * PIMPL
556 C[k] = ZRHODJ_DFDT_O_DZ[k + 1] * 0.5 * PIMPL
559 Y[-1] = rhodJ[-1] * PVARM[-1] / timestep
560 B[-1] = rhodJ[-1] / timestep
563 gam = numpy.zeros(nkt)
565 PVARP = numpy.zeros(nkt)
566 PVARP[0] = Y[0] / bet
568 for k
in range(1, nkt):
569 gam[k] = C[k - 1] / bet
570 bet = B[k] - A[k] * gam[k]
571 PVARP[k] = (Y[k] - A[k] * PVARP[k - 1]) / bet
573 for k
in range(nkt - 2, -1, -1):
574 PVARP[k] = PVARP[k] - gam[k + 1] * PVARP[k + 1]
576 return (PVARP - PVARM) / timestep
579 _tridiag_solver(theta_l, PF_theta_l),
580 _tridiag_solver(rt, PF_rt),
581 _tridiag_solver(state[
'u'], PF_u),
582 _tridiag_solver(state[
'v'], PF_v),
588 def step(self, state, timestep, timestep_number):
589 state[
'tke'] = numpy.maximum(1e-6, state[
'tke'])
599 rv = state[
'qv'] / ctx[
'qdm']
600 rc = state[
'qc'] / ctx[
'qdm']
602 rv = state[
'rv'].copy()
603 rc = state[
'rc'].copy()
604 theta = ctx[
'theta'].copy()
606 drc = self.
_adjust(theta, rv, rc, ctx)
611 + (self.
cst.Cpv - self.
cst.Cl) * (ctx[
'exner'] * theta - self.
cst.Tt))
612 cp = ctx[
'qdm'] * (self.
cst.Cpd + self.
cst.Cpv * rv
615 theta = ctx[
'theta'] + lv / (cp * ctx[
'exner']) * rc
618 state[
'qv'] = rv * ctx[
'qdm']
619 state[
'qc'] = rc * ctx[
'qdm']
620 state[
'T'] = theta * ctx[
'exner']
624 state[
'Theta'] = theta
633 sfu, sfv, sfth, sfrv = self.
_surface(state, ctx)
639 tend_theta_l, tend_rt, tend_u, tend_v = self.
_massflux(state, ctx, timestep)
643 state[
'Theta'] += tend_theta_l * timestep
645 state[
'T'] += tend_theta_l * ctx[
'exner'] * timestep
648 state[
'qv'] += tend_rt * ctx[
'qdm'] * timestep
649 state[
'qv'] = numpy.maximum(state[
'qv'], 0.)
651 state[
'rv'] += tend_rt * timestep
652 state[
'rv'] = numpy.maximum(state[
'rv'], 0.)
654 state[
'u'] += tend_u * timestep
655 state[
'v'] += tend_v * timestep
663 theta = state[
'Theta']
665 theta = state[
'T'] / ctx[
'exner']
670 rv = state[
'qv'] / ctx[
'qdm']
672 tend_theta, tend_rv, tend_u, tend_v = self.
_k_diff(
673 theta, rv, state[
'u'], state[
'v'], ctx,
674 sfth, sfrv, sfu, sfv, timestep)
677 state[
'Theta'] += tend_theta * timestep
679 state[
'T'] += tend_theta * ctx[
'exner'] * timestep
682 state[
'rv'] += tend_rv * timestep
684 state[
'qv'] += tend_rv * ctx[
'qdm'] * timestep
686 state[
'u'] += tend_u * timestep
687 state[
'v'] += tend_v * timestep