Skip to content

Commit

Permalink
Make periodic extrapolation smooth
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Dec 3, 2024
1 parent 77a5875 commit 8395f22
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 0 additions & 7 deletions src/interpolation_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ function _interpolate(A::SmoothedConstantInterpolation{<:AbstractVector}, t::Num
idx = get_idx(A, t, iguess)
d_lower, d_upper, c_lower, c_upper = get_parameters(A, idx)

# Fix extrapolation behavior as constant for now
if t <= first(A.t)
return first(A.u)
elseif t >= last(A.t)
return A.u[end - 1]
end

out = A.u[idx]

if (t - A.t[idx]) < d_lower
Expand Down
6 changes: 3 additions & 3 deletions src/interpolation_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ function get_parameters(A::SmoothedConstantInterpolation, idx)
c_upper = A.p.c[idx + 1]
d_lower, d_upper, c_lower, c_upper
else
d_lower, c_lower = smoothed_linear_interpolation_parameters(A.u, A.t, A.d_max, idx)
d_upper, c_upper = smoothed_linear_interpolation_parameters(
A.u, A.t, A.d_max, idx + 1)
d_lower, c_lower = smoothed_constant_interpolation_parameters(A.u, A.t, A.d_max, idx, A.extrapolation_left, A.extrapolation_right)
d_upper, c_upper = smoothed_constant_interpolation_parameters(
A.u, A.t, A.d_max, idx + 1, A.extrapolation_left, A.extrapolation_right)
d_lower, d_upper, c_lower, c_upper
end
end
Expand Down
16 changes: 12 additions & 4 deletions src/parameter_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

function SmoothedConstantParameterCache(u, t, cache_parameters, d_max)
if cache_parameters
parameters = smoothed_linear_interpolation_parameters.(
parameters = smoothed_constant_interpolation_parameters.(
Ref(u), Ref(t), d_max, eachindex(t))
d, c = collect.(eachrow(stack(collect.(parameters))))
SmoothedConstantParameterCache(d, c)
Expand All @@ -48,10 +48,18 @@ function SmoothedConstantParameterCache(u, t, cache_parameters, d_max)
end
end

function smoothed_linear_interpolation_parameters(u, t, d_max, idx)
# TODO: Add support for making periodic extrapolation smooth
function smoothed_constant_interpolation_parameters(u, t, d_max, idx, extrapolation_left, extrapolation_right)
if isone(idx) || (idx == length(t))
zero(one(eltype(t))) / 2, zero(one(eltype(u)) / 2)
# If extrapolation is periodic, make the transition differentiable
if extrapolation_left == extrapolation_right == ExtrapolationType.Periodic
if isone(idx)
min(t[end] - t[end - 1], t[2] - t[1], 2d_max) / 2, (u[1] - u[end - 1]) / 2
else
min(t[end] - t[end - 1], t[2] - t[1], 2d_max) / 2, (u[1] - u[end - 1]) / 2
end
else
zero(one(eltype(t)) / 2), zero(one(eltype(u)) / 2)
end
else
min(t[idx] - t[idx - 1], t[idx + 1] - t[idx], 2d_max) / 2, (u[idx] - u[idx - 1]) / 2
end
Expand Down

0 comments on commit 8395f22

Please sign in to comment.