SciML / NonlinearSolve.jl

High-performance and differentiation-enabled nonlinear solvers (Newton methods), bracketed rootfinding (bisection, Falsi), with sparsity and Newton-Krylov support.
https://docs.sciml.ai/NonlinearSolve/stable/
MIT License
232 stars 40 forks source link

`TrustRegion(radius_update_scheme=RadiusUpdateSchemes.Bastin)` doesn't respect autodiff setting . #360

Closed oscardssmith closed 8 months ago

oscardssmith commented 8 months ago

When solving a nonlinear problem that doesn't support autodiff, the Bastin update scheme uses autodiff anyways. This is especially problematic because Bastin is used in the default algorithm.

Minimal Reproducible Example 👇

Without MRE, we would only be able to help you to a limited extent, and attention to the issue would be limited. to know more about MRE refer to wikipedia and stackoverflow.

using NonlinearSolve
g(u::Vector{Float64},p) = u
p = NonlinearProblem(g, [1.])
solve(p, TrustRegion(autodiff=AutoFiniteDiff(), radius_update_scheme=RadiusUpdateSchemes.Bastin))

Error & Stacktrace ⚠️

ERROR: MethodError: no method matching g(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{…}, Float64, 1}}, ::SciMLBase.NullParameters)
An arithmetic operation was performed on a NullParameters object. This means no parameters were passed
into the AbstractSciMLProblem (e.x.: ODEProblem) but the parameters object `p` was used in an arithmetic
expression. Two common reasons for this issue are:

1. Forgetting to pass parameters into the problem constructor. For example, `ODEProblem(f,u0,tspan)` should
be `ODEProblem(f,u0,tspan,p)` in order to use parameters.

2. Using the wrong function signature. For example, with `ODEProblem`s the function signature is always
`f(du,u,p,t)` for the in-place form or `f(u,p,t)` for the out-of-place form. Note that the `p` argument
will always be in the function signature regardless of if the problem is defined with parameters!

ERROR: MethodError: no method matching g(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{…}, Float64, 1}}, ::SciMLBase.NullParameters)

Closest candidates are:
  g(::Vector{Float64}, ::Any)
   @ Main REPL[65]:1

Stacktrace:
  [1] (::NonlinearFunction{…})(::Vector{…}, ::Vararg{…})
    @ SciMLBase ~/.julia/packages/SciMLBase/bp17m/src/scimlfunctions.jl:2356
  [2] (::SciMLBase.JacobianWrapper{false, NonlinearFunction{…}, SciMLBase.NullParameters})(u::Vector{ForwardDiff.Dual{…}})
    @ SciMLBase ~/.julia/packages/SciMLBase/bp17m/src/function_wrappers.jl:97
  [3] auto_jacvec(f::SciMLBase.JacobianWrapper{…}, x::Vector{…}, v::Vector{…})
    @ SparseDiffTools ~/.julia/packages/SparseDiffTools/qxnHN/src/differentiation/jaches_products.jl:32
  [4] (::NonlinearSolve.var"#95#106"{…})(v::Vector{…}, u::Vector{…}, p::SciMLBase.NullParameters)
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/internal/operators.jl:114
  [5] (::NonlinearSolve.JacobianOperator{…})(Jv::Vector{…}, v::Vector{…}, u::Vector{…}, p::SciMLBase.NullParameters)
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/internal/operators.jl:198
  [6] mul!(Jv::Vector{…}, J::NonlinearSolve.StatefulJacobianOperator{…}, v::Vector{…})
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/internal/operators.jl:234
  [7] __mul!(C::Vector{…}, A::NonlinearSolve.StatefulJacobianOperator{…}, B::Vector{…})
    @ MaybeInplace ~/.julia/packages/MaybeInplace/DQx9o/src/MaybeInplace.jl:304
  [8] macro expansion
    @ ~/.julia/packages/MaybeInplace/DQx9o/src/MaybeInplace.jl:171 [inlined]
  [9] __internal_solve!(cache::NonlinearSolve.GenericTrustRegionSchemeCache{…}, J::Matrix{…}, fu::Vector{…}, u::Vector{…}, δu::Vector{…}, descent_stats::@NamedTuple{…})
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/globalization/trust_region.jl:519
 [10] __step!(cache::NonlinearSolve.GeneralizedFirstOrderAlgorithmCache{…}; recompute_jacobian::Nothing, kwargs::@Kwargs{})
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generalized_first_order.jl:244
 [11] __step!
    @ ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generalized_first_order.jl:203 [inlined]
 [12] #step!#207
    @ ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generic.jl:52 [inlined]
 [13] step!
    @ ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generic.jl:48 [inlined]
 [14] solve!(cache::NonlinearSolve.AbstractNonlinearSolveCache)
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generic.jl:13 [inlined]
 [15] __solve(::NonlinearProblem{…}, ::GeneralizedFirstOrderAlgorithm{…}; kwargs::@Kwargs{})
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generic.jl:4
 [16] __solve
    @ NonlinearSolve ~/.julia/packages/NonlinearSolve/7Y0dI/src/core/generic.jl:1 [inlined]
 [17] #solve_call#34
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:609 [inlined]
 [18] solve_call
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:567 [inlined]
 [19] #solve_up#42
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:1050 [inlined]
 [20] solve_up
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:1044 [inlined]
 [21] #solve#41
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:1038 [inlined]
 [22] solve(prob::NonlinearProblem{…}, args::GeneralizedFirstOrderAlgorithm{…})
    @ DiffEqBase ~/.julia/packages/DiffEqBase/eLhx9/src/solve.jl:1028
 [23] top-level scope
    @ REPL[66]:1