Making Optimal Solver Choices for Physical Simulation
For desktop simulation, it is recommended that you use a variable-step solver. A variable-step solver provides better simulation performance and accuracy, particularly for systems with a spread of time constants.
Even if you ultimately plan to run fixed time step, debug and validate your model using variable time step. Simulation with a variable-step solver provides you baseline results against which to check subsequent fixed-step simulations. Additionally, running fixed time step can mask modeling issues. Running with a variable-step solver first lets you uncover and fix these issues before switching to fixed time step.
For deployment to hardware, fixed time step is required. There are two main options:
Simscape™ local solver. With this option, no subsampling occurs, because the Simscape network is being updated at each sample time only. The choice of solver and its parameters for the rest of the model is independent of the local solver. Therefore, you can have multiple fixed-step rates in the rest of the model, for example, to support a controller that uses multiple update rates.
Simulink® global fixed-step solver. This option allows only one sample time for the whole model, but it supports subsampling if you turn on fixed-step zero crossings. For more information, see Use Fixed-Step Zero-Crossing Detection for Faster Simulations. This option can help with pulse-width-modulation (PWM) simulation when the fixed time step does not exactly resolve the PWM on-time fraction.
Option 1 may outperform option 2 in terms of simulation time. In both options, you need to ensure that the fixed step size is small enough to deal with fastest time constant of interest. For more information on deployment to hardware, see Real-Time Simulation.
For the key simulation concepts to consider before making optimal solver choices, see Important Concepts and Choices in Physical Simulation.
Simulating with Variable Time Step
When you first create a model, the default Simulink solver is VariableStepAuto
. Auto solver chooses a suitable
solver as described in Choose a Solver. For Simscape models, the auto solver selection depends on the type of the model:
For new models created in R2021a and beyond, if your model contains Simscape blocks and Differential Algebraic Equations (DAEs), auto solver selects
daessc
. For such models created prior to R2021a, auto solver usesode23t
. For information on how to upgrade your existing models to usedaessc
, see Upgrading Your Models to Use the daessc Solver.If the system can be reduced to an ordinary differential equation (ODE) and the model is stiff, auto solver selects
ode15s
.If the system can be reduced to an ordinary differential equation (ODE) and the model is nonstiff, auto solver selects an explicit solver,
ode45
.
Rather than relying on the auto solver selection, you can explicitly choose a solver for your model. To select a solver, follow a procedure similar to the procedure in Modifying Initial Settings.
The daessc
variable-step Simulink solver is designed specifically for physical modeling. For more information,
see Using the daessc Solver.
Other variable-step solvers recommended for a typical Simscape model are ode15s
and ode23t
. Of these
two solvers:
The
ode15s
solver is more stable, but tends to damp out oscillations.The
ode23t
solver captures oscillations better but is less stable.
With Simscape models, these solvers solve the differential and algebraic parts of the physical model simultaneously, making the simulation more efficient.
Simulating with Fixed Time Step — Local and Global Fixed-Step Solvers
In a Simscape model, it is recommended that you implement fixed-step solvers by continuing to use a global variable-step solver and switching the physical networks within your model to local fixed-step solvers through each network Solver Configuration block. The local solver choices are:
The Backward Euler tends to damp out oscillations, but is more stable, especially if you increase the time step.
The Trapezoidal Rule solver captures oscillations better but is less stable.
The Partitioning solver lets you increase real-time simulation speed by partitioning the entire system of equations corresponding to a Simscape network into a cascade of smaller equation systems. Not all networks can be partitioned. However, when a system can be partitioned, this solver provides a significant increase in real-time simulation speed. For more information, see Understanding How the Partitioning Solver Works and Increase Simulation Speed Using the Partitioning Solver.
Regardless of which local solver you choose, the Backward Euler method is always applied:
Right at the start of simulation.
Right after an instantaneous change, when the corresponding block undergoes an internal discrete change. Such changes include clutches locking and unlocking, valve actuators opening and closing, and the switching of the PS Asynchronous Sample & Hold block.
Switching to Discrete States and Solvers
If you switch a physical network to a local solver, the global solver treats that network as having discrete states.
If other physical networks in your model are not using local solvers, or if the non-Simscape parts of your model have continuous states, then you must use a continuous global solver.
If all physical networks in your model use local solvers, and all other parts of your model have only discrete states, then the global solver effectively sees only discrete states. In that case, a discrete, fixed-step global solver is recommended. If you are attempting a fixed-cost simulation with discrete states, you must use a discrete, fixed-step global solver.
Note
Input filtering may introduce continuous states. If you are using a combination of discrete and local solvers and get an error message about the model containing continuous states, check the Simulink-PS Converter blocks in the model and turn off input filtering, if needed. For more information, see Filtering Input Signals and Providing Time Derivatives.
For Maximum Accuracy with Fixed-Step Simulation
If solution accuracy is your single overriding requirement, use the global Simulink fixed-step solver ode14x, without local solvers. This implicit solver is the best global fixed-step choice for physical systems. While it is more accurate than the Simscape local solvers for most models, ode14x can be computationally more intensive and slower when you use it by itself than it is when you use it in combination with local solvers.
In this solver, you must limit the number of global implicit iterations per time step. Control these iterations with the Number Newton’s iterations parameter in the Solver pane of the Configuration Parameters dialog box.
Simulating with Fixed Cost
Many Simscape models need to iterate multiple times within one time step to find a solution. If you want to fix the cost of simulation per time step, you must limit the number of these iterations, regardless of whether you are using a local solver, or a global solver like ode14x. For more information, see Unbounded, Bounded, and Fixed-Cost Simulation and Fixed-Cost Simulation for Real-Time Viability.
To limit the iterations, open the Solver Configuration block of each physical network.
Select Use fixed-cost runtime consistency iterations and set
limits for the number of nonlinear and mode iterations per time step. You can determine the
optimum number of nonlinear iterations using the simscape.getLocalSolverFixedCostInfo
function.
Tip
Fixed-cost simulation with variable-step solvers is not possible in most simulations. Attempt fixed-cost simulation with a fixed-step solver only and avoid using fixed-cost iterations with variable-step solvers.
Troubleshooting and Improving Solver Performance
Consider the basic trade-off of speed versus accuracy and stability. A larger time step or tolerance results in faster simulation, but also less accurate and less stable simulation. If a system undergoes sudden or rapid changes, larger tolerance or step size can cause major errors. Consider tightening the tolerance or step size if your simulation:
Is not accurate enough or looks unphysical.
Exhibits discontinuities in state values.
Reaches the minimum step size allowed without converging, usually a sign that one or more events or rapid changes occur within a time step.
Any one or all of these steps increase accuracy, but make the simulation run more slowly.
For Local Solvers
Models with friction or hard stops are particularly difficult for local solvers, and may not work or may require a very small time step.
With the Trapezoidal Rule solver, oscillatory “ringing” can become more of a problem as the time step is increased. For a larger time step in a local solver, consider switching to Backward Euler.
For ODE Systems
In certain cases, your model reduces to an ODE system, with no dependent algebraic variables. (See How Simscape Models Represent Physical Systems.) If so, you can use any global Simulink solver, with no special physical modeling considerations. An explicit solver is often the best choice in such situations.
Through careful analysis, you can sometimes determine if your model is represented by an ODE system.
If you create a Simscape model from a mathematical representation using the Simscape language, you can determine directly if the resulting system is ODE.
For Large Systems
Depending on the number of system states, you can simulate more efficiently if you switch the value of the Linear Algebra setting in the Solver Configuration block.
For smaller systems, Full
provides faster results. For larger
systems, Sparse
is typically faster.
Multiple Local Solvers Example with a Mixed Stiff-Nonstiff System
In this example, a Simscape model contains three physical networks.
Two networks (numbers 1 and 3) use local solvers, making these two networks appear to the global solver as if they had discrete states. Internally, these networks still have continuous states. These networks are moderately and highly stiff, respectively.
One of these networks (number 1) uses the Backward Euler (BE) local solver. The other (number 3) uses the Trapezoidal Rule (TR) local solver.
The remaining network (number 2) uses the global Simulink solver. Its states appear to the model as continuous. This network is not stiff and is pure ODE. Use an explicit global solver.
Because at least one network appears to the model as continuous, you must use a continuous solver. However, if you remove network 2, and if the model contains no continuous Simulink states, Simulink automatically switches to a discrete global solver.