Opened 18 years ago
Last modified 14 years ago
#76 closed defect (fixed)
TabIndex Simulation failed
Reported by: | Robert.Wotzlaw | Owned by: | Robert.Wotzlaw |
---|---|---|---|
Priority: | critical | Milestone: | |
Component: | Version: | ||
Keywords: | Cc: | Robert.Wotzlaw, Adrian Pop |
Description
See comments.
Change History (7)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
In my bug report I made two mistaks.
- The row "Integer k;" should be "parameter Integer k = 1;". The row with "k = 1;" must be erased. The syntax is not right.
- stopTime should be "1" and not "%".
I will send a new bug report, because with the parameter prefix
the bug will vanish. I belive.
regards,
Robert Wotzlaw
comment:3 by , 18 years ago
I am pretty sure that subscripts in variables must be of parameter or constant
variablity in Modelica. Workaround, write
parameter Integer k=1;
comment:4 by , 18 years ago
Dear Mr. Peter Aronsson,
first thanks for Your answer. I tried the prefix "parameter" in front of the row
"Integer k = 1;", but the simulation failed with the following error:
"TabIndex.cpp:279: error: `tabelle' undeclared(first use this function)"
"TabIndex.cpp:279: error: `k' undeclared(first use this function)"
This is nothing new. Before I succeed with my report, I will explain You my
intentions. I want build a event counter. The intput is a time event the output
the counted event. The counted event is a index in a table. But the compiler
builds no error free C code, so I build a simpler class, the class TabIndex.
After some time I recognized my fault. In the equation section I can't
build expression like "k = 1", but I needed a index to show You the problem.
So I will show You my index counter in OpenModelica source code.
The BugCounterTabIndex.mo OpenModelica source code file:
You can find a similar example "class WillowForest" in Peter Fritzson book
"Principles of Object-Oriented Modeling and Simulation with Modelica 2.1"
chapter 15, page 560, 561.
class BugCounterTabIndex
parameter Real first = 0;
parameter Real interval = first + 1;
parameter Real tabelle[2] = {1,2};
Real x;
Integer k(start = 1);
equation
when sample(first,interval) then
k = (if pre(k)+1 == 3 then 1 else pre(k)+1);
x = tabelle[k];
end when;
end BugCounterTabIndex;
In the OMShell
loadFile("BugCounterTabIndex.mo")
true
instantiateModel(BugCounterTabIndex)
"fclass BugCounterTabIndex
parameter Real first = 0;
parameter Real interval = 1.0 + first;
parameter Real tabelle[1] = 1;
parameter Real tabelle[2] = 2;
Real x;
Integer k(start=1);
equation
when sample(first,interval) then
k = if 1 + pre(k) == 3 then 1 else 1 + pre(k);
x = tabelle[k];
end when;
end BugCounterTabIndex;
"
simulate(BugCounterTabIndex,startTime=0, stopTime=3)
record
resultFile = "Simulation failed.
"
end record
The error.log file
1:BugCounterTabIndex.cpp: In function `int function_when(int)':
2:BugCounterTabIndex.cpp:403: error: `tabelle' undeclared (first use this
3: function)
4:BugCounterTabIndex.cpp:403: error: (Each undeclared identifier is reported only
5: once for each function it appears in.)
6:BugCounterTabIndex.cpp:403: error: `k' undeclared (first use this function)
7:make: * [BugCounterTabIndex] Error 1
8:-build_model failed
BugCounterTabIndex.cpp file generated from omc (excerpts)
041:#define $tabelle$leftBracket2$rightBracket loacalData->parameters[3]
042:#define $tabelle$leftBracket1$rightBracket loacalData->parameters[2]
043:#define $tabelle loacalData->parameters[2]
044:#define $interval loacalData->parameters[1]
045:#define $first loacalData->parameters[0]
046:#define $k loacalData->algebraics[1]
047:#define $x loacalData->algebraics[0]
374:int function_when(int i)
375:{
399: case 1:when sample($first,$interval)
400:
401: save($x);
402:
403: $x = (*real_array_element_addr1(&tabelle, 1, k));
413:}
Perhaps You see the problem in row 403. Now You find the undeclared identifier
tabelle and k in the function function_when(). I think OpenModelica can't life
with indexed tables in a class.
The example function "bubblesort" shiped with sources use indexed tables and
has no problems with that.
Later I show You my workaround, but now again my class TabIndex renamed as
class BugTabIndex.
The BugTabIndex.mo OpenModelica source code file:
class BugTabIndex
parameter Real tabelle[2] = {1,2};
Real x;
Real y;
Integer k = 1;
equation
x = tabelle[k];
y = tabelle[2];
end BugTabIndex;
In the OMShell
loadFile("BugTabIndex.mo")
true
instantiateModel(BugTabIndex)
"fclass BugTabIndex
parameter Real tabelle[1] = 1;
parameter Real tabelle[2] = 2;
Real x;
Real y;
Integer k;
equation
k = 1;
x = tabelle[k];
y = tabelle[2];
end BugTabIndex;
"
simulate(BugTabIndex,startTime=0, stopTime=2)
record
resultFile = "Simulation failed.
"
end record
The error.log file
1:BugTabIndex.cpp: In function `int functionDAE_output()':
2:BugTabIndex.cpp:273: error: `tabelle' undeclared (first use this function)
3:BugTabIndex.cpp:273: error: (Each undeclared identifier is reported only
4: once for each function it appears in.)
5:BugTabIndex.cpp:273: error: `k' undeclared (first use this function)
6:make: * [BugTabIndex] Error 1
7:-build_model failed
BugTabIndex.cpp file generated from omc (excerpts)
041:#define $y loacalData->algebraics[2]
042:#define $x loacalData->algebraics[1]
043:#define $tabelle$leftBracket2$rightBracket loacalData->parameters[1]
044:#define $tabelle$leftBracket1$rightBracket loacalData->parameters[0]
045:#define $tabelle loacalData->parameters[0]
046:#define $k loacalData->algebraics[0]
267:int functionDAE_output()
268:{
273: $x = tabelle[k];
274:
275: $y = $tabelle$leftBracket2$rightBracket;
281:}
399:int initial_residual()
400:{
406: localData->initialResiduals[i++] = ($k - 1);
407: localData->initialResiduals[i++] = $derivative$$dumy;
408: localData->initialResiduals[i++] = ($x -
(*real_arry_element_addr1(&tabelle, 1, k)));
409: localData->initialResiduals[i++] = ($y - $tabelle$leftBracket2$rightBracket);
Now You find the undeclared identifier tabelle and k in the function
functionDAE_when() and in the function initial_residual().
I think it is not the missing prefix "parameter" in the in the row
"Integer k = 1;" that make the problem.
And now the end is near, my workaround
The FunCounterTabIndex.mo OpenModelica source code file:
function ftabindex
input Integer x;
output Real y;
Real curvetab[4];
algorithm
curvetab[1] := 3.0;
curvetab[1] := 1.5;
curvetab[1] := 5.0;
curvetab[1] := -1.5;
y := curvetab[x];
end ftabindex;
class FunCounterTabIndex
parameter Real first = 0;
parameter Real interval = first + 1;
Integer n(start = 1);
Real b(start = 3.0 , fixed = true);
equation
when sample(first, interval) then
n = (if pre(n)+1 == 5 then 1 else pre(n)+1);
b = ftabindex(n);
end when;
end FunCounterTabIndex;
In the OMShell
loadFile("FunCounterTabIndex.mo")
true
instantiateModel(FunCounterTabIndex)
"fclass FunCounterTabIndex
parameter Real first = 0;
parameter Real interval = 1.0 + first;
Integer n(start = 1);
Real b(start = 3.0 , fixed =true);
equation
when sample(first, interval) then
n = (if pre(n)+1 == 5 then 1 else pre(n)+1);
b = ftabindex(n);
end when;
end FunCounterTabIndex;
"
simulate(FunCounterTabIndex,startTime=0, stopTime=8)
record
resultFile = "FunCounterTabIndex_res.plt"
end record
The error.log file
1:FunCounterTabIndex.cpp: In function `int function_when(int)':
2:FunCounterTabIndex.cpp:400: warning: converting to int' from
double'
FunCounterTabIndex.cpp file generated from omc (excerpts)
370:int function_when(int i)
371:{
396: case 1:when sample($first,$interval)
397:
398: save($b);
399:
400: tmp3 = _ftabindex($n);
401: $b = tmp3.ftabindex_rettype_1;
With this workaround the things are runing. My workaround shall show that
the indexing in a class is source of the C file compiling error. The mentioned
class "WillowForest" is runing in OpenModelica? I didn't try it, but my class
"BubCounterTabIndex" has the same shape. I hope my examples are correct.
I would be glad if You could help me.
regards,
Robert Wotzlaw
comment:5 by , 18 years ago
Dear Mr. Peter Aronsson,
now something new. The first example I tried with OpenModelica OMC was
the class "SamplingClock". You find this class in Peter Fritzson book
"Principles of Object-Oriented Modeling and Simulation with Modelica 2.1",
chapter 13, page 415.
class SamplingClock
parameter Real first = 0;
parameter Real interval = first + 1;
Boolean clock;
equation
clock = sample(first, interval);
when clock then
...
end when;
end SamplingClock;
I integrated the following rows in the class FunCounterTabIndex and renamed
FunCounterTabIndex into BooCounterTabIndex:
1: Boolean clock;
2: clock = sample(first, interval);
3: when clock then
The BooCounterTabIndex.mo OpenModelica source code file:
function ftabindex
input Integer x;
output Real y;
Real curvetab[4];
algorithm
curvetab[1] := 3.0;
curvetab[1] := 1.5;
curvetab[1] := 5.0;
curvetab[1] := -1.5;
y := curvetab[x];
end ftabindex;
class BooCounterTabIndex
parameter Real first = 0;
parameter Real interval = first + 1;
Integer n(start = 1);
Real b(start = 3.0 , fixed = true);
Boolean clock; <--- the first row
equation
clock = sample(first, interval); <--- the second
when clock then <--- the third
n = (if pre(n)+1 == 5 then 1 else pre(n)+1);
b = ftabindex(n);
end when;
end BooCounterTabIndex;
In the OMShell
loadFile("BooCounterTabIndex.mo")
true
instantiateModel(BooCounterTabIndex)
"fclass BooCounterTabIndex
parameter Real first = 0;
parameter Real interval = 1.0 + first;
Integer n(start = 1);
Real b(start = 3.0 , fixed =true);
Boolean clock;
equation
clock = sample(first, interval);
when clock then
n = (if pre(n)+1 == 5 then 1 else pre(n)+1);
b = ftabindex(n);
end when;
end BooCounterTabIndex;
"
simulate(BooCounterTabIndex,startTime=0, stopTime=8)
record
resultFile = "Simulation failed
"
end record
The error.log file
01:BooCounterTabIndex.cpp: In function `int handleZeroCrossing(long int)':
02:BooCounterTabIndex.cpp:340: error: `sample_rettype' undeclared (first use
this function)
03:BooCounterTabIndex.cpp:340: error: (Each undeclared identifier is reported
04: only once for each function it appears in.)
05:BooCounterTabIndex.cpp:340: error: parse error before `;' token
06:BooCounterTabIndex.cpp:348: error: `tmp0' undeclared (first use this function)
07:BooCounterTabIndex.cpp: In function `int function_when(int)':
08:BooCounterTabIndex.cpp:405: warning: converting to int' from
double'
09:BooCounterTabIndex.cpp: In function `int initial_residual()':
10:BooCounterTabIndex.cpp:440: error: parse error before `;' token
11:make: * [BooCounterTabIndex] Error 1
12:-build_model failed
BooCounterTabIndex.cpp file generated from omc (excerpts)
041:#define $interval localData->parameters[1]
042:#define $first localData->parameters[0]
043:#define $clock localData->algebraics[2]
044:#define $b localData->algebraics[1]
045:#define $n localData->algebraics[0]
337:int handleZeroCrossing(long index)
338:{
339: state mem_state;
340: sample_rettype tmp0;
345: case 0:
346:
347: save($clock);
348: tmp0 = sample(first, $interval);
349: $clock = tmp0;
359:}
375:int function_when(int i)
376:{
377: pre_rettype tmp0;
378: modelica_boolean tmp1;
379: pre_rettype tmp2;
380: ftabindex_rettype tmp3;
381: state mem_state;
386: case 0:when $clock
387:
388: save($n);
389:
390: tmp0 = pre($n);
405: tmp3 = _ftabindex($n);
406: $b = tmp3.ftabindex_rettype_1;
438:int initial_residual()
439:{
440: sample_rettype tmp0;
441: int i=0;
446: tmp0 = sample($first, $interval);
The C compiler can't compile the source, because the internal function sample()
has no typedef for the sample_rettype in the include file modelica.h.
Now I have a question concerning the OpenModelica generated makefile. I found
the library parameter -lg2c in the makefile. Is this correct? I found only
the following libraries libc_runtime.a, libf2c.a, libf2c.lbc, libf2c.sy and
libsim.a in the omc library directory. Should the parameter -lg2c called -lf2c
instead.
regards,
Robert Wotzlaw
comment:6 by , 18 years ago
I added sample_rettype in modelica.h to be of type modelica_real.
This will fix some of your problems.
comment:7 by , 14 years ago
The code posted in the comments seems to run in the current omc, so I am closing this bug.
The platform and compiler
Debian Sarge Linux Knoppix Quantian with GNU GCC 3.3 and OpenModelica 1.4.0
The source file TabIndex.mo
class TabIndex
equation
end TabIndex;
Inside OMShell
instantiateModel(TabIndex)
" fclass TabIndex
Real tabelle[1];
Real tabelle[2];
Real x;
Real y;
Integer k;
equation
end TabIndex
"
simulation(TabIndex,startTime=0,stopTime=%)
record
"
end record
The error.log file
TabIndex.cpp: In function `int functionDAE_output()';
TabIndex.cpp: 279 error: `tabelle' undeclared(first use this function)
TabIndex.cpp: 279 error: (Each undeclared identifier is reported only once for
each function it appears in.)
TabIndex.cpp: 279 error: `k' undeclared(first use this function)
make: *[TabIndex]Error1
_build_model faild
The TabIndex.cpp excerpts
043:#define $k localData->algebraics[2]
046:#define $tabelle localData->algebraics[0]
269:int functionDAE_output()
270:{
279: $x = tabelle[k];
280:
281: $y = $tabelle$leftBracket2$rightBracket;
287:}
407:initial_residual()
408:{
418: localData->initialResiduals[i++]=(&x -
(*real_arry_element_addr1(&tabelle,1,k)));
419: localData->initialResiduals[i++]=(&y - $tabelle$leftBracket2$rightBracket);
424:}
I hope You see what I saw, the index tabel wan't compile.
regards,
Robert Wotzlaw