Skip to content

fix: record the element type of indices into non-tunable array parameters in CopyParamsByTemplate - #5238

Merged
baggepinnen merged 1 commit into
masterfrom
fb/copyparams-template-eltype
Oct 1, 2026
Merged

baggepinnen merged 1 commit into
masterfrom
fb/copyparams-template-eltype

Conversation

@baggepinnen

@baggepinnen baggepinnen commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

CopyParamsByTemplate(srcsys, syms) records the type of each template entry in elem_types, and __specialize_templates converts a template of more than four entries to a Vector of the union of these types. The branch for an index into a non-tunable array parameter, _bufidx isa Tuple{Vararg{Int}}, pushed the entry to the template without recording its type. With more than four such entries, the conversion fails:

ArgumentError: cannot convert a value to Union{} for assignment
  [1] convert(::Core.TypeofBottom, ::ModelingToolkitBase.ParameterIndex{SciMLStructures.Constants, Tuple{Int64, Int64, Int64}})
  ...
 [10] Vector{Union{}}(x::Vector{Any})
 [11] __specialize_templates(template::Vector{Any}, elem_types::Set{DataType})
 [12] ModelingToolkitBase.CopyParamsByTemplate(srcsys::System, syms::Vector{...})
 ...
 [20] construct_initializeprobpmap(sys::System, initsys::System; ...)
 ...
 [35] ODEProblem(sys::System, op::Vector{Any}, tspan::Tuple{Float64, Float64})

This occurred when building the ODEProblem of a closed-loop model with a linear MPC controller whose output dimension changed from four to five. The initialization problem then copies three non-tunable array parameters of five entries each, element by element. With four outputs the template has at most four entries and is converted to a Tuple, so the missing type has no effect.

Change

The branch records typeof(symidx) in elem_types, as the other branches do.

The new test in test/initial_values.jl builds the template of the five elements of a non-tunable array parameter and evaluates it. Without the change it errors with the message above; with the change it passes (run with Julia 1.12.7 against this branch).


Host: battomenstation · Claude Code session fab5403f-e741-4cad-a739-8ff803fe0f80

🤖 Generated with Claude Code

https://claude.ai/code/session_01SYKqL6Rd8TvbY8hGUX4uLL

…ters in `CopyParamsByTemplate`

The branch for an index into a non-tunable array parameter pushed the template entry without its type
to `elem_types`. With more than four entries, `__specialize_templates` then converted the template to
a vector of the union of the recorded types, which does not contain the type of these entries, and
failed with "cannot convert a value to Union{} for assignment", e.g. when building the
initialization problem of a system whose parameters include such arrays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SYKqL6Rd8TvbY8hGUX4uLL
@baggepinnen
baggepinnen marked this pull request as ready for review October 1, 2026 10:59
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia vlts)

Time benchmarks
master bb38ae9... master / bb38ae9...
ODEProblem 5.62 ± 0.11 ms 5.57 ± 0.093 ms 1.01 ± 0.026
init 0.106 ± 0.028 ms 0.103 ± 0.03 ms 1.03 ± 0.4
large_parameter_init/ODEProblem 16.1 ± 0.65 ms 16.1 ± 0.49 ms 1 ± 0.051
large_parameter_init/init 0.0952 ± 0.03 ms 0.0946 ± 0.029 ms 1.01 ± 0.44
mtkcompile 10.1 ± 0.2 ms 10.1 ± 0.2 ms 1 ± 0.028
sparse_analytical_jacobian/ODEProblem 19.9 ± 0.7 ms 19.7 ± 0.41 ms 1.01 ± 0.041
sparse_analytical_jacobian/f_iip 0.07 ± 0.001 μs 0.07 ± 0.001 μs 1 ± 0.02
sparse_analytical_jacobian/f_oop 0.37 ± 0.01 ms 0.369 ± 0.011 ms 1 ± 0.04
time_to_load 7.22 ± 0.08 s 7.1 ± 0.046 s 1.02 ± 0.013
Memory benchmarks
master bb38ae9... master / bb38ae9...
ODEProblem 0.0346 M allocs: 1.82 MB 0.0346 M allocs: 1.81 MB 1.01
init 0.486 k allocs: 0.0779 MB 0.486 k allocs: 0.0779 MB 1
large_parameter_init/ODEProblem 0.147 M allocs: 5.54 MB 0.147 M allocs: 5.54 MB 1
large_parameter_init/init 0.618 k allocs: 0.172 MB 0.618 k allocs: 0.172 MB 1
mtkcompile 0.0594 M allocs: 3.37 MB 0.0594 M allocs: 3.37 MB 1
sparse_analytical_jacobian/ODEProblem 0.106 M allocs: 5.33 MB 0.106 M allocs: 5.33 MB 1
sparse_analytical_jacobian/f_iip 0 allocs: 0 B 0 allocs: 0 B
sparse_analytical_jacobian/f_oop 0.634 k allocs: 19.6 kB 0.634 k allocs: 19.6 kB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master bb38ae9... master / bb38ae9...
ODEProblem 4.89 ± 0.082 ms 4.93 ± 0.11 ms 0.993 ± 0.028
init 0.046 ± 0.0053 ms 0.0468 ± 0.0055 ms 0.984 ± 0.16
large_parameter_init/ODEProblem 13.6 ± 1.1 ms 14.8 ± 1.1 ms 0.918 ± 0.1
large_parameter_init/init 0.0678 ± 0.022 ms 0.0677 ± 0.021 ms 1 ± 0.45
mtkcompile 8.61 ± 0.88 ms 8.63 ± 0.87 ms 0.998 ± 0.14
sparse_analytical_jacobian/ODEProblem 17.5 ± 2 ms 18 ± 1.6 ms 0.974 ± 0.14
sparse_analytical_jacobian/f_iip 0.06 ± 0.01 μs 0.07 ± 0.001 μs 0.857 ± 0.14
sparse_analytical_jacobian/f_oop 0.106 ± 0.012 ms 0.107 ± 0.013 ms 0.99 ± 0.16
time_to_load 5.29 ± 0.11 s 5.24 ± 0.018 s 1.01 ± 0.021
Memory benchmarks
master bb38ae9... master / bb38ae9...
ODEProblem 0.034 M allocs: 1.59 MB 0.034 M allocs: 1.59 MB 1
init 0.475 k allocs: 0.0602 MB 0.475 k allocs: 0.0602 MB 1
large_parameter_init/ODEProblem 0.144 M allocs: 4.8 MB 0.143 M allocs: 4.79 MB 1
large_parameter_init/init 0.757 k allocs: 0.166 MB 0.757 k allocs: 0.166 MB 1
mtkcompile 0.0558 M allocs: 2.65 MB 0.0558 M allocs: 2.63 MB 1.01
sparse_analytical_jacobian/ODEProblem 0.103 M allocs: 4.25 MB 0.103 M allocs: 4.25 MB 1
sparse_analytical_jacobian/f_iip 0 allocs: 0 B 0 allocs: 0 B
sparse_analytical_jacobian/f_oop 0.848 k allocs: 27 kB 0.848 k allocs: 27 kB 1
time_to_load 0.2 k allocs: 11.7 kB 0.2 k allocs: 11.7 kB 1

@baggepinnen
baggepinnen merged commit 34711f6 into master Oct 1, 2026
101 of 111 checks passed
@baggepinnen
baggepinnen deleted the fb/copyparams-template-eltype branch October 26, 2026 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants