comparison test/SbpOperators/volumeops/derivatives/second_derivative_test.jl @ 875:067a322e4f73 laplace_benchmarks

Merge with feature/laplace_opset
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 27 Jan 2022 10:55:08 +0100
parents d2f4ac2be47f
children 2ae62dbaf839
comparison
equal deleted inserted replaced
874:7e9ebd572deb 875:067a322e4f73
1 using Test
2
3 using Sbplib.SbpOperators
4 using Sbplib.Grids
5 using Sbplib.LazyTensors
6
7 import Sbplib.SbpOperators.VolumeOperator
8
9 @testset "SecondDerivative" begin
10 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4)
11 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
12 closure_stencils = parse_stencil.(stencil_set["D2"]["closure_stencils"])
13 Lx = 3.5
14 Ly = 3.
15 g_1D = EquidistantGrid(121, 0.0, Lx)
16 g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly))
17
18 @testset "Constructors" begin
19 @testset "1D" begin
20 Dₓₓ = second_derivative(g_1D,inner_stencil,closure_stencils)
21 @test Dₓₓ == second_derivative(g_1D,inner_stencil,closure_stencils,1)
22 @test Dₓₓ isa VolumeOperator
23 end
24 @testset "2D" begin
25 Dₓₓ = second_derivative(g_2D,inner_stencil,closure_stencils,1)
26 D2 = second_derivative(g_1D,inner_stencil,closure_stencils)
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
31 end
32
33 # Exact differentiation is measured point-wise. In other cases
34 # the error is measured in the l2-norm.
35 @testset "Accuracy" begin
36 @testset "1D" begin
37 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2));
38 monomials = ()
39 maxOrder = 4;
40 for i = 0:maxOrder-1
41 f_i(x) = 1/factorial(i)*x^i
42 monomials = (monomials...,evalOn(g_1D,f_i))
43 end
44 v = evalOn(g_1D,x -> sin(x))
45 vₓₓ = evalOn(g_1D,x -> -sin(x))
46
47 # 2nd order interior stencil, 1nd order boundary stencil,
48 # implies that L*v should be exact for monomials up to order 2.
49 @testset "2nd order" begin
50 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=2)
51 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
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
55 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
56 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
57 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2
58 end
59
60 # 4th order interior stencil, 2nd order boundary stencil,
61 # implies that L*v should be exact for monomials up to order 3.
62 @testset "4th order" begin
63 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4)
64 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
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
68 # due to accumulation of round-off errors/cancellation errors?
69 @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
71 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
72 @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10
73 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2
74 end
75 end
76
77 @testset "2D" begin
78 l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2));
79 binomials = ()
80 maxOrder = 4;
81 for i = 0:maxOrder-1
82 f_i(x,y) = 1/factorial(i)*y^i + x^i
83 binomials = (binomials...,evalOn(g_2D,f_i))
84 end
85 v = evalOn(g_2D, (x,y) -> sin(x)+cos(y))
86 v_yy = evalOn(g_2D,(x,y) -> -cos(y))
87
88 # 2nd order interior stencil, 1st order boundary stencil,
89 # implies that L*v should be exact for binomials up to order 2.
90 @testset "2nd order" begin
91 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=2)
92 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
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
96 @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
98 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2
99 end
100
101 # 4th order interior stencil, 2nd order boundary stencil,
102 # implies that L*v should be exact for binomials up to order 3.
103 @testset "4th order" begin
104 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=4)
105 inner_stencil = parse_stencil(stencil_set["D2"]["inner_stencil"])
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
109 # due to accumulation of round-off errors/cancellation errors?
110 @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
112 @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9
113 @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9
114 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2
115 end
116 end
117 end
118 end