Skip to content

Commit 787ac62

Browse files
author
Tomas Lycken
committed
First attempt at linear bc/extrap for B2
1 parent 1d03a0b commit 787ac62

File tree

3 files changed

+88
-17
lines changed

3 files changed

+88
-17
lines changed

src/Interpolations.jl

+22-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export
1212
ExtrapError,
1313
ExtrapNaN,
1414
ExtrapConstant,
15+
ExtrapLinear,
1516
OnCell,
1617
OnGrid,
1718
ExtendInner,
18-
Flat
19+
Flat,
20+
LinearBC
1921

2022
abstract Degree{N}
2123

@@ -27,6 +29,7 @@ abstract BoundaryCondition
2729
type None <: BoundaryCondition end
2830
type ExtendInner <: BoundaryCondition end
2931
type Flat <: BoundaryCondition end
32+
type LinearBC <: BoundaryCondition end
3033

3134
abstract InterpolationType{D<:Degree,BC<:BoundaryCondition,GR<:GridRepresentation}
3235

@@ -66,9 +69,19 @@ include("quadratic.jl")
6669

6770

6871
# This creates getindex methods for all supported combinations
69-
for IT in (Constant{OnCell},Linear{OnGrid},Quadratic{ExtendInner,OnCell},Quadratic{Flat,OnCell})
70-
for EB in (ExtrapError,ExtrapNaN,ExtrapConstant)
71-
72+
for IT in (
73+
Constant{OnCell},
74+
Linear{OnGrid},
75+
Quadratic{ExtendInner,OnCell},
76+
Quadratic{Flat,OnCell},
77+
Quadratic{LinearBC,OnCell}
78+
)
79+
for EB in (
80+
ExtrapError,
81+
ExtrapNaN,
82+
ExtrapConstant,
83+
ExtrapLinear
84+
)
7285
eval(:(function getindex{T}(itp::Interpolation{T,1,$IT,$EB}, x::Real, d)
7386
d == 1 || throw(BoundsError())
7487
itp[x]
@@ -107,7 +120,11 @@ for IT in (Constant{OnCell},Linear{OnGrid},Quadratic{ExtendInner,OnCell},Quadrat
107120
end
108121

109122
# This creates prefilter specializations for all interpolation types that need them
110-
for IT in (Quadratic{ExtendInner,OnCell},Quadratic{Flat,OnCell})
123+
for IT in (
124+
Quadratic{ExtendInner,OnCell},
125+
Quadratic{Flat,OnCell},
126+
Quadratic{LinearBC,OnCell}
127+
)
111128
@ngenerate N promote_type_grid(T, x...) function prefilter{T,N}(A::Array{T,N},it::IT)
112129
ret = similar(A)
113130
szs = collect(size(A))

src/extrapolation.jl

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
abstract ExtrapolationBehavior
32
type ExtrapError <: ExtrapolationBehavior end
43

@@ -25,3 +24,26 @@ function extrap_gen(::OnGrid, ::ExtrapConstant, N)
2524
end
2625
end
2726
extrap_gen(::OnCell, e::ExtrapConstant, N) = extrap_gen(OnGrid(), e, N)
27+
28+
type ExtrapLinear <: ExtrapolationBehavior end
29+
30+
function extrap_gen(::OnGrid, ::ExtrapLinear, N)
31+
quote
32+
@nexprs $N d->begin
33+
if x_d < 1
34+
fx_d = x_d - convert(typeof(x_d), 1)
35+
36+
k = itp[1] - itp[2]
37+
return itp[1] - k * fx_d
38+
end
39+
if x_d > size(itp, d)
40+
s_d = size(itp,d)
41+
fx_d = x_d - convert(typeof(x_d), s_d)
42+
43+
k = itp[s_d] - itp[s_d - 1]
44+
return itp[s_d] + k * fx_d
45+
end
46+
end
47+
end
48+
end
49+
extrap_gen(::OnCell, e::ExtrapLinear, N) = extrap_gen(OnGrid(), e, N)

src/quadratic.jl

+43-11
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ function indices(::Quadratic{ExtendInner,OnCell}, N)
2626
end
2727
end
2828

29-
function coefficients(::Quadratic{ExtendInner,OnCell}, N)
30-
quote
31-
@nexprs $N d->begin
32-
cm_d = (fx_d-.5)^2 / 2
33-
c_d = 0.75 - fx_d^2
34-
cp_d = (fx_d+.5)^2 / 2
35-
end
36-
end
37-
end
38-
3929
function bc_gen(::Quadratic{Flat,OnCell}, N)
4030
quote
4131
# After extrapolation has been handled, 1 <= x_d <= size(itp,d)
@@ -63,8 +53,42 @@ function indices(::Quadratic{Flat,OnCell}, N)
6353
end
6454
end
6555

56+
function bc_gen(::Quadratic{LinearBC,OnCell}, N)
57+
quote
58+
@nexprs $N d->begin
59+
if x_d < 1
60+
# extrapolate towards -∞
61+
fx_d = x_d - convert(typeof(x_d), 1)
62+
k = itp[2] - itp[1]
63+
64+
return itp[1] + k * fx_d
65+
end
66+
if x_d > size(itp, d)
67+
# extrapolate towards ∞
68+
s_d = size(itp, d)
69+
fx_d = x_d - convert(typeof(x_d), s_d)
70+
k = itp[s_d] - itp[s_d - 1]
71+
72+
return itp[s_d] + k * fx_d
73+
end
74+
75+
ix_d = iround(x_d)
76+
end
77+
end
78+
end
6679

67-
function coefficients(::Quadratic{Flat,OnCell}, N)
80+
function indices(::Quadratic{LinearBC,OnCell}, N)
81+
quote
82+
@nexprs $N d->begin
83+
ixp_d = ix_d + 1
84+
ixm_d = ix_d - 1
85+
86+
fx_d = x_d - convert(typeof(x_d), ix_d)
87+
end
88+
end
89+
end
90+
91+
function coefficients(::Quadratic, N)
6892
quote
6993
@nexprs $N d->begin
7094
cm_d = (fx_d-.5)^2 / 2
@@ -114,3 +138,11 @@ function prefiltering_system_matrix{T}(::Type{T}, n::Int, q::Quadratic{Flat,OnCe
114138
dl[end] += 1/8
115139
lufact!(Tridiagonal(dl, d, du))
116140
end
141+
142+
function prefiltering_system_matrix{T}(::Type{T}, n::Int, q::Quadratic{LinearBC,OnCell})
143+
dl,d,du = unmodified_system_matrix(T,n,q)
144+
145+
d[1] = d[end] = 1
146+
du[1] = dl[end] = 0
147+
lufact!(Tridiagonal(dl, d, du))
148+
end

0 commit comments

Comments
 (0)