Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interpolant evaluation error #119

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 21 additions & 48 deletions src/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
cache
end

function DiffEqBase.interp_summary(interp::MIRKInterpolation)
return "MIRK Order $(interp.cache.order) Interpolation"

Check warning on line 8 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L7-L8

Added lines #L7 - L8 were not covered by tests
end

function (id::MIRKInterpolation)(tvals, idxs, deriv, p, continuity::Symbol = :left)
interpolation(tvals, id, idxs, deriv, p, continuity)
end
Expand All @@ -12,15 +16,11 @@
interpolation!(val, tvals, id, idxs, deriv, p, continuity)
end

@inline function interpolation(tvals,
id::I,
idxs,
deriv::D,
p,
# FIXME: Fix the interpolation outside the tspan

@inline function interpolation(tvals, id::I, idxs, deriv::D, p,

Check warning on line 21 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L21

Added line #L21 was not covered by tests
continuity::Symbol = :left) where {I, D}
t = id.t
u = id.u
cache = id.cache
@unpack t, u, cache = id

Check warning on line 23 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L23

Added line #L23 was not covered by tests
tdir = sign(t[end] - t[1])
idx = sortperm(tvals, rev = tdir < 0)

Expand All @@ -33,56 +33,29 @@
end

for j in idx
tval = tvals[j]
i = interval(t, tval)
dt = t[i + 1] - t[i]
θ = (tval - t[i]) / dt
weights, _ = interp_weights(θ, cache.alg)
z = zeros(cache.M)
sum_stages!(z, cache, weights, i)
vals[j] = copy(z)
z = similar(cache.fᵢ₂_cache)
interp_eval!(z, id.cache, tvals[j], id.cache.mesh, id.cache.mesh_dt)
vals[j] = z

Check warning on line 38 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L36-L38

Added lines #L36 - L38 were not covered by tests
end
DiffEqArray(vals, tvals)
return DiffEqArray(vals, tvals)

Check warning on line 40 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L40

Added line #L40 was not covered by tests
end

@inline function interpolation!(vals,
tvals,
id::I,
idxs,
deriv::D,
p,
@inline function interpolation!(vals, tvals, id::I, idxs, deriv::D, p,

Check warning on line 43 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L43

Added line #L43 was not covered by tests
continuity::Symbol = :left) where {I, D}
t = id.t
cache = id.cache
@unpack t, cache = id

Check warning on line 45 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L45

Added line #L45 was not covered by tests
tdir = sign(t[end] - t[1])
idx = sortperm(tvals, rev = tdir < 0)

for j in idx
tval = tvals[j]
i = interval(t, tval)
dt = t[i] - t[i - 1]
θ = (tval - t[i]) / dt
weights, _ = interp_weights(θ, cache.alg)
z = zeros(cache.M)
sum_stages!(z, cache, weights, i)
vals[j] = copy(z)
z = similar(cache.fᵢ₂_cache)
interp_eval!(z, id.cache, tvals[j], id.cache.mesh, id.cache.mesh_dt)
vals[j] = z

Check warning on line 52 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L50-L52

Added lines #L50 - L52 were not covered by tests
end
end

@inline function interpolation(tval::Number,
id::I,
idxs,
deriv::D,
p,
@inline function interpolation(tval::Number, id::I, idxs, deriv::D, p,
continuity::Symbol = :left) where {I, D}
t = id.t
cache = id.cache
i = interval(t, tval)
dt = t[i] - t[i - 1]
θ = (tval - t[i]) / dt
weights, _ = interp_weights(θ, cache.alg)
z = zeros(cache.M)
sum_stages!(z, cache, weights, i)
val = copy(z)
val
z = similar(id.cache.fᵢ₂_cache)
interp_eval!(z, id.cache, tval, id.cache.mesh, id.cache.mesh_dt)
return z
end
33 changes: 33 additions & 0 deletions test/interpolation_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BoundaryValueDiffEq, DiffEqBase, DiffEqDevTools, LinearAlgebra, Test

λ = 1
function prob_bvp_linear_analytic(u, λ, t)
a = 1 / sqrt(λ)
[(exp(-a * t) - exp((t - 2) * a)) / (1 - exp(-2 * a)),
(-a * exp(-t * a) - a * exp((t - 2) * a)) / (1 - exp(-2 * a))]
end
function prob_bvp_linear_f!(du, u, p, t)
du[1] = u[2]
du[2] = 1 / p * u[1]
end
function prob_bvp_linear_bc!(res, u, p, t)
res[1] = u[1][1] - 1
res[2] = u[end][1]
end
prob_bvp_linear_function = ODEFunction(prob_bvp_linear_f!, analytic = prob_bvp_linear_analytic)
prob_bvp_linear_tspan = (0.0, 1.0)
prob_bvp_linear = BVProblem(prob_bvp_linear_function, prob_bvp_linear_bc!,
[1.0, 0.0], prob_bvp_linear_tspan, λ)
testTol = 1e-6

for order in (2, 3, 4, 5, 6)
s = Symbol("MIRK$(order)")
@eval mirk_solver(::Val{$order}) = $(s)()
end

@testset "Interpolation" begin
@testset "MIRK$order" for order in (2, 3, 4, 5, 6)
@time sol = solve(prob_bvp_linear, mirk_solver(Val(order)); dt = 0.001)
@test sol(0.001) ≈ [0.998687464, -1.312035941] atol=testTol
end
end
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ using Test, SafeTestsets
include("non_vector_inputs.jl")
end
end

@time @testset "Interpolation Tests" begin
@time @safetestset "MIRK Interpolation Test" begin
include("interpolation_test.jl")
end
end
end
Loading