2

For example, I have a C external function that returns a structure. Is it possible to return the structure to Modelica?

struct point{
   double x;
   double y;
}

struct point return_Struct(double a, double b){
    struct point pt;
    pt.x = a;
    pt.y = b;
    return pt;
};

In Modelica,

function structReturn
  input Real x;
  input Real y;
  output ??????;
external"C" ????? = return_Struct(x,y)
  annotation (Include="#include <cStructReturn.c>");
end structReturn;
Hang Yu
  • 476
  • 2
  • 14

1 Answers1

3

Use a record and pass it by reference. See section 12.9.1.3 Records in the Modelica specification. Note that the record may have a different name in the Modelica tool from what you expect, so pass it through void* and cast it explicitly. Use a library not an included C-file to hide the interface or the code might not compile.

void return_Struct(double a, double b, void* result){
    struct point *pt = result;
    pt->x = a;
    pt->y = b;
};
record R
  Real x,y;
end R;

function structReturn
  input Real x;
  input Real y;
  output R r;
external"C" return_Struct(x,y,r)
  annotation (Library="cstructreturn");
end structReturn;

But I recommend passing 2 reals as output of the external function and constructing the record in a Modelica wrapper function instead.

function multipleReturn
  input Real x;
  input Real y;
  output Real ox;
  output Real oy;
external"C" return_notStruct(x,y,ox,oy)
  annotation (Library="cstructreturn");
end multipleReturn;
sjoelund.se
  • 3,468
  • 12
  • 15