Opened 15 years ago
Last modified 15 years ago
#1502 closed defect (fixed)
pre operator cannot be compiled
| Reported by: | fh77 | Owned by: | fh77 |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | Version: | 1.7.0 | |
| Keywords: | Cc: | fh77, Frenkel TUD |
Description
Here I'm not sure if its a bug.
The pre operator cannot be compiled if it is applied on a
variable, which does not change during the runtime
(in the sample the value of i1.el.val is always 12)
If the value is incremented the pre operator can be
compiled:
Here is the small sample:
block tst4B
String k;
output ct i1;
output ct i2;
output ct i3;
Boolean clock;
Integer cntr;
initial equation
cntr=0;
equation
clock = sample(0,0.1);
i1.el.val=12;
i2.el.val=pre(cntr)+2;
i3.el.val=14;
algorithm
when clock then
cntr:=cntr+1;
end when;
when (pre(i1.el.val)<> i1.el.val) then
k:= f3(String(i1.el.val)+"abcdef185","eee1.txt");
end when;
end tst4B;
type Int16 = Integer;
record tstR
Int16 val;
end tstR;
connector ct
tstR el;
end ct;
function f3
input String s2;
input String s1;
import Modelica.Utilities.Streams;
output String s3;
algorithm
Streams.print(s1 + s2 + "xyz" ,"eee1.txt");
s3:=s1+s2;
end f3;
Change History (8)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
Ok, this is due to known variable eliminiation...
tmp1023 = pre((modelica_integer) 12);
This means when we substitute i1.el.val=12, we also need to substitute pre(i1.el.val)=12 (maybe also der(i1.el.val) = 0). This would be good to do since further optimizations could be performed.
Adding Jens to the ticket :)
comment:3 by , 15 years ago
Why not add the rule "pre(const) = const" to ExpressionSimplify.simplify
comment:4 by , 15 years ago
I was considering doing that as well. It would be good if we changed simplify from bottom-up to top-down+bottom-up, since then we could also record if subexpressions are constant without traversing subexpressions again...
The question is... Do we ever perform any substitution of pre-variable to non-constant expressions?
comment:5 by , 15 years ago
From the theoretical side yes, for example alias variables, "a=-b, pre(a) = pre(-b) = -pre(b)". To replace c=f(time) is not usefull. Maybe some information if the variable is used in pre is necesarry for the variable.
comment:6 by , 15 years ago
The alias variables, we can easily handle in ExpressionSimplify at least
comment:7 by , 15 years ago
I should probably open up a new bug for this optimization. I found it after adding the rule for pre(const)=const...
statement: when false then
k := f3("12abcdef185","eee1.txt");
end when;
[a.mo:19:1-21:9]
operations:
simplify: pre(12) <> 12 => false
subst: pre(i1.el.val) <> i1.el.val => pre(12) <> 12
/*#modelicaLine [a.mo:19:1-21:9]*/
localData->helpVars[0] = (0);
if (edge(localData->helpVars[0])) {
Clearly, this condition can never be fulfilled, so we should not generate code for it :)

Hi,
If you add three curly braces before and after the code it will keep the formatting, like this: