@@ -6,29 +6,29 @@ using Plots: Plots, plot
66
77# ## Tests on non-layered model (everything should work). ###
88
9- @parameters a b c d
10- @variables s1 (t) s2 (t)
9+ @testset " Basic indexing" begin
10+ @parameters a b c d
11+ @variables s1 (t) s2 (t)
12+
13+ eqs = [D (s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
14+ D (s2) ~ + c * s2 / (1 + s1 + s2) - d * s2]
15+
16+ @mtkcompile population_model = System (eqs, t)
17+
18+ # Tests on ODEProblem.
19+ u0 = [s1 => 2.0 , s2 => 1.0 ]
20+ p = [a => 2.0 , b => 1.0 , c => 1.0 , d => 1.0 ]
21+ tspan = (0.0 , 1000000.0 )
22+ oprob = ODEProblem (population_model, [u0; p], tspan)
23+ sol = solve (oprob, Rodas4 ())
24+
25+ @test sol[s1] == sol[population_model. s1] == sol[:s1 ]
26+ @test sol[s2] == sol[population_model. s2] == sol[:s2 ]
27+ @test sol[s1][end ] ≈ 1.0
28+ @test_throws Exception sol[a]
29+ @test_throws Exception sol[population_model. a]
30+ @test_throws Exception sol[:a ]
1131
12- eqs = [D (s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
13- D (s2) ~ + c * s2 / (1 + s1 + s2) - d * s2]
14-
15- @mtkcompile population_model = System (eqs, t)
16-
17- # Tests on ODEProblem.
18- u0 = [s1 => 2.0 , s2 => 1.0 ]
19- p = [a => 2.0 , b => 1.0 , c => 1.0 , d => 1.0 ]
20- tspan = (0.0 , 1000000.0 )
21- oprob = ODEProblem (population_model, [u0; p], tspan)
22- sol = solve (oprob, Rodas4 ())
23-
24- @test sol[s1] == sol[population_model. s1] == sol[:s1 ]
25- @test sol[s2] == sol[population_model. s2] == sol[:s2 ]
26- @test sol[s1][end ] ≈ 1.0
27- @test_throws Exception sol[a]
28- @test_throws Exception sol[population_model. a]
29- @test_throws Exception sol[:a ]
30-
31- @testset " plot ODE solution" begin
3232 Plots. unicodeplots ()
3333 f = ODEFunction ((u, p, t) -> - u, analytic = (u0, p, t) -> u0 * exp (- t))
3434
@@ -49,104 +49,104 @@ sol = solve(oprob, Rodas4())
4949 sol = solve (ode, Tsit5 ())
5050 @test_nowarn plot (sol)
5151 @test_nowarn plot (sol; plot_analytic = true )
52- end
53-
54- # Tests on SDEProblem
55- noiseeqs = [0.1 * s1,
56- 0.1 * s2]
57- @named noisy_population_model = SDESystem (population_model, noiseeqs)
58- noisy_population_model = complete (noisy_population_model)
59- sprob = SDEProblem (noisy_population_model, [u0; p], (0.0 , 100.0 ))
60- sol = solve (sprob, ImplicitEM ())
61-
62- @test sol[s1] == sol[noisy_population_model. s1] == sol[:s1 ]
63- @test sol[s2] == sol[noisy_population_model. s2] == sol[:s2 ]
64- @test_throws Exception sol[a]
65- @test_throws Exception sol[noisy_population_model. a]
66- @test_throws Exception sol[:a ]
67- @test_nowarn sol (0.5 , idxs = noisy_population_model. s1)
68- # ## Tests on layered model (some things should not work). ###
69-
70- @parameters σ ρ β
71- @variables x (t) y (t) z (t)
72-
73- eqs = [D (x) ~ σ * (y - x),
74- D (y) ~ x * (ρ - z) - y,
75- D (z) ~ x * y - β * z]
76-
77- @named lorenz1 = System (eqs, t)
78- @named lorenz2 = System (eqs, t)
79-
80- @parameters γ
81- @variables a (t) α (t)
82- connections = [0 ~ lorenz1. x + lorenz2. y + a * γ,
83- α ~ 2 lorenz1. x + a * γ]
84- @mtkcompile sys = System (connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
85-
86- u0 = [lorenz1. x => 1.0 ,
87- lorenz1. y => 0.0 ,
88- lorenz1. z => 0.0 ,
89- lorenz2. x => 0.0 ,
90- lorenz2. y => 1.0 ,
91- lorenz2. z => 0.0 ]
9252
93- p = [lorenz1. σ => 10.0 ,
94- lorenz1. ρ => 28.0 ,
95- lorenz1. β => 8 / 3 ,
96- lorenz2. σ => 10.0 ,
97- lorenz2. ρ => 28.0 ,
98- lorenz2. β => 8 / 3 ,
99- γ => 2.0 ]
100-
101- tspan = (0.0 , 100.0 )
102- prob = ODEProblem (sys, [u0; p], tspan)
103- sol = solve (prob, Rodas4 ())
104-
105- @test_throws ArgumentError sol[x]
106- @test in (sol[lorenz1. x], [getindex .(sol. u, i) for i in 1 : length (unknowns (sol. prob. f. sys))])
107- @test_throws KeyError sol[:x ]
108-
109- # ## Non-symbolic indexing tests
110- @test sol[:, 1 ] isa AbstractVector
111- @test sol[:, 1 : 2 ] isa AbstractDiffEqArray
112- @test sol[:, [1 , 2 ]] isa AbstractDiffEqArray
113-
114- sol1 = sol (0.0 : 1.0 : 10.0 )
115- @test sol1. u isa Vector
116- @test first (sol1. u) isa Vector
117- @test length (sol1. u) == 11
118- @test length (sol1. t) == 11
119-
120- sol2 = sol (0.1 )
121- @test sol2 isa Vector
122- @test length (sol2) == length (unknowns (sys))
123- @test first (sol2) isa Real
124-
125- sol3 = sol (0.0 : 1.0 : 10.0 , idxs = [lorenz1. x, lorenz2. x])
126-
127- sol7 = sol (0.0 : 1.0 : 10.0 , idxs = [2 , 1 ])
128- @test sol7. u isa Vector
129- @test first (sol7. u) isa Vector
130- @test length (sol7. u) == 11
131- @test length (sol7. t) == 11
132- @test collect (sol7[t]) ≈ sol3. t
133- @test collect (sol7[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
134-
135- sol8 = sol (0.1 , idxs = [2 , 1 ])
136- @test sol8 isa Vector
137- @test length (sol8) == 2
138- @test first (sol8) isa Real
139-
140- sol9 = sol (0.0 : 1.0 : 10.0 , idxs = 2 )
141- @test sol9. u isa Vector
142- @test first (sol9. u) isa Real
143- @test length (sol9. u) == 11
144- @test length (sol9. t) == 11
145- @test collect (sol9[t]) ≈ sol3. t
146- @test collect (sol9[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
147-
148- sol10 = sol (0.1 , idxs = 2 )
149- @test sol10 isa Real
53+ # Tests on SDEProblem
54+ noiseeqs = [0.1 * s1,
55+ 0.1 * s2]
56+ @named noisy_population_model = SDESystem (population_model, noiseeqs)
57+ noisy_population_model = complete (noisy_population_model)
58+ sprob = SDEProblem (noisy_population_model, [u0; p], (0.0 , 100.0 ))
59+ sol = solve (sprob, ImplicitEM ())
60+
61+ @test sol[s1] == sol[noisy_population_model. s1] == sol[:s1 ]
62+ @test sol[s2] == sol[noisy_population_model. s2] == sol[:s2 ]
63+ @test_throws Exception sol[a]
64+ @test_throws Exception sol[noisy_population_model. a]
65+ @test_throws Exception sol[:a ]
66+ @test_nowarn sol (0.5 , idxs = noisy_population_model. s1)
67+ # ## Tests on layered model (some things should not work). ###
68+
69+ @parameters σ ρ β
70+ @variables x (t) y (t) z (t)
71+
72+ eqs = [D (x) ~ σ * (y - x),
73+ D (y) ~ x * (ρ - z) - y,
74+ D (z) ~ x * y - β * z]
75+
76+ @named lorenz1 = System (eqs, t)
77+ @named lorenz2 = System (eqs, t)
78+
79+ @parameters γ
80+ @variables a (t) α (t)
81+ connections = [0 ~ lorenz1. x + lorenz2. y + a * γ,
82+ α ~ 2 lorenz1. x + a * γ]
83+ @mtkcompile sys = System (connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
84+
85+ u0 = [lorenz1. x => 1.0 ,
86+ lorenz1. y => 0.0 ,
87+ lorenz1. z => 0.0 ,
88+ lorenz2. x => 0.0 ,
89+ lorenz2. y => 1.0 ,
90+ lorenz2. z => 0.0 ]
91+
92+ p = [lorenz1. σ => 10.0 ,
93+ lorenz1. ρ => 28.0 ,
94+ lorenz1. β => 8 / 3 ,
95+ lorenz2. σ => 10.0 ,
96+ lorenz2. ρ => 28.0 ,
97+ lorenz2. β => 8 / 3 ,
98+ γ => 2.0 ]
99+
100+ tspan = (0.0 , 100.0 )
101+ prob = ODEProblem (sys, [u0; p], tspan)
102+ sol = solve (prob, Rodas4 ())
103+
104+ @test_throws ArgumentError sol[x]
105+ @test in (sol[lorenz1. x], [getindex .(sol. u, i) for i in 1 : length (unknowns (sol. prob. f. sys))])
106+ @test_throws KeyError sol[:x ]
107+
108+ # ## Non-symbolic indexing tests
109+ @test sol[:, 1 ] isa AbstractVector
110+ @test sol[:, 1 : 2 ] isa AbstractDiffEqArray
111+ @test sol[:, [1 , 2 ]] isa AbstractDiffEqArray
112+
113+ sol1 = sol (0.0 : 1.0 : 10.0 )
114+ @test sol1. u isa Vector
115+ @test first (sol1. u) isa Vector
116+ @test length (sol1. u) == 11
117+ @test length (sol1. t) == 11
118+
119+ sol2 = sol (0.1 )
120+ @test sol2 isa Vector
121+ @test length (sol2) == length (unknowns (sys))
122+ @test first (sol2) isa Real
123+
124+ sol3 = sol (0.0 : 1.0 : 10.0 , idxs = [lorenz1. x, lorenz2. x])
125+
126+ sol7 = sol (0.0 : 1.0 : 10.0 , idxs = [2 , 1 ])
127+ @test sol7. u isa Vector
128+ @test first (sol7. u) isa Vector
129+ @test length (sol7. u) == 11
130+ @test length (sol7. t) == 11
131+ @test collect (sol7[t]) ≈ sol3. t
132+ @test collect (sol7[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
133+
134+ sol8 = sol (0.1 , idxs = [2 , 1 ])
135+ @test sol8 isa Vector
136+ @test length (sol8) == 2
137+ @test first (sol8) isa Real
138+
139+ sol9 = sol (0.0 : 1.0 : 10.0 , idxs = 2 )
140+ @test sol9. u isa Vector
141+ @test first (sol9. u) isa Real
142+ @test length (sol9. u) == 11
143+ @test length (sol9. t) == 11
144+ @test collect (sol9[t]) ≈ sol3. t
145+ @test collect (sol9[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
146+
147+ sol10 = sol (0.1 , idxs = 2 )
148+ @test sol10 isa Real
149+ end
150150
151151@testset " Plot idxs" begin
152152 @variables x (t) y (t)
198198 (0.0 , 1.0 ); build_initializeprob = false )
199199 dae_sol = solve (prob, DFBDF (); save_idxs = [x])
200200
201- @brownian a b
201+ @brownians a b
202202 @mtkcompile sys = System ([D (x) ~ x + p * y + x * a, D (y) ~ 2 p + x^ 2 + y * b], t)
203203 xidx = variable_index (sys, x)
204204 prob = SDEProblem (sys, [x => 1.0 , y => 2.0 , p => 2.0 ], (0.0 , 1.0 ))
0 commit comments