changeset 765:fdd48f6ace1c operator_storage_array_of_table

Merge in default
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 07 Feb 2021 21:32:21 +0100
parents d34b515b0ae7 (diff) 621460cf8279 (current diff)
children 7624a1350ece
files src/SbpOperators/readoperator.jl test/testSbpOperators.jl
diffstat 3 files changed, 103 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/operators/standard_diagonal.toml	Sun Feb 07 21:16:40 2021 +0100
+++ b/src/SbpOperators/operators/standard_diagonal.toml	Sun Feb 07 21:32:21 2021 +0100
@@ -1,36 +1,42 @@
 [meta]
 authors = "Ken Mattson"
-descripion = "Standard operators for equidistant grids"
+description = "Standard operators for equidistant grids"
 type = "equidistant"
+cite = "A paper a long time ago in a galaxy far far away."
 
-[order2]
+[[stencil_set]]
+
+order = 2
+
 H.inner = ["1"]
 H.closure = ["1/2"]
 
 D1.inner_stencil = ["-1/2", "0", "1/2"]
 D1.closure_stencils = [
-    ["-1", "1"],
+    {s = ["-1", "1"], c = 1},
 ]
 
 D2.inner_stencil = ["1", "-2", "1"]
 D2.closure_stencils = [
-    ["1", "-2", "1"],
+    {s = ["1", "-2", "1"], c = 1},
 ]
 
 e.closure = ["1"]
-d1.closure = ["-3/2", "2", "-1/2"]
+d1.closure = {s = ["-3/2", "2", "-1/2"], c = 1}
 
-[order4]
+[[stencil_set]]
+
+order = 4
 H.inner = ["1"]
 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"],
+    {s = [     "2",    "-5",      "4",       "-1",     "0",     "0"], c = 1},
+    {s = [     "1",    "-2",      "1",        "0",     "0",     "0"], c = 2},
+    {s = [ "-4/43", "59/43", "-110/43",   "59/43", "-4/43",     "0"], c = 3},
+    {s = [ "-1/49",     "0",   "59/49", "-118/49", "64/49", "-4/49"], c = 4},
 ]
 
 e.closure = ["1"]
-d1.closure = ["-11/6", "3", "-3/2", "1/3"]
+d1.closure = {s = ["-11/6", "3", "-3/2", "1/3"], c = 1}
--- a/src/SbpOperators/readoperator.jl	Sun Feb 07 21:16:40 2021 +0100
+++ b/src/SbpOperators/readoperator.jl	Sun Feb 07 21:32:21 2021 +0100
@@ -1,5 +1,8 @@
 using TOML
 
+export read_stencil_set
+export get_stencil_set
+
 export read_D2_operator
 export read_stencil
 export read_stencils
@@ -47,6 +50,40 @@
     return d2
 end
 
+"""
+    read_stencil_set(fn, filter_pairs::Vararg{Pair})
+
+Picks out a stencil set from the given toml file based on some filters.
+If more than one set matches the filters an error is raised.
+
+The stencil set is not parsed beyond the inital toml parse. To get usable
+stencils use the `parse_stencil` functions on the fields of the stencil set.
+"""
+read_stencil_set(fn, filter_pairs::Vararg{Pair}) = get_stencil_set(TOML.parsefile(fn), filter_pairs...)
+
+"""
+    get_stencil_set(parsed_toml; filters...)
+
+Same as `read_stencil_set` but works on already parsed TOML.
+"""
+function get_stencil_set(parsed_toml; filters...)
+    matches = findall(parsed_toml["stencil_set"]) do set
+        for (key, val) ∈ filters
+            if set[string(key)] != val
+                return false
+            end
+        end
+
+        return true
+    end
+
+    if length(matches) != 1
+        throw(ArgumentError("filters must pick out a single stencil set"))
+    end
+
+    i = matches[1]
+    return parsed_toml["stencil_set"][i]
+end
 
 """
     read_stencil(fn, path...; [center])
@@ -133,6 +170,9 @@
     return t
 end
 
+function get_rationals()
+end
+
 # TODO: Probably should be deleted once we have gotten rid of read_D2_operator()
 function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T
     return Tuple(T.(parse_rational.(arr)))
--- a/test/testSbpOperators.jl	Sun Feb 07 21:16:40 2021 +0100
+++ b/test/testSbpOperators.jl	Sun Feb 07 21:32:21 2021 +0100
@@ -44,31 +44,69 @@
 @testset "readoperator" begin
     toml_str = """
         [meta]
+        authors = "Ken Mattson"
+        description = "Standard operators for equidistant grids"
         type = "equidistant"
+        cite = "A paper a long time ago in a galaxy far far away."
 
-        [order2]
+        [[stencil_set]]
+
+        order = 2
+        test = 2
+
         H.inner = ["1"]
+        H.closure = ["1/2"]
 
         D1.inner_stencil = ["-1/2", "0", "1/2"]
         D1.closure_stencils = [
-            ["-1", "1"],
+            {s = ["-1", "1"], c = 1},
+        ]
+
+        D2.inner_stencil = ["1", "-2", "1"]
+        D2.closure_stencils = [
+            {s = ["1", "-2", "1"], c = 1},
         ]
 
-        d1.closure = ["-3/2", "2", "-1/2"]
+        e.closure = ["1"]
+        d1.closure = {s = ["-3/2", "2", "-1/2"], c = 1}
 
-        [order4]
+        [[stencil_set]]
+
+        order = 4
+        test = 1
+        H.inner = ["1"]
         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"],
+            {s = [     "2",    "-5",      "4",       "-1",     "0",     "0"], c = 1},
+            {s = [     "1",    "-2",      "1",        "0",     "0",     "0"], c = 2},
+            {s = [ "-4/43", "59/43", "-110/43",   "59/43", "-4/43",     "0"], c = 3},
+            {s = [ "-1/49",     "0",   "59/49", "-118/49", "64/49", "-4/49"], c = 4},
         ]
+
+        e.closure = ["1"]
+        d1.closure = {s = ["-11/6", "3", "-3/2", "1/3"], c = 1}
+
+        [[stencil_set]]
+        order = 4
+        test = 2
+
+        H.closure = ["-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"]
     """
 
     parsed_toml = TOML.parse(toml_str)
+
+    @testset "get_stencil_set" begin
+        @test get_stencil_set(parsed_toml; order = 2) isa Dict
+        @test get_stencil_set(parsed_toml; order = 2) == parsed_toml["stencil_set"][1]
+        @test get_stencil_set(parsed_toml; test = 1) == parsed_toml["stencil_set"][2]
+        @test get_stencil_set(parsed_toml; order = 4, test = 2) == parsed_toml["stencil_set"][3]
+
+        @test_throws ArgumentError get_stencil_set(parsed_toml; test = 2)
+        @test_throws ArgumentError get_stencil_set(parsed_toml; order = 4)
+    end
+
     @testset "get_stencil" begin
         @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil") == Stencil(-1/2, 0., 1/2, center=2)
         @test get_stencil(parsed_toml, "order2", "D1", "inner_stencil", center=1) == Stencil(-1/2, 0., 1/2; center=1)