Skip to content

Commit

Permalink
Add 1D and 3D tests for boundary conditions (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
luraess authored Aug 28, 2024
1 parent 6e3ae01 commit 4eab90f
Showing 1 changed file with 186 additions and 39 deletions.
225 changes: 186 additions & 39 deletions test/test_boundary_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,201 @@ using Chmy.BoundaryConditions
for backend in backends
@testset "$(basename(@__FILE__)) (backend: $backend)" begin
arch = Arch(backend)
nx, ny = 8, 8
grid = UniformGrid(arch; origin=(-π, -π), extent=(2π, 2π), dims=(nx, ny))
field = Field(arch, grid, (Center(), Vertex()))

@testset "default Dirichlet" begin
set!(field, 1)
bc!(arch, grid, field => Dirichlet())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ .-field_i[2, 2:end-1])
@test all(field_i[end, 2:end-1] .≈ .-field_i[end-1, 2:end-1])

@test all(field_i[2:end-1, 2] .≈ 0.0)
@test all(field_i[2:end-1, end-1] .≈ 0.0)

@testset "1D Cartesian Center()" begin
nx = 8
grid = UniformGrid(arch; origin=(-π,), extent=(2π,), dims=(nx,))
field = Field(arch, grid, Center())

@testset "default Dirichlet" begin
set!(field, 1)
bc!(arch, grid, field => Dirichlet())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1] .≈ .-field_i[2])
@test all(field_i[end] .≈ .-field_i[end-1])
end

@testset "default Neumann" begin
set!(field, 1)
bc!(arch, grid, field => Neumann())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1] .≈ field_i[2])
@test all(field_i[end] .≈ field_i[end-1])
end

@testset "non-homogenous Dirichlet" begin
set!(field, 1)
v = 2.0
bc!(arch, grid, field => Dirichlet(v))
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1] .≈ .-field_i[2] .+ 2v)
@test all(field_i[end] .≈ .-field_i[end-1] .+ 2v)
end

@testset "non-homogenous Neumann" begin
set!(field, 1)
q = 2.0
bc!(arch, grid, field => Neumann(q))
field_i = interior(field; with_halo=true) |> Array
@test all((field_i[2] .- field_i[1]) ./ Δx(grid, Vertex(), 1) .≈ q)
@test all((field_i[end] .- field_i[end-1]) ./ Δx(grid, Vertex(), nx + 1) .≈ q)
end
end

@testset "default Neumann" begin
set!(field, 1)
bc!(arch, grid, field => Neumann())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ field_i[2, 2:end-1])
@test all(field_i[end, 2:end-1] .≈ field_i[end-1, 2:end-1])
@testset "1D Cartesian Vertex()" begin
nx = 8
grid = UniformGrid(arch; origin=(-π,), extent=(2π,), dims=(nx,))
field = Field(arch, grid, Vertex())

@testset "default Dirichlet" begin
set!(field, 1)
bc!(arch, grid, field => Dirichlet())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[2] .≈ 0.0)
@test all(field_i[end-1] .≈ 0.0)
end

@testset "default Neumann" begin
set!(field, 1)
bc!(arch, grid, field => Neumann())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1] .≈ field_i[2])
@test all(field_i[end] .≈ field_i[end-1])
end

@test all(field_i[2:end-1, 1] .≈ field_i[2:end-1, 2])
@test all(field_i[2:end-1, end] .≈ field_i[2:end-1, end-1])
@testset "non-homogenous Dirichlet" begin
set!(field, 1)
v = 2.0
bc!(arch, grid, field => Dirichlet(v))
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[2] .≈ v)
@test all(field_i[end-1] .≈ v)
end

@testset "non-homogenous Neumann" begin
set!(field, 1)
q = 2.0
bc!(arch, grid, field => Neumann(q))
field_i = interior(field; with_halo=true) |> Array
@test all((field_i[2] .- field_i[1]) ./ Δx(grid, Center(), 0) .≈ q)
@test all((field_i[end] .- field_i[end-1]) ./ Δx(grid, Center(), nx + 1) .≈ q)
end
end

@testset "non-homogenous Dirichlet" begin
set!(field, 1)
v = 2.0
bc!(arch, grid, field => Dirichlet(v))
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ .-field_i[2, 2:end-1] .+ 2v)
@test all(field_i[end, 2:end-1] .≈ .-field_i[end-1, 2:end-1] .+ 2v)
@testset "2D Cartesian" begin
nx, ny = 8, 8
grid = UniformGrid(arch; origin=(-π, -π), extent=(2π, 2π), dims=(nx, ny))
field = Field(arch, grid, (Center(), Vertex()))

@testset "default Dirichlet" begin
set!(field, 1)
bc!(arch, grid, field => Dirichlet())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ .-field_i[2, 2:end-1])
@test all(field_i[end, 2:end-1] .≈ .-field_i[end-1, 2:end-1])

@test all(field_i[2:end-1, 2] .≈ 0.0)
@test all(field_i[2:end-1, end-1] .≈ 0.0)
end

@testset "default Neumann" begin
set!(field, 1)
bc!(arch, grid, field => Neumann())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ field_i[2, 2:end-1])
@test all(field_i[end, 2:end-1] .≈ field_i[end-1, 2:end-1])

@test all(field_i[2:end-1, 1] .≈ field_i[2:end-1, 2])
@test all(field_i[2:end-1, end] .≈ field_i[2:end-1, end-1])
end

@testset "non-homogenous Dirichlet" begin
set!(field, 1)
v = 2.0
bc!(arch, grid, field => Dirichlet(v))
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1] .≈ .-field_i[2, 2:end-1] .+ 2v)
@test all(field_i[end, 2:end-1] .≈ .-field_i[end-1, 2:end-1] .+ 2v)

@test all(field_i[2:end-1, 2] .≈ v)
@test all(field_i[2:end-1, end-1] .≈ v)
@test all(field_i[2:end-1, 2] .≈ v)
@test all(field_i[2:end-1, end-1] .≈ v)
end

@testset "non-homogenous Neumann" begin
set!(field, 1)
q = 2.0
bc!(arch, grid, field => Neumann(q))
field_i = interior(field; with_halo=true) |> Array
@test all((field_i[2, 2:end-1] .- field_i[1, 2:end-1]) ./ Δx(grid, Vertex(), 1, 1) .≈ q)
@test all((field_i[end, 2:end-1] .- field_i[end-1, 2:end-1]) ./ Δx(grid, Vertex(), nx + 1, 1) .≈ q)

@test all((field_i[2:end-1, 2] .- field_i[2:end-1, 1]) ./ Δy(grid, Center(), 1, 0) .≈ q)
@test all((field_i[2:end-1, end] .- field_i[2:end-1, end-1]) ./ Δy(grid, Center(), 1, ny + 1) .≈ q)
end
end

@testset "non-homogenous Neumann" begin
set!(field, 1)
q = 2.0
bc!(arch, grid, field => Neumann(q))
field_i = interior(field; with_halo=true) |> Array
@test all((field_i[2, 2:end-1] .- field_i[1, 2:end-1]) ./ Δx(grid, Vertex(), 1, 1) .≈ q)
@test all((field_i[end, 2:end-1] .- field_i[end-1, 2:end-1]) ./ Δx(grid, Vertex(), nx + 1, 1) .≈ q)
@testset "3D Cartesian" begin
nx, ny, nz = 8, 8, 6
grid = UniformGrid(arch; origin=(-π, -π, -π), extent=(2π, 2π, 2π), dims=(nx, ny, nz))
field = Field(arch, grid, (Center(), Vertex(), Center()))

@testset "default Dirichlet" begin
set!(field, 1)
bc!(arch, grid, field => Dirichlet())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1, 2:end-1] .≈ .-field_i[2, 2:end-1, 2:end-1])
@test all(field_i[end, 2:end-1, 2:end-1] .≈ .-field_i[end-1, 2:end-1, 2:end-1])

@test all(field_i[2:end-1, 2, 2:end-1] .≈ 0.0)
@test all(field_i[2:end-1, end-1, 2:end-1] .≈ 0.0)

@test all(field_i[2:end-1, 2:end-1, 1] .≈ .-field_i[2:end-1, 2:end-1, 2])
@test all(field_i[2:end-1, 2:end-1, end] .≈ .-field_i[2:end-1, 2:end-1, end-1])
end

@testset "default Neumann" begin
set!(field, 1)
bc!(arch, grid, field => Neumann())
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1, 2:end-1] .≈ field_i[2, 2:end-1, 2:end-1])
@test all(field_i[end, 2:end-1, 2:end-1] .≈ field_i[end-1, 2:end-1, 2:end-1])

@test all(field_i[2:end-1, 1, 2:end-1] .≈ field_i[2:end-1, 2, 2:end-1])
@test all(field_i[2:end-1, end, 2:end-1] .≈ field_i[2:end-1, end-1, 2:end-1])

@test all(field_i[2:end-1, 2:end-1, 1] .≈ field_i[2:end-1, 2:end-1, 2])
@test all(field_i[2:end-1, 2:end-1, end] .≈ field_i[2:end-1, 2:end-1, end-1])
end

@testset "non-homogenous Dirichlet" begin
set!(field, 1)
v = 2.0
bc!(arch, grid, field => Dirichlet(v))
field_i = interior(field; with_halo=true) |> Array
@test all(field_i[1, 2:end-1, 2:end-1] .≈ .-field_i[2, 2:end-1, 2:end-1] .+ 2v)
@test all(field_i[end, 2:end-1, 2:end-1] .≈ .-field_i[end-1, 2:end-1, 2:end-1] .+ 2v)

@test all(field_i[2:end-1, 2, 2:end-1] .≈ v)
@test all(field_i[2:end-1, end-1, 2:end-1] .≈ v)

@test all(field_i[2:end-1, 2:end-1, 1] .≈ .-field_i[2:end-1, 2:end-1, 2] .+ 2v)
@test all(field_i[2:end-1, 2:end-1, end] .≈ .-field_i[2:end-1, 2:end-1, end-1] .+ 2v)
end

@testset "non-homogenous Neumann" begin
set!(field, 1)
q = 2.0
bc!(arch, grid, field => Neumann(q))
field_i = interior(field; with_halo=true) |> Array
@test all((field_i[2, 2:end-1, 2:end-1] .- field_i[1, 2:end-1, 2:end-1]) ./ Δx(grid, Vertex(), 1, 1, 1) .≈ q)
@test all((field_i[end, 2:end-1, 2:end-1] .- field_i[end-1, 2:end-1, 2:end-1]) ./ Δx(grid, Vertex(), nx + 1, 1, 1) .≈ q)

@test all((field_i[2:end-1, 2, 2:end-1] .- field_i[2:end-1, 1, 2:end-1]) ./ Δy(grid, Center(), 1, 0, 1) .≈ q)
@test all((field_i[2:end-1, end, 2:end-1] .- field_i[2:end-1, end-1, 2:end-1]) ./ Δy(grid, Center(), 1, ny + 1, 1) .≈ q)

@test all((field_i[2:end-1, 2] .- field_i[2:end-1, 1]) ./ Δy(grid, Center(), 1, 0) .≈ q)
@test all((field_i[2:end-1, end] .- field_i[2:end-1, end-1]) ./ Δy(grid, Center(), 1, ny + 1) .≈ q)
@test all((field_i[2:end-1, 2:end-1, 2] .- field_i[2:end-1, 2:end-1, 1]) ./ Δz(grid, Vertex(), 1, 1, 1) .≈ q)
@test all((field_i[2:end-1, 2:end-1, end] .- field_i[2:end-1, 2:end-1, end-1]) ./ Δz(grid, Vertex(), 1, 1, nz + 1) .≈ q)
end
end
end
end

2 comments on commit 4eab90f

@utkinis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/114060

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.19 -m "<description of version>" 4eab90f2e7a72b38d8c35dd57e37805fcb78e649
git push origin v0.1.19

Please sign in to comment.