@@ -136,6 +136,9 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
136136 double epsilon;
137137 // These must be accessed via a pointer to localVal
138138 double timeValue, $P$old$timeValue;
139+ // Helping variables for when clauses
140+ bool helpVars[<%vi.numHelpVars%>], helpVars_saved[<%vi.numHelpVars%>];
141+ const int numHelpVars() const { return <% vi.numHelpVars%> ; }
139142
140143 void save_vars();
141144 void restore_vars();
@@ -356,7 +359,15 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
356359 {
357360 <% (zeroCrossings |> ZERO_CROSSING(__) hasindex i0 =>
358361 ' if (state_event[<%i0%>]) zc<%i0%> = !zc<%i0%>;' ) ; separator= " \n " %>
362+ // Record the values of the when clauses before the event
363+ for (int i = 0; i < numHelpVars(); i++)
364+ helpVars_saved[i] = helpVars[i];
359365 calc_vars(q);
366+ // Update the values of the when clauses to the new values
367+ for (int i = 0; i < numHelpVars(); i++)
368+ helpVars[i] = helpVars_saved[i];
369+ // Reinitialize state variables that need to be reinitialized
370+ <% (vars.stateVars |> SIMVAR(__) => ' q[<%index%>]=<%cref(name)%>;' ) ;separator= " \n " %>
360371 }
361372
362373 >>
@@ -425,6 +436,8 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
425436 <% initVals(vars.intParamVars)%>
426437 <% initVals(vars.boolParamVars)%>
427438 <% (zeroCrossings |> ZERO_CROSSING(__) hasindex i0 => ' zc<%i0%> = -1;' ) ; separator= " \n " %>
439+ for (int i = 0; i < numHelpVars(); i++)
440+ helpVars_saved[i] = false ;
428441 // Calculate any equations that provide initial values
429442 <% makeInitialEqns(initialEquations)%>
430443 // Calculate derived values
@@ -1390,23 +1403,6 @@ template functionWhenReinitStatementThen(list<WhenOperator> reinits, Text &varDe
13901403 >>
13911404end functionWhenReinitStatementThen;
13921405
1393- //Pavol: this one is never used, is it obsolete ??
1394- template functionWhenReinitStatementElse(list<WhenOperator > reinits, Text &preExp /*BUFP*/,
1395- Text &varDecls /*BUFP*/)
1396- "Generates re-init statement for when equation."
1397- ::=
1398- let body = (reinits |> reinit =>
1399- match reinit
1400- case REINIT(__) then
1401- let val = daeExp(value, contextSimulationDiscrete,
1402- &preExp /*BUFC*/, &varDecls /*BUFD*/)
1403- '<%cref(stateVar)%> = $P$PRE<%cref(stateVar)%>;';separator="\n"
1404- )
1405- <<
1406- <%body%>
1407- >>
1408- end functionWhenReinitStatementElse;
1409-
14101406template functionODE(list<SimEqSystem > derivativEquations, Text method)
14111407 "Generates function in simulation file."
14121408::=
0 commit comments