SciML / SciMLBase.jl

The Base interface of the SciML ecosystem
https://docs.sciml.ai/SciMLBase/stable
MIT License
118 stars 91 forks source link

Symbolic indexing fails for Nonlinear problem integrators #662

Closed TorkelE closed 2 months ago

TorkelE commented 2 months ago

A rather niche situation. It was discussed in private correspondence that it probably should work though, but realised there wasn't actually an issue to keep track of it, so raising one here.

using ModelingToolkit, NonlinearSolve

# Creates our system.
@parameters p d
@variables X(t)
eqs = [0 ~ sin(X+p) - d*sqrt(X+1)]
@mtkbuild nlsys = NonlinearSystem(eqs, [X], [p, d])

# Creates an integrator.
nlprob = NonlinearProblem(nlsys, [X => 1.0], [p => 2.0, d => 3.0])
nint = init(nlprob, NewtonRaphson())

# All yield errors.
nint[X]
nint[nlsys.X]
nint[:X]
nint.ps[p]
nint.ps[nlsys.p]
nint.ps[:p]
TorkelE commented 3 weeks ago

A slight follow-up on this. Is it intentional that you cannot use setu on NonlinearProblem integrators (but setp, getp, andgetu` all works)? I don't really see in what situation this actually makes sense to do (but neither updating parameters, that was why I got confused).


using ModelingToolkit, NonlinearSolve

# Creates our system.
@parameters p d
@variables t X(t)
eqs = [0 ~ sin(X+p) - d*sqrt(X+1)]
@mtkbuild nlsys = NonlinearSystem(eqs, [X], [p, d])

# Creates an integrator.
nlprob = NonlinearProblem(nlsys, [X => 1.0], [p => 2.0, d => 3.0])
nint = init(nlprob, NewtonRaphson())

nint[X] # works
nint[nlsys.X] # works
nint[:X] # works
nint.ps[p] # works
nint.ps[nlsys.p] # works
nint.ps[:p] # works
nint[X] = 1 # Error
nint[nlsys.X] = 1 # Error
nint[:X] = 1 # Error
nint.ps[p] = 1 # works
nint.ps[nlsys.p] = 1 # works
nint.ps[:p] = 1 # works
ChrisRackauckas commented 3 weeks ago

Not intentional, just missing an overload.

AayushSabharwal commented 3 weeks ago

setindex! doesn't work, setu does. I'll add the setindex! method

AayushSabharwal commented 3 weeks ago

https://github.com/SciML/NonlinearSolve.jl/pull/447