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

Newbie, having trouble with external function/ trying to get a RNG

Newbie, having trouble with external function/ trying to get a RNG

Hi, I'm fairly new to Modelica and c (java and matlab guy, not a prolific programmer) and I'm having trouble getting modelica to import a c function.
the C function is:

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int main(float y)
{
    srand ( time(NULL) );
    float r = (float)rand()/(float)RAND_MAX;
    r = (float)rand()/(float)RAND_MAX;
    int x=r*100;
    return x;
}

The C is just supposed to generate a random integer from 1-100
The function I use to import is:

function RNG
  input Real y;
  output Real r;
  external "C" r=main(y)  annotation(Include = "#include <main.c>");
end RNG;

And the model i'm using as a test case is:

model testmodel
  Real y =0; 
  Real r;
equation
r= RNG(y);
end testmodel;

I continue to get a error message when i try and simulate/check:
Error building simulator. Buildlog: gcc   -falign-functions -msse2 -mfpmath=sse   -I"C:/OpenModelica1.9.0//include/omc" -I.    -c -o RLCpack.testmodel.o RLCpack.testmodel.c
In file included from RLCpack.testmodel.c:18:
C:/OpenModelica1.9.0//include/omc/main.c:4: error: conflicting types for 'main'
RLCpack.testmodel_functions.h:19: note: previous declaration of 'main' was here
RLCpack.testmodel.c:374: error: conflicting types for 'main'
RLCpack.testmodel_functions.h:19: note: previous declaration of 'main' was here
mingw32-make: *** [RLCpack.testmodel.o] Error 1

Both the function and model pass the check test in OMEdit, but when I try to run it spits out this error
I'm not exactly sure where to start with this error and any help would be appreciated. Or if you know of an easier way to generate random numbers.

Re: Newbie, having trouble with external function/ trying to get a RNG

Don't name the function "main". It's reserved for the executable entry-point in C and has to have type int main(int argc, char** argv).

Re: Newbie, having trouble with external function/ trying to get a RNG

Thanks for your help, I renamed the variables and external c file, but it is throwing a different error message now

The changes I made were:
In C:
int main(float y)   =>  int rnd(float y)

In function:
external "C" r=main(y)  annotation(Include = "#include <main.c>");   =>
external "C" r=rnd(y) annotation(Include = "#include <rnd2.c>";

and renaming the c file from main.c to rnd2.c

And the error now thrown is:

Error building simulator. Buildlog: gcc   -falign-functions -msse2 -mfpmath=sse   -I"C:/OpenModelica1.9.0//include/omc" -I.    -c -o RLCpack.testmodel.o RLCpack.testmodel.c
In file included from RLCpack.testmodel.c:18:
C:/OpenModelica1.9.0//include/omc/rnd2.c:4: error: conflicting types for 'rnd'
RLCpack.testmodel_functions.h:19: note: previous declaration of 'rnd' was here
mingw32-make: *** [RLCpack.testmodel.o] Error 1

I tried to explicitly cast everything in integers as well, in case variable type was throwing the error, but that did not work either.

Re: Newbie, having trouble with external function/ trying to get a RNG

Look at the header auto-generated by OpenModelica. It should say that it expects the function to take a double. If you want to use single-precision floats, simple typecast it in your own function (will probably be automatic).

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