Skip to content

Commit 00082c0

Browse files
Merge pull request #3447 from CliMA/ck/elim_some_cache
Eliminate ViscousSponge cache, fix unit test
2 parents 116608a + 704a0ac commit 00082c0

File tree

13 files changed

+96
-83
lines changed

13 files changed

+96
-83
lines changed

src/ClimaAtmos.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ClimaAtmos
33
using NVTX
44
import Thermodynamics as TD
55

6+
include("compat.jl")
67
include(joinpath("parameters", "Parameters.jl"))
78
import .Parameters as CAP
89

src/cache/cache.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ struct AtmosCache{
1414
SCRA,
1515
HYPE,
1616
DSS,
17-
RS,
18-
VS,
1917
PR,
2018
LSAD,
2119
EXTFORCING,
@@ -76,8 +74,6 @@ struct AtmosCache{
7674
do_dss::DSS
7775

7876
"""Additional parameters used by the various tendencies"""
79-
rayleigh_sponge::RS
80-
viscous_sponge::VS
8177
precipitation::PR
8278
large_scale_advection::LSAD
8379
external_forcing::EXTFORCING
@@ -183,8 +179,6 @@ function build_cache(Y, atmos, params, surface_setup, sim_info, aerosol_names)
183179
(start_date, params, atmos.ozone, aerosol_names, atmos.insolation) : ()
184180

185181
hyperdiff = hyperdiffusion_cache(Y, atmos)
186-
rayleigh_sponge = rayleigh_sponge_cache(Y, atmos)
187-
viscous_sponge = viscous_sponge_cache(Y, atmos)
188182
precipitation = precipitation_cache(Y, atmos)
189183
large_scale_advection = large_scale_advection_cache(Y, atmos)
190184
external_forcing = external_forcing_cache(Y, atmos, params)
@@ -211,8 +205,6 @@ function build_cache(Y, atmos, params, surface_setup, sim_info, aerosol_names)
211205
scratch,
212206
hyperdiff,
213207
do_dss,
214-
rayleigh_sponge,
215-
viscous_sponge,
216208
precipitation,
217209
large_scale_advection,
218210
external_forcing,

src/compat.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ClimaCore
2+
import ClimaCore: Domains, Spaces, Topologies
3+
4+
# To allow for backwards compatibility of ClimaCore:
5+
if pkgversion(ClimaCore) < v"0.14.18"
6+
"""
7+
z_max(::ClimaCore.Spaces.AbstractSpace)
8+
9+
The domain maximum along the z-direction.
10+
"""
11+
function z_max end
12+
13+
z_max(domain::Domains.IntervalDomain) = domain.coord_max.z
14+
function z_max(space::Spaces.AbstractSpace)
15+
mesh = Topologies.mesh(Spaces.vertical_topology(space))
16+
domain = Topologies.domain(mesh)
17+
return z_max(domain)
18+
end
19+
20+
else
21+
z_max(s::Spaces.AbstractSpace) = Spaces.z_max(s)
22+
end

src/parameterized_tendencies/sponge/rayleigh_sponge.jl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44

55
import ClimaCore.Fields as Fields
66

7-
rayleigh_sponge_cache(Y, atmos::AtmosModel) =
8-
rayleigh_sponge_cache(Y, atmos.rayleigh_sponge)
9-
10-
rayleigh_sponge_cache(Y, ::Nothing) = (;)
117
rayleigh_sponge_tendency!(Yₜ, Y, p, t, ::Nothing) = nothing
128

13-
rayleigh_sponge_cache(Y, rs::RayleighSponge) = nothing
14-
159
αₘ(s::RayleighSponge{FT}, z, α) where {FT} = ifelse(z > s.zd, α, FT(0))
16-
ζ_rayleigh(s::RayleighSponge{FT}, z) where {FT} =
17-
sin(FT(π) / 2 * (z - s.zd) / (s.zmax - s.zd))^2
18-
β_rayleigh_uₕ(s::RayleighSponge{FT}, z) where {FT} =
19-
αₘ(s, z, s.α_uₕ) * ζ_rayleigh(s, z)
20-
β_rayleigh_w(s::RayleighSponge{FT}, z) where {FT} =
21-
αₘ(s, z, s.α_w) * ζ_rayleigh(s, z)
10+
ζ_rayleigh(s::RayleighSponge{FT}, z, zmax) where {FT} =
11+
sin(FT(π) / 2 * (z - s.zd) / (zmax - s.zd))^2
12+
β_rayleigh_uₕ(s::RayleighSponge{FT}, z, zmax) where {FT} =
13+
αₘ(s, z, s.α_uₕ) * ζ_rayleigh(s, z, zmax)
14+
β_rayleigh_w(s::RayleighSponge{FT}, z, zmax) where {FT} =
15+
αₘ(s, z, s.α_w) * ζ_rayleigh(s, z, zmax)
2216

2317
function rayleigh_sponge_tendency!(Yₜ, Y, p, t, s::RayleighSponge)
2418
ᶜz = Fields.coordinate_field(Y.c).z
25-
@. Yₜ.c.uₕ -= β_rayleigh_uₕ(s, ᶜz) * Y.c.uₕ
19+
zmax = z_max(axes(Y.f))
20+
@. Yₜ.c.uₕ -= β_rayleigh_uₕ(s, ᶜz, zmax) * Y.c.uₕ
2621
end

src/parameterized_tendencies/sponge/viscous_sponge.jl

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,35 @@ import ClimaCore.Fields as Fields
66
import ClimaCore.Geometry as Geometry
77
import ClimaCore.Spaces as Spaces
88

9-
viscous_sponge_cache(Y, atmos::AtmosModel) =
10-
viscous_sponge_cache(Y, atmos.viscous_sponge)
119

12-
viscous_sponge_cache(Y, ::Nothing) = (;)
1310
viscous_sponge_tendency!(Yₜ, Y, p, t, ::Nothing) = nothing
1411

15-
function viscous_sponge_cache(Y, viscous_sponge::ViscousSponge)
16-
(; κ₂, zd) = viscous_sponge
17-
FT = Spaces.undertype(axes(Y.c))
18-
ᶜz = Fields.coordinate_field(Y.c).z
19-
ᶠz = Fields.coordinate_field(Y.f).z
20-
ᶜαₘ = @. ifelse(ᶜz > zd, κ₂, FT(0))
21-
ᶠαₘ = @. ifelse(ᶠz > zd, κ₂, FT(0))
22-
zmax = maximum(ᶠz)
23-
ᶜβ_viscous = @. ᶜαₘ * sin(FT(π) / 2 * (ᶜz - zd) / (zmax - zd))^2
24-
ᶠβ_viscous = @. ᶠαₘ * sin(FT(π) / 2 * (ᶠz - zd) / (zmax - zd))^2
25-
return (; ᶜβ_viscous, ᶠβ_viscous)
26-
end
12+
αₘ(s::ViscousSponge{FT}, z) where {FT} = ifelse(z > s.zd, s.κ₂, FT(0))
13+
ζ_viscous(s::ViscousSponge{FT}, z, zmax) where {FT} =
14+
sin(FT(π) / 2 * (z - s.zd) / (zmax - s.zd))^2
15+
β_viscous(s::ViscousSponge{FT}, z, zmax) where {FT} =
16+
αₘ(s, z) * ζ_viscous(s, z, zmax)
2717

28-
function viscous_sponge_tendency!(Yₜ, Y, p, t, ::ViscousSponge)
29-
(; ᶜβ_viscous, ᶠβ_viscous) = p.viscous_sponge
18+
function viscous_sponge_tendency!(Yₜ, Y, p, t, s::ViscousSponge)
3019
(; ᶜh_tot, ᶜspecific) = p.precomputed
3120
ᶜuₕ = Y.c.uₕ
21+
ᶜz = Fields.coordinate_field(Y.c).z
22+
ᶠz = Fields.coordinate_field(Y.f).z
23+
zmax = z_max(axes(ᶠz))
3224
@. Yₜ.c.uₕ +=
33-
ᶜβ_viscous * (
25+
β_viscous(s, ᶜz, zmax) * (
3426
wgradₕ(divₕ(ᶜuₕ)) - Geometry.project(
3527
Geometry.Covariant12Axis(),
3628
wcurlₕ(Geometry.project(Geometry.Covariant3Axis(), curlₕ(ᶜuₕ))),
3729
)
3830
)
3931
@. Yₜ.f.u₃.components.data.:1 +=
40-
ᶠβ_viscous * wdivₕ(gradₕ(Y.f.u₃.components.data.:1))
32+
β_viscous(s, ᶠz, zmax) * wdivₕ(gradₕ(Y.f.u₃.components.data.:1))
4133

42-
@. Yₜ.c.ρe_tot += ᶜβ_viscous * wdivₕ(Y.c.ρ * gradₕ(ᶜh_tot))
34+
@. Yₜ.c.ρe_tot += β_viscous(s, ᶜz, zmax) * wdivₕ(Y.c.ρ * gradₕ(ᶜh_tot))
4335
for (ᶜρχₜ, ᶜχ, χ_name) in matching_subfields(Yₜ.c, ᶜspecific)
4436
χ_name == :e_tot && continue
45-
@. ᶜρχₜ += ᶜβ_viscous * wdivₕ(Y.c.ρ * gradₕ(ᶜχ))
46-
@. Yₜ.c.ρ += ᶜβ_viscous * wdivₕ(Y.c.ρ * gradₕ(ᶜχ))
37+
@. ᶜρχₜ += β_viscous(s, ᶜz, zmax) * wdivₕ(Y.c.ρ * gradₕ(ᶜχ))
38+
@. Yₜ.c.ρ += β_viscous(s, ᶜz, zmax) * wdivₕ(Y.c.ρ * gradₕ(ᶜχ))
4739
end
4840
end

src/prognostic_equations/implicit/implicit_solver.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,13 @@ function update_implicit_equation_jacobian!(A, Y, p, dtγ)
568568
dtγ * ᶠp_grad_matrix DiagonalMatrixRow(-(ᶜkappa_m) * ᶜρ) ∂ᶜK_∂ᶜuₕ
569569
rs = p.atmos.rayleigh_sponge
570570
ᶠz = Fields.coordinate_field(Y.f).z
571+
zmax = z_max(axes(Y.f))
571572
if rs isa RayleighSponge
572573
@. ∂ᶠu₃_err_∂ᶠu₃ =
573574
dtγ * (
574575
ᶠp_grad_matrix DiagonalMatrixRow(-(ᶜkappa_m) * ᶜρ)
575576
∂ᶜK_∂ᶠu₃ +
576-
DiagonalMatrixRow(-β_rayleigh_w(rs, ᶠz) * (one_C3xACT3,))
577+
DiagonalMatrixRow(-β_rayleigh_w(rs, ᶠz, zmax) * (one_C3xACT3,))
577578
) - (I_u₃,)
578579
else
579580
@. ∂ᶠu₃_err_∂ᶠu₃ =
@@ -849,7 +850,7 @@ function update_implicit_equation_jacobian!(A, Y, p, dtγ)
849850
ᶠtridiagonal_matrix_c3
850851
DiagonalMatrixRow(adjoint(CT3(Y.f.sgsʲs.:(1).u₃))) -
851852
DiagonalMatrixRow(
852-
β_rayleigh_w(rs, ᶠz) * (one_C3xACT3,),
853+
β_rayleigh_w(rs, ᶠz, zmax) * (one_C3xACT3,),
853854
)
854855
) - (I_u₃,)
855856
else
@@ -862,8 +863,9 @@ function update_implicit_equation_jacobian!(A, Y, p, dtγ)
862863
matrix[@name(f.sgsʲs.:(1).u₃), @name(f.sgsʲs.:(1).u₃)]
863864
@. ∂ᶠu₃ʲ_err_∂ᶠu₃ʲ =
864865
dtγ *
865-
-DiagonalMatrixRow(β_rayleigh_w(rs, ᶠz) * (one_C3xACT3,)) -
866-
(I_u₃,)
866+
-DiagonalMatrixRow(
867+
β_rayleigh_w(rs, ᶠz, zmax) * (one_C3xACT3,),
868+
) - (I_u₃,)
867869
end
868870
end
869871

src/prognostic_equations/implicit/implicit_tendency.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ function implicit_vertical_advection_tendency!(Yₜ, Y, p, t)
166166

167167
if rayleigh_sponge isa RayleighSponge
168168
ᶠz = Fields.coordinate_field(Y.f).z
169+
zmax = z_max(axes(Y.f))
169170
rs = rayleigh_sponge
170-
@. Yₜ.f.u₃ -= β_rayleigh_w(rs, ᶠz) * Y.f.u₃
171+
@. Yₜ.f.u₃ -= β_rayleigh_w(rs, ᶠz, zmax) * Y.f.u₃
171172
if turbconv_model isa PrognosticEDMFX
172173
for j in 1:n
173174
@. Yₜ.f.sgsʲs.:($$j).u₃ -=
174-
β_rayleigh_w(rs, ᶠz) * Y.f.sgsʲs.:($$j).u₃
175+
β_rayleigh_w(rs, ᶠz, zmax) * Y.f.sgsʲs.:($$j).u₃
175176
end
176177
end
177178
end

src/solver/model_getters.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,13 @@ end
158158

159159
function get_rayleigh_sponge_model(parsed_args, params, ::Type{FT}) where {FT}
160160
rs_name = parsed_args["rayleigh_sponge"]
161-
zmax = parsed_args["z_max"]
162161
return if rs_name in ("false", false)
163162
nothing
164163
elseif rs_name in ("true", true, "RayleighSponge")
165164
zd = params.zd_rayleigh
166165
α_uₕ = params.alpha_rayleigh_uh
167166
α_w = params.alpha_rayleigh_w
168-
RayleighSponge{FT}(; zmax, zd, α_uₕ, α_w)
167+
RayleighSponge{FT}(; zd, α_uₕ, α_w)
169168
else
170169
error("Uncaught rayleigh sponge model `$rs_name`.")
171170
end

src/solver/types.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,17 @@ diffuse_momentum(::FriersonDiffusion{DM}) where {DM} = DM
120120
diffuse_momentum(::Nothing) = false
121121

122122
abstract type AbstractSponge end
123+
Base.Broadcast.broadcastable(x::AbstractSponge) = tuple(x)
123124
Base.@kwdef struct ViscousSponge{FT} <: AbstractSponge
124125
zd::FT
125126
κ₂::FT
126127
end
127128

128129
Base.@kwdef struct RayleighSponge{FT} <: AbstractSponge
129-
zmax::FT
130130
zd::FT
131131
α_uₕ::FT
132132
α_w::FT
133133
end
134-
Base.Broadcast.broadcastable(x::RayleighSponge) = tuple(x)
135134

136135
abstract type AbstractGravityWave end
137136
Base.@kwdef struct NonOrographyGravityWave{FT} <: AbstractGravityWave

test/coupler_compatibility.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ const T2 = 290
8080
p.scratch,
8181
p.hyperdiff,
8282
p.do_dss,
83-
p.rayleigh_sponge,
84-
p.viscous_sponge,
8583
p.precipitation,
8684
p.large_scale_advection,
8785
p.external_forcing,

0 commit comments

Comments
 (0)