comparison src/SbpOperators/readoperator.jl @ 333:01b851161018 refactor/combine_to_one_package

Start converting to one package by moving all the files to their correct location
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 25 Sep 2020 13:06:02 +0200
parents SbpOperators/src/readoperator.jl@51e7de109c25
children 29df39a7b568
comparison
equal deleted inserted replaced
332:535f1bff4bcc 333:01b851161018
1 function readOperator(D2fn, Hfn)
2 d = readSectionedFile(D2fn)
3 h = readSectionedFile(Hfn)
4
5 # Create inner stencil
6 innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1])
7 width = length(innerStencilWeights)
8 r = (-div(width,2), div(width,2))
9
10 innerStencil = Stencil(r, innerStencilWeights)
11
12 # Create boundary stencils
13 boundarySize = length(d["boundary_stencils"])
14 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
15
16 for i ∈ 1:boundarySize
17 stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i])
18 width = length(stencilWeights)
19 r = (1-i,width-i)
20 closureStencils = (closureStencils..., Stencil(r, stencilWeights))
21 end
22
23 quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize)
24 eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize))
25 dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize))
26
27 d2 = D2(
28 quadratureClosure,
29 innerStencil,
30 closureStencils,
31 eClosure,
32 dClosure,
33 even
34 )
35
36 return d2
37 end
38
39 function readSectionedFile(filename)::Dict{String, Vector{String}}
40 f = open(filename)
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
62
63 function stringToTuple(T::DataType, s::String)
64 return Tuple(stringToVector(T,s))
65 end
66
67 function stringToVector(T::DataType, s::String)
68 return T.(eval.(Meta.parse.(split(s))))
69 end
70
71 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T}
72 if N >= n
73 return t
74 else
75 return pad_tuple((t..., zero(T)), n)
76 end
77 end
78
79 sbp_operators_path() = (@__DIR__) * "/../operators/"
80 export sbp_operators_path