In many simulations, you will want to use if..then logic in your model to specify how a particular parameter changes as a function of some state of the system (e.g., time).
For example, you might want to use if…then logic to define how a particular variable changes with time: if(ETime < 10 yr, 5, 6). Simple relationships such as these can readily be entered into an Expression element. In some situations, however, you will need to define complex, nested if..then logic to represent your system. Consider, for example, the following complex nested if..then statement:
if A <10
then X = 5
else if A < 20
then X = 6
else if A < 40 and B > 2
then X = 7
else X=8
This would be represented within an Expression element using GoldSim's built-in if..then function and relational operators as follows:
Needless to say, a nested if…then statement such as this is cumbersome to enter (and difficult to interpret once it has been entered). The Selector element was specifically designed to facilitate the representation of such logic
The properties dialog for a Selector element looks like this:
Selector elements have a single output, which can be a value or a condition, and can be specified as a scalar, a vector or a matrix. You can specify these attributes by pressing the Type… button. By default, a new Selector element is a scalar, dimensionless value. You can also use Selector elements to operate on and/or create vectors and matrices.
Each switch (row) in the Selector dialog represents a single if…then statement and has two parts: the condition (the first column), and the result if the condition is true (the second column). The if statements are evaluated in order, and the Selector takes on the value corresponding to the first true statement that is encountered. All Selectors have a final else statement, which represents the result if all the conditions are false.
A new Selector has a single switch. A Selector with a single switch has the same functionality as a simple if..then statement: If(A then B else C).
You can add switches using the Add Switch button. New switches are added below the switch which is currently selected. If the final else is selected, however, the switch will be inserted above it. If the first switch is selected, you will be prompted for whether you want to insert the new switch above or below the selected switch.
To delete a switch, select it (it will turn yellow) and press Delete Switch. Note that the last result (the else) cannot be deleted.
You can use the Move Up and Move Down buttons to change the order of the switches (and the order is important!).
The example discussed above would be represented in a Selector as follows:
This is clearly easier to enter and read than using nested if..then functions.
Note: The dialog for a Selector is wider than other elements to allow long expressions to be easily displayed in the two columns. Note, however, that if an expression does not completely fit into one of the fields, like other input fields in GoldSim, the field will expand and the expression will wrap when you click into it. In addition, the Selector dialog is resizable. This allows you to stretch the dialog so as to see all of the expressions that you may have entered into the dialog at once.
Both the condition column and the result column can be entered as links or expressions. The first column (the condition) must either be:
• a link which is a scalar condition;
• a conditional expression; or
• the words "True" or "False".
Note: If your conditions are complex, you might want to create them using GoldSim’s logical elements.
The entries in the result column must have the same data type, order and dimensions as the element's output.
Note: Although the output of a Selector can be an array, the If column (the condition) must be a scalar. This means that you cannot do an item-by-item calculation on an array using a Selector. If you need to carry out an item-by-item if, then calculation on an array, you can do this by using an If( , , ) function in an Expression. If the first argument of the If function is an array, then the remaining two arguments must also be arrays (with the same Array Label Set), and GoldSim carries out an item by item calculation to construct the output array.
As shown below, the browser view of a Selector element shows a single output, and the following inputs: an Else (the result if all conditions are false), and two inputs for every switch (If_n and Then_n, where n is the switch number):
Note: Element inputs and outputs are only shown in the browser if you choose to Show Element Subitems (accessed via the browser context menu by right-clicking in the browser).
A simple example model which uses a Selector element (Selector.gsm) can be found in the General Examples folder in your GoldSim directory (accessed by selecting File | Open Example... from the main menu).
Learn more about: