Why does using set_param() in a for loop run Simulink models so fast when compared with the sim() function?
16 次查看(过去 30 天)
显示 更早的评论
Hi,
For the longest time I have been using the sim() function within for loops to programatically execute Simulink simulatons with iteratively changing inputs. Per iteration, the loop hangs while the simulation is running (which seems to take the same amount of time as if I were to manually run a single simulation).
However, today I was doing some reading and found the set_param() function, which can also be used to start simulations. This no longer causes the for loop to hang and seems to just send 'i' number of calls to run the simulation almost at once.
My question is, why is set_param() so much faster than sim()?
Is this running the simulations in parallel or is it something more else, like defaulting to some kind of accelerated mode with a call to set_param()? I did have a try changing between execution modes, fast restart, etc. but sim() still seems to run one-by-one very slowly.
For reference, here is the sort of code I am using to run my simulations using a script:
for i = 1:length(f_xg)
trans(i,1) = i;
trans(i,2) = f_xg(i);
w_xg = 2*pi*f_xg(i); %Convert input frequency to [rad/s]
ddx_g = -x_g*w_xg^2; %Calculate acceleration input amplitude [m]
%sim('Multi_layer_isolator')
set_param('Multi_layer_isolator',"SimulationCommand","start");
end
Thanks in advance for any contributions.
p.s. If you found this post looking into how to speed up your simulations, you may be in luck. This has resulted in my simulations completing within 9% of the time needed previously, reducing average execution time down from 6.5 sec per to 0.6 sec per. With 100 iterations, for example, that is 10.8 mins down to 1 min flat. Amazing!
Cheers
4 个评论
Paul
2023-10-24
At the risk of asking a silly question, did you verify that that results from your loop using sim or set_param are exactly the same? I don't see why they wouldn't be, but thought I'd ask anyway.
Can you expand on your comment that set_param seems to run the simulations in parallel when fast restart is enabled? I don't understand how that could be the case in the construct of the for loop in your code, and so would like to learn more about it. What is the evidence that multiple simulations may be running in parallel? Is this progress bar a result of the set_param command itself, or is it something you've constructed in your loop, using waitbar for example?
Fast restart can also be used with sim.
Also, it probaby won't address your question, but you may be interested in the workflow using the Simulink.SimulationInput object. Check its doc page for more info.
Based on my reading of the doc, "start" is used to start a simulation that is not currently running. "continue" is used to resume a simulation that's paused, if that's what you meant by "proceed from the existing state."
采纳的回答
Harimurali
2023-11-6
Hi Matt,
I understand that you want to know why using the “set_param” function inside a for loop run Simulink models much faster than when simulated using the “sim” function.
The “sim” function supports fast restart which ensure that the model is only compiled once, and simulation is not terminated when iterative simulations are performed. There are a few cases when a simulation does not support fast restart. Please refer to the Limitations section of the following documentation to learn about the limitations of fast restart:
Without fast restart, iterative simulations run using the “sim” function will appear slow.
Both the “set_param” and “sim” functions run simulations sequentially, but “set_param” may appear faster inside a for loop due to the following reasons:
- Overhead reduction: By using "set_param" to modify simulation parameters inside the loop, you avoid the overhead of re-initializing the model and recompiling the generated code, which can occur when calling "sim" repeatedly within the loop. This can result in some performance improvement.
- Caching effects: When "set_param" is used inside a loop, the model and its associated data may be cached in memory, reducing the overhead of loading, and initializing the model for each iteration. This can lead to faster execution times compared to running "sim" repeatedly within the loop.
Please refer the following links for more information on running simulations programmatically:
Hope this helps.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!