How to improve Simulink simulation speed?

        When the basic model is available, the faster the model simulation is, the faster and more efficient the algorithm development will be. In the case of certain machine performance (CPU/memory/graphics card, etc.), it is necessary to know how to improve the model simulation speed!

        Now, the information on improving the simulation speed of Simulink is organized on the Internet as follows, and the validity has not been verified. According to the simulation process, it is organized into the following parts: the setting of model simulation parameters (step size, solver, simulation time, etc.), the construction of model modules, model display and output settings.

Model simulation parameter settings

1. Small step size or sampling time (or sampling times that are not multiples of each other): In order to capture important events during simulation, it is sometimes necessary to set a minimum enough step size; conversely, a step size that is too small will result in Unnecessary output points, thus slowing down the simulation.

2. The maximum step size is too small: If you have changed the maximum step size, try running the simulation with the default value (set to auto).

3. The accuracy you require may be too high: the default relative tolerance (0.1% accuracy) is usually sufficient. For models with states approaching zero, if the absolute tolerance parameter is too small, the simulation may take too many steps around the near-zero state value. Remember that all tolerance values ​​to be entered are absolute values. So, when the default relative tolerance value is set to 1e-3, it means that the relative tolerance is 0.001, or 0.1% (as a percentage).

4. The time scale may be too long: reduce the time interval.

Solver selection

5. This is probably a stiff problem and you are using a non-stiff solver: try ode15s.

Summary: In discrete system simulation, if the step size can be increased under the condition of ensuring accuracy, increase the step size, and the total simulation time shall meet the simulation requirements and be shortened as much as possible.

Building the model module

1. There is a MATLAB function block in the model: When the MATLAB Fcn block is present, the MATLAB interpreter is called at every sampling time. This slows down the simulation considerably. Therefore, the built-in Simulink blocks should be used whenever possible.

2. MATLAB S-Function (S-Function): The S-Function is calculated at each time step. Converting MATLAB code to MEX files can significantly improve performance. Also, whenever possible, try to model using built-in blocks.

3. Your model contains a Memory block: Using the Memory block causes the variable-order solvers (ode15s and ode113) to be reset back to order 1 at each step.

4. There are Mask modules in the Extras library that contain one of the above three items (ie Graph Scope, Autoscaling Scopes, Spectrumanalyzer, etc.): Unmask modules to see if they call S-Function.

5. Algebraic loop: To understand the algebraic loop, iterative calculations are performed at each step. Therefore, it greatly reduces the speed.

6. Do not introduce the white noise module in the integral function: For continuous systems, use the band-limited white noise module in the Extras/Sources library.

7. You may have encountered continuous zero crossings, causing the simulation to gradually "stall" for a long (and possibly infinite) time: To fix this, you can disable zero crossing detection. This can be achieved by selecting the Disable zerocrossing detection option in the Advanced pane of the Simulation Parameters dialog box. In R11, this can be achieved by going to the Simulation->Parameters->Diagnostics section of the model.

8. You may also want to try setting up the Model Parameter Configuration dialog so that your Simulink model uses the Inline Parameters option: select this option to enable Simulink to treat the specified parameters as constants to speed up the simulation.

9. Simulink Accelerator simulation mode can speed up simulation time. Profiler can analyze model performance bottlenecks to improve simulation speed.

    The first is the construction of the model. When modeling in the Simulink environment, there are fewer problems when modeling with s-domain expressions. Increasing the error limit and canceling the zero-crossing detection can improve the simulation speed and reduce warning messages. In the case of the z-domain, the problem to be paid attention to will be a little more. The first point is to eliminate the algebraic loop (Algebraic Loop) in the model; because the data processed in the z-domain are processed according to the sampling cycle, so if a data is used as input, at the same time without delay As the output, there is a problem of numerical processing, resulting in a significant decrease in the simulation speed. The processing method is generally not difficult, just add a delay link to the feedback. The other is the problem of the simulation step size. In the case of ensuring the simulation accuracy, using a large step size as much as possible can significantly improve the simulation speed; the maximum simulation step size can naturally be determined according to Shannon's sampling law, but in general, choose simulation A step size of 4-10 times the maximum sampling frequency is sufficient.

  The second is the mode in which the simulation can be changed. In the old version of Simulink, this option was very few, but the new version has modes such as Normal, Accelerator and Rapid Accelerator, HIL. Hardware in the loop HIL is obviously the fastest, but it is not applicable in many cases, because firstly, there must be corresponding hardware, and secondly, the simulation object must support code generation. Because Simulink uses an interpretive language, the normal mode can be understood as Matlab interprets a sentence, the operating system executes a sentence, and the speed will not be too fast; Accelerator compiles some shared modules into library files, such as dll calls. , which is equivalent to a hybrid mode, which includes interpretation-execution, direct invocation, and speed; Rapid Accelerator compiles the entire model into a program that runs independently under the operating system, without the work that Simulink interprets to the operating system, and the natural running speed is fast, The price is that it takes a certain amount of time to compile the model; this mode requires relatively high hardware configuration of the PC, and it is prone to errors when the memory is less than 3GB.


Summary: Use the S-FUNCTION of matlab function, m or c as little as possible for modeling, and try to use the built-in algebraic operation module; avoid algebraic loops; avoid using white noise for a few minutes

Model Display and Output Settings

1. Scope modules are used: although their impact is likely not obvious.

2. The dynamic refresh module is used. For example, dynamically refresh the displayed points on the plot graph.

 - If there are scopes or other visualization output devices open during simulation, these can reduce performance

 - Performance proportional to CPU speed

 - If the variables stored during simulation are larger than the amount of RAM on the system, the performance will be poor. The solution is to run a smaller simulation or increase the memory

        In addition, there are some modules that will significantly slow down the simulation speed, which are equivalent to the "short board" in "Barrel Theory". For example, the XY diagram in Simulink is a module that refreshes the drawing in real time. If there is this module in the mode, it will refresh the XY graph slowly during simulation, and the simulation speed will definitely not be fast. If there are other alternatives, the simulation speed can be accelerated, such as saving the data to the workspace first, and then drawing the XY diagram after the simulation is over. Some nonlinear modules, such as a nonlinear MOSFET model, naturally require longer simulation times than ideal switching.

        The fourth method is parallel execution and distributed execution, which is equivalent to multiple people completing a job together, provided that there is permission for parallel execution and permission for distributed execution. Parallel execution is to open multiple Matlabs on a computer with a multi-core CPU, and then automatically or manually allocate them for parallel processing; distributed execution is to process multiple computers separately after using high-speed network interconnection. The speedup of this simulation method is very significant, especially when dealing with a large number of iterative calculations, but it is not available to the average developer.

Performance testing and runtime comparison

Measuring performance

You can use tic the , toc and sim commands to compare the execution time of Accelerator or Rapid Accelerator mode with Normal mode.
  1. Open your model.

  2. Select Normal from the Simulation > Mode menu .

  3. ticUse the tocand simcommands at the command prompt to measure the time it takes for a model to simulate in Normal mode:

    tic,[t,x,y]=sim('myModel',10000);toc

    Use ticand toctogether to log and return elapsed time with a message like this:

    Elapsed time is 17.789364 seconds.
  4. Select Accelerator or Rapid Accelerator from the Simulation > Mode menu , then click the Run button to generate an executable for the model. Accelerated mode uses this executable in subsequent simulations as long as the model has not been structurally altered. Situations that can lead to model rebuilding are discussed in Code Regeneration in Accelerated Models .

  5. Rerun the compiled model at the command prompt:

    tic,[t,x,y]=sim('myModel',10000);toc
  6. The elapsed time shown shows the running time of the accelerated model. E.g:

    Elapsed time is 12.419914 seconds.
    

The difference between the two elapsed times (5.369450 seconds in this example) shows the speed gain obtained by accelerating the model.


source:

http://www.360doc.com/content/18/0503/15/55172235_750792155.shtml

https://zhidao.baidu.com/question/811590541818007132.html

https://ww2.mathworks.cn/help/simulink/ug/comparing-performance.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815732&siteId=291194637