diff src/SbpOperators/readoperator.jl @ 594:cc86b920531a refactor/toml_operator_format

Change the readoperator function to use the .toml format
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 02 Dec 2020 15:26:13 +0100
parents 29df39a7b568
children a9744aa5e235
line wrap: on
line diff
--- a/src/SbpOperators/readoperator.jl	Wed Dec 02 15:24:56 2020 +0100
+++ b/src/SbpOperators/readoperator.jl	Wed Dec 02 15:26:13 2020 +0100
@@ -1,30 +1,35 @@
-function readOperator(D2fn, Hfn)
-    d = readSectionedFile(D2fn)
-    h = readSectionedFile(Hfn)
+using TOML
+function read_D2_operator(fn; order)
+    operators = TOML.parsefile(fn)["order$order"]
+    D2 = operators["D2"]
+    H = operators["H"]
+    e = operators["e"]
+    d1 = operators["d1"]
 
     # Create inner stencil
-    innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1])
+    innerStencilWeights = toml_string_array_to_tuple(Float64, D2["inner_stencil"])
+
     width = length(innerStencilWeights)
     r = (-div(width,2), div(width,2))
 
     innerStencil = Stencil(r, innerStencilWeights)
 
     # Create boundary stencils
-    boundarySize = length(d["boundary_stencils"])
+    boundarySize = length(D2["closure_stencils"])
     closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
 
     for i ∈ 1:boundarySize
-        stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i])
+        stencilWeights = toml_string_array_to_tuple(Float64, D2["closure_stencils"][i])
         width = length(stencilWeights)
         r = (1-i,width-i)
         closureStencils = (closureStencils..., Stencil(r, stencilWeights))
     end
 
-    quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize)
-    eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize))
-    dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize))
+    quadratureClosure = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize)
+    eClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize))
+    dClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize))
 
-    d2 = D2(
+    d2 = SbpOperators.D2(
         quadratureClosure,
         innerStencil,
         closureStencils,
@@ -36,36 +41,13 @@
     return d2
 end
 
-function readSectionedFile(filename)::Dict{String, Vector{String}}
-    f = open(filename)
-    sections = Dict{String, Vector{String}}()
-    currentKey = ""
-
-    for ln ∈ eachline(f)
-        if ln == "" || ln[1] == '#' # Skip comments and empty lines
-            continue
-        end
-
-        if isletter(ln[1]) # Found start of new section
-            if ~haskey(sections, ln)
-                sections[ln] =  Vector{String}()
-            end
-            currentKey = ln
-            continue
-        end
-
-        push!(sections[currentKey], ln)
-    end
-
-    return sections
+function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T
+    return Tuple(T.(parse_rational.(arr)))
 end
 
-function stringToTuple(T::DataType, s::String)
-    return Tuple(stringToVector(T,s))
-end
-
-function stringToVector(T::DataType, s::String)
-    return T.(eval.(Meta.parse.(split(s))))
+function parse_rational(str)
+    expr = Meta.parse(replace(str, "/"=>"//"))
+    return eval(:(Rational($expr)))
 end
 
 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T}