ModelicaSpecification icon indicating copy to clipboard operation
ModelicaSpecification copied to clipboard

Base Modelica algorithm outputs and component arrays

Open henrikt-ma opened this issue 6 months ago • 0 comments

Consider lowering the following to Base Modelica:

model ComponentArray

  model C
    Real x;
  algorithm
    x := 1.0;  
  end C;

  C[2] comps;

end ComponentArray;

Since Base Modelica does not have component arrays, there should be two algorithms. Depending on scalarization I can imagine some different variants, including

algorithm
  'comps[1].x' := 1.0;
algorithm
  'comps[2].x' := 1.0;

and

algorithm
  'comps'[1].'x' := 1.0;
algorithm
  'comps'[2].'x' := 1.0;

In order to work, all of these algorithms should be considered assigning just a single scalar output, so that a total equation rank of 2 is obtained.

Then consider the following Modelica model with a record array:

model RecordArray

  record R
    Real x;
  end R;

  R[2] comps;
algorithm
  comps[1].x := 1.0;

end RecordArray;

According to Modelica algorithm semantics, this is algorithm has the two outputs comps[1].x and comps[2].x.

If the Base Modelica algorithm would look like

algorithm
  'comps[1].x' := 1.0;

or

algorithm
  'comps'[1].'x' := 1.0;

this would imply that Base Modelica would need to have the same sort of algorithm initialization rule for array assignments as Modelica (modified to take name mangling into consideration), but then the Base Modelica algorithms for ComponentArray would actually have two outputs each instead of one (resulting in a total equation rank of 4 instead of 2).

I am thinking that we might be able to resolve this by having a difference in algorithm initialization between Modelica and Base Modelica, requiring that a Base Modelica algorithm makes all initialization explicit. When determining the outputs of an algorithm in Base Modelica, it would then be sufficient to only look for constant subscripts, and it would be an error for a Base Modelica algorithm to have a non-constant output subscript taking on a value which is not also present as a constant subscript. For example, this would then be an invalid Base Modelica model:

model 'BadAlgorithm'
  Real 'x'[2];
  Integer 'k';
initial equation
  'k' = 1;
equation
  when time > 0.5 then
    'k' = 2;
  end when;
algorithm
  'x'[1] = 1.0;
  'x'['k'] = 1.0; // Runtime error at time 0.5; 'x'[2] is not an algorithm output.
end 'BadAlgorithm';

Of course, avoiding the need for runtime checks would be welcome. Better ideas?

henrikt-ma avatar Jul 09 '25 15:07 henrikt-ma