Help - Search - Members - Calendar
Full Version: External (C-)Function return array
Modelica Forum > Modelica > Dymola
ElKunzo
Hi everybody,

I have some external functions which I want to use in Dymola.
For the most of the functions everything works fine, but if I want to return an array, I get the following error:
QUOTE

Error: return variable in external function call for function getN must be a scalar-valued temporary or output variable.
But got 'n' which is not a scalar variable.
Errors detected in functions.


Here is the code snippet of the C-Function:

CODE

void* getN(......)
{

    double *n = malloc(3 * sizeof(double));
    n[0] = ....
    n[1] = ....    
    n[2] = ....

    return (void *) n;
}


And here is the call in Dymola:

CODE

function getN
  
annotation(Library="my_lib");
  input .....;
  input .....;
  output Real n[3];
  external "C" n = getN(..........);
end getN;


I already have a workaround in which I have a function for every part of the array (e.g. getN_x, getN_y, getN_z) but the performance in this solution is very bad.

Does anyone know how to return arrays from external functions?
Thanks in advance,
el Kunzo
smoss
elKunzo,

Do you have a copy of Peter Fritzson's book, "Principles of object-oriented modelling and simulation with Modelica 2.1"? He has written a few pages on external functions, e.g. pp311-321. If you don't have a copy of Fritzson's book, it might be useful to borrow a copy for your array problem. A few quotes from Fritzson's book which may be relevant are:

QUOTE
There are two ways for an external function to return results:
1. Through output formal parameters
2. As a function return value.


QUOTE
An array can also be returned as a function return value. In that case only the address of the array or record is returned since storage for the returned data must be preallocated by the caller. Dimenson sizes are not returned since the dimensions of the preallocated return array are set by the caller.


QUOTE
When we pass array arguments or return arrays, these arrays are passed or returned by reference i.e. a pointer to each argument is passed or returned.


An example Modelica code he gives is,

CODE
function joinThreeVectors     "Modelica external function version"
  input Real v1[:], v2[:], v3[:];
  output Real vres[size(v1,1)+size(v2,1)+size(v3,1)];
  external "C" join3vec(v1,v2,v3,vres,size(v1,1),size(v2,1),size(v3,1));
                 annotation(arrayLayout = "rowMajor");
end joinThreeVectors;


where the C prototype is

CODE
void join3vec(double*, double*, double*,double*,size_t,size_t,size_t);
ElKunzo
Hello smoss,

I already ordered the book, but it has not been delivered yet.

Thank you very much for your help, through your good explanation I understand the use of external functions much better, and everything is working out fine.

Thanks again,
el Kunzo
DrJonny
Hello,

does anybody know how to return a number of double values from an
external c function to dymola. I already tried the example presented
in Peter Fritzson's book but it didn't work.

Could anybody post a simple c-function and the corresponding dymola
code where more than one real or integer values are returned to dymola.

Best regards
DrJonny
liuliu
QUOTE(DrJonny @ Sep 15 2008, 01:13 PM) *

Hello,

does anybody know how to return a number of double values from an
external c function to dymola. I already tried the example presented
in Peter Fritzson's book but it didn't work.

Could anybody post a simple c-function and the corresponding dymola
code where more than one real or integer values are returned to dymola.

Best regards
DrJonny


Hello,

define your external c function in this manner :
CODE

void function(int intput1, int input2, int * output1, int * output2)
{...
....
*output1=1;
*output2=2;

}



then in modelica

CODE


input Integer input1;
input Integer input2;
output Integer output1;
output Integer output2;

external "C" function(input1,input2, output1, output2);



in this case, because you have already definded the variable in Modelica, so you only need to pass the pointer of your variables to the c program. Hope it works.

Regards
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.