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:布尔变量b扩展为“(b而不是pre(b))”。适用与pre()运算符相同的限制(例如,不在函数类中使用)。
pre:在时刻t返回变量y(t)的“左极限” y(tpre)。 (以及其他说明它确实是变量的文本。)
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;我只是非正式地使用它。)