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

Output array of external function is not translated properly

Output array of external function is not translated properly

Hi everybody,

I'm quite new to the modelica language and the OM environment, I'm having troubles with a call to an external function written in C.
The function is quite simple and returns a container class :

struct Container{
double *value;
};


Containter externalfunc(double value){
Container p;
p.value = new double[10];
for(int i=0;i<10;++i){
p.value[i] = i*value;}
return p;
}

When I use this function in OM I have the following call from the function class :
function extern1
  input Real a;
  output Container b;
external "C"   b = externalfunc(a);
end extern1;

where Container is a record that reads
record Container
Real value[10];
end Container;

The compiler gives the following error:

error: '_b' undeclared (first use in this function)

This error seems to be concerned with the translation in C by the OMC compiler.
if I look into the corresponding c file created by omc I have this line

  struct Container _b_ext;
  /* functionBodyExternalFunction: preExp */
  /* functionBodyExternalFunction: outputAlloc */
  alloc_real_array(&_b.value, 1, 10);

where clearly the difference is on the name of the variable to be allocated _b instead of _b_ext

I'm using the latest version 1.9.0_beta4 under linux ( ubuntu 12.10)
Do you have any suggestion? Where can I see the code that generates this wrong line?

Thank you
Chiara
chiara.riccobene@moxoff.com

Re: Output array of external function is not translated properly

Note that the only error of OpenModelica is that it did not tell you your code was illegal.

See Modelica Spec 3.3, section 12.9.1.3 Records:
"Arrays cannot be mapped"

Re: Output array of external function is not translated properly

HI,

I'm sorry, as I mentioned before I'm new to Modelica... i think anyway that there is indeed an error of translation, because if I change _b into _b_ext and I compile manually with gcc the code compiles correctly. But I didn't know that was forbidden to use arrays in records, so my apologies!

My question is : can I have an output array from an external function? I had a look at the documentation you told me but if I couldn't make it, could you suggest me a thread or an example that does this?

Thank you again

Re: Output array of external function is not translated properly

Yes, you may pass arrays, like Section 12.9.1.2 Arrays says.

As an example, see:
https://openmodelica.org/svn/OpenModeli … /Matrix.mo
It uses the default mapping:
https://openmodelica.org/svn/OpenModeli … ons/Func.h
(inArr,inArrSizeDim1,inArrSizeDim2,outArr,outArrSizeDim1,outArrSizeDim2)

If you pass an output array without using the default mapping, note that you are recommended to pass the sizes of all dimensions explicitly (and that in C, the output array must be passed as an input to the function). No allocation may be done by the user program; OpenModelica must do this for you.

There are 0 guests and 0 other users also viewing this topic
You are here: