Technical Articles

Speed Up Your Simulations with Rapid Accelerator Mode

By Guy Rouleau, MathWorks


If you are doing Monte Carlo simulation or system optimization on your Simulink® model, you’ll need to run multiple simulations while varying model parameters. How do you get each simulation to run as quickly as possible?

In Simulink, there are three desktop simulation modes: Normal, Accelerator, and Rapid Accelerator. For most models, Accelerator is faster than Normal, and Rapid Accelerator is faster still. Rapid Accelerator mode speeds up simulation by generating an executable for your model. The exact speedup varies depending on the model, but Rapid Accelerator can be more than 10 times faster than Normal mode.

Using Rapid Accelerator involves trading off flexibility and performance—Rapid Accelerator does not support the debugger or profiler, and it only works when the entire model is capable of generating C code—but if your goal is efficient batch and Monte Carlo simulations, then Rapid Accelerator is your best option.

This article shows how to set up your batch simulation script to get the maximum performance out of Rapid Accelerator.

Setting Up the Model—A Bouncing Ball Example

Each time you run a model or use the sim command, Simulink checks for changes in the model. Depending on the size of the model, this initialization phase can be time-consuming. In Rapid Accelerator mode, if you know that your model did not change structurally, you can skip this step by simply turning off the RapidAcceleratorUpToDateCheck option. For any model, no matter how large, this reduces initialization time to zero.

For illustration, we’ll use a simple model of a bouncing ball (Figure 1). In this model, a ball drops from a certain height onto a hard surface. The position and velocity of the ball are measured at each step of the simulation. The parameters are gravity, the ball’s initial height and velocity, and the coefficient of restitution (the bounciness of the ball).

A diagram showing the parameters for the bouncing ball model.
Figure 1. Bouncing ball model.

Suppose we want to vary the coefficient of restitution, specified as k in the block dialog.

Define the vector of values to be simulated:

k_values = [-0.9:0.1:-0.1];

Create an array of Simulink.SimulationInput objects that will tell Simulink to:

  • Simulate the model in Rapid Accelerator mode.
  • Skip the Rapid Accelerator up-to-date check.
  • Use the coefficient of restitution values from the above vector.
mdl = 'sldemo_bounce'; 
in(1:length(k_values)) = Simulink.SimulationInput(mdl); 
for i = 1:length(k_values) 
    in(i) = in(i).setModelParameter('SimulationMode','Rapid'); 
    in(i) = in(i).setModelParameter('RapidAcceleratorUpToDateCheck','off'); 
    in(i) = in(i).setVariable('k',k_values(i)); 
end

We are now ready to simulate the model multiple times in Rapid Accelerator mode using the sim command:

out = sim(in);

This will return an array of Simulink.SimulationOutput objects containing the simulation results.

Using this workflow with parallel computing is straightforward. All you do is replace sim with parsim in the above line of code.

If you modify the model and need to rebuild the Rapid Accelerator target, you can use Simulink.BlockDiagram.buildRapidAcceleratorTarget.

Rapid Accelerator mode is just one way to speed up simulations. You can also use the Performance Advisor, which lets you analyze your model for simulation bottlenecks, or Fast Restart, which lets you perform iterative simulations without recompiling the model or terminating the simulation each time.

Published 2023 - 92303v00

View Articles for Related Capabilities