Skip to content

Conversation

@AayushSabharwal
Copy link
Member

Close #3990

@sablonl
Copy link

sablonl commented Nov 3, 2025

The fix works well when the parameter is a vector, but I still get an error with solve when it's a matrix:

MRE:

using Pkg;
Pkg.activate(temp = true)

Pkg.add(url="https://github.com/AayushSabharwal/ModelingToolkit.jl", rev="as/event-bugs")
Pkg.add("OrdinaryDiffEqTsit5")

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEqTsit5

N = 2
@parameters s(t)::Float64
@parameters v(t)[1:N]::Float64
@parameters M(t)[1:N, 1:N]::Float64

@variables x(t)::Float64

Mini = rand(N, N) ./ (N^2)
vini = vec(sum(Mini, dims = 1))
sMini = sum(Mini)

s_eq = [D(x) ~ x * s]
v_eq = [D(x) ~ x * Symbolics.scalarize(sum(v))]
M_eq = [D(x) ~ x * Symbolics.scalarize(sum(M))]


s_event = ModelingToolkit.SymbolicDiscreteCallback(
	1.0,
	[s ~ -Pre(s)],
	discrete_parameters = [s],
)

v_event = ModelingToolkit.SymbolicDiscreteCallback(
	1.0,
	[v ~ -Pre(v)],
	discrete_parameters = [v], # replacing with collect(v) works
)

M_event = ModelingToolkit.SymbolicDiscreteCallback(
	1.0,
	[M ~ -Pre(M)],
	discrete_parameters = [M], # replacing with collect(M) still errors
)

@mtkcompile s_sys = System(s_eq, t; discrete_events = s_event)
@mtkcompile v_sys = System(v_eq, t; discrete_events = v_event)
@mtkcompile M_sys = System(M_eq, t; discrete_events = M_event)

u0p0_map = Dict(x => 1.0, M => Mini, s => sMini, v => vini)

s_prob = ODEProblem(s_sys, u0p0_map, (0.0, 2.5))
v_prob = ODEProblem(v_sys, u0p0_map, (0.0, 2.5))
M_prob = ODEProblem(M_sys, u0p0_map, (0.0, 2.5))

s_sol = solve(s_prob, Tsit5()) #, tstops = 1.0) # perfect
v_sol = solve(v_prob, Tsit5()) #, tstops = 1.0) # perfect
M_sol = solve(M_prob, Tsit5()) #, tstops = 1.0) # fails
perfect

# using Plots
# plot(s_sol, label = "s event")
# plot!(v_sol, label = "v event")
# plot!(M_sol, label = "M event")

with

julia> show(err)
1-element ExceptionStack:
LoadError: MethodError: Cannot `convert` an object of type 
  ModelingToolkit.NestedGetIndex{Tuple{Array{Matrix{Float64},1}}} to an object of type 
  ModelingToolkit.NestedGetIndex{Tuple{Array{Base.ReshapedArray{Float64, 2, SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, Tuple{}},1}}}
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  (::Type{ModelingToolkit.NestedGetIndex{T}} where T)(::Any)
   @ ModelingToolkit /cyfast/lsablon/.julia/packages/ModelingToolkit/75pJw/src/systems/parameter_buffer.jl:801
  convert(::Type{T}, ::T) where T
   @ Base Base_compiler.jl:133

@AayushSabharwal
Copy link
Member Author

Amazing, thanks for trying it out and for the MWE. I'll patch it.

@AayushSabharwal
Copy link
Member Author

InterfaceII is fixed in another PR

@AayushSabharwal AayushSabharwal merged commit 8d6be1e into SciML:master Nov 4, 2025
41 of 51 checks passed
@AayushSabharwal AayushSabharwal deleted the as/event-bugs branch November 4, 2025 05:17
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.

Cannot update vector parameters with

2 participants