Search code examples
modelica

Return multiple array outputs from function to parameter


I have a function that returns two arrays of unequal sizes (A,B). These are for determining initial conditions of a model so I would like for them each to be defined as a parameter array in the model. Is this possible and, if so, how?

Below is some pseudo code of the question:

Function

function myfunc
input Real[:] alpha;
input Real[:] beta;

output Real[size(alpha,1)] A;
output Real[size(beta,1)] B;
algorithm
//equations, etc.
end myfunc;

Model

model mymodel

parameter Real[2] alpha = {1,2};
parameter Real[3] beta = {3,4,5};

parameter Real (A_start,Bstart) = myfunc(alpha,beta)

Real[size(alpha ,1)] A(start=A_start);
Real[size(beta,1)] B(start=B_start);

equation

//equations, etc.

end mymodel;

I've tried several things that failed. One successful method used the initial equation section. However, it requires me, for example, to define A_start as a variable and to add der(A_start) = 0 in an equation section, and prevents me from providing a non-fixed guess value (i.e., A(start=A_start)) to the variables, which could cause problems.

Thanks for your help, even if its confirming that I'm out of luck.


Solution

  • The solution by Scott G works, but it is also possible to solve in other ways - that may or may not be better. One common idea is as follows:

      record R
        Real A[:],B[:];
      end R;
    
      function bar
        input Real[:] alpha;
       input Real[:] beta;
       output R r(redeclare Real A[size(alpha,1)],redeclare Real B[size(beta,1)]);
      algorithm 
        (r.A,r.B):=myfunc(alpha,beta);
      end bar;
    
    parameter R r=bar(alpha,beta);
    
    Real[size(alpha, 1)] A(start=r.A);
    Real[size(beta,1)] B(start=r.B);