Mercurial > repos > public > sbplib_julia
changeset 594:cc86b920531a refactor/toml_operator_format
Change the readoperator function to use the .toml format
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 02 Dec 2020 15:26:13 +0100 |
parents | fa03dae0ff0b |
children | 03ef4d4740ab |
files | src/SbpOperators/d2.jl src/SbpOperators/readoperator.jl test/testDiffOps.jl test/testSbpOperators.jl |
diffstat | 4 files changed, 34 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/src/SbpOperators/d2.jl Wed Dec 02 15:24:56 2020 +0100 +++ b/src/SbpOperators/d2.jl Wed Dec 02 15:26:13 2020 +0100 @@ -1,4 +1,4 @@ -export D2, closuresize, readOperator +export D2, closuresize, read_D2_operator @enum Parity begin odd = -1
--- a/src/SbpOperators/readoperator.jl Wed Dec 02 15:24:56 2020 +0100 +++ b/src/SbpOperators/readoperator.jl Wed Dec 02 15:26:13 2020 +0100 @@ -1,30 +1,35 @@ -function readOperator(D2fn, Hfn) - d = readSectionedFile(D2fn) - h = readSectionedFile(Hfn) +using TOML +function read_D2_operator(fn; order) + operators = TOML.parsefile(fn)["order$order"] + D2 = operators["D2"] + H = operators["H"] + e = operators["e"] + d1 = operators["d1"] # Create inner stencil - innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1]) + innerStencilWeights = toml_string_array_to_tuple(Float64, D2["inner_stencil"]) + width = length(innerStencilWeights) r = (-div(width,2), div(width,2)) innerStencil = Stencil(r, innerStencilWeights) # Create boundary stencils - boundarySize = length(d["boundary_stencils"]) + boundarySize = length(D2["closure_stencils"]) closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type? for i ā 1:boundarySize - stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i]) + stencilWeights = toml_string_array_to_tuple(Float64, D2["closure_stencils"][i]) width = length(stencilWeights) r = (1-i,width-i) closureStencils = (closureStencils..., Stencil(r, stencilWeights)) end - quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize) - eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize)) - dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize)) + quadratureClosure = pad_tuple(toml_string_array_to_tuple(Float64, H["closure"]), boundarySize) + eClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, e["closure"]), boundarySize)) + dClosure = Stencil((0,boundarySize-1), pad_tuple(toml_string_array_to_tuple(Float64, d1["closure"]), boundarySize)) - d2 = D2( + d2 = SbpOperators.D2( quadratureClosure, innerStencil, closureStencils, @@ -36,36 +41,13 @@ return d2 end -function readSectionedFile(filename)::Dict{String, Vector{String}} - f = open(filename) - sections = Dict{String, Vector{String}}() - currentKey = "" - - for ln ā eachline(f) - if ln == "" || ln[1] == '#' # Skip comments and empty lines - continue - end - - if isletter(ln[1]) # Found start of new section - if ~haskey(sections, ln) - sections[ln] = Vector{String}() - end - currentKey = ln - continue - end - - push!(sections[currentKey], ln) - end - - return sections +function toml_string_array_to_tuple(::Type{T}, arr::AbstractVector{String}) where T + return Tuple(T.(parse_rational.(arr))) end -function stringToTuple(T::DataType, s::String) - return Tuple(stringToVector(T,s)) -end - -function stringToVector(T::DataType, s::String) - return T.(eval.(Meta.parse.(split(s)))) +function parse_rational(str) + expr = Meta.parse(replace(str, "/"=>"//")) + return eval(:(Rational($expr))) end function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T}
--- a/test/testDiffOps.jl Wed Dec 02 15:24:56 2020 +0100 +++ b/test/testDiffOps.jl Wed Dec 02 15:26:13 2020 +0100 @@ -8,7 +8,7 @@ @testset "DiffOps" begin # # @testset "BoundaryValue" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) # # e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}()) @@ -69,7 +69,7 @@ # end # # @testset "NormalDerivative" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) # # d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) @@ -146,7 +146,7 @@ # end # # @testset "BoundaryQuadrature" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) # # H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}())
--- a/test/testSbpOperators.jl Wed Dec 02 15:24:56 2020 +0100 +++ b/test/testSbpOperators.jl Wed Dec 02 15:26:13 2020 +0100 @@ -16,7 +16,7 @@ end # @testset "apply_quadrature" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # h = 0.5 # # @test apply_quadrature(op, h, 1.0, 10, 100) == h @@ -37,7 +37,7 @@ # end @testset "SecondDerivative" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) L = 3.5 g = EquidistantGrid(101, 0.0, L) Dāā = SecondDerivative(g,op.innerStencil,op.closureStencils) @@ -77,7 +77,7 @@ @testset "Laplace2D" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) Lx = 1.5 Ly = 3.2 g = EquidistantGrid((102,131), (0.0, 0.0), (Lx,Ly)) @@ -119,7 +119,7 @@ end @testset "DiagonalInnerProduct" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) L = 2.3 g = EquidistantGrid(77, 0.0, L) H = DiagonalInnerProduct(g,op.quadratureClosure) @@ -132,7 +132,7 @@ end @testset "Quadrature" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) Lx = 2.3 Ly = 5.2 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) @@ -152,7 +152,7 @@ end @testset "InverseDiagonalInnerProduct" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) L = 2.3 g = EquidistantGrid(77, 0.0, L) H = DiagonalInnerProduct(g, op.quadratureClosure) @@ -166,7 +166,7 @@ end @testset "InverseQuadrature" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) Lx = 7.3 Ly = 8.2 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) @@ -182,7 +182,7 @@ end @testset "BoundaryRestrictrion" begin - op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) g_1D = EquidistantGrid(11, 0.0, 1.0) g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) @@ -320,7 +320,7 @@ end # # @testset "NormalDerivative" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) # # d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) @@ -397,7 +397,7 @@ # end # # @testset "BoundaryQuadrature" begin -# op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) # g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) # # H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}())