Skip to content

complete swaps order of metadata in ReactionSystems #1353

@TorkelE

Description

@TorkelE

For some reason, in some cases where you do complete on a ReactionSystem, that can swap around the order on metadata. Since metadata is not overwritten, but just added to the metadata dict, that means that what is retrieved from getmetadata can change.

In principle, this just involves MTK functions, so everything should transfer to ReactionSystem. However, since things work fine for System, something is obviously going on in Catalyst.

using Catalyst, ModelingToolkitBase
t = default_t()
D = default_time_deriv()
# Create model.
@species X(t)
@parameters d
@named rs2 = ReactionSystem([Reaction(d, [X], nothing)], t; metadata = [MiscSystemData => π])
rs2 = ModelingToolkitBase.setmetadata(rs2, MiscSystemData, ones(2, 3))
ModelingToolkitBase.getmetadata(complete(rs2), MiscSystemData, nothing) # π = 3.1415926535897... (incorrect)

@species X(t)
@parameters d
@named sys2 = System([Differential(t)(X) ~ -d*X], t; metadata = [MiscSystemData => π])
sys2 = ModelingToolkitBase.setmetadata(sys2, MiscSystemData, ones(2, 3))
ModelingToolkitBase.getmetadata(complete(sys2), MiscSystemData, nothing) # 2×3 Matrix{Float64}: (correct)

And the actual metadata dicts are:

julia> ModelingToolkitBase.get_metadata((rs2))
Base.ImmutableDict{DataType, Any} with 3 entries:
  MiscSystemData  => [1.0 1.0 1.0; 1.0 1.0 1.0]
  MutableCacheKey => Dict{DataType, Any}()
  MiscSystemData  => π

julia> ModelingToolkitBase.get_metadata(complete(rs2))
Base.ImmutableDict{DataType, Any} with 3 entries:
  MiscSystemData  => π
  MutableCacheKey => Dict{DataType, Any}()
  MiscSystemData  => [1.0 1.0 1.0; 1.0 1.0 1.0]

julia> ModelingToolkitBase.get_metadata((sys2))
Base.ImmutableDict{DataType, Any} with 3 entries:
  MiscSystemData  => [1.0 1.0 1.0; 1.0 1.0 1.0]
  MutableCacheKey => Dict{DataType, Any}()
  MiscSystemData  => π

julia> ModelingToolkitBase.get_metadata(complete(sys2))
Base.ImmutableDict{DataType, Any} with 3 entries:
  MiscSystemData  => [1.0 1.0 1.0; 1.0 1.0 1.0]
  MutableCacheKey => Dict{DataType, Any}()
  MiscSystemData  => π

This is only relevant on the current branch for Catalyst v16 (but will likely remain after that merges)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions