using reinit() in "when" statements algorithms - Stack Overflow most recent 30 from stackoverflow.com 2026-04-14T22:30:21Z https://stackoverflow.com/feeds/question/79921389 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/79921389 0 using reinit() in "when" statements algorithms AngeliqueR https://stackoverflow.com/users/19013021 2026-04-07T08:47:45Z 2026-04-07T15:51:47Z <p>I have a piece of modelica code in a long <code>when-elsewhen</code> statement that uses <code>reinit()</code> for a continuous time variable and with <code>if-elseif</code> embedded in an <code>equation</code> section. In some cases, if a specific condition occurs, then I'm using the kind of code below to do nothing and have the same variables assignments as with other sections of the <code>when</code> and <code>if</code> sections:</p> <pre><code>variable = pre(variable) ; </code></pre> <p>It seems that Dymola rewrites my equations in the form (I try to reproduce in a very synthetic form the code I'm using):</p> <pre><code>condition1 = pre(variable1); ... conditionN = .... ; when {condition1, ..., consitionN}  then variable1 = if NothingToDoCondition then pre(variable)  else value; variable2 = if NothingToDoCondition then pre(variable)  else anotherValue; .... variableM =... //as above if condition then reinit(x,y) ; else //no equation end if ; elsewhen .... // repeat as above end when; </code></pre> <p>The Dymola check complains that:</p> <pre><code>This elsewhen in equations is not supported, it can be worked around by rewriting as algorithm, however be careful since the right-hand-side is dependent on left-hand-side variables and that is handled differently in algorithms </code></pre> <p>First, I don't know how to convert my piece of code into an <code>algorithm</code> section since I'm using <code>reinit()</code>, which cannot be used in the <code>algorithm</code> section. Is there an alternative to the <code>reinit()</code> that I can use in an <code>algorithm</code> section?</p> <p>Second, how to interpret the message from the Dymola check ? What's the implications for using a <code>when</code> statement in an <code>algorithm</code> or <code>equation</code> ?</p> https://stackoverflow.com/questions/79921389/-/79921671#79921671 2 Answer by Hans Olsson for using reinit() in "when" statements algorithms Hans Olsson https://stackoverflow.com/users/5603247 2026-04-07T15:51:47Z 2026-04-07T15:51:47Z <p>There's an alternative to using algorithms.</p> <p>In general</p> <pre><code>equation when {condition1, ..., conditionN} then variable1 = if NothingToDoCondition then pre(variable1) else value11; variable2 = if NothingToDoCondition then pre(variable2) else value12; .... variableM =... //as above if condition then reinit(x,y) ; else //no equation end if ; elsewhen .... variable1 = if NothingToDoCondition then pre(variable1) else value21; variable2 = if NothingToDoCondition then pre(variable2) else value22; .... variableM =... //as above end when; </code></pre> <p>is equivalent to:</p> <pre><code>equation when {condition1, ..., conditionN} then variable1 = if NothingToDoCondition then pre(variable1) else value11; elsewhen .... variable1 = if NothingToDoCondition then pre(variable1) else value21; end when; when {condition1, ..., conditionN} then variable2 = if NothingToDoCondition then pre(variable2) else value12; elsewhen .... variable2 = if NothingToDoCondition then pre(variable2) else value22; end when; ... when {condition1, ..., conditionN} then variableM = if NothingToDoCondition then pre(variableM) else value1M; elsewhen .... variableM = if NothingToDoCondition then pre(variableM) else value2M; end when; when {condition1, ..., conditionN} then if condition then reinit(x,y) ; else //no equation end if ; end when; </code></pre> <p>or variants thereof - it might suffice to split off the reinit.</p> <p>And Dymola should be able to do this automatically (depending on version) as far as I can tell. However, if variables don't appear in the same form in all of the branches that may fail - and in that case just splitting off the reinit would likely work.</p>