Skip to content

Commit 8f9cb8c

Browse files
committed
rft: clean up diffusivity code
1 parent f131c57 commit 8f9cb8c

File tree

3 files changed

+76
-77
lines changed

3 files changed

+76
-77
lines changed
Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,70 @@
1-
function ᶜcompute_eddy_diffusivity_coefficient(
2-
ᶜρ,
3-
vert_diff::DecayWithHeightDiffusion,
4-
)
1+
2+
"""
3+
ᶜcompute_eddy_diffusivity_coefficient(ᶜρ, (; D₀, H)::DecayWithHeightDiffusion)
4+
5+
Return lazy representation of the vertical profile of eddy diffusivity
6+
for the `DecayWithHeightDiffusion` model.
7+
8+
The profile is given by:
9+
```
10+
K(z) = D₀ ⋅ exp(-(z - z_sfc) / H)
11+
```
12+
13+
# Arguments
14+
- `ᶜρ`: Cell-centered field whose axes provide vertical coordinates.
15+
- Instance of `DecayWithHeightDiffusion` model, with fields:
16+
- `D₀`: Surface eddy diffusivity magnitude.
17+
- `H`: E-folding height for the exponential decay.
18+
19+
# See also
20+
- [`DecayWithHeightDiffusion`] for the model specification
21+
- [`vertical_diffusion_boundary_layer_tendency!`] where this coefficient is applied
22+
"""
23+
function ᶜcompute_eddy_diffusivity_coefficient(ᶜρ, (; D₀, H)::DecayWithHeightDiffusion)
524
(; ᶜz, ᶠz) = z_coordinate_fields(axes(ᶜρ))
625
ᶠz_sfc = Fields.level(ᶠz, Fields.half)
7-
return @. lazy(
8-
eddy_diffusivity_coefficient_H(vert_diff.D₀, vert_diff.H, ᶠz_sfc, ᶜz),
9-
)
26+
return @. lazy(eddy_diffusivity_coefficient_H(D₀, H, ᶠz_sfc, ᶜz))
1027
end
28+
eddy_diffusivity_coefficient_H(D₀, H, z_sfc, z) = D₀ * exp(-(z - z_sfc) / H)
1129

12-
function ᶜcompute_eddy_diffusivity_coefficient(
13-
ᶜuₕ,
14-
ᶜp,
15-
vert_diff::VerticalDiffusion,
16-
)
30+
"""
31+
ᶜcompute_eddy_diffusivity_coefficient(ᶜuₕ, ᶜp, (; C_E)::VerticalDiffusion)
32+
33+
Return lazy representation of the vertical profile of eddy diffusivity
34+
for the `VerticalDiffusion` model.
35+
36+
The profile is given by:
37+
```
38+
K(z) = K_E, if p > p_pbl
39+
= K_E * exp(-((p_pbl - p) / p_strato)^2), otherwise
40+
```
41+
where `K_E` is given by:
42+
```
43+
K_E = C_E ⋅ norm(ᶜuₕ(z_bot)) ⋅ Δz_bot / 2
44+
```
45+
where `z_bot` is the first interior center level of the model,
46+
and `Δz_bot` is the thickness of the surface layer.
47+
48+
# Arguments
49+
- `ᶜuₕ`: Cell-centered horizontal velocity field; its first interior level is used.
50+
- `ᶜp`: Cell-centered thermodynamic pressure field (or proxy) used by the closure.
51+
- Instance of `VerticalDiffusion` model, with field `C_E`:
52+
- `C_E`: Dimensionless eddy-coefficient factor.
53+
54+
# See also
55+
- [`VerticalDiffusion`] for the model specification
56+
"""
57+
function ᶜcompute_eddy_diffusivity_coefficient(ᶜuₕ, ᶜp, (; C_E)::VerticalDiffusion)
1758
interior_uₕ = Fields.level(ᶜuₕ, 1)
1859
ᶜΔz_surface = Fields.Δz_field(interior_uₕ)
1960
return @. lazy(
20-
eddy_diffusivity_coefficient(
21-
vert_diff.C_E,
22-
norm(interior_uₕ),
23-
ᶜΔz_surface / 2,
24-
ᶜp,
25-
),
61+
eddy_diffusivity_coefficient(C_E, norm(interior_uₕ), ᶜΔz_surface / 2, ᶜp),
2662
)
2763
end
64+
65+
function eddy_diffusivity_coefficient(C_E, norm_uₕ_bottom, Δz_bottom, p)
66+
p_pbl = 85000
67+
p_strato = 10000
68+
K_E = C_E * norm_uₕ_bottom * Δz_bottom
69+
return p > p_pbl ? K_E : K_E * exp(-((p_pbl - p) / p_strato)^2)
70+
end

src/cache/precomputed_quantities.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,6 @@ ts_gs(thermo_params, moisture_model, microphysics_model, ᶜY, K, Φ, ρ) =
431431
ρ,
432432
)
433433

434-
function eddy_diffusivity_coefficient_H(D₀, H, z_sfc, z)
435-
return D₀ * exp(-(z - z_sfc) / H)
436-
end
437-
function eddy_diffusivity_coefficient(C_E, norm_v_a, z_a, p)
438-
p_pbl = 85000
439-
p_strato = 10000
440-
K_E = C_E * norm_v_a * z_a
441-
return p > p_pbl ? K_E : K_E * exp(-((p_pbl - p) / p_strato)^2)
442-
end
443-
444434
"""
445435
set_implicit_precomputed_quantities!(Y, p, t)
446436
set_implicit_precomputed_quantities_part1!(Y, p, t)

src/prognostic_equations/vertical_diffusion_boundary_layer.jl

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,14 @@ Arguments:
6767
Modifies components of tendency vector `Yₜ.c` (e.g., `Yₜ.c.uₕ`, `Yₜ.c.ρe_tot`, `Yₜ.c.ρ`, and
6868
various tracer fields such as `Yₜ.c.ρq_tot`).
6969
"""
70-
7170
vertical_diffusion_boundary_layer_tendency!(Yₜ, Y, p, t) =
72-
vertical_diffusion_boundary_layer_tendency!(
73-
Yₜ,
74-
Y,
75-
p,
76-
t,
77-
p.atmos.vertical_diffusion,
78-
)
79-
80-
vertical_diffusion_boundary_layer_tendency!(Yₜ, Y, p, t, ::Nothing) = nothing
81-
82-
function vertical_diffusion_boundary_layer_tendency!(
83-
Yₜ,
84-
Y,
85-
p,
86-
t,
71+
vertical_diffusion_boundary_layer_tendency!(Yₜ, Y, p, t, p.atmos.vertical_diffusion)
72+
73+
vertical_diffusion_boundary_layer_tendency!(_, _, _, _, ::Nothing) = nothing
74+
75+
function vertical_diffusion_boundary_layer_tendency!(Yₜ, Y, p, _,
8776
::Union{VerticalDiffusion, DecayWithHeightDiffusion},
8877
)
89-
FT = eltype(Y)
9078
(; vertical_diffusion) = p.atmos
9179
α_vert_diff_tracer = CAP.α_vert_diff_tracer(p.params)
9280
thermo_params = CAP.thermodynamics_params(p.params)
@@ -96,33 +84,20 @@ function vertical_diffusion_boundary_layer_tendency!(
9684
if vertical_diffusion isa DecayWithHeightDiffusion
9785
ᶜK_h .= ᶜcompute_eddy_diffusivity_coefficient(Y.c.ρ, vertical_diffusion)
9886
elseif vertical_diffusion isa VerticalDiffusion
99-
ᶜK_h .= ᶜcompute_eddy_diffusivity_coefficient(
100-
Y.c.uₕ,
101-
ᶜp,
102-
vertical_diffusion,
103-
)
87+
ᶜK_h .= ᶜcompute_eddy_diffusivity_coefficient(Y.c.uₕ, ᶜp, vertical_diffusion)
10488
end
10589

10690
if !disable_momentum_vertical_diffusion(p.atmos.vertical_diffusion)
10791
ᶠstrain_rate = compute_strain_rate_face_vertical(ᶜu)
108-
@. Yₜ.c.uₕ -= C12(
109-
ᶜdivᵥ(-2 * ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h) * ᶠstrain_rate) / Y.c.ρ,
110-
) # assumes ᶜK_u = ᶜK_h
92+
@. Yₜ.c.uₕ -= C12(ᶜdivᵥ(-2 * ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h) * ᶠstrain_rate) / Y.c.ρ) # assumes ᶜK_u = ᶜK_h
11193
end
11294

113-
ᶜdivᵥ_ρe_tot = Operators.DivergenceF2C(
114-
top = Operators.SetValue(C3(0)),
115-
bottom = Operators.SetValue(C3(0)),
116-
)
117-
ᶜh_tot = @. lazy(
118-
TD.total_specific_enthalpy(
119-
thermo_params,
120-
ᶜts,
121-
specific(Y.c.ρe_tot, Y.c.ρ),
122-
),
123-
)
124-
@. Yₜ.c.ρe_tot -=
125-
ᶜdivᵥ_ρe_tot(-(ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h) * ᶠgradᵥ(ᶜh_tot)))
95+
top = bottom = Operators.SetValue(C3(0))
96+
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(; top, bottom)
97+
98+
e_tot = @. lazy(specific(Y.c.ρe_tot, Y.c.ρ))
99+
ᶜh_tot = @. lazy(TD.total_specific_enthalpy(thermo_params, ᶜts, e_tot))
100+
@. Yₜ.c.ρe_tot -= ᶜdivᵥ_ρχ(-(ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h) * ᶠgradᵥ(ᶜh_tot)))
126101

127102
ᶜρχₜ_diffusion = p.scratch.ᶜtemp_scalar_2
128103
ᶜK_h_scaled = p.scratch.ᶜtemp_scalar_3
@@ -133,17 +108,8 @@ function vertical_diffusion_boundary_layer_tendency!(
133108
else
134109
@. ᶜK_h_scaled = ᶜK_h
135110
end
136-
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(
137-
top = Operators.SetValue(C3(0)),
138-
bottom = Operators.SetValue(C3(0)),
139-
)
140-
@. ᶜρχₜ_diffusion = ᶜdivᵥ_ρχ(
141-
-(
142-
ᶠinterp(Y.c.ρ) *
143-
ᶠinterp(ᶜK_h_scaled) *
144-
ᶠgradᵥ(specific(ᶜρχ, Y.c.ρ))
145-
),
146-
)
111+
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
112+
@. ᶜρχₜ_diffusion = ᶜdivᵥ_ρχ(-(ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h_scaled) * ᶠgradᵥ(ᶜχ)))
147113
@. ᶜρχₜ -= ᶜρχₜ_diffusion
148114
# Only add contribution from total water diffusion to mass tendency
149115
# (exclude contributions from diffusion of condensate, precipitation)

0 commit comments

Comments
 (0)