Condicional Statements

In OptFerm if or if...else statements can be used, in order to provide decision making strutures allowing the evaluation of one or more conditions. Moreover, nested if and/or if...else statements can be also implemented, which means that it is possible to use one or several if and/or if...else statements inside another if or if...else statements.

However, there are some aspects that must be taken in consideration:

If more than one statement is declared after an "if" or "else" statement is mandatory to put these statments between curly brackets. Otherwise this is not necessary.

Example, with mandatory curly brackets:

if(condition){
        statement 1;
        statement 2;
        statement 3;
 }
else{
     statement 4;
     statement 5;
 }

Example, without curly brackets:

if(condition)
     statement 1
else
    statement 2

Conditional Operators that can be used are:

  1. equal to ==
  2. not equal to !=
  3. greater than >
  4. greater than or equal to >=
  5. less than <
  6. less than or equal to <=

Nested if and/or if...else statements can be implemented as presented below:

Example, of a nested if statement:

if(condition 1)
    if(condition 2)
         statement 1;




if(condition 1){

     statement 1;
     statement 2;

     if(condition 2)
          statement 3;

     if(condition 3){
        statement 4;
        statement 5;
     }
}

Example, of a nested if...else statement:

if(condition 1){

     statement 1;

     if(condition 2){
          statement 2;
          statement 3;
     }
     else (condition 3){
        statement 4;
        statement 5;
     }
}




if(condition 1){
     if(condition 2){
          statement 2;
          statement 3;
     }
}
else{
    if(condition 3){
        statement 4;
        statement 5;
     }

     else (condition 4){
        statement 6;

        if(condition 5)
            statement 7;
     }
}

For Loop Statement

The for loop statement can be used essentially in the definition of the objective functions.The utilization of this statement is limited to mathematical operations in which the values of the State Variables or main Kinetic Variables are needed to be used or known at certain time or iteration process.

Thus, there are some considerations that must be taken into account before to know how to implement a for loop.

Considerations

The size of the vector for the State Variables and Kinetics Variables can vary during the OptFerm operations, depending mainly of the step-size (dt) that is used in the simulation process.

Thus, the size of these vectors can be accessed, using the following annotations:

[endstatevars] --> to know the size of State Variables vector.
[endrates] --> to know the size of the Kinetic Variables vector.

A value of a State Variable or a main Kinetic Variable can be accessed in a specific position of their vectors, since that position stays within the size range of that vector.

To access to a specific position, just apply the following annotation:

Name of Variable [position number]

For example, suppose that a State Variable S was defined and there is an interest to access to their value (that is calculated internally by OptFerm) in position 100. This can be performed through:

S[100]

In a for loop statement is mandatory to define a variable of increment, that will take the different position values during the loop iteration.

Thus, based on that was described before, to access to the different positions of a State Variable or Kinetic Variable throughout loop iteration process, use the following annotation:

Name of Variable [Increment Variable]

Suppose that was defined a increment variable i in the for loop statement, and we want to sum the first 100 values of one State Variable S.

Example of the loop:

 for i from 0 to 100 {
    dosum = $sum(S[i]);

 }

where, the dosum variable will record the obtained result; $sum is a special function of OptFerm that performs the sum of variables during the iteration process; S[i] represents the vector of variable S that can take a position i

For loop formulation

The for loop can be defined using two distinct formulation types:

Formulation 1:

for increment_variable from initial_value to final_value {

        statement that uses increment_variable
}

Formulation 2:

for increment_variable : initial_value -> final_value {

        statement that uses increment_variable
}

Examples:

for i:0->[endstatevars] {

   res=$sum(S[i]+A[i]);
}


for i from 0 to [endrates] {

   if(mu[i]>20)
      res=mu[i];
   else
      res=2;  
}


n=1000;
for a from 0 to n {
   res=X[a]*mu;
}


for bu from 0 to 1000 {
   res=X[bu]*mu;
}