-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tlycken/linear-extrap
More boundary conditions for quadratic B-splines
- Loading branch information
Showing
13 changed files
with
25,462 additions
and
116 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# This assumes integral values ixm_d, ix_d, ixp_d, and ixpp_d (typically ixm_d = ix_d-1, ixp_d = ix_d+1, except at boundaries), | ||
# coefficients cm_d, c_d, cp_d, and cpp_d, and an array itp.coefs | ||
function index_gen(::Type{Cubic}, N::Integer, offsets...) | ||
if length(offsets) < N | ||
d = length(offsets)+1 | ||
symm, sym, symp, sympp = symbol(string("cm_",d)), symbol(string("c_",d)), symbol(string("cp_",d)), symbol(string("cpp_",d)) | ||
return :($symm * $(index_gen(Cubic, N, offsets...,-1)) + $sym * $(index_gen(Cubic, N, offsets..., 0)) + | ||
$symp * $(index_gen(Cubic, N, offsets..., 1)) + $sympp * $(index_gen(Cubic, N, offsets..., 2))) | ||
else | ||
indices = [offsetsym(offsets[d], d) for d = 1:N] | ||
return :(itp.coefs[$(indices...)]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,119 @@ | ||
|
||
abstract ExtrapolationBehavior | ||
type ExtrapError <: ExtrapolationBehavior end | ||
|
||
type ExtrapError <: ExtrapolationBehavior end | ||
function extrap_gen(::OnGrid, ::ExtrapError, N) | ||
quote | ||
@nexprs $N d->(1 <= x_d <= size(itp,d) || throw(BoundsError())) | ||
end | ||
end | ||
extrap_gen(::OnCell, e::ExtrapError, N) = extrap_gen(OnGrid(), e, N) | ||
function extrap_gen(::OnCell, ::ExtrapError, N) | ||
quote | ||
@nexprs $N d->(.5 <= x_d <= size(itp,d) + .5 || throw(BoundsError())) | ||
end | ||
end | ||
|
||
type ExtrapNaN <: ExtrapolationBehavior end | ||
|
||
function extrap_gen(::OnGrid, ::ExtrapNaN, N) | ||
quote | ||
@nexprs $N d->(1 <= x_d <= size(itp,d) || return convert(T, NaN)) | ||
end | ||
end | ||
extrap_gen(::OnCell, e::ExtrapNaN, N) = extrap_gen(OnGrid(), e, N) | ||
function extrap_gen(::OnCell, ::ExtrapNaN, N) | ||
quote | ||
@nexprs $N d->(.5 <= x_d <= size(itp,d) + .5 || return convert(T, NaN)) | ||
end | ||
end | ||
|
||
type ExtrapConstant <: ExtrapolationBehavior end | ||
function extrap_gen(::OnGrid, ::ExtrapConstant, N) | ||
quote | ||
@nexprs $N d->(x_d = clamp(x_d, 1, size(itp,d))) | ||
end | ||
end | ||
extrap_gen(::OnCell, e::ExtrapConstant, N) = extrap_gen(OnGrid(), e, N) | ||
function extrap_gen(::OnCell, ::ExtrapConstant, N) | ||
quote | ||
@nexprs $N d->(x_d = clamp(x_d, .5, size(itp,d)+.5)) | ||
end | ||
end | ||
|
||
type ExtrapReflect <: ExtrapolationBehavior end | ||
function extrap_gen(::OnGrid, ::ExtrapReflect, N) | ||
quote | ||
@nexprs $N d->begin | ||
# translate x_d to inside the domain, and count the translations | ||
ntransl = 0 | ||
while x_d < 1 | ||
x_d += size(itp, d) - 1 | ||
ntransl += 1 | ||
end | ||
while x_d > size(itp, d) | ||
x_d -= size(itp, d) - 1 | ||
ntransl += 1 | ||
end | ||
|
||
# if odd number of translations, also reflect inside the domain | ||
if ntransl > 0 && mod(ntransl, 2) != 0 | ||
x_d = size(itp, d) + 1 - x_d | ||
end | ||
end | ||
end | ||
end | ||
function extrap_gen(::OnCell, ::ExtrapReflect, N) | ||
quote | ||
@nexprs $N d->begin | ||
# translate x_d to inside the domain, and count the translations | ||
ntransl = 0 | ||
while x_d < .5 | ||
x_d += size(itp,d) | ||
ntransl += 1 | ||
end | ||
while x_d > size(itp,d) + .5 | ||
x_d -= size(itp,d) | ||
ntransl += 1 | ||
end | ||
|
||
# if odd number of translations, also reflect inside the domain | ||
if ntransl > 0 && mod(ntransl, 2) != 0 | ||
x_d = size(itp, d) + 1 - x_d | ||
end | ||
end | ||
end | ||
end | ||
|
||
type ExtrapPeriodic <: ExtrapolationBehavior end | ||
function extrap_gen(::GridRepresentation, ::ExtrapPeriodic, N) | ||
quote | ||
@nexprs $N d->begin | ||
# translate x_d to inside the domain | ||
n = convert(typeof(x_d), size(itp,d)) | ||
while x_d < .5 | ||
x_d += n | ||
end | ||
while x_d >= n + .5 | ||
x_d -= n | ||
end | ||
end | ||
end | ||
end | ||
|
||
type ExtrapLinear <: ExtrapolationBehavior end | ||
function extrap_gen(::OnGrid, ::ExtrapLinear, N) | ||
quote | ||
@nexprs $N d->begin | ||
if x_d < 1 | ||
fx_d = x_d - convert(typeof(x_d), 1) | ||
|
||
k = -4*itp.coefs[1] | ||
return itp[1] - k * fx_d | ||
end | ||
if x_d > size(itp, d) | ||
s_d = size(itp,d) | ||
fx_d = x_d - convert(typeof(x_d), s_d) | ||
|
||
k = itp[s_d] - itp[s_d - 1] | ||
return itp[s_d] + k * fx_d | ||
end | ||
end | ||
end | ||
end | ||
extrap_gen(::OnCell, e::ExtrapLinear, N) = extrap_gen(OnGrid(), e, N) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.