154154
155155
156156"""
157- RobustAndOptimalControl.named_ss(sys::ModelingToolkit.AbstractTimeDependentSystem , inputs, outputs; descriptor=true, kwargs...)
157+ RobustAndOptimalControl.named_ss(sys::ModelingToolkit.AbstractSystem , inputs, outputs; descriptor=true, simple_infeigs =true, kwargs...)
158158
159159Convert an `ODESystem` to a `NamedStateSpace` using linearization. `inputs, outputs` are vectors of variables determining the inputs and outputs respectively. See docstring of `ModelingToolkit.linearize` for more info on `kwargs`.
160160
@@ -170,6 +170,7 @@ function RobustAndOptimalControl.named_ss(
170170 inputs,
171171 outputs;
172172 descriptor = true ,
173+ simple_infeigs = true ,
173174 kwargs... ,
174175)
175176
@@ -209,20 +210,25 @@ function RobustAndOptimalControl.named_ss(
209210 # This indicates that input derivatives are present
210211 duinds = findall (any (! iszero, eachcol (matrices. B[:, nu+ 1 : end ]))) .+ nu
211212 u2du = (1 : nu) .=> duinds # This maps inputs to their derivatives
212- lsys = causal_simplification (matrices, u2du; descriptor)
213+ lsys = causal_simplification (matrices, u2du; descriptor, simple_infeigs )
213214 else
214215 lsys = ss (matrices... )
215216 end
217+ # If simple_infeigs=false, the system might have been reduced and the state names might not match the original system.
218+ x_names = simple_infeigs ? symstr .(unknowns (ssys)) : [string (nameof (sys))* " _x$i " for i in 1 : lsys. nx]
216219 named_ss (
217220 lsys;
218- x = symstr .( unknowns (ssys)) ,
221+ x = x_names ,
219222 u = unames,
220223 y = symstr .(outputs),
221224 name = string (Base. nameof (sys)),
222225 )
223226end
224227
225- function causal_simplification (sys, u2duinds:: Vector{Pair{Int, Int}} ; descriptor= true )
228+ """
229+ causal_simplification(sys, u2duinds::Vector{Pair{Int, Int}}; descriptor=true, simple_infeigs = true)
230+ """
231+ function causal_simplification (sys, u2duinds:: Vector{Pair{Int, Int}} ; descriptor= true , simple_infeigs = true )
226232 fm (x) = convert (Matrix{Float64}, x)
227233 nx = size (sys. A, 1 )
228234 ny = size (sys. C, 1 )
@@ -245,7 +251,7 @@ function causal_simplification(sys, u2duinds::Vector{Pair{Int, Int}}; descriptor
245251 Ce = [fm (sys. C) zeros (ny, ndu)]
246252 De = fm (D)
247253 dsys = dss (Ae, E, Be, Ce, De)
248- return ss (RobustAndOptimalControl. DescriptorSystems. dss2ss (dsys)[1 ])
254+ return ss (RobustAndOptimalControl. DescriptorSystems. dss2ss (dsys; simple_infeigs )[1 ])
249255 else
250256 ss (sys. A, B, sys. C, D) + ss (sys. A, B̄, sys. C, D̄)* tf (' s' )
251257 end
@@ -288,6 +294,8 @@ function named_sensitivity_function(
288294 fun,
289295 sys:: ModelingToolkit.AbstractTimeDependentSystem ,
290296 inputs, args... ;
297+ descriptor = true ,
298+ simple_infeigs = true ,
291299 kwargs... ,
292300)
293301
@@ -310,29 +318,17 @@ function named_sensitivity_function(
310318 unames = symstr .(inputs)
311319 fm (x) = convert (Matrix{Float64}, x)
312320 if nu > 0 && size (matrices. B, 2 ) == 2 nu
313- nx = size (matrices. A, 1 )
314- # This indicates that input derivatives are present
315- duinds = findall (any (! iszero, eachcol (matrices. B[:, nu+ 1 : end ])))
316- B̄ = matrices. B[:, duinds .+ nu]
317- ndu = length (duinds)
318- B = matrices. B[:, 1 : nu]
319- Iu = duinds .== (1 : nu)'
320- E = [I (nx) - B̄; zeros (ndu, nx+ ndu)]
321-
322- Ae = cat (matrices. A, - I (ndu), dims= (1 ,2 ))
323- Be = [B; Iu]
324- Ce = [fm (matrices. C) zeros (ny, ndu)]
325- De = fm (matrices. D[:, 1 : nu])
326- dsys = dss (Ae, E, Be, Ce, De)
327- lsys = ss (RobustAndOptimalControl. DescriptorSystems. dss2ss (dsys)[1 ])
328- # unames = [unames; Symbol.("der_" .* string.(unames))]
329- # sys = ss(matrices...)
321+ # This indicates that input derivatives are present
322+ duinds = findall (any (! iszero, eachcol (matrices. B[:, nu+ 1 : end ]))) .+ nu
323+ u2du = (1 : nu) .=> duinds # This maps inputs to their derivatives
324+ lsys = causal_simplification (matrices, u2du; descriptor, simple_infeigs)
330325 else
331326 lsys = ss (matrices... )
332327 end
328+ x_names = simple_infeigs ? symstr .(unknowns (ssys)) : [string (nameof (sys))* " _x$i " for i in 1 : lsys. nx]
333329 named_ss (
334330 lsys;
335- x = symstr .( unknowns (ssys)) ,
331+ x = x_names ,
336332 u = unames,
337333 y = unames, # Symbol.("out_" .* string.(inputs)),
338334 name = string (Base. nameof (sys)),
0 commit comments