# Naming of states, inputs and outputs in \`ControlSystemsMTK\`

**URL:** <https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit, controlsystems\
**Created:** [March 18, 2024, 5:14pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783 "2024-03-18T17:14:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 18, 2024, 5:14pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/1 "2024-03-18T17:14:46Z")

</div>

Hi, I just tested naming states, inputs and outputs while passing a state-space model to `ODESystem`.

It seems the optional names are ignored, am I doing something wrong here ?

```julia
using ControlSystemsBase, ControlSystemsMTK, ModelingToolkit, LinearAlgebra
ssmodel = ss(Diagonal(rand(10)), rand(10,3), rand(2,10),0)
x_names = [Symbol("x$i") for i in 1:10]
sys = ModelingToolkit.ODESystem(ssmodel; name=:sys, x=x_names)

```

This returns

> Model sys with 12 equations  
> Unknowns (15):  
> (x(t))[1] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[2] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[3] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[4] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[5] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[6] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[7] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[8] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[9] [defaults to 0.0]: State variables of StateSpace system sys  
> (x(t))[10] [defaults to 0.0]: State variables of StateSpace system sys  
> ⋮  
> Parameters (0):

I’m on

> [a6e380b2] ControlSystems v1.10.0  
> [aaaaaaaa] ControlSystemsBase v1.10.2  
> [687d7614] ControlSystemsMTK v2.0.1  
> [961ee093] ModelingToolkit v9.5.0

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 18, 2024, 5:58pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/2 "2024-03-18T17:58:03Z")

</div>

Yeah, I don’t think it’s possible to easily change the name of a variable that appears in a ModelingToolkit system, and here we use `Blocks.StateSpace` from MTK stdlib. Maybe one could do something with `replace`, I haven’t looked into the possibilities here.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 18, 2024, 8:52pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/3 "2024-03-18T20:52:29Z")

</div>

Ah, I think I just understood your answer, maybe I should just implement my own block then, because this seems to fall down to `RealInput` / `RealOutput`. So if I need to name my variables it looks simpler to rebuilt `StateSpace` with additional parameters and one named subcomponent per input/output.

Regarding the `replace` I wouldn’t even know what to replace ^^. The MTK internals are somewhat cryptic sometimes

**EDIT** Something else related, would you know how to connect a `Block` to the input or output of a `StateSpace`, I’m receiving `ERROR: None Sym BasicSymbolic doesn't have a name` errors…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 19, 2024, 5:37am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/4 "2024-03-19T05:37:26Z")

</div>

> [@BambOoxX](#):
>
> **EDIT** Something else related, would you know how to connect a `Block` to the input or output of a `StateSpace`, I’m receiving `ERROR: None Sym BasicSymbolic doesn't have a name` errors…

You need to be more precise here, what is a `Block`? and are you talking about `ControlSystems.StateSpace` or `ModelingToolkitStandardLibrary.Blocks.StateSpace`? Here’s an example that performs multiple connections between systems from the `ModelingToolkitStandardLibrary.Blocks` submodule

> **[Batch linearization and gain scheduling · ControlSystemsMTK Documentation](https://juliacontrol.github.io/ControlSystemsMTK.jl/dev/batch_linearization/#Gain-scheduling)**
>
> Documentation for ControlSystemsMTK Documentation.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 19, 2024, 7:20am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/5 "2024-03-19T07:20:54Z")

</div>

Sorry, my previous message was not very clear indeed.

I’m trying to do something like

```julia
using ControlSystemsBase, ControlSystemsMTK, ModelingToolkit, LinearAlgebra, ModelingToolkitStandardLibrary
ssmodel = ss(Diagonal(rand(10)), rand(10,3), rand(2,10),0)
sys = ModelingToolkit.ODESystem(ssmodel; name=:sys)

@named sine = ModelingToolkitStandardLibrary.Blocks.Sine(; frequency=3)

eqs = Equation[
       connect(sys.input.u[3], sine.output)
]

```

But the last line fails with `ERROR: None Sym BasicSymbolic doesn't have a name`. Unless I missed something, in the example you linked, there are only scalar `connect`ions, right ?

I guess if this is not yet supported a quick and dirty way to solve both problems is to scalarize everything in a `ScalarStateSpace`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 19, 2024, 7:31am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/6 "2024-03-19T07:31:43Z")

</div>

The problem here is that `sys.input` is a connector, but `sys.input.u` is a _variable_. You can only form connections between connectors. If you want to say that a particular variable is equal to something else, you instead use a normal equation, i.e.,

```julia
sys.input.u[3] ~ sine.output.u

```

* * *

This actually gave me an idea of a much nicer interface, give me a few minuts and I’ll see if I can improve the experience here

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 19, 2024, 8:22am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/7 "2024-03-19T08:22:33Z")

</div>

> <https://github.com/JuliaControl/ControlSystemsMTK.jl/pull/65>
>
> This PR adds connectors with names specified in \`u\_names\` and \`y\_names\` when an …ODESystem is created from an LTI system.
> 
> \`x\_names\` is unfortunately still ignored, the reason is that it turned out to be hard to get \`build\_quadratic\_cost\_matrix\` to do the right thing in all situations when there were additional variables that could be chosen as state.

`x_names` are still ignored, but names for inputs and outputs now have connectors, so you should be able to do

```julia
connect(sys.input3name, sine.output)

```

for some `input3name` specified in `u_names`

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 20, 2024, 3:10pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/8 "2024-03-20T15:10:35Z")

</div>

Thanks for the quick work ! It works perfectly. A quick comment, I’m working with relatively large matrices that overflow the REPL upon `show`ing the state space model. I was think of using ideas from [BlockArrays.jl/src/show.jl at master · JuliaArrays/BlockArrays.jl · GitHub](https://github.com/JuliaArrays/BlockArrays.jl/blob/master/src/show.jl), do you mind if I propose a PR about that ?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 20, 2024, 7:11pm UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/9 "2024-03-20T19:11:51Z")

</div>

> [@BambOoxX](#):
>
> I was think of using ideas from [BlockArrays.jl](https://github.com/JuliaArrays/BlockArrays.jl/blob/master/src/show.jl)

How do those array display? Proposing a PR is of course welcome, we even have a brand new [CONTRIBUTING.md](https://github.com/JuliaControl/ControlSystems.jl/blob/master/CONTRIBUTING.md) file 🙂

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 21, 2024, 8:16am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/10 "2024-03-21T08:16:06Z")

</div>

From [BlockArrays main page](https://github.com/JuliaArrays/BlockArrays.jl/tree/master), you can see this kind of printing.

```julia
2×2-blocked 3×5 BlockMatrix{Float64}:
 -1.17797 0.359738 │ 0.87676 -2.06495 1.74256
  1.54787 1.64133 │ -0.0416484 -2.00241 -0.522441
 ───────────────────────┼──────────────────────────────────
  0.430093 -0.0263753 │ -1.31275 0.278447 -0.139579

```

I figured it would be nice to have something similar to preserve the

```julia
dx = A x + B u
 y = C x + D u

```

shape of state space models

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 21, 2024, 9:45am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/11 "2024-03-21T09:45:02Z")

</div>

How will that help with matrices that are too large to fit in one repl screen? Won’t it actually make the problem worse?

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 21, 2024, 9:57am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/12 "2024-03-21T09:57:40Z")

</div>

Well, I was thinking of using this to keep the structure clean, and using the `IOContext` so that there is no overflow, but a cropping of the internals of the large matrices instead

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 21, 2024, 10:27am UTC](https://discourse.julialang.org/t/naming-of-states-inputs-and-outputs-in-controlsystemsmtk/111783/13 "2024-03-21T10:27:57Z")

</div>

> [@BambOoxX](#):
>
> but a cropping of the internals of the large matrices instead

I have always questioned the utility of seeing some but not all the values? I generally find magic like this a bit annoying. With the current behavior, I can resize the terminal to have the matrices display nicer if the overflow is slight. There is also no information hidden from me, which there would be if the show method decides to crop a bit in the middle. If the information being printed is too much for your liking, maybe it’s better to avoid displaying very large systems in the REPL in the first place?

If the show method can be implemented such that the block-arrays style is used when it fits _completely without cropping_, but the current display style is used otherwise, perhaps that could be an okay compromise? I don’t really want to introduce any cropping, unless perhaps the matrices are _very large_.
