Skip to content
New issue

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

By clicking “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? Sign in to your account

Array variable units #3009

Closed
aml5600 opened this issue Sep 3, 2024 · 2 comments
Closed

Array variable units #3009

aml5600 opened this issue Sep 3, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@aml5600
Copy link
Contributor

aml5600 commented Sep 3, 2024

Describe the bug 🐞

It appears that while units may be given to array variables, those units cannot then be "gotten". This is leading to further issues when trying to use the variables.

Expected behavior

For a symbolic array (versus an array of variables?) return the correct unit type.

Minimal Reproducible Example 👇

using ModelingToolkit, DynamicQuantities
@variables x(t)[1:3], [unit = u"m"]
ModelingToolkit.get_unit(x)

Error & Stacktrace ⚠️

julia> ModelingToolkit.get_unit(x)
ERROR: There was an error processing arrayop expression 1.0 m.
Dimension of output index i in (i,) could not be inferred
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] (::Symbolics.var"#99#102"{Tuple{}, Quantity{}, Dict{}, Dict{}})(i::SymbolicUtils.BasicSymbolic{Int64})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/arrays.jl:222
  [3] map
    @ ./tuple.jl:291 [inlined]
  [4] make_shape(output_idx::Tuple{…}, expr::Quantity{…}, ranges::Dict{…})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/arrays.jl:216
  [5] Symbolics.ArrayOp(T::Type, output_idx::Tuple{…}, expr::Quantity{…}, reduce::Function, term::SymbolicUtils.BasicSymbolic{…}, ranges::Dict{…}; metadata::Nothing)
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/arrays.jl:53
  [6] ArrayOp
    @ ~/.julia/packages/Symbolics/qoXk1/src/arrays.jl:52 [inlined]
  [7] Symbolics.ArrayOp(T::Type, output_idx::Tuple{…}, expr::Quantity{…}, reduce::Function, term::SymbolicUtils.BasicSymbolic{…})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/arrays.jl:52
  [8] _map(::Function, ::SymbolicUtils.BasicSymbolic{Vector{Real}})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/array-lib.jl:340
  [9] macro expansion
    @ ~/.julia/packages/Symbolics/qoXk1/src/array-lib.jl:326 [inlined]
 [10] var"Base.map_4228762831532280247"(nothing::Nothing, f::Function, x::SymbolicUtils.BasicSymbolic{Vector{Real}})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/wrapper-types.jl:143
 [11] map(f::Function, x::Symbolics.Arr{Num, 1})
    @ Symbolics ~/.julia/packages/Symbolics/qoXk1/src/wrapper-types.jl:152
 [12] get_unit(x::Symbolics.Arr{Num, 1})
    @ ModelingToolkit ~/workspace/ModelingToolkit.jl/src/systems/unit_check.jl:70
 [13] top-level scope
    @ REPL[4]:1
Some type information was truncated. Use `show(err)` to see complete types.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
(ModelingToolkit) pkg> st
Project ModelingToolkit v9.36.0
Status `~/workspace/ModelingToolkit.jl/Project.toml`
  [1520ce14] AbstractTrees v0.4.5
  [4fba245c] ArrayInterface v7.15.0
  [8e7c35d0] BlockArrays v1.1.0
  [861a8166] Combinatorics v1.0.2
  [34da2185] Compat v4.16.0
  [187b0558] ConstructionBase v1.5.7
  [864edb3b] DataStructures v0.18.20
  [2b5f629d] DiffEqBase v6.154.0
  [459566f4] DiffEqCallbacks v3.8.0
  [77a26b50] DiffEqNoiseProcess v5.23.0
  [b552c78f] DiffRules v1.15.1
  [31c24e10] Distributions v0.25.110
  [ffbed154] DocStringExtensions v0.9.3
  [5b8099bc] DomainSets v0.7.14
⌅ [06fc5a27] DynamicQuantities v0.13.2
  [e2ba6199] ExprTools v0.1.10
⌅ [6b7a57c9] Expronicon v0.8.5
  [64ca27bc] FindFirstFunctions v1.3.0
  [f6369f11] ForwardDiff v0.10.36
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [86223c79] Graphs v1.11.2
  [98e50ef6] JuliaFormatter v1.0.59
  [ccbc3e58] JumpProcesses v9.13.3
  [23fbe1c1] Latexify v0.16.5
  [d8e11817] MLStyle v0.4.17
  [77ba4419] NaNMath v1.0.2
  [8913a72c] NonlinearSolve v3.14.0
  [bac558e1] OrderedCollections v1.6.3
  [aea7be01] PrecompileTools v1.2.1
  [731186ca] RecursiveArrayTools v3.27.0
  [189a3867] Reexport v1.2.2
  [7e49a35a] RuntimeGeneratedFunctions v0.5.13
  [0bca4576] SciMLBase v2.50.1
  [53ae85a6] SciMLStructures v1.4.2
  [efcf1570] Setfield v1.1.1
  [727e6d20] SimpleNonlinearSolve v1.12.0
  [276daf66] SpecialFunctions v2.4.0
  [90137ffa] StaticArrays v1.9.7
  [2efcf032] SymbolicIndexingInterface v0.3.28
  [d1185830] SymbolicUtils v3.4.0
  [0c5d862f] Symbolics v6.3.0
  [5c2747f8] URIs v1.5.1
  [3a884ed6] UnPack v1.0.2
  [1986cc42] Unitful v1.21.0
  [8ba89e20] Distributed
  [b77e0a4c] InteractiveUtils
  [8f399da3] Libdl
  [37e2e46d] LinearAlgebra
  [9e88b42a] Serialization
  [2f01184e] SparseArrays v1.10.0
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
  • Output of versioninfo()
julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 8 default, 0 interactive, 4 GC (on 6 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 8

Additional context

Add any other context about the problem here.

@aml5600 aml5600 added the bug Something isn't working label Sep 3, 2024
@aml5600
Copy link
Contributor Author

aml5600 commented Sep 3, 2024

ModelingToolkit.get_unit(ModelingToolkit.unwrap(x)) returns the correct result, though. So if this is the desired fix, I can make a PR. Thanks!

@ChrisRackauckas
Copy link
Member

That is correct. I guess Num on Arr was just missing a dispatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants