## Real-Time Model Preparation Workflow

The figure shows the real-time model preparation workflow.

A real-time-capable model is both fast and accurate. When you simulate a real-time-capable model on your real-time target machine, it runs to completion and generates results that match your expectations, as based on theoretical models and empirical data. The only way to determine if your model is real-time capable is to run it on your real-time target machine. You can, however, use desktop simulation, that is, simulation on your development computer, to determine the likelihood that your model is real-time capable before you deploy it.

The real-time model preparation workflow is the first of two workflows that you perform on your development computer to make it more probable that your model is real-time capable. The workflow shows you how to adjust the size or fidelity of your model to improve speed without sacrificing and accuracy. When you finish this workflow, use the real-time simulation workflow to find the best fixed-step, fixed-cost solver configuration to use for simulating your model in real time.

### Prepare Your Model for Real-Time Simulation

#### Obtain Reference Results

Use empirical or theoretical data to design and build your Simscape™ model.
Use a Simulink^{®} global variable-step solver to simulate your model.
Refine your model as needed to obtain simulation results that the
underlying data supports. Reference results provide a baseline to
assess model accuracy against throughout all stages of the model preparation
and real-time simulation workflows.

#### Evaluate Overrun Risk

An overrun occurs when the step size is too small to allow the real-time computer to complete all the processing required for any one step. If your model requires a step size that is so small that it is likely to cause an overrun, then your model is not fast enough for real-time simulation. To determine if small steps are likely to cause an overrun, create a plot of the size of the steps that the variable-step step solver uses to execute the simulation of your model. The step size plot tells you the number and size of the small steps that the solver uses during the simulation.

There are no hard metrics for the size or number of small steps that are likely to cause a real-time-simulation overrun. Moving your model from desktop simulation to real-time simulation is an iterative process. The process, which involves modifying, simulating, and analyzing your model, helps you to determine if the small steps in your model are time limiting or numerous enough to force an overrun.

Experience that you gain by simulating different models on your real-time machine can also help you decide if the small steps in your model are likely to force an overrun. For example, consider a case with two models, M1 and M2, and two different real-time processors, RT1 and RT2. Processors RT1 and RT2 have the same nominal processing speed. Models M1, a mechanical model, and M2, an electrical model, both have a few steps that are 1e-15 seconds. It is possible for model M1 to simulate with sufficiently accurate results on real-time processor RT1, but to incur an overrun or simulate with insufficiently accurate results on real-time processor RT2. It is also possible that model M1 runs to completion with accurate results on RT1 and RT2, whereas model M2 generates an overrun on both processors. These scenarios are possible because the distinct model topologies yield different dynamics and because nominal processing speed is not the only determinant for simulation execution time. Other factors such as the operating system and I/O configuration also affect how simulation execution proceeds on a real-time processor. Familiarity with system dynamics and the processing power of your real-time equipment can guide your decision making when you assess the impact of small step sizes on the real-time viability of a model.

#### Adjust Model Fidelity or Scope

Modify the model to increase speed or accuracy if your analysis indicates that real-time simulation with the model is likely to have an overrun or yield insufficiently accurate results.

When you evaluate overrun risk, if you find that the simulation uses too many small steps, use these approaches to improve simulation speed:

Reduce computation costs.

Reduce numerical stiffness.

Reduce zero crossings.

Reduce fast dynamics

Partition the model for parallel processing.

When you evaluate model accuracy, if you find that the simulation results do not match the reference results, use these approaches to improve model accuracy:

Simulink best practices for modeling dynamic systems

Simscape essential modeling techniques

#### Obtain Results with a Variable-Step Solver

Using a Simulink global variable-step solver, obtain results for the modified version of your model.

The step size plot also helps you to:

Estimate the maximum step size to use for the fixed-step solver to achieve accurate results during real-time simulation.

Identify the exact times when discontinuities or fast dynamics slow down the simulation.

#### Evaluate Model Accuracy

Compare the results from simulating on the target computer to your reference results. Are the reference and modified model results the same? If not, are they similar enough that the empirical or theoretical data also supports the results from the simulation of the modified model? Is the modified model representing the phenomena that you want it to measure? Is it representing those phenomena correctly? If you plan on using your model to test your controller design, is the model accurate enough to produce results that you can rely on for system qualification? The answers to these questions help you to decide if your real-time results are accurate enough.

#### Perform Real-Time Simulation Workflow

When variable-simulation results indicate that your model has the speed and accuracy required for real-time processing, you can use the Real-Time Simulation Workflow to configure your model for fixed-step, fixed-cost simulation.

#### Return to the Real-Time Model Preparation Workflow

The connector is an entry point for returning to the real-time model preparation workflow from another workflow (for example, the real-time simulation workflow or the hardware-in-the-loop simulation workflow).

### Insufficient Computational Capability for Workflow Completion

It is possible that your real-time target machine lacks the computational capability for running your model in real time. If, after multiple iterations of the workflow, there is no level of model complexity that makes your model real-time capable, consider these options for increasing processing power:

## Related Examples

- Determine Step Size
- Estimate Computation Costs
- Reduce Computation Costs
- Reduce Fast Dynamics
- Reduce Numerical Stiffness
- Reduce Zero Crossings
- Partition a Model
- Manage Model Variants