changeset 598:19e1b169aa9c refactor/toml_operator_format

Add funcitons for reading several stecils or reading tuples
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 04 Dec 2020 13:11:24 +0100
parents 98cd99237176
children a21fe9b6e5b6
files src/SbpOperators/readoperator.jl
diffstat 1 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/readoperator.jl	Wed Dec 02 17:30:18 2020 +0100
+++ b/src/SbpOperators/readoperator.jl	Fri Dec 04 13:11:24 2020 +0100
@@ -48,22 +48,42 @@
 ```
 """
 read_stencil(fn, path...; center=nothing) = get_stencil(TOML.parsefile(fn), path...; center=center)
-
-function get_stencil(parsed_toml, path...; center=nothing)
-    if length(path) == 0
-        @assert parsed_toml isa Vector
-        stencil_weights = Float64.(parse_rational.(parsed_toml))
+read_stencils(fn, path..., centers=nothing) = get_stencils(TOML.parsefile(fn), path...; centers=centers)
+read_tuple(fn, path..., centers=nothing) = get_tuple(TOML.parsefile(fn), path...)
 
-        width = length(stencil_weights)
+get_stencil(parsed_toml, path...; center=nothing) = get_stencil(parsed_toml[path[1]], path[2:end]...; center=center)
+function get_stencil(parsed_toml; center=nothing)
+    @assert parsed_toml isa Vector{String}
+    stencil_weights = Float64.(parse_rational.(parsed_toml))
 
-        if isnothing(center)
-            center = div(width,2)+1
-        end
+    width = length(stencil_weights)
 
-        return Stencil(Tuple(stencil_weights), center=center)
+    if isnothing(center)
+        center = div(width,2)+1
     end
 
-    return get_stencil(parsed_toml[path[1]], path[2:end]...; center=center)
+    return Stencil(Tuple(stencil_weights), center=center)
+end
+
+get_stencils(parsed_toml, path...; centers) = get_stencils(parsed_toml[path[1]], path[2:end]...; centers=centers)
+function get_stencils(parsed_toml; centers)
+    @assert parsed_toml isa Vector{Vector{String}}
+    @assert length(centers) == length(parsed_toml)
+
+    stencils = ()
+    for i ∈ 1:length(parsed_toml)
+        stencil = get_stencil(parsed_toml[i]), center = centers[i]
+        stencils = (stencils..., stencil)
+    end
+
+    return stencils
+end
+
+get_tuple(parsed_toml, path...) = get_tuple(parsed_toml[path[1]], path[2:end]...)
+function get_tuple(parsed_toml, path...)
+    @assert parsed_toml isa Vector{String}
+    t = Tuple(Float64.(parse_rational.(parsed_toml)))
+    return t
 end
 
 # TODO: Probably should be deleted once we have gotten rid of read_D2_operator()