# SciMLStructures Tunable includes u0 causing remake failures

**URL:** <https://discourse.julialang.org/t/scimlstructures-tunable-includes-u0-causing-remake-failures/126090>\
**Category:** Modelling & Simulations\
**Created:** [February 19, 2025, 9:40pm UTC](https://discourse.julialang.org/t/scimlstructures-tunable-includes-u0-causing-remake-failures/126090 "2025-02-19T21:40:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnb/32/44115_2.png) [@johnb](https://discourse.julialang.org/u/johnb)\
**Post date:** [February 19, 2025, 9:40pm UTC](https://discourse.julialang.org/t/scimlstructures-tunable-includes-u0-causing-remake-failures/126090/1 "2025-02-19T21:40:19Z")

</div>

I’ve been trying to understand how to use the `Tunable` parameter tag, but it seems to include initial conditions when returning a set of parameters as from the example in the ModelingToolkit.jl FAQ page. I’ve included an example below. Am I using this wrong, is there a bug, or am I intended to pass u0 appended to my tunables?

Thanks!

```julia
using ModelingToolkit, SciMLStructures, SymbolicIndexingInterface, DifferentialEquations
using SciMLStructures: replace, replace!, Tunable
using ModelingToolkit: t_nounits as t, D_nounits as D

@variables u(t)[1:5]
@parameters k[1:5] [tunable=true]
@parameters c[1:5] [tunable=false]

eqs = [D(u[i])~k[i]*u[i]/c[i] for i in 1:5];

@mtkbuild sys = ODESystem(eqs, t);

u0 = [1.0, 2.0, 3.0, 4.0, 5.0];

odeprob = ODEProblem(sys, [u=>u0], (0, 1), [k => 6*ones(5), c=>7*ones(5)])
@show odeprob.p[1];

newps = 8*ones(5);
currentps = parameter_values(odeprob) # obtain the parameter object from the problem
ps = SciMLStructures.replace(Tunable(), currentps, newps) # create a copy with the tunable values replaced
# remake the problem using the new parameter object
newprob = remake(odeprob, p = ps)

```

Interestingly, `@show odeprob.p[1]` returns the following:

```julia
odeprob.p[1] = [6.0, 6.0, 6.0, 6.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0]

```

It can be seen from above that the 6.0 values are the tunable parameters, but the vector `[1.0, 2.0, 3.0, 4.0, 5.0]` appended to those are clearly from the `u0` vector also passed in when constructing the ODEProblem.

This difference can also be seen in the `currentps` and `ps` lines, where the first element of the tuple is a different length, indicating that u0 is also present when obtaining the parameters that way.

Of course, since the new parameters are of a different length than the old parameters, `replace()` fails (as expected) and gives the following error:

```julia
ERROR: BoundsError: attempt to access 5-element Vector{Float64} at index [6]
Stacktrace:
  [1] throw_boundserror(A::Vector{Float64}, I::Tuple{Int64})
    @ Base .\essentials.jl:14
  [2] getindex
    @ .\essentials.jl:916 [inlined]
  [3] _ducktyped_parameter_values
    @ C:\Users\johnb\.julia\packages\ModelingToolkit\JpO3W\src\systems\parameter_buffer.jl:301 [inlined]
  [4] parameter_values
    @ C:\Users\johnb\.julia\packages\ModelingToolkit\JpO3W\src\systems\parameter_buffer.jl:296 [inlined]
  [5] parameter_values
    @ C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\value_provider_interface.jl:23 [inlined]
  [6] GetParameterIndex
    @ C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\parameter_indexing.jl:57 [inlined]
  [7] AbstractParameterGetIndexer
    @ C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\value_provider_interface.jl:168 [inlined]
  [8] (::SymbolicIndexingInterface.var"#50#51"{…})(g::SymbolicIndexingInterface.GetParameterIndex{…})
    @ SymbolicIndexingInterface C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\state_indexing.jl:214
  [9] iterate
    @ .\generator.jl:48 [inlined]
 [10] collect_to!(dest::Vector{…}, itr::Base.Generator{…}, offs::Int64, st::Int64)
    @ Base .\array.jl:849
 [11] collect_to!(dest::Vector{…}, itr::Base.Generator{…}, offs::Int64, st::Int64)
    @ Base .\array.jl:857
 [12] collect_to_with_first!(dest::Vector{…}, v1::Float64, itr::Base.Generator{…}, st::Int64)
    @ Base .\array.jl:827
 [13] _collect(c::Vector{…}, itr::Base.Generator{…}, ::Base.EltypeUnknown, isz::Base.HasShape{…})
    @ Base .\array.jl:821
 [14] collect_similar
    @ .\array.jl:720 [inlined]
 [15] map
    @ .\abstractarray.jl:3371 [inlined]
 [16] MultipleGetters
    @ C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\state_indexing.jl:214 [inlined]
 [17] MultipleGetters
    @ C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\state_indexing.jl:191 [inlined]
 [18] (::SymbolicIndexingInterface.MultipleGetters{…})(prob::ProblemState{…})
    @ SymbolicIndexingInterface C:\Users\johnb\.julia\packages\SymbolicIndexingInterface\MHJDc\src\value_provider_interface.jl:166
 [19] (::ModelingToolkit.ReconstructInitializeprob)(srcvalp::ProblemState{…}, dstvalp::NonlinearProblem{…})
    @ ModelingToolkit C:\Users\johnb\.julia\packages\ModelingToolkit\JpO3W\src\systems\nonlinear\initializesystem.jl:321
 [20] remake_initialization_data(sys::ODESystem, odefn::Function, u0::Missing, t0::Int64, p::MTKParameters{…}, newu0::Vector{…}, newp::MTKParameters{…})
    @ ModelingToolkit C:\Users\johnb\.julia\packages\ModelingToolkit\JpO3W\src\systems\nonlinear\initializesystem.jl:424
 [21] remake(prob::ODEProblem{…}; f::Missing, u0::Missing, tspan::Missing, p::MTKParameters{…}, kwargs::Missing, interpret_symbolicmap::Bool, build_initializeprob::Bool, use_defaults::Bool, lazy_initialization::Nothing, _kwargs::@Kwargs{})
    @ SciMLBase C:\Users\johnb\.julia\packages\SciMLBase\YTOjh\src\remake.jl:227
 [22] top-level scope
    @ tunable_testing.jl:23
Some type information was truncated. Use `show(err)` to see complete types.

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 2, 2025, 11:13pm UTC](https://discourse.julialang.org/t/scimlstructures-tunable-includes-u0-causing-remake-failures/126090/2 "2025-03-02T23:13:16Z")

</div>

This was a bug with a recent release. We’re working to remedy it.

@cryptic.ax
