comparison test/SbpOperators/stencil_set_test.jl @ 1040:7fc8df5157a7 refactor/lazy_tensors

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