Why does using set_param() in a for loop run Simulink models so fast when compared with the sim() function?
Show older comments
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 Comments
Walter Roberson
on 24 Oct 2023
If I understand correctly, "start" is able to proceed from the existing state, without having to re-initialize.
However, you would typically need to configure an end time if you are stepping state.
If you are wanting to run the same simulation over a number of different values for the same parameters then parsim
Matt
on 24 Oct 2023
Paul
on 24 Oct 2023
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."
Matt
on 31 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on Model, Block, and Port Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!