# Routing

If you want to combine or split signals, or define more complicated systems that bridge multiple sampling rates or diagrams, then you can use the following routing tools:

## Switch

The switch element allows you to select a specific signal, or dynamically switch between different signals from Lua based on the Selection property. If only one input is specified, then it will select the output from among a vector of input signals. If there is more than one input, then it selects the input specified as the Selection index.

For example, to create a system that will output a different constant depending on the selection, you could create the constants inside the switch as follows

```<switch name="myswitch">
<constant value="1.5"/>
<constant value="2.72"/>
<constant value="3.14"/>
</switch>```

whereas if you want to switch between three different constants that are outside of the switch, then you could create a system like

```<stack>
<constant value="1.5"/>
<constant value="2.72"/>
<constant value="3.14"/>
<stack>
<switch name="myswitch" inputs="3"/>```

or, you can even create a vector of the various inputs and select between the signals in the vector.

```<mux>
<constant value="1.5"/>
<constant value="2.72"/>
<constant value="3.14"/>
</mux>
<switch name="myswitch"/>```

In any case, you can define the selection property when you create the switch to define the element that you want to output, and then update the selection as needed from Lua using the following command:

`mysim.mysolver.myswitch.Selection = 2;`

The switch element can also be used to wire a single input into multiple output ports.

`<switch select="1 1"/>`

or to reroute the elements of a single input vector to different output ports.

`<switch select="1 3 2"/>`

## Mux

The mux block combines multiple signals together to form a single vector signal. The input signals can be specified inside of the block or beforehand inside of a stack. The following examples both perform the task of combining two sine waves and displaying the data on one plot.

```<stack>
<sine amplitude="1" />
<sine amplitude="3" />
</stack>
<mux inputs="2" />
<probe ref="mySection.myTimePlot" />```
```<mux>
<sine amplitude="1" />
<sine amplitude="3" />
</mux>
<probe ref="mySection.myTimePlot" />```

## Demux

The demux block is used to split a vector into its constituent components. The output signals can be specified inside of the block or afterwards inside of a stack. The following examples both perform the task of splitting two sine waves and displaying the data on different plots.

```<sine amplitude="3 5" />
<demux outputs="2" />
<stack>
<probe ref="mySection.firstTimePlot" />
<probe ref="mySection.secondTimePlot" />
</stack>```
```<sine amplitude="3 5" />
<demux>
<probe ref="mySection.firstTimePlot" />
<probe ref="mySection.secondTimePlot" />
</demux>```

Demultiplexing can be used to separate input data into specific outputs of varying widths. By setting the output attribute to a list of values, you can specify the widths of each output port. In the following example the input signal is a vector of width seven, and the demultiplexer separates the signal into three output ports.

```<constant value="1 2 3 4 5 6 7" />
<demux outputs="2 1 4"/>```

[1 2] on output port one.
 on output port two.
[4 5 6 7] on output port three.

This method of routing signals is invaluable when receiving multiple sets of data over a stream.

## Selector

The selector block selects between its input signals or ports based on its first input. If there are two input ports, the selector input determines which elements from the second input will be routed to the output. If there are more than two input ports, the selector input determines which input ports are routed to the output(s).

```<stack>
<constant value="2" />
<constant value="4 7" />
</stack>
<selector outputs="1" />```

Outputs the value 7 to the output port.

```<stack>
<constant value="3 5 1" />
<constant value="4 7 12 3 5" />
</stack>
<selector outputs="1" />```

Outputs the vector [12 5 4] to the output port.

``` <stack>
<constant value="2" />
<constant value="4" />
<constant value="8" />
<constant value="1" />
</stack>
<selector inputs="4" outputs="1" />```

Outputs the value 8 to the output port.

```<stack>
<constant value="2 3 1" />
<constant value="4" />
<constant value="8" />
<constant value="1" />
</stack>
<selector inputs="4" outputs="3" />```

Outputs value 8 to output port one, value 1 to output port two, and value 4 to output port three.

## Using

The using element allows its subsystem(s), to be treated as if they have the input and output numbers specified in the using attributes. This essentially forces the contained systems to map their input and output ports to the values specified instead of port 1. For example, a controller block defined in a parallel element contained in the using block could be forced to apply the control action to the third and forth elements of an input vector, but output only the second port.

## systemRef

The systemRef element can be used to inject a system from a separate part of the simulation into the current context as if it were defined locally. For example, you could reuse a particular system by simply referencing its name and local as solver.system to reuse it as follows:
{"ft_lang_label":"__label__en","ft_lang_prob":0.7420226,"math_prob":0.9434691,"size":5175,"snap":"2019-35-2019-39","text_gpt3_token_len":1240,"char_repetition_ratio":0.18835816,"word_repetition_ratio":0.11002445,"special_character_ratio":0.2589372,"punctuation_ratio":0.079229124,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9911755,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-25T23:57:33Z\",\"WARC-Record-ID\":\"<urn:uuid:e21fdb73-7fd6-419d-8f73-59ba0681e83f>\",\"Content-Length\":\"72650\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ee080db7-33ac-4b48-857b-d4858b6927ed>\",\"WARC-Concurrent-To\":\"<urn:uuid:401c5967-1f52-4098-975f-3b7a582f5ff3>\",\"WARC-IP-Address\":\"198.185.159.144\",\"WARC-Target-URI\":\"https://qdexapps.com/routing\",\"WARC-Payload-Digest\":\"sha1:HF63QAOPQYYHE5PBPPZ4YGOQJ54U3VLE\",\"WARC-Block-Digest\":\"sha1:2HMJIXRPY3ZWEQIODJV22UU3ENQMCVJB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027330907.46_warc_CC-MAIN-20190825215958-20190826001958-00242.warc.gz\"}"}