annotate src/SbpOperators/readoperator.jl @ 596:a9744aa5e235 refactor/toml_operator_format

Simplify read_D2_operator by introducing a get_stencil function
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 02 Dec 2020 17:12:04 +0100
parents cc86b920531a
children 98cd99237176
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
1 using TOML
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
2 function read_D2_operator(fn; order)
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
3 operators = TOML.parsefile(fn)["order$order"]
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
4 D2 = operators["D2"]
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
5 H = operators["H"]
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
6 e = operators["e"]
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
7 d1 = operators["d1"]
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
8
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
9 # Create inner stencil
596
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
10 innerStencil = get_stencil(operators, "D2", "inner_stencil")
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
11
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
12 # Create boundary stencils
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
13 boundarySize = length(D2["closure_stencils"])
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
14 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
15
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
16 for i ∈ 1:boundarySize
596
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
17 closureStencils = (closureStencils..., get_stencil(operators, "D2", "closure_stencils", i; center=i))
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
18 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
19
596
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
20 # TODO: Get rid of the padding here. Any padding should be handled by the consturctor accepting the stencils.
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
21 quadratureClosure = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize)
596
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
22 eClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize), center=1)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
23 dClosure = Stencil(pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize), center=1)
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
24
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
25 d2 = SbpOperators.D2(
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
26 quadratureClosure,
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
27 innerStencil,
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
28 closureStencils,
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
29 eClosure,
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
30 dClosure,
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
31 even
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
32 )
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
33
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
34 return d2
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
35 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
36
596
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
37 function get_stencil(parsed_toml, path...; center=nothing)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
38 if length(path) == 0
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
39 @assert parsed_toml isa Vector
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
40 stencil_weights = Float64.(parse_rational.(parsed_toml))
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
41
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
42 width = length(stencil_weights)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
43
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
44 if isnothing(center)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
45 center = div(width,2)+1
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
46 end
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
47
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
48 return Stencil(Tuple(stencil_weights), center=center)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
49 end
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
50
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
51 return get_stencil(parsed_toml[path[1]], path[2:end]...; center=center)
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
52 end
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
53
a9744aa5e235 Simplify read_D2_operator by introducing a get_stencil function
Jonatan Werpers <jonatan@werpers.com>
parents: 594
diff changeset
54 # TODO: Probably should be deleted once we have gotten rid of read_D2_operator()
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
55 function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
56 return Tuple(T.(parse_rational.(arr)))
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
57 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
58
594
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
59 function parse_rational(str)
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
60 expr = Meta.parse(replace(str, "/"=>"//"))
cc86b920531a Change the readoperator function to use the .toml format
Jonatan Werpers <jonatan@werpers.com>
parents: 340
diff changeset
61 return eval(:(Rational($expr)))
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
62 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
63
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
64 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T}
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
65 if N >= n
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
66 return t
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
67 else
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
68 return pad_tuple((t..., zero(T)), n)
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
69 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
70 end
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
71
340
29df39a7b568 readoperator.jl: Fix path to operators
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
72 sbp_operators_path() = (@__DIR__) * "/operators/"
249
7cb4492ccd60 Refactor package SbpOperators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
73 export sbp_operators_path