trulsf / UnitJuMP.jl

Julia package allowing unit modelling in JuMP using Unitful
MIT License
20 stars 1 forks source link

Improve error if variable defined without units #5

Closed odow closed 2 years ago

odow commented 2 years ago
julia> using UnitJuMP

julia> model = Model();

julia> @variable(model, x >= 0, u"m/s")
x m sā»Ā¹

julia> @variable(model, y)  # No units! What happens?
y

julia> @constraint(model, x >= y)
ERROR: MethodError: no method matching -(::UnitAffExpr{Unitful.FreeUnits{(m, sā»Ā¹), š‹ š“ā»Ā¹, nothing}}, ::VariableRef)

Closest candidates are:
  -(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at /Users/oscar/.julia/packages/MathOptInterface/ElZ45/src/Utilities/functions.jl:1779
  -(::GenericQuadExpr, ::AbstractVariableRef) at /Users/oscar/.julia/packages/JuMP/0C6kd/src/operators.jl:288
  -(::GenericAffExpr{C, V}, ::V) where {C, V<:AbstractVariableRef} at /Users/oscar/.julia/packages/JuMP/0C6kd/src/operators.jl:190
  ...
Stacktrace:
 [1] sub_mul(a::UnitAffExpr{Unitful.FreeUnits{(m, sā»Ā¹), š‹ š“ā»Ā¹, nothing}}, b::VariableRef)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/BOohi/src/MutableArithmetics.jl:38
 [2] operate(::typeof(MutableArithmetics.sub_mul), ::UnitAffExpr{Unitful.FreeUnits{(m, sā»Ā¹), š‹ š“ā»Ā¹, nothing}}, ::VariableRef)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/BOohi/src/interface.jl:180
 [3] operate_fallback!!(::MutableArithmetics.IsNotMutable, ::typeof(MutableArithmetics.sub_mul), ::UnitAffExpr{Unitful.FreeUnits{(m, sā»Ā¹), š‹ š“ā»Ā¹, nothing}}, ::VariableRef)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/BOohi/src/interface.jl:549
 [4] operate!!(op::typeof(MutableArithmetics.sub_mul), x::UnitAffExpr{Unitful.FreeUnits{(m, sā»Ā¹), š‹ š“ā»Ā¹, nothing}}, args::VariableRef)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/BOohi/src/rewrite.jl:88
 [5] macro expansion
   @ ~/.julia/packages/MutableArithmetics/BOohi/src/rewrite.jl:288 [inlined]
 [6] macro expansion
   @ ~/.julia/packages/JuMP/0C6kd/src/macros.jl:815 [inlined]
 [7] top-level scope
   @ REPL[64]:1