Run Simulink Model via parsim() and save after each simulation

7 views (last 30 days)
I am trying to run my simulink model in parallel while saving the results to a matfile after each simulation has completed.
Each worker should take the following action:
  1. Simulate model with simIn( j )
  2. Create matfile for above simulation
  3. Repeate with simIn ( K ) until all simulations are complete.
The below code is my starting point. "test_model" is a simulink model of only a sin wave going into a gain, integrator, and to workspace block simply to test out parsim().
%% Example of how to run simulink models in parallel
clear; close all; clc;
mdl = "test_model";
amp= 1:10;
freq = 1:10;
nSims = length(amp);
simIn(1:nSims) = Simulink.SimulationInput(mdl);
% Setup model workspace
k = 5;
for i =1:nSims
nm = [num2str(i) '.mat'];
simIn(i) = setVariable(simIn(i), "amp", amp(i), 'Workspace', mdl);
simIn(i) = setVariable(simIn(i), "freq", freq(i), 'Workspace', mdl);
simIn(i) = setPostSimFcn(simIn(i), @(x)postSim(x, simIn(i), nm));
end
out = parsim (simIn, 'UseFastRestart','on', 'TransferBaseWorkspaceVariables','on');
function postSim(out, in, nm)
% postSim - Run @ completion of each simulation
% Saves simulation input, output, and name to file "nm"
m = matfile(nm, 'Writable', true);
m.out = out;
m.in = in;
m.nm = nm;
end
I understand that save() does not work as expected in parallel, so I've implemented the example from the question below as a method of creating my mat files. The above code works exactly as expected if "parsim" is replaced with "sim"; however, no files are created when using "parsim".
Note: setting 'TransferBaseWorkspaceVariables' to 'off' and setting k via setVariable does not have any impact on this behavior.
Ask: How can I modify the above code to also work with the parsim command?

Accepted Answer

Trent Gatz
Trent Gatz on 23 Jul 2024
Edited: Trent Gatz on 23 Jul 2024
I still believe this to be an error with parsim(); however, one workaround is to use:
%out = parsim(simIn);
parfor i = 1:nSims
out(i) = sim(simIn(i));
end
  5 Comments
Trent Gatz
Trent Gatz on 25 Jul 2024
I need to save to a matfile after each simulation as the output is large and keeping it all in RAM wouldn't be possible. Additionally, if MATLAB were to crash or my computer were to restart / lose power, I'd like to maintain this checkpoint of progress completed rather than starting over with no simulations completed.
Paul
Paul on 25 Jul 2024
Edited: Paul on 25 Jul 2024
According to Data Logging for Multiple Simulations you can go to Model Settings -> Data Import/Export check the box for "Log data set to file" and specify the file name (call it 'logfile.mat' for sake of discussion)
After executing
simOut = parsim(simIn)
the files logfile_1.mat, log_file_2.mat, etc. will be in the directory from which parsim was executed (maybe the output directory can be changed?).
When I experimented, these file contain the signals that were marked for logging in the block diagram, but do not include anything else that shows up in simOut, like ToWorkspace data and the simulation metadata (and the simOut will not include the logged data) and won't include the simIn either (but simIn is known a priori and can be saved before starting parsim).
You can also turn on the logging to file in the SimIn object if you don't want to change the model itself. See this thread.
Don't know when these log files are actually written back so have no idea what the state of things would be if the parsim stops executing before it's complete.

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!