Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#4419 closed defect (fixed)

Bug in simplification of rem() or div()

Reported by: hkiel Owned by: lochel
Priority: blocker Milestone: 1.12.0
Component: Code Generation Version: v1.12.0
Keywords: Cc: sjoelund.se, adrpo, 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 Changed 7 years ago by hkiel

  • Cc sjoelund.se adrpo casella added

comment:2 Changed 7 years ago by hkiel

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 Changed 7 years ago by lochel

  • Component changed from Backend to Code Generation
  • Status changed from new to accepted

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

comment:4 Changed 7 years ago by lochel

  • Resolution set to fixed
  • Status changed from accepted to closed

Fixed with OMCompiler#1655.

comment:5 follow-up: Changed 7 years ago by hkiel

@lochel Can you add a test case for this?

comment:6 in reply to: ↑ 5 Changed 7 years ago by lochel

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.