Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have two comments. First, keep in mind that when dealing with <code>when</code> clauses, it is often prudent to utilize the <code>pre(...)</code> operator to make explicitly clear which value you are referring to, the new one or the previous. I don't know if it is strictly necessary in this case (and even if I did know, I'm not sure if all developers of Modelica tools use consistent semantics), but it is a good idea if only to make it clear to readers of the code.</p> <p>The other issue is with the handling of algorithms. Normally, the statements are executed in the order they appear in the <code>algorithm</code> section. But <code>when</code> clauses are a bit tricky since they have an asynchronous nature to them. Again, I'm not clear what the exact semantics are with regards to the interleaving of <code>when</code> clauses with other assignment statements in the model, but writing an <code>algorithm</code> section like you have is a bit ambiguous. Let's assume the <code>when</code> statement is evaluated and <code>x</code> is given a new value, how long do you expect it to keep that value? The compiler has latitude in repeatedly evaluating the algorithm section might be evaluated immediately after the <code>when</code> clause is invoked in which case <code>x</code> will be given a new value of <code>5</code> (perhaps even without time advancing at all).</p> <p>I'm not sure what your intention really is with this model. But if you want it to start with one value and then, after time>3, take on a new value (based on the old value), I see two ways of achieving this in Modelica and both of them involve how you set the initial value. Consider the following model:</p> <pre><code> model InitAndEvent discrete Integer x(start=1); algorithm when initial() then x := 5; end when; when time &gt;= 3 then x := pre(x) + 5; end when; end InitAndEvent; </code></pre> <p>This model uses the <code>pre</code> operator but, more importantly, it also uses the <code>initial()</code> event to provide the initial value for <code>x</code>. In this way, this avoids the problem you were probably experiencing with the previous model of constantly overwriting the value of <code>x</code> to <code>5</code>. Another way to do this could be:</p> <pre><code> model InitAndEvent discrete Integer x(start=1); initial algorithm x := 5; algorithm when time &gt;= 3 then x := pre(x) + 5; end when; end InitAndEvent; </code></pre> <p>Although I confess that I suspect different tools might have different semantics for each of these versions. I'd say the <code>when initial()</code> version is probably more universally consistent.</p> <p><strong>Update:</strong></p> <p>I think your misunderstanding comes from not considering the implications of <em>subsequent</em> evaluations. In the case of <code>try1</code>, at time==3 the <code>when</code> clause is evaluated and <code>x</code> is given a new value.</p> <p>But what happens on the next model evaluation? The algorithm section is evaluated again <strong>and <code>x</code> is set back to 5</strong>. The <code>when</code> clause <strong>does not</strong> get evaluated again until the condition expression turns false and then true again. So in this case, it triggers exactly once! If you want it to be evaluated for all times greater than 3, then you need to use an <code>if</code> statement.</p> <p>And keep this question in mind...how much simulation time elapses between the execution of the <code>when</code> clause and the next model evaluation (when <code>x</code> is reset)? Quite possibly <strong>none</strong>. You have no way of knowing when the algorithm section is executed (that depends on events, the integrator used, etc.). So if you want <code>x</code> to be given a value once at the start of the simulation and once at <code>time==3</code>, then you need to state that in your model (as I did in my examples).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload