@@ -6,6 +6,7 @@ using Optimization
6
6
using OptimizationOptimJL
7
7
using ForwardDiff
8
8
using SciMLStructures
9
+ using Test
9
10
10
11
probs = []
11
12
syss = []
@@ -67,30 +68,21 @@ push!(probs, OptimizationProblem(optsys, u0, p))
67
68
k = ShiftIndex (t)
68
69
@mtkbuild discsys = DiscreteSystem (
69
70
[x ~ x (k - 1 ) * ρ + y (k - 2 ), y ~ y (k - 1 ) * σ - z (k - 2 ), z ~ z (k - 1 ) * β + x (k - 2 )],
70
- t)
71
+ t; defaults = [x => 1.0 , y => 1.0 , z => 1.0 ] )
71
72
# Roundabout method to avoid having to specify values for previous timestep
72
- fn = DiscreteFunction (discsys)
73
- ps = ModelingToolkit. MTKParameters (discsys, p)
74
- discu0 = Dict ([u0... , x (k - 1 ) => 0.0 , y (k - 1 ) => 0.0 , z (k - 1 ) => 0.0 ])
73
+ discprob = DiscreteProblem (discsys, [], (0 , 10 ), p)
74
+ for (var, v) in u0
75
+ discprob[var] = v
76
+ discprob[var (k- 1 )] = 0.0
77
+ end
75
78
push! (syss, discsys)
76
- push! (probs, DiscreteProblem (fn, getindex .((discu0,), unknowns (discsys)), (0 , 10 ), ps))
77
-
78
- # TODO : Rewrite this example when the MTK codegen is merged
79
- @named sys1 = NonlinearSystem (
80
- [0 ~ x^ 3 * β + y^ 3 * ρ - σ, 0 ~ x^ 2 + 2 x * y + y^ 2 ], [x, y], [σ, β, ρ])
81
- sys1 = complete (sys1)
82
- @named sys2 = NonlinearSystem ([0 ~ z^ 2 - 4 z + 4 ], [z], [])
83
- sys2 = complete (sys2)
84
- @named fullsys = NonlinearSystem (
79
+ push! (probs, discprob)
80
+
81
+ @mtkbuild sys = NonlinearSystem (
85
82
[0 ~ x^ 3 * β + y^ 3 * ρ - σ, 0 ~ x^ 2 + 2 x * y + y^ 2 , 0 ~ z^ 2 - 4 z + 4 ],
86
83
[x, y, z], [σ, β, ρ])
87
- fullsys = complete (fullsys)
88
-
89
- prob1 = NonlinearProblem (sys1, u0, p)
90
- prob2 = NonlinearProblem (sys2, u0, prob1. p)
91
- sccprob = SCCNonlinearProblem (
92
- [prob1, prob2], [Returns (nothing ), Returns (nothing )], prob1. p, true ; sys = fullsys)
93
- push! (syss, fullsys)
84
+ sccprob = SCCNonlinearProblem (sys, u0, p)
85
+ push! (syss, sys)
94
86
push! (probs, sccprob)
95
87
96
88
for (sys, prob) in zip (syss, probs)
273
265
function SciMLBase. detect_cycles (
274
266
:: ModelingToolkit.AbstractSystem , varmap:: Dict{Any, Any} , vars)
275
267
for sym in vars
276
- if symbolic_type (ModelingToolkit. fixpoint_sub (sym, varmap; maxiters = 10 )) !=
268
+ newval = ModelingToolkit. fixpoint_sub (sym, varmap; maxiters = 10 )
269
+ vs = ModelingToolkit. vars (newval)
270
+ if ! isempty (vars) && any (in (Set (vars)), vs)
277
271
NotSymbolic ()
278
272
return true
279
273
end
296
290
end
297
291
298
292
@testset " SCCNonlinearProblem" begin
299
- @named sys1 = NonlinearSystem (
300
- [0 ~ x^ 3 * β + y^ 3 * ρ - σ, 0 ~ x^ 2 + 2 x * y + y^ 2 ], [x, y], [σ, β, ρ])
301
- sys1 = complete (sys1)
302
- @named sys2 = NonlinearSystem ([0 ~ z^ 2 - 4 z + 4 ], [z], [])
303
- sys2 = complete (sys2)
304
- @named fullsys = NonlinearSystem (
293
+ @mtkbuild fullsys = NonlinearSystem (
305
294
[0 ~ x^ 3 * β + y^ 3 * ρ - σ, 0 ~ x^ 2 + 2 x * y + y^ 2 , 0 ~ z^ 2 - 4 z + 4 ],
306
295
[x, y, z], [σ, β, ρ])
307
- fullsys = complete (fullsys)
308
296
309
297
u0 = [x => 1.0 ,
310
298
y => 0.0 ,
@@ -314,15 +302,17 @@ end
314
302
ρ => 10.0 ,
315
303
β => 8 / 3 ]
316
304
317
- prob1 = NonlinearProblem (sys1, u0, p)
318
- prob2 = NonlinearProblem (sys2, u0, prob1. p)
319
- sccprob = SCCNonlinearProblem (
320
- [prob1, prob2], [Returns (nothing ), Returns (nothing )], prob1. p, true ; sys = fullsys)
305
+ sccprob = SCCNonlinearProblem (fullsys, u0, p)
321
306
322
307
sccprob2 = remake (sccprob; u0 = 2 ones (3 ))
323
308
@test state_values (sccprob2) ≈ 2 ones (3 )
324
- @test sccprob2. probs[1 ]. u0 ≈ 2 ones (2 )
325
- @test sccprob2. probs[2 ]. u0 ≈ 2 ones (1 )
309
+ prob1, prob2 = if length (state_values (sccprob2. probs[1 ])) == 1
310
+ sccprob2. probs[2 ], sccprob2. probs[1 ]
311
+ else
312
+ sccprob2. probs[1 ], sccprob2. probs[2 ]
313
+ end
314
+ @test prob1. u0 ≈ 2 ones (2 )
315
+ @test prob2. u0 ≈ 2 ones (1 )
326
316
@test sccprob2. explicitfuns! != = missing
327
317
@test sccprob2. f. sys != = missing
328
318
333
323
@test_throws [" parameters_alias" , " SCCNonlinearProblem" ] remake (
334
324
sccprob; parameters_alias = false , p = [σ => 2.0 ])
335
325
336
- newp = remake_buffer (sys1, prob1 . p, [σ], [3.0 ])
326
+ newp = remake_buffer (sccprob . f . sys, sccprob . p, [σ], [3.0 ])
337
327
sccprob4 = remake (sccprob; parameters_alias = false , p = newp,
338
- probs = [remake (prob1 ; p = [σ => 3.0 ]), prob2 ])
328
+ probs = [remake (sccprob . probs[ 1 ] ; p = [σ => 3.0 ]), sccprob . probs[ 2 ] ])
339
329
@test ! sccprob4. parameters_alias
340
330
@test sccprob4. p != = sccprob4. probs[1 ]. p
341
331
@test sccprob4. p != = sccprob4. probs[2 ]. p
0 commit comments