comparison test/SbpOperators/stencil_set_test.jl @ 1049:3bb94ce74697 feature/variable_derivatives

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 23 Mar 2022 12:54:45 +0100
parents test/SbpOperators/readoperator_test.jl@d24b331547f3 test/SbpOperators/readoperator_test.jl@3031ce7a4999
children 84dc3b9b449b
comparison
equal deleted inserted replaced
1048:86aa69ad3304 1049:3bb94ce74697
1 using Test
2
3 using TOML
4 using Sbplib.SbpOperators
5
6 import Sbplib.SbpOperators.Stencil
7 import Sbplib.SbpOperators.NestedStencil
8
9 @testset "readoperator" begin
10 toml_str = """
11 [meta]
12 authors = "Ken Mattson"
13 description = "Standard operators for equidistant grids"
14 type = "equidistant"
15 cite = "A paper a long time ago in a galaxy far far away."
16
17 [[stencil_set]]
18
19 order = 2
20 test = 2
21
22 H.inner = ["1"]
23 H.closure = ["1/2"]
24
25 D1.inner_stencil = ["-1/2", "0", "1/2"]
26 D1.closure_stencils = [
27 {s = ["-1", "1"], c = 1},
28 ]
29
30 D2.inner_stencil = ["1", "-2", "1"]
31 D2.closure_stencils = [
32 {s = ["1", "-2", "1"], c = 1},
33 ]
34
35 e.closure = ["1"]
36 d1.closure = {s = ["-3/2", "2", "-1/2"], c = 1}
37
38 [[stencil_set]]
39
40 order = 4
41 test = 1
42 H.inner = ["1"]
43 H.closure = ["17/48", "59/48", "43/48", "49/48"]
44
45 D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"]
46 D2.closure_stencils = [
47 {s = [ "2", "-5", "4", "-1", "0", "0"], c = 1},
48 {s = [ "1", "-2", "1", "0", "0", "0"], c = 2},
49 {s = [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"], c = 3},
50 {s = [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"], c = 4},
51 ]
52
53 e.closure = ["1"]
54 d1.closure = {s = ["-11/6", "3", "-3/2", "1/3"], c = 1}
55
56 [[stencil_set]]
57 order = 4
58 test = 2
59
60 H.closure = ["-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"]
61 """
62
63 parsed_toml = TOML.parse(toml_str)
64
65 @testset "get_stencil_set" begin
66 @test get_stencil_set(parsed_toml; order = 2) isa Dict
67 @test get_stencil_set(parsed_toml; order = 2) == parsed_toml["stencil_set"][1]
68 @test get_stencil_set(parsed_toml; test = 1) == parsed_toml["stencil_set"][2]
69 @test get_stencil_set(parsed_toml; order = 4, test = 2) == parsed_toml["stencil_set"][3]
70
71 @test_throws ArgumentError get_stencil_set(parsed_toml; test = 2)
72 @test_throws ArgumentError get_stencil_set(parsed_toml; order = 4)
73 end
74
75 @testset "parse_stencil" begin
76 toml = """
77 s1 = ["-1/12","4/3","-5/2","4/3","-1/12"]
78 s2 = {s = ["2", "-5", "4", "-1", "0", "0"], c = 1}
79 s3 = {s = ["1", "-2", "1", "0", "0", "0"], c = 2}
80 s4 = "not a stencil"
81 s5 = [-1, 4, 3]
82 s6 = {k = ["1", "-2", "1", "0", "0", "0"], c = 2}
83 s7 = {s = [-1, 4, 3], c = 2}
84 s8 = {s = ["1", "-2", "1", "0", "0", "0"], c = [2,2]}
85 """
86
87 @test parse_stencil(TOML.parse(toml)["s1"]) == CenteredStencil(-1//12, 4//3, -5//2, 4//3, -1//12)
88 @test parse_stencil(TOML.parse(toml)["s2"]) == Stencil(2//1, -5//1, 4//1, -1//1, 0//1, 0//1; center=1)
89 @test parse_stencil(TOML.parse(toml)["s3"]) == Stencil(1//1, -2//1, 1//1, 0//1, 0//1, 0//1; center=2)
90
91 @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s4"])
92 @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s5"])
93 @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s6"])
94 @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s7"])
95 @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s8"])
96
97 stencil_set = get_stencil_set(parsed_toml; order = 4, test = 1)
98
99 @test parse_stencil.(stencil_set["D2"]["closure_stencils"]) == [
100 Stencil( 2//1, -5//1, 4//1, -1//1, 0//1, 0//1; center=1),
101 Stencil( 1//1, -2//1, 1//1, 0//1, 0//1, 0//1; center=2),
102 Stencil(-4//43, 59//43, -110//43, 59//43, -4//43, 0//1; center=3),
103 Stencil(-1//49, 0//1, 59//49, -118//49, 64//49, -4//49; center=4),
104 ]
105
106
107 @test parse_stencil(Float64, TOML.parse(toml)["s1"]) == CenteredStencil(-1/12, 4/3, -5/2, 4/3, -1/12)
108 @test parse_stencil(Float64, TOML.parse(toml)["s2"]) == Stencil(2/1, -5/1, 4/1, -1/1, 0/1, 0/1; center=1)
109 @test parse_stencil(Float64, TOML.parse(toml)["s3"]) == Stencil(1/1, -2/1, 1/1, 0/1, 0/1, 0/1; center=2)
110 end
111
112 @testset "parse_scalar" begin
113 toml = TOML.parse("""
114 a1 = 1
115 a2 = 1.5
116 a3 = 1.0
117 a4 = 10
118 a5 = "1/2"
119 a6 = "1.5"
120
121 e1 = [1,2,3]
122 e2 = "a string value"
123 """)
124
125 @test parse_scalar(toml["a1"]) == 1//1
126 @test parse_scalar(toml["a2"]) == 3//2
127 @test parse_scalar(toml["a3"]) == 1//1
128 @test parse_scalar(toml["a4"]) == 10//1
129 @test parse_scalar(toml["a5"]) == 1//2
130 @test parse_scalar(toml["a6"]) == 3//2
131
132 @test_throws ArgumentError parse_scalar(toml["e1"])
133 @test_throws ArgumentError parse_scalar(toml["e2"])
134 end
135
136 @testset "parse_tuple" begin
137 toml = TOML.parse("""
138 t1 = [1,3,4]
139 t2 = ["1/2","3/4","2/1"]
140
141 e1 = "not a tuple"
142 e2.a="1"
143 e3 = 1
144 e4 = ["1/2","3/4","not a number"]
145 """)
146
147 @test parse_tuple(toml["t1"]) == (1//1,3//1,4//1)
148 @test parse_tuple(toml["t2"]) == (1//2,3//4,2//1)
149
150 @test_throws ArgumentError parse_tuple(toml["e1"])
151 @test_throws ArgumentError parse_tuple(toml["e2"])
152 @test_throws ArgumentError parse_tuple(toml["e3"])
153 @test_throws ArgumentError parse_tuple(toml["e4"])
154 end
155 end
156
157 @testset "parse_rational" begin
158 @test SbpOperators.parse_rational("1") isa Rational
159 @test SbpOperators.parse_rational("1") == 1//1
160 @test SbpOperators.parse_rational("1/2") isa Rational
161 @test SbpOperators.parse_rational("1/2") == 1//2
162 @test SbpOperators.parse_rational("37/13") isa Rational
163 @test SbpOperators.parse_rational("37/13") == 37//13
164
165 @test SbpOperators.parse_rational(0.5) isa Rational
166 @test SbpOperators.parse_rational(0.5) == 1//2
167
168 @test SbpOperators.parse_rational("0.5") isa Rational
169 @test SbpOperators.parse_rational("0.5") == 1//2
170
171 @test SbpOperators.parse_rational(2) isa Rational
172 @test SbpOperators.parse_rational(2) == 2//1
173 end
174
175 @testset "parse_nested_stencil" begin
176 toml = TOML.parse("""
177 s1 = [["1/2", "1/2", "0"],[ "-1/2", "-1", "-1/2"],["0", "1/2", "1/2"]]
178 s2 = {s = [[ "2", "-1", "0"],[ "-3", "1", "0"],["1", "0", "0"]], c = 1}
179 s3 = {s = [[ "2", "-1", "0"],[ "-3", "1", "0"],["1", "0", "0"]], c = 2}
180 """)
181
182 @test parse_nested_stencil(toml["s1"]) == CenteredNestedStencil((1//2, 1//2, 0//1),( -1//2, -1//1, -1//2),(0//1, 1//2, 1//2))
183 @test parse_nested_stencil(toml["s2"]) == NestedStencil((2//1, -1//1, 0//1),( -3//1, 1//1, 0//1),(1//1, 0//1, 0//1), center = 1)
184 @test parse_nested_stencil(toml["s3"]) == NestedStencil((2//1, -1//1, 0//1),( -3//1, 1//1, 0//1),(1//1, 0//1, 0//1), center = 2)
185
186 @test parse_nested_stencil(Float64, toml["s1"]) == CenteredNestedStencil((1/2, 1/2, 0.),( -1/2, -1., -1/2),(0., 1/2, 1/2))
187 @test parse_nested_stencil(Int, toml["s2"]) == NestedStencil((2, -1, 0),( -3, 1, 0),(1, 0, 0), center = 1)
188 end