Understanding the Outputs of a Script Element

By default, a Script element has a single primary output. The attributes of this output are defined in the main Script dialog.

 It is also possible to define additional outputs for the element (with attributes and dimensions) when defining local variables within a script.

When you create a local variable, you can specify that you want it to be an output for the element by checking the Expose as Output checkbox (which by default is cleared):

For example, if a Script element (named Script1) had two local variables (X and Y), and only X was exposed as an output, then outputs of the Script element would look like this:

The primary output (referred to as Result in the script) would be referenced outside of the Script element as Script1.  The secondary output X would be referenced outside of the Script element as Script1.X.  The other local script variable (Y) could not be referenced outside of the Script element.

As is the case for other GoldSim elements, you can check the checkboxes at the bottom of a Script element dialog to save “Final Values” and/or “Time Histories” of all the exposed outputs (including the primary output).

There is an important difference in the behavior of the primary output and any other exposed outputs.  The primary output of a Script element is a state variable. A state variable provides inertia or “memory” to a system because its value is computed based on the historical value of the element’s inputs (as opposed to only being a function of the current value of the element’s inputs). All state variables have, by definition, an initial value. This allows the output to be computed when there are no historical inputs available (e.g., at the start of simulation).

This has an important implication for how the variables can be used in a script. In particular, the value of the primary output is “remembered” between updates of the element. As a result, in a script you could create a Variable Assignment such as “Result = ~Result + 1” as the very first statement. The expression would use the previous value of the Result when it was evaluated.  This is not possible for any of the other script variables, since they require a Variable Definition statement which initializes the variable every time the Script element is updated.

   Note: User-defined local script variables cannot reference themselves in a Variable Definition statement (e.g., X = ~X + 1 would be invalid, as there is no existing initial value of X to reference in this case). 

If you would like additional outputs of the Script element to be able to “remember” their previous update (and hence behave like state variables with initial conditions), you can do so by referencing the Script output in a Previous Value element outside of the Script element (and then referencing the Previous Value inside the script).

Related Topics…

Learn more about: