Skip to content

Commit 8f6828c

Browse files
test: ensure tunable_parameters are now correctly ordered
1 parent 00c56f2 commit 8f6828c

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/variables.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,12 @@ Create a tunable parameter by
381381
@parameters u [tunable=true]
382382
```
383383
384-
See also [`getbounds`](@ref), [`istunable`](@ref)
384+
For systems created with `split = true` (the default) and `default = true` passed to this function, the order
385+
of parameters returned is the order in which they are stored in the tunables portion of `MTKParameters`. Note
386+
that array variables will not be scalarized. To obtain the flattened representation of the tunables portion,
387+
call `Symbolics.scalarize(tunable_parameters(sys))` and concatenate the resulting arrays.
388+
389+
See also [`getbounds`](@ref), [`istunable`](@ref), [`MTKParameters`](@ref), [`complete`](@ref)
385390
"""
386391
function tunable_parameters(sys, p = parameters(sys); default = true)
387392
filter(x -> istunable(x, default), p)

test/index_cache.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ModelingToolkit, SymbolicIndexingInterface
1+
using ModelingToolkit, SymbolicIndexingInterface, SciMLStructures
22
using ModelingToolkit: t_nounits as t
33

44
# Ensure indexes of array symbolics are cached appropriately
@@ -43,3 +43,23 @@ ic = ModelingToolkit.get_index_cache(sys)
4343
@test isequal(ic.symbol_to_variable[:x], x)
4444
@test isequal(ic.symbol_to_variable[:y], y)
4545
@test isequal(ic.symbol_to_variable[:z], z)
46+
47+
@testset "tunable_parameters is ordered" begin
48+
@parameters p q[1:3] r[1:2, 1:2] s [tunable = false]
49+
@named sys = ODESystem(Equation[], t, [], [p, q, r, s])
50+
sys = complete(sys)
51+
@test all(splat(isequal), zip(tunable_parameters(sys), parameters(sys)[1:3]))
52+
53+
offset = 1
54+
for par in tunable_parameters(sys)
55+
idx = parameter_index(sys, par)
56+
@test idx.portion isa SciMLStructures.Tunable
57+
if Symbolics.isarraysymbolic(par)
58+
@test vec(idx.idx) == offset:(offset + length(par) - 1)
59+
else
60+
@test idx.idx == offset
61+
end
62+
offset += length(par)
63+
end
64+
end
65+

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ end
2424
@safetestset "Parsing Test" include("variable_parsing.jl")
2525
@safetestset "Simplify Test" include("simplify.jl")
2626
@safetestset "Direct Usage Test" include("direct.jl")
27-
@safetestset "IndexCache Test" include("index_cache.jl")
2827
@safetestset "System Linearity Test" include("linearity.jl")
2928
@safetestset "Input Output Test" include("input_output_handling.jl")
3029
@safetestset "Clock Test" include("clock.jl")
@@ -65,6 +64,7 @@ end
6564

6665
if GROUP == "All" || GROUP == "InterfaceII"
6766
@testset "InterfaceII" begin
67+
@safetestset "IndexCache Test" include("index_cache.jl")
6868
@safetestset "Variable Utils Test" include("variable_utils.jl")
6969
@safetestset "Variable Metadata Test" include("test_variable_metadata.jl")
7070
@safetestset "OptimizationSystem Test" include("optimizationsystem.jl")

0 commit comments

Comments
 (0)