## Making Optimal Solver Choices for Physical Simulation

For the key simulation concepts to consider before making these choices, see Important Concepts and Choices in Physical Simulation.

### Simulating with Variable Time Step

For a typical Simscape™ model, the Simulink® variable-step solvers ode15s and ode23t are recommended. Of these two global 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 accurate and 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 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.

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. 