Skip to content

Failure to access discrete-time variable in solution #3499

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

Open
baggepinnen opened this issue Mar 26, 2025 · 4 comments
Open

Failure to access discrete-time variable in solution #3499

baggepinnen opened this issue Mar 26, 2025 · 4 comments
Assignees
Labels
bug Something isn't working discrete-time

Comments

@baggepinnen
Copy link
Contributor

baggepinnen commented Mar 26, 2025

julia> sol[sys.conversion.y]
ERROR: ArgumentError: Symbol conversion₊y(t) is not present in the system.

MWE:

using ModelingToolkit
using OrdinaryDiffEq
t = ModelingToolkit.t_nounits
k = ShiftIndex(t)


function Float2Bool2(; name)
    @variables begin
        u(t)::Float64
        y(t)::Bool
    end
    equations = [
        y ~ u != 0
    ]
    DiscreteSystem(equations, t; name)
end

function Model2(; name)
    @variables begin
        x(t) = 1
    end
    systems = @named begin
        conversion = Float2Bool2()
    end
    equations = [
        x(k) ~ 0.99x(k-1)
        conversion.u ~ x
    ]
    DiscreteSystem(equations, t; name, systems)
end

@mtkbuild sys = Model2()

prob = DiscreteProblem(sys, [], (0,10))

sol = solve(prob, FunctionMap())
sol[sys.conversion.y]
@AayushSabharwal
Copy link
Member

This is known behavior. Due to how structural simplification of discrete systems is handled, observed variables are sihfted forward by one step. Fixing this requires proper compilation of synchronous systems.

julia> observed(sys)
2-element Vector{Equation}:
 Shift(t, 1)(conversion₊u(t)) ~ 0.99x(t)
 Shift(t, 1)(conversion₊y(t)) ~ Shift(t, 1)(conversion₊u(t)) != 0

julia> sol[sys.conversion.y(k+1)]
11-element BitVector:
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1

@AayushSabharwal
Copy link
Member

We have had this behavior since MTKv9

@baggepinnen
Copy link
Contributor Author

This is known behavior.

Do we have an existing issue for it? It may be known, but certainly not desirable so we should track it and eventually fix it

@AayushSabharwal
Copy link
Member

I don't think we do. This can serve as the tracking issue, I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discrete-time
Projects
None yet
Development

No branches or pull requests

2 participants