Skip to content
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

Variable does not exist when it used in discrete_events but not in dynamics equation #105

Closed
florian-die opened this issue Sep 1, 2022 · 1 comment

Comments

@florian-die
Copy link

Hi,

I'm trying to connect a linear system with a sensor. The output of the sensor is continuous and updated at sampled times, it stays constant in between sampled times.

My issue is that in the dynamics equation of the sensor, the state and control of the linear system do not appear. They appear only in the discrete_events affect. This throws an error when I try to connect the sensor and the system.

The code is the following.

using ModelingToolkit
using DifferentialEquations

A = [0. 1. ; 0. 0.] # 2×2 Matrix{Float64}
B = [0. ; 1.0 ;;] # 2×1 Matrix{Float64}
C = [1.0 0.0 ] # 1×2 Matrix{Float64}
D = [0.0 ;;] # 1×1 Matrix{Float64}

x0 = [0.0 ; 1.0 ;;] # 2×1 Matrix{Float64}
u0 = [0.0 ;;] # 1×1 Matrix{Float64}

Δt = 1.0
tf = 10.0

y0 = C*x0 + D*u0 # 1×1 Matrix{Float64}

function linear_system(A,B;name)
    nx = size(A,1) 
    nu = size(B,2)

    @variables t x(t)[1:nx,1] u(t)[1:nu,1] 
    d_dt = Differential(t)

    eqs = d_dt.(x) ~ A * x + B * u 

    ODESystem(eqs;name)
end

@named system = linear_system(A,B)

function linear_sensor(C,D,Δt;name)

    nx = size(C,2)
    ny = size(C,1)
    nu = size(D,2)

    @variables t x(t)[1:nx,1] u(t)[1:nu,1] y(t)[1:ny,1]
    d_dt = Differential(t)

    eqs = d_dt.(y) .~ 0. 

    measure = y ~ C * x  + D * u

    update = [Δt => measure]

    ODESystem(eqs;name,discrete_events=update)
end

@named sensor = linear_sensor(C,D,Δt)

connections = [ Symbolics.scalarize(sensor.x ~ system.x) ;
                Symbolics.scalarize(sensor.u ~ system.u) ]

The error occurring at the last line

ERROR: ArgumentError: System sensor: variable x does not exist
Stacktrace:
 [1] getvar(sys::ODESystem, name::Symbol; namespace::Bool)
   @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\abstractsystem.jl:316
 [2] getproperty(sys::ODESystem, name::Symbol; namespace::Bool)
   @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\abstractsystem.jl:270
 [3] getproperty(sys::ODESystem, name::Symbol)
   @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\abstractsystem.jl:269
 [4] top-level scope
   @ d:\fdietric\Documents\Julia\ModelingToolkit\kalmanfilter\kalmanfilter.jl:51

Additionally, I think my affect equation also throws an error of the type

ERROR: MethodError: no method matching compile_affect(::Matrix{Equation}, ::ODESystem, ::Vector{Term{Real, Nothing}}, ::Vector{Any}; expression=Val{false}, postprocess_affect_e
Closest candidates are:
  compile_affect(::ModelingToolkit.SymbolicContinuousCallback, ::Any...; kwargs...) at D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:275
  compile_affect(::Vector{Equation}, ::Any, ::Any, ::Any; outputidxs, expression, checkvars, postprocess_affect_expr!, kwargs...) at D:\fdietric\.julia\packages\ModelingToolkit\callbacks.jl:295
  compile_affect(::ModelingToolkit.FunctionalAffect, ::Any, ::Any, ::Any; kwargs...) at D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:439
Stacktrace:
  [1] generate_timed_callback(cb::ModelingToolkit.SymbolicDiscreteCallback, sys::ODESystem, dvs::Vector{Term{Real, Nothing}}, ps::Vector{Any}; postprocess_affect_expr!::Nothingrs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:446
  [2] generate_discrete_callback(cb::ModelingToolkit.SymbolicDiscreteCallback, sys::ODESystem, dvs::Vector{Term{Real, Nothing}}, ps::Vector{Any}; postprocess_affect_expr!::NothPairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:460
  [3] generate_discrete_callback
    @ D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:457 [inlined]
  [4] #313
    @ D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:477 [inlined]
  [5] iterate(::Base.Generator{Vector{ModelingToolkit.SymbolicDiscreteCallback}, ModelingToolkit.var"#313#314"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ODrm{Real, Nothing}}, Vector{Any}}})
    @ Base .\generator.jl:47
  [6] _collect(c::Vector{ModelingToolkit.SymbolicDiscreteCallback}, itr::Base.Generator{Vector{ModelingToolkit.SymbolicDiscreteCallback}, ModelingToolkit.var"#313#314"{Base.Pai, Tuple{}, NamedTuple{(), Tuple{}}}, ODESystem, Vector{Term{Real, Nothing}}, Vector{Any}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base .\array.jl:807
  [7] collect_similar(cont::Vector{ModelingToolkit.SymbolicDiscreteCallback}, itr::Base.Generator{Vector{ModelingToolkit.SymbolicDiscreteCallback}, ModelingToolkit.var"#313#314l, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ODESystem, Vector{Term{Real, Nothing}}, Vector{Any}}})
    @ Base .\array.jl:716
  [8] map(f::Function, A::Vector{ModelingToolkit.SymbolicDiscreteCallback})
    @ Base .\abstractarray.jl:2933
  [9] generate_discrete_callbacks(sys::ODESystem, dvs::Vector{Term{Real, Nothing}}, ps::Vector{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:476
 [10] generate_discrete_callbacks(sys::ODESystem, dvs::Vector{Term{Real, Nothing}}, ps::Vector{Any}) (repeats 2 times)
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:470
 [11] process_events(sys::ODESystem; callback::Nothing, has_difference::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\callbacks.jl:495
 [12] (ODEProblem{true})(sys::ODESystem, u0map::Matrix{Float64}, tspan::Tuple{Float64, Float64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, kwarbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\diffeqs\abstractodesystem.jl:640
 [13] (ODEProblem{true})(sys::ODESystem, u0map::Matrix{Float64}, tspan::Tuple{Float64, Float64}, parammap::SciMLBase.NullParameters) (repeats 2 times)
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\diffeqs\abstractodesystem.jl:632
 [14] ODEProblem(::ODESystem, ::Matrix{Float64}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\diffeqs\abstractodesystem.jl:629
 [15] ODEProblem(::ODESystem, ::Matrix{Float64}, ::Vararg{Any})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\5qzZn\src\systems\diffeqs\abstractodesystem.jl:628
 [16] top-level scope
    @ d:\fdietric\Documents\Julia\ModelingToolkit\kalmanfilter\kalmanfilter.jl:51

Thanks in advance for your help

pkg> status
Status `D:\fdietric\Documents\Julia\ModelingToolkit\kalmanfilter\Project.toml`
  [0c46a032] DifferentialEquations v7.3.0
  [961ee093] ModelingToolkit v8.20.0
@florian-die
Copy link
Author

wrong repo, sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant