Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#4419 closed defect (fixed)

Bug in simplification of rem() or div()

Reported by: Henning Kiel Owned by: Lennart Ochel
Priority: blocker Milestone: 1.12.0
Component: Code Generation Version: v1.12.0
Keywords: Cc: Martin Sjölund, Adrian Pop, Francesco Casella

Description

These two models behave wrong (and even differently):

model test1
  Real x(start=0,fixed=true);
  Real y1,y2;
equation
  der(x)=10;
  y1 = rem(x,2);
  y2 = rem(x+1,2);
end test1;
model test2
  Real x(start=0,fixed=true);
  Real y2;
equation
  der(x)=10;
  y2 = rem(x+1,2);
end test2;

Model test1 shows sawtooth, but not in the correct value range of 0..2, but in 1..3 for y2.

Model test2 does not show any sawtooth for y2, it is just plain ramp x+1.

Also the flattened code is not valid Modelica x + -2.0
instantiateModel(test1)

"class test1
  Real x(start = 0.0, fixed = true);
  Real y1;
  Real y2;
equation
  der(x) = 10.0;
  y1 = x + -2.0 * div(x, 2.0);
  y2 = 1.0 + x + -2.0 * div(1.0 + x, 2.0);
end test1;
"

I think that simplification does wrong things. The (syntactically fixed) model from instantiateModel(test1) is also simulated wrong, so maybe the problem lies within div simplifications.

Change History (6)

comment:1 by Henning Kiel, 8 years ago

Cc: Martin Sjölund Adrian Pop Francesco Casella added

comment:2 by Henning Kiel, 8 years ago

This seems to be a problem with zero crossing detection in div().
This model behaves wrong:

model d
  Real x(start=0,fixed=true);
  Real y;
equation
  der(x)=10;
  y=div(1.0+x,2.0);
end d;

this model behaves correct using noEvent() around the div():

model d
  Real x(start=0,fixed=true);
  Real y;
equation
  der(x)=10;
  y=noEvent(div(1.0+x,2.0));
end d;

However, adding noEvent() around the rem() in the initial problem does not fix it. It even makes things look worse...

comment:3 by Lennart Ochel, 8 years ago

Component: BackendCode Generation
Status: newaccepted

Thanks for this very good report. I will fix the issue.

comment:4 by Lennart Ochel, 8 years ago

Resolution: fixed
Status: acceptedclosed

Fixed with OMCompiler#1655.

comment:5 by Henning Kiel, 8 years ago

@lochel Can you add a test case for this?

in reply to:  5 comment:6 by Lennart Ochel, 8 years ago

Replying to hkiel:

@lochel Can you add a test case for this?

Test added with OpenModelica-testsuite#640.

Note: See TracTickets for help on using tickets.