## Model Preparation Process

Preparing a model for real-time simulation means reconfiguring the model for speed while maintaining accuracy. This may involve refining the scope of the model as well as the fidelity. The flowchart demonstrates an example model preparation process. The goal of the model preparation process is to get the model fast enough to test with a fixed-step, fixed-cost solver. You may need to iterate through the model preparation process before your model produces acceptable results at a large enough time step. Once your model is ready to test on a fixed-step, fixed-cost solver, proceed to the Fixed-Cost Simulation Process.

### 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 Fixed-Cost Simulation Process 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).