Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

External C function with array

External C function with array

Hello,

I'm tring to use external C function with array.
I'm testing very simple code for passing and returing the array.
but  I got a error message when I simulated the .mo file.

Followings are my .mo , .c and .h files.
(attached files)

Code:

.mo file

model External_Array
/*This code is very simple code for testing external c function with array*/
  function FuncArray               
    input Real[:] a;               
    output Real[2] b;   
    external b=ExternalC_array(a,2) annotation(Library="functionC.o",Include = "#include \"ExternalC.h\"");               
  end FuncArray;
         
    Real[2] x={1,2};               
    Real[2] y;               
  equation               
    y = FuncArray(x);       
end External_Array;


.c file
/* ExternalC.c */
double ExternalC_array(double *x, int n)
{
    int i;
    for(i = 0; i < n; i++){
        x[i] = x[i] / 2;
    }
    return x, n;
}

.h file
/* ExternalC.h */
double ExternalC_array(const double*, int);

And following is the error message.

Code:

[D:/.../External_Array.mo:2:3-8:16:writable] Error: The lhs (result) of the external function declaration has array type (Real[2]), but this is not allowed in the specification. You need to pass it as an input to the function (preferably also with a size()-expression to avoid out-of-bounds errors in the external call).

[D:/.../External_Array.mo:17:5-17:21:writable] Error: Class FuncArray not found in scope External_Array (looking for a function or record).
Error: Error occurred while flattening model External_Array

I know that I'm write wrong argument in .c file.

external b=ExternalC_array(a,2) annotation(Library="functionC.o",Include = "#include \"ExternalC.h\"");

but I don't know hot to fix it.

ExternalC-with-Array.zip

Questions
(1)  Plase teach me how to fix the avobe code?
(2)  Please let me know if you know a good example which contains .mo , .c code.

Edited by: Ueda - Jun-01-17 17:26:49

Re: External C function with array

Add,
I read following references, but I couldn't understand...
Modelica Specification 3.4 Chp 12.9
Full Version: External (C-)Function return array
Full Version: External (C-)Function return array

Edited by: Ueda - Jun-01-17 12:32:00
There are 0 guests and 0 other users also viewing this topic
You are here: