Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/readoperator.jl @ 633:a78bda7084f6 feature/quadrature_as_outer_product
Merge w. default
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 01 Jan 2021 16:34:55 +0100 |
parents | 7975143118e8 |
children | 4a81812150f4 |
comparison
equal
deleted
inserted
replaced
561:04d7b4eb63ef | 633:a78bda7084f6 |
---|---|
1 function readOperator(D2fn, Hfn) | 1 using TOML |
2 d = readSectionedFile(D2fn) | 2 |
3 h = readSectionedFile(Hfn) | 3 export read_D2_operator |
4 export read_stencil | |
5 export read_stencils | |
6 export read_tuple | |
7 | |
8 export get_stencil | |
9 export get_stencils | |
10 export get_tuple | |
11 | |
12 | |
13 function read_D2_operator(fn; order) | |
14 operators = TOML.parsefile(fn)["order$order"] | |
15 D2 = operators["D2"] | |
16 H = operators["H"] | |
17 e = operators["e"] | |
18 d1 = operators["d1"] | |
4 | 19 |
5 # Create inner stencil | 20 # Create inner stencil |
6 innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1]) | 21 innerStencil = get_stencil(operators, "D2", "inner_stencil") |
7 width = length(innerStencilWeights) | |
8 r = (-div(width,2), div(width,2)) | |
9 | |
10 innerStencil = Stencil(r, innerStencilWeights) | |
11 | 22 |
12 # Create boundary stencils | 23 # Create boundary stencils |
13 boundarySize = length(d["boundary_stencils"]) | 24 boundarySize = length(D2["closure_stencils"]) |
14 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type? | 25 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type? |
15 | 26 |
16 for i ∈ 1:boundarySize | 27 for i ∈ 1:boundarySize |
17 stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i]) | 28 closureStencils = (closureStencils..., get_stencil(operators, "D2", "closure_stencils", i; center=i)) |
18 width = length(stencilWeights) | |
19 r = (1-i,width-i) | |
20 closureStencils = (closureStencils..., Stencil(r, stencilWeights)) | |
21 end | 29 end |
22 | 30 |
23 quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize) | 31 # TODO: Get rid of the padding here. Any padding should be handled by the consturctor accepting the stencils. |
24 eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize)) | 32 quadratureClosure = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize) |
25 dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize)) | 33 eClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize), center=1) |
34 dClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize), center=1) | |
26 | 35 |
27 d2 = D2( | 36 d2 = SbpOperators.D2( |
28 quadratureClosure, | 37 quadratureClosure, |
29 innerStencil, | 38 innerStencil, |
30 closureStencils, | 39 closureStencils, |
31 eClosure, | 40 eClosure, |
32 dClosure, | 41 dClosure, |
34 ) | 43 ) |
35 | 44 |
36 return d2 | 45 return d2 |
37 end | 46 end |
38 | 47 |
39 function readSectionedFile(filename)::Dict{String, Vector{String}} | |
40 f = open(filename) | |
41 sections = Dict{String, Vector{String}}() | |
42 currentKey = "" | |
43 | 48 |
44 for ln ∈ eachline(f) | 49 """ |
45 if ln == "" || ln[1] == '#' # Skip comments and empty lines | 50 read_stencil(fn, path...; [center]) |
46 continue | |
47 end | |
48 | 51 |
49 if isletter(ln[1]) # Found start of new section | 52 Read a stencil at `path` from the file with name `fn`. |
50 if ~haskey(sections, ln) | 53 If a center is specified the given element of the stecil is set as the center. |
51 sections[ln] = Vector{String}() | |
52 end | |
53 currentKey = ln | |
54 continue | |
55 end | |
56 | 54 |
57 push!(sections[currentKey], ln) | 55 See also: [`read_stencils`](@ref), [`read_tuple`](@ref), [`get_stencil`](@ref). |
56 | |
57 # Examples | |
58 ``` | |
59 read_stencil(sbp_operators_path()*"standard_diagonal.toml", "order2", "D2", "inner_stencil") | |
60 read_stencil(sbp_operators_path()*"standard_diagonal.toml", "order2", "d1", "closure"; center=1) | |
61 ``` | |
62 """ | |
63 read_stencil(fn, path...; center=nothing) = get_stencil(TOML.parsefile(fn), path...; center=center) | |
64 | |
65 """ | |
66 read_stencils(fn, path...; centers) | |
67 | |
68 Read stencils at `path` from the file `fn`. | |
69 Centers of the stencils are specified as a tuple or array in `centers`. | |
70 | |
71 See also: [`read_stencil`](@ref), [`read_tuple`](@ref), [`get_stencils`](@ref). | |
72 """ | |
73 read_stencils(fn, path...; centers) = get_stencils(TOML.parsefile(fn), path...; centers=centers) | |
74 | |
75 """ | |
76 read_tuple(fn, path...) | |
77 | |
78 Read tuple at `path` from the file `fn`. | |
79 | |
80 See also: [`read_stencil`](@ref), [`read_stencils`](@ref), [`get_tuple`](@ref). | |
81 """ | |
82 read_tuple(fn, path...) = get_tuple(TOML.parsefile(fn), path...) | |
83 | |
84 """ | |
85 get_stencil(parsed_toml, path...; center=nothing) | |
86 | |
87 Same as [`read_stencil`](@ref)) but takes already parsed toml. | |
88 """ | |
89 get_stencil(parsed_toml, path...; center=nothing) = get_stencil(parsed_toml[path[1]], path[2:end]...; center=center) | |
90 function get_stencil(parsed_toml; center=nothing) | |
91 @assert parsed_toml isa Vector{String} | |
92 stencil_weights = Float64.(parse_rational.(parsed_toml)) | |
93 | |
94 width = length(stencil_weights) | |
95 | |
96 if isnothing(center) | |
97 center = div(width,2)+1 | |
58 end | 98 end |
59 | 99 |
60 return sections | 100 return Stencil(Tuple(stencil_weights), center=center) |
61 end | 101 end |
62 | 102 |
63 function stringToTuple(T::DataType, s::String) | 103 """ |
64 return Tuple(stringToVector(T,s)) | 104 get_stencils(parsed_toml, path...; centers) |
105 | |
106 Same as [`read_stencils`](@ref)) but takes already parsed toml. | |
107 """ | |
108 get_stencils(parsed_toml, path...; centers) = get_stencils(parsed_toml[path[1]], path[2:end]...; centers=centers) | |
109 function get_stencils(parsed_toml; centers) | |
110 @assert parsed_toml isa Vector{Vector{String}} | |
111 @assert length(centers) == length(parsed_toml) | |
112 | |
113 stencils = () | |
114 for i ∈ 1:length(parsed_toml) | |
115 stencil = get_stencil(parsed_toml[i], center = centers[i]) | |
116 stencils = (stencils..., stencil) | |
117 end | |
118 | |
119 return stencils | |
65 end | 120 end |
66 | 121 |
67 function stringToVector(T::DataType, s::String) | 122 """ |
68 return T.(eval.(Meta.parse.(split(s)))) | 123 get_tuple(parsed_toml, path...) |
124 | |
125 Same as [`read_tuple`](@ref)) but takes already parsed toml. | |
126 """ | |
127 get_tuple(parsed_toml, path...) = get_tuple(parsed_toml[path[1]], path[2:end]...) | |
128 function get_tuple(parsed_toml) | |
129 @assert parsed_toml isa Vector{String} | |
130 t = Tuple(Float64.(parse_rational.(parsed_toml))) | |
131 return t | |
132 end | |
133 | |
134 # TODO: Probably should be deleted once we have gotten rid of read_D2_operator() | |
135 function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T | |
136 return Tuple(T.(parse_rational.(arr))) | |
137 end | |
138 | |
139 function parse_rational(str) | |
140 expr = Meta.parse(replace(str, "/"=>"//")) | |
141 return eval(:(Rational($expr))) | |
69 end | 142 end |
70 | 143 |
71 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T} | 144 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T} |
72 if N >= n | 145 if N >= n |
73 return t | 146 return t |