Best way to optimize iterative Simulink calls

Hi guys, hope you all are doing well.
I have a script that simulates a Simulink model for various parameters of a PI controller with the sim() function. At each iteration only the parameters of this controller change, and nothing else in the model. It is a fixed step simulation (0.5s step) with duration of 6000s.
When I simulate the script with a fixed pair of PI parameters, it takes roughly ~48s to end the simulation, whereas in the the simple loop I made it takes way too long per call (something like 1min20s approximately). Is there a way to optimize those sequential calls? I've read through some MATLAB docs but nothing really insightful came up.
The loop I made is something like:
for i = 1:n_tests
% Parameters of the PI
Kp_PI = Kp_value(i); Ti_PI = Ti_value(i);
sim('Simulink_model', 6000)
end

Answers (1)

I have no idea why a run in the loop takes so much longer than a single run not in the loop. Might be worth reaching out to Tech Support.
A good start in the doc pages is Running Multiple Simulations. In particular you can pre-define an array of Simulink.SimulationInput objects, and the use either sim() or parsim(), if you have the Parallel Computing Toolbox.
For sure, the parsim() should be faster than calling sim() in a loop. Don't know if calling sim() with an array of Simulink.SimulationInput objects would be faster than the loop you wrote. Worth a look though.

7 Comments

Ty for the answer. I'll give it a try for parsim.
If parsim with a Simulink.SimulationInput array as input works out for you, would you mind reporting the timing results/comparison back here? I'm curious about this.
Another factor to consider .... is the simulation running in Normal mode or one of the Accelerator modes? If the latter, is it possible that changing those parameters causes the entire simulation to be rebuilt each iteration through the loop?
I'm not working on the project right now, but as soon as I get into it I'll make the results available for you here!
I was actually always running on Normal mode. It is definitely weird if I'm being honest. One thing I got from measuring the simulation time for a small loop earlier is that the lime lapsed is somewhat inconsistent in sequential calls: for the first 2 iterations it is consistent with ~48-60s, but then it goes for 1min20s+. I know that the range of the parameters I'm testing are in a stable region for the model so it shoudn't affect its beahaviour that much.
I'm not working on large scale model - is somewhat medium size. But I think the process of recompilling it for some reason is taking way too long (could be wrong tho).
I tried the simplest model I could think of and saw a single run take 41 seconds and a 10-run loop take 411 seconds. Given that your first two runs take around the expected time, and then run time goes up, I wonder if you're running into some memory issues where maybe the first two runs exhaust the RAM and the next runs have to go out to disk? That would be a lot of output though. I'm pretty sure there are ways to time how long each phase of the simulation takes, but I've not tried that myself.
Yeah I thought I was having some leaking issues but when I checked for a loop of 50 simulations, the RAM usage was capped at ~70% (on a 8gb laptop) and the disk usage was low (like, 1%) so idk. Super weird.
According to this thread
out = sim('model');
out.SimulationMetadata.TimingInfo % shows the timing for all phases of the simulation

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Products

Release

R2020a

Tags

Asked:

on 1 Nov 2021

Commented:

on 9 Nov 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!