Introduction to Fast Electrical Simulation
When working with a Simscape Electrical™ model, it is important to consider if the simulation is running at optimal performance. Depending on the purpose of your simulation, different techniques and best practices can be used to diagnose the model, identify bottlenecks, and improve simulation performance.
This guide provides steps to follow for applying optimization best practices to help you achieve optimal simulation performance while maintaining accuracy and meeting your specific simulation requirements.
Keep Exploring This Topic
- Simscape Electrical Modeling Practices for Fast Simulation - Video
- Fast Electrical Simulation Examples - File Exchange
Preparation: Diagnose Simulation Time and Signal Sample Times
The logged simulation metadata can be used to analyze and compare simulation time in subsequent steps. This information can be viewed through the SimulationMetaData object within the Simulink.SimulationOutput object.
Another piece of information relevant to analyzing the solver performance is the block sampling time. Depending on solver and block settings, you can find varying discrete as well as continuous sample times in your model. This can lead to unexpected behavior. Turn on the sample time display by going to the Debug tab in the Simulink® toolstrip, then Information Overlays > Sample Time > Colors. This will trigger a model update and then display color-coded sample times of all signals.
Simulink Profiler provides detailed insights into a model’s execution time. It captures overall simulation time and identifies block execution timings that help identify which parts of the model take longer to execute.
Using Simulink Profiler for Run-Time Analysis per Block or Subsystem
This video shows how the Simulink Profiler is used to figure out how much simulation time is needed per block.
The Solver Profiler report displays how often different blocks in the model are being executed and the time the execution takes. This helps identify parts of the model that are computationally more demanding. The execution time of the Simscape™ network will be summarized under the Solver Configuration block. If your model contains a lot of scopes, consider using the Simulation Data Inspector instead.
Next, the simulation’s purpose and how that impacts performance and settings will be covered.
Choosing the Right Simulation Approach
The performance of your model depends on how you intend to use it. Different purposes call for specific techniques and settings. This section reviews two simulation types: desktop simulation and hardware-in-the-loop simulation.
Desktop Simulation
In desktop simulations, the primary intent is often to develop a control algorithm and test it in a closed-loop simulation against the physical plant model. The plant model would be a Simscape network, and the algorithm would be modeled in Simulink. In this case, you want the plant model using a variable-step solver, meaning that the solver can use a large simulation time step where the model dynamics permit. The algorithm can run alongside it, executed at a variable time step or, if needed, at a fixed time step.
If this matches your use case, go to the Considerations for Desktop Simulation section below.
Hardware-in-the-Loop Simulation
Hardware-in-the-loop (HIL) simulation is a real-time simulation used to test a controller algorithm running on the controller hardware in real time. For the HIL simulation, a real-time computer is used to represent the plant model. For this purpose, the Simscape network needs to be configured to use a fixed-step solver before deploying it to the real-time hardware.
If this matches your use case, go to the Considerations for Hardware-in-the-Loop Simulation section below.
Considerations for Desktop Simulation
Solver Considerations for the Simscape Physical Network (Variable-Step Solver Plant)
To design the control algorithm and test it against the electrical Simscape network, it is recommended to start with the physical model being executed with a variable-step solver. This allows the solver to take a smaller time step where the model dynamics require it and choose a larger time step when possible. This potentially results in an overall performance improvement in the absence of predetermined, fixed time-step execution.
To use a variable-step solver for a Simscape network, disable the use local solver option in the Solver Configuration block. This allows access to the global solver, chosen in Model Settings. Typically, implicit solvers such as daessc, odes23t, or ode15s are recommended for Simscape networks.
Keep Exploring This Topic
- Choose a Solver for Performant Simulation (10:55) - Video
- Simulating with Variable Time Step - Documentation
Configuring Algorithms with a Fixed-Step Sample Time
The sample time coloring indicates at which solver time steps specific blocks are being executed. When designing the algorithm, you typically have a certain requirement on sample times for the control loop(s). To benefit from the advantages of a variable-step solver, you can run the plant model in variable-step and the algorithm in fixed-step. To achieve this, the global solver (set in the Model Settings) should be of variable-step type, and blocks in your model can be configured to only be executed at certain (fixed-step) sample rates.
When sample times are not explicitly defined in blocks, they often inherit sample times, which can lead to unwanted behavior. To avoid this, specify fixed-step sample rates in relevant blocks. Use the sample time setting where applicable to ensure correct execution timings. See Specify Sample Time.
Choosing the Appropriate Model Fidelity
Selecting the correct model fidelity is a critical step in achieving optimal simulation performance. Simscape Electrical technology and its components allow you to choose between different modeling and parameterization options, depending on your task at hand.
For example, detailed switching models may be necessary for analyzing power losses, while ideal switching can be used for harmonic analysis. For control design, an averaged model is often sufficient.
Keep Exploring This Topic
See examples of modeling components at varying fidelity:
Modeling Techniques for PWM
A power electronics model typically contains a PWM signal. This can be optimized, also in combination with the model fidelity of the inverter.
If your model contains a PWM signal, this can be another source of performance improvement.
Learn what you must consider when you build up a model for fast PWM switching.
Using the Solver Profiler for Diagnostics
When simulating a model with a variable-step solver, the Solver Profiler helps to identify when the simulation performance degrades due to small time steps or to simulation events, such as solver exceptions or zero crossings. For the Simscape network of your model, the Solver Profiler allows you to examine how different physical quantities might contribute to poor simulation performance.
Analyzing Variable-Step Solver Performance
This video shows, how the Solver Profiler can help to figure out performance bottlenecks for models using a variable step solver.
Additional Diagnostics Tools
Model Initialization Validation with the Variable Viewer
When running the simulation for a Simscape physical network, the solver computes the initial conditions at the beginning of the simulation at \(t=0\). The initial conditions for the Simscape network are typically configured on a block level, but they can also be taken from previously logged data. See Variable Initialization for more information.
The solver uses those settings to determine a suitable initial condition; this process can be time-consuming and result in warnings or errors. The Variable Viewer can be used to check and validate the results of the initial condition computation for your Simscape network. This provides useful information when determining potential root causes of slow or nonconverging initialization.
Analyzing Model Initialization with Variable Viewer
This video shows how the Variable Viewer can be used to check the results of the initialization for the model that uses Simscape.
Analyzing the Magnitude of Simscape Variables to Define Good Scaling Values
Defining nominal values for Simscape variables provides an option to specify the expected magnitude of a variable. See System Scaling by Nominal Values for more information. During simulation, the solver operates on these scaled, unitless values. Suitable scaling values across all variables enhance both simulation robustness and performance. For better performance, ensure that variable magnitudes are kept within a similar scale.
This video shows how the Variable Scaling Analyzer can be used for scaling Simscape variables to increase simulation robustness and performance.
Using Automated Checks for Optimal Model Settings
The following tools are designed for models containing mostly Simulink blocks and will only provide a few diagnostics specific to models containing Simscape networks.
The Model Advisor can check your model and subsystem for configurations or modeling features to meet certain modeling standards or to avoid inaccurate or inefficient simulations. The Model Advisor has built-in checks that can be specific to a task you want to achieve (for example, performance or accuracy).
The Model Advisor runs checks and delivers detailed reports with actionable recommendations and can even automatically implement those fixes.
The Performance Advisor is built within the Model Advisor framework and contains a predefined set of checks looking for configurations that might cause simulation slowdown.
Performance Checks for Optimal Model Settings
This video shows how the Model Advisor is used to perform several performance checks for your specific model concerning non optimal model settings or blocks.
Exploring Model Complexity with the Variable Statistics Viewer
The Statistics Viewer aggregates Simscape model statistics for a given model with its solver configurations. Investigating the statistics is a way to evaluate the complexity of a model containing Simscape blocks, even before simulation.
This video shows how the Statistics Viewer will aggregate Simscape model statistics for a given model with its solver configurations.
Additional Suggestions After Model Optimization
Simulation Modes and Fast Restart
Simulation modes can have an impact on simulation speed, especially when combined with fast restart. Models operate in normal mode by default. However, users can leverage accelerator and rapid accelerator mode.
While these modes offer performance improvements, they come with tradeoffs in model flexibility, interactivity, and diagnostic granularity. See Choosing a Simulation Mode. When utilizing Simscape models, the performance gains achieved through accelerator and rapid accelerator modes may be less substantial compared to pure Simulink models.
If you are repeatedly running a model without making structural changes in between runs—for example, during a parameter sweep—you can achieve time savings by skipping the compilation stage through fast restart functionality. For more information, see Get Started with Fast Restart in the MATLAB Help Center. To ensure Simscape parameters can be changed in between simulation runs, configure them as Simscape run-time parameters.
Using Simulation Models and Fast Restart
Using accelerator modes and fast restart for faster model execution | Simscape Electrical Modeling Practices for Fast Simulation
Parallel Computing
To save time when running multiple independent simulations, you can consider distributing these simulations among multiple cores or on a cluster. Use cases include parameter sweeps, Monte Carlo analysis, parallel calculations for an optimization problem, or model testing with Simulink Test™.
Running Models on Multiple Cores or on a Cluster
Learn how you can speed up electrical Simulation with Parallel Computing in normal or accelerator mode.
Considerations for Hardware-in-the-Loop Simulation
Even if your goal is to run the Simscape network in fixed step for a hardware-in-the-loop simulation, it is good practice to debug, validate, and improve your model using a variable-step solver. In order to achieve this, you can follow the steps starting from “Considerations for Desktop Simulations.” A fixed-step solver simulation can also hide underlying issues that are uncoverable only in variable-step simulations.
Choose a Solver for Performant Simulation
Learn more about solver and model considerations when simulation physical systems.
When converting to a fixed-step solver, you can either use the Simscape local solver or the Simulink global fixed-step solver. For better performance, it is generally recommended to choose the Simscape local solver. More details about the local solver and its settings can be found in documentation under Simulating with Fixed Time Step — Local and Global Fixed-Step Solvers.
An important step in configuring a Simscape model with a fixed-step solver for hardware deployment is iteratively determining the right solver in the time-step setting to achieve real-time viability with acceptable simulation results. Real-time simulation outlines and explains this workflow.
Simscape to HDL
For real-time simulation, the Simscape model is converted to C code. To achieve faster sampling rates, you can work with FPGA technology and HDL code. To benefit from this workflow for electrical network models in Simscape, the physical model needs to be converted into a state-space representation in Simulink. This is the main goal of the Simscape HDL Workflow Advisor.
Convert Electrical Plant Models to HDL Code
In this example, you learn how you can use the linearized switch approximation method to convert a Simscape motor model to an HDL implementation model for HDL code generation and synthesis.
Conclusion
This guide has presented a methodology for optimizing Simscape Electrical simulation performance across a range of applications, from desktop algorithm development to hardware-in-the-loop testing and FPGA implementation. By following the workflow outlined, beginning with diagnostic analysis, progressing through purpose-specific optimization strategies, and ending with advanced performance improvement, engineers can achieve significant improvements in simulation speed while maintaining accuracy and reliability.
By following this guide and leveraging the diagnostic tools available in MATLAB and Simulink, you can transform computationally intensive electrical simulations into efficient, accurate, and reliable modeling workflows.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)