Skip to content

simplification of affect systems with observed equations can yield incorrect updates to state variables #3976

@vyudu

Description

@vyudu

Describe the bug 🐞

Simplifying observed equations in affect systems can lead to unintended explicit updates to state variables when a callback is triggered. All the algebraic equations of the parent system and the observed equations of the parent system are added to the affect system. When the affect system is simplified, some of these observed equations get solved for state variables. Thus the simplified affect system may be fully explicit (i.e. have no actual equations, only observed equations), but this means there will be an explicit update to the state variable that was solved for. Instead of the observed value being updated in response to changes in the state variables, the state variable gets updated according to the previous value of the observed variable (since there are no algebraic equations to be respected).

Expected behavior

Observed equations of the parent system should not be solved for state variables in the affect system.

Minimal Reproducible Example 👇

using ModelingToolkit
t = ModelingToolkit.t_nounits; D = ModelingToolkit.D_nounits
@variables V(t) = -70.0 G(t) = 0.0 jcn(t) [input = true] z(t)
@parameters Eₘ=-70. θ=-50.

ev = (V  θ) => [V ~ Eₘ]

@named lif1 = System([D(V) ~ jcn], t, [V, jcn], [Eₘ, θ]; discrete_events = [ev])
@named lif2 = System([D(V) ~ jcn], t, [V, jcn], [Eₘ, θ]; discrete_events = [ev])
@named qif = System([D(G) ~ G + z, D(z) ~ z, D(V) ~ jcn], t, [V, G, z, jcn], [Eₘ, θ]; discrete_events = [ev])

sys2 = compose(System([lif1.jcn ~ lif2.V + qif.G, lif2.jcn ~ lif1.V + qif.G, qif.jcn ~ lif1.V + lif2.V], t; name = :outer), [lif1, lif2, qif])
sys2 = mtkcompile(sys2)

Then inspecting the update equations for the first discrete event using ModelingToolkit.observed(discrete_events(sys2)[1].affect.system), we get:

4-element Vector{Equation}:
 lif1₊V(t) ~ lif1₊Eₘ
 lif1₊jcn(t) ~ lif2₊jcn(t) - 2lif1₊V(t) + qif₊jcn(t)
 qif₊G(t) ~ lif2₊jcn(t) - lif1₊V(t)
 lif2₊V(t) ~ lif1₊jcn(t) - qif₊G(t)

The observed equation lif2.jcn ~ lif1.V + qif.Ghas been solved for qif.G. Thus the explicit affect will update qif.G using the previous value of lif2.jcn, but what the event should do is change lif1.V and then the value of lif2.jcn should update in response.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
  [7d9f7c33] Accessors v0.1.42
⌃ [459566f4] DiffEqCallbacks v4.9.0
⌃ [31c24e10] Distributions v0.25.120
  [bcd5d0fe] GraphDynamics v0.4.9 `../GraphDynamics.jl`
  [86223c79] Graphs v1.13.1
  [626554b9] MetaGraphs v0.8.1
  [961ee093] ModelingToolkit v10.26.0
  [c1a47a78] NeurobloxBase v0.1.0 `NeurobloxBase`
  [92933f4c] ProgressMeter v1.11.0
⌃ [731186ca] RecursiveArrayTools v3.37.1
  [189a3867] Reexport v1.2.2
⌃ [0bca4576] SciMLBase v2.120.0
  [2913bbd2] StatsBase v0.34.6
⌅ [d1185830] SymbolicUtils v3.32.0
⌃ [0c5d862f] Symbolics v6.55.0
  [9a3f8284] Random v1.11.0
  [2f01184e] SparseArrays v1.12.0
  • Output of versioninfo()
Julia Version 1.12.0
Commit b907bd0600f (2025-10-07 15:42 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M2
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m2)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)
Environment:
  JULIA_EDITOR = vim
  JULIA_GR_PROVIDER = GR
  JULIA_PKG_PRESERVE_TIERED_INSTALLED = true

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    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