Improving Speed and Accuracy
Speed and accuracy are the determining factors for making your model real-time capable. Your model is real-time capable when it meets your criteria for accuracy with no overruns. The real-time clock determines whether your model is fast enough for real-time simulation. For each step that the solver takes, your real-time hardware system tracks the time that it takes to complete these processing tasks:
Execute the simulation.
Process input and output.
Perform general computer tasks.
An overrun occurs when, for any time step, the time that it takes your system to complete the processing tasks exceeds the real-time limit for the tasks. If your target machine reports any overruns when you use it to simulate your model, your model is not fast enough for real-time simulation.
When determining whether the model meets your criteria for accuracy, consider these questions:
Is the model representing the phenomena that you want it to measure?
Is it representing those phenomena correctly?
If you plan to use your model to test your controller design, is the model accurate enough to produce results that you can rely on for system qualification?
Balancing Speed and Accuracy
Simulation speed and accuracy correlate to your choices for:
Model fidelity and scope
Real-time hardware computing power
Solver sample time (step size) and number of iterations
To try to increase simulation speed, potentially at the expense of accuracy:
Decrease model fidelity or scope.
Increase sample time.
Decrease the number of solver iterations.
To try to increase simulation accuracy, potentially at the expense of speed:
Increase model fidelity or scope.
Decrease sample time.
Increase the number of solver iterations.
To try to increase both accuracy and speed, or either one without sacrificing the other, increase computing power. To increase computing power, use a faster real-time processor or compute in parallel.
The type of solver that you specify also affects simulation speed and accuracy. For fixed-step simulation, Simscape™ local solvers are faster and as accurate as Simulink® global solvers. Implicit solvers are faster, but less accurate than explicit solvers. However, the numerical stiffness of the network is also a determinant for deciding whether to use an implicit solver or an explicit solver. Explicit solvers yield more accurate results for numerically stiff networks.
For more information on how model complexity affects speed and accuracy, see Eliminating Effects That Require Intensive Computation. For more information, on how solver configurations affect speed and accuracy, see Optimizing Local and Global Solver Configurations.
It is possible that there is no combination of model complexity and solver settings that can make your model real-time capable. You can simulate parts of the system in parallel to improve speed and accuracy. To learn more, see Simulating Parts of the System in Parallel.
Eliminating Effects That Require Intensive Computation
If your desktop simulation analysis indicates that your model likely is not fast enough for real-time simulation, eliminate effects that require intensive computation. Identify elements in your model that cause costly effects, such as discontinuities and rapid changes, that tend to slow down simulations.
Elements that cause discontinuities include:
Hard stops or backlash
Stick-slip friction
Switches or clutches
Elements with small time constants that cause rapid changes include:
Small masses attached to stiff springs with minimal damping
Electrical circuits with low capacitance, inductance, and resistance
Hydraulic circuits with small compressible volumes
To eliminate or modify the elements that are responsible for the effects that slow down your simulation, use these approaches:
Replace nonlinear components with linearized versions.
Replace complex equations with lookup tables for their solution.
Replace complicated components with simplified models.
Smooth discontinuous functions (step changes) with filters, delays, and other techniques.
Optimizing Local and Global Solver Configurations
You can also influence the speed and accuracy of your simulation by way of your solver specifications. The level of accuracy that your real-time target machine delivers does not necessarily correlate to a specific step size across all networks in a single model. A real-time target machine can give accurate results for a simple network in your model but inaccurate results for a more complex network. Take advantage of the ability to specify different solver configurations for each network in your Simscape model. To help make your model real-time capable, configure your fixed-step global solver and each local solver individually.
For information on solver options and determining the solvers that help to make your Simscape model real-time capable, see Solvers for Real-Time Simulation.
Simulating Parts of the System in Parallel
Another approach for increasing speed while maintaining accuracy is to configure your model to evaluate multiple physical networks in parallel. You can partition your model if the networks are not dependent upon one another. Work with and experiment with your model, the generated code, and the real-time target machine to use this approach.
Initialization Cost
Initialization occurs at the beginning of simulation when simulation time, t, is at the model Start Time. During the first call to ModelOutputs, Simscape performs model initialization. The solver determines the simulation starting point by iteratively computing the initial values for all system variables. Finding a solution that satisfies the model equations for a nonlinear system can take more time than the time step allows. If the computation time exceeds the time step, a central processing unit (CPU) overload occurs. Real-time processors typically respond to CPU overloads by terminating model execution.
If your real-time application terminates when t is at the model Start Time, disable the automatic shutdown response on your real-time hardware to CPU overloads for that time period. To determine how to disable such processes, check with your hardware vendor.
Related Examples
- Determine Step Size
- Estimate Computation Costs
- Reduce Computation Costs
- Reduce Fast Dynamics
- Reduce Numerical Stiffness
- Reduce Zero Crossings
- Partition a Model