@@ -9,19 +9,19 @@ using Plots: Plots, plot
9
9
@testset " Basic indexing" begin
10
10
@parameters a b c d
11
11
@variables s1 (t) s2 (t)
12
-
12
+
13
13
eqs = [D (s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
14
14
D (s2) ~ + c * s2 / (1 + s1 + s2) - d * s2]
15
-
15
+
16
16
@mtkcompile population_model = System (eqs, t)
17
-
17
+
18
18
# Tests on ODEProblem.
19
19
u0 = [s1 => 2.0 , s2 => 1.0 ]
20
20
p = [a => 2.0 , b => 1.0 , c => 1.0 , d => 1.0 ]
21
21
tspan = (0.0 , 1000000.0 )
22
22
oprob = ODEProblem (population_model, [u0; p], tspan)
23
23
sol = solve (oprob, Rodas4 ())
24
-
24
+
25
25
@test sol[s1] == sol[population_model. s1] == sol[:s1 ]
26
26
@test sol[s2] == sol[population_model. s2] == sol[:s2 ]
27
27
@test sol[s1][end ] ≈ 1.0
@@ -57,93 +57,94 @@ using Plots: Plots, plot
57
57
noisy_population_model = complete (noisy_population_model)
58
58
sprob = SDEProblem (noisy_population_model, [u0; p], (0.0 , 100.0 ))
59
59
sol = solve (sprob, ImplicitEM ())
60
-
60
+
61
61
@test sol[s1] == sol[noisy_population_model. s1] == sol[:s1 ]
62
62
@test sol[s2] == sol[noisy_population_model. s2] == sol[:s2 ]
63
63
@test_throws Exception sol[a]
64
64
@test_throws Exception sol[noisy_population_model. a]
65
65
@test_throws Exception sol[:a ]
66
66
@test_nowarn sol (0.5 , idxs = noisy_population_model. s1)
67
67
# ## Tests on layered model (some things should not work). ###
68
-
68
+
69
69
@parameters σ ρ β
70
70
@variables x (t) y (t) z (t)
71
-
71
+
72
72
eqs = [D (x) ~ σ * (y - x),
73
73
D (y) ~ x * (ρ - z) - y,
74
74
D (z) ~ x * y - β * z]
75
-
75
+
76
76
@named lorenz1 = System (eqs, t)
77
77
@named lorenz2 = System (eqs, t)
78
-
78
+
79
79
@parameters γ
80
80
@variables a (t) α (t)
81
81
connections = [0 ~ lorenz1. x + lorenz2. y + a * γ,
82
82
α ~ 2 lorenz1. x + a * γ]
83
83
@mtkcompile sys = System (connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
84
-
84
+
85
85
u0 = [lorenz1. x => 1.0 ,
86
86
lorenz1. y => 0.0 ,
87
87
lorenz1. z => 0.0 ,
88
88
lorenz2. x => 0.0 ,
89
89
lorenz2. y => 1.0 ,
90
90
lorenz2. z => 0.0 ]
91
-
91
+
92
92
p = [lorenz1. σ => 10.0 ,
93
93
lorenz1. ρ => 28.0 ,
94
94
lorenz1. β => 8 / 3 ,
95
95
lorenz2. σ => 10.0 ,
96
96
lorenz2. ρ => 28.0 ,
97
97
lorenz2. β => 8 / 3 ,
98
98
γ => 2.0 ]
99
-
99
+
100
100
tspan = (0.0 , 100.0 )
101
101
prob = ODEProblem (sys, [u0; p], tspan)
102
102
sol = solve (prob, Rodas4 ())
103
-
103
+
104
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))])
105
+ @test in (sol[lorenz1. x], [getindex .(sol. u, i)
106
+ for i in 1 : length (unknowns (sol. prob. f. sys))])
106
107
@test_throws KeyError sol[:x ]
107
-
108
+
108
109
# ## Non-symbolic indexing tests
109
110
@test sol[:, 1 ] isa AbstractVector
110
111
@test sol[:, 1 : 2 ] isa AbstractDiffEqArray
111
112
@test sol[:, [1 , 2 ]] isa AbstractDiffEqArray
112
-
113
+
113
114
sol1 = sol (0.0 : 1.0 : 10.0 )
114
115
@test sol1. u isa Vector
115
116
@test first (sol1. u) isa Vector
116
117
@test length (sol1. u) == 11
117
118
@test length (sol1. t) == 11
118
-
119
+
119
120
sol2 = sol (0.1 )
120
121
@test sol2 isa Vector
121
122
@test length (sol2) == length (unknowns (sys))
122
123
@test first (sol2) isa Real
123
-
124
+
124
125
sol3 = sol (0.0 : 1.0 : 10.0 , idxs = [lorenz1. x, lorenz2. x])
125
-
126
+
126
127
sol7 = sol (0.0 : 1.0 : 10.0 , idxs = [2 , 1 ])
127
128
@test sol7. u isa Vector
128
129
@test first (sol7. u) isa Vector
129
130
@test length (sol7. u) == 11
130
131
@test length (sol7. t) == 11
131
132
@test collect (sol7[t]) ≈ sol3. t
132
133
@test collect (sol7[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
133
-
134
+
134
135
sol8 = sol (0.1 , idxs = [2 , 1 ])
135
136
@test sol8 isa Vector
136
137
@test length (sol8) == 2
137
138
@test first (sol8) isa Real
138
-
139
+
139
140
sol9 = sol (0.0 : 1.0 : 10.0 , idxs = 2 )
140
141
@test sol9. u isa Vector
141
142
@test first (sol9. u) isa Real
142
143
@test length (sol9. u) == 11
143
144
@test length (sol9. t) == 11
144
145
@test collect (sol9[t]) ≈ sol3. t
145
146
@test collect (sol9[t, 1 : 5 ]) ≈ sol3. t[1 : 5 ]
146
-
147
+
147
148
sol10 = sol (0.1 , idxs = 2 )
148
149
@test sol10 isa Real
149
150
end
194
195
195
196
@mtkcompile sys = System ([D (x) ~ x + p * y, 1 ~ sin (y) + cos (x)], t)
196
197
xidx = variable_index (sys, x)
197
- prob = DAEProblem (sys, [D (x) => x + p * y, D (y) => 1 / sqrt (1 - (1 - cos (x))^ 2 ), x => 1.0 , y => asin (1 - cos (x)), p => 2.0 ],
198
+ prob = DAEProblem (sys,
199
+ [D (x) => x + p * y, D (y) => 1 / sqrt (1 - (1 - cos (x))^ 2 ),
200
+ x => 1.0 , y => asin (1 - cos (x)), p => 2.0 ],
198
201
(0.0 , 1.0 ); build_initializeprob = false )
199
202
dae_sol = solve (prob, DFBDF (); save_idxs = [x])
200
203
@@ -225,10 +228,11 @@ end
225
228
@testset " ODE with callbacks" begin
226
229
@variables x (t) y (t)
227
230
@parameters p q (t) r (t) s (t) u (t)
228
- evs = [
229
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ]; discrete_parameters = [q, s], iv = t)
230
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ]; discrete_parameters = [r, u], iv = t)
231
- ]
231
+ evs = [ModelingToolkit. SymbolicDiscreteCallback (
232
+ 0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ];
233
+ discrete_parameters = [q, s], iv = t)
234
+ ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ];
235
+ discrete_parameters = [r, u], iv = t)]
232
236
@mtkcompile sys = System ([D (x) ~ x + p * y, D (y) ~ 2 p + x], t, [x, y],
233
237
[p, q, r, s, u], discrete_events = evs)
234
238
@test length (unknowns (sys)) == 2
243
247
sidx = parameter_index (sys, s)
244
248
uidx = parameter_index (sys, u)
245
249
246
- prob = ODEProblem (sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ], (0.0 , 5.0 ))
250
+ prob = ODEProblem (
251
+ sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ],
252
+ (0.0 , 5.0 ))
247
253
248
254
@test SciMLBase. SavedSubsystem (sys, prob. p, [x, y, q, r, s, u]) === nothing
249
255
@@ -268,13 +274,16 @@ end
268
274
@testset " SavedSubsystemWithFallback" begin
269
275
@variables x (t) y (t)
270
276
@parameters p q (t) r (t) s (t) u (t)
271
- evs = [
272
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ]; discrete_parameters = [q, s], iv = t)
273
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ]; discrete_parameters = [r, u], iv = t)
274
- ]
277
+ evs = [ModelingToolkit. SymbolicDiscreteCallback (
278
+ 0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ];
279
+ discrete_parameters = [q, s], iv = t)
280
+ ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ];
281
+ discrete_parameters = [r, u], iv = t)]
275
282
@mtkcompile sys = System ([D (x) ~ x + p * y, D (y) ~ 2 p + x^ 2 ], t, [x, y],
276
283
[p, q, r, s, u], discrete_events = evs)
277
- prob = ODEProblem (sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ], (0.0 , 5.0 ))
284
+ prob = ODEProblem (
285
+ sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ],
286
+ (0.0 , 5.0 ))
278
287
ss = SciMLBase. SavedSubsystem (sys, prob. p, [x, q, s, r])
279
288
@test SciMLBase. get_saved_state_idxs (ss) == [variable_index (sys, x)]
280
289
sswf = SciMLBase. SavedSubsystemWithFallback (ss, sys)
@@ -314,13 +323,16 @@ end
314
323
@testset " get_save_idxs_and_saved_subsystem" begin
315
324
@variables x (t) y (t)
316
325
@parameters p q (t) r (t) s (t) u (t)
317
- evs = [
318
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ]; discrete_parameters = [q, s], iv = t)
319
- ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ]; discrete_parameters = [r, u], iv = t)
320
- ]
326
+ evs = [ModelingToolkit. SymbolicDiscreteCallback (
327
+ 0.1 , [q ~ Pre (q) + 1 , s ~ Pre (s) - 1 ];
328
+ discrete_parameters = [q, s], iv = t)
329
+ ModelingToolkit. SymbolicDiscreteCallback (0.1 , [r ~ 2 Pre (r), u ~ Pre (u) / 2 ];
330
+ discrete_parameters = [r, u], iv = t)]
321
331
@mtkcompile sys = System ([D (x) ~ x + p * y, D (y) ~ 2 p + x^ 2 ], t, [x, y],
322
332
[p, q, r, s, u], discrete_events = evs)
323
- prob = ODEProblem (sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ], (0.0 , 5.0 ))
333
+ prob = ODEProblem (
334
+ sys, [x => 1.0 , y => 1.0 , p => 0.5 , q => 0.0 , r => 1.0 , s => 10.0 , u => 4096.0 ],
335
+ (0.0 , 5.0 ))
324
336
325
337
_idxs, _ss = @inferred SciMLBase. get_save_idxs_and_saved_subsystem (prob, nothing )
326
338
@test _idxs === _ss === nothing
0 commit comments