Skip to content

Commit 51a77cc

Browse files
glwagnernavidcy
andauthored
Add adapt_structure for ForcingKernelFunction (#4579)
* add adapt_structure for ForcingKernelFunction * add test * Update Project.toml * remove stray line * fix tests cc @glwagner --------- Co-authored-by: Navid C. Constantinou <navidcy@users.noreply.github.com>
1 parent ed90f64 commit 51a77cc

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.96.30"
4+
version = "0.96.31"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/Forcings/continuous_forcing.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,3 @@ on_architecture(to, forcing::ContinuousForcing{LX, LY, LZ}) where {LX, LY, LZ} =
168168
on_architecture(to, forcing.field_dependencies),
169169
on_architecture(to, forcing.field_dependencies_indices),
170170
on_architecture(to, forcing.field_dependencies_interp))
171-

src/Models/forcing_operation.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using Adapt
12

23
import Oceananigans.Utils: prettysummary
34

45
struct ForcingKernelFunction{F}
56
forcing :: F
67
end
78

9+
Adapt.adapt_structure(to, fkf::ForcingKernelFunction) =
10+
ForcingKernelFunction(adapt(to, fkf.forcing))
11+
812
prettysummary(kf::ForcingKernelFunction) = "ForcingKernelFunction"
913

1014
@inline function (kf::ForcingKernelFunction)(i, j, k, grid, args...)

test/test_model_diagnostics.jl

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
include("dependencies_for_runtests.jl")
22

3-
using Oceananigans.Models: BoundaryConditionOperation, BoundaryConditionField
3+
using Oceananigans.Forcings: ContinuousForcing
4+
using Oceananigans.Models: BoundaryConditionOperation, BoundaryConditionField,
5+
ForcingOperation, ForcingField, ForcingKernelFunction
46

57
u_bottom_drag_continuous(x, y, t, grid, u, v, Cᴰ) = -Cᴰ * u * sqrt(u^2 + v^2)
68
v_bottom_drag_discrete(i, j, grid, clock, fields, Cᴰ) = - @inbounds Cᴰ * fields.v[i, j, 1]
@@ -85,10 +87,10 @@ c_bottom_flux(i, j, grid, clock, fields, t★) = - @inbounds fields.c[i, j, 1] /
8587

8688
@test location(c_bottom_bc) == (Center, Center, Nothing)
8789
@test location(c_top_bc) == (Center, Center, Nothing)
88-
90+
8991
initial_c(x, y, z) = 1 + 1 * (z > 0)
9092
set!(model, c=initial_c)
91-
93+
9294
c_bottom_bc_field = BoundaryConditionField(c, :bottom, model)
9395
c_top_bc_field = BoundaryConditionField(c, :top, model)
9496

@@ -97,11 +99,38 @@ c_bottom_flux(i, j, grid, clock, fields, t★) = - @inbounds fields.c[i, j, 1] /
9799

98100
@test c_bottom_bc_field isa BoundaryConditionField
99101
@test c_top_bc_field isa BoundaryConditionField
100-
102+
101103
@test all(interior(c_bottom_bc_field) .≈ - 1 / t★)
102104
@test all(interior(c_top_bc_field) .≈ + 2 / t★)
103105
end
104106
end
105107
end
106108
end
107109

110+
damping(x, y, z, t, c, τ) = - c / τ
111+
112+
@testset "ForcingOperation and ForcingField" begin
113+
for arch in archs
114+
grid = RectilinearGrid(arch,
115+
size=(4, 4, 4),
116+
x = (0, 1),
117+
y = (0, 1),
118+
z = (-1, 1),
119+
topology=(Bounded, Bounded, Bounded))
120+
121+
122+
c_forcing = Forcing(damping, field_dependencies=:c, parameters=60)
123+
model = NonhydrostaticModel(; grid, tracers=:c, forcing=(; c=c_forcing))
124+
c_forcing_op = ForcingOperation(:c, model)
125+
@test c_forcing_op isa KernelFunctionOperation
126+
@test c_forcing_op.kernel_function isa ForcingKernelFunction
127+
@test c_forcing_op.kernel_function.forcing isa ContinuousForcing
128+
@test c_forcing_op.kernel_function.forcing.parameters == 60
129+
130+
set!(model, c=1)
131+
c_forcing_field = ForcingField(:c, model)
132+
compute!(c_forcing_field)
133+
@test c_forcing_field isa Field
134+
@test all(interior(c_forcing_field) .== - 1/60)
135+
end
136+
end

0 commit comments

Comments
 (0)