PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
simple_physics.py
Go to the documentation of this file.
1"""
2Simplified physics scheme: all-or-nothing saturation adjustment,
3K-gradient turbulence, no radiation, only vapour and cloud liquid water.
4"""
5
6import numpy
7
8from . import wasp
9from .physics_base import PhysicsBase
10
11
13 """
14 Simplified physics scheme.
15
16 Features:
17 - all-or-nothing saturation adjustment (liquid only)
18 - K-gradient turbulence with constant K
19 - mass-flux shallow convection (prescribed half-sine profile)
20 - WASP or prescribed surface fluxes
21 - only vapour (v) and cloud liquid water (c)
22 """
23
24 OPTIONS_HELP = {
25 'mf_max': 'maximum of the prescribed mass flux (half-sine profile between the '
26 'surface and the top of the updraft)',
27 'mf_dtheta': 'theta_l excess (K) of the updraft with respect to the near-surface value',
28 'mf_rt_factor': 'factor applied to the near-surface total water to get the updraft '
29 'value',
30 'do_adjustment': 'activation of the saturation adjustment',
31 'do_shallow': 'activation of the shallow convection (mass flux)',
32 'do_turb': 'activation of the turbulence',
33 }
34
35 def __init__(self, dt, method, name, tag, case, grid, prognostic_variables,
36 attrs=None,
37 mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2,
38 do_adjustment=True, do_shallow=True, do_turb=True):
39 super().__init__(dt, method, name, tag, case, grid, prognostic_variables)
40 self.mf_max = mf_max
41 self.mf_dtheta = mf_dtheta
42 self.mf_rt_factor = mf_rt_factor
43 self.do_adjustment = do_adjustment
44 self.do_shallow = do_shallow
45 self.do_turb = do_turb
46
47 def setup(self, init_state, duration):
48 pass
49
50 # ------------------------------------------------------------------
51 # step helpers
52 # ------------------------------------------------------------------
53 def _k_diff(self, theta, rv, u, v, ctx, sfth, sfrv, sfu, sfv, timestep):
54 """
55 K-gradient diffusion tendencies for all variables in one pass.
56 Uses an implicit tridiagonal solver following PHYEX's TRIDIAG_THERMO.
57
58 Parameters
59 ----------
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)
66
67 Returns
68 -------
69 tuple of ndarray — (tend_theta, tend_rv, tend_u, tend_v)
70 """
71 nz = ctx['nkt']
72 dz_mass = ctx['dz_mass']
73 z_flux = ctx['z_flux']
74 dzz = ctx['dzz']
75
76 # Height above ground for flux levels
77 if self.grid.ascending:
78 z_ground = z_flux[0]
79 else:
80 z_ground = z_flux[-1]
81 height = numpy.abs(z_flux - z_ground)
82
83 # Blackadar length
84 lasymp = 200.
85 l = self.cst.Karman * height[1:-1] / (1. + self.cst.Karman * height[1:-1] / lasymp)
86
87 # Shear
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)
91
92 # Virtual potential temperature (buoyancy)
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])
95
96 # Brunt-Väisälä frequency squared at flux points
97 N2 = (self.cst.g / theta_v_flux) * (theta_v[1:] - theta_v[:-1]) / dz_mass
98
99 # Gradient Richardson number (only stable stratification reduces K)
100 Ri = numpy.maximum(N2, 0.0) / (du_dz**2 + 1e-10)
101
102 # Stability function (Louis 1979, simplified)
103 f_Ri = 1.0 / (1.0 + 10.0 * Ri)**2
104
105 K = l**2 * numpy.abs(du_dz) * f_Ri
106 K = numpy.maximum(K, 1e-4)
107 K = numpy.minimum(K, 200)
108
109 rhodJ = ctx['rhod'] * dzz
110
111 # MZM_RHODJ at flux points (0.5*(rhodJ[k] + rhodJ[k-1]))
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]
116
117 # PDZZ at flux points: distance between adjacent mass levels
118 PDZZ = numpy.ones(nz + 1)
119 PDZZ[1:-1] = dz_mass
120 PDZZ[0] = dz_mass[0]
121 PDZZ[-1] = dz_mass[-1]
122
123 # PDFDDTDZ = K at flux points
124 PDFDDTDZ = numpy.zeros(nz + 1)
125 PDFDDTDZ[1:-1] = -K
126 PDFDDTDZ[0] = -K[0]
127 PDFDDTDZ[-1] = -K[-1]
128
129 ZRHODJ_DF = MZM_RHODJ * PDFDDTDZ / (PDZZ ** 2)
130
131 PIMPL = 1.0
132
133 def _compute_fluxes(field, surface_flux):
134 PF = numpy.zeros(nz + 1)
135 PF[0] = surface_flux
136 PF[-1] = 0.
137 PF[1:-1] = - K * (field[1:] - field[:-1]) / dz_mass
138 return PF
139
140 def _tridiag_solver_thermo(PVARM, PF):
141 A = numpy.zeros(nz)
142 B = numpy.zeros(nz)
143 C = numpy.zeros(nz)
144 Y = numpy.zeros(nz)
145
146 # Bottom level
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]
152
153 B[0] = rhodJ[0] / timestep - ZRHODJ_DF[1] * PIMPL
154 C[0] = ZRHODJ_DF[1] * PIMPL
155
156 # Interior levels
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
169
170 # Top level
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
178
179 # Thomas algorithm
180 gam = numpy.zeros(nz)
181 bet = B[0]
182 PVARP = numpy.zeros(nz)
183 PVARP[0] = Y[0] / bet
184
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
189
190 for k in range(nz - 2, -1, -1):
191 PVARP[k] = PVARP[k] - gam[k + 1] * PVARP[k + 1]
192
193 return (PVARP - PVARM) / timestep
194
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)
199
200 return (
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),
205 )
206
207 def _preparation(self, state):
208 """Compute grid, density and reference fields, return a context dict."""
209 ctx = {}
210
211 ctx['pressure'] = self.grid.get_pressure(
212 'MASS', state, self.prognostic_variables)
213 ctx['z_mass'] = self.grid.get_altitude(
214 'MASS', state, self.prognostic_variables)
215 ctx['z_flux'] = self.grid.get_altitude(
216 'FLUX', state, self.prognostic_variables)
217
218 dzz = numpy.diff(ctx['z_flux'])
219 if self.grid.ascending:
220 if dzz[-1] == numpy.inf:
221 dzz[-1] = dzz[-2]
222 else:
223 dzz = -dzz
224 if dzz[0] == numpy.inf:
225 dzz[0] = dzz[1]
226 ctx['dzz'] = dzz
227 dz_mass = numpy.diff(ctx['z_mass'])
228 if not self.grid.ascending:
229 dz_mass = -dz_mass
230 ctx['dz_mass'] = dz_mass
231 ctx['nkt'] = len(ctx['pressure'])
232
233 ctx['exner'] = (ctx['pressure'] / 1.E5) ** (self.cst.Rd / self.cst.Cpd)
234
235 if 'T' in self.prognostic_variables:
236 ctx['theta'] = state['T'] / ctx['exner']
237 ctx['temperature'] = state['T']
238 else:
239 ctx['theta'] = state['Theta']
240 ctx['temperature'] = ctx['exner'] * ctx['theta']
241
242 if 'qv' in self.prognostic_variables:
243 qt = state['qv'] + state['qc']
244 ctx['qdm'] = 1. - qt
245 gas_constant = (self.cst.Rd
246 + state['qv'] * (self.cst.Rv - self.cst.Rd)
247 - state['qc'] * self.cst.Rd)
248 ctx['rho'] = ctx['pressure'] / (gas_constant * ctx['temperature'])
249 ctx['rhod'] = ctx['rho'] * ctx['qdm']
250 else:
251 rt = state['rv'] + state['rc']
252 ctx['qdm'] = 1. / (1. + rt)
253 gas_constant = (self.cst.Rd + state['rv'] * self.cst.Rv) / (1. + rt)
254 ctx['rho'] = ctx['pressure'] / (gas_constant * ctx['temperature'])
255 ctx['rhod'] = ctx['pressure'] / (
256 (self.cst.Rd + state['rv'] * self.cst.Rv)
257 * ctx['theta'] * ctx['exner'])
258
259 return ctx
260
261 def _adjust(self, theta, rv, rc, ctx):
262 """
263 Adjustment
264 """
265 T_cur = ctx['exner'] * theta
266 logT = numpy.log(T_cur.clip(min=1.))
267 es = numpy.exp(self.cst.alpw - self.cst.betaw / T_cur
268 - self.cst.gamw * logT)
269 es = numpy.minimum(es, 0.99 * ctx['pressure'])
270
271 eps = self.cst.Rd / self.cst.Rv
272 rsat = eps * es / (ctx['pressure'] - (1. - eps) * es)
273
274 drc = rv - rsat
275 drc = numpy.clip(drc, -rc, rv)
276 return drc
277
278 def _surface(self, state, ctx):
279 """Compute surface fluxes, return (sfu, sfv, sfth, sfrv)."""
280 klevgrd = 0 if self.grid.ascending else -1
281
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',):
285 need_scheme = True
286 else:
287 need_scheme = False
288
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)
293
294 if 'Ts' in state:
295 surface_temperature = state['Ts']
296 else:
297 if self.grid.ascending:
298 surface_temperature = ctx['temperature'][0]
299 else:
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))
305 else:
306 latent_heat = (self.cst.LsTt
307 - (self.cst.Cpv - self.cst.Ci)
308 * (surface_temperature - self.cst.Tt))
309
310 sshf_scheme = None
311 swf_scheme = None
312 sfu_scheme = None
313 sfv_scheme = None
314 if not need_scheme:
315 pass
316 else:
317 if self.case.surface_forcing_temp == 'none':
318 from . import Phyex1DError
319 raise Phyex1DError(
320 'surface_forcing_temp == none not implemented')
321 if self.case.surface_forcing_moisture in ('beta', 'mrsos'):
322 from . import Phyex1DError
323 raise Phyex1DError(
324 'surface_forcing_moisture == beta or mrsos '
325 'not implemented')
326 if self.case.surface_forcing_wind != 'none':
327 from . import Phyex1DError
328 raise 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)
333 if 'qv' in self.prognostic_variables:
334 qv_grd = state['qv'][klevgrd]
335 else:
336 qv_grd = state['rv'][klevgrd] * ctx['qdm'][klevgrd]
337 precip = 0.
338 wave_height = 0.
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])
350
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':
358 sshf = state['sshf']
359 elif self.case.surface_forcing_temp == 'ts':
360 sshf = sshf_scheme
361 else:
362 from . import Phyex1DError
363 raise Phyex1DError('Wrong surface_forcing_temp option')
364
365 if self.case.surface_forcing_moisture == 'none':
366 if swf_scheme is None:
367 from . import Phyex1DError
368 raise Phyex1DError("Surface scheme didn't compute swf")
369 swf = swf_scheme
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')
378 else:
379 from . import Phyex1DError
380 raise Phyex1DError('Wrong surface_forcing_moisture option')
381
382 if self.case.surface_forcing_wind == 'none':
383 sfu = sfu_scheme
384 sfv = sfv_scheme
385 elif self.case.surface_forcing_wind == 'z0':
386 ustar = (numpy.sqrt(state['u'][klevgrd] ** 2
387 + state['v'][klevgrd] ** 2)
388 * self.cst.Karman
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])
395 else:
396 from . import Phyex1DError
397 raise Phyex1DError('Wrong surface_forcing_wind option')
398
399 sfrv = swf / ctx['rhod'][0 if self.grid.ascending else -1]
400 sfth = sshf / (self.cst.Cpd * ctx['rho'][klevgrd])
401
402 state['sfrv'] = sfrv
403 state['swf'] = swf
404 state['slhf'] = swf * latent_heat
405 state['sfth'] = sfth
406 state['sshf'] = sshf
407
408 return sfu, sfv, sfth, sfrv
409
410 def _massflux(self, state, ctx, timestep):
411 """
412 Mass-flux shallow convection scheme with implicit tridiagonal solver
413 following PHYEX's TRIDIAG_MASSFLUX.
414
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).
419
420 Parameters
421 ----------
422 timestep : float — time step (s)
423
424 Returns
425 -------
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²)
430 """
431 nkt = ctx['nkt']
432
433 # Height above ground for flux levels
434 if self.grid.ascending:
435 z_ground = ctx['z_flux'][0]
436 klev = 0
437 else:
438 z_ground = ctx['z_flux'][-1]
439 klev = -1
440 height = numpy.abs(ctx['z_flux'] - z_ground)
441
442 # Current theta, rv, rc from state
443 if 'qv' in self.prognostic_variables:
444 rv = state['qv'] / ctx['qdm']
445 rc = state['qc'] / ctx['qdm']
446 else:
447 rv = state['rv']
448 rc = state['rc']
449
450 # Liquid water potential temperature
451 lv = (self.cst.LvTt
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
455
456 # Total water
457 rt = rv + rc
458
459 # Updraft properties (near-surface values with excess/factor)
460 theta_l_up = theta_l[klev] + self.mf_dtheta
461 rt_up = rt[klev] * self.mf_rt_factor
462 u_up = state['u'][klev]
463 v_up = state['v'][klev]
464
465 # Interpolate environmental profiles to flux levels
466 def to_flux(var):
467 vf = numpy.zeros(nkt + 1)
468 vf[1:-1] = 0.5 * (var[:-1] + var[1:])
469 vf[0] = var[klev]
470 vf[-1] = var[-1 if self.grid.ascending else 0]
471 return vf
472
473 theta_l_env = to_flux(theta_l)
474 rt_env = to_flux(rt)
475 u_env = to_flux(state['u'])
476 v_env = to_flux(state['v'])
477
478 # PBL top: first flux level above ground where theta_l_env >= theta_l_up
479 if self.grid.ascending:
480 ktop = nkt
481 for k in range(1, nkt + 1):
482 if theta_l_env[k] >= theta_l_up:
483 ktop = k
484 break
485 else:
486 ktop = 0
487 for k in range(nkt - 1, -1, -1):
488 if theta_l_env[k] >= theta_l_up:
489 ktop = k
490 break
491 pbl_height = height[ktop]
492
493 # Mass flux profile at interfaces (0 at surface, 0 at cloud top)
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)
498
499 # Explicit fluxes at flux points: F = mf * (updraft - env)
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)
504
505 # PDFDT = dF/d(env) at flux points = -mf
506 PDFDT = -mf
507
508 # Grid metrics for tridiagonal solver
509 dzz = ctx['dzz']
510 rhodJ = ctx['rhod'] * dzz
511
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]
516
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]
521
522 ZRHODJ_DFDT_O_DZ = MZM_RHODJ * PDFDT / PDZZ
523
524 PIMPL = 1.0
525
526 def _tridiag_solver(PVARM, PF):
527 A = numpy.zeros(nkt)
528 B = numpy.zeros(nkt)
529 C = numpy.zeros(nkt)
530 Y = numpy.zeros(nkt)
531
532 # Bottom level
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]
538
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
542
543 # Interior levels
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
557
558 # Top level
559 Y[-1] = rhodJ[-1] * PVARM[-1] / timestep
560 B[-1] = rhodJ[-1] / timestep
561
562 # Thomas algorithm
563 gam = numpy.zeros(nkt)
564 bet = B[0]
565 PVARP = numpy.zeros(nkt)
566 PVARP[0] = Y[0] / bet
567
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
572
573 for k in range(nkt - 2, -1, -1):
574 PVARP[k] = PVARP[k] - gam[k + 1] * PVARP[k + 1]
575
576 return (PVARP - PVARM) / timestep
577
578 return (
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),
583 )
584
585 # ------------------------------------------------------------------
586 # step
587 # ------------------------------------------------------------------
588 def step(self, state, timestep, timestep_number):
589 state['tke'] = numpy.maximum(1e-6, state['tke'])
590
591 ctx = self._preparation(state)
592
593
596
597 if self.do_adjustment:
598 if 'qv' in self.prognostic_variables:
599 rv = state['qv'] / ctx['qdm']
600 rc = state['qc'] / ctx['qdm']
601 else:
602 rv = state['rv'].copy()
603 rc = state['rc'].copy()
604 theta = ctx['theta'].copy()
605
606 drc = self._adjust(theta, rv, rc, ctx)
607 rv = rv - drc
608 rc = rc + drc
609
610 lv = (self.cst.LvTt
611 + (self.cst.Cpv - self.cst.Cl) * (ctx['exner'] * theta - self.cst.Tt))
612 cp = ctx['qdm'] * (self.cst.Cpd + self.cst.Cpv * rv
613 + self.cst.Cl * rc)
614
615 theta = ctx['theta'] + lv / (cp * ctx['exner']) * rc
616
617 if 'qv' in self.prognostic_variables:
618 state['qv'] = rv * ctx['qdm']
619 state['qc'] = rc * ctx['qdm']
620 state['T'] = theta * ctx['exner']
621 else:
622 state['rv'] = rv
623 state['rc'] = rc
624 state['Theta'] = theta
625
626 # Recompute derived fields after adjustment
627 ctx = self._preparation(state)
628
629
632
633 sfu, sfv, sfth, sfrv = self._surface(state, ctx)
634
635
638 if self.do_shallow:
639 tend_theta_l, tend_rt, tend_u, tend_v = self._massflux(state, ctx, timestep)
640
641 # Apply tendencies
642 if 'Theta' in self.prognostic_variables:
643 state['Theta'] += tend_theta_l * timestep
644 else:
645 state['T'] += tend_theta_l * ctx['exner'] * timestep
646
647 if 'qv' in self.prognostic_variables:
648 state['qv'] += tend_rt * ctx['qdm'] * timestep
649 state['qv'] = numpy.maximum(state['qv'], 0.)
650 else:
651 state['rv'] += tend_rt * timestep
652 state['rv'] = numpy.maximum(state['rv'], 0.)
653
654 state['u'] += tend_u * timestep
655 state['v'] += tend_v * timestep
656
657
660 if self.do_turb:
661 # Prepare variables in theta/r space
662 if 'Theta' in self.prognostic_variables:
663 theta = state['Theta']
664 else:
665 theta = state['T'] / ctx['exner']
666
667 if 'rv' in self.prognostic_variables:
668 rv = state['rv']
669 elif 'qv' in self.prognostic_variables:
670 rv = state['qv'] / ctx['qdm']
671
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)
675
676 if 'Theta' in self.prognostic_variables:
677 state['Theta'] += tend_theta * timestep
678 else:
679 state['T'] += tend_theta * ctx['exner'] * timestep
680
681 if 'rv' in self.prognostic_variables:
682 state['rv'] += tend_rv * timestep
683 elif 'qv' in self.prognostic_variables:
684 state['qv'] += tend_rv * ctx['qdm'] * timestep
685
686 state['u'] += tend_u * timestep
687 state['v'] += tend_v * timestep
688
689 return state
690
691
693 """
694 Simplified physics with T and q as prognostic variables
695 """
696
697 def __init__(self, dt, method, name, tag, case, grid,
698 attrs=None,
699 mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2,
700 do_adjustment=True, do_shallow=True, do_turb=True):
701 super().__init__(dt, method, name, tag, case, grid,
702 ['T', 'qv', 'qc', 'u', 'v', 'w', 'tke'],
703 attrs,
704 mf_max=mf_max, mf_dtheta=mf_dtheta, mf_rt_factor=mf_rt_factor,
705 do_adjustment=do_adjustment, do_shallow=do_shallow, do_turb=do_turb)
706
707
709 """
710 Simplified physics with Theta and r as prognostic variables
711 """
712
713 def __init__(self, dt, method, name, tag, case, grid,
714 attrs=None,
715 mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2,
716 do_adjustment=True, do_shallow=True, do_turb=True):
717 super().__init__(dt, method, name, tag, case, grid,
718 ['Theta', 'rv', 'rc', 'u', 'v', 'w', 'tke'],
719 attrs,
720 mf_max=mf_max, mf_dtheta=mf_dtheta, mf_rt_factor=mf_rt_factor,
721 do_adjustment=do_adjustment, do_shallow=do_shallow, do_turb=do_turb)
__init__(self, dt, method, name, tag, case, grid, attrs=None, mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2, do_adjustment=True, do_shallow=True, do_turb=True)
__init__(self, dt, method, name, tag, case, grid, attrs=None, mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2, do_adjustment=True, do_shallow=True, do_turb=True)
step(self, state, timestep, timestep_number)
__init__(self, dt, method, name, tag, case, grid, prognostic_variables, attrs=None, mf_max=0.05, mf_dtheta=.5, mf_rt_factor=1.2, do_adjustment=True, do_shallow=True, do_turb=True)
_k_diff(self, theta, rv, u, v, ctx, sfth, sfrv, sfu, sfv, timestep)