@@ -3,6 +3,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, MTKParameters
33using SymbolicIndexingInterface
44using SciMLStructures: SciMLStructures, canonicalize, Tunable, Discrete, Constants
55using ForwardDiff
6+ using JET
67
78@parameters a b c d:: Integer e[1 : 3 ] f[1 : 3 , 1 : 3 ]:: Int g:: Vector{AbstractFloat} h:: String
89@named sys = ODESystem (
@@ -121,3 +122,74 @@ ps = MTKParameters(sys, [p => 1.0, q => 2.0, r => 3.0])
121122newps = remake_buffer (sys, ps, Dict (p => 1.0f0 ))
122123@test newps. tunable[1 ] isa Vector{Float32}
123124@test newps. tunable[1 ] == [1.0f0 , 2.0f0 , 3.0f0 ]
125+
126+ # JET tests
127+
128+ # scalar parameters only
129+ function level1 ()
130+ @parameters p1= 0.5 [tunable = true ] p2 = 1 [tunable= true ] p3 = 3 [tunable = false ] p4= 3 [tunable = true ] y0= 1
131+ @variables x (t)= 2 y (t)= y0
132+ D = Differential (t)
133+
134+ eqs = [D (x) ~ p1 * x - p2 * x * y
135+ D (y) ~ - p3 * y + p4 * x * y]
136+
137+ sys = structural_simplify (complete (ODESystem (
138+ eqs, t, tspan = (0 , 3.0 ), name = :sys , parameter_dependencies = [y0 => 2 p4])))
139+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys)
140+ end
141+
142+ # scalar and vector parameters
143+ function level2 ()
144+ @parameters p1= 0.5 [tunable = true ] (p23[1 : 2 ]= [1 , 3.0 ]) [tunable = true ] p4= 3 [tunable = false ] y0= 1
145+ @variables x (t)= 2 y (t)= y0
146+ D = Differential (t)
147+
148+ eqs = [D (x) ~ p1 * x - p23[1 ] * x * y
149+ D (y) ~ - p23[2 ] * y + p4 * x * y]
150+
151+ sys = structural_simplify (complete (ODESystem (
152+ eqs, t, tspan = (0 , 3.0 ), name = :sys , parameter_dependencies = [y0 => 2 p4])))
153+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys)
154+ end
155+
156+ # scalar and vector parameters with different scalar types
157+ function level3 ()
158+ @parameters p1= 0.5 [tunable = true ] (p23[1 : 2 ]= [1 , 3.0 ]) [tunable = true ] p4:: Int = 3 [tunable = true ] y0:: Int = 1
159+ @variables x (t)= 2 y (t)= y0
160+ D = Differential (t)
161+
162+ eqs = [D (x) ~ p1 * x - p23[1 ] * x * y
163+ D (y) ~ - p23[2 ] * y + p4 * x * y]
164+
165+ sys = structural_simplify (complete (ODESystem (
166+ eqs, t, tspan = (0 , 3.0 ), name = :sys , parameter_dependencies = [y0 => 2 p4])))
167+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys)
168+ end
169+
170+ @testset " level$i " for (i, prob) in enumerate ([level1 (), level2 (), level3 ()])
171+ ps = prob. p
172+ @testset " Type stability of $portion " for portion in [Tunable (), Discrete (), Constants ()]
173+ @test_call canonicalize (portion, ps)
174+ # @inferred canonicalize(portion, ps)
175+ broken =
176+ (i ∈ [2 ,3 ] && portion == Tunable ())
177+
178+ # broken because the size of a vector of vectors can't be determined at compile time
179+ @test_opt broken= broken target_modules = (ModelingToolkit,) canonicalize (
180+ portion, ps)
181+
182+ buffer, repack, alias = canonicalize (portion, ps)
183+
184+ @test_call SciMLStructures. replace (portion, ps, ones (length (buffer)))
185+ @inferred SciMLStructures. replace (portion, ps, ones (length (buffer)))
186+ @test_opt target_modules= (ModelingToolkit,) SciMLStructures. replace (
187+ portion, ps, ones (length (buffer)))
188+
189+ @test_call target_modules = (ModelingToolkit,) SciMLStructures. replace! (
190+ portion, ps, ones (length (buffer)))
191+ @inferred SciMLStructures. replace! (portion, ps, ones (length (buffer)))
192+ @test_opt target_modules= (ModelingToolkit,) SciMLStructures. replace! (
193+ portion, ps, ones (length (buffer)))
194+ end
195+ end
0 commit comments