Skip to content

function parse_variable_arg: LoadError MethodError, No or invalid units providedΒ #3247

@wang890

Description

@wang890

Describe the bug 🐞

I did a lot of testing and trying under the ENV of Julia v1.10 and the latest v1.11.1, vscode 1.94.2
and the latest 1.95.3, vscode julia extension v1.124.2 and the latest 1.127.2:

Only the following package combination (old version) can run my code, and DynamicQuantities v0.13.2 can work. If upgrade any of these packages, an error will occur. I guess this may not be a bug of vscode.

βŒƒ [d360d2e6] ChainRulesCore v1.24.0
βŒƒ [2b5f629d] DiffEqBase v6.151.4
βŒ… [06fc5a27] DynamicQuantities v0.13.2
βŒƒ [961ee093] ModelingToolkit v9.19.0
βŒƒ [1dea7af3] OrdinaryDiffEq v6.84.0
βŒƒ [91a5bcdd] Plots v1.40.4

when I used the following package combination (compat version) of ModelingToolkit project.toml [compat], the same error occurred

βŒƒ [d360d2e6] ChainRulesCore v1
βŒƒ [2b5f629d] DiffEqBase v6.157
βŒ…[06fc5a27] DynamicQuantities v0.13.2
    [961ee093] ModelingToolkit v9.54
βŒƒ  [1dea7af3] OrdinaryDiffEq v6.82.0

when I used all the latest version as follows, the same error occurred

    [d360d2e6] ChainRulesCore v1.25.0
    [2b5f629d] DiffEqBase v6.160.0
βŒ…[06fc5a27] DynamicQuantities v0.13.2  
    [961ee093] ModelingToolkit v9.54.0
    [1dea7af3] OrdinaryDiffEq v6.90.1
    [91a5bcdd] Plots v1.40.9

The error is following when Run Without Debugging. the paragraph of MethodError(DynamicQuantities.uconvert ...... before "ERROR: LoadError..." was the output when I added a line of println(e) at function parse_variable_arg in model_parsing.jl:

MethodError(DynamicQuantities.uconvert, (1.0 m² s⁻³ A⁻² kg, 1.0), 0x00000000000069c5)
ERROR: LoadError: MethodError: no method matching setmetadata(::Nothing, ::Type{ModelingToolkit.VariableUnit}, ::DynamicQuantities.Quantity{Float64, DynamicQuantities.Dimensions{DynamicQuantities.FixedRational{Int32, 25200}}})
The function `setmetadata` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  setmetadata(::Num, ::Any, ::Any)
   @ Symbolics C:\Users\TCY\.julia\packages\Symbolics\YbNrd\src\Symbolics.jl:187
  setmetadata(::Complex{Num}, ::Any, ::Any)
   @ Symbolics C:\Users\TCY\.julia\packages\Symbolics\YbNrd\src\Symbolics.jl:187
  setmetadata(::SymbolicUtils.Symbolic, ::DataType, ::Any)
   @ SymbolicUtils C:\Users\TCY\.julia\packages\SymbolicUtils\jf8aQ\src\types.jl:685

ERROR: LoadError: No or invalid units provided for 'R'
Stacktrace:
  [1] set_scalar_metadata(x::Nothing, V::Type, val::DynamicQuantities.Quantity{Float64, DynamicQuantities.Dimensions{DynamicQuantities.FixedRational{Int32, 25200}}})
    @ Symbolics C:\Users\TCY\.julia\packages\Symbolics\YbNrd\src\variable.jl:43
  [2] #__Resistor__#11
    @ C:\Users\TCY\.julia\packages\UnPack\EkESO\src\UnPack.jl:905 [inlined]
  [3] __Resistor__(; name::Symbol, useHeatPort::Bool, conditionalHeatPort__useHeatPort::Nothing, R::Float64, T_ref::ModelingToolkit.NoValue, alpha::ModelingToolkit.NoValue, R_actual::ModelingToolkit.NoValue)
    @ Systemize.Electrical .\none:0
  [4] __Resistor__
    @ .\none:0 [inlined]
  [5] #_#441
    @ C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\model_parsing.jl:25 [inlined]
  [6] macro expansion
    @ C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\abstractsystem.jl:2113 [inlined]
  [7] __Rc__(; name::Symbol, useHeatPort::Bool, resistor__R::Nothing, resistor__useHeatPort::Nothing, capacitor__C::Nothing, source__V::Nothing, fixedTemperature__T::Nothing)
    @ Systemize.Electrical C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\model_parsing.jl:138
  [8] __Rc__
    @ C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\model_parsing.jl:138 [inlined]
  [9] #_#441
    @ C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\model_parsing.jl:25 [inlined]
 [10] top-level scope
    @ C:\Users\TCY\.julia\packages\ModelingToolkit\0O7FS\src\systems\abstractsystem.jl:2113
 [11] include(fname::String)
    @ Main .\sysimg.jl:38
 [12] run(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, error_handler::VSCodeDebugger.var"#3#4"{String})
    @ VSCodeDebugger.DebugAdapter c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\packagedef.jl:122
 [13] startdebugger()
    @ VSCodeDebugger c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeDebugger\src\VSCodeDebugger.jl:45
 [14] top-level scope
    @ c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\debugger\run_debugger.jl:12
 [15] include(mod::Module, _path::String)
    @ Base .\Base.jl:557
 [16] exec_options(opts::Base.JLOptions)
    @ Base .\client.jl:323
 [17] _start()
    @ Base .\client.jl:531

error("No or invalid units provided for \'" * string(:($$vv)) is at the following function in model_parsing.jl

function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
    vv, def, metadata_with_exprs = parse_variable_def!(
        dict, mod, arg, varclass, kwargs, where_types)
    if !(vv isa Tuple)
        name = getname(vv)
        varexpr = if haskey(metadata_with_exprs, VariableUnit)
            unit = metadata_with_exprs[VariableUnit]
            quote
                $name = if $name === $NO_VALUE
                    $setdefault($vv, $def)
                else
                    try
                        $setdefault($vv, $convert_units($unit, $name))
                    catch e
                        if isa(e, $(DynamicQuantities.DimensionError)) ||
                           isa(e, $(Unitful.DimensionError))
                            error("Unable to convert units for \'" * string(:($$vv)) * "\'")
                        elseif isa(e, MethodError)
                            error("No or invalid units provided for \'" * string(:($$vv)) *
                                  "\'")
                            println(e)
                        else
                            rethrow(e)
                        end
                    end
                end
            end
        else

The Resistor code in my code is as following. My temporary Unit solution like unit = u("Resistance") had been described at the issue . After I learned Julia macro and mtk, I would also like to contribute some code.

@mtkmodel Resistor begin
    @structural_parameters begin
        useHeatPort=false 
      end
    @extend i,v,p,n = onePort = OnePort()

    @components begin        
        conditionalHeatPort = ConditionalHeatPort(; useHeatPort=useHeatPort)    
    end
        
    @parameters begin
        R,                      [unit = u("Resistance"), guess = 1, description = "Resistance at temperature T_ref"]

        T_ref = 300.15, [unit = u("ThermodynamicTemperature"), bounds = (0.0, Inf), guess = 288.15, 
                                  description = "Reference temperature"] # 27 celsius degree
                          
        alpha = 0,         [unit = u("LinearTemperatureCoefficient"), 
                                  description = "Temperature coefficient of resistance R_actual = R*(1 + alpha*(T_heatPort - T_ref))"] 
    end
    @variables begin
        R_actual(t),       [unit = u("Resistance"),
                                  description = "Actual resistance = R*(1 + alpha*(T_heatPort - T_ref))"]
    end
    @equations begin     
        R_actual ~ R*(1 + alpha*(conditionalHeatPort.T_heatPort - T_ref))
        v ~ R_actual * i
        conditionalHeatPort.LossPower ~ v * i        
    end
end

The error becomes as following when Start Debugging.

β”Œ Error: Some Julia code in the VS Code extension crashed
β”” @ VSCodeDebugger c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\error_handler.jl:15
ERROR: UndefVarError: `uuid4` not defined in `VSCodeDebugger.DebugAdapter`
Suggestion: check for spelling errors or missing imports.
Hint: a global variable of this name may be made accessible by importing UUIDs in the current active module Main
Stacktrace:
 [1] stack_trace_request(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, params::VSCodeDebugger.DebugAdapter.StackTraceArguments)
   @ VSCodeDebugger.DebugAdapter c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\debugger_requests.jl:400
 [2] (::VSCodeDebugger.DebugAdapter.var"#110#139"{VSCodeDebugger.DebugAdapter.DebugSession})(params::VSCodeDebugger.DebugAdapter.StackTraceArguments)
   @ VSCodeDebugger.DebugAdapter c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\packagedef.jl:73
 [3] dispatch_msg(x::VSCodeDebugger.DebugAdapter.DAPRPC.DAPEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeDebugger.DebugAdapter.DAPRPC.MsgDispatcher, msg::Dict{String, Any})
   @ VSCodeDebugger.DebugAdapter.DAPRPC c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\DAPRPC\typed.jl:89
 [4] (::VSCodeDebugger.DebugAdapter.var"#132#161"{Dict{String, Any}, VSCodeDebugger.var"#3#4"{String}, VSCodeDebugger.DebugAdapter.DAPRPC.MsgDispatcher, VSCodeDebugger.DebugAdapter.DAPRPC.DAPEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}})()
   @ VSCodeDebugger.DebugAdapter c:\Users\TCY\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\packagedef.jl:98

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions