Skip to content

Commit 6c40d7e

Browse files
jlk9wsmosesglwagner
authored
Add test of enzyme flux bc (#3643)
* Added test of enzyme flux bc * Added comments * Switched advection-diffusion test with flux BC to discrete form * Update test_enzyme.jl * Combined advection-diffusion tests with and without flux BC into one test * Replaced use of end with a parameter for surface level and cleaned indenting * Update test/test_enzyme.jl * Update test/test_enzyme.jl * Update test/test_enzyme.jl --------- Co-authored-by: William Moses <[email protected]> Co-authored-by: Gregory L. Wagner <[email protected]>
1 parent f2a8fb3 commit 6c40d7e

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

test/test_enzyme.jl

+50-34
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ end
157157
end
158158
end
159159

160-
@testset "Enzyme on advection and diffusion" begin
160+
161+
@testset "Enzyme for advection and diffusion with various boundary conditions" begin
161162
Nx = Ny = 64
162163
Nz = 8
163164

@@ -183,54 +184,69 @@ end
183184
fill_halo_regions!(u)
184185
fill_halo_regions!(v)
185186

186-
@inline function tracer_flux(x, y, t, c, p)
187+
@inline function tracer_flux(i, j, grid, clock, model_fields, p)
187188
c₀ = p.surface_tracer_concentration
188189
u★ = p.piston_velocity
189-
return - u★ * (c₀ - c)
190+
return - u★ * (c₀ - model_fields.c[i, j, p.level])
190191
end
191192

192193
parameters = (surface_tracer_concentration = 1,
193-
piston_velocity = 0.1)
194+
piston_velocity = 0.1,
195+
level = Nz)
194196

195-
top_c_bc = FluxBoundaryCondition(tracer_flux, field_dependencies=:c; parameters)
197+
top_c_bc = FluxBoundaryCondition(tracer_flux; discrete_form=true, parameters)
196198
c_bcs = FieldBoundaryConditions(top=top_c_bc)
197199

198200
# TODO:
199201
# 1. Make the velocity fields evolve
200202
# 2. Add surface fluxes
201203
# 3. Do a problem where we invert for the tracer fluxes (maybe with CATKE)
202204

203-
model = HydrostaticFreeSurfaceModel(; grid,
204-
tracer_advection = WENO(),
205-
tracers = :c,
206-
velocities = PrescribedVelocityFields(; u, v),
207-
closure = diffusion)
208-
209-
# Compute derivative by hand
210-
κ₁, κ₂ = 0.9, 1.1
211-
c²₁ = stable_diffusion!(model, 1, κ₁)
212-
c²₂ = stable_diffusion!(model, 1, κ₂)
213-
dc²_dκ_fd = (c²₂ - c²₁) / (κ₂ - κ₁)
214-
215-
# Now for real
216-
amplitude = 1.0
217-
κ = 1.0
218-
dmodel = Enzyme.make_zero(model)
219-
set_diffusivity!(dmodel, 0)
220-
221-
dc²_dκ = autodiff(Enzyme.set_runtime_activity(Enzyme.Reverse),
222-
stable_diffusion!,
223-
Duplicated(model, dmodel),
224-
Const(amplitude),
225-
Active(κ))
226-
227-
@info """ \n
205+
model_no_bc = HydrostaticFreeSurfaceModel(; grid,
206+
tracer_advection = WENO(),
207+
tracers = :c,
208+
velocities = PrescribedVelocityFields(; u, v),
209+
closure = diffusion)
210+
211+
model_bc = HydrostaticFreeSurfaceModel(; grid,
212+
tracer_advection = WENO(),
213+
tracers = :c,
214+
velocities = PrescribedVelocityFields(; u, v),
215+
boundary_conditions = (; c=c_bcs),
216+
closure = diffusion)
217+
218+
models = [model_no_bc, model_bc]
219+
220+
@show "Advection-diffusion results, first without then with flux BC"
221+
222+
for i in 1:2
223+
# Compute derivative by hand
224+
κ₁, κ₂ = 0.9, 1.1
225+
c²₁ = stable_diffusion!(models[i], 1, κ₁)
226+
c²₂ = stable_diffusion!(models[i], 1, κ₂)
227+
dc²_dκ_fd = (c²₂ - c²₁) / (κ₂ - κ₁)
228+
229+
# Now for real
230+
amplitude = 1.0
231+
κ = 1.0
232+
dmodel = Enzyme.make_zero(models[i])
233+
set_diffusivity!(dmodel, 0)
234+
235+
dc²_dκ = autodiff(Enzyme.set_runtime_activity(Enzyme.Reverse),
236+
stable_diffusion!,
237+
Duplicated(models[i], dmodel),
238+
Const(amplitude),
239+
Active(κ))
240+
241+
@info """ \n
242+
Advection-diffusion:
228243
Enzyme computed $dc²_dκ
229244
Finite differences computed $dc²_dκ_fd
230-
"""
245+
"""
231246

232-
tol = 0.01
233-
rel_error = abs(dc²_dκ[1][3] - dc²_dκ_fd) / abs(dc²_dκ_fd)
234-
@test rel_error < tol
247+
tol = 0.01
248+
rel_error = abs(dc²_dκ[1][3] - dc²_dκ_fd) / abs(dc²_dκ_fd)
249+
@test rel_error < tol
250+
end
235251
end
236252

0 commit comments

Comments
 (0)