SciML / SciMLBase.jl

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

Segfault from bad @inbounds in interpolation when solve fails #458

Closed Keno closed 1 year ago

Keno commented 1 year ago

I'm seeing a segfault trying to index into a solution that failed during solve. Turning on boundschecks, we see the following:

[IDAS ERROR]  IDASolve
  At t = 0 and h = 1.49012e-18, the error test failed repeatedly or with |h| = hmin.

Error During Test at /home/keno/.julia/dev/CedarSim/test/inverter.jl:58
  Test threw exception
  Expression: isapprox_deftol(sol(0.0, idxs = sys.node_d), 0.0)
  BoundsError: attempt to access 1-element Vector{Float64} at index [2]
  Stacktrace:
   [1] getindex(A::Vector{Float64}, i1::Int64)
     @ Base ./essentials.jl:13 [inlined]
   [2] interpolation(tvals::Vector{Float64}, id::SciMLBase.HermiteInterpolation{Vector{Float64}, Vector{Vector{Float64}}, Vector{Vector{Float64}}}, idxs::Nothing, deriv::Type{Val{0}}, p::CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, continuity::Symbol)
     @ SciMLBase ~/.julia/packages/SciMLBase/KcGs1/src/interpolation.jl:104
   [3] (::SciMLBase.HermiteInterpolation{Vector{Float64}, Vector{Vector{Float64}}, Vector{Vector{Float64}}})(tvals::Vector{Float64}, idxs::Nothing, deriv::Type, p::CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, continuity::Symbol)
     @ SciMLBase ~/.julia/packages/SciMLBase/KcGs1/src/interpolation.jl:55
   [4] (::SciMLBase.DAESolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Nothing, Vector{Float64}, SciMLBase.DAEProblem{Vector{Float64}, Vector{Float64}, Tuple{Float64, Float64}, true, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, SciMLBase.DAEFunction{true, SciMLBase.FullSpecialize, Core.OpaqueClosure{Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, Float64}, Nothing}, Nothing, Nothing, DAECompiler.var"#116#117"{Tuple{typeof(DAECompiler.zero_first!), Core.OpaqueClosure{Tuple{AbstractMatrix{Float64}, Vector{Float64}, Vector{Float64}, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, Float64, Float64}, Nothing}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, DAECompiler.DAEReconstructedObserved, Nothing, DAECompiler.TransformedIRODESystem}, @Kwargs{callback::Nothing, tstops::Vector{Float64}, initializealg::CedarSim.CedarDCOp{SciMLNLSolve.NLSolveJL{LineSearches.Static, SciMLNLSolve.var"#2#4"}}}, Vector{Bool}}, Sundials.IDA{:Dense, Nothing, Nothing}, SciMLBase.HermiteInterpolation{Vector{Float64}, Vector{Vector{Float64}}, Vector{Vector{Float64}}}, DiffEqBase.Stats})(t::Float64, ::Type{Val{0}}, idxs::DAECompiler.ScopeRef{DAECompiler.IRODESystem}, continuity::Symbol)
     @ SciMLBase ~/.julia/packages/SciMLBase/KcGs1/src/solutions/ode_solutions.jl:133
   [5] (::SciMLBase.DAESolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Nothing, Vector{Float64}, SciMLBase.DAEProblem{Vector{Float64}, Vector{Float64}, Tuple{Float64, Float64}, true, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, SciMLBase.DAEFunction{true, SciMLBase.FullSpecialize, Core.OpaqueClosure{Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, Float64}, Nothing}, Nothing, Nothing, DAECompiler.var"#116#117"{Tuple{typeof(DAECompiler.zero_first!), Core.OpaqueClosure{Tuple{AbstractMatrix{Float64}, Vector{Float64}, Vector{Float64}, CedarSim.DefaultSim{Main.inverter_tests.var"#25#26"}, Float64, Float64}, Nothing}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, DAECompiler.DAEReconstructedObserved, Nothing, DAECompiler.TransformedIRODESystem}, @Kwargs{callback::Nothing, tstops::Vector{Float64}, initializealg::CedarSim.CedarDCOp{SciMLNLSolve.NLSolveJL{LineSearches.Static, SciMLNLSolve.var"#2#4"}}}, Vector{Bool}}, Sundials.IDA{:Dense, Nothing, Nothing}, SciMLBase.HermiteInterpolation{Vector{Float64}, Vector{Vector{Float64}}, Vector{Vector{Float64}}}, DiffEqBase.Stats})(t::Float64, ::Type{Val{0}}; idxs::DAECompiler.ScopeRef{DAECompiler.IRODESystem}, continuity::Symbol)
     @ SciMLBase ~/.julia/packages/SciMLBase/KcGs1/src/solutions/ode_solutions.jl:66 [inlined]
ChrisRackauckas commented 1 year ago

@oscardssmith were you going to look at this one? I think removing some @inbounds now that it's not needed in most scenarios could be helpful stuff anyways.

oscardssmith commented 1 year ago

will do.

oscardssmith commented 1 year ago

fixed by https://github.com/SciML/SciMLBase.jl/pull/476

oscardssmith commented 1 year ago

Also the mwe is pretty simple:

f(out, du, u, p, t) = out .= [t]
prob = DAEProblem(f, [1.0], [1.0], (0, 2); differential_vars = [false])
sol = solve(prob, IDA())
sol(1.0)