Run Simulink Model via parsim() and save after each simulation
9 次查看(过去 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 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!