comparison test/SbpOperators/volumeops/derivatives/first_derivative_test.jl @ 1207:f1c2a4fa0ee1 performance/get_region_type_inference

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 03 Feb 2023 22:14:47 +0100
parents c94a12327737
children 7d52c4835d15
comparison
equal deleted inserted replaced
919:b41180efb6c2 1207:f1c2a4fa0ee1
1 using Test
2
3
4 using Sbplib.SbpOperators
5 using Sbplib.Grids
6 using Sbplib.LazyTensors
7
8 using Sbplib.SbpOperators: closure_size, Stencil, VolumeOperator
9
10 """
11 monomial(x,k)
12
13 Evaluates ``x^k/k!` with the convetion that it is ``0`` for all ``k<0``.
14 Has the property that ``d/dx monomial(x,k) = monomial(x,k-1)``
15 """
16 function monomial(x,k)
17 if k < 0
18 return zero(x)
19 end
20 x^k/factorial(k)
21 end
22
23 @testset "first_derivative" begin
24 @testset "Constructors" begin
25 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order=2)
26
27 g₁ = EquidistantGrid(11, 0., 1.)
28 g₂ = EquidistantGrid((11,14), (0.,1.), (1.,3.))
29
30 @test first_derivative(g₁, stencil_set, 1) isa LazyTensor{Float64,1,1}
31 @test first_derivative(g₂, stencil_set, 2) isa LazyTensor{Float64,2,2}
32 @test first_derivative(g₁, stencil_set, 1) == first_derivative(g₁, stencil_set)
33
34 interior_stencil = CenteredStencil(-1,0,1)
35 closure_stencils = [Stencil(-1,1, center=1)]
36
37 @test first_derivative(g₁, interior_stencil, closure_stencils, 1) isa LazyTensor{Float64,1,1}
38 @test first_derivative(g₁, interior_stencil, closure_stencils, 1) isa VolumeOperator
39 @test first_derivative(g₁, interior_stencil, closure_stencils, 1) == first_derivative(g₁, interior_stencil, closure_stencils)
40 @test first_derivative(g₂, interior_stencil, closure_stencils, 2) isa LazyTensor{Float64,2,2}
41 end
42
43 @testset "Accuracy conditions" begin
44 N = 20
45 g = EquidistantGrid(N, 0//1,2//1)
46 @testset for order ∈ [2,4]
47 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order)
48 D₁ = first_derivative(g, stencil_set, 1)
49
50 @testset "boundary x^$k" for k ∈ 0:order÷2
51 v = evalOn(g, x->monomial(x,k))
52
53 @testset for i ∈ 1:closure_size(D₁)
54 x, = points(g)[i]
55 @test (D₁*v)[i] == monomial(x,k-1)
56 end
57
58 @testset for i ∈ (N-closure_size(D₁)+1):N
59 x, = points(g)[i]
60 @test (D₁*v)[i] == monomial(x,k-1)
61 end
62 end
63
64 @testset "interior x^$k" for k ∈ 0:order
65 v = evalOn(g, x->monomial(x,k))
66
67 x, = points(g)[10]
68 @test (D₁*v)[10] == monomial(x,k-1)
69 end
70 end
71 end
72
73 @testset "Accuracy on function" begin
74 # 1D
75 g = EquidistantGrid(30, 0.,1.)
76 v = evalOn(g, x->exp(x))
77 @testset for (order, tol) ∈ [(2, 6e-3),(4, 2e-4)]
78 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order)
79 D₁ = first_derivative(g, stencil_set, 1)
80
81 @test D₁*v ≈ v rtol=tol
82 end
83
84 # 2D
85 g = EquidistantGrid((30,60), (0.,0.),(1.,2.))
86 v = evalOn(g, (x,y)->exp(0.8x+1.2*y))
87 @testset for (order, tol) ∈ [(2, 6e-3),(4, 3e-4)]
88 stencil_set = read_stencil_set(sbp_operators_path()*"standard_diagonal.toml"; order)
89 Dx = first_derivative(g, stencil_set, 1)
90 Dy = first_derivative(g, stencil_set, 2)
91
92 @test Dx*v ≈ 0.8v rtol=tol
93 @test Dy*v ≈ 1.2v rtol=tol
94 end
95 end
96 end
97