Controlling the Number of Loops in a Looping Container

Looping Containers are intended to be used to carry out an iterative calculation at each timestep.  The number of loops carried out is controlled by specifying a condition (i.e., loop while or until a specified condition is met).

You can choose between two loop types:

Loop WHILE condition is true.  In this case, the loop is carried out as follows:

1.  At the beginning of the calculation, the loop counter is set to 1.

2.  GoldSim tests the Condition.

3.  If the Condition is False, the looping calculation is assumed to be complete.

4.  If the Condition is True, the contents of the Container are updated and the loop counter is incremented.

5.  Go to Step 2.

This is illustrated schematically below:

Loop UNTIL condition is true.  In this case, the loop is carried out as follows:

1.  At the beginning of the calculation, the loop counter is set to 1.

2.  The contents of the Container are updated.

3.  GoldSim tests the Condition.

4.  If the Condition is True, the looping calculation is assumed to be complete.

5.  If the Condition is False, the loop counter is incremented.

6.  Go to Step 2.

This is illustrated schematically below:

   Note: “Loop Until” Containers always update at least once.  “Loop While” Containers may not update at all (if the loop condition is not met).

   Warning: Because the Condition for a "Loop While" Container must be evaluated before the contents of the Container are evaluated, this Condition can only reference the loop counter or elements inside the Container that have well-defined initial conditions (i.e., state variables)

The loop counter itself can be referenced by any element inside the Container (and within the Condition field) as “~LoopCount”.  For example, if the Condition was defined as follows:

then the Container would complete exactly 10 loops.

As indicated by the way it is referenced, the loop counter is an example of a locally available property.  As such, it only has meaning inside the looping Container, and cannot be referenced outside of the Container. 

If you access the Insert Link dialog inside a looping Container (by right-clicking inside an input field of an element within the Container or in the Condition field itself), the loop counter appears in a Available Properties folder (displaying all of the locally available properties that can be accessed from that location):

The Condition must reference at least one output from within the looping Container (the loop counter is considered to be within the Container).  If it does not, GoldSim will display an error message (since otherwise, the Condition could not change from loop to loop, and hence has no meaning).

   Warning: GoldSim will not exit a loop until the Condition has been met, and there is no upper limit on the number of loops that can be carried out.  As a result, you should be careful when defining your Condition to ensure that you do not define an infinite loop.  You can always do so by directly referencing the loop counter (e.g., adding something to the Condition such as “OR ~LoopCount > 1000”) If it seems that you are in an infinite loop, you should pause the simulation (using the Pause button in the Run Control toolbar) and evaluate the loop counter and the condition.  You can subsequently stop a simulation using the Abort button in the Run Control toolbar.

Related Topics…

Learn more about: