Skip to content

@mtkmodel errors when module qualifier is used to create component #2289

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Closed
baggepinnen opened this issue Sep 28, 2023 · 2 comments
Closed
Assignees

Comments

@baggepinnen
Copy link
Contributor

The following is fine

using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks

@mtkmodel TankWithInput begin
    @components begin
        in = Constant(k=308.5)
    end
end

but with the namespace Blocks qualifying the access to Constant, the macro errors

using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks

@mtkmodel TankWithInput begin
    @components begin
        in = Blocks.Constant(k=308.5)
    end
end
ERROR: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:64
  Symbol(::Any...)
   @ Base strings/basic.jl:229

Stacktrace:
  [1] setindex!(A::Vector{Symbol}, x::Expr, i1::Int64)
    @ Base ./array.jl:969
  [2] _unsafe_copyto!(dest::Vector{Symbol}, doffs::Int64, src::Vector{Any}, soffs::Int64, n::Int64)
    @ Base ./array.jl:250
  [3] unsafe_copyto!
    @ ./array.jl:304 [inlined]
  [4] _copyto_impl!
    @ ./array.jl:327 [inlined]
  [5] copyto!
    @ ./array.jl:314 [inlined]
  [6] copyto!
    @ ./array.jl:339 [inlined]
  [7] copyto_axcheck!
    @ ./abstractarray.jl:1182 [inlined]
  [8] Vector{Symbol}(x::Vector{Any})
    @ Base ./array.jl:621
  [9] convert
    @ ./array.jl:613 [inlined]
 [10] push!(a::Vector{Vector{Symbol}}, item::Vector{Any})
    @ Base ./array.jl:1060
 [11] parse_components!(exprs::Vector{Any}, cs::Vector{Symbol}, dict::Dict{Symbol, Any}, body::Expr, kwargs::Vector{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/BsHty/src/systems/model_parsing.jl:284
 [12] parse_model!(exprs::Vector{Any}, comps::Vector{Symbol}, ext::Base.RefValue{Any}, eqs::Vector{Expr}, icon::Base.RefValue{Union{String, URIs.URI}}, vs::Vector{Any}, ps::Vector{Any}, sps::Vector{Any}, dict::Dict{Symbol, Any}, mod::Module, arg::Expr, kwargs::Vector{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/BsHty/src/systems/model_parsing.jl:238
 [13] _model_macro(mod::Module, name::Symbol, expr::Expr, isconnector::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/BsHty/src/systems/model_parsing.jl:49
 [14] var"@mtkmodel"(__source__::LineNumberNode, __module__::Module, name::Symbol, body::Any)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/BsHty/src/systems/model_parsing.jl:31
 [15] eval
    @ ./boot.jl:370 [inlined]
 [16] eval
    @ ./Base.jl:68 [inlined]
 [17] repleval(m::Module, code::Expr, #unused#::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.52.2/scripts/packages/VSCodeServer/src/repl.jl:229
 [18] (::VSCodeServer.var"#110#112"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.52.2/scripts/packages/VSCodeServer/src/repl.jl:192
 [19] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging ./logging.jl:514
 [20] with_logger
    @ ./logging.jl:626 [inlined]
 [21] (::VSCodeServer.var"#109#111"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.52.2/scripts/packages/VSCodeServer/src/repl.jl:193
 [22] #invokelatest#2
    @ ./essentials.jl:819 [inlined]
 [23] invokelatest(::Any)
    @ Base ./essentials.jl:816
 [24] macro expansion
    @ ~/.vscode/extensions/julialang.language-julia-1.52.2/scripts/packages/VSCodeServer/src/eval.jl:34 [inlined]
 [25] (::VSCodeServer.var"#62#63")()
    @ VSCodeServer ./task.jl:514
@baggepinnen
Copy link
Contributor Author

This is still a problem on the latest release of MTK, the following system works fine, but if I use Rotational.Torque instead of the alias RTorque (and similar for BSine) I get the error reproduced below

using Multibody
using ModelingToolkit
using JuliaSimCompiler
using OrdinaryDiffEq
using ModelingToolkitStandardLibrary.Blocks
import ModelingToolkitStandardLibrary.Mechanical.Rotational
@parameters t
D = Differential(t)

# Workarounds for @mtkmodel bugs and limitations
RTorque = Rotational.Torque
BSine = Blocks.Sine
W(args...; kwargs...) = Multibody.world

@mtkmodel ActuatedJoint begin
    @components begin
        world = W()
        torque = RTorque()
        joint = Revolute(useAxisFlange=true) # The axis flange provides an interface to the 1D torque input from ModelingToolkitStandardLibrary.Mechanical.Rotational
        torque_signal = BSine(frequency=1/5)
        body = BodyShape(; m = 1, r_cm = [0.4, 0, 0])
    end
    @equations begin
        connect(world.frame_b, joint.frame_a)
        connect(joint.frame_b, body.frame_a)
        connect(torque_signal.output, torque.tau)
        connect(torque.flange, joint.axis)
    end
end

@named model = ActuatedJoint()
ssys = structural_simplify(IRSystem(model))
prob = ODEProblem(ssys, [D(D(joint.phi)) => 0], (0, 10))
sol = solve(prob, Rodas4())
julia> @mtkmodel ActuatedJoint begin
           @components begin
               torque = Rotational.Torque()
               joint = Revolute(useAxisFlange=true) # The axis flange provides an interface to the 1D torque input from ModelingToolkitStandardLibrary.Mechanical.Rotational
               torque_signal = Blocks.Sine(frequency=1/5)
               body = BodyShape(; m = 1, r_cm = [0.4, 0, 0])
           end
           @equations begin
               connect(world.frame_b, joint.frame_a)
               connect(joint.frame_b, body.frame_a)
               connect(torque_signal.output, torque.tau)
               connect(torque.flange, joint.axis)
           end
       end
ERROR: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  Symbol(::Any...)
   @ Base strings/basic.jl:229

Stacktrace:
  [1] setindex!(A::Vector{Symbol}, x::Expr, i1::Int64)
    @ Base ./array.jl:1021
  [2] _unsafe_copyto!(dest::Vector{Symbol}, doffs::Int64, src::Vector{Any}, soffs::Int64, n::Int64)
    @ Base ./array.jl:299
  [3] unsafe_copyto!
    @ ./array.jl:353 [inlined]
  [4] _copyto_impl!(dest::Array, doffs::Integer, src::Array, soffs::Integer, n::Integer)
    @ Base ./array.jl:376 [inlined]
  [5] copyto!
    @ ./array.jl:363 [inlined]
  [6] copyto!
    @ ./array.jl:385 [inlined]
  [7] copyto_axcheck!(dest::Any, src::Any)
    @ Base ./abstractarray.jl:1174 [inlined]
  [8] Vector{Symbol}(x::Vector{Any})
    @ Base ./array.jl:673
  [9] convert
    @ Base ./array.jl:665 [inlined]
 [10] push!(a::Vector{Vector{Symbol}}, item::Vector{Any})
    @ Base ./array.jl:1118
 [11] parse_components!(exprs::Vector{Any}, cs::Vector{Symbol}, dict::Dict{Symbol, Any}, body::Expr, kwargs::Vector{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/66mEN/src/systems/model_parsing.jl:297
 [12] parse_model!(exprs::Vector{…}, comps::Vector{…}, ext::Base.RefValue{…}, eqs::Vector{…}, icon::Base.RefValue{…}, vs::Vector{…}, ps::Vector{…}, sps::Vector{…}, dict::Dict{…}, mod::Module, arg::Expr, kwargs::Vector{…})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/66mEN/src/systems/model_parsing.jl:250
 [13] _model_macro(mod::Module, name::Symbol, expr::Expr, isconnector::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/66mEN/src/systems/model_parsing.jl:49
 [14] var"@mtkmodel"(__source__::LineNumberNode, __module__::Module, name::Symbol, body::Any)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/66mEN/src/systems/model_parsing.jl:31
 [15] eval
    @ ./boot.jl:385 [inlined]
 [16] eval
    @ ./Base.jl:88 [inlined]
 [17] repleval(m::Module, code::Expr, ::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.61.2/scripts/packages/VSCodeServer/src/repl.jl:229
 [18] (::VSCodeServer.var"#110#112"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.61.2/scripts/packages/VSCodeServer/src/repl.jl:192
 [19] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging ./logging.jl:515
 [20] with_logger
    @ ./logging.jl:627 [inlined]
 [21] (::VSCodeServer.var"#109#111"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.61.2/scripts/packages/VSCodeServer/src/repl.jl:193
 [22] #invokelatest#2
    @ Base ./essentials.jl:887 [inlined]
 [23] invokelatest(::Any)
    @ Base ./essentials.jl:884
 [24] (::VSCodeServer.var"#62#63")()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.61.2/scripts/packages/VSCodeServer/src/eval.jl:34
Some type information was truncated. Use `show(err)` to see complete types.

@baggepinnen
Copy link
Contributor Author

Ah, I noticed that no release has been made since this fix was merged

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants