필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Trent Gatz
Trent Gatz 2024년 7월 23일
편집: Paul 2024년 7월 25일
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?

채택된 답변

Trent Gatz
Trent Gatz 2024년 7월 23일
편집: Trent Gatz 2024년 7월 23일
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
Trent Gatz
Trent Gatz 2024년 7월 25일
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 2024년 7월 25일
편집: Paul 2024년 7월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Run Multiple Simulations에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by