From 4eab90f2e7a72b38d8c35dd57e37805fcb78e649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20R=C3=A4ss?= <61313342+luraess@users.noreply.github.com> Date: Thu, 29 Aug 2024 00:18:36 +0200 Subject: [PATCH] Add 1D and 3D tests for boundary conditions (#38) --- test/test_boundary_conditions.jl | 225 +++++++++++++++++++++++++------ 1 file changed, 186 insertions(+), 39 deletions(-) diff --git a/test/test_boundary_conditions.jl b/test/test_boundary_conditions.jl index 45152dd4..3121f00a 100644 --- a/test/test_boundary_conditions.jl +++ b/test/test_boundary_conditions.jl @@ -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