@@ -6,33 +6,100 @@ function f(du,u,p,t)
6
6
du[2 ] = p[2 ]
7
7
end
8
8
9
- cb = ContinuousCallback ((u,t,i) -> u[1 ], (integrator)-> (println (" Stopped." );integrator. p[2 ]= zero (integrator. p[2 ])))
10
- function test_f (p)
11
- prob = ODEProblem (f,eltype (p).([1.0 ,0.0 ]),eltype (p).((0.0 ,1.0 )),copy (p))
12
- integrator = init (prob,Tsit5 (),abstol= 1e-14 ,reltol= 1e-14 ,callback= cb)
13
- step! (integrator)
14
- solve! (integrator). u[end ]
9
+ for x in 0 : 0.001 : 5
10
+ called = false
11
+ function test_f (p)
12
+ cb = ContinuousCallback ((u,t,i) -> u[1 ], (integrator)-> (called= true ;integrator. p[2 ]= zero (integrator. p[2 ])))
13
+ prob = ODEProblem (f,eltype (p).([1.0 ,0.0 ]),eltype (p).((0.0 ,1.0 )),copy (p))
14
+ integrator = init (prob,Tsit5 (),abstol= 1e-14 ,reltol= 1e-14 ,callback= cb)
15
+ step! (integrator)
16
+ solve! (integrator). u[end ]
17
+ end
18
+ p = [2.0 , x]
19
+ called = false
20
+ findiff = Calculus. finite_difference_jacobian (test_f,p)
21
+ @test called
22
+ called = false
23
+ fordiff = ForwardDiff. jacobian (test_f,p)
24
+ @test called
25
+ @test findiff ≈ fordiff
15
26
end
16
- p = [2.0 , 1.0 ]
17
- findiff = Calculus. finite_difference_jacobian (test_f,p)
18
- fordiff = ForwardDiff. jacobian (test_f,p)
19
- @test findiff ≈ fordiff
20
27
21
28
function f2 (du,u,p,t)
22
- du[1 ] = - u[1 ]
29
+ du[1 ] = - u[2 ]
23
30
du[2 ] = p[2 ]
24
31
end
25
32
33
+ for x in 2.1 : 0.001 : 5
34
+ called = false
35
+ function test_f2 (p)
36
+ cb = ContinuousCallback ((u,t,i) -> u[1 ], (integrator)-> (called= true ;integrator. p[2 ]= zero (integrator. p[2 ])))
37
+ prob = ODEProblem (f2,eltype (p).([1.0 ,0.0 ]),eltype (p).((0.0 ,1.0 )),copy (p))
38
+ integrator = init (prob,Tsit5 (),abstol= 1e-12 ,reltol= 1e-12 ,callback= cb)
39
+ step! (integrator)
40
+ solve! (integrator). u[end ]
41
+ end
42
+ p = [2.0 , x]
43
+ findiff = Calculus. finite_difference_jacobian (test_f2,p)
44
+ @test called
45
+ called = false
46
+ fordiff = ForwardDiff. jacobian (test_f2,p)
47
+ @test called
48
+ @test findiff ≈ fordiff
49
+ end
50
+
51
+ #=
52
+ #x = 2.0 is an interesting case
53
+
54
+ x = 2.0
55
+
26
56
function test_f2(p)
57
+ cb = ContinuousCallback((u,t,i) -> u[1], (integrator)->(@show(x,integrator.t);called=true;integrator.p[2]=zero(integrator.p[2])))
27
58
prob = ODEProblem(f2,eltype(p).([1.0,0.0]),eltype(p).((0.0,1.0)),copy(p))
28
- integrator = init (prob,Tsit5 (),abstol= 1e-14 ,reltol= 1e-14 ,callback= cb)
59
+ integrator = init(prob,Tsit5(),abstol=1e-12 ,reltol=1e-12 ,callback=cb)
29
60
step!(integrator)
30
61
solve!(integrator).u[end]
31
62
end
32
- p = [2.0 , 1.0 ]
63
+
64
+ p = [2.0, x]
33
65
findiff = Calculus.finite_difference_jacobian(test_f2,p)
66
+ @test called
67
+ called = false
34
68
fordiff = ForwardDiff.jacobian(test_f2,p)
35
- @test findiff ≈ fordiff
69
+ @test called
70
+
71
+ # At that value, it shouldn't be called, but a small perturbation will make it called, so finite difference is wrong!
72
+ =#
73
+
74
+ for x in 1.0 : 0.001 : 2.5
75
+ function lotka_volterra (du,u,p,t)
76
+ x, y = u
77
+ α, β, δ, γ = p
78
+ du[1 ] = dx = α* x - β* x* y
79
+ du[2 ] = dy = - δ* y + γ* x* y
80
+ end
81
+ u0 = [1.0 ,1.0 ]
82
+ tspan = (0.0 ,10.0 )
83
+ p = [x,1.0 ,3.0 ,1.0 ]
84
+ prob = ODEProblem (lotka_volterra,u0,tspan,p)
85
+ sol = solve (prob,Tsit5 ())
86
+
87
+ called= false
88
+ function test_lotka (p)
89
+ cb = ContinuousCallback ((u,t,i) -> u[1 ]- 2.5 , (integrator)-> (called= true ;integrator. p[4 ]= 1.5 ))
90
+ prob = ODEProblem (lotka_volterra,eltype (p).([1.0 ,1.0 ]),eltype (p).((0.0 ,10.0 )),copy (p))
91
+ integrator = init (prob,Tsit5 (),abstol= 1e-12 ,reltol= 1e-12 ,callback= cb)
92
+ step! (integrator)
93
+ solve! (integrator). u[end ]
94
+ end
95
+
96
+ findiff = Calculus. finite_difference_jacobian (test_lotka,p)
97
+ @test called
98
+ called = false
99
+ fordiff = ForwardDiff. jacobian (test_lotka,p)
100
+ @test called
101
+ @test findiff ≈ fordiff
102
+ end
36
103
37
104
# Gradients and Hessians
38
105
0 commit comments