Help - Search - Members - Calendar
Full Version: Functions with variable size inputs
Modelica Forum > Modelica > Modelica
Lars
Consider the following test function, and test usage:
CODE

function testfun
  parameter Integer n = size(a,1);
  input Real[n] a;
  output Real[n] b;
algorithm
  b := 2*a;
end testfun;

model testmod
  Real[2] a;
  Real[2] b;
equation
  b = {1,2};
  a = testfun(b);
end testmod;


The function can be checked (in Dymola) without errors, but when I check the model, I get

QUOTE
Error: Unknown variable n in size expression: n
for function Test.testfun
Errors or failure to expand the equation:
equation
a = Test.testfun(b);
Found in class Test.testmod, declaration window at line 6.


The usage of the parameter n is inspired from the example at page 303 (bottom) in Fritzon's book.

For this simple test example, I know that the code below would work. However, (I think) I really need to know the input sizes in defining outputs in the function where I have this problem. Compare for example if a in the model above is defines as Real[:] (which is still not the same error message I get, but similar).

CODE

function testfun
  input Real[:] a;
  output Real[:] b;
algorithm
  b := 2*a;
end testfun;
wagner
Hello Lars,

the following code for your test function runs on Dymola 6.0b. Maybe it solves the problem you have.

CODE

function testfun
  input Real[:] a;
  output Real[size(a,1)] b;
  
algorithm
  b := 2*a;
end testfun;


The workaround with parameter does not seem to work on the current versions of dymola.

Regards

Florian
Lars
Many thanks, this does solve my problem.

It seems there is an error in either Dymola or Fritzson's book.

Lars
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.