How to use unspecified array dimensions in blocks?

135 Views Asked by At

Using unspecified array dimensions (:) is an essential feature to design flexible components for reuse. I am well aware that the actual dimension has to be fixed when the model is compiled. To my knowledge binding a variable with unspecified array dimensions to one that has clearly defined dimensions should suffice.

So I am a bit confused why the following model Test will not validate in either OpenModelica or the Wolfram System Modeler:

package VectorFunctions

  model Test
      VectorSum converter "Component taking the sum of a vector input";
      InformationSource source "Vector input";
    equation
      connect( source.y, converter.u );
  end Test;

  block VectorSum "Take the sum of an input with unspecified dimension"
      Modelica.Blocks.Interfaces.RealInput u[:];
      Modelica.Blocks.Interfaces.RealOutput y;
    equation
      y = sum(u);
  end VectorSum;

  block InformationSource "Provide some vector output"
      Modelica.Blocks.Interfaces.RealOutput y[3];
    equation
      y = ones( 3 );
  end InformationSource;

end VectorFunctions;

How can something like this be done then?

2

There are 2 best solutions below

0
gwr On BEST ANSWER

I have been given (inofficial) feedback on Wolfram Community by someone from Wolfram MathCore (e.g. the developers of the System Modeler):

Hi, I agree with your interpretation, I think we should support it. I have filed a bug to keep track of this issue internally, unfortunately I do not see any work around. We will come back to you when we have fixed this problem.

So, hopefully flexbile array sizes will be supported for blocks as they are for functions.

1
marco On

My guess would be that the Modelica Spec does not specify, that vector sizes can be automatically detected from connections, so the tools don't support that.

I think you have to set the vector size somehow by yourself, e.g. with a parameter which is set in your Test model as follows:

  model Test
      VectorSum converter(nu=size(source.y, 1)) "Pass in the vector size";
      InformationSource source "Vector input";
  equation 
      connect(source.y, converter.u);
  end Test;

  block VectorSum "Take the sum of an input with unspecified dimension"
      Modelica.Blocks.Interfaces.RealInput u[nu];
      parameter Integer nu(min=0)=0;
      output Real y;
  equation 
      y = sum(u);
  end VectorSum;

Note that Dymola complains in your example code that connect statements can only be applied to connectors. Therefore I changed input Real to Modelica.Blocks.Interfaces.RealInput(and similar in InformationSource)