|
1 | 1 | using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, DynamicQuantities, Test
|
2 | 2 | using ModelingToolkitStandardLibrary.Blocks: RealInput, RealOutput
|
| 3 | +using SymbolicUtils: symtype |
3 | 4 |
|
4 | 5 | @independent_variables t
|
5 | 6 | D = Differential(t)
|
|
328 | 329 | D(x) ~ y]
|
329 | 330 | @test issetequal(equations(asys), eqs)
|
330 | 331 | end
|
| 332 | + |
| 333 | +abstract type AbstractFoo end |
| 334 | + |
| 335 | +struct Bar <: AbstractFoo end |
| 336 | +struct Baz <: AbstractFoo end |
| 337 | + |
| 338 | +@testset "`respecialize`" begin |
| 339 | + @parameters p::AbstractFoo p2(t)::AbstractFoo q[1:2]::AbstractFoo r |
| 340 | + rp, |
| 341 | + rp2 = let |
| 342 | + only(@parameters p::Bar), |
| 343 | + SymbolicUtils.term(operation(p2), arguments(p2)...; type = Baz) |
| 344 | + end |
| 345 | + @named sys1 = System(Equation[], t, [], [p, p2, q, r]) |
| 346 | + |
| 347 | + @test_throws ["completed systems"] respecialize(sys1) |
| 348 | + @test_throws ["completed systems"] respecialize(sys1, []) |
| 349 | + @test_throws ["split systems"] respecialize(complete(sys1; split = false)) |
| 350 | + @test_throws ["split systems"] respecialize(complete(sys1; split = false), []) |
| 351 | + |
| 352 | + sys = complete(sys1) |
| 353 | + |
| 354 | + @test_throws ["Parameter p", "associated value"] respecialize(sys) |
| 355 | + @test_throws ["Parameter p", "associated value"] respecialize(sys, [p]) |
| 356 | + |
| 357 | + sys2 = respecialize(sys, [p => Bar()]) |
| 358 | + @test ModelingToolkit.iscomplete(sys2) |
| 359 | + @test ModelingToolkit.is_split(sys2) |
| 360 | + ps = ModelingToolkit.get_ps(sys2) |
| 361 | + idx = findfirst(isequal(rp), ps) |
| 362 | + @test defaults(sys2)[rp] == Bar() |
| 363 | + @test symtype(ps[idx]) <: Bar |
| 364 | + ic = ModelingToolkit.get_index_cache(sys2) |
| 365 | + @test any(x -> x.type == Bar && x.length == 1, ic.nonnumeric_buffer_sizes) |
| 366 | + |
| 367 | + defaults(sys)[p2] = Baz() |
| 368 | + sys2 = respecialize(sys, [p => Bar()]; all = true) |
| 369 | + @test ModelingToolkit.iscomplete(sys2) |
| 370 | + @test ModelingToolkit.is_split(sys2) |
| 371 | + ps = ModelingToolkit.get_ps(sys2) |
| 372 | + idx = findfirst(isequal(rp2), ps) |
| 373 | + @test defaults(sys2)[rp2] == Baz() |
| 374 | + @test symtype(ps[idx]) <: Baz |
| 375 | + ic = ModelingToolkit.get_index_cache(sys2) |
| 376 | + @test any(x -> x.type == Baz && x.length == 1, ic.nonnumeric_buffer_sizes) |
| 377 | + delete!(defaults(sys), p2) |
| 378 | + |
| 379 | + @test_throws ["Numeric types cannot be respecialized"] respecialize(sys, [r => 1]) |
| 380 | + @test_throws ["array symbolics"] respecialize(sys, [q => Bar[Bar(), Bar()]]) |
| 381 | + @test_throws ["scalarized array"] respecialize(sys, [q[1] => Bar()]) |
| 382 | + |
| 383 | + @parameters foo::AbstractFoo |
| 384 | + @test_throws ["does not exist"] respecialize(sys, [foo => Bar()]) |
| 385 | +end |
0 commit comments