Skip to content

refactor(diffusion): consolidate the eddy-diffusivity pipeline and diffusive-flux application - #4669

Merged
haakon-e merged 7 commits into
he/edmf-3d-diffusionfrom
he/refactor-eddy-diffusivity-pipeline
Jul 13, 2026
Merged

refactor(diffusion): consolidate the eddy-diffusivity pipeline and diffusive-flux application#4669
haakon-e merged 7 commits into
he/edmf-3d-diffusionfrom
he/refactor-eddy-diffusivity-pipeline

Conversation

@haakon-e

@haakon-e haakon-e commented Jul 9, 2026

Copy link
Copy Markdown
Member

Consolidates the diffusion code in two layers: how eddy diffusivities are computed, and how diffusive fluxes are applied.

Coefficient layer. Extracts the TKE → mixing length → Prandtl → eddy viscosity/diffusivity chain, previously duplicated at seven sites, into a single ᶜeddy_diffusivities! function. Callers supply the mixing-length output field and optional K_u/K_h output fields, preserving each site's scratch slots and lazy or materialized policy; the closure inputs (grid_scale, buoyancy_gradient) pass through unchanged. The implicit-Jacobian scheme dispatcher is renamed eddy_diffusivity_coefficients!ᶜimplicit_diffusion_coefficients! to distinguish it from the closure chain. Migrated sites: the vertical and horizontal EDMFX SGS diffusive fluxes, the vertical and horizontal updraft diffusion, the TKE tendency for both EDMF models, and the diffusion Jacobian coefficients.

Flux-application layer. Adds two shared zero-flux boundary-condition divergence operators (ᶜdiffdivᵥ, ᶠdiffdivᵥ_u₃, next to the existing operator constants in abbreviations.jl) and two lazy helpers (ᶜdiffusive_flux_divergenceᵥ, ᶠtotal_enthalpy_gradientᵥ), then migrates the five vertical diffusion sites that rebuilt these per function — vertical Smagorinsky–Lilly, vertical AMD, vertical_diffusion_boundary_layer_tendency!, the EDMFX grid-mean SGS diffusive flux, and the EDMFX updraft vertical diffusion — one commit per site.

Behavior-preserving. The coefficient-layer extraction reproduces each site's original expression, and every flux-application commit is bit-for-bit identical to its parent. The dynamics test group (Prognostic equations, Tracer/mass transport consistency, EDMFX horizontal diffusive flux) passes on this branch, and a new unit test (test/diffusion_operators.jl) pins the shared operators and helpers, including the diffusive-flux sign, at Float32 and Float64.

Scope and limitations. The diagnostics that recompute the closure fields lazily (edt, evu, edth, evuh) are deliberately not routed through the new function, which mutates scratch; unifying them needs either a lazy variant or cached diffusivity fields. A behavior-changing correction to the vertical-diffusion tracer scaling, surfaced by this refactor, is deferred to a follow-up PR. Unifying the tendency functions themselves (a per-direction driver over a declarative field table) is out of scope here.

@haakon-e

haakon-e commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview for this PR: https://clima.github.io/ClimaAtmos.jl/previews/PR4669/

@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from b61b1f4 to 8632e7d Compare July 9, 2026 19:04
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from 25110fb to ef842c7 Compare July 9, 2026 19:04
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from 8632e7d to 6d768fa Compare July 10, 2026 01:15
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from ef842c7 to aab5f54 Compare July 10, 2026 01:15
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from 6d768fa to 2f58f5e Compare July 10, 2026 07:36
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from aab5f54 to 78f56d3 Compare July 10, 2026 07:36
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from 2f58f5e to 59f4895 Compare July 10, 2026 19:29
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from 78f56d3 to 4940847 Compare July 10, 2026 19:29
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from 59f4895 to 5af9a08 Compare July 10, 2026 20:30
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from 4940847 to c318221 Compare July 10, 2026 20:30
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from 5af9a08 to eb9ff2c Compare July 11, 2026 00:29
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from c318221 to 2152bdc Compare July 11, 2026 00:29
haakon-e added 7 commits July 10, 2026 18:33
Extract the TKE -> mixing length -> Prandtl -> eddy viscosity/diffusivity
chain, previously duplicated at seven sites, into `ᶜeddy_diffusivities!`.
Callers supply the mixing-length output field via the required
`ᶜmixing_length_field` keyword and optional `K_u`/`K_h` output fields,
preserving each site's scratch slots and lazy or materialized policy; the
closure inputs (`grid_scale`, `buoyancy_gradient`) pass through unchanged.
Migrated sites: the vertical and horizontal EDMFX SGS diffusive fluxes, the
vertical and horizontal updraft diffusion, the TKE tendency for both EDMF
models, and the diffusion Jacobian coefficients.
…helpers

Add two boundary-condition operators to the abbreviations: ᶜdiffdivᵥ for the
zero-flux scalar-flux divergence (Covariant3, matching ᶠgradᵥ output) and
ᶠdiffdivᵥ_u₃ for the zero-flux vertical-momentum divergence. Add two lazy flux
helpers to the eddy-diffusion closures: ᶜdiffusive_flux_divergenceᵥ for a single
diffusive scalar flux and ᶠtotal_enthalpy_gradientᵥ for the dry-static-energy +
water-enthalpy gradient. These centralize the per-function operators and flux
expressions currently rebuilt across the five vertical diffusion sites; no call
sites change in this commit.

Add a unit test asserting the new operators and lazy helpers reproduce the
per-function forms they replace, over both float types.
… helpers

Replace the per-function zero-flux divergence operators and gradient shadow in
vertical_smagorinsky_lilly_tendency! with the shared ᶜdiffdivᵥ, ᶠdiffdivᵥ_u₃,
and ᶜdiffusive_flux_divergenceᵥ, folding ρ and D into the flux coefficient in the
existing left-to-right order. Delete the momentum-flux boundary-correction term,
which is the divergence of a zero flux with a zero-value boundary and so
evaluates to zero identically.
Replace the per-function zero-flux divergence operators and gradient shadow in
vertical_amd_tendency! with the shared ᶜdiffdivᵥ, ᶠdiffdivᵥ_u₃, and
ᶜdiffusive_flux_divergenceᵥ. The energy term stays materialized before the
tracer loop, since the loop overwrites the ᶠD_amd scratch field per tracer.
Delete the momentum-flux boundary-correction term, which is the divergence of a
zero flux with a zero-value boundary and evaluates to zero identically. The AMD
diffusivity gradient operators (ᶠgradᵥ_uvw, ᶠgradᵥ_scalar) are retained.
…elpers

Replace the zero-flux divergence operators and gradient shadow in
vertical_diffusion_boundary_layer_tendency! with the shared ᶜdiffdivᵥ,
ᶠtotal_enthalpy_gradientᵥ (energy), and ᶜdiffusive_flux_divergenceᵥ (tracers).
The tracer eddy diffusivity keeps its existing scaling placement (α folded into
K_h before interpolation). The momentum term is unchanged.
…helpers

Replace the zero-flux divergence operators in edmfx_sgs_diffusive_flux_tendency!
with the shared ᶜdiffdivᵥ, ᶠtotal_enthalpy_gradientᵥ (energy), and
ᶜdiffusive_flux_divergenceᵥ (q_tot and tracers). The TKE flux keeps its own
divergence operator because it carries a nonzero surface-flux bottom boundary,
and the momentum term is unchanged.
…hared helpers

Replace the zero-flux divergence operators in edmfx_vertical_diffusion_tendency!
with the shared ᶜdiffusive_flux_divergenceᵥ for the updraft mse, q_tot, and SGS
tracer fluxes, keeping the trailing division by the updraft density.
@haakon-e
haakon-e force-pushed the he/edmfx-updraft-horizontal-diffusion branch from eb9ff2c to 7ab7415 Compare July 11, 2026 01:33
@haakon-e
haakon-e force-pushed the he/refactor-eddy-diffusivity-pipeline branch from 2152bdc to e91b55b Compare July 11, 2026 01:33
@haakon-e haakon-e changed the title refactor(diffusion): consolidate the TKE eddy-diffusivity pipeline refactor(diffusion): consolidate the eddy-diffusivity pipeline and diffusive-flux application Jul 11, 2026
Base automatically changed from he/edmfx-updraft-horizontal-diffusion to he/edmf-3d-diffusion July 13, 2026 17:50
@haakon-e
haakon-e merged commit e91b55b into he/edmf-3d-diffusion Jul 13, 2026
27 of 46 checks passed
@haakon-e
haakon-e deleted the he/refactor-eddy-diffusivity-pipeline branch July 13, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant