Main Content

State Reset Modeling

About State Reset

Event-based methods of state reinitialization and impulse handling let you model physical phenomena such as collisions and bouncing balls. Using these state reset methods provides a significant boost in simulation speed for such models, compared to continuous simulation.

To implement a state reset, use instantaneous modes and compound transitions in a mode chart. An instantaneous mode is a mode that is active only for one event iteration. You specify that a mode is instantaneous by using a compound transition:

A -> B -> C : t

The middle mode, B, is instantaneous. When predicate t becomes true, the system transitions from mode A to mode B, performs one event iteration, and then immediately transitions to mode C.

You declare instantaneous modes the same way as regular modes, by using the mode section of a mode chart. To specify that a mode is instantaneous, list it as the middle mode in a compound transition. Only one instantaneous mode is allowed per transition, therefore, a compound transition cannot contain more than three modes.

In the majority of state reset use cases, the reset value is a function of the previous value of the variable. For example, when modeling a bouncing ball, the new velocity depends on the velocity before impact. The entry section, which you declare within a mode section in a mode chart, lets you specify the actions to be performed upon entering the mode. These actions are event variable updates based on the value of a continuous expression immediately before entering the mode. When modeling a state reset, you can use entry actions to update the value of an event variable based on the value of the respective continuous variable immediately before entering the mode.

When you connect multiple ideal components that use state resets, the solver automatically detects and propagates impulses in continuous states during variable reinitialization. Impulse propagation can only trigger events whose predicates are linear expressions of continuous states. Also, impulse detection can add computational cost during transient initialization. Two options in the Solver Configuration block, Compute impulses and Impulse iterations, let you control the computational cost of impulse detection during transient initialization. If you use fixed-cost simulation for a model that contains components with state resets, select the Compute impulses check box to get the correct impulse propagation results.

State Reset Example

Use this simple example to understand how to model state resets.

The Translational Hard Stop block in the Simscape™ Foundation library models a hard stop as a spring and damper that come into contact with the slider at the bounds. In contrast, this example implements an ideal translational hard stop, where the slider velocity resets instantaneously upon hitting the upper or lower bound.

component ideal_hard_stop

nodes
    R = foundation.mechanical.translational.translational % R:left
    C = foundation.mechanical.translational.translational % C:right
end

parameters
    upper_bnd = { 0.1, 'm'} % Upper bound
    lower_bnd = {-0.1, 'm'} % Lower bound
    e = 0.8                 % Coefficient of restitution
end

variables
    v = {0, 'm/s'}   % Velocity
    f = {0, 'N'}     % Force
    x = {value = {0, 'm'}, priority = priority.high} % Position
end

variables(Event = true, Access = private, ExternalAccess = none)
    v_old = {0, 'm/s'}
end

branches
    f : R.f -> C.f
end

equations
    v == R.v - C.v
    x.der == v

    assert(e > 0);
    assert(e <= 1);
    assert(upper_bnd > 0);
    assert(lower_bnd < 0);
end

modecharts(ExternalAccess = observe)
  m = modechart
    modes
      mode FREE
        equations
          f == 0
        end
      end
      mode IMPACT
        entry
          v_old = v
        end
        equations
          v == -e*v_old
        end
      end
    end
    transitions
      FREE -> IMPACT -> FREE : x <= lower_bnd && v < 0
      FREE -> IMPACT -> FREE : x >= upper_bnd && v > 0
    end
  end
end

end

The mode chart m defines two modes:

  • FREE, when the slider travels freely between the bounds.

  • IMPACT, when the slider hits one of the bounds.

Each mode has an equations section that lists the equations applicable to that mode. The equations section outside the mode chart lists the asserts and equations that apply to both modes.

The transitions section defines two compound transitions, one for the slider hitting the lower bound and one for the upper bound. In each transition, when the predicate becomes true, the component switches from the FREE mode to IMPACT, and then back to FREE. IMPACT is an instantaneous mode.

When the component enters the IMPACT mode, the event variable v_old gets updated with the value of velocity before impact. This update action is defined in the entry section for that mode. Then, in the equations section for this mode, the velocity, v, is reset to a value that is a function of this previous velocity value and the coefficient of restitution, e.

The component implements separate transitions for the upper and lower bounds to improve code readability. The predicate for each of these compound transitions includes both the slider position and the velocity sign, to avoid entering a self-loop. Compound transitions follow the same rules as regular transitions. If a predicate is true, the system immediately enters the transition. Therefore, if you defined a compound transition based only on the slider position:

transitions
    FREE -> IMPACT -> FREE : x <= lower_bnd || x >= upper_bnd
end

the predicate could still be true after completing the transition, the system would enter an infinite loop and eventually generate an error. To avoid this situation, it is a good practice to try to model compound transitions in such a way that the instantaneous mode invalidates the predicate:

transitions
    FREE -> IMPACT -> FREE : x <= lower_bnd && v < 0
    FREE -> IMPACT -> FREE : x >= upper_bnd && v > 0
end

In this case, while in instantaneous mode, the velocity flips sign and the predicate is no longer valid.

For a more detailed example, see Mass on Cart Using an Ideal Hard Stop, which uses a custom Ideal Hard Stop block with additional options that cover a wider variety of use cases. That block has a more complex mode chart, but the modeling principles and the block behavior are similar.

See Also

| | | |

Related Topics