Customizing Solvers for Physical Models

Important Concepts and Choices in Physical Simulation

This section describes advanced concepts and trade-offs you might want to consider as you configure and test solvers and other simulation settings for your Simscape™ model. For a summary of recommended settings, see Making Optimal Solver Choices for Physical Simulation. For background information, consult How Simscape Models Represent Physical Systems and How Simscape Simulation Works.

Variable-Step and Fixed-Step Solvers

Variable-step solvers are the usual choice for design, prototyping, and exploratory simulation, and to precisely locate events during simulation. They are not useful for real-time simulation and can be costly if there are many events.

A variable-step solver automatically adjusts its step size as it moves forward in time to adapt to how well it controls solution error. You control the accuracy and speed of the variable-step solution by adjusting the solver tolerance. With many variable-step solvers, you can also limit the minimum and maximum time step size.

Fixed-step solvers are recommended or required if you want to make performance comparisons across platforms and operating systems, to generate a code version of your model, and to bound or fix simulation cost. A typical application is real-time simulation. For more information, see Code Generation and Real-Time Simulation.

With a fixed-step solver, you specify the time step size to control the accuracy and speed of your simulation. Fixed-step solvers do not adapt to improve accuracy or to locate events. These limitations can lead to significant simulation inaccuracies.

Explicit and Implicit Solvers

The degree of stiffness and the presence of algebraic constraints in your model influence the choice between an explicit or implicit solver. Explicit and implicit solvers use different numerical methods to simulate a system.

  • If the system is a nonstiff ODE system, choose an explicit solver. Explicit solvers require less computational effort than implicit solvers, if other simulation characteristics are fixed.

    To find a solution for each time step, an explicit solver uses a formula based on the local gradient of the ODE system.

  • If the system is stiff, use an implicit solver. Though an explicit solver may require less computational effort, for stiff problems an implicit solver is more accurate and often essential to obtain a solution. Implicit solvers require per-step iterations within the simulated time steps. With some implicit solvers, you can limit or fix these iterations.

    An implicit solver starts with the solution at the current step and iteratively solves for the solution at the next time step with an algebraic solver. An implicit algorithm does more work per simulation step, but can take fewer, larger steps.

  • If the system contains DAEs, even if it is not stiff, use an implicit solver. Such solvers are designed to simultaneously solve algebraic constraints and integrate differential equations.

Full and Sparse Linear Algebra

When you simulate a system with more than one state, the solver manipulates the mathematical system with matrices. For a large number of states, sparse linear algebra methods applied to large matrices can make the simulation more efficient.

Event Detection and Location

Events, in most cases, occur between simulated time steps.

  • Fixed-step solvers detect events after "stepping over" them, but cannot adaptively locate events in time. This can lead to large inaccuracies or failure to converge on a solution.

  • Variable-step solvers can both detect events and estimate the instants when they occur by adapting the timing and length of the time steps.

    Tip   To estimate the timing of events or rapid changes in your simulation, use a variable-step solver.

    If your simulation has to frequently adapt to events or rapid changes by changing its step size, much or all of the advantage of implicit solvers over explicit solvers is lost.

Unbounded, Bounded, and Fixed-Cost Simulation

In certain cases, such as real-time simulation, you need to simulate with an execution time that is not only bounded, but practically fixed to a predictable value. Fixing execution time can also improve performance when simulating frequent events.

The real-time cost of a variable-step simulation is potentially unlimited. The solver can take an indefinite amount of real time to solve a system over a finite simulated time, because the number and size of the time steps are adapted to the system. You can configure a fixed-step solver to take a bounded amount of real time to complete a simulation, although the exact amount of real time might still be difficult to predict before simulation. Even a fixed-step solver can take multiple iterations to find a solution at each time step. Such iterations are variable and not generally limited in number; the solver iterates as much as it needs to.

Fixing execution time implies fixed-cost simulation, which both fixes the time step and limits the number of per-step iterations. Fixed-cost simulation prevents execution overruns, when the execution time is longer than the simulation sample time. A bounded execution time without a known fixed cost might still cause some steps to overrun the sample time.

The actual amount of computational effort required by a solver is based on a number of other factors as well, including model complexity and computer processor. For more information, see Real-Time Simulation.

Global and Local Solvers

You can use different solvers on different parts of the system. For example, you might want to use implicit solvers on stiff parts of a system and explicit solvers everywhere else. Such local solvers make the simulation more efficient and reduce computational cost.

Such multisolver simulations must coordinate the separate sequences of time steps of each solver and each subsystem so that the various solvers can pass simulation updates to one another on some or all of the shared time steps.

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, MathWorks recommends the Simulink® variable-step solvers ode15s and ode23t. 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, MathWorks recommends 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 Backward Euler and Trapezoidal Rule. Of these two local solvers:

  • 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.

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 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 any non-Simscape parts of your model have only discrete states, then the global solver effectively sees only discrete states. In that case, MathWorks recommends a discrete, fixed-step global solver. If you are attempting a fixed-cost simulation with discrete states, you must use a discrete, fixed-step global solver.

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 Real-Time Simulation.

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.

Was this topic helpful?