comparison test/testSbpOperators.jl @ 671:e14627e79a54 feature/stencil_convenience

Add stencil constructor for centered stencils and change from tuple to vararg in stencil constructor taking cneter
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 06 Feb 2021 21:42:57 +0100
parents 351937390162
children 1ce3a104afc8 1accc3e051d0 fdd48f6ace1c
comparison
equal deleted inserted replaced
664:7d7c1d636de3 671:e14627e79a54
22 @test s isa Stencil{Float64, 5} 22 @test s isa Stencil{Float64, 5}
23 23
24 @test eltype(s) == Float64 24 @test eltype(s) == Float64
25 @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.)) 25 @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.))
26 26
27 @test Stencil((1,2,3,4), center=1) == Stencil((0, 3),(1,2,3,4)) 27 @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4))
28 @test Stencil((1,2,3,4), center=2) == Stencil((-1, 2),(1,2,3,4)) 28 @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4))
29 @test Stencil((1,2,3,4), center=4) == Stencil((-3, 0),(1,2,3,4)) 29 @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4))
30
31 @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5))
32 @test_throws ArgumentError CenteredStencil(1,2,3,4)
30 end 33 end
31 34
32 @testset "parse_rational" begin 35 @testset "parse_rational" begin
33 @test SbpOperators.parse_rational("1") isa Rational 36 @test SbpOperators.parse_rational("1") isa Rational
34 @test SbpOperators.parse_rational("1") == 1//1 37 @test SbpOperators.parse_rational("1") == 1//1
65 ] 68 ]
66 """ 69 """
67 70
68 parsed_toml = TOML.parse(toml_str) 71 parsed_toml = TOML.parse(toml_str)
69 @testset "get_stencil" begin 72 @testset "get_stencil" begin
70 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil((-1/2, 0., 1/2), center=2) 73 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil(-1/2, 0., 1/2, center=2)
71 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil((-1/2, 0., 1/2); center=1) 74 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil(-1/2, 0., 1/2; center=1)
72 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == Stencil((-1/2, 0., 1/2); center=3) 75 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == Stencil(-1/2, 0., 1/2; center=3)
73 76
74 @test get_stencil(parsed_toml, "order2", "H", "inner") == Stencil((1.,), center=1) 77 @test get_stencil(parsed_toml, "order2", "H", "inner") == Stencil(1.; center=1)
75 78
76 @test_throws AssertionError get_stencil(parsed_toml, "meta", "type") 79 @test_throws AssertionError get_stencil(parsed_toml, "meta", "type")
77 @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils") 80 @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils")
78 end 81 end
79 82
80 @testset "get_stencils" begin 83 @testset "get_stencils" begin
81 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (Stencil((-1., 1.), center=1),) 84 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (Stencil(-1., 1., center=1),)
82 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (Stencil((-1., 1.), center=2),) 85 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (Stencil(-1., 1., center=2),)
83 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (Stencil((-1., 1.), center=2),) 86 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (Stencil(-1., 1., center=2),)
84 87
85 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == ( 88 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == (
86 Stencil(( 2., -5., 4., -1., 0., 0.), center=1), 89 Stencil( 2., -5., 4., -1., 0., 0., center=1),
87 Stencil(( 1., -2., 1., 0., 0., 0.), center=1), 90 Stencil( 1., -2., 1., 0., 0., 0., center=1),
88 Stencil(( -4/43, 59/43, -110/43, 59/43, -4/43, 0.), center=1), 91 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=1),
89 Stencil(( -1/49, 0., 59/49, -118/49, 64/49, -4/49), center=1), 92 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1),
90 ) 93 )
91 94
92 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == ( 95 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == (
93 Stencil(( 2., -5., 4., -1., 0., 0.), center=4), 96 Stencil( 2., -5., 4., -1., 0., 0., center=4),
94 Stencil(( 1., -2., 1., 0., 0., 0.), center=2), 97 Stencil( 1., -2., 1., 0., 0., 0., center=2),
95 Stencil(( -4/43, 59/43, -110/43, 59/43, -4/43, 0.), center=3), 98 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3),
96 Stencil(( -1/49, 0., 59/49, -118/49, 64/49, -4/49), center=1), 99 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1),
97 ) 100 )
98 101
99 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == ( 102 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == (
100 Stencil(( 2., -5., 4., -1., 0., 0.), center=1), 103 Stencil( 2., -5., 4., -1., 0., 0., center=1),
101 Stencil(( 1., -2., 1., 0., 0., 0.), center=2), 104 Stencil( 1., -2., 1., 0., 0., 0., center=2),
102 Stencil(( -4/43, 59/43, -110/43, 59/43, -4/43, 0.), center=3), 105 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3),
103 Stencil(( -1/49, 0., 59/49, -118/49, 64/49, -4/49), center=4), 106 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=4),
104 ) 107 )
105 108
106 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3)) 109 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3))
107 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4)) 110 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4))
108 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2)) 111 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2))
114 @test_throws AssertionError get_tuple(parsed_toml, "meta", "type") 117 @test_throws AssertionError get_tuple(parsed_toml, "meta", "type")
115 end 118 end
116 end 119 end
117 120
118 @testset "VolumeOperator" begin 121 @testset "VolumeOperator" begin
119 inner_stencil = Stencil(1/4 .* (1.,2.,1.),center=2) 122 inner_stencil = CenteredStencil(1/4, 2/4, 1/4)
120 closure_stencils = (Stencil(1/2 .* (1.,1.),center=1),Stencil((0.,1.),center=2)) 123 closure_stencils = (Stencil(1/2, 1/2; center=1), Stencil(0.,1.; center=2))
121 g_1D = EquidistantGrid(11,0.,1.) 124 g_1D = EquidistantGrid(11,0.,1.)
122 g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.)) 125 g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.))
123 g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.)) 126 g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.))
124 @testset "Constructors" begin 127 @testset "Constructors" begin
125 @testset "1D" begin 128 @testset "1D" begin
288 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 291 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
289 @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10 292 @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10
290 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 293 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2
291 end 294 end
292 end 295 end
293 296
294 @testset "2D" begin 297 @testset "2D" begin
295 l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); 298 l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2));
296 binomials = () 299 binomials = ()
297 maxOrder = 4; 300 maxOrder = 4;
298 for i = 0:maxOrder-1 301 for i = 0:maxOrder-1
398 integral(H,v) = sum(H*v) 401 integral(H,v) = sum(H*v)
399 @testset "Constructors" begin 402 @testset "Constructors" begin
400 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) 403 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
401 @testset "1D" begin 404 @testset "1D" begin
402 H = DiagonalQuadrature(g_1D,op.quadratureClosure) 405 H = DiagonalQuadrature(g_1D,op.quadratureClosure)
403 inner_stencil = Stencil((1.,),center=1) 406 inner_stencil = CenteredStencil(1.)
404 @test H == Quadrature(g_1D,inner_stencil,op.quadratureClosure) 407 @test H == Quadrature(g_1D,inner_stencil,op.quadratureClosure)
405 @test H isa TensorMapping{T,1,1} where T 408 @test H isa TensorMapping{T,1,1} where T
406 end 409 end
407 @testset "1D" begin 410 @testset "1D" begin
408 H = DiagonalQuadrature(g_2D,op.quadratureClosure) 411 H = DiagonalQuadrature(g_2D,op.quadratureClosure)
482 g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) 485 g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly))
483 @testset "Constructors" begin 486 @testset "Constructors" begin
484 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) 487 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
485 @testset "1D" begin 488 @testset "1D" begin
486 Hi = InverseDiagonalQuadrature(g_1D, op.quadratureClosure); 489 Hi = InverseDiagonalQuadrature(g_1D, op.quadratureClosure);
487 inner_stencil = Stencil((1.,),center=1) 490 inner_stencil = CenteredStencil(1.)
488 closures = () 491 closures = ()
489 for i = 1:length(op.quadratureClosure) 492 for i = 1:length(op.quadratureClosure)
490 closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights)) 493 closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights))
491 end 494 end
492 @test Hi == InverseQuadrature(g_1D,inner_stencil,closures) 495 @test Hi == InverseQuadrature(g_1D,inner_stencil,closures)