Wolfram Language

Click for copyable input


Reinitialize state variable


reinit(x, expr)


The operator reinitializes x with expr at an event instant. x is a Real variable (or an array of Real variables) that must be selected as a state (resp., states), that is reinit on x implies stateSelect = StateSelect.always on x. expr needs to be type-compatible with x. The reinit operator can for the same variable (resp. array of variables) only be applied (either as an individual variable or as part of an array of variables) in one equation (having reinit of the same variable in when and else-when of the same variable is allowed). The reinit operator can only be used in the body of a when-equation. It cannot be used in an algorithm section.

The reinit operator does not break the single assignment rule, because reinit(x,expr) in equations evaluates expr to a value (value), then at the end of the current event iteration step it assigns this value to x (this copying from values to reinitialized state(s) is done after all other evaluations of the model and before copying x to pre(x)).

[If a higher index system is present, that is constraints between state variables, some state variables need to be redefined to non-state variables. During simulation, non-state variables should be chosen in such a way that variables with an applied reinit operator are selected as states at least when the corresponding when-clauses become active. If this is not possible, an error occurs, since otherwise the reinit operator would be applied on a non-state variable.]


// Bouncing ball
   parameter Real e=0.5 "Coefficient of restitution"
   Real h, v;
   Boolean flying;
   der(h) = v;
   der(v) = if flying then -g else 0;
   flying = not (h<=0 and v<=0);

   when h < 0 then
     reinit(v, -e*pre(v));
   end when;