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

WIP: Delayed LTI #178

Merged
merged 33 commits into from
May 22, 2019
Merged

WIP: Delayed LTI #178

merged 33 commits into from
May 22, 2019

Conversation

mfalt
Copy link
Member

@mfalt mfalt commented Jan 24, 2019

Do not pull yet, still WIP

List of problems, feel free to add to it:

Should be resolved:

  • More extensive tests needed
  • Some discussions on the code below

Would be nice to have:

  • lsim doesn't handle D22 term
  • lsim interpolates x to get y (with D21 != 0), which is the only inhibitor for great accuracy (it is still very much usable, and x is correct at the grid points)
  • impulse: This is quite tricky, the first impulse can be handled, but if there is a direct term D21 to the delay, then we have to some way "store" the impulse train in there. I am not even sure how to begin to think about D22 != 0
  • Some sort of approximation of the delays would be useful to implement, e.g. for use with the design tools

src/types/DelayLtiSystem.jl Outdated Show resolved Hide resolved
# TODO: Think through these promotions and conversions
Base.promote_rule(::Type{<:StateSpace{T1}}, ::Type{DelayLtiSystem{T2}}) where {T1<:Number,T2<:Number} = DelayLtiSystem{promote_type(T1,T2)}
Base.promote_rule(::Type{AbstractMatrix{T1}}, ::Type{DelayLtiSystem{T2}}) where {T1<:Number,T2<:Number} = DelayLtiSystem{promote_type(T1,T2)}
Base.promote_rule(::Type{T1}, ::Type{DelayLtiSystem{T2}}) where {T1<:Number,T2<:Number} = DelayLtiSystem{promote_type(T1,T2)}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably be caught somewhere else by
Type{AbstractMatrix}, ::Type{<:LTISystem}
and
Type{<:Number}, ::Type{<:LTISystem}

DelayLtiSystem(psys_new.P, Tau_new)
end

function delay(tau::Real, T::Type{<:Number}=Float64)
Copy link
Member Author

@mfalt mfalt Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the canonical way, or maybe we should have:

delay(tau::T) where T<:Real

and

delay(::Type{T}, tau::Real) where T<:Number

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, any of those options are probably better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is delay a reasonable name for the function? It seems quite appropriate, but another option would be exp(-tau*tf("s"). However this transfer function notation is a bit unfortunate since time delays and transfer functions tend to be numerically troublesome.

@@ -4,6 +4,9 @@ abstract type LTISystem <: AbstractSystem end
*(sys1::LTISystem, sys2::LTISystem) = *(promote(sys1, sys2)...)
/(sys1::LTISystem, sys2::LTISystem) = /(promote(sys1, sys2)...)

feedback(sys1::Union{LTISystem,Number,AbstractMatrix{<:Number}},
sys2::Union{LTISystem,Number,AbstractMatrix{<:Number}}) = feedback(promote(sys1, sys2)...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should think about being consistent here, but that discussion probably belongs elsewehere.
For /(sys1::LTISystem, sys2::LTISystem) = /(promote(sys1, sys2)...)
we dont allow matrices and scalars (and assume the specific implementation will solve it), but for
feedback we provide a fallback here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think there are many places where more we should be more consistent.



# There should already exist a function like this somewhare?
function blkdiag(A1::Matrix{T1}, A2::Matrix{T2}) where {T1<:Number, T2<:Number}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractMatrix maybe for future-proofing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we would have to figure out a way of constructing the new matrix to be the promotetype, which might be tricky. So maybe we skip that

src/delay_systems.jl Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jan 24, 2019

Coverage Status

Coverage increased (+2.07%) to 58.087% when pulling 96acc55 on delayed_lti into 6312567 on master.

example/delayed_lti_system.jl Show resolved Hide resolved
example/delayed_lti_timeresp.jl Outdated Show resolved Hide resolved
example/delayed_lti_timeresp.jl Outdated Show resolved Hide resolved
sys = feedback(1.0, ss(-1.0, 2, 1, 0) * (delay(2.0) + delay(3.0) + delay(2.5)))

t = 0:0.02:8
@time t, x, y = lsim(sys, t; u=t->[t>=0 ? 1.0 : 0.0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@time t, x, y = lsim(sys, t; u=t->[t>=0 ? 1.0 : 0.0])
t, x, y = lsim(sys, t; u=t->[t>=0 ? 1.0 : 0.0])

gues this was a leftover from your benchmarking?

@@ -75,9 +78,11 @@ export LTISystem,


# QUESTION: are these used? LaTeXStrings, Requires, IterTools
using Polynomials, OrdinaryDiffEq, Plots, LaTeXStrings, LinearAlgebra
using Polynomials, Plots, LaTeXStrings, LinearAlgebra
using DifferentialEquations, OrdinaryDiffEq, DelayDiffEq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't DifferentialEquations an umbrella that exports everything else?

src/delay_systems.jl Outdated Show resolved Hide resolved
src/delay_systems.jl Outdated Show resolved Hide resolved
src/delay_systems.jl Outdated Show resolved Hide resolved
ny = noutputs(sys) - length(Tau)

if nu < 0 || ny < 0
error("Time vector is too long.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error("Time vector is too long.")
throw(ArgumentError("Time vector is too long."))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An argument error is more informative, and people doing error handling in applications sometimes rely on the proper type of error being thrown. See https://discourse.julialang.org/t/error-vs-throw-argumenterror/18915

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being more specific also allows you to write tests with @test_throws that don’t just pass spuriously if the same type of error is thrown in some other part of the code under test.

src/types/PartionedStateSpace.jl Outdated Show resolved Hide resolved
ssold = sys.P.P
# Add to direct term from input to output
new_D = ssold.D
new_D[1:ny, 1:nu] .+= n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do broadcast

@baggepinnen
Copy link
Member

I rebased this on master, I think I managed to pull it off without screwing up.

@mfalt mfalt merged commit 9e5a846 into master May 22, 2019
@baggepinnen baggepinnen deleted the delayed_lti branch September 24, 2020 06:53
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

Successfully merging this pull request may close these issues.

4 participants