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.