Why is my model made of Simscape and Simscape Multibody simulating very slowly?

21 views (last 30 days)
I have a model made of Simscape and Simscape Multibody that simulates significantly slower than I would expect.
I ran the simulation through the Solver Profiler. For more information on the Solver Profiler, please run the following command in the command window of MATLAB R2020a to access the release-specific documentation:
>> web(fullfile(docroot, 'simulink/slref/solverprofiler.html'))
. The time step looks like the following picture. The step size remains small and chatters a lot. Also, in the Solver Profiler statistics, we see a large amount of newton iteration failures and Jacobian recalculation compared to the total number of steps.
When looking at the states contributing the most to newton iterations, they do not seem to change significantly fast, so capturing their behavior should not require such small step size.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Jan 2025
Edited: MathWorks Support Team on 4 Feb 2025
Here are steps to help investigate the performance of Simscape and Simscape Multibody simulations.

1. Solver Tolerances

The default tolerances for Simscape are sometimes not ideal for large Simscape models. Based on experience, we find the following usually works well for Simscape models:
  • For Relative tolerance, the default 1e-3 is usually a good first guess.
  • For Absolute Tolerance, specify a value one or two orders of magnitude smaller than the Relative Tolerance. The default "auto" is often too small for Simscape models.
  • Disable the option "Auto scale absolute tolerance." 

2. Solver Profiler

Simulate the model using the Solver Profiler. Enable logging of States and Zero Crossing, and Simscape Logging. In the Statistics pane, look for a large number of events relative to the total number of steps. Depending on which statistic shows a high number, try the following:
  • Zero-crossing: Use the Zero-Crossing Explorer to understand the reason for the zero-crossing sources triggering a large amount of time. Zero-crossing detection is necessary for simulation accuracy, to detect sudden changes accurately. To reduce the number of zero-crossing events, consider making the algorithm continuous, or look into modifying the dynamics of the system being modeled. See this article for an example where stiffness and damping used for contact modeling can be adjusted to compromise between performance and accuracy.
  • Solver Resets: One common source of solver resets is when a block running at a discrete rate drives a block with continuous states. When the value of the discrete signal changes, the solver needs to be reset to handle the discontinuity. In R2019b, a new block has been introduced in the continuous section of the Simulink Library. Please run the following command in the command window of MATLAB R2019b to access the release-specific documentation for the "First Order Hold" block:
    >> web(fullfile(docroot, 'simulink/slref/firstorderhold.html'))
    This block performs first-order interpolation of the discrete signal, making the signal to be integrated continuous, thus removing the need for a solver reset.
  • Solver Exceptions: If you see a large number of solver exceptions, examine the states with the largest numbers using the States Explorer. In the States Explorer, sort by solver exceptions. Verify if the signals are as expected, or if they contain unexpected numerical noise. If that's the case, see the next items.

3. States Scaling using Simscape Nominal Values

In the States Explorer, rank the states by State Value. If the first states have significantly large values, consider scaling the system using nominal values. To access the release-specific documentation for scaling with nominal values, run the following command in the MATLAB R2020a command window: 
>> web(fullfile(docroot, 'physmod/simscape/ug/system-scaling-by-nominal-values.html')
In the model configuration, it is possible to scale all the states with a specific unit, and using the Property Inspector, it is possible to scale individual states of a specific block.
In general, solvers behave better when states are scaled within the -1 to 1 range.
 

4. Jacobian Perturbation

We have found that for some models, limiting the level of perturbation used to compute the solver Jacobian can improve performance. Using the attached function "limitJacobianPerturbations," it is possible to limit the amplitude of this perturbation. Use it in the following ways.
limitJacobianPerturbations('recommended')
This will limit the numerical perturbation to a minimum of "sqrt(eps)" (1.4901e-08). We found that this value works well with a majority of impacted models.
limitJacobianPerturbations('reset')
This will restore factory settings.
limitJacobianPerturbations('custom',[lowerLimit upperLimit])
Allows you to specify custom limits. In some cases, the recommended setting ends up being still too small, and increasing the lower limit to values like 1e-6 or 1e-5 helps. Setting the lower limit too large can lead to less accurate results. Changing the upper limit typically does not have a significant impact; we recommend setting it to "inf" or a large number.

5. Modeling Issues

If none of the above works, use the Solver Profiler and associated tools like the States Explorer and Zero-Crossing Explorer to try identifying modeling issues. Start with the states ranked highest in the different tables, and then use the Simscape Explorer to observe surrounding states in the model to confirm that the results are what you expect.
Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!