Skip to content

Commit 9380fd7

Browse files
test: test respecialize
1 parent b162275 commit 9380fd7

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/basic_transformations.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, DynamicQuantities, Test
22
using ModelingToolkitStandardLibrary.Blocks: RealInput, RealOutput
3+
using SymbolicUtils: symtype
34

45
@independent_variables t
56
D = Differential(t)
@@ -328,3 +329,57 @@ end
328329
D(x) ~ y]
329330
@test issetequal(equations(asys), eqs)
330331
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

Comments
 (0)