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 arrays for external functions

Output arrays for external functions

Hi,

I have made a routine that should retrieve some information from a underlying thermodynamic calculation, but I can't get it to work.  I get an error in function 'bound_parameters' when I compile the model.  The below code shows the code that is causing the problem:

function get_results
  extends Modelica.Icons.Function;
  input ModelHolder m;
  input String property; 
  input Integer n;
  output Real res[n];
  external "C" getresults(m, property, res) annotation(Library="srk_modelica", Include="#include \"srk_modelica.h\"")
end get_results;

If i set a static size to res, e.g. res[2] the code above compiles fine.

Is this a feature that is not supported? If not, is there a way to get around this by defining the function differently?

Thanks in advance.

Re: Output arrays for external functions

What version of OpenModelica are you using? It seems to be compiling in the trunk version. The only issue I could find with it is that the result may be overwritten if you do not assign it to a real array right after the function call returns, i.e.:

Code:

function get_results 

  input Real m;
  input String property; 
  input Integer n;
  output Real res[n];
  external "C" getresults(m, property, res) annotation(Include="
void getresults(double m,const char *str,double *res) {
  res[0]=1.5;res[1]=2.5;
}");
end get_results;

function f
  input Real r;
  output Real res[2] := (get_results(r,"abc",2).+get_results(2*r,"abc",2)); /* This adds the first result to itself, only */
end f;

model M
  Real res[2] = f(1.5);
end M;

Re: Output arrays for external functions

Hi,

Your example works fine on the compiler version I have (1.8.0 (r10207)), and thanks to it I was able to figure out what was wrong in my "non-simplified" code.

Thanks for your excellent support!

Re: Output arrays for external functions

Hi,

I discovered something odd using the external function which is returning a double vector. When I use it in a class like this:

class MyModel
   ThermoObject p = ThermoObject()
   Real r[17] = getvector_data_from_external_function(p, "property",17);
end MyModel;

The function getvector_data_from_external_function in the above code is called 17 times. Do I do something wrong in the code?


KR,

Re: Output arrays for external functions

Hi,

This is the way we populate the r[1], r[2], .... r[17] right now by calling the function and indexing the result.
We should improve it to have just one call, but right now this is the way it goes.
We have a bug for it, so it will be fixed in some future:
https://openmodelica.org:8443/cb/issue/ … ation=true

Cheers,
Adrian Pop/

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