@@ -4,6 +4,7 @@ using SymbolicIndexingInterface
44using SciMLStructures: SciMLStructures, canonicalize, Tunable, Discrete, Constants
55using OrdinaryDiffEq
66using ForwardDiff
7+ using JET
78
89@parameters a b c d:: Integer e[1 : 3 ] f[1 : 3 , 1 : 3 ]:: Int g:: Vector{AbstractFloat} h:: String
910@named sys = ODESystem (
@@ -136,6 +137,77 @@ ps = [p => 1.0] # Value for `d` is missing
136137@test_throws ModelingToolkit. MissingVariablesError ODEProblem (sys, u0, tspan, ps)
137138@test_nowarn ODEProblem (sys, u0, tspan, [ps... , d => 1.0 ])
138139
140+ # JET tests
141+
142+ # scalar parameters only
143+ function level1 ()
144+ @parameters p1= 0.5 [tunable = true ] p2= 1 [tunable = true ] p3= 3 [tunable = false ] p4= 3 [tunable = true ] y0= 1
145+ @variables x (t)= 2 y (t)= y0
146+ D = Differential (t)
147+
148+ eqs = [D (x) ~ p1 * x - p2 * x * y
149+ D (y) ~ - p3 * 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
157+ function level2 ()
158+ @parameters p1= 0.5 [tunable = true ] (p23[1 : 2 ]= [1 , 3.0 ]) [tunable = true ] p4= 3 [tunable = false ] y0= 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+ # scalar and vector parameters with different scalar types
171+ function level3 ()
172+ @parameters p1= 0.5 [tunable = true ] (p23[1 : 2 ]= [1 , 3.0 ]) [tunable = true ] p4:: Int = 3 [tunable = true ] y0:: Int = 1
173+ @variables x (t)= 2 y (t)= y0
174+ D = Differential (t)
175+
176+ eqs = [D (x) ~ p1 * x - p23[1 ] * x * y
177+ D (y) ~ - p23[2 ] * y + p4 * x * y]
178+
179+ sys = structural_simplify (complete (ODESystem (
180+ eqs, t, tspan = (0 , 3.0 ), name = :sys , parameter_dependencies = [y0 => 2 p4])))
181+ prob = ODEProblem {true, SciMLBase.FullSpecialize} (sys)
182+ end
183+
184+ @testset " level$i " for (i, prob) in enumerate ([level1 (), level2 (), level3 ()])
185+ ps = prob. p
186+ @testset " Type stability of $portion " for portion in [
187+ Tunable (), Discrete (), Constants ()]
188+ @test_call canonicalize (portion, ps)
189+ # @inferred canonicalize(portion, ps)
190+ broken = (i ∈ [2 , 3 ] && portion == Tunable ())
191+
192+ # broken because the size of a vector of vectors can't be determined at compile time
193+ @test_opt broken= broken target_modules= (ModelingToolkit,) canonicalize (
194+ portion, ps)
195+
196+ buffer, repack, alias = canonicalize (portion, ps)
197+
198+ @test_call SciMLStructures. replace (portion, ps, ones (length (buffer)))
199+ @inferred SciMLStructures. replace (portion, ps, ones (length (buffer)))
200+ @test_opt target_modules= (ModelingToolkit,) SciMLStructures. replace (
201+ portion, ps, ones (length (buffer)))
202+
203+ @test_call target_modules= (ModelingToolkit,) SciMLStructures. replace! (
204+ portion, ps, ones (length (buffer)))
205+ @inferred SciMLStructures. replace! (portion, ps, ones (length (buffer)))
206+ @test_opt target_modules= (ModelingToolkit,) SciMLStructures. replace! (
207+ portion, ps, ones (length (buffer)))
208+ end
209+ end
210+
139211# Issue#2642
140212@parameters α β γ δ
141213@variables x (t) y (t)
0 commit comments