How to reinit NonlinearSolve.jl problem/integrator?

Consider

f(u, p) = u .* u .- p
u0 = @SVector[1.0, 1.0]
prob = NonlinearProblem(f, u0, [1])
solver = solve(prob)

How do I reinitialize the problem or the underlying integrator/cache with new u0 and p?

If you’re using the version that landed 2 weeks ago: What is a Nonlinear Solver? And How to easily build Newer Ones by avik-pal · Pull Request #345 · SciML/NonlinearSolve.jl · GitHub, it should just work like the ODE solver. Use init, reinit! and step!. Not fully documented yet, but what is documented is on latest Nonlinear Solver Iterator Interface · NonlinearSolve.jl.

1 Like

This seems to work:

f(u, p) = u .* u .- p
u0 = @SVector[1.0, 1.0]
prob = NonlinearProblem(f, u0, [3])
nlcache = init(prob, NewtonRaphson())
@show 0, nlcache.u
for i in 1:4
  step!(nlcache)
  @show i, nlcache.u
end

reinit!(nlcache, (@SVector[2.,1.]); p=[3])
@show 0, nlcache.u
for i in 1:4
  step!(nlcache)
  @show i, nlcache.u
end
(0, nlcache.u) = (0, [1.0, 1.0])
(i, nlcache.u) = (1, [2.0, 2.0])
(i, nlcache.u) = (2, [1.75, 1.75])
(i, nlcache.u) = (3, [1.7321428571428572, 1.7321428571428572])
(i, nlcache.u) = (4, [1.7320508100147276, 1.7320508100147276])

(0, nlcache.u) = (0, [2.0, 1.0])
(i, nlcache.u) = (1, [1.75, 2.0])
(i, nlcache.u) = (2, [1.7321428571428572, 1.75])
(i, nlcache.u) = (3, [1.7320508100147276, 1.7321428571428572])
(i, nlcache.u) = (4, [1.7320508075688772, 1.7320508100147276])

How do I replace the manual stepping via for loops with something similar to solve? solve(nlcache) errors.

julia> solve(nlcache)
ERROR: MethodError: no method matching init(::NonlinearSolve.GeneralizedFirstOrderAlgorithmCache{…})

Closest candidates are:
  init(::SciMLBase.AbstractJumpProblem, Any...; kwargs...)
   @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:545
  init(::NonlinearProblem{<:Union{Number, var"#s157"} where var"#s157"<:AbstractArray, iip, <:Union{var"#s156", var"#s155"} where {var"#s156"<:ForwardDiff.Dual{T, V, P}, var"#s155"<:(AbstractArray{<:ForwardDiff.Dual{T, V, P}})}}, ::Union{Nothing, SciMLBase.AbstractNonlinearAlgorithm}, ::Any...; kwargs...) where {T, V, P, iip}
   @ NonlinearSolve ~/.julia/packages/NonlinearSolve/UPIol/src/internal/forward_diff.jl:37
  init(::PDEProblem, ::SciMLBase.AbstractDEAlgorithm, ::Any...; kwargs...)
   @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:1140
  ...

Stacktrace:
 [1] solve(args::NonlinearSolve.GeneralizedFirstOrderAlgorithmCache{…}; kwargs::@Kwargs{})
   @ CommonSolve ~/.julia/packages/CommonSolve/JfpfI/src/CommonSolve.jl:23
 [2] top-level scope
   @ REPL[67]:1
Some type information was truncated. Use `show(err)` to see complete types.

solve!

1 Like