-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathjacobian.jl
66 lines (54 loc) · 1.79 KB
/
jacobian.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using DynamicalSystemsBase, Test
function oop(u, p, t)
return p[1] * SVector(u[1], -u[2])
end
function iip(du, u, p, t)
du .= oop(u, p, t)
return nothing
end
@testset "IDT=$(IDT), IIP=$(IIP)" for IDT in (true, false), IIP in (false, true)
SystemType = IDT ? DeterministicIteratedMap : CoupledODEs
rule = IIP ? iip : oop
p = 3.0
u0 = [1.0, 1.0]
result = [p 0.0; 0.0 -p]
ds = SystemType(rule, u0, p)
J0 = zeros(dimension(ds), dimension(ds))
J = jacobian(ds)
if IIP
J(J0, current_state(ds), current_parameters(ds), 0.0)
@test J0 == result
else
@test J(current_state(ds), current_parameters(ds), 0.0) == result
end
end
@testset "MTK Jacobian" begin
using ModelingToolkit
using ModelingToolkit: Num, RuntimeGeneratedFunctions.RuntimeGeneratedFunction
using DynamicalSystemsBase: SciMLBase
@independent_variables t
@variables u(t)[1:2]
D = Differential(t)
eqs = [D(u[1]) ~ 3.0 * u[1],
D(u[2]) ~ -3.0 * u[2]]
@named sys = ODESystem(eqs, t)
sys = structural_simplify(sys)
prob = ODEProblem(sys, [1.0, 1.0], (0.0, 1.0); jac=true)
ode = CoupledODEs(prob)
jac = jacobian(ode)
@test jac.jac_oop isa RuntimeGeneratedFunction
@test jac([1.0, 1.0], [], 0.0) == [3 0;0 -3]
@testset "CoupledSDEs" begin
# just to check if MTK @brownian does not give any problems
using StochasticDiffEq
@brownian β
eqs = [D(u[1]) ~ 3.0 * u[1]+ β,
D(u[2]) ~ -3.0 * u[2] + β]
@mtkbuild sys = System(eqs, t)
prob = SDEProblem(sys, [1.0, 1.0], (0.0, 1.0), jac=true)
sde = CoupledSDEs(prob)
jac = jacobian(sde)
@test jac.jac_oop isa RuntimeGeneratedFunction
@test jac([1.0, 1.0], [], 0.0) == [3 0;0 -3]
end
end