ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Passing initial state of submodel silently fails

Open JTaets opened this issue 3 years ago • 2 comments

This first codeblock is slightly adopted from the docs (and works).

@variables t x(t)=0.0 RHS(t)
@parameters τ=3.0

D = Differential(t) 
@named fol = ODESystem([ D(x)  ~ (1 - x)/τ])

prob = ODEProblem(fol, [x => 0.1], (0.0,10.0), [])
@assert prob.u0[1] == 0.1

In the next codeblock, the initial state [fol.x => 0.1] is ignored even without throwing an error.

function Fol(;name)
    @variables t x(t)=0.0 RHS(t)
    @parameters τ=3.0
    D = Differential(t) 
    ODESystem([ D(x)  ~ (1 - x)/τ];name)
end
@named fol = Fol()

prob = ODEProblem(fol, [fol.x => 0.1], (0.0,10.0), [])
@assert prob.u0[1] == 0.1

IMHO, this is an intuitive way to me atleast of setting the initial state, I was kinda surprised that it fails. So how do I do this correctly?

EDIT: related is #1299

JTaets avatar Jun 10 '22 13:06 JTaets

Use @unpack x = fol.

YingboMa avatar Jul 01 '22 05:07 YingboMa

prob = ODEProblem(fol, [fol.x => 0.1], (0.0,10.0), [])

I make this error all the time and it really is a huge footgun. It would be nice to detect any incoming variable specification that has the namespace fol in this case and throw a warning (or error if more appropriate). If we could just do the right thing despite the namespace fol.x, that'd be even better.

Arguably, ODEProblem(fol, [fol.x => 0.1], (0.0,10.0), []) this is the most natural and intuitive way of specifying the parameter, having to deal with the @unpack is very unfortunate. When I started using MTK, this was a very big pain point, and it still is for me.

baggepinnen avatar Jul 01 '22 09:07 baggepinnen

Fixed by complete

ChrisRackauckas avatar Feb 22 '24 16:02 ChrisRackauckas