Skip to content

substitute deletes events and metadata #3731

Open
@ctessum

Description

@ctessum

Describe the bug 🐞

Here is some code that creates a system with events and metadata, and then performs a substitution:

using ModelingToolkit
t = ModelingToolkit.t_nounits
D = ModelingToolkit.D_nounits
using Test

@parameters p(t)=1.0
@variables x(t)=0.0
event = [0.5] => [p ~ Pre(t)]
event2 = [x ~ 0.75] => [p ~ 2*Pre(t)]

struct TestMeta end

eq = [
    D(x) ~ p,
]
@named sys = ODESystem(eq, t, [x], [p], discrete_events=[event],
    continuous_events=[event2], metadata = Dict(TestMeta => "test"))

@variables x2(t)=0.0
sys2 = substitute(sys, [x => x2])

@test length(ModelingToolkit.get_discrete_events(sys)) == 1 # Passes
@test length(ModelingToolkit.get_discrete_events(sys2)) == 1 # Fails
@test length(ModelingToolkit.get_continuous_events(sys)) == 1 # Passes
@test length(ModelingToolkit.get_continuous_events(sys2)) == 1 # Fails
@test getmetadata(sys, TestMeta, nothing) == "test" # Passes
@test getmetadata(sys2, TestMeta, nothing) == "test" # Fails

When this code runs, it prints this warning:

┌ Warning: `substitute` only supports performing substitutions in equations. This system has events, which will not be updated.

Which is fine I guess, but the issue is that in fact the events are deleted instead of just being left alone. If the events were just copied over as-is, then the warning message would be correct and it would at least work for cases where the substituted variables aren't related to the events.

And also substitute deletes the metadata, as shown above

Environment (please complete the following information):

mtk v10.2.0

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions