1+ using ComponentArrays
2+ using DifferentialEquations
3+ using LabelledArrays
4+ using Sundials
5+ using Test
6+ using Unitful
7+
8+ @testset " Performance" begin
9+ @testset " Issue 36" begin
10+ function f1 (du,u,p,t)
11+ du. x .= - 1 .* u. x .* u. y .* p[1 ]
12+ du. y .= - 1 .* u. y .* p[2 ]
13+ end
14+
15+ n = 100
16+
17+ p = [0.1 ,0.1 ]
18+
19+ lu_0 = @LArray fill (1000.0 ,2 * n) (x= (1 : n), y= (n+ 1 : 2 * n))
20+ cu_0 = ComponentArray (x= fill (1000.0 , n), y= fill (1000.0 , n))
21+
22+ lprob1 = ODEProblem (f1,lu_0,(0 ,100.0 ),p)
23+ cprob1 = ODEProblem (f1,cu_0,(0 ,100.0 ),p)
24+
25+ solve (lprob1, Rodas5 ());
26+ solve (lprob1, Rodas5 (autodiff= false ));
27+ solve (cprob1, Rodas5 ());
28+ solve (cprob1, Rodas5 (autodiff= false ));
29+
30+ ltime1 = @elapsed lsol1 = solve (lprob1, Rodas5 ());
31+ ltime2 = @elapsed lsol2 = solve (lprob1, Rodas5 (autodiff= false ));
32+ ctime1 = @elapsed csol1 = solve (cprob1, Rodas5 ());
33+ ctime2 = @elapsed csol2 = solve (cprob1, Rodas5 (autodiff= false ));
34+
35+ @test (ctime1 - ltime1)/ ltime1 < 0.3
36+ @test (ctime2 - ltime2)/ ltime2 < 0.3
37+ end
38+
39+ @testset " Slack Issue 2021-2-19" begin
40+ DifferentialEquations. DiffEqBase. diffeqbc (x:: ComponentArray ) = DifferentialEquations. DiffEqBase. DiffEqBC (x)
41+
42+ function heat_conduction (du,u,p,t)
43+ nknots = 100
44+ h² = (1.0 / (nknots+ 1 ))^ 2
45+ du .= zero (eltype (u))
46+ u₃ = @view u[3 : end ]
47+ u₂ = @view u[2 : end - 1 ]
48+ u₁ = @view u[1 : end - 2 ]
49+ @. du[2 : end - 1 ] = (u₃ - 2 * u₂ + u₁)/ h²
50+ nothing
51+ end
52+
53+ t0, t1 = 0.0 , 1.0
54+ u0 = randn (300 )
55+ u0_ca = ComponentArray (a= u0[1 : 100 ],b= u0[101 : 200 ],c= u0[201 : 300 ])
56+ u0_la = @LArray u0 (a= 1 : 100 , b= 101 : 200 , c= 201 : 300 )
57+
58+ cprob = ODEProblem (heat_conduction, u0_ca, (t0, t1))
59+ lprob = ODEProblem (heat_conduction, u0_la, (t0, t1))
60+ prob = ODEProblem (heat_conduction, u0, (t0, t1))
61+
62+ solve (cprob, Tsit5 (), saveat= 0.2 )
63+ solve (lprob, Tsit5 (), saveat= 0.2 )
64+ solve (prob, Tsit5 (), saveat= 0.2 )
65+
66+ ctime = @elapsed solve (cprob, Tsit5 (), saveat= 0.2 )
67+ ltime = @elapsed solve (lprob, Tsit5 (), saveat= 0.2 )
68+ time = @elapsed solve (prob, Tsit5 (), saveat= 0.2 )
69+
70+ @test (ctime - time)/ time < 0.1
71+ @test (ctime - ltime)/ ltime < 0.05
72+ end
73+ end
74+
75+ @testset " Issue 53" begin
76+ x0 = ComponentArray (x= ones (10 ))
77+ prob = ODEProblem ((u,p,t)-> u, x0, (0. ,1. ))
78+ sol = solve (prob, CVODE_BDF (linear_solver= :BCG ), reltol= 1e-15 , abstol= 1e-15 )
79+ @test sol (1 )[1 ] ≈ exp (1 )
80+ end
81+
82+ @testset " Issue 55" begin
83+ f! (D, x, p, t) = nothing
84+ x0 = ComponentArray (x= zeros (4 ))
85+ prob = ODEProblem (f!, x0, (0.0 , 1.0 ), 0.0 )
86+ sol = solve (prob, Rodas4 ())
87+ @test sol[1 ] == x0
88+ end
89+
90+ @testset " Unitful" begin
91+ tspan = (0.0 u " s" , 10.0 u " s" )
92+ pos = 0.0 u " m"
93+ vel = 0.0 u " m/s"
94+ x0 = ComponentArray (pos= pos, vel= vel)
95+ F (t) = 1
96+
97+ # double integrator in state-space form
98+ A = Union{typeof (0 u " s^-1" ), typeof (0 u " s^-2" ), Int}[0 u " s^-1" 1 ; 0 u " s^-2" 0 u " s^-1" ]
99+ B = Union{typeof (0 u " m/s" ), typeof (1 u " m/s^2" )}[0 u " m/s" ; 1 u " m/s^2" ]
100+ di (x,u,t) = A* x .+ B* u (t)
101+
102+ prob = ODEProblem (di, x0, tspan, F)
103+ sol = solve (prob, Tsit5 ())
104+ @test unit (sol[end ]. pos) == u " m"
105+ @test unit (sol[end ]. vel) == u " m/s"
106+ end
0 commit comments