comparison SbpOperators/src/SbpOperators.jl @ 231:fbabfd4e8f20

Merge in boundary_conditions
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 26 Jun 2019 15:07:47 +0200
parents 235f0a771c8f
children d119dfdd749c
comparison
equal deleted inserted replaced
144:ce56727e4232 231:fbabfd4e8f20
1 module SbpOperators
2
3 using RegionIndices
4
5 include("stencil.jl")
6
7 export D2, closureSize, apply, readOperator, apply_e, apply_d
8
9 abstract type ConstantStencilOperator end
10
11 # Apply for different regions Lower/Interior/Upper or Unknown region
12 @inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, i::Index{Lower})
13 return @inbounds h*h*apply(op.closureStencils[Int(i)], v, Int(i))
14 end
15
16 @inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, i::Index{Interior})
17 return @inbounds h*h*apply(op.innerStencil, v, Int(i))
18 end
19
20 @inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, i::Index{Upper})
21 N = length(v)
22 return @inbounds h*h*Int(op.parity)*apply_backwards(op.closureStencils[N-Int(i)+1], v, Int(i))
23 end
24
25 @inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, index::Index{Unknown})
26 cSize = closureSize(op)
27 N = length(v)
28
29 i = Int(index)
30
31 if 0 < i <= cSize
32 return apply(op, h, v, Index{Lower}(i))
33 elseif cSize < i <= N-cSize
34 return apply(op, h, v, Index{Interior}(i))
35 elseif N-cSize < i <= N
36 return apply(op, h, v, Index{Upper}(i))
37 else
38 error("Bounds error") # TODO: Make this more standard
39 end
40 end
41
42
43 # Wrapper functions for using regular indecies without specifying regions
44 @inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, i::Int)
45 return apply(op, h, v, Index{Unknown}(i))
46 end
47
48 @enum Parity begin
49 odd = -1
50 even = 1
51 end
52
53 struct D2{T,N,M,K} <: ConstantStencilOperator
54 quadratureClosure::NTuple{M,T}
55 innerStencil::Stencil{T,N}
56 closureStencils::NTuple{M,Stencil{T,K}}
57 eClosure::Stencil{T,M}
58 dClosure::Stencil{T,M}
59 parity::Parity
60 end
61
62 function closureSize(D::D2)::Int
63 return length(D.quadratureClosure)
64 end
65
66 function readOperator(D2fn, Hfn)
67 d = readSectionedFile(D2fn)
68 h = readSectionedFile(Hfn)
69
70 # Create inner stencil
71 innerStencilWeights = stringToTuple(Float64, d["inner_stencil"][1])
72 width = length(innerStencilWeights)
73 r = (-div(width,2), div(width,2))
74
75 innerStencil = Stencil(r, innerStencilWeights)
76
77 # Create boundary stencils
78 boundarySize = length(d["boundary_stencils"])
79 closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
80
81 for i ∈ 1:boundarySize
82 stencilWeights = stringToTuple(Float64, d["boundary_stencils"][i])
83 width = length(stencilWeights)
84 r = (1-i,width-i)
85 closureStencils = (closureStencils..., Stencil(r, stencilWeights))
86 end
87
88 quadratureClosure = pad_tuple(stringToTuple(Float64, h["closure"][1]), boundarySize)
89 eClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["e"][1]), boundarySize))
90 dClosure = Stencil((0,boundarySize-1), pad_tuple(stringToTuple(Float64, d["d1"][1]), boundarySize))
91
92 d2 = D2(
93 quadratureClosure,
94 innerStencil,
95 closureStencils,
96 eClosure,
97 dClosure,
98 even
99 )
100
101 return d2
102 end
103
104
105 function apply_e(op::D2, v::AbstractVector, ::Type{Lower})
106 apply(op.eClosure,v,1)
107 end
108
109 function apply_e(op::D2, v::AbstractVector, ::Type{Upper})
110 apply(flip(op.eClosure),v,length(v))
111 end
112
113
114 function apply_d(op::D2, h_inv::Real, v::AbstractVector, ::Type{Lower})
115 -h_inv*apply(op.dClosure,v,1)
116 end
117
118 function apply_d(op::D2, h_inv::Real, v::AbstractVector, ::Type{Upper})
119 -h_inv*apply(flip(op.dClosure),v,length(v))
120 end
121
122 function readSectionedFile(filename)::Dict{String, Vector{String}}
123 f = open(filename)
124 sections = Dict{String, Vector{String}}()
125 currentKey = ""
126
127 for ln ∈ eachline(f)
128 if ln == "" || ln[1] == '#' # Skip comments and empty lines
129 continue
130 end
131
132 if isletter(ln[1]) # Found start of new section
133 if ~haskey(sections, ln)
134 sections[ln] = Vector{String}()
135 end
136 currentKey = ln
137 continue
138 end
139
140 push!(sections[currentKey], ln)
141 end
142
143 return sections
144 end
145
146 function stringToTuple(T::DataType, s::String)
147 return Tuple(stringToVector(T,s))
148 end
149
150 function stringToVector(T::DataType, s::String)
151 return T.(eval.(Meta.parse.(split(s))))
152 end
153
154
155 function pad_tuple(t::NTuple{N, T}, n::Integer) where {N,T}
156 if N >= n
157 return t
158 else
159 return pad_tuple((t..., zero(T)), n)
160 end
161 end
162
163 end # module