Skip to content

Commit e92b5ff

Browse files
test: test respecialize
1 parent 0bc198f commit e92b5ff

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/basic_transformations.jl

Lines changed: 64 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,66 @@ 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+
foofn(x) = 4
339+
@register_symbolic foofn(x::AbstractFoo)
340+
341+
@testset "`respecialize`" begin
342+
@parameters p::AbstractFoo p2(t)::AbstractFoo q[1:2]::AbstractFoo r
343+
rp,
344+
rp2 = let
345+
only(@parameters p::Bar),
346+
SymbolicUtils.term(operation(p2), arguments(p2)...; type = Baz)
347+
end
348+
@variables x(t) = 1.0
349+
@named sys1 = System([D(x) ~ foofn(p) + foofn(p2) + x], t, [x], [p, p2, q, r])
350+
351+
@test_throws ["completed systems"] respecialize(sys1)
352+
@test_throws ["completed systems"] respecialize(sys1, [])
353+
@test_throws ["split systems"] respecialize(complete(sys1; split = false))
354+
@test_throws ["split systems"] respecialize(complete(sys1; split = false), [])
355+
356+
sys = complete(sys1)
357+
358+
@test_throws ["Parameter p", "associated value"] respecialize(sys)
359+
@test_throws ["Parameter p", "associated value"] respecialize(sys, [p])
360+
361+
sys2 = respecialize(sys, [p => Bar()])
362+
@test ModelingToolkit.iscomplete(sys2)
363+
@test ModelingToolkit.is_split(sys2)
364+
ps = ModelingToolkit.get_ps(sys2)
365+
idx = findfirst(isequal(rp), ps)
366+
@test defaults(sys2)[rp] == Bar()
367+
@test symtype(ps[idx]) <: Bar
368+
ic = ModelingToolkit.get_index_cache(sys2)
369+
@test any(x -> x.type == Bar && x.length == 1, ic.nonnumeric_buffer_sizes)
370+
prob = ODEProblem(sys2, [p2 => Bar(), q => [Bar(), Bar()], r => 1], (0.0, 1.0))
371+
@test any(x -> x isa Vector{Bar} && length(x) == 1, prob.p.nonnumeric)
372+
373+
defaults(sys)[p2] = Baz()
374+
sys2 = respecialize(sys, [p => Bar()]; all = true)
375+
@test ModelingToolkit.iscomplete(sys2)
376+
@test ModelingToolkit.is_split(sys2)
377+
ps = ModelingToolkit.get_ps(sys2)
378+
idx = findfirst(isequal(rp2), ps)
379+
@test defaults(sys2)[rp2] == Baz()
380+
@test symtype(ps[idx]) <: Baz
381+
ic = ModelingToolkit.get_index_cache(sys2)
382+
@test any(x -> x.type == Baz && x.length == 1, ic.nonnumeric_buffer_sizes)
383+
delete!(defaults(sys), p2)
384+
prob = ODEProblem(sys2, [q => [Bar(), Bar()], r => 1], (0.0, 1.0))
385+
@test any(x -> x isa Vector{Bar} && length(x) == 1, prob.p.nonnumeric)
386+
@test any(x -> x isa Vector{Baz} && length(x) == 1, prob.p.nonnumeric)
387+
388+
@test_throws ["Numeric types cannot be respecialized"] respecialize(sys, [r => 1])
389+
@test_throws ["array symbolics"] respecialize(sys, [q => Bar[Bar(), Bar()]])
390+
@test_throws ["scalarized array"] respecialize(sys, [q[1] => Bar()])
391+
392+
@parameters foo::AbstractFoo
393+
@test_throws ["does not exist"] respecialize(sys, [foo => Bar()])
394+
end

0 commit comments

Comments
 (0)