changeset 764:d34b515b0ae7 operator_storage_array_of_table

Add functions for reading stencil sets
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 06 Feb 2021 20:45:34 +0100
parents 5ff162f3ed72
children fdd48f6ace1c
files src/SbpOperators/readoperator.jl test/testSbpOperators.jl
diffstat 2 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/readoperator.jl	Sat Feb 06 20:38:24 2021 +0100
+++ b/src/SbpOperators/readoperator.jl	Sat Feb 06 20:45:34 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
@@ -53,11 +56,33 @@
 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...)
 
-function get_stencil_set(parsed_toml, filter_pairs::Vararg{Pair})
+"""
+    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
 
 """
--- a/test/testSbpOperators.jl	Sat Feb 06 20:38:24 2021 +0100
+++ b/test/testSbpOperators.jl	Sat Feb 06 20:45:34 2021 +0100
@@ -93,6 +93,17 @@
     """
 
     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)
@@ -317,7 +328,7 @@
                 @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2
             end
         end
-        
+
         @testset "2D" begin
             l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2));
             binomials = ()