ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

SDESystem with empty noise equations crashes in `check_units`

Open isaacsas opened this issue 3 years ago • 3 comments

I assume for composition we'd want to allow SDESystems with no noise? In that case this errors out:

@parameters σ ρ β
@variables t x(t) y(t) z(t)
D = Differential(t)
eqs = [D(x) ~ σ*(y-x),
       D(y) ~ x*(ρ-z)-y,
       D(z) ~ x*y - β*z]
@named de = SDESystem(eqs,[],t,[x,y,z],[σ,ρ,β])

with error

ERROR: BoundsError: attempt to access 0-element Vector{Any} at index [1]
Stacktrace:
  [1] getindex
    @ ./array.jl:801 [inlined]
  [2] (::ModelingToolkit.var"#478#479"{String, Vector{Equation}, Vector{Any}})(idx::Int64)
    @ ModelingToolkit ./none:0
  [3] iterate
    @ ./generator.jl:47 [inlined]
  [4] collect(itr::Base.Generator{UnitRange{Int64}, ModelingToolkit.var"#478#479"{String, Vector{Equation}, Vector{Any}}})
    @ Base ./array.jl:678
  [5] #validate#477
    @ ~/.julia/packages/ModelingToolkit/9DDjU/src/systems/validation.jl:132 [inlined]
  [6] validate(eqs::Vector{Equation}, noise::Vector{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/9DDjU/src/systems/validation.jl:132
  [7] check_units(::Vector{Equation}, ::Vararg{Any, N} where N)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/9DDjU/src/systems/validation.jl:138
  [8] SDESystem
    @ ~/.julia/packages/ModelingToolkit/9DDjU/src/systems/diffeqs/sdesystem.jl:94 [inlined]
  [9] SDESystem(deqs::Vector{Equation}, neqs::Vector{Any}, iv::Num, dvs::Vector{Num}, ps::Vector{Num}; controls::Vector{Num}, observed::Vector{Num}, systems::Vector{SDESystem}, default_u0::Dict{Any, Any}, default_p::Dict{Any, Any}, defaults::Dict{Any, Any}, name::Symbol, connection_type::Nothing, checks::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/9DDjU/src/systems/diffeqs/sdesystem.jl:137
 [10] top-level scope
    @ REPL[69]:1

I'm not sure if the desired fix is to allow SDESystems without noise equations, or to allow ODE/Nonlinear/SDE systems to have sub-systems of different system types (which currently is not supported).

isaacsas avatar Sep 15 '21 16:09 isaacsas

I'm not sure why check_units is being called -- the guard conditional here all_dimensionless([dvs;ps;iv]) should be true. What version are you on?

It would be a pretty simple fix if we wanted to support this directly. We could just check the size of the noise eqs and don't pass them into check_units inside the SDE inner constructor if size is zero.

I think it would make more sense for this to be an ODESystem, but if there's already an expectation to allow SDESystems like this then we'll support it.

lamorton avatar Sep 15 '21 19:09 lamorton

You are right, this is a classic case of my typing ]up ModelingToolkit earlier today and then not catching that it left me at 6.4.8... So it does indeed work in the unitless case. Sorry about that!

However, I do think it should be made to work with units too. Right now sub-systems are required to all have the same type. So any SDAEs or coupled ODE/SDE systems would have to be represented as SDESystems I think.

isaacsas avatar Sep 15 '21 21:09 isaacsas

Can we close this?

lamorton avatar Aug 27 '22 22:08 lamorton