Skip to content

Commit aa29df2

Browse files
committed
named ss utils
1 parent cad2b0d commit aa29df2

4 files changed

Lines changed: 43 additions & 11 deletions

File tree

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RobustAndOptimalControl"
22
uuid = "21fd56a4-db03-40ee-82ee-a87907bee541"
33
authors = ["Fredrik Bagge Carlson", "Marcus Greiff"]
4-
version = "0.2.5"
4+
version = "0.3.0"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
@@ -15,6 +15,7 @@ MatrixEquations = "99c1a7ee-ab34-5fd5-8076-27c950a045f4"
1515
MatrixPencils = "48965c70-4690-11ea-1f13-43a2532b2fa8"
1616
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
1717
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
18+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1819
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1920
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2021
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
@@ -32,15 +33,16 @@ MatrixEquations = "2"
3233
MatrixPencils = "1"
3334
MonteCarloMeasurements = "1.0"
3435
Optim = "1.5"
36+
Plots = "1"
3537
RecipesBase = "1"
3638
UnPack = "1.0"
3739
julia = "1.6"
3840

3941
[extras]
42+
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
4043
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
4144
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4245
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
43-
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
4446

4547
[targets]
4648
test = ["Test", "Plots", "Zygote", "FiniteDiff"]

src/RobustAndOptimalControl.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module RobustAndOptimalControl
22

33
using ControlSystems: issiso
44
using LinearAlgebra, Statistics
5+
using Plots
56
using RecipesBase
67
using ControlSystems
78
import ControlSystems: ss, ssdata, ninputs, noutputs, nstates, isdiscrete, iscontinuous, to_matrix, timeevol, _string_mat_with_headers, PartionedStateSpace, common_timeevol

src/named_systems2.jl

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,17 @@ function named_ss(sys::AbstractStateSpace{T};
141141
end
142142

143143
"""
144-
named_ss(sys::AbstractStateSpace, name)
144+
named_ss(sys::AbstractStateSpace, name; x, y, u)
145145
146146
If a single name is provided, the outputs, inputs and states will be automatically named
147147
`y,u,x` with `name` as prefix.
148148
"""
149-
function named_ss(sys::AbstractStateSpace, name)
150-
named_ss(sys,
151-
x = Symbol(string(name)*"x"),
152-
y = Symbol(string(name)*"y"),
153-
u = Symbol(string(name)*"u"),
149+
function named_ss(sys::AbstractStateSpace, name;
150+
x = Symbol(string(name)*"x"),
151+
y = Symbol(string(name)*"y"),
152+
u = Symbol(string(name)*"u"),
154153
)
154+
named_ss(sys; x, y, u)
155155
end
156156

157157
ControlSystems.ss(sys::NamedStateSpace) = ss(sys.sys)
@@ -579,6 +579,35 @@ function named_ss(sys::ExtendedStateSpace{T}, name="";
579579
end
580580

581581

582+
function partition(P::NamedStateSpace; u=nothing, y=nothing,
583+
w = nothing,
584+
z = nothing
585+
)
586+
if w === nothing
587+
w = names2indices(setdiff(P.u, u), P.u)
588+
u = names2indices(u, P.u)
589+
end
590+
if z === nothing
591+
z = names2indices(setdiff(P.y, y), P.y)
592+
y = names2indices(y, P.y)
593+
end
594+
if u === nothing
595+
u = names2indices(setdiff(P.u, w), P.u)
596+
w = names2indices(w, P.u)
597+
end
598+
if y === nothing
599+
y = names2indices(setdiff(P.y, z), P.y)
600+
z = names2indices(z, P.y)
601+
end
602+
u = vcat(u)
603+
y = vcat(y)
604+
z = vcat(z)
605+
w = vcat(w)
606+
ss(P.A, P.B[:, w], P.B[:, u], P.C[z, :], P.C[y, :],
607+
P.D[z, w], P.D[z, u], P.D[y, w], P.D[y, u], P.timeevol)
608+
end
609+
610+
582611
CS.@recipe function plot(res::CS.SimResult{<:Any, <:Any, <:Any, <:Any, <:NamedStateSpace})
583612
s = res.sys
584613
label --> permutedims(["From $n" for n in s.u])

src/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ D =
2121
Continuous-time state-space model
2222
2323
julia> show_construction(sys, name="Jörgen")
24-
sys = let
24+
Jörgen = let
2525
JörgenA = [-1.0;;]
2626
JörgenB = [1.0;;]
2727
JörgenC = [1.0;;]
@@ -32,13 +32,13 @@ end
3232
"""
3333
function show_construction(io::IO, sys::LTISystem; name = "temp", letb = true)
3434
# sys = StateSpace(sys)
35-
letb && println(io, "sys = let")
35+
letb && println(io, "$name = let")
3636
prestr = letb ? " " : ""
3737
println(io, prestr*"$(name)A = ", sys.A)
3838
println(io, prestr*"$(name)B = ", sys.B)
3939
println(io, prestr*"$(name)C = ", sys.C)
4040
println(io, prestr*"$(name)D = ", sys.D)
41-
letb || print(io, "sys = ")
41+
letb || print(io, "$name = ")
4242
if isdiscrete(sys)
4343
println(io, prestr*"ss($(name)A, $(name)B, $(name)C, $(name)D, $(sys.Ts))")
4444
else

0 commit comments

Comments
 (0)