Question

I try to compile the following code with Dymola:

class abc
  import Modelica.SIunits;
  parameter SIunits.Time delta_t=0.5;

  constant Real a[:]={4,2,6,-1,3,5,7,4,-3,-6};
  Real x;
  Integer j(start=1);
  Integer k=size(a, 1);

algorithm 
  when {(sample(0, delta_t) and j < k),j == 1} then
    x := a[j];
    j := j + 1;
  end when;
end abc;

and for time = 0 the variable j starts with 2. But it should start with j = 1.
Does anybody have an idea for this problem?

Was it helpful?

Solution

Keep in mind that sample(x,y) means that sample is true at x+i*y where i starts at zero. Which is to say that sample(0, ...) becomes true at time=0.

Since j starts at 1 and k is presumably more than 1, it doesn't seem unexpected to me that sample(0, delta_t) and j<k should become true at the start of the simulation.

I suspect what you want is:

class abc
  import Modelica.SIunits;
  parameter SIunits.Time delta_t=0.5;

  constant Real a[:]={4,2,6,-1,3,5,7,4,-3,-6};
  Real x;
  Integer j(start=1);
  Integer k=size(a, 1);

algorithm 
  when {(sample(delta_t, delta_t) and j < k),j == 1} then
    x := a[pre(j)];
    j := pre(j) + 1;
  end when;
end abc;

I don't really see the point of the j==1 condition. It is true at the outset which means it doesn't "become" true then. And since j is never decremented, I don't see why it should ever return to the value 1 once it increments for the first time.

Note that I added a pre around the right-hand side values for j. If this were in an equation section, I'm pretty sure the pre would be required. Since it is an algorithm section, it is mainly to document the intent of the code. It also makes the code robust to switching from equation to algorithm section.

OTHER TIPS

Of course, there is an event at time = 0 triggered by the expression sample(0, delta_t) and j<k which becomes true.

But in older versions of Dymola there is an bug with the initialization of discrete variables. For instance even if you remove sample(0.0, delta_t) and j<k in dymola74, j will become 2 at time=0. The issue was that the pre values of when clauses, where not initialized correct. As far as I know this is corrected at least in the version FD1 2013.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top