ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Specifying arbitrary metadata

Open cstjean opened this issue 5 months ago • 3 comments

Is your feature request related to a problem? Please describe.

@mtkmodel is great, and I would like to use it to encode several user-specific metadata properties (for instance, I'd like to specify the DCS tag name corresponding to some variable).

I don't particularly care about this metadata being efficiently stored / accessed.

Describe the solution you’d like

I'd like to have syntax for arbitrary metadata. It is already possible to do it with misc

@parameters tau [misc=(; link="myurl", specifity=30)]

but that's not elegant, and hard to read for non-programmers. Ideally, I'd like:

@parameters tau [link="myurl", specifity=30]

that expands to @parameters tau [misc=(; link="myurl", specifity=30)], with some accessor like getmisc(tau, :link). In other words, all unknown parameters become misc named tuple fields.

Describe alternatives you’ve considered

@parameters tau [misc_link="myurl", misc_specifity=30]

would be uglier, but it would perhaps be more palatable for MTK.

Additional context

This is a straight-forward change, and I'd be happy to provide a PR if this is considered desirable. I get that this is breaking-ish.

cstjean avatar Jul 15 '25 02:07 cstjean

This is functionality that Symbolics provides. You are free to define your own metadata keys by

  1. defining a type associated with the metadata key. So abstract type MiscLink end and abstract type MiscSpecificity end
  2. Symbolics.option_to_metadata_type(::Val{:misc_link}) = MiscLink and similarly for :misc_specificity

Then the syntax you want will work. The metadata will be available using SymbolicUtils.getmetadata(tau, MiscLink, nothing)

AayushSabharwal avatar Jul 15 '25 07:07 AayushSabharwal

Awesome, exactly what we need! If it's in the documentation then we should close this issue, but I couldn't find it here or in Symbolics.jl.

cstjean avatar Jul 15 '25 08:07 cstjean

Doesn't look like it is surfaced anywhere. I'll add the relevant docs.

AayushSabharwal avatar Jul 15 '25 15:07 AayushSabharwal