SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 204 forks source link

Unhelpful error messages when non-existing named parameters are requested #2592

Open Datseris opened 5 months ago

Datseris commented 5 months ago

Trying to obtain a parameter that doesn't exist gives error: ERROR: ArgumentError: invalid index: nothing of type Nothing, see MWE below.

Would be nice to throw an error that the parameter does not exist at the moment we do

i = SymbolicIndexingInterface.getp(prob, index)

and explicitly include index in the error message.

MWE:

using ModelingToolkit

@variables t
D = Differential(t)
@mtkmodel Roessler begin
    @parameters begin
        a = 0.2
        b = 0.2
        c = 5.7
    end
    @variables begin
        x(t) = 1.0
        y(t) = 0.0
        z(t) = 0.0
        nlt(t) # nonlinear term
    end
    @equations begin
        D(x) ~ -y -z
        D(y) ~ x + a*y
        D(z) ~ b + nlt
        nlt ~ z*(x - c)
    end
end

@mtkbuild roessler_model = Roessler()
prob = ODEProblem(roessler_model)

ModelingToolkit.getp(prob, :τ)(parameters(prob))
ERROR: ArgumentError: invalid index: nothing of type Nothing
Stacktrace:
  [1] to_index(i::Nothing)
    @ Base .\indices.jl:300
  [2] to_index(A::Vector{Any}, i::Nothing)
    @ Base .\indices.jl:277
  [3] _to_indices1(A::Vector{Any}, inds::Tuple{Base.OneTo{Int64}}, I1::Nothing)
    @ Base .\indices.jl:359
  [4] to_indices
    @ .\indices.jl:354 [inlined]
  [5] to_indices
    @ .\indices.jl:345 [inlined]
  [6] getindex
    @ .\abstractarray.jl:1288 [inlined]
  [7] parameter_values(arr::Vector{Any}, i::Nothing)
    @ SymbolicIndexingInterface C:\Users\datse\.julia\packages\SymbolicIndexingInterface\Pz8Cf\src\parameter_indexing.jl:15
  [8] (::SymbolicIndexingInterface.var"#_getter#9"{Nothing})(::SymbolicIndexingInterface.NotTimeseries, prob::Vector{Any})
    @ SymbolicIndexingInterface C:\Users\datse\.julia\packages\SymbolicIndexingInterface\Pz8Cf\src\parameter_indexing.jl:117
  [9] (::SymbolicIndexingInterface.var"#getter#10"{SymbolicIndexingInterface.var"#_getter#9"{Nothing}})(::Vector{Any})
    @ SymbolicIndexingInterface C:\Users\datse\.julia\packages\SymbolicIndexingInterface\Pz8Cf\src\parameter_indexing.jl:139
 [10] top-level scope
    @ REPL[44]:1
Datseris commented 5 months ago

(note: such an issue is super easy to come across when having a typo in the request parameter. Yet, the error message does not elude to that easily)