comparison test/SbpOperators/volumeops/derivatives/second_derivative_test.jl @ 1858:4a9be96f2569 feature/documenter_logo

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 12 Jan 2025 21:18:44 +0100
parents 471a948cd2b2
children
comparison
equal deleted inserted replaced
1857:ffde7dad9da5 1858:4a9be96f2569
1 using Test 1 using Test
2 2
3 using Sbplib.SbpOperators 3 using Diffinitive.SbpOperators
4 using Sbplib.Grids 4 using Diffinitive.Grids
5 using Sbplib.LazyTensors 5 using Diffinitive.LazyTensors
6 6
7 import Sbplib.SbpOperators.VolumeOperator 7 import Diffinitive.SbpOperators.VolumeOperator
8
9 # TODO: Refactor these test to look more like the tests in first_derivative_test.jl.
8 10
9 @testset "SecondDerivative" begin 11 @testset "SecondDerivative" begin
10 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4) 12 operator_path = sbp_operators_path()*"standard_diagonal.toml"
13 stencil_set = read_stencil_set(operator_path; order=4)
11 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 14 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
12 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"]) 15 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
13 Lx = 3.5 16 Lx = 3.5
14 Ly = 3. 17 Ly = 3.
15 g_1D = EquidistantGrid(121, 0.0, Lx) 18 g_1D = equidistant_grid(0.0, Lx, 121)
16 g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) 19 g_2D = equidistant_grid((0.0, 0.0), (Lx, Ly), 121, 123)
17 20
18 @testset "Constructors" begin 21 @testset "Constructors" begin
19 @testset "1D" begin 22 @testset "1D" begin
20 Dₓₓ = second_derivative(g_1D,inner_stencil,closure_stencils) 23 Dₓₓ = second_derivative(g_1D, stencil_set)
21 @test Dₓₓ == second_derivative(g_1D,inner_stencil,closure_stencils,1) 24 @test Dₓₓ == second_derivative(g_1D, inner_stencil, closure_stencils)
22 @test Dₓₓ isa VolumeOperator 25 @test Dₓₓ isa LazyTensor{Float64,1,1}
23 end 26 end
24 @testset "2D" begin 27 @testset "2D" begin
25 Dₓₓ = second_derivative(g_2D,inner_stencil,closure_stencils,1) 28 Dₓₓ = second_derivative(g_2D,stencil_set,1)
26 D2 = second_derivative(g_1D,inner_stencil,closure_stencils) 29 @test Dₓₓ isa LazyTensor{Float64,2,2}
27 I = IdentityMapping{Float64}(size(g_2D)[2])
28 @test Dₓₓ == D2⊗I
29 @test Dₓₓ isa TensorMapping{T,2,2} where T
30 end 30 end
31 end 31 end
32 32
33 # Exact differentiation is measured point-wise. In other cases 33 # Exact differentiation is measured point-wise. In other cases
34 # the error is measured in the l2-norm. 34 # the error is measured in the l2-norm.
37 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); 37 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2));
38 monomials = () 38 monomials = ()
39 maxOrder = 4; 39 maxOrder = 4;
40 for i = 0:maxOrder-1 40 for i = 0:maxOrder-1
41 f_i(x) = 1/factorial(i)*x^i 41 f_i(x) = 1/factorial(i)*x^i
42 monomials = (monomials...,evalOn(g_1D,f_i)) 42 monomials = (monomials...,eval_on(g_1D,f_i))
43 end 43 end
44 v = evalOn(g_1D,x -> sin(x)) 44 v = eval_on(g_1D,x -> sin(x))
45 vₓₓ = evalOn(g_1D,x -> -sin(x)) 45 vₓₓ = eval_on(g_1D,x -> -sin(x))
46 46
47 # 2nd order interior stencil, 1nd order boundary stencil, 47 # 2nd order interior stencil, 1nd order boundary stencil,
48 # 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.
49 @testset "2nd order" begin 49 @testset "2nd order" begin
50 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=2) 50 stencil_set = read_stencil_set(operator_path; order=2)
51 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 51 Dₓₓ = second_derivative(g_1D,stencil_set)
52 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
53 Dₓₓ = second_derivative(g_1D,inner_stencil,closure_stencils)
54 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 52 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
55 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 53 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
56 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 54 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
57 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2 55 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2
58 end 56 end
59 57
60 # 4th order interior stencil, 2nd order boundary stencil, 58 # 4th order interior stencil, 2nd order boundary stencil,
61 # implies that L*v should be exact for monomials up to order 3. 59 # implies that L*v should be exact for monomials up to order 3.
62 @testset "4th order" begin 60 @testset "4th order" begin
63 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4) 61 stencil_set = read_stencil_set(operator_path; order=4)
64 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 62 Dₓₓ = second_derivative(g_1D,stencil_set)
65 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
66 Dₓₓ = second_derivative(g_1D,inner_stencil,closure_stencils)
67 # NOTE: high tolerances for checking the "exact" differentiation 63 # NOTE: high tolerances for checking the "exact" differentiation
68 # due to accumulation of round-off errors/cancellation errors? 64 # due to accumulation of round-off errors/cancellation errors?
69 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 65 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
70 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 66 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
71 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 67 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
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(sbp_operators_path()*"standard_diagonal.toml"; order=2) 87 stencil_set = read_stencil_set(operator_path; order=2)
92 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 88 Dyy = second_derivative(g_2D,stencil_set,2)
93 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
94 Dyy = second_derivative(g_2D,inner_stencil,closure_stencils,2)
95 @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
96 @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
97 @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
98 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 92 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2
99 end 93 end
100 94
101 # 4th order interior stencil, 2nd order boundary stencil, 95 # 4th order interior stencil, 2nd order boundary stencil,
102 # 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 @testset "4th order" begin 97 @testset "4th order" begin
104 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4) 98 stencil_set = read_stencil_set(operator_path; order=4)
105 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"]) 99 Dyy = second_derivative(g_2D,stencil_set,2)
106 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
107 Dyy = second_derivative(g_2D,inner_stencil,closure_stencils,2)
108 # NOTE: high tolerances for checking the "exact" differentiation 100 # NOTE: high tolerances for checking the "exact" differentiation
109 # due to accumulation of round-off errors/cancellation errors? 101 # due to accumulation of round-off errors/cancellation errors?
110 @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
111 @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
112 @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
113 @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
114 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 106 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2
115 end 107 end
116 end 108 end
117 end 109 end
118 end 110 end