Skip to content

Commit f2a8fb3

Browse files
(0.93.2) Update Adapt.jl compat and fix Float32 CATKE on GPU (#3876)
Co-authored-by: Gregory L. Wagner <[email protected]>
1 parent ea7eb5f commit f2a8fb3

File tree

6 files changed

+57
-25
lines changed

6 files changed

+57
-25
lines changed

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Oceananigans"
22
uuid = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
33
authors = ["Climate Modeling Alliance and contributors"]
4-
version = "0.93.1"
4+
version = "0.93.2"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -43,7 +43,7 @@ OceananigansEnzymeExt = "Enzyme"
4343
OceananigansMakieExt = ["MakieCore", "Makie"]
4444

4545
[compat]
46-
Adapt = "3, 4"
46+
Adapt = "^4.1.1"
4747
CUDA = "4.1.1, 5"
4848
Crayons = "4"
4949
CubedSphere = "0.2, 0.3"

src/Grids/grid_generation.jl

-1
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,3 @@ generate_coordinate(FT, ::Flat, N, H, c::Number, coordinate_name, arch) =
129129

130130
generate_coordinate(FT, ::Flat, N, H, ::Nothing, coordinate_name, arch) =
131131
FT(1), nothing, nothing, FT(1), FT(1)
132-

src/TurbulenceClosures/turbulence_closure_implementations/TKEBasedVerticalDiffusivities/catke_mixing_length.jl

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Base.@kwdef struct CATKEMixingLength{FT}
1616
:: FT = 1.131 # Surface distance coefficient for shear length scale
1717
Cᵇ :: FT = Inf # Bottom distance coefficient for shear length scale
1818
Cˢᵖ :: FT = 0.505 # Sheared convective plume coefficient
19-
CRiᵟ :: FT = 1.02 # Stability function width
19+
CRiᵟ :: FT = 1.02 # Stability function width
2020
CRi⁰ :: FT = 0.254 # Stability function lower Ri
2121
Cʰⁱu :: FT = 0.242 # Shear mixing length coefficient for momentum at high Ri
2222
Cˡᵒu :: FT = 0.361 # Shear mixing length coefficient for momentum at low Ri
@@ -131,7 +131,7 @@ end
131131
ϵˢᵖ = 1 - Cˢᵖ * Riᶠ # ϵ = Sheared convection factor
132132
ℓᵉ = clip(ϵˢᵖ * ℓᵉ)
133133
=#
134-
134+
135135
# Figure out which mixing length applies
136136
convecting = (Jᵇ > Jᵇᵋ) & (N² < 0)
137137
entraining = (Jᵇ > Jᵇᵋ) & (N² > 0) & (N²_above < 0)
@@ -299,4 +299,3 @@ Base.show(io::IO, ml::CATKEMixingLength) =
299299
" ├── Cˢᵖ: ", ml.Cˢᵖ, '\n',
300300
" ├── CRiᵟ: ", ml.CRiᵟ, '\n',
301301
" └── CRi⁰: ", ml.CRi⁰)
302-

src/TurbulenceClosures/turbulence_closure_implementations/TKEBasedVerticalDiffusivities/catke_vertical_diffusivity.jl

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
struct CATKEVerticalDiffusivity{TD, CL, FT, DT, TKE} <: AbstractScalarDiffusivity{TD, VerticalFormulation, 2}
32
mixing_length :: CL
43
turbulent_kinetic_energy_equation :: TKE
@@ -18,7 +17,7 @@ function CATKEVerticalDiffusivity{TD}(mixing_length::CL,
1817
maximum_viscosity::FT,
1918
minimum_tke::FT,
2019
minimum_convective_buoyancy_flux::FT,
21-
negative_tke_damping_time_scale::FT,
20+
negative_tke_damping_time_scale::FT,
2221
tke_time_step::DT) where {TD, CL, FT, DT, TKE}
2322

2423
return CATKEVerticalDiffusivity{TD, CL, FT, DT, TKE}(mixing_length,
@@ -173,7 +172,7 @@ function DiffusivityFields(grid, tracer_names, bcs, closure::FlavorOfCATKE)
173172
return (; κu, κc, κe, Le, Jᵇ,
174173
previous_compute_time, previous_velocities,
175174
_tupled_tracer_diffusivities, _tupled_implicit_linear_coefficients)
176-
end
175+
end
177176

178177
@inline viscosity_location(::FlavorOfCATKE) = (c, c, f)
179178
@inline diffusivity_location(::FlavorOfCATKE) = (c, c, f)
@@ -228,7 +227,7 @@ end
228227
Jᵇᵋ = closure.minimum_convective_buoyancy_flux
229228
Jᵇᵢⱼ = @inbounds Jᵇ[i, j, 1]
230229
Jᵇ⁺ = max(Jᵇᵋ, Jᵇᵢⱼ, Jᵇ★) # selects fastest (dominant) time-scale
231-
t★ = (ℓᴰ^2 / Jᵇ⁺)^(1/3)
230+
t★ = cbrt(ℓᴰ^2 / Jᵇ⁺)
232231
ϵ = Δt / t★
233232

234233
@inbounds Jᵇ[i, j, 1] = (Jᵇᵢⱼ + ϵ * Jᵇ★) / (1 + ϵ)
@@ -263,28 +262,34 @@ end
263262
ℓu = momentum_mixing_lengthᶜᶜᶠ(i, j, k, grid, closure, velocities, tracers, buoyancy, surface_buoyancy_flux)
264263
κu = ℓu * w★
265264
κu_max = closure.maximum_viscosity
266-
return min(κu, κu_max)
265+
κu★ = min(κu, κu_max)
266+
FT = eltype(grid)
267+
return κu★::FT
267268
end
268269

269270
@inline function κcᶜᶜᶠ(i, j, k, grid, closure, velocities, tracers, buoyancy, surface_buoyancy_flux)
270271
w★ = ℑzᵃᵃᶠ(i, j, k, grid, turbulent_velocityᶜᶜᶜ, closure, tracers.e)
271272
ℓc = tracer_mixing_lengthᶜᶜᶠ(i, j, k, grid, closure, velocities, tracers, buoyancy, surface_buoyancy_flux)
272273
κc = ℓc * w★
273274
κc_max = closure.maximum_tracer_diffusivity
274-
return min(κc, κc_max)
275+
κc★ = min(κc, κc_max)
276+
FT = eltype(grid)
277+
return κc★::FT
275278
end
276279

277280
@inline function κeᶜᶜᶠ(i, j, k, grid, closure, velocities, tracers, buoyancy, surface_buoyancy_flux)
278281
w★ = ℑzᵃᵃᶠ(i, j, k, grid, turbulent_velocityᶜᶜᶜ, closure, tracers.e)
279282
ℓe = TKE_mixing_lengthᶜᶜᶠ(i, j, k, grid, closure, velocities, tracers, buoyancy, surface_buoyancy_flux)
280283
κe = ℓe * w★
281284
κe_max = closure.maximum_tke_diffusivity
282-
return min(κe, κe_max)
285+
κe★ = min(κe, κe_max)
286+
FT = eltype(grid)
287+
return κe★::FT
283288
end
284289

285290
@inline viscosity(::FlavorOfCATKE, diffusivities) = diffusivities.κu
286291
@inline diffusivity(::FlavorOfCATKE, diffusivities, ::Val{id}) where id = diffusivities._tupled_tracer_diffusivities[id]
287-
292+
288293
#####
289294
##### Show
290295
#####
@@ -334,4 +339,3 @@ function Base.show(io::IO, clo::CATKEVD)
334339
" ├── CᵂwΔ: ", prettysummary(clo.turbulent_kinetic_energy_equation.CᵂwΔ), '\n',
335340
" └── Cᵂϵ: ", prettysummary(clo.turbulent_kinetic_energy_equation.Cᵂϵ))
336341
end
337-

src/TurbulenceClosures/turbulence_closure_implementations/TKEBasedVerticalDiffusivities/time_step_catke_equation.jl

+2-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ end
117117
# Then the contribution of Jᵉ to the implicit flux is
118118
#
119119
# Lᵂ = - Cᵂϵ * √e / Δz.
120-
120+
121121
on_bottom = !inactive_cell(i, j, k, grid) & inactive_cell(i, j, k-1, grid)
122122
Δz = Δzᶜᶜᶜ(i, j, k, grid)
123123
Cᵂϵ = closure_ij.turbulent_kinetic_energy_equation.Cᵂϵ
@@ -190,7 +190,7 @@ function tracer_tendency_kernel_function(model::HFSM, ::Val{:e}, closures::Tuple
190190
else
191191
catke_closure = closures[catke_index]
192192
catke_diffusivity_fields = diffusivity_fields[catke_index]
193-
return compute_hydrostatic_free_surface_Ge!, catke_closure, catke_diffusivity_fields
193+
return compute_hydrostatic_free_surface_Ge!, catke_closure, catke_diffusivity_fields
194194
end
195195
end
196196
@@ -202,4 +202,3 @@ end
202202
return NamedTuple{tuple(names...)}(tuple(values...))
203203
end
204204
=#
205-

test/test_hydrostatic_free_surface_models.jl

+38-7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ function hydrostatic_free_surface_model_tracers_and_forcings_work(arch)
5454
return nothing
5555
end
5656

57+
function time_step_hydrostatic_model_with_catke_works(arch, FT)
58+
grid = LatitudeLongitudeGrid(
59+
arch,
60+
FT,
61+
topology = (Bounded, Bounded, Bounded),
62+
size = (8, 8, 8),
63+
longitude = (0, 1),
64+
latitude = (0, 1),
65+
z = (-100, 0)
66+
)
67+
68+
model = HydrostaticFreeSurfaceModel(;
69+
grid,
70+
buoyancy = BuoyancyTracer(),
71+
tracers = (:b, :e),
72+
closure = CATKEVerticalDiffusivity(FT)
73+
)
74+
75+
simulation = Simulation(model, Δt=1.0, stop_iteration=1)
76+
77+
run!(simulation)
78+
79+
return model.clock.iteration == 1
80+
end
81+
5782
topo_1d = (Flat, Flat, Bounded)
5883

5984
topos_2d = ((Periodic, Flat, Bounded),
@@ -66,7 +91,7 @@ topos_3d = ((Periodic, Periodic, Bounded),
6691

6792
@testset "Hydrostatic free surface Models" begin
6893
@info "Testing hydrostatic free surface models..."
69-
94+
7095
@testset "$topo_1d model construction" begin
7196
@info " Testing $topo_1d model construction..."
7297
for arch in archs, FT in [Float64] #float_types
@@ -80,7 +105,7 @@ topos_3d = ((Periodic, Periodic, Bounded),
80105
@test !( keys(fields(model))) # doesn't include free surface
81106
end
82107
end
83-
108+
84109
for topo in topos_2d
85110
@testset "$topo model construction" begin
86111
@info " Testing $topo model construction..."
@@ -92,7 +117,7 @@ topos_3d = ((Periodic, Periodic, Bounded),
92117
end
93118
end
94119
end
95-
120+
96121
for topo in topos_3d
97122
@testset "$topo model construction" begin
98123
@info " Testing $topo model construction..."
@@ -180,8 +205,8 @@ topos_3d = ((Periodic, Periodic, Bounded),
180205
precompute_metrics = true
181206
lat_lon_sector_grid = LatitudeLongitudeGrid(arch; size=(H, H, H), longitude=(0, 60), latitude=(15, 75), z=(-1, 0), precompute_metrics, halo)
182207
lat_lon_strip_grid = LatitudeLongitudeGrid(arch; size=(H, H, H), longitude=(-180, 180), latitude=(15, 75), z=(-1, 0), precompute_metrics, halo)
183-
184-
z = z_face_generator()
208+
209+
z = z_face_generator()
185210
lat_lon_sector_grid_stretched = LatitudeLongitudeGrid(arch; size=(H, H, H), longitude=(0, 60), latitude=(15, 75), z, precompute_metrics, halo)
186211
lat_lon_strip_grid_stretched = LatitudeLongitudeGrid(arch; size=(H, H, H), longitude=(-180, 180), latitude=(15, 75), z, precompute_metrics, halo)
187212

@@ -196,7 +221,7 @@ topos_3d = ((Periodic, Periodic, Bounded),
196221
topo = topology(grid)
197222
grid_type = typeof(grid).name.wrapper
198223
free_surface_type = typeof(free_surface).name.wrapper
199-
test_label = "[$arch, $grid_type, $topo, $free_surface_type]"
224+
test_label = "[$arch, $grid_type, $topo, $free_surface_type]"
200225
@testset "Time-stepping HydrostaticFreeSurfaceModels with various grids $test_label" begin
201226
@info " Testing time-stepping HydrostaticFreeSurfaceModels with various grids $test_label..."
202227
@test time_step_hydrostatic_model_works(grid; free_surface)
@@ -278,7 +303,7 @@ topos_3d = ((Periodic, Periodic, Bounded),
278303

279304
@test time_step_hydrostatic_model_works(rectilinear_grid, momentum_advection = nothing, velocities = velocities)
280305
@test time_step_hydrostatic_model_works(lat_lon_sector_grid, momentum_advection = nothing, velocities = velocities)
281-
306+
282307
parameters = (U=1, m=0.1, W=0.001)
283308
u(x, y, z, t, p) = p.U
284309
v(x, y, z, t, p) = exp(p.m * z)
@@ -294,5 +319,11 @@ topos_3d = ((Periodic, Periodic, Bounded),
294319
@info " Testing HydrostaticFreeSurfaceModel with tracers and forcings [$arch]..."
295320
hydrostatic_free_surface_model_tracers_and_forcings_work(arch)
296321
end
322+
323+
# See: https://github.com/CliMA/Oceananigans.jl/issues/3870
324+
@testset "HydrostaticFreeSurfaceModel with Float32 CATKE [$arch]" begin
325+
@info " Testing HydrostaticFreeSurfaceModel with Float32 CATKE [$arch]..."
326+
@test time_step_hydrostatic_model_with_catke_works(arch, Float32)
327+
end
297328
end
298329
end

0 commit comments

Comments
 (0)