compiler-errors - 使用 OpenModelica v1.14.1(64 位)编译简单的 Modelica 代码时出现多个未声明的标识符错误

标签 compiler-errors modelica openmodelica

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

最佳答案

只是为了澄清:

  • edge 的参数确实必须是一个变量,Modelica 3.4 声明:

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

    pre:返回变量 y(t) 在时刻 t 的“左极限”y(tpre)。 (以及其他说明它确实是一个变量的文本。)
  • 忽略 (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) ;这可以简化为假。

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

    关于compiler-errors - 使用 OpenModelica v1.14.1(64 位)编译简单的 Modelica 代码时出现多个未声明的标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60076631/

    相关文章:

    scala - 为什么编译器会错误地推断函数中 Future.sequence 声明的返回类型?

    include - 获取编译器:Error-Row when the compile error occurs in an include file

    modelica - 在 Modelica 中访问仿真参数

    modelica - Modelica 中的条件组件

    c - 外部函数: Calling a C script that use external Library

    delphi - 为什么我的代码无法编译,而是得到 E2506 在接口(interface)部分声明的参数化类型的方法不能使用本地符号

    c++ - Linux g++编译错误: error: expected ',' or '...' before '||' token

    modelica - 基于 bool 参数的开关类型(不继承)?

    modelica - 作为除数的导数在 Dymola 初始化期间除以零

    openmp - OpenModelica 中调用外部 C 函数对 CPU 时间的影响