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

Non-numeric parameters are broken inside @mtkmodel #3231

Closed
cstjean opened this issue Nov 22, 2024 · 1 comment · Fixed by #3236
Closed

Non-numeric parameters are broken inside @mtkmodel #3231

cstjean opened this issue Nov 22, 2024 · 1 comment · Fixed by #3236
Assignees
Labels
bug Something isn't working

Comments

@cstjean
Copy link
Contributor

cstjean commented Nov 22, 2024

Describe the bug 🐞

@parameters p3::MyCustomStructType works OK standalone, but fails with iteration error inside of @mtkmodel

Expected behavior

@parameters should work inside of @mtkmodel.

Minimal Reproducible Example 👇

The docs claim that the line @parameters p3::MyCustomStructType # non-numeric should work. It seems to work stand-alone.

julia> struct MyCustomStructType end

julia> @parameters p3::MyCustomStructType
1-element Vector{SymbolicUtils.BasicSymbolic{MyCustomStructType}}:
 p3

but fails inside of @mtkmodel

julia> @mtkmodel MyModel begin
           @parameters begin
               p3::MyCustomStructType
           end
       end
ModelingToolkit.Model{typeof(__MyModel__), Dict{Symbol, Any}}(__MyModel__, Dict{Symbol, Any}(:kwargs => Dict{Symbol, Dict}(:p3 => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => MyCustomStructType)), :independent_variable => t, :parameters => Dict{Symbol, Dict{Symbol, Any}}(:p3 => Dict(:type => MyCustomStructType))), false)

julia> @named sys = MyModel(p3=MyCustomStructType())
ERROR: MethodError: no method matching iterate(::SymbolicUtils.BasicSymbolic{MyCustomStructType})

Closest candidates are:
  iterate(::LaTeXStrings.LaTeXString, ::Int64)
   @ LaTeXStrings C:\Users\\.julia\packages\LaTeXStrings\6NrIG\src\LaTeXStrings.jl:108
  iterate(::LaTeXStrings.LaTeXString)
   @ LaTeXStrings C:\Users\\.julia\packages\LaTeXStrings\6NrIG\src\LaTeXStrings.jl:109
  iterate(::DataStructures.TrieIterator)
   @ DataStructures C:\Users\\.julia\packages\DataStructures\95DJa\src\trie.jl:112
  ...

Stacktrace:
 [1] __MyModel__(; name::Symbol, p3::MyCustomStructType)
   @ Main C:\Users\\.julia\packages\ModelingToolkit\2hOLJ\src\systems\model_parsing.jl:914
 [2] (::ModelingToolkit.Model{typeof(__MyModel__), Dict{Symbol, Any}})(; kw::@Kwargs{name::Symbol, p3::MyCustomStructType})
   @ ModelingToolkit C:\Users\\.julia\packages\ModelingToolkit\2hOLJ\src\systems\model_parsing.jl:25
 [3] top-level scope
   @ REPL[29]:1

Environment (please complete the following information):

Julia 1.10.5
MTK 9.52.0

Might be related to the work necessary for #3182

@cstjean cstjean added the bug Something isn't working label Nov 22, 2024
@cstjean
Copy link
Contributor Author

cstjean commented Nov 22, 2024

As a work-around, I can make my MyCustomStructType iterable, and it seems to work.

julia> Base.iterate(s::SymbolicUtils.BasicSymbolic{MyCustomStructType}, iter::Bool) = nothing

julia> Base.iterate(s::SymbolicUtils.BasicSymbolic{MyCustomStructType}, iter::Nothing=nothing) = (s, false)

so it looks like the bug is "non-numeric objects should not be iterated"...?

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

Successfully merging a pull request may close this issue.

2 participants