【问题标题】:Multiple undeclared identifier error compiling simple Modelica code with OpenModelica v1.14.1 (64-bit)使用 OpenModelica v1.14.1(64 位)编译简单的 Modelica 代码时出现多个未声明的标识符错误
【发布时间】:2020-02-05 13:11:45
【问题描述】:

OpenModelica生成的以下简单Modelica模型的C源代码编译失败。这很尴尬,因为 C 编译器抱怨缺少应该由工具自动生成的符号。但是,我可以补充一点,该模型不使用 Dymola 进行检查。报告了几个错误,第一个是The operand in edge(particles[bIdx].alive) must be a variable.,据我理解的语言,不是这样。

我希望通过下面的源代码,有人可以对这种情况有所了解,并且可以确定我是否做错了什么(模型不应该很好地检查)或者我在 C 源代码生成器中发现了一个错误(以及Dymola 翻译)。

干杯!

model TestAlgorithm

  import SI = Modelica.SIunits;

  model Particle

    import SI = Modelica.SIunits;

    SI.Length s "Position of the particle";   
    SI.Velocity v "Velocity of the particle";   
    Real theta "Quantity transported by the particle";   
    Real alpha(min = 0, max = 1);   
    Boolean alive;
//    Integer alive; // This doen't help
//    Real alive; // This don't even translate

  equation

    der(s) = v;
    der(theta) = 0;

  end Particle;


  parameter SI.Length L = 10 "Length of the box";
  parameter Integer nParticles = 4 "Number of particles";
  parameter SI.Velocity v = 1.5; 
  parameter SI.Length Ds = L/nParticles;

  Particle particles[nParticles+1];

protected

  Integer aIdx;
  Integer bIdx;

initial algorithm

  aIdx := 1;
  bIdx := nParticles + 1;

initial equation

  for i in 1:(nParticles + 1) loop

    particles[i].s = (i - 1)*Ds; // Initial position of the particles
    particles[i].alive = true;
//    particles[i].alive = 1; // To be used with Integer and Real
    particles[i].v = v; // Initial velocity of the paraticles 
  end for;


  for i in 1:integer(nParticles/4) loop

    particles[i].theta = 2;   
  end for;

  for i in (integer(nParticles/4)+1):(nParticles + 1) loop

    particles[i].theta = 1;   
  end for;

  // Note! Moving alpha initialization from initial algorithm to initial equation made the model translable to C (still not compiling)
  particles[1].alpha = 0.5;

  for i in 2:nParticles loop
    particles[i].alpha = 1.0;
  end for;

  particles[nParticles+1].alpha = 0.5;

algorithm

  when 
   (edge(particles[bIdx].alive) and pre(particles[bIdx].alive)) or 
   (edge(particles[aIdx].alive) and pre(particles[aIdx].alive)) then
//   (change(particles[bIdx].alive) and pre(particles[bIdx].alive)==1) or // To be used with Integer and Real
//   (change(particles[aIdx].alive) and pre(particles[aIdx].alive)==1) then // To be used with Integer and Real

    if v >= 0 then

      aIdx := bIdx;
      bIdx := mod(bIdx - 1 - 1, nParticles + 1) + 1;
    else

      bIdx := aIdx;
      aIdx := mod(aIdx + 1 - 1, nParticles + 1) + 1;
    end if;
  end when;

  //(nParticles + 1)*3 equations
  for i in 1:nParticles + 1 loop

    particles[i].alive := (particles[i].s + Ds/2 > 0) and (particles[i].s - Ds/2 < L);
//    particles[i].alive := if (particles[i].s + Ds/2 > 0) and (particles[i].s - Ds/2 < L) then 1 else 0; // To be used with Integer and Real
    particles[i].alpha := if (particles[i].s  - Ds/2) < 0 then particles[i].s/Ds + 1/2 else if (particles[i].s + Ds/2) > L then (particles[i].s - L)/Ds + 1/2 else 1;
  end for;

equation

  when 
   (edge(particles[bIdx].alive) and pre(particles[bIdx].alive)) or 
   (edge(particles[aIdx].alive) and pre(particles[aIdx].alive)) then
//   (change(particles[bIdx].alive) and pre(particles[bIdx].alive)==1) or // To be used with Integer and Real
//   (change(particles[aIdx].alive) and pre(particles[aIdx].alive)==1) then // To be used with Integer and Real

    if v >= 0 then

      reinit(particles[aIdx].s, particles[mod(aIdx + 1 - 1, nParticles+1) + 1].s - Ds);
      reinit(particles[aIdx].theta, particles[aIdx].theta); // Loop condition for debug purposes
    else

      reinit(particles[bIdx].s, particles[mod(bIdx - 1 - 1, nParticles+1) + 1].s + Ds);
      reinit(particles[bIdx].theta, particles[bIdx].theta); // Loop condition for debug purposes
    end if;
  end when;

  for i in 1:nParticles + 1 loop
    particles[i].v = v;
  end for;

  annotation ();
end TestAlgorithm;

错误信息是

C:/Program Files/OpenModelica1.14.1-64bit//share/omc/scripts/Compile.bat TestAlgorithm gcc mingw64 parallel 8 0
PATH = "C:\PROGRA~1\OPENMO~1.1-6\tools\msys\mingw64\bin;C:\PROGRA~1\OPENMO~1.1-6\tools\msys\mingw64\bin\..\..\usr\bin;"
mingw32-make: Entering directory 'C:/Users/ASOPPE~1/AppData/Local/Temp/OPENMO~1/OMEdit/TESTAL~1'
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm.o TestAlgorithm.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_functions.o TestAlgorithm_functions.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_records.o TestAlgorithm_records.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_01exo.o TestAlgorithm_01exo.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_02nls.o TestAlgorithm_02nls.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_03lsy.o TestAlgorithm_03lsy.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_04set.o TestAlgorithm_04set.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_05evt.o TestAlgorithm_05evt.c
TestAlgorithm.c: In function 'TestAlgorithm_eqFunction_60':
TestAlgorithm.c:339:5: error: 'particles' undeclared (first use in this function)
     particles[aIdx].s = (&data->localData[0]->realVars[0] /* particles[1].s STATE(1,particles[1].v) */)[calc_base_index_dims_subs(1, 5, modelica_integer_mod(data->localData[0]->integerVars[0] /* aIdx DISCRETE */ + ((modelica_integer) 1) - ((modelica_integer) 1), tmp10) + ((modelica_integer) 1))] - data->simulationInfo->realParameter[0] /* Ds PARAM */;
     ^
TestAlgorithm.c:339:5: note: each undeclared identifier is reported only once for each function it appears in
TestAlgorithm.c:339:15: error: 'aIdx' undeclared (first use in this function)
     particles[aIdx].s = (&data->localData[0]->realVars[0] /* particles[1].s STATE(1,particles[1].v) */)[calc_base_index_dims_subs(1, 5, modelica_integer_mod(data->localData[0]->integerVars[0] /* aIdx DISCRETE */ + ((modelica_integer) 1) - ((modelica_integer) 1), tmp10) + ((modelica_integer) 1))] - data->simulationInfo->realParameter[0] /* Ds PARAM */;
               ^
TestAlgorithm.c: In function 'TestAlgorithm_eqFunction_59':
TestAlgorithm.c:359:5: error: 'particles' undeclared (first use in this function)
     particles[aIdx].theta = (&data->localData[0]->realVars[5] /* particles[1].theta STATE(1) */)[calc_base_index_dims_subs(1, 5, data->localData[0]->integerVars[0] /* aIdx DISCRETE */)];
     ^
TestAlgorithm.c:359:15: error: 'aIdx' undeclared (first use in this function)
     particles[aIdx].theta = (&data->localData[0]->realVars[5] /* particles[1].theta STATE(1) */)[calc_base_index_dims_subs(1, 5, data->localData[0]->integerVars[0] /* aIdx DISCRETE */)];
               ^
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_06inz.o TestAlgorithm_06inz.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_07dly.o TestAlgorithm_07dly.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I"C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=TestAlgorithm -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=1 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o TestAlgorithm_08bnd.o TestAlgorithm_08bnd.c
<builtin>: recipe for target 'TestAlgorithm.o' failed

【问题讨论】:

  • 对不起,我看到 OM 正在使用 gcc。 Dymola使用VS2017。

标签: compiler-errors modelica openmodelica


【解决方案1】:

澄清一下:

  1. Edge 的参数确实必须是一个变量,Modelica 3.4 指出:

edge:为布尔变量 b 扩展为“(b 而不是 pre(b))”。与 pre() 运算符相同的限制适用(例如,不能在函数类中使用)。

pre:返回变量 y(t) 在时刻 t 的“左极限”y(tpre)。 (以及其他说明它确实是一个变量的文本。)

  1. 忽略 (1) when 语句无法触发,因为 edge(particles[bIdx].alive) and pre(particles[bIdx].alive 与上面的定义相同 particles[bIdx].alive and not pre(particles[bIdx].alive) and pre(particles[bIdx].alive);可以简化为 false。

允许pre(v[bIdx]) 的问题在于不清楚它是打算成为pre(v)[bIdx] - 还是pre(v)[pre(bIdx)];后者的工作方式类似于pre(b)Boolean b=v[bIdx];。 (注意:索引 pre(...) 是不合法的 Modelica;我只是非正式地使用它。)

【讨论】:

  • 好吧,我缺少一些东西。为什么particles[bIdx].alive 不是变量?
  • 我尝试自己回答我的评论,但我刚开始研究这种语言,所以它可能是错误的。 'particles[bIdx].alive' 是一个计算类型为 Boolean 的表达式。但是,该表达式包含一个符号,该符号是另一个变量bIdx,因此,尽管该表达式标识了单个标量变量,但它的解析需要另一个变量,这使得它更像是对具有状态的函数的调用,而不是对确定的内存位置。顺便说一句,感谢您的两个见解,我认为我现在可以想出一个解决方案。
猜你喜欢
  • 2014-08-17
  • 1970-01-01
  • 2020-12-19
  • 2020-08-10
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多