Skip to content

Commit 0b0243f

Browse files
committed
fix cached boundary values in env
1 parent 8c8917b commit 0b0243f

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

reproducibility_tests/ref_counter.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
258
1+
259
22

33
# **README**
44
#
@@ -20,6 +20,9 @@
2020

2121

2222
#=
23+
259
24+
- Fix env boundary values when setting implicit and explicit cache
25+
2326
258
2427
- Update deps, specifically ClimaParams.jl and Thermodynamics.jl
2528

src/cache/precomputed_quantities.jl

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,31 @@ end
411411

412412
"""
413413
set_implicit_precomputed_quantities!(Y, p, t)
414+
set_implicit_precomputed_quantities_part1!(Y, p, t)
415+
set_implicit_precomputed_quantities_part2!(Y, p, t)
414416
415-
Updates the precomputed quantities that are handled implicitly based on the
416-
current state `Y`. This is called before each evaluation of either
417-
`implicit_tendency!` or `remaining_tendency!`, and it includes quantities used
417+
Update the precomputed quantities that are handled implicitly based on the
418+
current state `Y`. These are called before each evaluation of either
419+
`implicit_tendency!` or `remaining_tendency!`, and they include quantities used
418420
in both tedencies.
419421
420-
This function also applies a "filter" to `Y` in order to ensure that `ᶠu³` is 0
422+
These functions also apply a "filter" to `Y` in order to ensure that `ᶠu³` is 0
421423
at the surface (i.e., to enforce the impenetrable boundary condition). If the
422424
`turbconv_model` is EDMFX, the filter also ensures that `ᶠu³⁰` and `ᶠu³ʲs` are 0
423425
at the surface. In the future, we will probably want to move this filtering
424426
elsewhere, but doing it here ensures that it occurs whenever the precomputed
425427
quantities are updated.
428+
429+
These functions are split into two parts so that the first stage of the implicit
430+
and explicit calculations can be executed in sequence before completing the
431+
remaining steps. This ordering is required to correctly compute variables at
432+
the environment boundary after applying the boundary conditions.
426433
"""
427434
NVTX.@annotate function set_implicit_precomputed_quantities!(Y, p, t)
435+
set_implicit_precomputed_quantities_part1!(Y, p, t)
436+
set_implicit_precomputed_quantities_part2!(Y, p, t)
437+
end
438+
NVTX.@annotate function set_implicit_precomputed_quantities_part1!(Y, p, t)
428439
(; turbconv_model, moisture_model, microphysics_model) = p.atmos
429440
(; ᶜΦ) = p.core
430441
(; ᶜu, ᶠu³, ᶠu, ᶜK, ᶜts, ᶜp) = p.precomputed
@@ -463,6 +474,16 @@ NVTX.@annotate function set_implicit_precomputed_quantities!(Y, p, t)
463474

464475
if turbconv_model isa PrognosticEDMFX
465476
set_prognostic_edmf_precomputed_quantities_draft!(Y, p, ᶠuₕ³, t)
477+
elseif !(isnothing(turbconv_model))
478+
# Do nothing for other turbconv models for now
479+
end
480+
end
481+
NVTX.@annotate function set_implicit_precomputed_quantities_part2!(Y, p, t)
482+
(; turbconv_model) = p.atmos
483+
ᶠuₕ³ = p.scratch.ᶠtemp_CT3
484+
@. ᶠuₕ³ = $compute_ᶠuₕ³(Y.c.uₕ, Y.c.ρ)
485+
486+
if turbconv_model isa PrognosticEDMFX
466487
set_prognostic_edmf_precomputed_quantities_environment!(Y, p, ᶠuₕ³, t)
467488
set_prognostic_edmf_precomputed_quantities_implicit_closures!(Y, p, t)
468489
elseif !(isnothing(turbconv_model))
@@ -471,20 +492,22 @@ NVTX.@annotate function set_implicit_precomputed_quantities!(Y, p, t)
471492
end
472493

473494
"""
474-
set_explicit_precomputed_quantities!(Y, p, t)
495+
set_explicit_precomputed_quantities_part1!(Y, p, t)
496+
set_explicit_precomputed_quantities_part2!(Y, p, t)
475497
476-
Updates the precomputed quantities that are handled explicitly based on the
477-
current state `Y`. This is only called before each evaluation of
478-
`remaining_tendency!`, though it includes quantities used in both
498+
Update the precomputed quantities that are handled explicitly based on the
499+
current state `Y`. These are only called before each evaluation of
500+
`remaining_tendency!`, though they include quantities used in both
479501
`implicit_tendency!` and `remaining_tendency!`.
502+
503+
These functions are split into two parts so that the first stage of the implicit
504+
and explicit calculations can be executed in sequence before completing the
505+
remaining steps. This ordering is required to correctly compute variables at
506+
the environment boundary after applying the boundary conditions.
480507
"""
481-
NVTX.@annotate function set_explicit_precomputed_quantities!(Y, p, t)
482-
(; turbconv_model, moisture_model, microphysics_model, cloud_model) =
483-
p.atmos
484-
(; vertical_diffusion, call_cloud_diagnostics_per_stage) = p.atmos
485-
(; ᶜΦ) = p.core
486-
(; ᶜu, ᶜts, ᶜp) = p.precomputed
487-
ᶠuₕ³ = p.scratch.ᶠtemp_CT3 # updated in set_implicit_precomputed_quantities!
508+
NVTX.@annotate function set_explicit_precomputed_quantities_part1!(Y, p, t)
509+
(; turbconv_model) = p.atmos
510+
(; ᶜts) = p.precomputed
488511
thermo_params = CAP.thermodynamics_params(p.params)
489512

490513
if !isnothing(p.sfc_setup)
@@ -502,6 +525,19 @@ NVTX.@annotate function set_explicit_precomputed_quantities!(Y, p, t)
502525

503526
if turbconv_model isa PrognosticEDMFX
504527
set_prognostic_edmf_precomputed_quantities_bottom_bc!(Y, p, t)
528+
elseif turbconv_model isa DiagnosticEDMFX
529+
set_diagnostic_edmf_precomputed_quantities_bottom_bc!(Y, p, t)
530+
elseif !(isnothing(turbconv_model))
531+
# Do nothing for other turbconv models for now
532+
end
533+
534+
return nothing
535+
end
536+
NVTX.@annotate function set_explicit_precomputed_quantities_part2!(Y, p, t)
537+
(; turbconv_model, moisture_model, cloud_model) = p.atmos
538+
(; call_cloud_diagnostics_per_stage) = p.atmos
539+
540+
if turbconv_model isa PrognosticEDMFX
505541
set_prognostic_edmf_precomputed_quantities_explicit_closures!(Y, p, t)
506542
set_prognostic_edmf_precomputed_quantities_precipitation!(
507543
Y,
@@ -510,7 +546,6 @@ NVTX.@annotate function set_explicit_precomputed_quantities!(Y, p, t)
510546
)
511547
end
512548
if turbconv_model isa DiagnosticEDMFX
513-
set_diagnostic_edmf_precomputed_quantities_bottom_bc!(Y, p, t)
514549
set_diagnostic_edmf_precomputed_quantities_do_integral!(Y, p, t)
515550
set_diagnostic_edmf_precomputed_quantities_top_bc!(Y, p, t)
516551
set_diagnostic_edmf_precomputed_quantities_env_closures!(Y, p, t)
@@ -560,6 +595,8 @@ end
560595
Updates all precomputed quantities based on the current state `Y`.
561596
"""
562597
function set_precomputed_quantities!(Y, p, t)
563-
set_implicit_precomputed_quantities!(Y, p, t)
564-
set_explicit_precomputed_quantities!(Y, p, t)
598+
set_implicit_precomputed_quantities_part1!(Y, p, t)
599+
set_explicit_precomputed_quantities_part1!(Y, p, t)
600+
set_implicit_precomputed_quantities_part2!(Y, p, t)
601+
set_explicit_precomputed_quantities_part2!(Y, p, t)
565602
end

src/prognostic_equations/edmfx_sgs_flux.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,12 @@ function edmfx_sgs_mass_flux_tendency!(
4242
(; edmfx_sgsflux_upwinding) = p.atmos.numerics
4343
(; ᶠu³) = p.precomputed
4444
(; ᶠu³ʲs, ᶜKʲs, ᶜρʲs) = p.precomputed
45-
(; ᶜK⁰, ᶜts⁰, ᶜts) = p.precomputed
45+
(; ᶠu³⁰, ᶜK⁰, ᶜts⁰, ᶜts) = p.precomputed
4646
(; dt) = p
4747

4848
thermo_params = CAP.thermodynamics_params(p.params)
4949
ᶜρ⁰ = @. lazy(TD.air_density(thermo_params, ᶜts⁰))
5050
ᶜρa⁰ = @. lazy(ρa⁰(Y.c.ρ, Y.c.sgsʲs, turbconv_model))
51-
# TODO Here we need to recompute ᶠu³⁰ from the mass flux constraint (instead
52-
# of using the cached value), because the cached boundary value violates the
53-
# constraint after applying the area-fraction boundary condition in
54-
# `set_explicit_precomputed_quantities!` (this is written for only one draft!)
55-
ᶠu³⁰ = @. lazy(
56-
(ᶠinterp(Y.c.ρ) * ᶠu³ - ᶠinterp(Y.c.sgsʲs.:(1).ρa) * ᶠu³ʲs.:(1)) /
57-
ᶠinterp(ᶜρa⁰),
58-
)
5951

6052
if p.atmos.edmfx_model.sgs_mass_flux isa Val{true}
6153

0 commit comments

Comments
 (0)