Skip to content

Commit 690e7f9

Browse files
committed
preserve op through balancing and sminreal
1 parent 58c2aea commit 690e7f9

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/named_systems2.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ end
112112

113113
function Base.convert(::Type{NamedStateSpace{T, S}}, s::NamedStateSpace{T, U}) where {T, S <: AbstractStateSpace, U <: AbstractStateSpace}
114114
sys = Base.convert(S, s.sys)
115-
NamedStateSpace{T,typeof(sys)}(sys, s.x, s.u, s.y, s.name)
115+
NamedStateSpace{T,typeof(sys)}(sys, s.x, s.u, s.y, s.name, s.extra)
116116
end
117117

118118
function Base.convert(::Type{NamedStateSpace{T, S}}, s::U) where {T, S <: AbstractStateSpace, U <: TransferFunction}
@@ -151,6 +151,7 @@ end
151151
Return a named tuple `(; x, u)` containing the operating point of the system `P`. If no operating point is set, a zero operating point of correct dimension is returned.
152152
"""
153153
operating_point(P::NamedStateSpace) = get_extra(P, :operating_point, (x=zeros(P.nx), u=zeros(P.nu)))
154+
operating_point(P::AbstractStateSpace) = (x=zeros(P.nx), u=zeros(P.nu))
154155
set_operating_point!(P::NamedStateSpace, xu::NamedTuple{(:x,:u)}) = set_extra!(P, :operating_point, xu)
155156

156157
const NamedIndex = Union{Symbol, Vector{Symbol}, Colon}
@@ -748,7 +749,11 @@ function ControlSystemsBase.sminreal(s::NamedStateSpace)
748749
return s
749750
end
750751
_, _, _, inds = CS.struct_ctrb_obsv(s.sys) # we do this one more time to get the inds. This implies repeated calculations, but will allow inner systems of exotic types that have a special method for sminreal to keep their type.
751-
named_ss(sys; x=s.x[inds], s.u, s.y, s.name)
752+
op = operating_point(s)
753+
op = (x = op.x[inds], u = op.u)
754+
newsys = named_ss(sys; x=s.x[inds], s.u, s.y, s.name, s.extra)
755+
set_operating_point!(newsys, op)
756+
return newsys
752757
end
753758

754759
names2indices(::Colon, allnames) = 1:length(allnames)
@@ -927,7 +932,7 @@ function CS.add_output(sys::NamedStateSpace, C2::AbstractArray, D2=0; y = [Symbo
927932
x = sys.x
928933
u = sys.u
929934
y = [sys.y; y]
930-
named_ss(ss(A, B, [C; C2], [D; D3]), sys.timeevol; x, u, y)
935+
named_ss(ss(A, B, [C; C2], [D; D3]), sys.timeevol; x, u, y, sys.extra)
931936
end
932937

933938

@@ -948,4 +953,13 @@ for fun in [:baltrunc2, :baltrunc_coprime]
948953
msys, rest... = $(fun)(sys.sys, args...; kwargs...)
949954
named_ss(msys; sys.u, sys.y, sys.name), rest...
950955
end
956+
end
957+
958+
function CS.balance_statespace(sys::NamedStateSpace, args...; kwargs...)
959+
msys, T, rest... = CS.balance_statespace(sys.sys, args...; kwargs...)
960+
op = operating_point(sys)
961+
op = (x = T*op.x, u = op.u)
962+
newsys = named_ss(msys; sys.u, sys.y, sys.name)
963+
set_operating_point!(newsys, op)
964+
newsys, T, rest...
951965
end

0 commit comments

Comments
 (0)