Mercurial > repos > public > sbplib_julia
changeset 711:df88aee35bb9 feature/selectable_tests
Switch to _test.jl suffix
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 20 Feb 2021 20:45:40 +0100 |
parents | 44fa9a171557 |
children | de2df1214394 |
files | test/DiffOps/DiffOps_test.jl test/DiffOps/testDiffOps.jl test/Grids/Grids_test.jl test/Grids/testGrids.jl test/LazyTensors/LazyTensors_test.jl test/LazyTensors/testLazyTensors.jl test/RegionIndices/RegionIndices_test.jl test/RegionIndices/testRegionIndices.jl test/SbpOperators/SbpOperators_test.jl test/SbpOperators/testSbpOperators.jl test/runtests.jl |
diffstat | 11 files changed, 1722 insertions(+), 1727 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/DiffOps/DiffOps_test.jl Sat Feb 20 20:45:40 2021 +0100 @@ -0,0 +1,198 @@ +using Test +using Sbplib.DiffOps +using Sbplib.Grids +using Sbplib.SbpOperators +using Sbplib.RegionIndices +using Sbplib.LazyTensors + +@testset "DiffOps" begin +# +# @testset "BoundaryValue" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) +# +# e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}()) +# e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}()) +# e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}()) +# e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}()) +# +# v = zeros(Float64, 4, 5) +# v[:,5] = [1, 2, 3,4] +# v[:,4] = [1, 2, 3,4] +# v[:,3] = [4, 5, 6, 7] +# v[:,2] = [7, 8, 9, 10] +# v[:,1] = [10, 11, 12, 13] +# +# @test e_w isa TensorMapping{T,2,1} where T +# @test e_w' isa TensorMapping{T,1,2} where T +# +# @test domain_size(e_w, (3,2)) == (2,) +# @test domain_size(e_e, (3,2)) == (2,) +# @test domain_size(e_s, (3,2)) == (3,) +# @test domain_size(e_n, (3,2)) == (3,) +# +# @test size(e_w'*v) == (5,) +# @test size(e_e'*v) == (5,) +# @test size(e_s'*v) == (4,) +# @test size(e_n'*v) == (4,) +# +# @test collect(e_w'*v) == [10,7,4,1.0,1] +# @test collect(e_e'*v) == [13,10,7,4,4.0] +# @test collect(e_s'*v) == [10,11,12,13.0] +# @test collect(e_n'*v) == [1,2,3,4.0] +# +# g_x = [1,2,3,4.0] +# g_y = [5,4,3,2,1.0] +# +# G_w = zeros(Float64, (4,5)) +# G_w[1,:] = g_y +# +# G_e = zeros(Float64, (4,5)) +# G_e[4,:] = g_y +# +# G_s = zeros(Float64, (4,5)) +# G_s[:,1] = g_x +# +# G_n = zeros(Float64, (4,5)) +# G_n[:,5] = g_x +# +# @test size(e_w*g_y) == (UnknownDim,5) +# @test size(e_e*g_y) == (UnknownDim,5) +# @test size(e_s*g_x) == (4,UnknownDim) +# @test size(e_n*g_x) == (4,UnknownDim) +# +# # These tests should be moved to where they are possible (i.e we know what the grid should be) +# @test_broken collect(e_w*g_y) == G_w +# @test_broken collect(e_e*g_y) == G_e +# @test_broken collect(e_s*g_x) == G_s +# @test_broken collect(e_n*g_x) == G_n +# end +# +# @testset "NormalDerivative" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) +# +# d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) +# d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}()) +# d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}()) +# d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}()) +# +# +# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) +# v∂x = evalOn(g, (x,y)-> 2*x + y) +# v∂y = evalOn(g, (x,y)-> 2*(y-1) + x) +# +# @test d_w isa TensorMapping{T,2,1} where T +# @test d_w' isa TensorMapping{T,1,2} where T +# +# @test domain_size(d_w, (3,2)) == (2,) +# @test domain_size(d_e, (3,2)) == (2,) +# @test domain_size(d_s, (3,2)) == (3,) +# @test domain_size(d_n, (3,2)) == (3,) +# +# @test size(d_w'*v) == (6,) +# @test size(d_e'*v) == (6,) +# @test size(d_s'*v) == (5,) +# @test size(d_n'*v) == (5,) +# +# @test collect(d_w'*v) ≈ v∂x[1,:] +# @test collect(d_e'*v) ≈ v∂x[5,:] +# @test collect(d_s'*v) ≈ v∂y[:,1] +# @test collect(d_n'*v) ≈ v∂y[:,6] +# +# +# d_x_l = zeros(Float64, 5) +# d_x_u = zeros(Float64, 5) +# for i ∈ eachindex(d_x_l) +# d_x_l[i] = op.dClosure[i-1] +# d_x_u[i] = -op.dClosure[length(d_x_u)-i] +# end +# +# d_y_l = zeros(Float64, 6) +# d_y_u = zeros(Float64, 6) +# for i ∈ eachindex(d_y_l) +# d_y_l[i] = op.dClosure[i-1] +# d_y_u[i] = -op.dClosure[length(d_y_u)-i] +# end +# +# function prod_matrix(x,y) +# G = zeros(Float64, length(x), length(y)) +# for I ∈ CartesianIndices(G) +# G[I] = x[I[1]]*y[I[2]] +# end +# +# return G +# end +# +# g_x = [1,2,3,4.0,5] +# g_y = [5,4,3,2,1.0,11] +# +# G_w = prod_matrix(d_x_l, g_y) +# G_e = prod_matrix(d_x_u, g_y) +# G_s = prod_matrix(g_x, d_y_l) +# G_n = prod_matrix(g_x, d_y_u) +# +# +# @test size(d_w*g_y) == (UnknownDim,6) +# @test size(d_e*g_y) == (UnknownDim,6) +# @test size(d_s*g_x) == (5,UnknownDim) +# @test size(d_n*g_x) == (5,UnknownDim) +# +# # These tests should be moved to where they are possible (i.e we know what the grid should be) +# @test_broken collect(d_w*g_y) ≈ G_w +# @test_broken collect(d_e*g_y) ≈ G_e +# @test_broken collect(d_s*g_x) ≈ G_s +# @test_broken collect(d_n*g_x) ≈ G_n +# end +# +# @testset "BoundaryQuadrature" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) +# +# H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}()) +# H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}()) +# H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}()) +# H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}()) +# +# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) +# +# function get_quadrature(N) +# qc = op.quadratureClosure +# q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...) +# @assert length(q) == N +# return q +# end +# +# v_w = v[1,:] +# v_e = v[10,:] +# v_s = v[:,1] +# v_n = v[:,11] +# +# q_x = spacing(g)[1].*get_quadrature(10) +# q_y = spacing(g)[2].*get_quadrature(11) +# +# @test H_w isa TensorOperator{T,1} where T +# +# @test domain_size(H_w, (3,)) == (3,) +# @test domain_size(H_n, (3,)) == (3,) +# +# @test range_size(H_w, (3,)) == (3,) +# @test range_size(H_n, (3,)) == (3,) +# +# @test size(H_w*v_w) == (11,) +# @test size(H_e*v_e) == (11,) +# @test size(H_s*v_s) == (10,) +# @test size(H_n*v_n) == (10,) +# +# @test collect(H_w*v_w) ≈ q_y.*v_w +# @test collect(H_e*v_e) ≈ q_y.*v_e +# @test collect(H_s*v_s) ≈ q_x.*v_s +# @test collect(H_n*v_n) ≈ q_x.*v_n +# +# @test collect(H_w'*v_w) == collect(H_w'*v_w) +# @test collect(H_e'*v_e) == collect(H_e'*v_e) +# @test collect(H_s'*v_s) == collect(H_s'*v_s) +# @test collect(H_n'*v_n) == collect(H_n'*v_n) +# end + +end
--- a/test/DiffOps/testDiffOps.jl Sat Feb 20 20:36:27 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,198 +0,0 @@ -using Test -using Sbplib.DiffOps -using Sbplib.Grids -using Sbplib.SbpOperators -using Sbplib.RegionIndices -using Sbplib.LazyTensors - -@testset "DiffOps" begin -# -# @testset "BoundaryValue" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) -# -# e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}()) -# e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}()) -# e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}()) -# e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}()) -# -# v = zeros(Float64, 4, 5) -# v[:,5] = [1, 2, 3,4] -# v[:,4] = [1, 2, 3,4] -# v[:,3] = [4, 5, 6, 7] -# v[:,2] = [7, 8, 9, 10] -# v[:,1] = [10, 11, 12, 13] -# -# @test e_w isa TensorMapping{T,2,1} where T -# @test e_w' isa TensorMapping{T,1,2} where T -# -# @test domain_size(e_w, (3,2)) == (2,) -# @test domain_size(e_e, (3,2)) == (2,) -# @test domain_size(e_s, (3,2)) == (3,) -# @test domain_size(e_n, (3,2)) == (3,) -# -# @test size(e_w'*v) == (5,) -# @test size(e_e'*v) == (5,) -# @test size(e_s'*v) == (4,) -# @test size(e_n'*v) == (4,) -# -# @test collect(e_w'*v) == [10,7,4,1.0,1] -# @test collect(e_e'*v) == [13,10,7,4,4.0] -# @test collect(e_s'*v) == [10,11,12,13.0] -# @test collect(e_n'*v) == [1,2,3,4.0] -# -# g_x = [1,2,3,4.0] -# g_y = [5,4,3,2,1.0] -# -# G_w = zeros(Float64, (4,5)) -# G_w[1,:] = g_y -# -# G_e = zeros(Float64, (4,5)) -# G_e[4,:] = g_y -# -# G_s = zeros(Float64, (4,5)) -# G_s[:,1] = g_x -# -# G_n = zeros(Float64, (4,5)) -# G_n[:,5] = g_x -# -# @test size(e_w*g_y) == (UnknownDim,5) -# @test size(e_e*g_y) == (UnknownDim,5) -# @test size(e_s*g_x) == (4,UnknownDim) -# @test size(e_n*g_x) == (4,UnknownDim) -# -# # These tests should be moved to where they are possible (i.e we know what the grid should be) -# @test_broken collect(e_w*g_y) == G_w -# @test_broken collect(e_e*g_y) == G_e -# @test_broken collect(e_s*g_x) == G_s -# @test_broken collect(e_n*g_x) == G_n -# end -# -# @testset "NormalDerivative" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) -# -# d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) -# d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}()) -# d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}()) -# d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}()) -# -# -# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) -# v∂x = evalOn(g, (x,y)-> 2*x + y) -# v∂y = evalOn(g, (x,y)-> 2*(y-1) + x) -# -# @test d_w isa TensorMapping{T,2,1} where T -# @test d_w' isa TensorMapping{T,1,2} where T -# -# @test domain_size(d_w, (3,2)) == (2,) -# @test domain_size(d_e, (3,2)) == (2,) -# @test domain_size(d_s, (3,2)) == (3,) -# @test domain_size(d_n, (3,2)) == (3,) -# -# @test size(d_w'*v) == (6,) -# @test size(d_e'*v) == (6,) -# @test size(d_s'*v) == (5,) -# @test size(d_n'*v) == (5,) -# -# @test collect(d_w'*v) ≈ v∂x[1,:] -# @test collect(d_e'*v) ≈ v∂x[5,:] -# @test collect(d_s'*v) ≈ v∂y[:,1] -# @test collect(d_n'*v) ≈ v∂y[:,6] -# -# -# d_x_l = zeros(Float64, 5) -# d_x_u = zeros(Float64, 5) -# for i ∈ eachindex(d_x_l) -# d_x_l[i] = op.dClosure[i-1] -# d_x_u[i] = -op.dClosure[length(d_x_u)-i] -# end -# -# d_y_l = zeros(Float64, 6) -# d_y_u = zeros(Float64, 6) -# for i ∈ eachindex(d_y_l) -# d_y_l[i] = op.dClosure[i-1] -# d_y_u[i] = -op.dClosure[length(d_y_u)-i] -# end -# -# function prod_matrix(x,y) -# G = zeros(Float64, length(x), length(y)) -# for I ∈ CartesianIndices(G) -# G[I] = x[I[1]]*y[I[2]] -# end -# -# return G -# end -# -# g_x = [1,2,3,4.0,5] -# g_y = [5,4,3,2,1.0,11] -# -# G_w = prod_matrix(d_x_l, g_y) -# G_e = prod_matrix(d_x_u, g_y) -# G_s = prod_matrix(g_x, d_y_l) -# G_n = prod_matrix(g_x, d_y_u) -# -# -# @test size(d_w*g_y) == (UnknownDim,6) -# @test size(d_e*g_y) == (UnknownDim,6) -# @test size(d_s*g_x) == (5,UnknownDim) -# @test size(d_n*g_x) == (5,UnknownDim) -# -# # These tests should be moved to where they are possible (i.e we know what the grid should be) -# @test_broken collect(d_w*g_y) ≈ G_w -# @test_broken collect(d_e*g_y) ≈ G_e -# @test_broken collect(d_s*g_x) ≈ G_s -# @test_broken collect(d_n*g_x) ≈ G_n -# end -# -# @testset "BoundaryQuadrature" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) -# -# H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}()) -# H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}()) -# H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}()) -# H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}()) -# -# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) -# -# function get_quadrature(N) -# qc = op.quadratureClosure -# q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...) -# @assert length(q) == N -# return q -# end -# -# v_w = v[1,:] -# v_e = v[10,:] -# v_s = v[:,1] -# v_n = v[:,11] -# -# q_x = spacing(g)[1].*get_quadrature(10) -# q_y = spacing(g)[2].*get_quadrature(11) -# -# @test H_w isa TensorOperator{T,1} where T -# -# @test domain_size(H_w, (3,)) == (3,) -# @test domain_size(H_n, (3,)) == (3,) -# -# @test range_size(H_w, (3,)) == (3,) -# @test range_size(H_n, (3,)) == (3,) -# -# @test size(H_w*v_w) == (11,) -# @test size(H_e*v_e) == (11,) -# @test size(H_s*v_s) == (10,) -# @test size(H_n*v_n) == (10,) -# -# @test collect(H_w*v_w) ≈ q_y.*v_w -# @test collect(H_e*v_e) ≈ q_y.*v_e -# @test collect(H_s*v_s) ≈ q_x.*v_s -# @test collect(H_n*v_n) ≈ q_x.*v_n -# -# @test collect(H_w'*v_w) == collect(H_w'*v_w) -# @test collect(H_e'*v_e) == collect(H_e'*v_e) -# @test collect(H_s'*v_s) == collect(H_s'*v_s) -# @test collect(H_n'*v_n) == collect(H_n'*v_n) -# end - -end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/Grids/Grids_test.jl Sat Feb 20 20:45:40 2021 +0100 @@ -0,0 +1,104 @@ +using Sbplib.Grids +using Test +using Sbplib.RegionIndices + +@testset "Grids" begin + +@testset "EquidistantGrid" begin + @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid + @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid + # constuctor + @test_throws DomainError EquidistantGrid(0,0.0,1.0) + @test_throws DomainError EquidistantGrid(1,1.0,1.0) + @test_throws DomainError EquidistantGrid(1,1.0,-1.0) + @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,)) + + @testset "Base" begin + @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64 + @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int + @test size(EquidistantGrid(4,0.0,1.0)) == (4,) + @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) + end + + # dimension + @test dimension(EquidistantGrid(4,0.0,1.0)) == 1 + @test dimension(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 + + # spacing + @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 + @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 + + # inverse_spacing + @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 + @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 + + # points + g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) + gp = points(g); + p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); + (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); + (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); + (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); + (0.,0.) (0.,7.11/2) (0.,7.11)] + for i ∈ eachindex(gp) + @test [gp[i]...] ≈ [p[i]...] atol=5e-13 + end + + # restrict + g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0)) + @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0) + @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0) + + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) + @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) + @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) + @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0)) + @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) + @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0)) + @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0)) + + @testset "boundary_identifiers" begin + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), + CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(), + CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}()) + @test boundary_identifiers(g) == bids + @inferred boundary_identifiers(g) + end + + @testset "boundary_grid" begin + @testset "1D" begin + g = EquidistantGrid(5,0.0,2.0) + (id_l, id_r) = boundary_identifiers(g) + @test boundary_grid(g,id_l) == EquidistantGrid{Float64}() + @test boundary_grid(g,id_r) == EquidistantGrid{Float64}() + @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}()) + @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}()) + end + @testset "2D" begin + g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) + (id_w, id_e, id_s, id_n) = boundary_identifiers(g) + @test boundary_grid(g,id_w) == restrict(g,2) + @test boundary_grid(g,id_e) == restrict(g,2) + @test boundary_grid(g,id_s) == restrict(g,1) + @test boundary_grid(g,id_n) == restrict(g,1) + @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) + end + @testset "3D" begin + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + (id_w, id_e, + id_s, id_n, + id_t, id_b) = boundary_identifiers(g) + @test boundary_grid(g,id_w) == restrict(g,[2,3]) + @test boundary_grid(g,id_e) == restrict(g,[2,3]) + @test boundary_grid(g,id_s) == restrict(g,[1,3]) + @test boundary_grid(g,id_n) == restrict(g,[1,3]) + @test boundary_grid(g,id_t) == restrict(g,[1,2]) + @test boundary_grid(g,id_b) == restrict(g,[1,2]) + @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) + end + end +end + +end
--- a/test/Grids/testGrids.jl Sat Feb 20 20:36:27 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -using Sbplib.Grids -using Test -using Sbplib.RegionIndices - -@testset "Grids" begin - -@testset "EquidistantGrid" begin - @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid - @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid - # constuctor - @test_throws DomainError EquidistantGrid(0,0.0,1.0) - @test_throws DomainError EquidistantGrid(1,1.0,1.0) - @test_throws DomainError EquidistantGrid(1,1.0,-1.0) - @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,)) - - @testset "Base" begin - @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64 - @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int - @test size(EquidistantGrid(4,0.0,1.0)) == (4,) - @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) - end - - # dimension - @test dimension(EquidistantGrid(4,0.0,1.0)) == 1 - @test dimension(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 - - # spacing - @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 - @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 - - # inverse_spacing - @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 - @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 - - # points - g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) - gp = points(g); - p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); - (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); - (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); - (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); - (0.,0.) (0.,7.11/2) (0.,7.11)] - for i ∈ eachindex(gp) - @test [gp[i]...] ≈ [p[i]...] atol=5e-13 - end - - # restrict - g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0)) - @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0) - @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0) - - g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) - @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) - @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) - @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) - @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0)) - @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) - @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0)) - @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0)) - - @testset "boundary_identifiers" begin - g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) - bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), - CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(), - CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}()) - @test boundary_identifiers(g) == bids - @inferred boundary_identifiers(g) - end - - @testset "boundary_grid" begin - @testset "1D" begin - g = EquidistantGrid(5,0.0,2.0) - (id_l, id_r) = boundary_identifiers(g) - @test boundary_grid(g,id_l) == EquidistantGrid{Float64}() - @test boundary_grid(g,id_r) == EquidistantGrid{Float64}() - @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}()) - @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}()) - end - @testset "2D" begin - g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) - (id_w, id_e, id_s, id_n) = boundary_identifiers(g) - @test boundary_grid(g,id_w) == restrict(g,2) - @test boundary_grid(g,id_e) == restrict(g,2) - @test boundary_grid(g,id_s) == restrict(g,1) - @test boundary_grid(g,id_n) == restrict(g,1) - @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) - end - @testset "3D" begin - g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) - (id_w, id_e, - id_s, id_n, - id_t, id_b) = boundary_identifiers(g) - @test boundary_grid(g,id_w) == restrict(g,[2,3]) - @test boundary_grid(g,id_e) == restrict(g,[2,3]) - @test boundary_grid(g,id_s) == restrict(g,[1,3]) - @test boundary_grid(g,id_n) == restrict(g,[1,3]) - @test boundary_grid(g,id_t) == restrict(g,[1,2]) - @test boundary_grid(g,id_b) == restrict(g,[1,2]) - @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) - end - end -end - -end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/LazyTensors/LazyTensors_test.jl Sat Feb 20 20:45:40 2021 +0100 @@ -0,0 +1,580 @@ +using Test +using Sbplib.LazyTensors +using Sbplib.RegionIndices + +using Tullio + +@testset "LazyTensors" begin + +@testset "Generic Mapping methods" begin + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end + LazyTensors.apply(m::DummyMapping{T,R,D}, v, I::Vararg{Any,R}) where {T,R,D} = :apply + @test range_dim(DummyMapping{Int,2,3}()) == 2 + @test domain_dim(DummyMapping{Int,2,3}()) == 3 + @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0,0) == :apply + @test eltype(DummyMapping{Int,2,3}()) == Int + @test eltype(DummyMapping{Float64,2,3}()) == Float64 +end + +@testset "Mapping transpose" begin + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end + + LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply + LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose + + LazyTensors.range_size(m::DummyMapping) = :range_size + LazyTensors.domain_size(m::DummyMapping) = :domain_size + + m = DummyMapping{Float64,2,3}() + @test m' isa TensorMapping{Float64, 3,2} + @test m'' == m + @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose + @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply + @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply + + @test range_size(m') == :domain_size + @test domain_size(m') == :range_size +end + +@testset "TensorApplication" begin + struct SizeDoublingMapping{T,R,D} <: TensorMapping{T,R,D} + domain_size::NTuple{D,Int} + end + + LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i) + LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size + LazyTensors.domain_size(m::SizeDoublingMapping) = m.domain_size + + + m = SizeDoublingMapping{Int, 1, 1}((3,)) + v = [0,1,2] + @test m*v isa AbstractVector{Int} + @test size(m*v) == 2 .*size(v) + @test (m*v)[0] == (:apply,v,(0,)) + @test m*m*v isa AbstractVector{Int} + @test (m*m*v)[1] == (:apply,m*v,(1,)) + @test (m*m*v)[3] == (:apply,m*v,(3,)) + @test (m*m*v)[6] == (:apply,m*v,(6,)) + @test_broken BoundsError == (m*m*v)[0] + @test_broken BoundsError == (m*m*v)[7] + @test_throws MethodError m*m + + m = SizeDoublingMapping{Int, 2, 1}((3,)) + @test_throws MethodError m*ones(Int,2,2) + @test_throws MethodError m*m*v + + m = SizeDoublingMapping{Float64, 2, 2}((3,3)) + v = ones(3,3) + @test size(m*v) == 2 .*size(v) + @test (m*v)[1,2] == (:apply,v,(1,2)) + + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + m = ScalingOperator{Int,1}(2,(3,)) + v = [1,2,3] + @test m*v isa AbstractVector + @test m*v == [2,4,6] + + m = ScalingOperator{Int,2}(2,(2,2)) + v = [[1 2];[3 4]] + @test m*v == [[2 4];[6 8]] + @test (m*v)[2,1] == 6 +end + +@testset "TensorMapping binary operations" begin + struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} + λ::T + range_size::NTuple{R,Int} + domain_size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalarMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = m.λ*v[I...] + LazyTensors.range_size(m::ScalarMapping) = m.domain_size + LazyTensors.domain_size(m::ScalarMapping) = m.range_size + + A = ScalarMapping{Float64,1,1}(2.0, (3,), (3,)) + B = ScalarMapping{Float64,1,1}(3.0, (3,), (3,)) + + v = [1.1,1.2,1.3] + for i ∈ eachindex(v) + @test ((A+B)*v)[i] == 2*v[i] + 3*v[i] + end + + for i ∈ eachindex(v) + @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] + end + + @test range_size(A+B) == range_size(A) == range_size(B) + @test domain_size(A+B) == domain_size(A) == domain_size(B) +end + +@testset "LazyArray" begin + @testset "LazyConstantArray" begin + @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2} + + lca = LazyTensors.LazyConstantArray(3.0,(3,2)) + @test eltype(lca) == Float64 + @test ndims(lca) == 2 + @test size(lca) == (3,2) + @test lca[2] == 3.0 + end + struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} + data::T1 + end + Base.size(v::DummyArray) = size(v.data) + Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] + + # Test lazy operations + v1 = [1, 2.3, 4] + v2 = [1., 2, 3] + s = 3.4 + r_add_v = v1 .+ v2 + r_sub_v = v1 .- v2 + r_times_v = v1 .* v2 + r_div_v = v1 ./ v2 + r_add_s = v1 .+ s + r_sub_s = v1 .- s + r_times_s = v1 .* s + r_div_s = v1 ./ s + @test isa(v1 +̃ v2, LazyArray) + @test isa(v1 -̃ v2, LazyArray) + @test isa(v1 *̃ v2, LazyArray) + @test isa(v1 /̃ v2, LazyArray) + @test isa(v1 +̃ s, LazyArray) + @test isa(v1 -̃ s, LazyArray) + @test isa(v1 *̃ s, LazyArray) + @test isa(v1 /̃ s, LazyArray) + @test isa(s +̃ v1, LazyArray) + @test isa(s -̃ v1, LazyArray) + @test isa(s *̃ v1, LazyArray) + @test isa(s /̃ v1, LazyArray) + for i ∈ eachindex(v1) + @test (v1 +̃ v2)[i] == r_add_v[i] + @test (v1 -̃ v2)[i] == r_sub_v[i] + @test (v1 *̃ v2)[i] == r_times_v[i] + @test (v1 /̃ v2)[i] == r_div_v[i] + @test (v1 +̃ s)[i] == r_add_s[i] + @test (v1 -̃ s)[i] == r_sub_s[i] + @test (v1 *̃ s)[i] == r_times_s[i] + @test (v1 /̃ s)[i] == r_div_s[i] + @test (s +̃ v1)[i] == r_add_s[i] + @test (s -̃ v1)[i] == -r_sub_s[i] + @test (s *̃ v1)[i] == r_times_s[i] + @test (s /̃ v1)[i] == 1/r_div_s[i] + end + @test_throws BoundsError (v1 +̃ v2)[4] + v2 = [1., 2, 3, 4] + # Test that size of arrays is asserted when not specified inbounds + # TODO: Replace these errors with SizeMismatch + @test_throws DimensionMismatch v1 +̃ v2 + + # Test operations on LazyArray + v1 = DummyArray([1, 2.3, 4]) + v2 = [1., 2, 3] + @test isa(v1 + v2, LazyArray) + @test isa(v2 + v1, LazyArray) + @test isa(v1 - v2, LazyArray) + @test isa(v2 - v1, LazyArray) + for i ∈ eachindex(v2) + @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i] + @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i] + end + @test_throws BoundsError (v1 + v2)[4] + v2 = [1., 2, 3, 4] + # Test that size of arrays is asserted when not specified inbounds + # TODO: Replace these errors with SizeMismatch + @test_throws DimensionMismatch v1 + v2 +end + + +@testset "LazyFunctionArray" begin + @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] + @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ + 1 2; + 2 4; + 3 6; + ] + + @test size(LazyFunctionArray(i->i^2, (3,))) == (3,) + @test size(LazyFunctionArray((i,j)->i*j, (3,2))) == (3,2) + + @inferred LazyFunctionArray(i->i^2, (3,))[2] + + @test_throws BoundsError LazyFunctionArray(i->i^2, (3,))[4] + @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2] + @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3] + +end + +@testset "TensorMappingComposition" begin + A = rand(2,3) + B = rand(3,4) + + à = LazyLinearMap(A, (1,), (2,)) + B̃ = LazyLinearMap(B, (1,), (2,)) + + @test Ã∘B̃ isa TensorMappingComposition + @test range_size(Ã∘B̃) == (2,) + @test domain_size(Ã∘B̃) == (4,) + @test_throws SizeMismatch B̃∘à + + # @test @inbounds B̃∘à # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16) + + v = rand(4) + @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 + + v = rand(2) + @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14 +end + +@testset "LazyLinearMap" begin + # Test a standard matrix-vector product + # mapping vectors of size 4 to vectors of size 3. + A = rand(3,4) + à = LazyLinearMap(A, (1,), (2,)) + v = rand(4) + w = rand(3) + + @test à isa LazyLinearMap{T,1,1} where T + @test à isa TensorMapping{T,1,1} where T + @test range_size(Ã) == (3,) + @test domain_size(Ã) == (4,) + + @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 + @test Ã*v ≈ A*v atol=5e-13 + @test Ã'*w ≈ A'*w + + A = rand(2,3,4) + @test_throws DomainError LazyLinearMap(A, (3,1), (2,)) + + # Test more exotic mappings + B = rand(3,4,2) + # Map vectors of size 2 to matrices of size (3,4) + B̃ = LazyLinearMap(B, (1,2), (3,)) + v = rand(2) + + @test range_size(B̃) == (3,4) + @test domain_size(B̃) == (2,) + @test B̃ isa TensorMapping{T,2,1} where T + @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 + @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13 + + # Map matrices of size (3,2) to vectors of size 4 + B̃ = LazyLinearMap(B, (2,), (1,3)) + v = rand(3,2) + + @test range_size(B̃) == (4,) + @test domain_size(B̃) == (3,2) + @test B̃ isa TensorMapping{T,1,2} where T + @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + + B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 + @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] + + B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 + + + # TODO: + # @inferred (B̃*v)[2] +end + + +@testset "IdentityMapping" begin + @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T + @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T + @test IdentityMapping{Float64}((4,5)) == IdentityMapping{Float64}(4,5) + + @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} + + for sz ∈ [(4,5),(3,),(5,6,4)] + I = IdentityMapping{Float64}(sz) + v = rand(sz...) + @test I*v == v + @test I'*v == v + + @test range_size(I) == sz + @test domain_size(I) == sz + end + + I = IdentityMapping{Float64}((4,5)) + v = rand(4,5) + @inferred (I*v)[3,2] + @inferred (I'*v)[3,2] + @inferred range_size(I) + + @inferred range_dim(I) + @inferred domain_dim(I) + + à = rand(4,2) + A = LazyLinearMap(Ã,(1,),(2,)) + I1 = IdentityMapping{Float64}(2) + I2 = IdentityMapping{Float64}(4) + @test A∘I1 == A + @test I2∘A == A + @test I1∘I1 == I1 + @test_throws SizeMismatch I1∘A + @test_throws SizeMismatch A∘I2 + @test_throws SizeMismatch I1∘I2 +end + +@testset "InflatedTensorMapping" begin + I(sz...) = IdentityMapping(sz...) + + à = rand(4,2) + B̃ = rand(4,2,3) + C̃ = rand(4,2,3) + + A = LazyLinearMap(Ã,(1,),(2,)) + B = LazyLinearMap(B̃,(1,2),(3,)) + C = LazyLinearMap(C̃,(1,),(2,3)) + + @testset "Constructors" begin + @test InflatedTensorMapping(I(3,2), A, I(4)) isa TensorMapping{Float64, 4, 4} + @test InflatedTensorMapping(I(3,2), B, I(4)) isa TensorMapping{Float64, 5, 4} + @test InflatedTensorMapping(I(3), C, I(2,3)) isa TensorMapping{Float64, 4, 5} + @test InflatedTensorMapping(C, I(2,3)) isa TensorMapping{Float64, 3, 4} + @test InflatedTensorMapping(I(3), C) isa TensorMapping{Float64, 2, 3} + @test InflatedTensorMapping(I(3), I(2,3)) isa TensorMapping{Float64, 3, 3} + end + + @testset "Range and domain size" begin + @test range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) + @test domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) + + @test range_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,4,2,4) + @test domain_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,3,4) + + @test range_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,4,2,3) + @test domain_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,2,3,2,3) + + @inferred range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) + @inferred domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) + end + + @testset "Application" begin + # Testing regular application and transposed application with inflation "before", "after" and "before and after". + # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input. + tests = [ + ( + InflatedTensorMapping(I(3,2), A, I(4)), + (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply + (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose + ), + ( + InflatedTensorMapping(I(3,2), B, I(4)), + (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]), + (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]), + ), + ( + InflatedTensorMapping(I(3,2), C, I(4)), + (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]), + (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]), + ), + ( + InflatedTensorMapping(I(3,2), A), + (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]), + (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]), + ), + ( + InflatedTensorMapping(I(3,2), B), + (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]), + (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]), + ), + ( + InflatedTensorMapping(I(3,2), C), + (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]), + (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]), + ), + ( + InflatedTensorMapping(A,I(4)), + (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]), + (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]), + ), + ( + InflatedTensorMapping(B,I(4)), + (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]), + (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]), + ), + ( + InflatedTensorMapping(C,I(4)), + (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]), + (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]), + ), + ] + + @testset "apply" begin + for i ∈ 1:length(tests) + tm = tests[i][1] + v = rand(domain_size(tm)...) + true_value = tests[i][2](v) + @test tm*v ≈ true_value rtol=1e-14 + end + end + + @testset "apply_transpose" begin + for i ∈ 1:length(tests) + tm = tests[i][1] + v = rand(range_size(tm)...) + true_value = tests[i][3](v) + @test tm'*v ≈ true_value rtol=1e-14 + end + end + + @testset "Inference of application" begin + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + tm = InflatedTensorMapping(I(2,3),ScalingOperator(2.0, (3,2)),I(3,4)) + v = rand(domain_size(tm)...) + + @inferred apply(tm,v,1,2,3,2,2,4) + @inferred (tm*v)[1,2,3,2,2,4] + end + end + + @testset "InflatedTensorMapping of InflatedTensorMapping" begin + A = ScalingOperator(2.0,(2,3)) + itm = InflatedTensorMapping(I(3,2), A, I(4)) + @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2)) + @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2)) + @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4)) + + @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type. + end +end + +@testset "split_index" begin + @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4)) + @test LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,:,:,5,6),(3,4)) + @test LazyTensors.split_index(Val(3),Val(1),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,5,6),(4,)) + @test LazyTensors.split_index(Val(3),Val(2),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,:,5,6),(4,)) + @test LazyTensors.split_index(Val(1),Val(1),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,4,5,6),(2,3)) + @test LazyTensors.split_index(Val(1),Val(2),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,:,4,5,6),(2,3)) + + @test LazyTensors.split_index(Val(0),Val(1),Val(3),Val(3),1,2,3,4,5,6) == ((:,4,5,6),(1,2,3)) + @test LazyTensors.split_index(Val(3),Val(1),Val(3),Val(0),1,2,3,4,5,6) == ((1,2,3,:),(4,5,6)) + + @inferred LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,2,2,4) +end + +@testset "slice_tuple" begin + @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(1), Val(3)) == (1,2,3) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(4), Val(6)) == (4,5,6) +end + +@testset "split_tuple" begin + @testset "2 parts" begin + @test LazyTensors.split_tuple((),Val(0)) == ((),()) + @test LazyTensors.split_tuple((1,),Val(0)) == ((),(1,)) + @test LazyTensors.split_tuple((1,),Val(1)) == ((1,),()) + + @test LazyTensors.split_tuple((1,2,3,4),Val(0)) == ((),(1,2,3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(1)) == ((1,),(2,3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(2)) == ((1,2),(3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(3)) == ((1,2,3),(4,)) + @test LazyTensors.split_tuple((1,2,3,4),Val(4)) == ((1,2,3,4),()) + + @test LazyTensors.split_tuple((1,2,true,4),Val(3)) == ((1,2,true),(4,)) + + @inferred LazyTensors.split_tuple((1,2,3,4),Val(3)) + @inferred LazyTensors.split_tuple((1,2,true,4),Val(3)) + end + + @testset "3 parts" begin + @test LazyTensors.split_tuple((),Val(0),Val(0)) == ((),(),()) + @test LazyTensors.split_tuple((1,2,3),Val(1), Val(1)) == ((1,),(2,),(3,)) + @test LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) == ((1,),(true,),(3,)) + + @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(1),Val(2)) == ((1,),(2,3),(4,5,6)) + @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) == ((1,2,3),(4,5),(6,)) + + @inferred LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) + @inferred LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) + end +end + +@testset "flatten_tuple" begin + @test LazyTensors.flatten_tuple((1,)) == (1,) + @test LazyTensors.flatten_tuple((1,2,3,4,5,6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple((1,2,(3,4),5,6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple((1,2,(3,(4,5)),6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple(((1,2),(3,4),(5,),6)) == (1,2,3,4,5,6) +end + + +@testset "LazyOuterProduct" begin + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + A = ScalingOperator(2.0, (5,)) + B = ScalingOperator(3.0, (3,)) + C = ScalingOperator(5.0, (3,2)) + + AB = LazyOuterProduct(A,B) + @test AB isa TensorMapping{T,2,2} where T + @test range_size(AB) == (5,3) + @test domain_size(AB) == (5,3) + + v = rand(range_size(AB)...) + @test AB*v == 6*v + + ABC = LazyOuterProduct(A,B,C) + + @test ABC isa TensorMapping{T,4,4} where T + @test range_size(ABC) == (5,3,3,2) + @test domain_size(ABC) == (5,3,3,2) + + @test A⊗B == AB + @test A⊗B⊗C == ABC + + A = rand(3,2) + B = rand(2,4,3) + + v₁ = rand(2,4,3) + v₂ = rand(4,3,2) + + à = LazyLinearMap(A,(1,),(2,)) + B̃ = LazyLinearMap(B,(1,),(2,3)) + + ÃB̃ = LazyOuterProduct(Ã,B̃) + @tullio ABv[i,k] := A[i,j]*B[k,l,m]*v₁[j,l,m] + @test ÃB̃*v₁ ≈ ABv + + B̃à = LazyOuterProduct(B̃,Ã) + @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j] + @test B̃Ã*v₂ ≈ BAv + + @testset "Indentity mapping arguments" begin + @test LazyOuterProduct(IdentityMapping(3,2), IdentityMapping(1,2)) == IdentityMapping(3,2,1,2) + + à = LazyLinearMap(A,(1,),(2,)) + @test LazyOuterProduct(IdentityMapping(3,2), Ã) == InflatedTensorMapping(IdentityMapping(3,2),Ã) + @test LazyOuterProduct(Ã, IdentityMapping(3,2)) == InflatedTensorMapping(Ã,IdentityMapping(3,2)) + + I1 = IdentityMapping(3,2) + I2 = IdentityMapping(4) + @test I1⊗Ã⊗I2 == InflatedTensorMapping(I1, Ã, I2) + end + +end + +end
--- a/test/LazyTensors/testLazyTensors.jl Sat Feb 20 20:36:27 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,580 +0,0 @@ -using Test -using Sbplib.LazyTensors -using Sbplib.RegionIndices - -using Tullio - -@testset "LazyTensors" begin - -@testset "Generic Mapping methods" begin - struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end - LazyTensors.apply(m::DummyMapping{T,R,D}, v, I::Vararg{Any,R}) where {T,R,D} = :apply - @test range_dim(DummyMapping{Int,2,3}()) == 2 - @test domain_dim(DummyMapping{Int,2,3}()) == 3 - @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0,0) == :apply - @test eltype(DummyMapping{Int,2,3}()) == Int - @test eltype(DummyMapping{Float64,2,3}()) == Float64 -end - -@testset "Mapping transpose" begin - struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end - - LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply - LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose - - LazyTensors.range_size(m::DummyMapping) = :range_size - LazyTensors.domain_size(m::DummyMapping) = :domain_size - - m = DummyMapping{Float64,2,3}() - @test m' isa TensorMapping{Float64, 3,2} - @test m'' == m - @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose - @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply - @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply - - @test range_size(m') == :domain_size - @test domain_size(m') == :range_size -end - -@testset "TensorApplication" begin - struct SizeDoublingMapping{T,R,D} <: TensorMapping{T,R,D} - domain_size::NTuple{D,Int} - end - - LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i) - LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size - LazyTensors.domain_size(m::SizeDoublingMapping) = m.domain_size - - - m = SizeDoublingMapping{Int, 1, 1}((3,)) - v = [0,1,2] - @test m*v isa AbstractVector{Int} - @test size(m*v) == 2 .*size(v) - @test (m*v)[0] == (:apply,v,(0,)) - @test m*m*v isa AbstractVector{Int} - @test (m*m*v)[1] == (:apply,m*v,(1,)) - @test (m*m*v)[3] == (:apply,m*v,(3,)) - @test (m*m*v)[6] == (:apply,m*v,(6,)) - @test_broken BoundsError == (m*m*v)[0] - @test_broken BoundsError == (m*m*v)[7] - @test_throws MethodError m*m - - m = SizeDoublingMapping{Int, 2, 1}((3,)) - @test_throws MethodError m*ones(Int,2,2) - @test_throws MethodError m*m*v - - m = SizeDoublingMapping{Float64, 2, 2}((3,3)) - v = ones(3,3) - @test size(m*v) == 2 .*size(v) - @test (m*v)[1,2] == (:apply,v,(1,2)) - - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - m = ScalingOperator{Int,1}(2,(3,)) - v = [1,2,3] - @test m*v isa AbstractVector - @test m*v == [2,4,6] - - m = ScalingOperator{Int,2}(2,(2,2)) - v = [[1 2];[3 4]] - @test m*v == [[2 4];[6 8]] - @test (m*v)[2,1] == 6 -end - -@testset "TensorMapping binary operations" begin - struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} - λ::T - range_size::NTuple{R,Int} - domain_size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalarMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = m.λ*v[I...] - LazyTensors.range_size(m::ScalarMapping) = m.domain_size - LazyTensors.domain_size(m::ScalarMapping) = m.range_size - - A = ScalarMapping{Float64,1,1}(2.0, (3,), (3,)) - B = ScalarMapping{Float64,1,1}(3.0, (3,), (3,)) - - v = [1.1,1.2,1.3] - for i ∈ eachindex(v) - @test ((A+B)*v)[i] == 2*v[i] + 3*v[i] - end - - for i ∈ eachindex(v) - @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] - end - - @test range_size(A+B) == range_size(A) == range_size(B) - @test domain_size(A+B) == domain_size(A) == domain_size(B) -end - -@testset "LazyArray" begin - @testset "LazyConstantArray" begin - @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2} - - lca = LazyTensors.LazyConstantArray(3.0,(3,2)) - @test eltype(lca) == Float64 - @test ndims(lca) == 2 - @test size(lca) == (3,2) - @test lca[2] == 3.0 - end - struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} - data::T1 - end - Base.size(v::DummyArray) = size(v.data) - Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] - - # Test lazy operations - v1 = [1, 2.3, 4] - v2 = [1., 2, 3] - s = 3.4 - r_add_v = v1 .+ v2 - r_sub_v = v1 .- v2 - r_times_v = v1 .* v2 - r_div_v = v1 ./ v2 - r_add_s = v1 .+ s - r_sub_s = v1 .- s - r_times_s = v1 .* s - r_div_s = v1 ./ s - @test isa(v1 +̃ v2, LazyArray) - @test isa(v1 -̃ v2, LazyArray) - @test isa(v1 *̃ v2, LazyArray) - @test isa(v1 /̃ v2, LazyArray) - @test isa(v1 +̃ s, LazyArray) - @test isa(v1 -̃ s, LazyArray) - @test isa(v1 *̃ s, LazyArray) - @test isa(v1 /̃ s, LazyArray) - @test isa(s +̃ v1, LazyArray) - @test isa(s -̃ v1, LazyArray) - @test isa(s *̃ v1, LazyArray) - @test isa(s /̃ v1, LazyArray) - for i ∈ eachindex(v1) - @test (v1 +̃ v2)[i] == r_add_v[i] - @test (v1 -̃ v2)[i] == r_sub_v[i] - @test (v1 *̃ v2)[i] == r_times_v[i] - @test (v1 /̃ v2)[i] == r_div_v[i] - @test (v1 +̃ s)[i] == r_add_s[i] - @test (v1 -̃ s)[i] == r_sub_s[i] - @test (v1 *̃ s)[i] == r_times_s[i] - @test (v1 /̃ s)[i] == r_div_s[i] - @test (s +̃ v1)[i] == r_add_s[i] - @test (s -̃ v1)[i] == -r_sub_s[i] - @test (s *̃ v1)[i] == r_times_s[i] - @test (s /̃ v1)[i] == 1/r_div_s[i] - end - @test_throws BoundsError (v1 +̃ v2)[4] - v2 = [1., 2, 3, 4] - # Test that size of arrays is asserted when not specified inbounds - # TODO: Replace these errors with SizeMismatch - @test_throws DimensionMismatch v1 +̃ v2 - - # Test operations on LazyArray - v1 = DummyArray([1, 2.3, 4]) - v2 = [1., 2, 3] - @test isa(v1 + v2, LazyArray) - @test isa(v2 + v1, LazyArray) - @test isa(v1 - v2, LazyArray) - @test isa(v2 - v1, LazyArray) - for i ∈ eachindex(v2) - @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i] - @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i] - end - @test_throws BoundsError (v1 + v2)[4] - v2 = [1., 2, 3, 4] - # Test that size of arrays is asserted when not specified inbounds - # TODO: Replace these errors with SizeMismatch - @test_throws DimensionMismatch v1 + v2 -end - - -@testset "LazyFunctionArray" begin - @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] - @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ - 1 2; - 2 4; - 3 6; - ] - - @test size(LazyFunctionArray(i->i^2, (3,))) == (3,) - @test size(LazyFunctionArray((i,j)->i*j, (3,2))) == (3,2) - - @inferred LazyFunctionArray(i->i^2, (3,))[2] - - @test_throws BoundsError LazyFunctionArray(i->i^2, (3,))[4] - @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2] - @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3] - -end - -@testset "TensorMappingComposition" begin - A = rand(2,3) - B = rand(3,4) - - à = LazyLinearMap(A, (1,), (2,)) - B̃ = LazyLinearMap(B, (1,), (2,)) - - @test Ã∘B̃ isa TensorMappingComposition - @test range_size(Ã∘B̃) == (2,) - @test domain_size(Ã∘B̃) == (4,) - @test_throws SizeMismatch B̃∘à - - # @test @inbounds B̃∘à # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16) - - v = rand(4) - @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 - - v = rand(2) - @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14 -end - -@testset "LazyLinearMap" begin - # Test a standard matrix-vector product - # mapping vectors of size 4 to vectors of size 3. - A = rand(3,4) - à = LazyLinearMap(A, (1,), (2,)) - v = rand(4) - w = rand(3) - - @test à isa LazyLinearMap{T,1,1} where T - @test à isa TensorMapping{T,1,1} where T - @test range_size(Ã) == (3,) - @test domain_size(Ã) == (4,) - - @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 - @test Ã*v ≈ A*v atol=5e-13 - @test Ã'*w ≈ A'*w - - A = rand(2,3,4) - @test_throws DomainError LazyLinearMap(A, (3,1), (2,)) - - # Test more exotic mappings - B = rand(3,4,2) - # Map vectors of size 2 to matrices of size (3,4) - B̃ = LazyLinearMap(B, (1,2), (3,)) - v = rand(2) - - @test range_size(B̃) == (3,4) - @test domain_size(B̃) == (2,) - @test B̃ isa TensorMapping{T,2,1} where T - @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 - @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13 - - # Map matrices of size (3,2) to vectors of size 4 - B̃ = LazyLinearMap(B, (2,), (1,3)) - v = rand(3,2) - - @test range_size(B̃) == (4,) - @test domain_size(B̃) == (3,2) - @test B̃ isa TensorMapping{T,1,2} where T - @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + - B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 - @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] + - B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 - - - # TODO: - # @inferred (B̃*v)[2] -end - - -@testset "IdentityMapping" begin - @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T - @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T - @test IdentityMapping{Float64}((4,5)) == IdentityMapping{Float64}(4,5) - - @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} - - for sz ∈ [(4,5),(3,),(5,6,4)] - I = IdentityMapping{Float64}(sz) - v = rand(sz...) - @test I*v == v - @test I'*v == v - - @test range_size(I) == sz - @test domain_size(I) == sz - end - - I = IdentityMapping{Float64}((4,5)) - v = rand(4,5) - @inferred (I*v)[3,2] - @inferred (I'*v)[3,2] - @inferred range_size(I) - - @inferred range_dim(I) - @inferred domain_dim(I) - - à = rand(4,2) - A = LazyLinearMap(Ã,(1,),(2,)) - I1 = IdentityMapping{Float64}(2) - I2 = IdentityMapping{Float64}(4) - @test A∘I1 == A - @test I2∘A == A - @test I1∘I1 == I1 - @test_throws SizeMismatch I1∘A - @test_throws SizeMismatch A∘I2 - @test_throws SizeMismatch I1∘I2 -end - -@testset "InflatedTensorMapping" begin - I(sz...) = IdentityMapping(sz...) - - à = rand(4,2) - B̃ = rand(4,2,3) - C̃ = rand(4,2,3) - - A = LazyLinearMap(Ã,(1,),(2,)) - B = LazyLinearMap(B̃,(1,2),(3,)) - C = LazyLinearMap(C̃,(1,),(2,3)) - - @testset "Constructors" begin - @test InflatedTensorMapping(I(3,2), A, I(4)) isa TensorMapping{Float64, 4, 4} - @test InflatedTensorMapping(I(3,2), B, I(4)) isa TensorMapping{Float64, 5, 4} - @test InflatedTensorMapping(I(3), C, I(2,3)) isa TensorMapping{Float64, 4, 5} - @test InflatedTensorMapping(C, I(2,3)) isa TensorMapping{Float64, 3, 4} - @test InflatedTensorMapping(I(3), C) isa TensorMapping{Float64, 2, 3} - @test InflatedTensorMapping(I(3), I(2,3)) isa TensorMapping{Float64, 3, 3} - end - - @testset "Range and domain size" begin - @test range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) - @test domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) - - @test range_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,4,2,4) - @test domain_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,3,4) - - @test range_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,4,2,3) - @test domain_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,2,3,2,3) - - @inferred range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) - @inferred domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) - end - - @testset "Application" begin - # Testing regular application and transposed application with inflation "before", "after" and "before and after". - # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input. - tests = [ - ( - InflatedTensorMapping(I(3,2), A, I(4)), - (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply - (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose - ), - ( - InflatedTensorMapping(I(3,2), B, I(4)), - (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]), - (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]), - ), - ( - InflatedTensorMapping(I(3,2), C, I(4)), - (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]), - (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]), - ), - ( - InflatedTensorMapping(I(3,2), A), - (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]), - (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]), - ), - ( - InflatedTensorMapping(I(3,2), B), - (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]), - (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]), - ), - ( - InflatedTensorMapping(I(3,2), C), - (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]), - (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]), - ), - ( - InflatedTensorMapping(A,I(4)), - (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]), - (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]), - ), - ( - InflatedTensorMapping(B,I(4)), - (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]), - (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]), - ), - ( - InflatedTensorMapping(C,I(4)), - (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]), - (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]), - ), - ] - - @testset "apply" begin - for i ∈ 1:length(tests) - tm = tests[i][1] - v = rand(domain_size(tm)...) - true_value = tests[i][2](v) - @test tm*v ≈ true_value rtol=1e-14 - end - end - - @testset "apply_transpose" begin - for i ∈ 1:length(tests) - tm = tests[i][1] - v = rand(range_size(tm)...) - true_value = tests[i][3](v) - @test tm'*v ≈ true_value rtol=1e-14 - end - end - - @testset "Inference of application" begin - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - tm = InflatedTensorMapping(I(2,3),ScalingOperator(2.0, (3,2)),I(3,4)) - v = rand(domain_size(tm)...) - - @inferred apply(tm,v,1,2,3,2,2,4) - @inferred (tm*v)[1,2,3,2,2,4] - end - end - - @testset "InflatedTensorMapping of InflatedTensorMapping" begin - A = ScalingOperator(2.0,(2,3)) - itm = InflatedTensorMapping(I(3,2), A, I(4)) - @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2)) - @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2)) - @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4)) - - @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type. - end -end - -@testset "split_index" begin - @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4)) - @test LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,:,:,5,6),(3,4)) - @test LazyTensors.split_index(Val(3),Val(1),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,5,6),(4,)) - @test LazyTensors.split_index(Val(3),Val(2),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,:,5,6),(4,)) - @test LazyTensors.split_index(Val(1),Val(1),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,4,5,6),(2,3)) - @test LazyTensors.split_index(Val(1),Val(2),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,:,4,5,6),(2,3)) - - @test LazyTensors.split_index(Val(0),Val(1),Val(3),Val(3),1,2,3,4,5,6) == ((:,4,5,6),(1,2,3)) - @test LazyTensors.split_index(Val(3),Val(1),Val(3),Val(0),1,2,3,4,5,6) == ((1,2,3,:),(4,5,6)) - - @inferred LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,2,2,4) -end - -@testset "slice_tuple" begin - @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(1), Val(3)) == (1,2,3) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(4), Val(6)) == (4,5,6) -end - -@testset "split_tuple" begin - @testset "2 parts" begin - @test LazyTensors.split_tuple((),Val(0)) == ((),()) - @test LazyTensors.split_tuple((1,),Val(0)) == ((),(1,)) - @test LazyTensors.split_tuple((1,),Val(1)) == ((1,),()) - - @test LazyTensors.split_tuple((1,2,3,4),Val(0)) == ((),(1,2,3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(1)) == ((1,),(2,3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(2)) == ((1,2),(3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(3)) == ((1,2,3),(4,)) - @test LazyTensors.split_tuple((1,2,3,4),Val(4)) == ((1,2,3,4),()) - - @test LazyTensors.split_tuple((1,2,true,4),Val(3)) == ((1,2,true),(4,)) - - @inferred LazyTensors.split_tuple((1,2,3,4),Val(3)) - @inferred LazyTensors.split_tuple((1,2,true,4),Val(3)) - end - - @testset "3 parts" begin - @test LazyTensors.split_tuple((),Val(0),Val(0)) == ((),(),()) - @test LazyTensors.split_tuple((1,2,3),Val(1), Val(1)) == ((1,),(2,),(3,)) - @test LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) == ((1,),(true,),(3,)) - - @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(1),Val(2)) == ((1,),(2,3),(4,5,6)) - @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) == ((1,2,3),(4,5),(6,)) - - @inferred LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) - @inferred LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) - end -end - -@testset "flatten_tuple" begin - @test LazyTensors.flatten_tuple((1,)) == (1,) - @test LazyTensors.flatten_tuple((1,2,3,4,5,6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple((1,2,(3,4),5,6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple((1,2,(3,(4,5)),6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple(((1,2),(3,4),(5,),6)) == (1,2,3,4,5,6) -end - - -@testset "LazyOuterProduct" begin - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - A = ScalingOperator(2.0, (5,)) - B = ScalingOperator(3.0, (3,)) - C = ScalingOperator(5.0, (3,2)) - - AB = LazyOuterProduct(A,B) - @test AB isa TensorMapping{T,2,2} where T - @test range_size(AB) == (5,3) - @test domain_size(AB) == (5,3) - - v = rand(range_size(AB)...) - @test AB*v == 6*v - - ABC = LazyOuterProduct(A,B,C) - - @test ABC isa TensorMapping{T,4,4} where T - @test range_size(ABC) == (5,3,3,2) - @test domain_size(ABC) == (5,3,3,2) - - @test A⊗B == AB - @test A⊗B⊗C == ABC - - A = rand(3,2) - B = rand(2,4,3) - - v₁ = rand(2,4,3) - v₂ = rand(4,3,2) - - à = LazyLinearMap(A,(1,),(2,)) - B̃ = LazyLinearMap(B,(1,),(2,3)) - - ÃB̃ = LazyOuterProduct(Ã,B̃) - @tullio ABv[i,k] := A[i,j]*B[k,l,m]*v₁[j,l,m] - @test ÃB̃*v₁ ≈ ABv - - B̃à = LazyOuterProduct(B̃,Ã) - @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j] - @test B̃Ã*v₂ ≈ BAv - - @testset "Indentity mapping arguments" begin - @test LazyOuterProduct(IdentityMapping(3,2), IdentityMapping(1,2)) == IdentityMapping(3,2,1,2) - - à = LazyLinearMap(A,(1,),(2,)) - @test LazyOuterProduct(IdentityMapping(3,2), Ã) == InflatedTensorMapping(IdentityMapping(3,2),Ã) - @test LazyOuterProduct(Ã, IdentityMapping(3,2)) == InflatedTensorMapping(Ã,IdentityMapping(3,2)) - - I1 = IdentityMapping(3,2) - I2 = IdentityMapping(4) - @test I1⊗Ã⊗I2 == InflatedTensorMapping(I1, Ã, I2) - end - -end - -end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/RegionIndices/RegionIndices_test.jl Sat Feb 20 20:45:40 2021 +0100 @@ -0,0 +1,6 @@ +using Sbplib.RegionIndices +using Test + +@testset "RegionIndices" begin + @test_broken false +end
--- a/test/RegionIndices/testRegionIndices.jl Sat Feb 20 20:36:27 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -using Sbplib.RegionIndices -using Test - -@testset "RegionIndices" begin - @test_broken false -end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/SbpOperators_test.jl Sat Feb 20 20:45:40 2021 +0100 @@ -0,0 +1,832 @@ +using Test +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.RegionIndices +using Sbplib.LazyTensors +using LinearAlgebra +using TOML + +import Sbplib.SbpOperators.Stencil +import Sbplib.SbpOperators.VolumeOperator +import Sbplib.SbpOperators.volume_operator +import Sbplib.SbpOperators.BoundaryOperator +import Sbplib.SbpOperators.boundary_operator +import Sbplib.SbpOperators.even +import Sbplib.SbpOperators.odd + + +@testset "SbpOperators" begin + +@testset "Stencil" begin + s = Stencil((-2,2), (1.,2.,2.,3.,4.)) + @test s isa Stencil{Float64, 5} + + @test eltype(s) == Float64 + @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.)) + + @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4)) + @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4)) + @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4)) + + @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5)) + @test_throws ArgumentError CenteredStencil(1,2,3,4) +end + +@testset "parse_rational" begin + @test SbpOperators.parse_rational("1") isa Rational + @test SbpOperators.parse_rational("1") == 1//1 + @test SbpOperators.parse_rational("1/2") isa Rational + @test SbpOperators.parse_rational("1/2") == 1//2 + @test SbpOperators.parse_rational("37/13") isa Rational + @test SbpOperators.parse_rational("37/13") == 37//13 +end + +@testset "readoperator" begin + toml_str = """ + [meta] + type = "equidistant" + + [order2] + H.inner = ["1"] + + D1.inner_stencil = ["-1/2", "0", "1/2"] + D1.closure_stencils = [ + ["-1", "1"], + ] + + d1.closure = ["-3/2", "2", "-1/2"] + + [order4] + H.closure = ["17/48", "59/48", "43/48", "49/48"] + + D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"] + D2.closure_stencils = [ + [ "2", "-5", "4", "-1", "0", "0"], + [ "1", "-2", "1", "0", "0", "0"], + [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"], + [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"], + ] + """ + + parsed_toml = TOML.parse(toml_str) + @testset "get_stencil" begin + @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil(-1/2, 0., 1/2, center=2) + @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil(-1/2, 0., 1/2; center=1) + @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == Stencil(-1/2, 0., 1/2; center=3) + + @test get_stencil(parsed_toml, "order2", "H", "inner") == Stencil(1.; center=1) + + @test_throws AssertionError get_stencil(parsed_toml, "meta", "type") + @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils") + end + + @testset "get_stencils" begin + @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (Stencil(-1., 1., center=1),) + @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (Stencil(-1., 1., center=2),) + @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (Stencil(-1., 1., center=2),) + + @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == ( + Stencil( 2., -5., 4., -1., 0., 0., center=1), + Stencil( 1., -2., 1., 0., 0., 0., center=1), + Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=1), + Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1), + ) + + @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == ( + Stencil( 2., -5., 4., -1., 0., 0., center=4), + Stencil( 1., -2., 1., 0., 0., 0., center=2), + Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3), + Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1), + ) + + @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == ( + Stencil( 2., -5., 4., -1., 0., 0., center=1), + Stencil( 1., -2., 1., 0., 0., 0., center=2), + Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3), + Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=4), + ) + + @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3)) + @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4)) + @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2)) + end + + @testset "get_tuple" begin + @test get_tuple(parsed_toml, "order2", "d1", "closure") == (-3/2, 2, -1/2) + + @test_throws AssertionError get_tuple(parsed_toml, "meta", "type") + end +end + +@testset "VolumeOperator" begin + inner_stencil = CenteredStencil(1/4, 2/4, 1/4) + closure_stencils = (Stencil(1/2, 1/2; center=1), Stencil(0.,1.; center=2)) + g_1D = EquidistantGrid(11,0.,1.) + g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.)) + g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.)) + @testset "Constructors" begin + @testset "1D" begin + op = VolumeOperator(inner_stencil,closure_stencils,(11,),even) + @test op == VolumeOperator(g_1D,inner_stencil,closure_stencils,even) + @test op == volume_operator(g_1D,inner_stencil,closure_stencils,even,1) + @test op isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) + Ix = IdentityMapping{Float64}((11,)) + Iy = IdentityMapping{Float64}((12,)) + @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy + @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even) + @test op_x isa TensorMapping{T,2,2} where T + @test op_y isa TensorMapping{T,2,2} where T + end + @testset "3D" begin + op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) + op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) + Ix = IdentityMapping{Float64}((11,)) + Iy = IdentityMapping{Float64}((12,)) + Iz = IdentityMapping{Float64}((10,)) + @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy⊗Iz + @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even)⊗Iz + @test op_z == Ix⊗Iy⊗VolumeOperator(inner_stencil,closure_stencils,(10,),even) + @test op_x isa TensorMapping{T,3,3} where T + @test op_y isa TensorMapping{T,3,3} where T + @test op_z isa TensorMapping{T,3,3} where T + end + end + + @testset "Sizes" begin + @testset "1D" begin + op = volume_operator(g_1D,inner_stencil,closure_stencils,even,1) + @test range_size(op) == domain_size(op) == size(g_1D) + end + + @testset "2D" begin + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) + @test range_size(op_y) == domain_size(op_y) == + range_size(op_x) == domain_size(op_x) == size(g_2D) + end + @testset "3D" begin + op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) + op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) + @test range_size(op_z) == domain_size(op_z) == + range_size(op_y) == domain_size(op_y) == + range_size(op_x) == domain_size(op_x) == size(g_3D) + end + end + + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,odd,2) + v = zeros(size(g_2D)) + Nx = size(g_2D)[1] + Ny = size(g_2D)[2] + for i = 1:Nx + v[i,:] .= i + end + rx = copy(v) + rx[1,:] .= 1.5 + rx[Nx,:] .= (2*Nx-1)/2 + ry = copy(v) + ry[:,Ny-1:Ny] = -v[:,Ny-1:Ny] + + @testset "Application" begin + @test op_x*v ≈ rx rtol = 1e-14 + @test op_y*v ≈ ry rtol = 1e-14 + end + + @testset "Regions" begin + @test (op_x*v)[Index(1,Lower),Index(3,Interior)] ≈ rx[1,3] rtol = 1e-14 + @test (op_x*v)[Index(2,Lower),Index(3,Interior)] ≈ rx[2,3] rtol = 1e-14 + @test (op_x*v)[Index(6,Interior),Index(3,Interior)] ≈ rx[6,3] rtol = 1e-14 + @test (op_x*v)[Index(10,Upper),Index(3,Interior)] ≈ rx[10,3] rtol = 1e-14 + @test (op_x*v)[Index(11,Upper),Index(3,Interior)] ≈ rx[11,3] rtol = 1e-14 + + @test_throws BoundsError (op_x*v)[Index(3,Lower),Index(3,Interior)] + @test_throws BoundsError (op_x*v)[Index(9,Upper),Index(3,Interior)] + + @test (op_y*v)[Index(3,Interior),Index(1,Lower)] ≈ ry[3,1] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(2,Lower)] ≈ ry[3,2] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(6,Interior)] ≈ ry[3,6] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(11,Upper)] ≈ ry[3,11] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(12,Upper)] ≈ ry[3,12] rtol = 1e-14 + + @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(10,Upper)] + @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(3,Lower)] + end + + @testset "Inferred" begin + @inferred apply(op_x, v,1,1) + @inferred apply(op_x, v, Index(1,Lower),Index(1,Lower)) + @inferred apply(op_x, v, Index(6,Interior),Index(1,Lower)) + @inferred apply(op_x, v, Index(11,Upper),Index(1,Lower)) + + @inferred apply(op_y, v,1,1) + @inferred apply(op_y, v, Index(1,Lower),Index(1,Lower)) + @inferred apply(op_y, v, Index(1,Lower),Index(6,Interior)) + @inferred apply(op_y, v, Index(1,Lower),Index(11,Upper)) + end + +end + +@testset "SecondDerivative" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Lx = 3.5 + Ly = 3. + g_1D = EquidistantGrid(121, 0.0, Lx) + g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) + + @testset "Constructors" begin + @testset "1D" begin + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + @test Dₓₓ == second_derivative(g_1D,op.innerStencil,op.closureStencils,1) + @test Dₓₓ isa VolumeOperator + end + @testset "2D" begin + Dₓₓ = second_derivative(g_2D,op.innerStencil,op.closureStencils,1) + D2 = second_derivative(g_1D,op.innerStencil,op.closureStencils) + I = IdentityMapping{Float64}(size(g_2D)[2]) + @test Dₓₓ == D2⊗I + @test Dₓₓ isa TensorMapping{T,2,2} where T + end + end + + # Exact differentiation is measured point-wise. In other cases + # the error is measured in the l2-norm. + @testset "Accuracy" begin + @testset "1D" begin + l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); + monomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x) = 1/factorial(i)*x^i + monomials = (monomials...,evalOn(g_1D,f_i)) + end + v = evalOn(g_1D,x -> sin(x)) + vₓₓ = evalOn(g_1D,x -> -sin(x)) + + # 2nd order interior stencil, 1nd order boundary stencil, + # implies that L*v should be exact for monomials up to order 2. + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 + @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2 + end + + # 4th order interior stencil, 2nd order boundary stencil, + # implies that L*v should be exact for monomials up to order 3. + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 + @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10 + @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 + end + end + + @testset "2D" begin + l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); + binomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x,y) = 1/factorial(i)*y^i + x^i + binomials = (binomials...,evalOn(g_2D,f_i)) + end + v = evalOn(g_2D, (x,y) -> sin(x)+cos(y)) + v_yy = evalOn(g_2D,(x,y) -> -cos(y)) + + # 2nd order interior stencil, 1st order boundary stencil, + # implies that L*v should be exact for binomials up to order 2. + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) + @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 + @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 + end + + # 4th order interior stencil, 2nd order boundary stencil, + # implies that L*v should be exact for binomials up to order 3. + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 + @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9 + @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 + end + end + end +end + +@testset "Laplace" begin + g_1D = EquidistantGrid(101, 0.0, 1.) + g_3D = EquidistantGrid((51,101,52), (0.0, -1.0, 0.0), (1., 1., 1.)) + @testset "Constructors" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + L = laplace(g_1D, op.innerStencil, op.closureStencils) + @test L == second_derivative(g_1D, op.innerStencil, op.closureStencils) + @test L isa TensorMapping{T,1,1} where T + end + @testset "3D" begin + L = laplace(g_3D, op.innerStencil, op.closureStencils) + @test L isa TensorMapping{T,3,3} where T + Dxx = second_derivative(g_3D, op.innerStencil, op.closureStencils,1) + Dyy = second_derivative(g_3D, op.innerStencil, op.closureStencils,2) + Dzz = second_derivative(g_3D, op.innerStencil, op.closureStencils,3) + @test L == Dxx + Dyy + Dzz + end + end + + # Exact differentiation is measured point-wise. In other cases + # the error is measured in the l2-norm. + @testset "Accuracy" begin + l2(v) = sqrt(prod(spacing(g_3D))*sum(v.^2)); + polynomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x,y,z) = 1/factorial(i)*(y^i + x^i + z^i) + polynomials = (polynomials...,evalOn(g_3D,f_i)) + end + v = evalOn(g_3D, (x,y,z) -> sin(x) + cos(y) + exp(z)) + Δv = evalOn(g_3D,(x,y,z) -> -sin(x) - cos(y) + exp(z)) + + # 2nd order interior stencil, 1st order boundary stencil, + # implies that L*v should be exact for binomials up to order 2. + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + L = laplace(g_3D,op.innerStencil,op.closureStencils) + @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 + @test L*v ≈ Δv rtol = 5e-2 norm = l2 + end + + # 4th order interior stencil, 2nd order boundary stencil, + # implies that L*v should be exact for binomials up to order 3. + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + L = laplace(g_3D,op.innerStencil,op.closureStencils) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 + @test L*polynomials[4] ≈ polynomials[2] atol = 5e-9 + @test L*v ≈ Δv rtol = 5e-4 norm = l2 + end + end +end + +@testset "Diagonal-stencil inner_product" begin + Lx = π/2. + Ly = Float64(π) + Lz = 1. + g_1D = EquidistantGrid(77, 0.0, Lx) + g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) + g_3D = EquidistantGrid((10,10, 10), (0.0, 0.0, 0.0), (Lx,Ly,Lz)) + integral(H,v) = sum(H*v) + @testset "inner_product" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "0D" begin + H = inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) + @test H == IdentityMapping{Float64}() + @test H isa TensorMapping{T,0,0} where T + end + @testset "1D" begin + H = inner_product(g_1D,op.quadratureClosure) + inner_stencil = CenteredStencil(1.) + @test H == inner_product(g_1D,op.quadratureClosure,inner_stencil) + @test H isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + H = inner_product(g_2D,op.quadratureClosure) + H_x = inner_product(restrict(g_2D,1),op.quadratureClosure) + H_y = inner_product(restrict(g_2D,2),op.quadratureClosure) + @test H == H_x⊗H_y + @test H isa TensorMapping{T,2,2} where T + end + end + + @testset "Sizes" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + H = inner_product(g_1D,op.quadratureClosure) + @test domain_size(H) == size(g_1D) + @test range_size(H) == size(g_1D) + end + @testset "2D" begin + H = inner_product(g_2D,op.quadratureClosure) + @test domain_size(H) == size(g_2D) + @test range_size(H) == size(g_2D) + end + end + + @testset "Accuracy" begin + @testset "1D" begin + v = () + for i = 0:4 + f_i(x) = 1/factorial(i)*x^i + v = (v...,evalOn(g_1D,f_i)) + end + u = evalOn(g_1D,x->sin(x)) + + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_1D,op.quadratureClosure) + for i = 1:2 + @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 + end + @test integral(H,u) ≈ 1. rtol = 1e-4 + end + + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_1D,op.quadratureClosure) + for i = 1:4 + @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 + end + @test integral(H,u) ≈ 1. rtol = 1e-8 + end + end + + @testset "2D" begin + b = 2.1 + v = b*ones(Float64, size(g_2D)) + u = evalOn(g_2D,(x,y)->sin(x)+cos(y)) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_2D,op.quadratureClosure) + @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 + @test integral(H,u) ≈ π rtol = 1e-4 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_2D,op.quadratureClosure) + @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 + @test integral(H,u) ≈ π rtol = 1e-8 + end + end + end +end + +@testset "Diagonal-stencil inverse_inner_product" begin + Lx = π/2. + Ly = Float64(π) + g_1D = EquidistantGrid(77, 0.0, Lx) + g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) + @testset "inverse_inner_product" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "0D" begin + Hi = inverse_inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) + @test Hi == IdentityMapping{Float64}() + @test Hi isa TensorMapping{T,0,0} where T + end + @testset "1D" begin + Hi = inverse_inner_product(g_1D, op.quadratureClosure); + inner_stencil = CenteredStencil(1.) + closures = () + for i = 1:length(op.quadratureClosure) + closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights)) + end + @test Hi == inverse_inner_product(g_1D,closures,inner_stencil) + @test Hi isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + Hi_x = inverse_inner_product(restrict(g_2D,1),op.quadratureClosure) + Hi_y = inverse_inner_product(restrict(g_2D,2),op.quadratureClosure) + @test Hi == Hi_x⊗Hi_y + @test Hi isa TensorMapping{T,2,2} where T + end + end + + @testset "Sizes" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test domain_size(Hi) == size(g_1D) + @test range_size(Hi) == size(g_1D) + end + @testset "2D" begin + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test domain_size(Hi) == size(g_2D) + @test range_size(Hi) == size(g_2D) + end + end + + @testset "Accuracy" begin + @testset "1D" begin + v = evalOn(g_1D,x->sin(x)) + u = evalOn(g_1D,x->x^3-x^2+1) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_1D,op.quadratureClosure) + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_1D,op.quadratureClosure) + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + end + @testset "2D" begin + v = evalOn(g_2D,(x,y)->sin(x)+cos(y)) + u = evalOn(g_2D,(x,y)->x*y + x^5 - sqrt(y)) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_2D,op.quadratureClosure) + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_2D,op.quadratureClosure) + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + end + end +end + +@testset "BoundaryOperator" begin + closure_stencil = Stencil((0,2), (2.,1.,3.)) + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) + + @testset "Constructors" begin + @testset "1D" begin + op_l = BoundaryOperator{Lower}(closure_stencil,size(g_1D)[1]) + @test op_l == BoundaryOperator(g_1D,closure_stencil,Lower()) + @test op_l == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Lower}()) + @test op_l isa TensorMapping{T,0,1} where T + + op_r = BoundaryOperator{Upper}(closure_stencil,size(g_1D)[1]) + @test op_r == BoundaryOperator(g_1D,closure_stencil,Upper()) + @test op_r == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Upper}()) + @test op_r isa TensorMapping{T,0,1} where T + end + + @testset "2D" begin + e_w = boundary_operator(g_2D,closure_stencil,CartesianBoundary{1,Upper}()) + @test e_w isa InflatedTensorMapping + @test e_w isa TensorMapping{T,1,2} where T + end + end + + op_l = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Lower}()) + op_r = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Upper}()) + + op_w = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Lower}()) + op_e = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Upper}()) + op_s = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Lower}()) + op_n = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Upper}()) + + @testset "Sizes" begin + @testset "1D" begin + @test domain_size(op_l) == (11,) + @test domain_size(op_r) == (11,) + + @test range_size(op_l) == () + @test range_size(op_r) == () + end + + @testset "2D" begin + @test domain_size(op_w) == (11,15) + @test domain_size(op_e) == (11,15) + @test domain_size(op_s) == (11,15) + @test domain_size(op_n) == (11,15) + + @test range_size(op_w) == (15,) + @test range_size(op_e) == (15,) + @test range_size(op_s) == (11,) + @test range_size(op_n) == (11,) + end + end + + @testset "Application" begin + @testset "1D" begin + v = evalOn(g_1D,x->1+x^2) + u = fill(3.124) + @test (op_l*v)[] == 2*v[1] + v[2] + 3*v[3] + @test (op_r*v)[] == 2*v[end] + v[end-1] + 3*v[end-2] + @test (op_r*v)[1] == 2*v[end] + v[end-1] + 3*v[end-2] + @test op_l'*u == [2*u[]; u[]; 3*u[]; zeros(8)] + @test op_r'*u == [zeros(8); 3*u[]; u[]; 2*u[]] + end + + @testset "2D" begin + v = rand(size(g_2D)...) + u = fill(3.124) + @test op_w*v ≈ 2*v[1,:] + v[2,:] + 3*v[3,:] rtol = 1e-14 + @test op_e*v ≈ 2*v[end,:] + v[end-1,:] + 3*v[end-2,:] rtol = 1e-14 + @test op_s*v ≈ 2*v[:,1] + v[:,2] + 3*v[:,3] rtol = 1e-14 + @test op_n*v ≈ 2*v[:,end] + v[:,end-1] + 3*v[:,end-2] rtol = 1e-14 + + + g_x = rand(size(g_2D)[1]) + g_y = rand(size(g_2D)[2]) + + G_w = zeros(Float64, size(g_2D)...) + G_w[1,:] = 2*g_y + G_w[2,:] = g_y + G_w[3,:] = 3*g_y + + G_e = zeros(Float64, size(g_2D)...) + G_e[end,:] = 2*g_y + G_e[end-1,:] = g_y + G_e[end-2,:] = 3*g_y + + G_s = zeros(Float64, size(g_2D)...) + G_s[:,1] = 2*g_x + G_s[:,2] = g_x + G_s[:,3] = 3*g_x + + G_n = zeros(Float64, size(g_2D)...) + G_n[:,end] = 2*g_x + G_n[:,end-1] = g_x + G_n[:,end-2] = 3*g_x + + @test op_w'*g_y == G_w + @test op_e'*g_y == G_e + @test op_s'*g_x == G_s + @test op_n'*g_x == G_n + end + + @testset "Regions" begin + u = fill(3.124) + @test (op_l'*u)[Index(1,Lower)] == 2*u[] + @test (op_l'*u)[Index(2,Lower)] == u[] + @test (op_l'*u)[Index(6,Interior)] == 0 + @test (op_l'*u)[Index(10,Upper)] == 0 + @test (op_l'*u)[Index(11,Upper)] == 0 + + @test (op_r'*u)[Index(1,Lower)] == 0 + @test (op_r'*u)[Index(2,Lower)] == 0 + @test (op_r'*u)[Index(6,Interior)] == 0 + @test (op_r'*u)[Index(10,Upper)] == u[] + @test (op_r'*u)[Index(11,Upper)] == 2*u[] + end + end + + @testset "Inferred" begin + v = ones(Float64, 11) + u = fill(1.) + + @inferred apply(op_l, v) + @inferred apply(op_r, v) + + @inferred apply_transpose(op_l, u, 4) + @inferred apply_transpose(op_l, u, Index(1,Lower)) + @inferred apply_transpose(op_l, u, Index(2,Lower)) + @inferred apply_transpose(op_l, u, Index(6,Interior)) + @inferred apply_transpose(op_l, u, Index(10,Upper)) + @inferred apply_transpose(op_l, u, Index(11,Upper)) + + @inferred apply_transpose(op_r, u, 4) + @inferred apply_transpose(op_r, u, Index(1,Lower)) + @inferred apply_transpose(op_r, u, Index(2,Lower)) + @inferred apply_transpose(op_r, u, Index(6,Interior)) + @inferred apply_transpose(op_r, u, Index(10,Upper)) + @inferred apply_transpose(op_r, u, Index(11,Upper)) + end + +end + +@testset "boundary_restriction" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) + + @testset "boundary_restriction" begin + @testset "1D" begin + e_l = boundary_restriction(g_1D,op.eClosure,Lower()) + @test e_l == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Lower}()) + @test e_l == BoundaryOperator(g_1D,op.eClosure,Lower()) + @test e_l isa BoundaryOperator{T,Lower} where T + @test e_l isa TensorMapping{T,0,1} where T + + e_r = boundary_restriction(g_1D,op.eClosure,Upper()) + @test e_r == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Upper}()) + @test e_r == BoundaryOperator(g_1D,op.eClosure,Upper()) + @test e_r isa BoundaryOperator{T,Upper} where T + @test e_r isa TensorMapping{T,0,1} where T + end + + @testset "2D" begin + e_w = boundary_restriction(g_2D,op.eClosure,CartesianBoundary{1,Upper}()) + @test e_w isa InflatedTensorMapping + @test e_w isa TensorMapping{T,1,2} where T + end + end + + @testset "Application" begin + @testset "1D" begin + e_l = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Lower}()) + e_r = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Upper}()) + + v = evalOn(g_1D,x->1+x^2) + u = fill(3.124) + + @test (e_l*v)[] == v[1] + @test (e_r*v)[] == v[end] + @test (e_r*v)[1] == v[end] + end + + @testset "2D" begin + e_w = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Lower}()) + e_e = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Upper}()) + e_s = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Lower}()) + e_n = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Upper}()) + + v = rand(11, 15) + u = fill(3.124) + + @test e_w*v == v[1,:] + @test e_e*v == v[end,:] + @test e_s*v == v[:,1] + @test e_n*v == v[:,end] + end + end +end + +@testset "normal_derivative" begin + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,12), (0.0, 0.0), (1.0,1.0)) + @testset "normal_derivative" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + d_l = normal_derivative(g_1D, op.dClosure, Lower()) + @test d_l == normal_derivative(g_1D, op.dClosure, CartesianBoundary{1,Lower}()) + @test d_l isa BoundaryOperator{T,Lower} where T + @test d_l isa TensorMapping{T,0,1} where T + end + @testset "2D" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + Ix = IdentityMapping{Float64}((size(g_2D)[1],)) + Iy = IdentityMapping{Float64}((size(g_2D)[2],)) + d_l = normal_derivative(restrict(g_2D,1),op.dClosure,Lower()) + d_r = normal_derivative(restrict(g_2D,2),op.dClosure,Upper()) + @test d_w == d_l⊗Iy + @test d_n == Ix⊗d_r + @test d_w isa TensorMapping{T,1,2} where T + @test d_n isa TensorMapping{T,1,2} where T + end + end + @testset "Accuracy" begin + v = evalOn(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y) + v∂x = evalOn(g_2D, (x,y)-> 2*x + y) + v∂y = evalOn(g_2D, (x,y)-> 2*(y-1) + x) + # TODO: Test for higher order polynomials? + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) + d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + + @test d_w*v ≈ v∂x[1,:] atol = 1e-13 + @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 + @test d_s*v ≈ v∂y[:,1] atol = 1e-13 + @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 + end + + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) + d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + + @test d_w*v ≈ v∂x[1,:] atol = 1e-13 + @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 + @test d_s*v ≈ v∂y[:,1] atol = 1e-13 + @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 + end + end +end + +end
--- a/test/SbpOperators/testSbpOperators.jl Sat Feb 20 20:36:27 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,832 +0,0 @@ -using Test -using Sbplib.SbpOperators -using Sbplib.Grids -using Sbplib.RegionIndices -using Sbplib.LazyTensors -using LinearAlgebra -using TOML - -import Sbplib.SbpOperators.Stencil -import Sbplib.SbpOperators.VolumeOperator -import Sbplib.SbpOperators.volume_operator -import Sbplib.SbpOperators.BoundaryOperator -import Sbplib.SbpOperators.boundary_operator -import Sbplib.SbpOperators.even -import Sbplib.SbpOperators.odd - - -@testset "SbpOperators" begin - -@testset "Stencil" begin - s = Stencil((-2,2), (1.,2.,2.,3.,4.)) - @test s isa Stencil{Float64, 5} - - @test eltype(s) == Float64 - @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.)) - - @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4)) - @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4)) - @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4)) - - @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5)) - @test_throws ArgumentError CenteredStencil(1,2,3,4) -end - -@testset "parse_rational" begin - @test SbpOperators.parse_rational("1") isa Rational - @test SbpOperators.parse_rational("1") == 1//1 - @test SbpOperators.parse_rational("1/2") isa Rational - @test SbpOperators.parse_rational("1/2") == 1//2 - @test SbpOperators.parse_rational("37/13") isa Rational - @test SbpOperators.parse_rational("37/13") == 37//13 -end - -@testset "readoperator" begin - toml_str = """ - [meta] - type = "equidistant" - - [order2] - H.inner = ["1"] - - D1.inner_stencil = ["-1/2", "0", "1/2"] - D1.closure_stencils = [ - ["-1", "1"], - ] - - d1.closure = ["-3/2", "2", "-1/2"] - - [order4] - H.closure = ["17/48", "59/48", "43/48", "49/48"] - - D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"] - D2.closure_stencils = [ - [ "2", "-5", "4", "-1", "0", "0"], - [ "1", "-2", "1", "0", "0", "0"], - [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"], - [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"], - ] - """ - - parsed_toml = TOML.parse(toml_str) - @testset "get_stencil" begin - @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil(-1/2, 0., 1/2, center=2) - @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil(-1/2, 0., 1/2; center=1) - @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == Stencil(-1/2, 0., 1/2; center=3) - - @test get_stencil(parsed_toml, "order2", "H", "inner") == Stencil(1.; center=1) - - @test_throws AssertionError get_stencil(parsed_toml, "meta", "type") - @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils") - end - - @testset "get_stencils" begin - @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (Stencil(-1., 1., center=1),) - @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (Stencil(-1., 1., center=2),) - @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (Stencil(-1., 1., center=2),) - - @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == ( - Stencil( 2., -5., 4., -1., 0., 0., center=1), - Stencil( 1., -2., 1., 0., 0., 0., center=1), - Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=1), - Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1), - ) - - @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == ( - Stencil( 2., -5., 4., -1., 0., 0., center=4), - Stencil( 1., -2., 1., 0., 0., 0., center=2), - Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3), - Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1), - ) - - @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == ( - Stencil( 2., -5., 4., -1., 0., 0., center=1), - Stencil( 1., -2., 1., 0., 0., 0., center=2), - Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3), - Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=4), - ) - - @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3)) - @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4)) - @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2)) - end - - @testset "get_tuple" begin - @test get_tuple(parsed_toml, "order2", "d1", "closure") == (-3/2, 2, -1/2) - - @test_throws AssertionError get_tuple(parsed_toml, "meta", "type") - end -end - -@testset "VolumeOperator" begin - inner_stencil = CenteredStencil(1/4, 2/4, 1/4) - closure_stencils = (Stencil(1/2, 1/2; center=1), Stencil(0.,1.; center=2)) - g_1D = EquidistantGrid(11,0.,1.) - g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.)) - g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.)) - @testset "Constructors" begin - @testset "1D" begin - op = VolumeOperator(inner_stencil,closure_stencils,(11,),even) - @test op == VolumeOperator(g_1D,inner_stencil,closure_stencils,even) - @test op == volume_operator(g_1D,inner_stencil,closure_stencils,even,1) - @test op isa TensorMapping{T,1,1} where T - end - @testset "2D" begin - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) - Ix = IdentityMapping{Float64}((11,)) - Iy = IdentityMapping{Float64}((12,)) - @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy - @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even) - @test op_x isa TensorMapping{T,2,2} where T - @test op_y isa TensorMapping{T,2,2} where T - end - @testset "3D" begin - op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) - op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) - Ix = IdentityMapping{Float64}((11,)) - Iy = IdentityMapping{Float64}((12,)) - Iz = IdentityMapping{Float64}((10,)) - @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy⊗Iz - @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even)⊗Iz - @test op_z == Ix⊗Iy⊗VolumeOperator(inner_stencil,closure_stencils,(10,),even) - @test op_x isa TensorMapping{T,3,3} where T - @test op_y isa TensorMapping{T,3,3} where T - @test op_z isa TensorMapping{T,3,3} where T - end - end - - @testset "Sizes" begin - @testset "1D" begin - op = volume_operator(g_1D,inner_stencil,closure_stencils,even,1) - @test range_size(op) == domain_size(op) == size(g_1D) - end - - @testset "2D" begin - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) - @test range_size(op_y) == domain_size(op_y) == - range_size(op_x) == domain_size(op_x) == size(g_2D) - end - @testset "3D" begin - op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) - op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) - @test range_size(op_z) == domain_size(op_z) == - range_size(op_y) == domain_size(op_y) == - range_size(op_x) == domain_size(op_x) == size(g_3D) - end - end - - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,odd,2) - v = zeros(size(g_2D)) - Nx = size(g_2D)[1] - Ny = size(g_2D)[2] - for i = 1:Nx - v[i,:] .= i - end - rx = copy(v) - rx[1,:] .= 1.5 - rx[Nx,:] .= (2*Nx-1)/2 - ry = copy(v) - ry[:,Ny-1:Ny] = -v[:,Ny-1:Ny] - - @testset "Application" begin - @test op_x*v ≈ rx rtol = 1e-14 - @test op_y*v ≈ ry rtol = 1e-14 - end - - @testset "Regions" begin - @test (op_x*v)[Index(1,Lower),Index(3,Interior)] ≈ rx[1,3] rtol = 1e-14 - @test (op_x*v)[Index(2,Lower),Index(3,Interior)] ≈ rx[2,3] rtol = 1e-14 - @test (op_x*v)[Index(6,Interior),Index(3,Interior)] ≈ rx[6,3] rtol = 1e-14 - @test (op_x*v)[Index(10,Upper),Index(3,Interior)] ≈ rx[10,3] rtol = 1e-14 - @test (op_x*v)[Index(11,Upper),Index(3,Interior)] ≈ rx[11,3] rtol = 1e-14 - - @test_throws BoundsError (op_x*v)[Index(3,Lower),Index(3,Interior)] - @test_throws BoundsError (op_x*v)[Index(9,Upper),Index(3,Interior)] - - @test (op_y*v)[Index(3,Interior),Index(1,Lower)] ≈ ry[3,1] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(2,Lower)] ≈ ry[3,2] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(6,Interior)] ≈ ry[3,6] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(11,Upper)] ≈ ry[3,11] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(12,Upper)] ≈ ry[3,12] rtol = 1e-14 - - @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(10,Upper)] - @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(3,Lower)] - end - - @testset "Inferred" begin - @inferred apply(op_x, v,1,1) - @inferred apply(op_x, v, Index(1,Lower),Index(1,Lower)) - @inferred apply(op_x, v, Index(6,Interior),Index(1,Lower)) - @inferred apply(op_x, v, Index(11,Upper),Index(1,Lower)) - - @inferred apply(op_y, v,1,1) - @inferred apply(op_y, v, Index(1,Lower),Index(1,Lower)) - @inferred apply(op_y, v, Index(1,Lower),Index(6,Interior)) - @inferred apply(op_y, v, Index(1,Lower),Index(11,Upper)) - end - -end - -@testset "SecondDerivative" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Lx = 3.5 - Ly = 3. - g_1D = EquidistantGrid(121, 0.0, Lx) - g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) - - @testset "Constructors" begin - @testset "1D" begin - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - @test Dₓₓ == second_derivative(g_1D,op.innerStencil,op.closureStencils,1) - @test Dₓₓ isa VolumeOperator - end - @testset "2D" begin - Dₓₓ = second_derivative(g_2D,op.innerStencil,op.closureStencils,1) - D2 = second_derivative(g_1D,op.innerStencil,op.closureStencils) - I = IdentityMapping{Float64}(size(g_2D)[2]) - @test Dₓₓ == D2⊗I - @test Dₓₓ isa TensorMapping{T,2,2} where T - end - end - - # Exact differentiation is measured point-wise. In other cases - # the error is measured in the l2-norm. - @testset "Accuracy" begin - @testset "1D" begin - l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); - monomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x) = 1/factorial(i)*x^i - monomials = (monomials...,evalOn(g_1D,f_i)) - end - v = evalOn(g_1D,x -> sin(x)) - vₓₓ = evalOn(g_1D,x -> -sin(x)) - - # 2nd order interior stencil, 1nd order boundary stencil, - # implies that L*v should be exact for monomials up to order 2. - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 - @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2 - end - - # 4th order interior stencil, 2nd order boundary stencil, - # implies that L*v should be exact for monomials up to order 3. - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 - @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10 - @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 - end - end - - @testset "2D" begin - l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); - binomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x,y) = 1/factorial(i)*y^i + x^i - binomials = (binomials...,evalOn(g_2D,f_i)) - end - v = evalOn(g_2D, (x,y) -> sin(x)+cos(y)) - v_yy = evalOn(g_2D,(x,y) -> -cos(y)) - - # 2nd order interior stencil, 1st order boundary stencil, - # implies that L*v should be exact for binomials up to order 2. - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) - @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 - @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 - end - - # 4th order interior stencil, 2nd order boundary stencil, - # implies that L*v should be exact for binomials up to order 3. - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 - @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9 - @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 - end - end - end -end - -@testset "Laplace" begin - g_1D = EquidistantGrid(101, 0.0, 1.) - g_3D = EquidistantGrid((51,101,52), (0.0, -1.0, 0.0), (1., 1., 1.)) - @testset "Constructors" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - L = laplace(g_1D, op.innerStencil, op.closureStencils) - @test L == second_derivative(g_1D, op.innerStencil, op.closureStencils) - @test L isa TensorMapping{T,1,1} where T - end - @testset "3D" begin - L = laplace(g_3D, op.innerStencil, op.closureStencils) - @test L isa TensorMapping{T,3,3} where T - Dxx = second_derivative(g_3D, op.innerStencil, op.closureStencils,1) - Dyy = second_derivative(g_3D, op.innerStencil, op.closureStencils,2) - Dzz = second_derivative(g_3D, op.innerStencil, op.closureStencils,3) - @test L == Dxx + Dyy + Dzz - end - end - - # Exact differentiation is measured point-wise. In other cases - # the error is measured in the l2-norm. - @testset "Accuracy" begin - l2(v) = sqrt(prod(spacing(g_3D))*sum(v.^2)); - polynomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x,y,z) = 1/factorial(i)*(y^i + x^i + z^i) - polynomials = (polynomials...,evalOn(g_3D,f_i)) - end - v = evalOn(g_3D, (x,y,z) -> sin(x) + cos(y) + exp(z)) - Δv = evalOn(g_3D,(x,y,z) -> -sin(x) - cos(y) + exp(z)) - - # 2nd order interior stencil, 1st order boundary stencil, - # implies that L*v should be exact for binomials up to order 2. - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - L = laplace(g_3D,op.innerStencil,op.closureStencils) - @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 - @test L*v ≈ Δv rtol = 5e-2 norm = l2 - end - - # 4th order interior stencil, 2nd order boundary stencil, - # implies that L*v should be exact for binomials up to order 3. - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - L = laplace(g_3D,op.innerStencil,op.closureStencils) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 - @test L*polynomials[4] ≈ polynomials[2] atol = 5e-9 - @test L*v ≈ Δv rtol = 5e-4 norm = l2 - end - end -end - -@testset "Diagonal-stencil inner_product" begin - Lx = π/2. - Ly = Float64(π) - Lz = 1. - g_1D = EquidistantGrid(77, 0.0, Lx) - g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) - g_3D = EquidistantGrid((10,10, 10), (0.0, 0.0, 0.0), (Lx,Ly,Lz)) - integral(H,v) = sum(H*v) - @testset "inner_product" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "0D" begin - H = inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) - @test H == IdentityMapping{Float64}() - @test H isa TensorMapping{T,0,0} where T - end - @testset "1D" begin - H = inner_product(g_1D,op.quadratureClosure) - inner_stencil = CenteredStencil(1.) - @test H == inner_product(g_1D,op.quadratureClosure,inner_stencil) - @test H isa TensorMapping{T,1,1} where T - end - @testset "2D" begin - H = inner_product(g_2D,op.quadratureClosure) - H_x = inner_product(restrict(g_2D,1),op.quadratureClosure) - H_y = inner_product(restrict(g_2D,2),op.quadratureClosure) - @test H == H_x⊗H_y - @test H isa TensorMapping{T,2,2} where T - end - end - - @testset "Sizes" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - H = inner_product(g_1D,op.quadratureClosure) - @test domain_size(H) == size(g_1D) - @test range_size(H) == size(g_1D) - end - @testset "2D" begin - H = inner_product(g_2D,op.quadratureClosure) - @test domain_size(H) == size(g_2D) - @test range_size(H) == size(g_2D) - end - end - - @testset "Accuracy" begin - @testset "1D" begin - v = () - for i = 0:4 - f_i(x) = 1/factorial(i)*x^i - v = (v...,evalOn(g_1D,f_i)) - end - u = evalOn(g_1D,x->sin(x)) - - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = inner_product(g_1D,op.quadratureClosure) - for i = 1:2 - @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 - end - @test integral(H,u) ≈ 1. rtol = 1e-4 - end - - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = inner_product(g_1D,op.quadratureClosure) - for i = 1:4 - @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 - end - @test integral(H,u) ≈ 1. rtol = 1e-8 - end - end - - @testset "2D" begin - b = 2.1 - v = b*ones(Float64, size(g_2D)) - u = evalOn(g_2D,(x,y)->sin(x)+cos(y)) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = inner_product(g_2D,op.quadratureClosure) - @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 - @test integral(H,u) ≈ π rtol = 1e-4 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = inner_product(g_2D,op.quadratureClosure) - @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 - @test integral(H,u) ≈ π rtol = 1e-8 - end - end - end -end - -@testset "Diagonal-stencil inverse_inner_product" begin - Lx = π/2. - Ly = Float64(π) - g_1D = EquidistantGrid(77, 0.0, Lx) - g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) - @testset "inverse_inner_product" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "0D" begin - Hi = inverse_inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) - @test Hi == IdentityMapping{Float64}() - @test Hi isa TensorMapping{T,0,0} where T - end - @testset "1D" begin - Hi = inverse_inner_product(g_1D, op.quadratureClosure); - inner_stencil = CenteredStencil(1.) - closures = () - for i = 1:length(op.quadratureClosure) - closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights)) - end - @test Hi == inverse_inner_product(g_1D,closures,inner_stencil) - @test Hi isa TensorMapping{T,1,1} where T - end - @testset "2D" begin - Hi = inverse_inner_product(g_2D,op.quadratureClosure) - Hi_x = inverse_inner_product(restrict(g_2D,1),op.quadratureClosure) - Hi_y = inverse_inner_product(restrict(g_2D,2),op.quadratureClosure) - @test Hi == Hi_x⊗Hi_y - @test Hi isa TensorMapping{T,2,2} where T - end - end - - @testset "Sizes" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - Hi = inverse_inner_product(g_1D,op.quadratureClosure) - @test domain_size(Hi) == size(g_1D) - @test range_size(Hi) == size(g_1D) - end - @testset "2D" begin - Hi = inverse_inner_product(g_2D,op.quadratureClosure) - @test domain_size(Hi) == size(g_2D) - @test range_size(Hi) == size(g_2D) - end - end - - @testset "Accuracy" begin - @testset "1D" begin - v = evalOn(g_1D,x->sin(x)) - u = evalOn(g_1D,x->x^3-x^2+1) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = inner_product(g_1D,op.quadratureClosure) - Hi = inverse_inner_product(g_1D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = inner_product(g_1D,op.quadratureClosure) - Hi = inverse_inner_product(g_1D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - end - @testset "2D" begin - v = evalOn(g_2D,(x,y)->sin(x)+cos(y)) - u = evalOn(g_2D,(x,y)->x*y + x^5 - sqrt(y)) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = inner_product(g_2D,op.quadratureClosure) - Hi = inverse_inner_product(g_2D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = inner_product(g_2D,op.quadratureClosure) - Hi = inverse_inner_product(g_2D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - end - end -end - -@testset "BoundaryOperator" begin - closure_stencil = Stencil((0,2), (2.,1.,3.)) - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) - - @testset "Constructors" begin - @testset "1D" begin - op_l = BoundaryOperator{Lower}(closure_stencil,size(g_1D)[1]) - @test op_l == BoundaryOperator(g_1D,closure_stencil,Lower()) - @test op_l == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Lower}()) - @test op_l isa TensorMapping{T,0,1} where T - - op_r = BoundaryOperator{Upper}(closure_stencil,size(g_1D)[1]) - @test op_r == BoundaryOperator(g_1D,closure_stencil,Upper()) - @test op_r == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Upper}()) - @test op_r isa TensorMapping{T,0,1} where T - end - - @testset "2D" begin - e_w = boundary_operator(g_2D,closure_stencil,CartesianBoundary{1,Upper}()) - @test e_w isa InflatedTensorMapping - @test e_w isa TensorMapping{T,1,2} where T - end - end - - op_l = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Lower}()) - op_r = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Upper}()) - - op_w = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Lower}()) - op_e = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Upper}()) - op_s = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Lower}()) - op_n = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Upper}()) - - @testset "Sizes" begin - @testset "1D" begin - @test domain_size(op_l) == (11,) - @test domain_size(op_r) == (11,) - - @test range_size(op_l) == () - @test range_size(op_r) == () - end - - @testset "2D" begin - @test domain_size(op_w) == (11,15) - @test domain_size(op_e) == (11,15) - @test domain_size(op_s) == (11,15) - @test domain_size(op_n) == (11,15) - - @test range_size(op_w) == (15,) - @test range_size(op_e) == (15,) - @test range_size(op_s) == (11,) - @test range_size(op_n) == (11,) - end - end - - @testset "Application" begin - @testset "1D" begin - v = evalOn(g_1D,x->1+x^2) - u = fill(3.124) - @test (op_l*v)[] == 2*v[1] + v[2] + 3*v[3] - @test (op_r*v)[] == 2*v[end] + v[end-1] + 3*v[end-2] - @test (op_r*v)[1] == 2*v[end] + v[end-1] + 3*v[end-2] - @test op_l'*u == [2*u[]; u[]; 3*u[]; zeros(8)] - @test op_r'*u == [zeros(8); 3*u[]; u[]; 2*u[]] - end - - @testset "2D" begin - v = rand(size(g_2D)...) - u = fill(3.124) - @test op_w*v ≈ 2*v[1,:] + v[2,:] + 3*v[3,:] rtol = 1e-14 - @test op_e*v ≈ 2*v[end,:] + v[end-1,:] + 3*v[end-2,:] rtol = 1e-14 - @test op_s*v ≈ 2*v[:,1] + v[:,2] + 3*v[:,3] rtol = 1e-14 - @test op_n*v ≈ 2*v[:,end] + v[:,end-1] + 3*v[:,end-2] rtol = 1e-14 - - - g_x = rand(size(g_2D)[1]) - g_y = rand(size(g_2D)[2]) - - G_w = zeros(Float64, size(g_2D)...) - G_w[1,:] = 2*g_y - G_w[2,:] = g_y - G_w[3,:] = 3*g_y - - G_e = zeros(Float64, size(g_2D)...) - G_e[end,:] = 2*g_y - G_e[end-1,:] = g_y - G_e[end-2,:] = 3*g_y - - G_s = zeros(Float64, size(g_2D)...) - G_s[:,1] = 2*g_x - G_s[:,2] = g_x - G_s[:,3] = 3*g_x - - G_n = zeros(Float64, size(g_2D)...) - G_n[:,end] = 2*g_x - G_n[:,end-1] = g_x - G_n[:,end-2] = 3*g_x - - @test op_w'*g_y == G_w - @test op_e'*g_y == G_e - @test op_s'*g_x == G_s - @test op_n'*g_x == G_n - end - - @testset "Regions" begin - u = fill(3.124) - @test (op_l'*u)[Index(1,Lower)] == 2*u[] - @test (op_l'*u)[Index(2,Lower)] == u[] - @test (op_l'*u)[Index(6,Interior)] == 0 - @test (op_l'*u)[Index(10,Upper)] == 0 - @test (op_l'*u)[Index(11,Upper)] == 0 - - @test (op_r'*u)[Index(1,Lower)] == 0 - @test (op_r'*u)[Index(2,Lower)] == 0 - @test (op_r'*u)[Index(6,Interior)] == 0 - @test (op_r'*u)[Index(10,Upper)] == u[] - @test (op_r'*u)[Index(11,Upper)] == 2*u[] - end - end - - @testset "Inferred" begin - v = ones(Float64, 11) - u = fill(1.) - - @inferred apply(op_l, v) - @inferred apply(op_r, v) - - @inferred apply_transpose(op_l, u, 4) - @inferred apply_transpose(op_l, u, Index(1,Lower)) - @inferred apply_transpose(op_l, u, Index(2,Lower)) - @inferred apply_transpose(op_l, u, Index(6,Interior)) - @inferred apply_transpose(op_l, u, Index(10,Upper)) - @inferred apply_transpose(op_l, u, Index(11,Upper)) - - @inferred apply_transpose(op_r, u, 4) - @inferred apply_transpose(op_r, u, Index(1,Lower)) - @inferred apply_transpose(op_r, u, Index(2,Lower)) - @inferred apply_transpose(op_r, u, Index(6,Interior)) - @inferred apply_transpose(op_r, u, Index(10,Upper)) - @inferred apply_transpose(op_r, u, Index(11,Upper)) - end - -end - -@testset "boundary_restriction" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) - - @testset "boundary_restriction" begin - @testset "1D" begin - e_l = boundary_restriction(g_1D,op.eClosure,Lower()) - @test e_l == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Lower}()) - @test e_l == BoundaryOperator(g_1D,op.eClosure,Lower()) - @test e_l isa BoundaryOperator{T,Lower} where T - @test e_l isa TensorMapping{T,0,1} where T - - e_r = boundary_restriction(g_1D,op.eClosure,Upper()) - @test e_r == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Upper}()) - @test e_r == BoundaryOperator(g_1D,op.eClosure,Upper()) - @test e_r isa BoundaryOperator{T,Upper} where T - @test e_r isa TensorMapping{T,0,1} where T - end - - @testset "2D" begin - e_w = boundary_restriction(g_2D,op.eClosure,CartesianBoundary{1,Upper}()) - @test e_w isa InflatedTensorMapping - @test e_w isa TensorMapping{T,1,2} where T - end - end - - @testset "Application" begin - @testset "1D" begin - e_l = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Lower}()) - e_r = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Upper}()) - - v = evalOn(g_1D,x->1+x^2) - u = fill(3.124) - - @test (e_l*v)[] == v[1] - @test (e_r*v)[] == v[end] - @test (e_r*v)[1] == v[end] - end - - @testset "2D" begin - e_w = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Lower}()) - e_e = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Upper}()) - e_s = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Lower}()) - e_n = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Upper}()) - - v = rand(11, 15) - u = fill(3.124) - - @test e_w*v == v[1,:] - @test e_e*v == v[end,:] - @test e_s*v == v[:,1] - @test e_n*v == v[:,end] - end - end -end - -@testset "normal_derivative" begin - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,12), (0.0, 0.0), (1.0,1.0)) - @testset "normal_derivative" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - d_l = normal_derivative(g_1D, op.dClosure, Lower()) - @test d_l == normal_derivative(g_1D, op.dClosure, CartesianBoundary{1,Lower}()) - @test d_l isa BoundaryOperator{T,Lower} where T - @test d_l isa TensorMapping{T,0,1} where T - end - @testset "2D" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - Ix = IdentityMapping{Float64}((size(g_2D)[1],)) - Iy = IdentityMapping{Float64}((size(g_2D)[2],)) - d_l = normal_derivative(restrict(g_2D,1),op.dClosure,Lower()) - d_r = normal_derivative(restrict(g_2D,2),op.dClosure,Upper()) - @test d_w == d_l⊗Iy - @test d_n == Ix⊗d_r - @test d_w isa TensorMapping{T,1,2} where T - @test d_n isa TensorMapping{T,1,2} where T - end - end - @testset "Accuracy" begin - v = evalOn(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y) - v∂x = evalOn(g_2D, (x,y)-> 2*x + y) - v∂y = evalOn(g_2D, (x,y)-> 2*(y-1) + x) - # TODO: Test for higher order polynomials? - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) - d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) - d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - - @test d_w*v ≈ v∂x[1,:] atol = 1e-13 - @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 - @test d_s*v ≈ v∂y[:,1] atol = 1e-13 - @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 - end - - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) - d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) - d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - - @test d_w*v ≈ v∂x[1,:] atol = 1e-13 - @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 - @test d_s*v ≈ v∂y[:,1] atol = 1e-13 - @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 - end - end -end - -end
--- a/test/runtests.jl Sat Feb 20 20:36:27 2021 +0100 +++ b/test/runtests.jl Sat Feb 20 20:45:40 2021 +0100 @@ -6,7 +6,7 @@ run_testfiles(path) run_testfiles(path, glob) -Find and run all files with filenames starting with "test". If `path` is omitted the test folder is assumed. +Find and run all files with filenames ending with "_test.jl". If `path` is omitted the test folder is assumed. The argument `glob` can optionally be supplied to filter which test files are run. """ function run_testfiles(args) @@ -19,7 +19,6 @@ run_testfiles(".", glob) end -# TODO change from prefix `test` to suffix `_test` for testfiles function run_testfiles(path, glob) for name ∈ readdir(path) filepath = joinpath(path, name) @@ -30,11 +29,7 @@ end end - if !endswith(name, ".jl") ## TODO combine this into test below when switching to suffix - continue - end - - if startswith(name, "test") && occursin(glob, filepath) + if endswith(name, "_test.jl") && occursin(glob, filepath) printstyled("Running "; bold=true, color=:green) println(filepath) include(filepath)