1
votes

If an array is defined in Dymola and any one array element is assigned a value in an algorithm section, it appears that Dymola implicitly adds equations for all of the unassigned array elements. Consider the example below:

model AlgorithmAssignment

  // Declare an array with two elements
  Real myArray[2];

algorithm 
  // Assign a value to array element 1 in algorithm section
  // This yields no error and implicitly assigns myArray[2] := 0;
  myArray[1] := 10;

end AlgorithmAssignment;

This issue came to my attention when trying to run a model similar to the code below:

model EquationAndAlgorithmAssignment

  // Declare an array with two elements
  Real myArray[2];

equation 
  // Define element 1 in the equation section
  myArray[1] = 10;

algorithm 
  // Define element 2 in the algorithm section
  myArray[2] := 1;

  // This model is over-defined because the equation myArray[2] := 1 in the
  // algorithm section implicitly adds an equation myArray[1] := 0
  // This behavior is unexpected.

end EquationAndAlgorithmAssignment;

As noted in the code comments, this behavior seems strange to me. One array element is assigned in the equation section, the other is assigned in the algorithm section. It is unintuitive to me that Dymola implicitly adds an equation myArray[1] := 0 simply because the algorithm section contains the equation myArray[2] := 1.

I would appreciate if anyone can provide guidance or comments on why this occurs, and if/why this is design behavior.

1

1 Answers

3
votes

Modelica Language Specification 3.4 (section 11.1.2) says:

  • A non-discrete variable is initialized with its start value (i.e. the value of the start-attribute).
  • A discrete variable v is initialized with pre(v).
  • If at least one element of an array appears on the left hand side of the assignment operator, then the complete array is initialized in this algorithm section.

[...]

An algorithm section is treated as an atomic vector-equation, which is sorted together with all other equations.

So all your variables will be initialized with the start-value at the start of each execution of the algorithm block. There is no memory, pre-variables, etc so if you assign to variables in an if-statement this value will be lost upon the next execution of the section.

Part of the reason for this is that it is not always known which indexes are assigned to and which are. So they are considered to always be assigned even if all variables are unconditionally assigned with constant indexes.