Mercurial > repos > public > sbplib_julia
comparison 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 |
comparison
equal
deleted
inserted
replaced
593:fa03dae0ff0b | 594:cc86b920531a |
---|---|
1 function readOperator(D2fn, Hfn) | 1 using TOML |
2 d = readSectionedFile(D2fn) | 2 function read_D2_operator(fn; order) |
3 h = readSectionedFile(Hfn) | 3 operators = TOML.parsefile(fn)["order$order"] |
4 D2 = operators["D2"] | |
5 H = operators["H"] | |
6 e = operators["e"] | |
7 d1 = operators["d1"] | |
4 | 8 |
5 # Create inner stencil | 9 # Create inner stencil |
6 innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1]) | 10 innerStencilWeights = toml_string_array_to_tuple(Float64, D2["inner_stencil"]) |
11 | |
7 width = length(innerStencilWeights) | 12 width = length(innerStencilWeights) |
8 r = (-div(width,2), div(width,2)) | 13 r = (-div(width,2), div(width,2)) |
9 | 14 |
10 innerStencil = Stencil(r, innerStencilWeights) | 15 innerStencil = Stencil(r, innerStencilWeights) |
11 | 16 |
12 # Create boundary stencils | 17 # Create boundary stencils |
13 boundarySize = length(d["boundary_stencils"]) | 18 boundarySize = length(D2["closure_stencils"]) |
14 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type? | 19 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type? |
15 | 20 |
16 for i ∈ 1:boundarySize | 21 for i ∈ 1:boundarySize |
17 stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i]) | 22 stencilWeights = toml_string_array_to_tuple(Float64, D2["closure_stencils"][i]) |
18 width = length(stencilWeights) | 23 width = length(stencilWeights) |
19 r = (1-i,width-i) | 24 r = (1-i,width-i) |
20 closureStencils = (closureStencils..., Stencil(r, stencilWeights)) | 25 closureStencils = (closureStencils..., Stencil(r, stencilWeights)) |
21 end | 26 end |
22 | 27 |
23 quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize) | 28 quadratureClosure = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize) |
24 eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize)) | 29 eClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize)) |
25 dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize)) | 30 dClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize)) |
26 | 31 |
27 d2 = D2( | 32 d2 = SbpOperators.D2( |
28 quadratureClosure, | 33 quadratureClosure, |
29 innerStencil, | 34 innerStencil, |
30 closureStencils, | 35 closureStencils, |
31 eClosure, | 36 eClosure, |
32 dClosure, | 37 dClosure, |
34 ) | 39 ) |
35 | 40 |
36 return d2 | 41 return d2 |
37 end | 42 end |
38 | 43 |
39 function readSectionedFile(filename)::Dict{String, Vector{String}} | 44 function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T |
40 f = open(filename) | 45 return Tuple(T.(parse_rational.(arr))) |
41 sections = Dict{String, Vector{String}}() | |
42 currentKey = "" | |
43 | |
44 for ln ∈ eachline(f) | |
45 if ln == "" || ln[1] == '#' # Skip comments and empty lines | |
46 continue | |
47 end | |
48 | |
49 if isletter(ln[1]) # Found start of new section | |
50 if ~haskey(sections, ln) | |
51 sections[ln] = Vector{String}() | |
52 end | |
53 currentKey = ln | |
54 continue | |
55 end | |
56 | |
57 push!(sections[currentKey], ln) | |
58 end | |
59 | |
60 return sections | |
61 end | 46 end |
62 | 47 |
63 function stringToTuple(T::DataType, s::String) | 48 function parse_rational(str) |
64 return Tuple(stringToVector(T,s)) | 49 expr = Meta.parse(replace(str, "/"=>"//")) |
65 end | 50 return eval(:(Rational($expr))) |
66 | |
67 function stringToVector(T::DataType, s::String) | |
68 return T.(eval.(Meta.parse.(split(s)))) | |
69 end | 51 end |
70 | 52 |
71 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T} | 53 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T} |
72 if N >= n | 54 if N >= n |
73 return t | 55 return t |