I have two comments. First, keep in mind that when working with when clauses, it is often wise to use the pre(...) operator to explicitly indicate which value you are referring to, new or previous. I donβt know if this is strict in this case (and even if I knew, Iβm not sure that all Modelica tool developers use consistent semantics), but this is a good idea, if only to make the readers of the code understand.
Another problem is the processing of algorithms. Typically, statements are executed in the order specified in the algorithm section. But when clauses are a bit complicated, as they are asynchronous. Again, I donβt quite understand what exact semantics are regarding the alternation of when clauses with other assignment operators in the model, but the record of the algorithm section, as you have, is a bit ambiguous. Suppose the when statement evaluates and x gets a new value, how long do you expect it to keep this value? The compiler has a breadth in the multiple evaluation of the algorithm section, which can be evaluated immediately after the when clause is called, in which case x will be assigned a new value of 5 (possibly even without any time).
I'm not sure what your intention is with this model. But if you want it to start with one value, and then, after a time> 3, take a new value (based on the old value), I see two ways to achieve this in Modelica, and both of them are related to how you set the initial cost. Consider the following model:
model InitAndEvent discrete Integer x(start=1); algorithm when initial() then x := 5; end when; when time >= 3 then x := pre(x) + 5; end when; end InitAndEvent;
This model uses the pre operator, but, more importantly, it also uses the initial() event to provide an initial value for x . Thus, this avoids the problem that you probably encountered with the previous model, constantly rewriting the value of x to 5 . Another way to do this is to:
model InitAndEvent discrete Integer x(start=1); initial algorithm x := 5; algorithm when time >= 3 then x := pre(x) + 5; end when; end InitAndEvent;
Although I admit that I suspect that different tools may have different semantics for each of these versions. I would say that the when initial() version is probably more universal.
Update:
I think that your misunderstanding comes from the fact that you are not considering the consequences of subsequent assessments. In case of try1 , at the moment == 3, the expression when is evaluated and x gets a new value.
But what happens during the next evaluation of the model? The algorithm section is evaluated again , and x set to 5 . The when clause does not check until the condition expression becomes false and then returns again. Thus, in this case, it works exactly once! If you want it to be evaluated at all times greater than 3, you need to use the if .
And remember this question ... how long is the simulation between the execution of the when clause and the next model evaluation (when x is reset)? Probably none . You do not have the opportunity to find out when the algorithm section is running (which depends on the events, the integrator used, etc.). Therefore, if you want x be set once at the beginning of the simulation and once at time==3 , you need to specify this in your model (as it was in my examples).