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

Error in C code generation

Error in C code generation

When I try to translate my Modelica models in OpenModelica I got several times the following error message:
Translation    08:00:36        0:0-0:0    Error building simulator. Buildlog: gcc  -O3 -falign-functions -msse2 -mfpmath=sse   -I"C:/OpenModelica1.8.1//include/omc" -I. -L"C:/OpenModelica/MetaModels/BeastModelicaPendulum/ModelicaShaft"   -c -o PendulumTLM.o PendulumTLM.c
PendulumTLM.c: I funktion "eqFunction_245":
PendulumTLM.c:8555: fel: l-v�rde kr�vs som v�nstra operand i tilldelning
PendulumTLM.c:8566: fel: l-v�rde kr�vs som v�nstra operand i tilldelning
mingw32-make: *** [PendulumTLM.o] Error 1


First thing is that the compiler gives error messages in Swedish, which I have never ever seen before. Therefore here a translation of the error message:
PendulumTLM.c:8555: error: lvalue required as left operand of assignment

Looking at line 8555 in PendulumTLM.c I see the following:
  0.0 = tmp2124.c1;

And, yes that is not C code.

Is this a common problem with OpenModelica? I got this several times. What can I do about it?
For your information: I use OpenModelica 1.8.1+ on a WindowsXP system.

Alexander

Re: Error in C code generation

Would you mind sharing the code of the C-function and the lines of Modelica code that caused the error?
To me it looks your Modelica code might be:

Code:

0.0 = f(x);

And for some reason the backend does not realize it needs to solve this equation as a non-linear system, but I could be wrong...

Re: Error in C code generation

OK, here is the code:

Code:


package TLM
  function TLMSetMotion
    input String name "Name of the interface";
    input Real time_in "Simulation time";
    input Real R[3] "Position vector - can be neglected (set {0,0,0}) if you do not use 3D modeling and verification";
    input Real A[3,3] "Rotation matrix - can be neglected (set {1,0,0}{0,1,0}{0,0,1}) if you do not use 3D modeling and verification";
    input Real v[3] "Velocity";
    input Real w[3] "Angular velocity";

    external "C" set_tlm_motion(name,time_in,R,A,v,w)     annotation(Include = "#include<tlmforce.h>", Library = "tlmmodelica");
  end TLMSetMotion;

  function TLMGetDelay
    input String name "Name of the interface";
    output Real TLMdelay "The TLM delay for the secific interface";

    external "C" get_tlm_parameters(name,TLMdelay)     annotation(Include = "#include<tlmforce.h>", Library = "tlmmodelica");
  end TLMGetDelay;

  function TLMGetForce
    input String name "Name of the interface";
    input Real time_in "Simulation time";
    input Real r[3] "Position Vector";
    input Real A[3,3] "Rotation matrix";
    input Real v[3] "Velocity";
    input Real w[3] "Angular velocity";
    output Real f[3] "Force vector";
    output Real t[3] "Torque/moment vector";

    external "C" calc_tlm_force(name,time_in,r,A,v,w,f,t)     annotation(Include = "#include<tlmforce.h>", Library = "tlmmodelica");
  end TLMGetForce;

  model MBS_TLMInterface
    annotation(Diagram(), Icon(graphics = {Rectangle(rotation = 0, lineColor = {0,0,255}, fillColor = {0,0,255}, pattern = LinePattern.Solid, fillPattern = FillPattern.HorizontalCylinder, lineThickness = 0.25, extent = {{-29.5484,42.5806},{96.9032,-45.8065}}),Text(rotation = 0, lineColor = {0,0,0}, fillColor = {0,0,255}, pattern = LinePattern.Solid, fillPattern = FillPattern.None, lineThickness = 0.25, extent = {{-6.32258,18.0645},{74.9677,-20.6452}}, textString = "TLM", fontSize = 28),Line(points = {{-63.2258,-2.58065},{-28.3871,-2.58065}}, rotation = 0, color = {0,0,255}, pattern = LinePattern.Solid, thickness = 5)}));
    Modelica.Mechanics.MultiBody.Interfaces.Frame_a frame_a annotation(Placement(visible = true, transformation(origin = {-67.9907,-3.03738}, extent = {{-12,-12},{12,12}}, rotation = 0), iconTransformation(origin = {-67.9907,-3.03738}, extent = {{-12,-12},{12,12}}, rotation = 0)));
    parameter String interfaceName = "tlm";
    Real v[3];
    Real f[3];
    Real t[3];
    Real tlmDelay = 0.0;
    Boolean initializedFlg = false;
    Real lastSendTime(start = 0.0);
  initial algorithm
    if initializedFlg == false then
        tlmDelay:=TLMGetDelay(interfaceName);
    assert(tlmDelay > 0.0, "Bad TLM delay in" + interfaceName + ", give up");
    initializedFlg:=true;

    else
    end if;
  equation
    v = der(frame_a.r_0);
    frame_a.t = -t;
    frame_a.f = -f;
  algorithm
    (f,t):=TLMGetForce(interfaceName, time, frame_a.r_0, frame_a.R.T, v, frame_a.R.w);
  algorithm
    when time >= lastSendTime + tlmDelay then
          TLMSetMotion(interfaceName, time, frame_a.r_0, frame_a.R.T, v, frame_a.R.w);
      lastSendTime:=time;
   
    end when;
  end MBS_TLMInterface;
end TLM;

model PendulumTLM
  extends Modelica.Icons.Example;
  inner Modelica.Mechanics.MultiBody.World world(gravityType = Modelica.Mechanics.MultiBody.Types.GravityTypes.UniformGravity, n = {-1,0,0}) annotation(Placement(transformation(extent = {{-60,0},{-40,20}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Joints.Revolute rev(n = {0,1,0}, useAxisFlange = true, phi(fixed = true), w(fixed = true)) annotation(Placement(transformation(extent = {{-20,0},{0,20}}, rotation = 0)));
  Modelica.Mechanics.Rotational.Components.Damper damper(d = 0.1) annotation(Placement(transformation(extent = {{-20,40},{0,60}}, rotation = 0)));
  TLM.MBS_TLMInterface mbs_tlminterface1 annotation(Placement(visible = true, transformation(origin = {85.0467,10.7477}, extent = {{-12,-12},{12,12}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Parts.BodyBox bodybox1(r = {0,0,0.32}) annotation(Placement(visible = true, transformation(origin = {36.215,10.0467}, extent = {{-12,-12},{12,12}}, rotation = 0)));
equation
  connect(bodybox1.frame_b,mbs_tlminterface1.frame_a) annotation(Line(points = {{48.215,10.0467},{77.1028,10.0467},{77.1028,10.3832},{76.8878,10.3832}}));
  connect(rev.frame_b,bodybox1.frame_a) annotation(Line(points = {{5.55112e-016,10},{25,10},{25,10.0467},{24.215,10.0467}}));
  connect(world.frame_b,rev.frame_a) annotation(Line(points = {{-40,10},{-20,10}}, color = {95,95,95}, thickness = 0.5));
  connect(damper.flange_b,rev.axis) annotation(Line(points = {{0,50},{4,50},{4,26},{-10,26},{-10,20}}, color = {0,0,0}));
  connect(rev.support,damper.flange_a) annotation(Line(points = {{-16,20},{-16,26},{-28,26},{-28,50},{-20,50}}, color = {0,0,0}));
  annotation(experiment(StopTime = 5), Diagram(coordinateSystem(preserveAspectRatio = true, extent = {{-100,-100},{100,100}}, grid = {2,2}), graphics), Documentation(info = "<html></html>"));
end PendulumTLM;

The external functions are part of some C-code that is too big to publish here. Besides does the problem occur before linking the code.

Alexander

Re: Error in C code generation

Sorry, I forgot the C-function generated by the Modelica code before, here it is:

Code:


void eqFunction_245(DATA *data) {
  TLM_TLMGetDelay_rettype tmp2124;
  modelica_string tmp2125;
  modelica_string tmp2126;
  /*#modelicaLine [c:/OpenModelica/TLM.mo:74:9-74:45]*/

  #ifdef _OMC_MEASURE_TIME
  SIM_PROF_TICK_FN(TLM_TLMGetDelay_index);
  #endif
  tmp2124 = _TLM_TLMGetDelay($Pmbs_tlminterface1$PinterfaceName);

  #ifdef _OMC_MEASURE_TIME
  SIM_PROF_ACC_FN(TLM_TLMGetDelay_index);
  #endif
  0.0 = tmp2124.c1;
  /*#endModelicaLine*/
  /*#modelicaLine [c:/OpenModelica/TLM.mo:75:5-75:77]*/
  if (!(0)) {
    tmp2125 = cat_modelica_string("Bad TLM delay in",$Pmbs_tlminterface1$PinterfaceName);
    tmp2126 = cat_modelica_string(tmp2125,", give up");
    FILE_INFO info = {"c:/OpenModelica/TLM.mo",75,5,75,77,0};
    MODELICA_ASSERT(info, tmp2126);
  }
  /*#endModelicaLine*/
  /*#modelicaLine [c:/OpenModelica/TLM.mo:76:5-76:25]*/
  (0) = (1);
  /*#endModelicaLine*/
}

Re: Error in C code generation

Note, that I removed some unnecessary code from the TLM.mo file, line 74 in the TLM.mo file is the following:

Code:


        tlmDelay:=TLMGetDelay(interfaceName);

Re: Error in C code generation

The total error can be reduced to the following:

Code:

model M

  Real tlmDelay = 0.0;
initial algorithm
  tlmDelay := 1.5;
end M;

I believe this is an erroneous model (the model is reduced to 0.0 := 1.5). The backend should detect this error and scream a bit.

I guess you want to give tlmDelay a default value and modify it slightly during initialization? I'm a bit unsure how you do this in a good way in Modelica. You probably simply want to make tlmDelay a parameter instead and avoid sources for error.

Re: Error in C code generation

OK, the following construct compiles at least:

Code:


  parameter Real tlmDelay = 0.0;
initial algorithm
  tlmDelay := TLM.TLMGetDelay(interfaceName);
  assert(tlmDelay > 0.0, "Bad tlmDelay in " + interfaceName + ", give up.");

I'm just not sure if I can actually change the parameter value inside "initial algorithm"?! Is that possible?

Also, now the simulation crashes with the following message:

    messages = "Simulation execution failed for model: PendulumTLM
throw   | [line] 192 | [file] simulation/solver/events.c
        | Could not allocate Memory for initSample!


Any ideas what's wrong here?

Re: Error in C code generation

Just for the record, here is the code that actually works:

Code:


  parameter Real tlmDelay = TLMGetDelay(interfaceName);

initial algorithm
  assert(tlmDelay > 0.0, "Bad tlmDelay in " + interfaceName + ", give up.");

I didn't realize that one can call a function in parameter initialization.
Thanks for the support.

Re: Error in C code generation

Yes, there was a problem in the try above since tlmDelay was set in 2 places. It would probably have worked if tlmDelay = 0.0 was not set (or used start = 0.0 instead). The code below is of course the nicest current/smile Glad to see it worked.

Re: Error in C code generation

Hey,

I am trying to figure out how to generate C code while making a model in Modelica. Could you let me know how you did it?

Thanks

Re: Error in C code generation

Hello

I generate C code associated to a MODELICA file nammed for instance "test.mo" with a shell sccript with the simCodeTarget option

#... Executables
omcCompiler="omc"
exeCompil=$BIN_OPENMODELICA/$omcCompiler

options="+noSimplify +showErrorMessages +d=failtrace +d=disableSingleFlowEq +g=Modelica +d=initialization +d=debugAlias +d=checkSimplify"
options2="+postOptModules=generateSymbolicJacobian"

$exeCompil $options $options2 +simCodeTarget=C   +s "test.mo"

Best regards

ALAIN_2

Re: Error in C code generation

'Error occurred building AST
Syntax Error
[<interactive>:1:0-1:0:writable] Error: Lexer failed to recognize '#... Execut''

This is what I am getting as the message when i write this command

Re: Error in C code generation



It give you only a part of code belonging to a shell script
You must include it in ,for instance, lance_omc.sh,

#!/bin/bash
#set -x

test=$1
if [ ! -f ${test} ]; then
   echo "%% Error : le fichier de donnees \"$test\" n'existe pas."
   exit
fi
BIN_OPENMODELICA=$INSTALL_OPENMODELICA/bin  <= Put the place where omc is installed

....


Best regards

ALAIN_2

Re: Error in C code generation



It give you only a part of code belonging to a shell script
You must include it in ,for instance, lance_omc.sh,

#!/bin/bash
#set -x

test=$1
if [ ! -f ${test} ]; then
   echo "%% Error : le fichier de donnees \"$test\" n'existe pas."
   exit
fi
BIN_OPENMODELICA=$INSTALL_OPENMODELICA/bin  <= Put the place where omc is installed

....


Best regards

ALAIN_2

Re: Error in C code generation



It give you only a part of code belonging to a shell script
You must include it in ,for instance, lance_omc.sh,

#!/bin/bash
#set -x

test=$1
if [ ! -f ${test} ]; then
   echo "%% Error : le fichier de donnees \"$test\" n'existe pas."
   exit
fi
BIN_OPENMODELICA=$INSTALL_OPENMODELICA/bin  <= Put the place where omc is installed

....


Best regards

ALAIN_2

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