Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/readoperator.jl @ 345:2fcc960836c6
Merge branch refactor/combine_to_one_package.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 26 Sep 2020 15:22:13 +0200 |
parents | 29df39a7b568 |
children | cc86b920531a |
comparison
equal
deleted
inserted
replaced
343:e12c9a866513 | 345:2fcc960836c6 |
---|---|
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 |