comparison src/SbpOperators/readoperator.jl @ 768:7c87a33963c5 operator_storage_array_of_table

Add some notes, delete functions that won't be needed
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 14 Jul 2021 23:40:10 +0200
parents 210d3f58bd56
children d0c1d0b4da52
comparison
equal deleted inserted replaced
767:210d3f58bd56 768:7c87a33963c5
3 export read_stencil_set 3 export read_stencil_set
4 export get_stencil_set 4 export get_stencil_set
5 5
6 export parse_stencil 6 export parse_stencil
7 7
8 export read_D2_operator 8 # The read_stencil_set and get_stencil_set functions return the freshly parsed
9 export read_stencil 9 # toml. The generic code in these functions can't be expected to know anyhting
10 export read_stencils 10 # about how to read different stencil sets as they may contain many different
11 export read_tuple 11 # kinds of stecils. We should how ever add read_ and get_ functions for all
12 # the types of stencils we know about.
13 #
14 # After getting a stencil set the user can use parse functions to parse what
15 # they want from the TOML dict. I.e no more "paths".
16 # Functions needed:
17 # * parse stencil
18 # * parse rational
19 #
20 # maybe there is a better name than parse?
21 # Would be nice to be able to control the type in the stencil
12 22
13 export get_stencil 23 # TODO: Control type for the stencil
14 export get_stencils 24 # TODO: Think about naming and terminology around freshly parsed TOML.
15 export get_tuple
16
17 function read_D2_operator(fn; order)
18 operators = TOML.parsefile(fn)["order$order"]
19 D2 = operators["D2"]
20 H = operators["H"]
21 e = operators["e"]
22 d1 = operators["d1"]
23
24 # Create inner stencil
25 innerStencil = get_stencil(operators, "D2", "inner_stencil")
26
27 # Create boundary stencils
28 boundarySize = length(D2["closure_stencils"])
29 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
30 for i ∈ 1:boundarySize
31 closureStencils = (closureStencils..., get_stencil(operators, "D2", "closure_stencils", i; center=i))
32 end
33 # TODO: Get rid of the padding here. Any padding should be handled by the consturctor accepting the stencils.
34 eClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize)..., center=1)
35 dClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize)..., center=1)
36
37 q_tuple = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize)
38 quadratureClosure = Vector{typeof(innerStencil)}()
39 for i ∈ 1:boundarySize
40 quadratureClosure = (quadratureClosure..., Stencil(q_tuple[i], center=1))
41 end
42
43 d2 = SbpOperators.D2(
44 innerStencil,
45 closureStencils,
46 eClosure,
47 dClosure,
48 quadratureClosure,
49 even
50 )
51
52 return d2
53 end
54 25
55 """ 26 """
56 read_stencil_set(fn; filters) 27 read_stencil_set(fn; filters)
57 28
58 Picks out a stencil set from the given toml file based on some filters. 29 Picks out a stencil set from the given toml file based on some filters.
85 56
86 i = matches[1] 57 i = matches[1]
87 return parsed_toml["stencil_set"][i] 58 return parsed_toml["stencil_set"][i]
88 end 59 end
89 60
61 """
62 parse_stencil(toml)
63
64 Accepts parsed toml and reads it as a stencil
65 """
90 function parse_stencil(toml) 66 function parse_stencil(toml)
91 check_stencil_toml(toml) 67 check_stencil_toml(toml)
92 68
93 if toml isa Array 69 if toml isa Array
94 weights = Float64.(parse_rational.(toml)) 70 weights = Float64.(parse_rational.(toml))
119 if !(toml["c"] isa Int) 95 if !(toml["c"] isa Int)
120 throw(ArgumentError("the center of a stencil must be specified as an integer.")) 96 throw(ArgumentError("the center of a stencil must be specified as an integer."))
121 end 97 end
122 end 98 end
123 99
124 """
125 read_stencil(fn, path...; [center])
126
127 Read a stencil at `path` from the file with name `fn`.
128 If a center is specified the given element of the stecil is set as the center.
129
130 See also: [`read_stencils`](@ref), [`read_tuple`](@ref), [`get_stencil`](@ref).
131
132 # Examples
133 ```
134 read_stencil(sbp_operators_path()*"standard_diagonal.toml", "order2", "D2", "inner_stencil")
135 read_stencil(sbp_operators_path()*"standard_diagonal.toml", "order2", "d1", "closure"; center=1)
136 ```
137 """
138 read_stencil(fn, path...; center=nothing) = get_stencil(TOML.parsefile(fn), path...; center=center)
139
140 """
141 read_stencils(fn, path...; centers)
142
143 Read stencils at `path` from the file `fn`.
144 Centers of the stencils are specified as a tuple or array in `centers`.
145
146 See also: [`read_stencil`](@ref), [`read_tuple`](@ref), [`get_stencils`](@ref).
147 """
148 read_stencils(fn, path...; centers) = get_stencils(TOML.parsefile(fn), path...; centers=centers)
149
150 """
151 read_tuple(fn, path...)
152
153 Read tuple at `path` from the file `fn`.
154
155 See also: [`read_stencil`](@ref), [`read_stencils`](@ref), [`get_tuple`](@ref).
156 """
157 read_tuple(fn, path...) = get_tuple(TOML.parsefile(fn), path...)
158
159 """
160 get_stencil(parsed_toml, path...; center=nothing)
161
162 Same as [`read_stencil`](@ref)) but takes already parsed toml.
163 """
164 get_stencil(parsed_toml, path...; center=nothing) = get_stencil(parsed_toml[path[1]], path[2:end]...; center=center)
165 function get_stencil(parsed_toml; center=nothing)
166 @assert parsed_toml isa Vector{String}
167 stencil_weights = Float64.(parse_rational.(parsed_toml))
168
169 width = length(stencil_weights)
170
171 if isnothing(center)
172 center = div(width,2)+1
173 end
174
175 return Stencil(stencil_weights..., center=center)
176 end
177
178 """
179 get_stencils(parsed_toml, path...; centers)
180
181 Same as [`read_stencils`](@ref)) but takes already parsed toml.
182 """
183 get_stencils(parsed_toml, path...; centers) = get_stencils(parsed_toml[path[1]], path[2:end]...; centers=centers)
184 function get_stencils(parsed_toml; centers)
185 @assert parsed_toml isa Vector{Vector{String}}
186 @assert length(centers) == length(parsed_toml)
187
188 stencils = ()
189 for i ∈ 1:length(parsed_toml)
190 stencil = get_stencil(parsed_toml[i], center = centers[i])
191 stencils = (stencils..., stencil)
192 end
193
194 return stencils
195 end
196
197 """
198 get_tuple(parsed_toml, path...)
199
200 Same as [`read_tuple`](@ref)) but takes already parsed toml.
201 """
202 get_tuple(parsed_toml, path...) = get_tuple(parsed_toml[path[1]], path[2:end]...)
203 function get_tuple(parsed_toml)
204 @assert parsed_toml isa Vector{String}
205 t = Tuple(Float64.(parse_rational.(parsed_toml)))
206 return t
207 end
208
209 function get_rationals()
210 end
211
212 # TODO: Probably should be deleted once we have gotten rid of read_D2_operator()
213 function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T
214 return Tuple(T.(parse_rational.(arr)))
215 end
216
217 function parse_rational(str) 100 function parse_rational(str)
218 expr = Meta.parse(replace(str, "/"=>"//")) 101 expr = Meta.parse(replace(str, "/"=>"//"))
219 return eval(:(Rational($expr))) 102 return eval(:(Rational($expr)))
220 end 103 end
221 104