WOLFRAM SYSTEM MODELER

'for'

for

Wolfram Language

In[1]:=
SystemModel["ModelicaReference.'for'"]
Out[1]:=

Information

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Repeat equations or statements a specific number of times

Examples

for clauses are mostly used in algorithm sections, such as

    parameter Integer np=10;
    Real p[np], x, y;
  algorithm
     y := p[1];
     for i in 2:np loop   // i shall not be declared
        y := y*x + p[i];
     end for;

Other forms of the for condition:

    for i in 1:10 loop            // i takes the values 1,2,3,...,10
    for r in 1.0 : 1.5 : 5.5 loop // r takes the values 1.0, 2.5, 4.0, 5.5
    for i in {1,3,6,7} loop       // i takes the values 1, 3, 6, 7

In equation sections, for clauses are expanded at translation time in order that symbolic transformations can be applied. Typically, a for clause in an equation section is used on component arrays, e.g., to connect elements of an array of components together:

    parameter Integer nR=10 "Number of resistances";
    Modelica.Electrical.Analog.Basic.Resistor R[nR];
  equation
     for i in 1:nR-1 loop
        connect(R[i].p R[i+1].n);   // 9 connect equations
     end for;

Syntax

In equation sections:

  for for_indices loop
     { equation ";" }
  end for;
  for_indices : for_index {"," for_index}
  for_index   : IDENT [ in expression ]

In algorithm sections:

  for for_indices loop
     { algorithm ";" }
  end for;
  for_indices : for_index {"," for_index}
  for_index   : IDENT [ in expression ]

Description

A clause

    for IDENT in expression loop

is one example of a for clause.

The expression of a for clause shall be a vector expression. It is evaluated once for each for clause, and is evaluated in the scope immediately enclosing the for clause. In an equation section, the expression of a for clause shall be a parameter expression (in order that the for clause can be expanded into equations during translation). The loop-variable is in scope inside the loop-construct and shall not be assigned to.

[Example:

The loop-variable may hide other variables as in the following example. Using another name for the loop-variable is, however, strongly recommended.

    constant Integer j=4;
    Real x[j];
  equation
    for j in 1:j loop // The loop-variable j takes the values 1,2,3,4
      x[j]=j; // Uses the loop-variable j
    end for;

]

Several iterators

The notation with several iterators is a shorthand notation for nested for-clauses (or reduction-expressions). For for-clauses it can be expanded into the usual form by replacing each "," by 'loop for' and adding extra 'end for'. For reduction-expressions it can be expanded into the usual form by replacing each ',' by ') for' and prepending the reduction-expression with 'function-name('.

[Example:

    Real x[4,3];
  equation
    for j, i in 1:2 loop
      // The loop-variable j takes the values 1,2,3,4 (due to use)
      // The loop-variable i takes the values 1,2 (given range)
      x[j,i]=j+i;
    end for;

]