From bc845205672ae5e7c8e52769c02911d94ed1a47e Mon Sep 17 00:00:00 2001 From: David Widmann Date: Mon, 29 Jul 2019 23:59:05 +0200 Subject: [PATCH 1/2] Add differentiation tests --- Project.toml | 4 ++- test/differentiation.jl | 56 +++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/differentiation.jl 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..97f0306 --- /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())) + 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 = 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())).u[end] + end + + p = [1.0, 1.0, 1.0, 0.0, 1.0] + findiff = Calculus.finite_difference_jacobian(test, p) + fordiff = 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())) + 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 = 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 From 849214f5883490386e1dee117a4cb89dcfcd11a3 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 30 Jul 2019 00:43:18 +0200 Subject: [PATCH 2/2] Set low tolerances --- test/differentiation.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/differentiation.jl b/test/differentiation.jl index 97f0306..053dd60 100644 --- a/test/differentiation.jl +++ b/test/differentiation.jl @@ -13,14 +13,14 @@ h(p, t) = [p[4]] 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())) + 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 = ForwardDiff.gradient(test, p) + fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.gradient(test, p) @test_broken findiff ≈ fordiff end @@ -29,12 +29,12 @@ end 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())).u[end] + 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 = ForwardDiff.jacobian(test, p) + fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.jacobian(test, p) @test_broken findiff ≈ fordiff end @@ -43,14 +43,14 @@ end 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())) + 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 = ForwardDiff.hessian(test, p) + fordiff = @test_logs (:warn, r"^dt <= dtmin") ForwardDiff.hessian(test, p) @test_broken findiff ≈ fordiff end