comparison test/SbpOperators/readoperator_test.jl @ 864:9a2776352c2a

Merge operator_storage_array_of_table
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 19 Jan 2022 11:08:43 +0100
parents 760c11e81fd4
children 06c510d40ebb
comparison
equal deleted inserted replaced
858:5088de9b6d65 864:9a2776352c2a
3 using TOML 3 using TOML
4 using Sbplib.SbpOperators 4 using Sbplib.SbpOperators
5 5
6 import Sbplib.SbpOperators.Stencil 6 import Sbplib.SbpOperators.Stencil
7 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
8 155
9 @testset "parse_rational" begin 156 @testset "parse_rational" begin
10 @test SbpOperators.parse_rational("1") isa Rational 157 @test SbpOperators.parse_rational("1") isa Rational
11 @test SbpOperators.parse_rational("1") == 1//1 158 @test SbpOperators.parse_rational("1") == 1//1
12 @test SbpOperators.parse_rational("1/2") isa Rational 159 @test SbpOperators.parse_rational("1/2") isa Rational
13 @test SbpOperators.parse_rational("1/2") == 1//2 160 @test SbpOperators.parse_rational("1/2") == 1//2
14 @test SbpOperators.parse_rational("37/13") isa Rational 161 @test SbpOperators.parse_rational("37/13") isa Rational
15 @test SbpOperators.parse_rational("37/13") == 37//13 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
16 end 172 end
17
18 @testset "readoperator" begin
19 toml_str = """
20 [meta]
21 type = "equidistant"
22
23 [order2]
24 H.inner = ["1"]
25
26 D1.inner_stencil = ["-1/2", "0", "1/2"]
27 D1.closure_stencils = [
28 ["-1", "1"],
29 ]
30
31 d1.closure = ["-3/2", "2", "-1/2"]
32
33 [order4]
34 H.closure = ["17/48", "59/48", "43/48", "49/48"]
35
36 D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"]
37 D2.closure_stencils = [
38 [ "2", "-5", "4", "-1", "0", "0"],
39 [ "1", "-2", "1", "0", "0", "0"],
40 [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"],
41 [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"],
42 ]
43 """
44
45 parsed_toml = TOML.parse(toml_str)
46 @testset "get_stencil" begin
47 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil(-1/2, 0., 1/2, center=2)
48 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil(-1/2, 0., 1/2; center=1)
49 @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == Stencil(-1/2, 0., 1/2; center=3)
50
51 @test get_stencil(parsed_toml, "order2", "H", "inner") == Stencil(1.; center=1)
52
53 @test_throws AssertionError get_stencil(parsed_toml, "meta", "type")
54 @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils")
55 end
56
57 @testset "get_stencils" begin
58 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (Stencil(-1., 1., center=1),)
59 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (Stencil(-1., 1., center=2),)
60 @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (Stencil(-1., 1., center=2),)
61
62 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == (
63 Stencil( 2., -5., 4., -1., 0., 0., center=1),
64 Stencil( 1., -2., 1., 0., 0., 0., center=1),
65 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=1),
66 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1),
67 )
68
69 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == (
70 Stencil( 2., -5., 4., -1., 0., 0., center=4),
71 Stencil( 1., -2., 1., 0., 0., 0., center=2),
72 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3),
73 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=1),
74 )
75
76 @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == (
77 Stencil( 2., -5., 4., -1., 0., 0., center=1),
78 Stencil( 1., -2., 1., 0., 0., 0., center=2),
79 Stencil( -4/43, 59/43, -110/43, 59/43, -4/43, 0., center=3),
80 Stencil( -1/49, 0., 59/49, -118/49, 64/49, -4/49, center=4),
81 )
82
83 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3))
84 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4))
85 @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2))
86 end
87
88 @testset "get_tuple" begin
89 @test get_tuple(parsed_toml, "order2", "d1", "closure") == (-3/2, 2, -1/2)
90
91 @test_throws AssertionError get_tuple(parsed_toml, "meta", "type")
92 end
93 end