Run Simulink Model via parsim() and save after each simulation
조회 수: 5 (최근 30일)
이전 댓글 표시
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:
- Simulate model with simIn( j )
- Create matfile for above simulation
- 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?
댓글 수: 0
채택된 답변
Trent Gatz
2024년 7월 23일
편집: Trent Gatz
2024년 7월 23일
댓글 수: 5
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 Center 및 File Exchange에서 Run Multiple Simulations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!