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
  • Index
  • » Users
  • » olaama
  • » Profile

Posts

Posts

Jul-29-15 12:53:28
problem while passing a struct output of an external function as input of an other one.

Thank you for your answer ! It was very helpul.

Jul-24-15 22:00:43
problem while passing a struct output of an external function as input of an other one.

Yes, the problem Is solved, the solution is to define the first function in OM like this : "impure function Chirp" instead of "function chirp", and in the equation part you must call it this way

equation
u1 = Chirp();
u2 = mafonction(Chirp());
end ChirpSignal;

//instead of :
/*
u1=Chirp();
u2 = mafonction(u1);
*/

that's it.

Jul-24-15 21:37:01
problem while passing a struct output of an external function as input of an other one.

Hi ,
for the C code I said that I totally agree with u (there was a mistake thank u for reminding me).
for the OM code : OK , I thought it was equivalent . but still, even with what u gave me, the result is the same, the values are in the reverse order.
Have u tried what I gave u ? It works !!

Cheers,
Olaama

Jul-24-15 21:19:37
problem while passing a struct output of an external function as input of an other one.

Hi Adrian,

First , thank you for your answer.
What you proposed doesn't change anything.
for the error in C code, I totally agree with you. It was just an error while copying the code.
for the OM code : 
"external u1 = mafonction(u) annotation(Library = "Chirp");"  and  "external mafonction(u1, u2) annotation(Library = "Chirp", LibraryDirectory="modelica://ChirpSignal"); " are totally equivalent (at least in this exemple), I still get the same result.

Actually the solution is to declare the first function as impure and pass it as argument of the second one.  :

impure function Chirp
    output Rec u;
 
    external Chirp(u) annotation(Library = "Chirp1", LibraryDirectory = "modelica://ChirpSignal");
  end Chirp;

  function mafonction
    input Rec u1;
    output Rec u2;
 
    external u2 = mafonction(u1) annotation(Library = "Chirp1", LibraryDirectory = "modelica://ChirpSignal");
  end mafonction;

  Rec u2;
equation
  u2 = mafonction(Chirp());
end ChirpSignal;

Try this and tell me what u get (It works just fine).

Anyway, thanks for your Help.

Cheers,
Olaama

Jul-23-15 13:02:13
problem while passing a struct output of an external function as input of an other one.

Help Please !

Jul-23-15 10:00:55
problem while passing a struct output of an external function as input of an other one.

Hi guys, I have a little problem with the execution of an external C function in openmodelica.
Here is the program that I'm trying to implement :

model ChirpSignal
  record Rec
    Real a1;
    Real a2;
    Real a3;
    Real a4;
    Real a5;

  end Rec;

  function Chirp
    output Rec u1;
 
    external  Chirp(u1) annotation(Library = "Chirp");
  end Chirp;

  function mafonction
    input Rec u;
    output Rec u1;
 
    external u1 = mafonction(u) annotation(Library = "Chirp");
  end mafonction;

  Rec u1;
  Rec u2;
 
equation
  u1 = Chirp();
  u2 = mafonction(u1);
end ChirpSignal;

// the C program is :


typedef struct  {

double id ;
double id1 ;
double id2;
double id3;
double id4;

}rec;


rec* Chirp(rec* test)
{

test->id = 7;
test->id1 = 6;
test->id2 = 5;
test->id3 = 4;
test->id4 = 15;

}

rec* mafonction(rec test,rec* test1)
{

memcpy ( &test1, &test, sizeof(rec) );

return test1;
}


I'm trying to use the output of the first function (Chirp) as input of the second one (mafonction).
the problem is that the values of u1 are not transmitted in the correct order.
for exemple : for u1 I get :
a1 =  7;
a2 = 6;
a3 = 5;
a4 = 4;
a5 = 15;

I'm expecting to have the same for u2, but I get :
a1 =  15;
a2 = 4;
a3 = 5;
a4 = 6;
a5 = 7;

Do you have any Idea why the values are in the reverse order ??  Is there an other way to implement this correctly ?
Thanks for your help.

Anybody ? Please Help. I really need to find a solution.
Thanks
Olaama

thanks for your answer. actually I had a compilation error from gcc and I fixed it but my problem with OM is still not fixed.
As u suggested I used "u" as an input for the second function like this :

  function mafonction
    input rec u;
    external mafonction(u) annotation(Library = "Chirp");
  end mafonction;

equation

  (id, u) = Chirp();
  mafonction(u);
end ChirpSignal;

the compilation works (Compilation process finished successfully), but not the execution  . I get the following error :
stdout | error | <p>Process crashed<br>
Simulation process failed. Exited with code -1073741819.</p>

note that the first function (Chirp) works just fine, the simulation is crashing when calling the second function.
Thanks

I used "u" as an input for the second function and I got the following error :

c:/openmodelica1.9.3nightly/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot open output file ChirpSignal.exe: Permission denied
collect2: ld returned 1 exit status
mingw32-make: *** [omc_main_target] Error 1

Hello,

I'm trying to execute the following program in Openmodelica. I want to use the output of the first function as input of the second one.

model ChirpSignal
  record Rec
    Integer a1;
    Real b;
    Real c;
    Real d;
    Real d1;
    Real d2;
    Real d3;
    Real e;
  end Rec;

  record pp_actor
    Integer a;
  end pp_actor;

  record rec
    pp_actor base;
    Rec mstdatas;
  end rec;

  function Chirp
    output String id;
    output rec u;
 
    external chirp(id, u) annotation(Library = "Chirp");
  end Chirp;

  function mafonction
    output Integer alpha;
    output rec u;
 
    external alpha = mafonction(u) annotation(Library = "Chirp");
  end mafonction;

  String id;
  rec u;
  Integer alpha;
equation
  (id, u) = Chirp();
  (alpha, u) = mafonction();
end ChirpSignal;

this program is linked as a binary library to the following one :

typedef struct  {

int id1;
double id2;
double id3;
double id4;
double T_Temp[4] ;

}Rec;

typedef struct  {
int c;
}pp_tp_actor_t;

typedef struct  {
pp_tp_actor_t base;
Rec mstdatas;
}rec;



rec* chirp(const char* id,rec* test)
{

test->mstdatas.id1 = 6;
test->mstdatas.id2 = 5;
test->mstdatas.id3 = 4;
test->mstdatas.id4 = 3;
int i=0;
for (i=0; i<4; i++)
    {
test->mstdatas.T_Temp[i]=2;
}
test->base.c = 5;
}

int mafonction(rec* self)
{
    self->mstdatas.id1 = 2;
     self->mstdatas.id2 = 2;
      self->mstdatas.id3 = 2;
       self->mstdatas.id4 = 2;

    return 2;
};

I don't understand why is this not working. I get the following error :

[1] 13:08:27 Symbolique Erreur
Too many equations, over-determined system. The model has 20 equation(s) and 11 variable(s).

[2] 13:08:27 Traduction Erreur
Internal error pre-optimization module clockPartitioning failed.

[3] 13:08:27 Symbolique Erreur
[ChirpSignal: 40:3-40:20]: Found equation without time-dependent variables: u.base.a = 5


Thank you
olaama

Jul-07-15 11:07:13
Trying to run Exercise3, Chapter 9 Algorithms but getting error

Okey ! Thanks !

Hi adeel it's me again,

In the definition of "record rec" I changer the Integers by Real, and I don't have the error anymore, but the values I get for a and b are totally absurd. (Instead of having a=2 and b=3, I get a = 4.31109e-308, b=2.78583e+165.
Any Idea ?

Hi Adeel,

this is the error message I get :

"assert | debug | <p>ERROR: Too many event iterations. System is inconsistent. Simulation terminate.</p>"

I have to say that when I try to return a struct instead of a pointer to a struct, it works just fine. for exemple the following exemple works fine :

typedef struct {
int id1;
int id2;
}mystruct;

mystruct Chirp(double a,double b)
{
mystruct test;
//test = malloc(sizeof(mystruct));
test.id1 = 1;
test.id2 = 2;
return test;
}

do you have any idea or solution for my problem?
Thank you.

olaama

Hello,

I'm trying to call an external C function that returns a pointer to a struct.

Here is the program I use (in OpenModelica :

record rec
  Integer a;
  Integer b;
end rec;

function Chirp
  input Real id;
  input Real id2;
  output rec u;

  external u = Chirp(id, id2) annotation(Library = "Chirp");
end Chirp;

model ChirpSignal
  rec u;
  parameter Real id = 1;
  parameter Real id1 = 2;
equation
  u = Chirp(id, id1);
end ChirpSignal;


the programm in C is :

typedef struct {
int id1;
int id2;
}mystruct;


mystruct* Chirp(double a,double b)
{
mystruct* test;
test = malloc(sizeof(mystruct));
test->id1 = 2;
test->id2  =3;

return test;
}


The problem is that I don't have the right to change the C-program, I have to keep it like it is.
Can anybody please tell me if there is something to change in the OpenModelica programm so as to make it work.
Thank you.



Thanks a lot for your answer. It was very helpful.

Thanks for your answer.
actually I'm getting started in OpenModelica and  I'm in need to use a struct as argument (in C), so can you please be more explicit when you talk about "link as a binary library". How do I do that ?
Thank you



I'm trying to execute the following program in OpenModelica

    model MyModel
  record R
    Integer a;
  end R;

  Modelica.Blocks.Interfaces.RealOutput u;
  Real A = 1;
  Real M = 10;
  Modelica.SIunits.AngularVelocity w_start = 0;
  Modelica.SIunits.AngularVelocity w_end = 10;
  R r(a=2);

  function Chirp
    input R r;
    input Modelica.SIunits.AngularVelocity w_start;
    input Modelica.SIunits.AngularVelocity w_end;
    input Real A;
    input Real M;
    input Real t;
    output Real u "output signal";

    external "C"  annotation(Include = "#include \"Chirp.c\"");
  end Chirp;
equation
  u = Chirp(r, w_start, w_end, A, M, time);
end MyModel;

the chirp C function is :

typedef struct {
int a;
}mystruct;
double Chirp(mystruct test,double w1, double w2, double A, double M, double time)
{
test.a = 2;
double res;
res=(test.a)*A*cos(w1*time+(w2-w1)*time*time/(2*M));
return res;

}

I get the following error msg :

myModel_functions.c: In function 'omc_MyModel_Chirp':
myModel_functions.c:37: error: incompatible type for argument 1 of 'Chirp'
C:/OpenModelica1.9.3Nightly//include/omc/c/Chirp.c:6: note: expected 'mystruct' but argument is of type 'MyModel_R'
mingw32-make: *** [myModel_functions.o] Error 1
mingw32-make: *** Waiting for unfinished jobs....
Compilation process failed. Exited with code 2.

can anybody tell me what is wrong with my model ? (I'm using OpenModelica v1.9.3-dev-680) thanks.

  • Index
  • » Users
  • » olaama
  • » Profile
You are here: