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

Add differentiation tests #139

Merged
merged 2 commits into from
Jul 30, 2019
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ Unitful = "≥ 0.7.1"
julia = "≥ 1.0.0"

[extras]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d"
DiffEqProblemLibrary = "a077e3f3-b75c-5d7f-a0c6-6bc4c8ec64a9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["DiffEqCallbacks", "DiffEqDevTools", "DiffEqProblemLibrary", "Random", "RecursiveArrayTools", "SafeTestsets", "Test", "Unitful"]
test = ["Calculus", "DiffEqCallbacks", "DiffEqDevTools", "DiffEqProblemLibrary", "LinearAlgebra", "Random", "RecursiveArrayTools", "SafeTestsets", "Test", "Unitful"]
56 changes: 56 additions & 0 deletions test/differentiation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using DelayDiffEq, Calculus, ForwardDiff
using LinearAlgebra, Test

# Hutchinson's equation
function f(du, u, h, p, t)
du[1] = p[1] * u[1] * (1 - h(p, t - p[2])[1] / p[3])
nothing
end

h(p, t) = [p[4]]

@testset "Gradient" begin
function test(p)
prob = DDEProblem(f, [p[5]], h, eltype(p).((0.0, 20.0)), copy(p);
constant_lags = [p[2]])
sol = solve(prob, MethodOfSteps(Tsit5()); abstol = 1e-14, reltol = 1e-14)
diff = @. sol[1, :] - 10 * exp(-sol.t)
dot(diff, diff)
end

p = [1.0, 1.0, 1.0, 0.0, 1.0]
findiff = Calculus.finite_difference(test, p)
fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.gradient(test, p)

@test_broken findiff ≈ fordiff
end

@testset "Jacobian" begin
function test(p)
prob = DDEProblem(f, [p[5]], h, eltype(p).((0.0, 20.0)), copy(p);
constant_lags = [p[2]])
solve(prob, MethodOfSteps(Tsit5()); abstol = 1e-14, reltol = 1e-14).u[end]
end

p = [1.0, 1.0, 1.0, 0.0, 1.0]
findiff = Calculus.finite_difference_jacobian(test, p)
fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.jacobian(test, p)

@test_broken findiff ≈ fordiff
end

@testset "Hessian" begin
function test(p)
prob = DDEProblem(f, [p[5]], h, eltype(p).((0.0, 20.0)), copy(p);
constant_lags = [p[2]])
sol = solve(prob, MethodOfSteps(Tsit5()); abstol = 1e-14, reltol = 1e-14)
diff = @. sol[1, :] - 10 * exp(-sol.t)
dot(diff, diff)
end

p = [1.0, 1.0, 1.0, 0.0, 1.0]
findiff = Calculus.finite_difference_hessian(test, p)
fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.hessian(test, p)

@test_broken findiff ≈ fordiff
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if GROUP == "All" || GROUP == "Interface"
@time @safetestset "saveat Tests" begin include("saveat.jl") end
@time @safetestset "save_idxs Tests" begin include("save_idxs.jl") end
@time @safetestset "Event Tests" begin include("events.jl") end
@time @safetestset "Differentiation Tests" begin include("differentiation.jl") end
@time @safetestset "Cache Tests" begin include("cache.jl") end
@time @safetestset "Iterator Tests" begin include("iterator.jl") end
@time @safetestset "Units Tests" begin include("units.jl") end
Expand Down