|
| 1 | +using ModelingToolkitBase |
| 2 | +using ModelingToolkitBase: t_nounits as t, MTKParameters |
| 3 | +using SciMLBase |
| 4 | +using SciMLStructures: SciMLStructures, canonicalize, Tunable, Discrete, Constants |
| 5 | +using JET |
| 6 | +using Test |
| 7 | + |
| 8 | +# scalar parameters only |
| 9 | +function level1() |
| 10 | + @parameters p1 = 0.5 [tunable = true] p2 = 1 [tunable = true] p3 = 3 [tunable = false] p4 = 3 [tunable = true] y0 |
| 11 | + @variables x(t) = 2 y(t) = y0 |
| 12 | + D = Differential(t) |
| 13 | + |
| 14 | + eqs = [ |
| 15 | + D(x) ~ p1 * x - p2 * x * y |
| 16 | + D(y) ~ -p3 * y + p4 * x * y |
| 17 | + ] |
| 18 | + |
| 19 | + sys = mtkcompile( |
| 20 | + complete( |
| 21 | + System( |
| 22 | + eqs, t, name = :sys, bindings = [y0 => 2p4] |
| 23 | + ) |
| 24 | + ) |
| 25 | + ) |
| 26 | + return prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) |
| 27 | +end |
| 28 | + |
| 29 | +# scalar and vector parameters |
| 30 | +function level2() |
| 31 | + @parameters p1 = 0.5 [tunable = true] (p23[1:2] = [1, 3.0]) [tunable = true] p4 = 3 [tunable = false] y0 |
| 32 | + @variables x(t) = 2 y(t) = y0 |
| 33 | + D = Differential(t) |
| 34 | + |
| 35 | + eqs = [ |
| 36 | + D(x) ~ p1 * x - p23[1] * x * y |
| 37 | + D(y) ~ -p23[2] * y + p4 * x * y |
| 38 | + ] |
| 39 | + |
| 40 | + sys = mtkcompile( |
| 41 | + complete( |
| 42 | + System( |
| 43 | + eqs, t, name = :sys, bindings = [y0 => 2p4] |
| 44 | + ) |
| 45 | + ) |
| 46 | + ) |
| 47 | + return prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) |
| 48 | +end |
| 49 | + |
| 50 | +# scalar and vector parameters with different scalar types |
| 51 | +function level3() |
| 52 | + @parameters p1 = 0.5 [tunable = true] (p23[1:2] = [1, 3.0]) [tunable = true] p4::Int = 3 [tunable = true] y0::Int |
| 53 | + @variables x(t) = 2 y(t) = y0 |
| 54 | + D = Differential(t) |
| 55 | + |
| 56 | + eqs = [ |
| 57 | + D(x) ~ p1 * x - p23[1] * x * y |
| 58 | + D(y) ~ -p23[2] * y + p4 * x * y |
| 59 | + ] |
| 60 | + |
| 61 | + sys = mtkcompile( |
| 62 | + complete( |
| 63 | + System( |
| 64 | + eqs, t, name = :sys, bindings = [y0 => 2p4] |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + return prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) |
| 69 | +end |
| 70 | + |
| 71 | +@testset "level$i" for (i, prob) in enumerate([level1(), level2(), level3()]) |
| 72 | + ps = prob.p |
| 73 | + @testset "Type stability of $portion" for portion in [ |
| 74 | + Tunable(), Discrete(), Constants(), |
| 75 | + ] |
| 76 | + @test_call canonicalize(portion, ps) |
| 77 | + |
| 78 | + # broken because the size of a vector of vectors can't be determined at compile time |
| 79 | + @test_opt target_modules = (ModelingToolkitBase,) canonicalize( |
| 80 | + portion, ps |
| 81 | + ) |
| 82 | + |
| 83 | + buffer, repack, alias = canonicalize(portion, ps) |
| 84 | + |
| 85 | + # broken because dependent update functions break inference |
| 86 | + @test_call target_modules = (ModelingToolkitBase,) SciMLStructures.replace( |
| 87 | + portion, ps, ones(length(buffer)) |
| 88 | + ) |
| 89 | + @test_opt target_modules = (ModelingToolkitBase,) SciMLStructures.replace( |
| 90 | + portion, ps, ones(length(buffer)) |
| 91 | + ) |
| 92 | + |
| 93 | + @test_call target_modules = (ModelingToolkitBase,) SciMLStructures.replace!( |
| 94 | + portion, ps, ones(length(buffer)) |
| 95 | + ) |
| 96 | + @test_opt target_modules = (ModelingToolkitBase,) SciMLStructures.replace!( |
| 97 | + portion, ps, ones(length(buffer)) |
| 98 | + ) |
| 99 | + end |
| 100 | +end |
0 commit comments