changeset 604:6901d5375311 refactor/toml_operator_format

Add tests for get_stencil, get_stencils and get_tuple
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 05 Dec 2020 13:51:11 +0100
parents c2239c7cd71e
children 8fcf9c9afb30
files test/Manifest.toml test/Project.toml test/testSbpOperators.jl
diffstat 3 files changed, 85 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/test/Manifest.toml	Sat Dec 05 13:05:53 2020 +0100
+++ b/test/Manifest.toml	Sat Dec 05 13:51:11 2020 +0100
@@ -109,6 +109,12 @@
 uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
 version = "0.10.3"
 
+[[TOML]]
+deps = ["Dates"]
+git-tree-sha1 = "d0ac7eaad0fb9f6ba023a1d743edca974ae637c4"
+uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
+version = "1.0.0"
+
 [[Test]]
 deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
 uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
--- a/test/Project.toml	Sat Dec 05 13:05:53 2020 +0100
+++ b/test/Project.toml	Sat Dec 05 13:51:11 2020 +0100
@@ -1,5 +1,6 @@
 [deps]
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
+TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
 Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
 TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04"
 Tullio = "bc48ee85-29a4-5162-ae0b-a64e1601d4bc"
--- a/test/testSbpOperators.jl	Sat Dec 05 13:05:53 2020 +0100
+++ b/test/testSbpOperators.jl	Sat Dec 05 13:51:11 2020 +0100
@@ -4,6 +4,7 @@
 using Sbplib.RegionIndices
 using Sbplib.LazyTensors
 using LinearAlgebra
+using TOML
 
 @testset "SbpOperators" begin
 
@@ -19,6 +20,83 @@
     @test SbpOperators.Stencil((1,2,3,4), center=4) == SbpOperators.Stencil((-3, 0),(1,2,3,4))
 end
 
+@testset "readoperator" begin
+    toml_str = """
+        [meta]
+        type = "equidistant"
+
+        [order2]
+        H.inner = ["1"]
+
+        D1.inner_stencil = ["-1/2", "0", "1/2"]
+        D1.closure_stencils = [
+            ["-1", "1"],
+        ]
+
+        d1.closure = ["-3/2", "2", "-1/2"]
+
+        [order4]
+        H.closure = ["17/48", "59/48", "43/48", "49/48"]
+
+        D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"]
+        D2.closure_stencils = [
+            [     "2",    "-5",      "4",       "-1",     "0",     "0"],
+            [     "1",    "-2",      "1",        "0",     "0",     "0"],
+            [ "-4/43", "59/43", "-110/43",   "59/43", "-4/43",     "0"],
+            [ "-1/49",     "0",   "59/49", "-118/49", "64/49", "-4/49"],
+        ]
+    """
+
+    parsed_toml = TOML.parse(toml_str)
+    @testset "get_stencil" begin
+        @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == SbpOperators.Stencil((-1/2, 0., 1/2), center=2)
+        @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == SbpOperators.Stencil((-1/2, 0., 1/2); center=1)
+        @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=3) == SbpOperators.Stencil((-1/2, 0., 1/2); center=3)
+
+        @test get_stencil(parsed_toml, "order2", "H", "inner") == SbpOperators.Stencil((1.,), center=1)
+
+        @test_throws AssertionError get_stencil(parsed_toml, "meta", "type")
+        @test_throws AssertionError get_stencil(parsed_toml, "order2", "D1", "closure_stencils")
+    end
+
+    @testset "get_stencils" begin
+        @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(1,)) == (SbpOperators.Stencil((-1., 1.), center=1),)
+        @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=(2,)) == (SbpOperators.Stencil((-1., 1.), center=2),)
+        @test get_stencils(parsed_toml, "order2", "D1", "closure_stencils", centers=[2]) == (SbpOperators.Stencil((-1., 1.), center=2),)
+
+        @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=[1,1,1,1]) == (
+            SbpOperators.Stencil((    2.,    -5.,      4.,     -1.,    0.,    0.), center=1),
+            SbpOperators.Stencil((    1.,    -2.,      1.,      0.,    0.,    0.), center=1),
+            SbpOperators.Stencil(( -4/43,  59/43, -110/43,   59/43, -4/43,    0.), center=1),
+            SbpOperators.Stencil(( -1/49,     0.,   59/49, -118/49, 64/49, -4/49), center=1),
+        )
+
+        @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(4,2,3,1)) == (
+            SbpOperators.Stencil((    2.,    -5.,      4.,     -1.,    0.,    0.), center=4),
+            SbpOperators.Stencil((    1.,    -2.,      1.,      0.,    0.,    0.), center=2),
+            SbpOperators.Stencil(( -4/43,  59/43, -110/43,   59/43, -4/43,    0.), center=3),
+            SbpOperators.Stencil(( -1/49,     0.,   59/49, -118/49, 64/49, -4/49), center=1),
+        )
+
+        @test get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=1:4) == (
+            SbpOperators.Stencil((    2.,    -5.,      4.,     -1.,    0.,    0.), center=1),
+            SbpOperators.Stencil((    1.,    -2.,      1.,      0.,    0.,    0.), center=2),
+            SbpOperators.Stencil(( -4/43,  59/43, -110/43,   59/43, -4/43,    0.), center=3),
+            SbpOperators.Stencil(( -1/49,     0.,   59/49, -118/49, 64/49, -4/49), center=4),
+        )
+
+        @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3))
+        @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "closure_stencils",centers=(1,2,3,5,4))
+        @test_throws AssertionError get_stencils(parsed_toml, "order4", "D2", "inner_stencil",centers=(1,2))
+    end
+
+    @testset "get_tuple" begin
+        @test get_tuple(parsed_toml, "order2", "d1", "closure") == (-3/2, 2, -1/2)
+
+        @test_throws AssertionError get_tuple(parsed_toml, "meta", "type")
+    end
+end
+
 # @testset "apply_quadrature" begin
 #     op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
 #     h = 0.5