Script Example: Referencing a Previous Value Element in a Script

By default, a Script element has a single primary output For an element with multiple outputs, the output that has the same name as the element.. However, it is possible to define additional outputs for the element (with their own attributes and dimensions An output attribute for an element that defines the dimensionality (in terms of Length, Time and other fundamental dimensions) of the output.) when defining local variables within a script.

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 The output of an element in GoldSim whose 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). State variables have well-defined initial conditions. Feedback loops can only be created if they contain at least one 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 a 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.

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 element's output in a Previous Value element An element that outputs the value of its input from the previous model update. outside of the Script element (and then referencing the Previous Value inside the script.

This very simple example illustrates how the Script element can be used in conjunction with a Previous Value element. It can be found in a file (Script.gsm) that is located in the General Examples folder of your GoldSim directory (accessed by selecting File | Open Example... from the main menu).

The script simply updates the primary output (Result) and a secondary variable (Exposed) every time the Script element is updated (in this case, every timestep A discrete interval of time used in dynamic simulations.). In order for the defined variable (Exposed) to be "remembered" between timesteps, it is stored in a Previous Value element, and it is that element that is referenced by the script):

As can be seen, the script references the Previous Value element in a loop: