-
Notifications
You must be signed in to change notification settings - Fork 203
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
add a unit test for background flux divergence and clean up unrelated commits #4028
Open
liuchihl
wants to merge
9
commits into
CliMA:glw/background-flux-divergence
Choose a base branch
from
liuchihl:background-flux-divergence-clean
base: glw/background-flux-divergence
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e63e88e
add a unit test for background flux divergence
liuchihl 9b9ad36
Add a correctness test
liuchihl 5ef31ee
Update test/test_background_flux_divergence.jl
liuchihl 5dfe7d1
Update test/test_background_flux_divergence.jl
liuchihl 9debb4e
Update test/test_background_flux_divergence.jl
liuchihl acd13bd
Update test/test_background_flux_divergence.jl
liuchihl 5d8c07c
Update test/test_background_flux_divergence.jl
liuchihl c1e133d
Update test/test_background_flux_divergence.jl
liuchihl 9c97738
Update test/test_background_flux_divergence.jl
liuchihl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
include("depedencies_for_runtests.jl") | ||
|
||
""" | ||
function run_with_background_fields(arch; with_background=true) | ||
|
||
Run a model with or without background fields and compare the two. | ||
""" | ||
function run_with_background_fields(arch; with_background=true) | ||
grid = RectilinearGrid(arch, size=10, z=(0, 1), topology=(Flat, Flat, Bounded)) | ||
# Setup model with or without background fields | ||
if with_background | ||
background_fields = Oceananigans.BackgroundFields(; | ||
background_closure_fluxes=true, b=B̄_field) | ||
# we want no flux bottom boundary (∂B∂z = 0) and infinite ocean at the top boundary | ||
B_bcs = FieldBoundaryConditions( | ||
bottom = GradientBoundaryCondition(-N^2), # ∂B∂z = 0 → ∂b∂z = -∂B∂z = -N² | ||
top = GradientBoundaryCondition(0.) # ∂B∂z = ∂B̄∂z+∂b∂z = N² → ∂b∂z = 0 | ||
); | ||
model = NonhydrostaticModel(; grid, background_fields, tracers = :b, buoyancy=BuoyancyTracer(), | ||
boundary_conditions=(; b = B_bcs)) | ||
b = model.tracers.b | ||
B̄ = model.background_fields.tracers.b | ||
B = B̄ + b # total buoyancy field | ||
filepath = "with_background.nc" | ||
else | ||
# again we want no flux bottom boundary (∂B∂z = 0) and infinite ocean at the top boundary | ||
B_bcs = FieldBoundaryConditions( | ||
bottom = GradientBoundaryCondition(0), # ∂B∂z = 0 | ||
top = GradientBoundaryCondition(N^2) # ∂B∂z = N² | ||
); | ||
model = NonhydrostaticModel(; grid, tracers = :b, buoyancy=BuoyancyTracer(), | ||
boundary_conditions=(b = B_bcs,)) | ||
Bᵢ(z) = constant_stratification(z, 0, (; N² = N^2)) | ||
set!(model, b=Bᵢ) # add background buoyancy as an initial condition | ||
b = model.tracers.b | ||
B = b # total buoyancy field = perturbation buoyancy because there is no background buoyancy | ||
filepath = "without_background.nc" | ||
end | ||
# remove output file if existed | ||
!isfile(filepath) ? nothing : rm(filepath) | ||
|
||
# Run for a few iterations | ||
simulation = Simulation(model, Δt=0.1, stop_iteration=5) | ||
simulation.output_writers[:profile] = NetCDFOutputWriter(model, (; B), | ||
schedule = IterationInterval(1), | ||
verbose=true, | ||
filename = filepath, | ||
overwrite_existing = true) | ||
run!(simulation) | ||
ds = NCDataset(filepath) | ||
|
||
|
||
return ds["B"][1,1,:,:] | ||
end | ||
|
||
# Linear background stratification | ||
N = 1e-3 | ||
@inline constant_stratification(z, t, p) = p.N² * z | ||
B̄_field = BackgroundField(constant_stratification, parameters=(; N² = N^2)) | ||
|
||
@testset "Background Fields Tests" begin | ||
for arch in test_archs | ||
|
||
# Test model runs with background fields | ||
@test run_with_background_fields(arch, with_background=true) !== nothing | ||
|
||
# Test model runs without background fields | ||
@test run_with_background_fields(arch, with_background=false) !== nothing | ||
|
||
# Test that background fields affect the solution | ||
b_with = run_with_background_fields(arch, with_background=true) | ||
b_without = run_with_background_fields(arch, with_background=false) | ||
|
||
# Compare the computed values | ||
@test all(isapprox.(b_with, b_without, rtol=1e-10)) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to use output here? I think you can directly return the value of
B
without output. That will make the test simpler and easier to maintain which is super important.