Opened 9 years ago

Closed 3 years ago

#3284 closed defect (fixed)

Problem with "when+reinit" when using vectors

Reported by: hkiel Owned by: somebody
Priority: high Milestone: 1.19.0
Component: Backend Version: trunk
Keywords: Cc:

Description

The following model implements the bouncing ball model with
1) scalars
2) array of arbitrary length (e.g. 1)

Though both models flatten to the same code (except for the "[1]" where appropriate) the model result is different!

model reinit_test
  parameter Integer n = 1;
  parameter Real g = 9.81;
  parameter Real e = 0.9;
  Real v[n](start = 10 * (0:n - 1));
  Real h[n](each start = 10);
  Boolean flying;
  Real v_(start = 0);
  Real h_(start = 10);
  Boolean flying_;
equation
  der(h) = v;
  der(v) = if flying then -g * ones(n) else zeros(n);
  flying = not (h[1] <= 0 and v[1] <= 0);
  when h[1] < 0 then
    reinit(v, -e * pre(v));
  end when;
  der(h_) = v_;
  der(v_) = if flying_ then -g else 0;
  flying_ = not (h_ <= 0 and v_ <= 0);
  when h_ < 0 then
    reinit(v_, -e * pre(v_));
  end when;
  annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.002));
end reinit_test;

Change History (3)

comment:1 Changed 9 years ago by hkiel

  • Component changed from Backend to Code Generation
  • Owner changed from somebody to lochel

I suspect the error to be (at least) in CodeGeneration.

When comparing the C output I find a difference in how the when condition is done. For the array code there is no hysteresis used:

 /*
  equation index: 10
  type: SIMPLE_ASSIGN
- $whenCondition1 = h < 0.0
+ $whenCondition1 = h[1] < 0.0
  */
 void reinit_test_eqFunction_10(DATA *data)
 {
   TRACE_PUSH
   const int equationIndexes[2] = {1,10};
   modelica_boolean tmp4;
-  RELATIONHYSTERESIS(tmp4, $Ph, 0.0, 2, Less);
+  tmp4 = Less($Ph$lB1$rB,0.0);
   $P$whenCondition1 = tmp4;
   TRACE_POP
 }

comment:2 Changed 9 years ago by hkiel

  • Component changed from Code Generation to Backend
  • Owner changed from lochel to somebody

Looking deeper into CodegenC.tpl, I see that the relation has no index (=-1), so it's probably the fault of the Backend.

comment:3 Changed 3 years ago by casella

  • Milestone changed from Future to 1.19.0
  • Resolution set to fixed
  • Status changed from new to closed

Works in 1.19.0

Note: See TracTickets for help on using tickets.