Skip to content

Commit 6cdf394

Browse files
authored
Merge pull request #113 from JuliaControl/namedfixes
QOL improvements for named systems
2 parents a41416d + ae72457 commit 6cdf394

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/named_systems2.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ end
4747
NamedStateSpace(A,B,C,D,x::AbstractVector,u,y,name::String="") = NamedStateSpace{Continuous, StateSpace{Continuous, eltype(A)}}(ss(A,B,C,D), x, u, y, name)
4848
NamedStateSpace(A,B,C,D,Ts::Number,x,u,y,name::String="") = NamedStateSpace{Discrete{typeof(Ts)}, StateSpace{Discrete{typeof(Ts)}, eltype(A)}}(ss(A,B,C,D,Ts), x, u, y,name)
4949

50+
# This method is used by the basetype(ST)(A, B, C, D, timeevol) construct
51+
NamedStateSpace(A,B,C,D,te::ControlSystemsBase.TimeEvolution, args...; kwargs...) = named_ss(ss(A,B,C,D,te), args...; kwargs...)
52+
5053
function Base.promote_rule(::Type{U}, ::Type{NamedStateSpace{T, S}}) where
5154
{T, U<:AbstractStateSpace{T} , S<:AbstractStateSpace{T}}
5255
inner = promote_type(U,S)
@@ -70,6 +73,10 @@ function Base.promote_rule(::Type{U}, ::Type{NamedStateSpace{T, S}}) where
7073
NamedStateSpace{T, inner}
7174
end
7275

76+
function Base.promote_rule(::Type{NamedStateSpace{TE, StateSpace{TE, T1}}}, ::Type{N}) where {TE, T1, N<:Number}
77+
NamedStateSpace{TE, StateSpace{TE, promote_type(T1,N)}}
78+
end
79+
7380

7481

7582
function Base.convert(::Type{NamedStateSpace{T, S}}, s::U) where {T, S <: AbstractStateSpace, U <: AbstractStateSpace}
@@ -93,6 +100,12 @@ function Base.convert(::Type{NamedStateSpace{T, S}}, s::U) where {T, S <: Abstra
93100
named_ss(s2, x = gensym("x"), u = gensym("u"), y = gensym("y"))
94101
end
95102

103+
function Base.convert(::Type{NamedStateSpace{T, S}}, N::Number) where {T, S <: AbstractStateSpace}
104+
te = T <: Discrete ? Discrete(ControlSystemsBase.UNDEF_SAMPLEPETIME) : Continuous()
105+
NT = numeric_type(S)
106+
named_ss(tf(NT(N), te), x = gensym("x"), u = gensym("u"), y = gensym("y"))
107+
end
108+
96109
# function Base.convert(::Type{TransferFunction{TE, S}}, s::U) where {TE, S, U <: NamedStateSpace{TE}}
97110
# convert(TransferFunction{TE, S}, s.sys)
98111
# end
@@ -199,6 +212,11 @@ named_ss(G::LTISystem, args...; kwargs...) = named_ss(ss(G), args...; kwargs...)
199212

200213
ControlSystemsBase.ss(sys::NamedStateSpace) = ss(sys.sys)
201214

215+
function ControlSystemsBase.StaticStateSpace(sys::NamedStateSpace)
216+
ssys = StaticStateSpace(sys.sys)
217+
named_ss(ssys, sys.name; sys.x, sys.u, sys.y)
218+
end
219+
202220
iterable(s::Symbol) = [s]
203221
iterable(v) = v
204222

@@ -330,6 +348,15 @@ end
330348
function Base.:/(s::NamedStateSpace{T, S}, n::Number) where {T <: CS.TimeEvolution, S}
331349
s*(1/n)
332350
end
351+
352+
function Base.:/(n::Number, sys::NamedStateSpace)
353+
isys = n / sys.sys
354+
if isone(n)
355+
return named_ss(isys, sys.name*"_inverse", x = sys.x, u = sys.y, y = sys.u)
356+
else
357+
return named_ss(isys, sys.name*"_scaled_inverse")
358+
end
359+
end
333360
##
334361

335362
function Base.hcat(systems::NamedStateSpace{T,S}...) where {T,S}

test/test_diskmargin.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ControlSystemsBase, RobustAndOptimalControl, MonteCarloMeasurements
1+
using ControlSystemsBase, RobustAndOptimalControl, MonteCarloMeasurements, Test
22
# using RobustAndOptimalControl: bisect_a
33

44
# Example from the diskmargin paper
@@ -21,7 +21,11 @@ gainphaseplot(L)
2121
@test dm.phasemargin == dm.ϕm
2222
@test :gainmargin propertynames(dm)
2323

24-
24+
dm = diskmargin(named_ss(L), 0)
25+
@test dm.ω0 1.94 atol=0.02
26+
@test dm.γmin 0.63 atol=0.02
27+
@test dm.γmax 1.59 atol=0.02
28+
@test dm.α 0.46 atol=0.02
2529

2630
## Frequency-dependent margin
2731
w = exp10.(LinRange(-2, 2, 500))

test/test_named_systems2.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,16 @@ sys12 = connect(
425425

426426
@test sys12.B[1:2,1] == sys1.B[:, 1]
427427
@test sys12.B[:,2] == [sys1.B[:, 2]; sys2.B[:, 1]]
428-
@test sys12.B[3:4,3] == sys2.B[:, 2]
428+
@test sys12.B[3:4,3] == sys2.B[:, 2]
429+
430+
## Inv
431+
s1 = named_ss(ssrand(2,2,2))
432+
isys = inv(s1)
433+
@test isys.sys == inv(s1.sys)
434+
@test isys.x == s1.x
435+
@test isys.u == s1.y # Names are reversed
436+
@test isys.y == s1.u
437+
438+
isys = 2/s1
439+
@test isys.sys == 2/s1.sys
440+
@test isys.x != s1.x

0 commit comments

Comments
 (0)