From d3c75c6ea8f4b4a4ef3ed0f421827b7bb065285f Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 12 May 2023 15:05:25 +0200 Subject: [PATCH 1/3] make `balance_statespace` work for more system types closes #835 --- lib/ControlSystemsBase/src/types/conversion.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ControlSystemsBase/src/types/conversion.jl b/lib/ControlSystemsBase/src/types/conversion.jl index ee52021e9..bcdc6d9e2 100644 --- a/lib/ControlSystemsBase/src/types/conversion.jl +++ b/lib/ControlSystemsBase/src/types/conversion.jl @@ -187,9 +187,10 @@ end # balance_statespace(A2, B2, C2, perm) # end -function balance_statespace(sys::StateSpace, perm::Bool=false) +function balance_statespace(sys::AbstractStateSpace, perm::Bool=false) A, B, C, T = balance_statespace(sys.A,sys.B,sys.C, perm) - return ss(A,B,C,sys.D,sys.timeevol), T + + return basetype(sys)(A,B,C,sys.D, ntuple(i->getfield(sys, i+4), fieldcount(T)-4)...), T end # Method that might fail for some exotic types, such as TrackedArrays From 5e121f57748f4e0e503ed3f354aa72d91fbdd144 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 12 May 2023 15:26:53 +0200 Subject: [PATCH 2/3] mention balancing in simulation section as well --- docs/src/man/numerical.md | 2 ++ lib/ControlSystemsBase/src/types/conversion.jl | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/src/man/numerical.md b/docs/src/man/numerical.md index 6fd34d26b..5ca66768b 100644 --- a/docs/src/man/numerical.md +++ b/docs/src/man/numerical.md @@ -114,3 +114,5 @@ Linear systems with zero-order-hold inputs can be exactly simulated in discrete For discrete-time systems, the function [`lsim!`](@ref) accepts a pre-allocated workspace objects that can be used to avoid allocations for repeated simulations. +### Numerical balancing +If you are only interested in the simulated outputs, not the state trajectories, you may consider applying balancing to the statespace model using [`balance_statespace`](@ref) before simulating, see the section on [State-space balancing](@ref) above. If the state trajectories are of interest, balancing can still be performed before simulation, and the inverse transformation applied to the state trajectories after simulation. \ No newline at end of file diff --git a/lib/ControlSystemsBase/src/types/conversion.jl b/lib/ControlSystemsBase/src/types/conversion.jl index bcdc6d9e2..329dc0b33 100644 --- a/lib/ControlSystemsBase/src/types/conversion.jl +++ b/lib/ControlSystemsBase/src/types/conversion.jl @@ -190,7 +190,7 @@ end function balance_statespace(sys::AbstractStateSpace, perm::Bool=false) A, B, C, T = balance_statespace(sys.A,sys.B,sys.C, perm) - return basetype(sys)(A,B,C,sys.D, ntuple(i->getfield(sys, i+4), fieldcount(T)-4)...), T + return basetype(sys)(A,B,C,sys.D, ntuple(i->getfield(sys, i+4), fieldcount(sys)-4)...), T end # Method that might fail for some exotic types, such as TrackedArrays From 3c7632f5a316abc320410e863978e11906103de4 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Sat, 13 May 2023 07:46:49 +0200 Subject: [PATCH 3/3] Update conversion.jl --- lib/ControlSystemsBase/src/types/conversion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ControlSystemsBase/src/types/conversion.jl b/lib/ControlSystemsBase/src/types/conversion.jl index 329dc0b33..932fa2875 100644 --- a/lib/ControlSystemsBase/src/types/conversion.jl +++ b/lib/ControlSystemsBase/src/types/conversion.jl @@ -190,7 +190,7 @@ end function balance_statespace(sys::AbstractStateSpace, perm::Bool=false) A, B, C, T = balance_statespace(sys.A,sys.B,sys.C, perm) - return basetype(sys)(A,B,C,sys.D, ntuple(i->getfield(sys, i+4), fieldcount(sys)-4)...), T + return basetype(sys)(A,B,C,sys.D, ntuple(i->getfield(sys, i+4), fieldcount(typeof(sys))-4)...), T end # Method that might fail for some exotic types, such as TrackedArrays