Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn cache into struct #2296

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 94 additions & 11 deletions src/cache/cache.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,85 @@
struct AtmosCache{
FT <: AbstractFloat,
SIM,
AM,
NUM,
CAP,
COR,
SFC,
GHOST,
ENV,
PREC,
SCRA,
HYPE,
DSS,
RS,
VS,
PR,
SUB,
LSAD,
EDMFCOR,
FOR,
NONGW,
ORGW,
RAD,
NETFLUXTOA,
NETFLUXSFC,
}
"""Timestep of the simulation (in seconds). This is also used by callbacks and tendencies"""
dt::FT

"""NamedTuple with job_id, output_dir (used in post), and start_date (used for insolation)."""
simulation::SIM

"""AtmosModel"""
atmos::AM

"""Limiter"""
numerics::NUM

"""ClimaAtmosParameters that have to be used"""
params::CAP

"""Variables that are used generally, such as ᶜρ_ref, ᶜΦ"""
core::COR

"""Used by update_surface_conditions! in set_precomputed_quantities! and coupler"""
sfc_setup::SFC

"""Center and face ghost buffers used by DSS"""
ghost_buffer::GHOST

env_thermo_quad::ENV

"""Quantities that are updated with set_precomputed_quantities!"""
precomputed::PREC

"""Pre-allocated areas of memory to store temporary values"""
scratch::SCRA

"""Hyperdiffision quantities for grid and subgrid scale quantities, potentially with
ghost buffers for DSS"""
hyperdiff::HYPE

do_dss::DSS

"""Additional parameters used by the various tendencies"""
rayleigh_sponge::RS
viscous_sponge::VS
precipitation::PR
subsidence::SUB
large_scale_advection::LSAD
edmf_coriolis::EDMFCOR
forcing::FOR
non_orographic_gravity_wave::NONGW
orographic_gravity_wave::ORGW
radiation::RAD

"""Net energy flux coming through top of atmosphere and surface"""
net_energy_flux_toa::NETFLUXTOA
net_energy_flux_sfc::NETFLUXSFC
end

# Functions on which the model depends:
# CAP.R_d(params) # dry specific gas constant
# CAP.kappa_d(params) # dry adiabatic exponent
Expand Down Expand Up @@ -43,15 +125,15 @@ function build_cache(Y, atmos, params, surface_setup, simulation)
!do_dss ? (;) :
(; c = Spaces.create_dss_buffer(Y.c), f = Spaces.create_dss_buffer(Y.f))

net_energy_flux_toa = [Geometry.WVector(FT(0))]
net_energy_flux_sfc = [Geometry.WVector(FT(0))]

limiter =
isnothing(atmos.numerics.limiter) ? nothing :
atmos.numerics.limiter(similar(Y.c, FT))

numerics = (; limiter)

net_energy_flux_toa = [Geometry.WVector(FT(0))]
net_energy_flux_sfc = [Geometry.WVector(FT(0))]

core = (
ᶜΦ,
ᶠgradᵥ_ᶜΦ = ᶠgradᵥ.(ᶜΦ),
Expand Down Expand Up @@ -92,16 +174,20 @@ function build_cache(Y, atmos, params, surface_setup, simulation)
orographic_gravity_wave = orographic_gravity_wave_cache(Y, atmos)
radiation = radiation_model_cache(Y, atmos, radiation_args...)

p = (;
args = (
simulation.dt,
simulation,
atmos,
numerics,
params,
core,
sfc_setup,
params,
do_dss,
ghost_buffer,
env_thermo_quad,
precomputed,
scratch,
hyperdiff,
do_dss,
rayleigh_sponge,
viscous_sponge,
precipitation,
Expand All @@ -112,12 +198,9 @@ function build_cache(Y, atmos, params, surface_setup, simulation)
non_orographic_gravity_wave,
orographic_gravity_wave,
radiation,
env_thermo_quad,
ghost_buffer,
dt = simulation.dt,
net_energy_flux_toa,
net_energy_flux_sfc,
numerics,
)
return p

return AtmosCache{map(typeof, args)...}(args...)
end
28 changes: 27 additions & 1 deletion test/coupler_compatibility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,33 @@ const T2 = 290
)
sfc_setup = similar(Spaces.level(Y.f, half), typeof(surface_state))
@. sfc_setup = (surface_state,)
p_overwritten = merge(p, (; sfc_setup))
p_overwritten = CA.AtmosCache(
p.simulation.dt,
p.simulation,
p.atmos,
p.numerics,
p.params,
p.core,
sfc_setup,
p.ghost_buffer,
p.env_thermo_quad,
p.precomputed,
p.scratch,
p.hyperdiff,
p.do_dss,
p.rayleigh_sponge,
p.viscous_sponge,
p.precipitation,
p.subsidence,
p.large_scale_advection,
p.edmf_coriolis,
p.forcing,
p.non_orographic_gravity_wave,
p.orographic_gravity_wave,
p.radiation,
p.net_energy_flux_toa,
p.net_energy_flux_sfc,
)

# Test that set_precomputed_quantities! can be used to update the surface
# temperature to T1 and then to T2.
Expand Down
Loading