comparison test/SbpOperators/volumeops/derivatives/secondderivative_test.jl @ 728:45966c77cb20 feature/selectable_tests

Split tests for SbpOperators over several files
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 17 Mar 2021 20:34:40 +0100
parents
children 6114274447f5
comparison
equal deleted inserted replaced
727:95b207729b7a 728:45966c77cb20
1 using Test
2
3 using Sbplib.SbpOperators
4 using Sbplib.Grids
5
6 import Sbplib.SbpOperators.VolumeOperator
7
8 @testset "SecondDerivative" begin
9 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
10 Lx = 3.5
11 Ly = 3.
12 g_1D = EquidistantGrid(121, 0.0, Lx)
13 g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly))
14
15 @testset "Constructors" begin
16 @testset "1D" begin
17 Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils)
18 @test Dₓₓ == second_derivative(g_1D,op.innerStencil,op.closureStencils,1)
19 @test Dₓₓ isa VolumeOperator
20 end
21 @testset "2D" begin
22 Dₓₓ = second_derivative(g_2D,op.innerStencil,op.closureStencils,1)
23 D2 = second_derivative(g_1D,op.innerStencil,op.closureStencils)
24 I = IdentityMapping{Float64}(size(g_2D)[2])
25 @test Dₓₓ == D2⊗I
26 @test Dₓₓ isa TensorMapping{T,2,2} where T
27 end
28 end
29
30 # Exact differentiation is measured point-wise. In other cases
31 # the error is measured in the l2-norm.
32 @testset "Accuracy" begin
33 @testset "1D" begin
34 l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2));
35 monomials = ()
36 maxOrder = 4;
37 for i = 0:maxOrder-1
38 f_i(x) = 1/factorial(i)*x^i
39 monomials = (monomials...,evalOn(g_1D,f_i))
40 end
41 v = evalOn(g_1D,x -> sin(x))
42 vₓₓ = evalOn(g_1D,x -> -sin(x))
43
44 # 2nd order interior stencil, 1nd order boundary stencil,
45 # implies that L*v should be exact for monomials up to order 2.
46 @testset "2nd order" begin
47 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2)
48 Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils)
49 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
50 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
51 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
52 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2
53 end
54
55 # 4th order interior stencil, 2nd order boundary stencil,
56 # implies that L*v should be exact for monomials up to order 3.
57 @testset "4th order" begin
58 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
59 Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils)
60 # NOTE: high tolerances for checking the "exact" differentiation
61 # due to accumulation of round-off errors/cancellation errors?
62 @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
63 @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10
64 @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10
65 @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10
66 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2
67 end
68 end
69
70 @testset "2D" begin
71 l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2));
72 binomials = ()
73 maxOrder = 4;
74 for i = 0:maxOrder-1
75 f_i(x,y) = 1/factorial(i)*y^i + x^i
76 binomials = (binomials...,evalOn(g_2D,f_i))
77 end
78 v = evalOn(g_2D, (x,y) -> sin(x)+cos(y))
79 v_yy = evalOn(g_2D,(x,y) -> -cos(y))
80
81 # 2nd order interior stencil, 1st order boundary stencil,
82 # implies that L*v should be exact for binomials up to order 2.
83 @testset "2nd order" begin
84 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2)
85 Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2)
86 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
87 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
88 @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9
89 @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2
90 end
91
92 # 4th order interior stencil, 2nd order boundary stencil,
93 # implies that L*v should be exact for binomials up to order 3.
94 @testset "4th order" begin
95 op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
96 Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2)
97 # NOTE: high tolerances for checking the "exact" differentiation
98 # due to accumulation of round-off errors/cancellation errors?
99 @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
100 @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9
101 @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9
102 @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9
103 @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2
104 end
105 end
106 end
107 end