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

Obscure error message when trying to connect components with ~ instead of connect

Open BenChung opened this issue 2 years ago • 1 comments

I was building the basic spring-damper system as

using ModelingToolkit, ModelingToolkitStandardLibrary, DifferentialEquations
using ModelingToolkitStandardLibrary.Mechanical.Translational

@mtkmodel SMDModel begin
    @components begin
        mass = Mass(m = 1.0)
        spring = Spring(k = 1.0)
        damper = Damper(d = 1.0)
        fixed = Fixed()
    end
    @equations begin
        mass.flange ~ spring.flange_a 
        mass.flange ~ damper.flange_a
        spring.flange_b ~ fixed.flange
        damper.flange_b ~ fixed.flange
    end
end

@mtkbuild hw1_model = SMDModel(mass.m = 1.0, spring.k = 3.0, damper.d = 0.25)

which accidentally uses ~ instead of connect to link the flanges. This fails with the completely inscrutable error message

ERROR: AssertionError: !(isempty(regular_ss))
Stacktrace:
  [1] connection2set!(connectionsets::Vector{ModelingToolkit.ConnectionSet}, namespace::Nothing, ss::Vector{ODESystem}, isouter::ModelingToolkit.var"#isouter#234"{Vector{Symbol}})
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:214
  [2] generate_connection_set!(connectionsets::Vector{ModelingToolkit.ConnectionSet}, domain_csets::Vector{ModelingToolkit.ConnectionSet}, sys::ODESystem, find::Nothing, replace::Nothing, namespace::Nothing)
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:327
  [3] generate_connection_set!
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:284 [inlined]
  [4] generate_connection_set
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:275 [inlined]
  [5] expand_connections(sys::ODESystem, find::Nothing, replace::Nothing; debug::Bool, tol::Float64)
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:453
  [6] expand_connections (repeats 2 times)
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\connectors.jl:451 [inlined]
  [7] __structural_simplify(sys::ODESystem, io::Nothing; simplify::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\systems.jl:38
  [8] __structural_simplify
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\systems.jl:36 [inlined]
  [9] structural_simplify(sys::ODESystem, io::Nothing; simplify::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\systems.jl:21
 [10] structural_simplify
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\systems.jl:19 [inlined]
 [11] structural_simplify(sys::ODESystem)
    @ ModelingToolkit C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\systems.jl:19
 [12] top-level scope
    @ C:\Users\benchung\.julia\packages\ModelingToolkit\oIgbi\src\systems\abstractsystem.jl:1192

which tells the user absolutely nothing about what went wrong.

The error message should tell the user:

  • What went wrong (you can't link ports with ~, with a line number)
  • A suggestion about how to fix it (use connect instead of ~)

BenChung avatar Oct 11 '23 07:10 BenChung

This could probably be caught already in the ~ function if it detects that either of the sides of the equations are of unexpected types

baggepinnen avatar Oct 11 '23 08:10 baggepinnen