diff --git a/Project.toml b/Project.toml index b6f204c..e6fa104 100644 --- a/Project.toml +++ b/Project.toml @@ -27,9 +27,11 @@ 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" @@ -37,4 +39,4 @@ 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"] diff --git a/test/differentiation.jl b/test/differentiation.jl new file mode 100644 index 0000000..053dd60 --- /dev/null +++ b/test/differentiation.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index d8fb067..15f7b05 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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