comparison test/SbpOperators/volumeops/derivatives/second_derivative_test.jl @ 1395:bdcdbd4ea9cd feature/boundary_conditions

Merge with default. Comment out broken tests for boundary_conditions at sat
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 26 Jul 2023 21:35:50 +0200
parents 356ec6a72974
children 43aaf710463e
comparison
equal deleted inserted replaced
1217:ea2e8254820a 1395:bdcdbd4ea9cd
4 using Sbplib.Grids 4 using Sbplib.Grids
5 using Sbplib.LazyTensors 5 using Sbplib.LazyTensors
6 6
7 import Sbplib.SbpOperators.VolumeOperator 7 import Sbplib.SbpOperators.VolumeOperator
8 8
9 # TODO: Refactor these test to look more like the tests in first_derivative_test.jl.
10
9 @testset "SecondDerivative" begin 11 @testset "SecondDerivative" begin
10 operator_path = sbp_operators_path()*"standard_diagonal.toml" 12 operator_path = sbp_operators_path()*"standard_diagonal.toml"
11 stencil_set = read_stencil_set(operator_path; order=4) 13 stencil_set = read_stencil_set(operator_path; order=4)
12 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 14 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
13 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"]) 15 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
14 Lx = 3.5 16 Lx = 3.5
15 Ly = 3. 17 Ly = 3.
16 g_1D = EquidistantGrid(121, 0.0, Lx) 18 g_1D = equidistant_grid(121, 0.0, Lx)
17 g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) 19 g_2D = equidistant_grid((121,123), (0.0, 0.0), (Lx, Ly))
18 20
19 @testset "Constructors" begin 21 @testset "Constructors" begin
20 @testset "1D" begin 22 @testset "1D" begin
21 Dₓₓ = second_derivative(g_1D,inner_stencil,closure_stencils,1) 23 Dₓₓ = second_derivative(g_1D, stencil_set)
22 @test Dₓₓ == second_derivative(g_1D,inner_stencil,closure_stencils) 24 @test Dₓₓ == second_derivative(g_1D, inner_stencil, closure_stencils)
23 @test Dₓₓ == second_derivative(g_1D,stencil_set,1) 25 @test Dₓₓ isa LazyTensor{Float64,1,1}
24 @test Dₓₓ == second_derivative(g_1D,stencil_set)
25 @test Dₓₓ isa VolumeOperator
26 end 26 end
27 @testset "2D" begin 27 @testset "2D" begin
28 Dₓₓ = second_derivative(g_2D,inner_stencil,closure_stencils,1) 28 Dₓₓ = second_derivative(g_2D,stencil_set,1)
29 D2 = second_derivative(g_1D,inner_stencil,closure_stencils,1) 29 @test Dₓₓ isa LazyTensor{Float64,2,2}
30 I = IdentityTensor{Float64}(size(g_2D)[2])
31 @test Dₓₓ == D2⊗I
32 @test Dₓₓ == second_derivative(g_2D,stencil_set,1)
33 @test Dₓₓ isa LazyTensor{T,2,2} where T
34 end 30 end
35 end 31 end
36 32
37 # Exact differentiation is measured point-wise. In other cases 33 # Exact differentiation is measured point-wise. In other cases
38 # the error is measured in the l2-norm. 34 # the error is measured in the l2-norm.
41 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); 37 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2));
42 monomials = () 38 monomials = ()
43 maxOrder = 4; 39 maxOrder = 4;
44 for i = 0:maxOrder-1 40 for i = 0:maxOrder-1
45 f_i(x) = 1/factorial(i)*x^i 41 f_i(x) = 1/factorial(i)*x^i
46 monomials = (monomials...,evalOn(g_1D,f_i)) 42 monomials = (monomials...,eval_on(g_1D,f_i))
47 end 43 end
48 v = evalOn(g_1D,x -> sin(x)) 44 v = eval_on(g_1D,x -> sin(x))
49 vₓₓ = evalOn(g_1D,x -> -sin(x)) 45 vₓₓ = eval_on(g_1D,x -> -sin(x))
50 46
51 # 2nd order interior stencil, 1nd order boundary stencil, 47 # 2nd order interior stencil, 1nd order boundary stencil,
52 # implies that L*v should be exact for monomials up to order 2. 48 # implies that L*v should be exact for monomials up to order 2.
53 @testset "2nd order" begin 49 @testset "2nd order" begin
54 stencil_set = read_stencil_set(operator_path; order=2) 50 stencil_set = read_stencil_set(operator_path; order=2)
73 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 69 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2
74 end 70 end
75 end 71 end
76 72
77 @testset "2D" begin 73 @testset "2D" begin
78 l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); 74 l2(v) = sqrt(prod(spacing.(g_2D.grids))*sum(v.^2));
79 binomials = () 75 binomials = ()
80 maxOrder = 4; 76 maxOrder = 4;
81 for i = 0:maxOrder-1 77 for i = 0:maxOrder-1
82 f_i(x,y) = 1/factorial(i)*y^i + x^i 78 f_i(x,y) = 1/factorial(i)*y^i + x^i
83 binomials = (binomials...,evalOn(g_2D,f_i)) 79 binomials = (binomials...,eval_on(g_2D,f_i))
84 end 80 end
85 v = evalOn(g_2D, (x,y) -> sin(x)+cos(y)) 81 v = eval_on(g_2D, (x,y) -> sin(x)+cos(y))
86 v_yy = evalOn(g_2D,(x,y) -> -cos(y)) 82 v_yy = eval_on(g_2D,(x,y) -> -cos(y))
87 83
88 # 2nd order interior stencil, 1st order boundary stencil, 84 # 2nd order interior stencil, 1st order boundary stencil,
89 # implies that L*v should be exact for binomials up to order 2. 85 # implies that L*v should be exact for binomials up to order 2.
90 @testset "2nd order" begin 86 @testset "2nd order" begin
91 stencil_set = read_stencil_set(operator_path; order=2) 87 stencil_set = read_stencil_set(operator_path; order=2)
92 Dyy = second_derivative(g_2D,stencil_set,2) 88 Dyy = second_derivative(g_2D,stencil_set,2)
93 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 89 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
94 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 90 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
95 @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 91 @test Dyy*binomials[3] ≈ eval_on(g_2D,(x,y)->1.) atol = 5e-9
96 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 92 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2
97 end 93 end
98 94
99 # 4th order interior stencil, 2nd order boundary stencil, 95 # 4th order interior stencil, 2nd order boundary stencil,
100 # implies that L*v should be exact for binomials up to order 3. 96 # implies that L*v should be exact for binomials up to order 3.
103 Dyy = second_derivative(g_2D,stencil_set,2) 99 Dyy = second_derivative(g_2D,stencil_set,2)
104 # NOTE: high tolerances for checking the "exact" differentiation 100 # NOTE: high tolerances for checking the "exact" differentiation
105 # due to accumulation of round-off errors/cancellation errors? 101 # due to accumulation of round-off errors/cancellation errors?
106 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 102 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
107 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 103 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
108 @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 104 @test Dyy*binomials[3] ≈ eval_on(g_2D,(x,y)->1.) atol = 5e-9
109 @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9 105 @test Dyy*binomials[4] ≈ eval_on(g_2D,(x,y)->y) atol = 5e-9
110 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 106 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2
111 end 107 end
112 end 108 end
113 end 109 end
114 end 110 end