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

default for array variable broken #3083

Open
baggepinnen opened this issue Sep 28, 2024 · 15 comments
Open

default for array variable broken #3083

baggepinnen opened this issue Sep 28, 2024 · 15 comments
Assignees
Labels
bug Something isn't working

Comments

@baggepinnen
Copy link
Contributor

The following definition used to be fine, but is now broken on latest master

ERROR: LoadError: KeyError: key :parameters not found
Stacktrace:
 [1] getindex
   @ ./dict.jl:498 [inlined]
 [2] parse_variable_def!(dict::Dict{…}, mod::Module, arg::Expr, varclass::Symbol, kwargs::OrderedCollections.OrderedSet{…}, where_types::Vector{…}; def::Nothing, indices::Nothing, type::Type, meta::Dict{…})
   @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/model_parsing.jl:329
 [3] parse_variable_def!

The issue is the default value for r, if I remove it things work.

@mtkmodel Fixed begin
    @parameters begin
        (r[1:2] = [0, 0]), [description = "Fixed absolute xy-position, resolved in planarWorld frame"]
        phi = 0, [description = "Fixed angle"]
    end

    @components begin
        frame_b = Frame()
    end

    @equations begin
        frame_b.x ~ r[1]
        frame_b.y ~ r[2]
        frame_b.phi ~ phi
    end
end

the error is

@baggepinnen baggepinnen added the bug Something isn't working label Sep 28, 2024
@ven-k
Copy link
Member

ven-k commented Sep 28, 2024

Actually, parsing has gotten better; () aren't needed anymore.

This works:

@mtkmodel Fixed begin
    @parameters begin
        r[1:2] = [0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"]
        phi = 0, [description = "Fixed angle"]
    end
   ...

@baggepinnen
Copy link
Contributor Author

baggepinnen commented Sep 28, 2024

oki that's good, but breaking what was previously working (and required!) is not good

@ChrisRackauckas
Copy link
Member

The () was actually incorrect... it's kind of surprising that parsing actually worked 😅 . It was definitely not intended and not documented.

@baggepinnen
Copy link
Contributor Author

It used to error without it but not with it

@baggepinnen
Copy link
Contributor Author

Also, yingbo closed the issue complaining about this problem by showing to use parenthesis instead
#2298 (comment)
I thus consider this breaking

@baggepinnen baggepinnen reopened this Sep 28, 2024
@ChrisRackauckas
Copy link
Member

Can you show where it's documented?

@baggepinnen
Copy link
Contributor Author

I can not, but are you saying that following the advice of a core developer should lead to your code breaking in a patch release? Especially when the issue complaining about this exact problem was closed with this very advice? We must stop breaking people's code like this if we want people to look at MTK as a reliable tool.

@ChrisRackauckas
Copy link
Member

The 2024-2025 push is to make MTK match the rest of the reliability in SciML by strictly defining its interfaces, documentation, and better erroring on the breaks. A core developer shared an undocumented, untested, and unpromoted script that was a hacky temporary workaround. This is the exact opposite of the kind of code you're asking for and is exactly the kind of thing that anyone who wants reliability will crack down. Yes, prior unchecked interfaces in this package had traditionally let things slip through. When we turn on checks of interfaces and make things match how they are documented to work, you will get errors if you break the interface. I suggest not relying on hacks around the documented interface, helping with the documentation clarity of the interfaces, and erroring on things to better define the boundary. Hopefully there aren't many left as we have chopped out most of them with the parsing changes and the move to a documented SymbolicIndexingInterface.

@isaacsas
Copy link
Member

@ChrisRackauckas it would be nice as part of this push to also have an API for people developing on top of MTK who may need access to some of the internals (i.e. to declare some of the currently internal symbolic handling code, system code, and macro parsing code public).

@isaacsas
Copy link
Member

(That is another source of things breaking in non-breaking MTK releases we see quite frequently in Catalyst, which is our fault for using such functions, but really the only option in many cases short of copying the code from MTK into Catalyst.)

@ChrisRackauckas
Copy link
Member

Most of the internal symbolic handling code was made public API when Symbolics.jl was section out. If there's anything missing from that it should get an issue. At this point I think almost all of it is documented Symbolics.jl or SymbolicUtils.jl actions?

For the system code, yeah there's still a few things we need to document and make API. Some of the query functions in particular are nice to use and likely should get tested, like some of the stuff around metadata.

Macro parsing code 😅 , I'm not sure about how public API that should really be... unless it's sectioned off to some macro parsing utils package or something. That's quite internal stuff.

@YingboMa
Copy link
Member

YingboMa commented Oct 3, 2024

Why shouldn't

        (r[1:2] = [0, 0]), [description = "Fixed absolute xy-position, resolved in planarWorld frame"]

be supported? Also, by Julia syntax,

begin
       r = [0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"]
       a
end

means

begin
       r = ([0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"])
       a
end

so

        (r[1:2] = [0, 0]), [description = "Fixed absolute xy-position, resolved in planarWorld frame"]

is always the correct syntax anyway.

Proof:

julia> ex = :(begin
       r = [0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"]
       a
       end)
quote
    #= REPL[14]:2 =#
    r = ([0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"])
    #= REPL[14]:3 =#
    a
end

@YingboMa
Copy link
Member

YingboMa commented Oct 3, 2024

The revert PR #3092 fixes this issue.

@ven-k
Copy link
Member

ven-k commented Oct 8, 2024

Note that, the

r[1:2] = [0, 0], [description = "Fixed absolute xy-position, resolved in planarWorld frame"]

syntax was picked for mtkmodel and internally parser handled was set to correctly assign default and metadata values (and we had unit tests for this). This was to avoid the () that would be necessary with vanilla-@variables.

That's why (r[1:2] = [0, 0]), [...] wasn't part of the test suite and when updated this error was not caught.
However, there was nothing (code or docs) that explicitly disallowed that method either, resulting in confusion.

Now, with: #3107

The updated parser works with both syntaxes and has dedicated tests for both
Also, I've added a dedicated section in docs describing all the different ways to define arrays in mtkmodel.

@isaacsas
Copy link
Member

isaacsas commented Oct 8, 2024

If this is going to be added to the DSL why not add to the standalone macros too? Seems likely to be confusing to people otherwise.

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

5 participants