Skip to content
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

Refactor of some stretched rectilinear grid tests #2917

Merged
merged 5 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions test/test_grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,53 +334,40 @@ function test_architecturally_correct_stretched_grid(FT, arch, zᵃᵃᶠ)
return nothing
end

function test_correct_constant_grid_spacings(FT, Nz)
grid = RectilinearGrid(CPU(), FT, size=(1, 1, Nz), x=(0, 1), y=(0, 1), z=collect(0:Nz))
function test_rectilinear_grid_correct_spacings(FT, N)
S = 3
zᵃᵃᶠ(k) = tanh(S * (2 * (k - 1) / N - 1)) / tanh(S)

@test all(grid.Δzᵃᵃᶜ .== 1)
@test all(grid.Δzᵃᵃᶠ .== 1)
grid = RectilinearGrid(CPU(), FT, size=(N, N, N), x=collect(0:N), y=collect(0:N).^2, z=zᵃᵃᶠ)
tomchor marked this conversation as resolved.
Show resolved Hide resolved

return nothing
end
@test all(grid.Δxᶜᵃᵃ .== 1)
@test all(grid.Δxᶠᵃᵃ .== 1)

function test_correct_quadratic_grid_spacings(FT, Nz)
grid = RectilinearGrid(CPU(), FT, size=(1, 1, Nz), x=(0, 1), y=(0, 1), z=collect(0:Nz).^2)
yᵃᶠᵃ(j) = (j-1)^2
yᵃᶜᵃ(j) = (j^2 + (j-1)^2) / 2
Δyᵃᶠᵃ(j) = yᵃᶜᵃ(j) - yᵃᶜᵃ(j-1)
Δyᵃᶜᵃ(j) = j^2 - (j-1)^2
navidcy marked this conversation as resolved.
Show resolved Hide resolved

zᵃᵃᶠ(k) = (k-1)^2
zᵃᵃᶜ(k) = (k^2 + (k-1)^2) / 2
Δzᵃᵃᶠ(k) = k^2 - (k-1)^2
Δzᵃᵃᶜ(k) = zᵃᵃᶜ(k) - zᵃᵃᶜ(k-1)

@test all(isapprox.( grid.zᵃᵃᶠ[1:Nz+1], zᵃᵃᶠ.(1:Nz+1) ))
@test all(isapprox.( grid.zᵃᵃᶜ[1:Nz], zᵃᵃᶜ.(1:Nz) ))
@test all(isapprox.( grid.Δzᵃᵃᶜ[1:Nz], Δzᵃᵃᶠ.(1:Nz) ))
@test all(isapprox.( grid.yᵃᶠᵃ[1:N+1], yᵃᶠᵃ.(1:N+1) ))
@test all(isapprox.( grid.yᵃᶜᵃ[1:N], yᵃᶜᵃ.(1:N) ))
@test all(isapprox.( grid.Δyᵃᶜᵃ[1:N], Δyᵃᶜᵃ.(1:N) ))

# Note that Δzᵃᵃᶠ[1] involves a halo point, which is not directly determined by
# the user-supplied zᵃᵃᶠ
@test all(isapprox.( grid.Δzᵃᵃᶠ[2:Nz], Δzᵃᵃᶜ.(2:Nz) ))

return nothing
end

function test_correct_tanh_grid_spacings(FT, Nz)
S = 3 # Stretching factor
zᵃᵃᶠ(k) = tanh(S * (2 * (k - 1) / Nz - 1)) / tanh(S)

grid = RectilinearGrid(CPU(), FT, size=(1, 1, Nz), x=(0, 1), y=(0, 1), z=zᵃᵃᶠ)
@test all(isapprox.( grid.Δyᵃᶠᵃ[2:N], Δyᵃᶠᵃ.(2:N) ))

zᵃᵃᶜ(k) = (zᵃᵃᶠ(k) + zᵃᵃᶠ(k+1)) / 2
Δzᵃᵃᶠ(k) = zᵃᵃᶠ(k+1) - zᵃᵃᶠ(k)
Δzᵃᵃᶜ(k) = zᵃᵃᶜ(k) - zᵃᵃᶜ(k-1)
zᵃᵃᶜ(k) = (zᵃᵃᶠ(k) + zᵃᵃᶠ(k+1)) / 2
Δzᵃᵃᶜ(k) = zᵃᵃᶠ(k+1) - zᵃᵃᶠ(k)
Δzᵃᵃᶠ(k) = zᵃᵃᶜ(k) - zᵃᵃᶜ(k-1)

@test all(isapprox.( grid.zᵃᵃᶠ[1:Nz+1], zᵃᵃᶠ.(1:Nz+1) ))
@test all(isapprox.( grid.zᵃᵃᶜ[1:Nz], zᵃᵃᶜ.(1:Nz) ))
@test all(isapprox.( grid.Δzᵃᵃᶜ[1:Nz], Δzᵃᵃᶠ.(1:Nz) ))
@test all(isapprox.( grid.zᵃᵃᶠ[1:N+1], zᵃᵃᶠ.(1:N+1) ))
@test all(isapprox.( grid.zᵃᵃᶜ[1:N], zᵃᵃᶜ.(1:N) ))
@test all(isapprox.( grid.Δzᵃᵃᶜ[1:N], Δzᵃᵃᶜ.(1:N) ))

# Note that Δzᵃᵃᶠ[1] involves a halo point, which is not directly determined by
# the user-supplied zᵃᵃᶠ
@test all(isapprox.( grid.Δzᵃᵃᶠ[2:Nz], Δzᵃᵃᶜ.(2:Nz) ))
@test all(isapprox.( grid.Δzᵃᵃᶠ[2:N], Δzᵃᵃᶠ.(2:N) ))

return nothing
tomchor marked this conversation as resolved.
Show resolved Hide resolved
end

#####
Expand Down Expand Up @@ -708,10 +695,8 @@ end

@testset "Vertically stretched rectilinear grid spacings [$(typeof(arch)), $FT]" begin
@info " Testing vertically stretched rectilinear grid spacings [$(typeof(arch)), $FT]..."
for Nz in [16, 17]
test_correct_constant_grid_spacings(FT, Nz)
test_correct_quadratic_grid_spacings(FT, Nz)
test_correct_tanh_grid_spacings(FT, Nz)
for N in [16, 17]
test_rectilinear_grid_correct_spacings(FT, N)
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_netcdf_output_writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ function test_netcdf_spatial_average(arch)
set!(model, c=1)

Δt = 1/64 # Nice floating-point number
simulation = Simulation(model, Δt=Δt, stop_time=50Δt)
simulation = Simulation(model, Δt=Δt, stop_iteration=10)

∫c_dx = Field(Average(model.tracers.c, dims=(1)))
∫∫c_dxdy = Field(Average(model.tracers.c, dims=(1, 2)))
Expand All @@ -530,7 +530,7 @@ function test_netcdf_spatial_average(arch)
array_type = Array{Float64},
verbose = true,
filename = volume_avg_nc_filepath,
schedule = TimeInterval(10Δt))
schedule = IterationInterval(2))
run!(simulation)

ds = NCDataset(volume_avg_nc_filepath)
Expand Down