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.
[3] 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: