## How Simscape Simulation Works

### Simscape Simulation Phases

You might find this brief overview helpful for constructing models and understanding errors. For more information, see How Simscape Models Represent Physical Systems.

Simscape™ software gives you multiple ways to simulate and analyze physical systems in the Simulink^{®} environment. Running a physical model simulation is similar to simulating any Simulink model. It entails setting various simulation options, starting the simulation, and viewing the simulation results. This topic describes various aspects of simulation specific to Simscape models. For specifics of simulating and analyzing with individual Simscape add-on products, refer to the documentation for those individual add-on products.

This flow chart presents the Simscape simulation sequence.

The flow chart consists of the following major phases:

### Model Validation

The Simscape solver first validates the model configuration and checks your data entries from the block dialog boxes.

All Simscape blocks in a diagram must be connected into one or more physical networks.

Each topologically distinct physical network in a diagram requires exactly one Solver Configuration block.

If your model contains fluid elements (such as two-phase fluids, gas, moist air, isothermal or thermal liquid), each topologically distinct circuit in a diagram can contain a block that defines the fluid properties for all the blocks that connect to the circuit. If no fluid block is attached to a loop, the blocks in this loop use the default fluid. However, more than one fluid block in a loop generates an error.

Signal units specified in a Simulink-PS Converter block must match the input type expected by the Simscape block connected to it. For example, when you provide the input signal for an Ideal Angular Velocity Source block, specify angular velocity units, such as

`rad/s`

or`rpm`

, in the Simulink-PS Converter block, or leave it unitless. Similarly, units specified in a PS-Simulink Converter block must match the type of physical signal provided by the Simscape block outport.

### Network Construction

After validating the model, the Simscape solver constructs the physical network based on the following principles:

Two directly connected Conserving ports have the same values for all their Across variables (such as voltage or angular velocity).

Any Through variable (such as current or torque) transferred along the Physical connection line is divided among the multiple components connected by the branches. For each Through variable, the sum of all its values flowing into a branch point equals the sum of all its values flowing out.

### Equation Construction

Based on the network configuration, the parameter values in the block dialog boxes, and the global parameters defined by the fluid properties, if applicable, the Simscape solver constructs the system of equations for the model.

These equations contain system variables of the following types:

*Dynamic*— Time derivatives of these variables appear in equations. Dynamic, or differential, variables add dynamics to the system and require the solver to use numerical integration to compute their values. Dynamic variables can produce either independent or dependent states for simulation.*Algebraic*— Time derivatives of these variables do not appear in equations. These variables appear in algebraic equations but add no dynamics, and this typically occurs in physical systems due to conservation laws, such as conservation of mass and energy. The states of algebraic variables are always dependent on dynamic variables, other algebraic variables, or inputs.

The solver then performs the analysis and eliminates variables that are not needed to solve the system of equations. After variable elimination, the remaining variables (algebraic, dynamic dependent, and dynamic independent) get mapped to Simulink state vector of the model.

For information on how to view and analyze model variables, see Statistics Viewer.

### Initial Conditions Computation

The Simscape solver computes the initial conditions only once, at the beginning of simulation (*t* = 0). In the Solver Configuration block, the default is that the **Start simulation from steady state** check box is not selected. If it is selected in your model, see Finding an Initial Steady State.

The solver computes the initial conditions by finding initial values for all the system variables that exactly satisfy all the model equations. You can affect the initial conditions computation by *block-level variable initialization*, that is, by specifying the priority and target initial values in the **Initial Targets** section of the block dialog box. You can also initialize variables for a whole model from a saved operating point.

The values you specify during variable initialization are not the actual values of the respective variables, but rather their target values at the beginning of simulation (*t* = 0). Depending on the results of the solve, some of these targets may or may not be satisfied. The solver tries to satisfy the high-priority targets first, then the low-priority ones:

At first, the solver tries to find a solution where all the high-priority variable targets are met exactly, and the low-priority targets are approximated as closely as possible. If the solution is found during this stage, it satisfies all the high-priority targets. Some of the low-priority targets might also be met exactly, the others are approximated.

If the solver cannot find a solution that exactly satisfies all the high-priority targets, it issues a warning and enters the second stage, where

`High`

priority is relaxed to`Low`

. That is, the solver tries to find a solution by approximating both the high-priority and the low-priority targets as closely as possible.

After you initialize the variables and prior to simulating the model, you can open the Variable Viewer to see which of the variable targets have been satisfied. For more information on block-level variable initialization, see Variable Initialization.

#### Finding an Initial Steady State

When you select the **Start simulation from steady state** check box in the Solver Configuration block:

For models compatible with frequency-and-time equation formulation, the solver attempts to perform sinusoidal steady-state initialization. In other words, initialization is performed using frequency-time equations, and then the simulation proceeds using the actual equation formulation and other options selected in the Solver Configuration block. For more information, see Frequency and Time Simulation Mode.

If the model is not frequency-and-time compatible, the solver attempts to find the steady state that would result if the inputs to the system were held constant for a long enough time, starting from the initial state obtained from the initial conditions computation described in the previous section. Steady state means that the system variables are no longer changing with time.

If the steady-state solve succeeds, the state found is some steady state (within tolerance), but not necessarily the state expected from the given initial conditions. Simulation then starts from this steady state.

A model can have more than one steady state. In this case, the solver selects the steady-state solution that is consistent with the variable targets specified during block-level variable initialization, as well as mode charts and event variables present in the model. For more information, see Variable Initialization and Discrete Events and Mode Charts.

### Transient Initialization

After computing the initial conditions, or after a subsequent event (such as a discontinuity resulting, for example, from a valve opening, or from a hard stop), the Simscape solver performs transient initialization. Transient initialization fixes all dynamic variables and solves for algebraic variables and derivatives of dynamic variables. The goal of transient initialization is to provide a consistent set of initial conditions for the next phase, transient solve.

### Transient Solve

Finally, the Simscape solver performs transient solve of the system of equations. In transient solve, continuous differential equations are integrated in time to compute all the variables as a function of time.

The solver continues to perform the simulation according to the results of the transient solve until the solver encounters an event, such as a zero crossing or discontinuity. The event may be within the physical network or elsewhere in the Simulink model. If the solver encounters an event, the solver returns to the phase of transient initialization, and then back to transient solve. This cycle continues until the end of simulation.

### Status Messages

When you run a model simulation, the first phase of simulation invokes the model compiler. The model compiler converts the model to an executable form, a process called compilation. Compilation and initialization of a large Simscape model can take several minutes. The simulation status bar in the bottom-left corner of the model window displays a series of messages that correspond to the various stages of physical network compilation and initialization, such as:

Compiling:Constructing equation systems for Simscape physical networks

Compiling:Analyzing equation systems for Simscape physical networks

Compiling:Setting up simulation for Simscape physical networks

Running:Initializing equation systems for Simscape physical networks

A new message appears when each stage is started and then completed. Therefore, for
each stage, the status bar displays the message

while the
computations are going on. Then the message
* <stage>*:Started

`<stage>`

:Completed

displays
briefly and the process goes on to the next stage.These messages help you monitor compilation and initialization progress.