How to make variables assigned to SimulationInput object accessible to a PostSimFunction when using parsim

2 visualizaciones (últimos 30 días)
I am trying to save logged data in SimulationOutput objects to .mat files when running simulations in parallel, so I can clear the data from the SimulationOutput object to reduce memory usage. There's also another function (writeMSLDataToCSVFile) that writes some of the logged data to csv files that needs to run in the PostSimFunction. The .mat filename needs to be read from an array, 'extract', using the index 'casenumber', which is the parallel case number. How can I make the array, 'extract', and the 'casenumber' variable accessible to the PostSimFunction. There ars some other variables that need to be available to the PostSimFunction as well, as these are requtied by the writeMSLDataToCSVFile function.
I am assigning 'extract' and 'casenumber' to the SimulationInput object. Is there a way to pass this to the PostSimFunction, so it can get the variables from it.
This is what I've tried
for casenumber = 1:length(cases)
%% Setup parallel Simulation
simIn(casenumber) = Simulink.SimulationInput('XWB_MSL_EEC');
simIn(casenumber) = simIn(casenumber).setVariable('Experiment',Experiment);
simIn(casenumber) = simIn(casenumber).setVariable('parallel_casenumber',casenumber);
simIn(casenumber) = simIn(casenumber).setVariable('toolGitStatus',toolGitStatus);
simIn(casenumber) = simIn(casenumber).setVariable('configGitStatus',configGitStatus);
simIn(casenumber) = simIn(casenumber).setVariable('toolConfigVersion',toolConfigVersion);
simIn(casenumber) = simIn(casenumber).setVariable('configGitInfo',configGitInfo);
simIn(casenumber) = simIn(casenumber).setVariable('switchBatchMode',switchBatchMode);
simIn(casenumber) = simIn(casenumber).setVariable('extract',extract);
simIn(casenumber) = simIn(casenumber).setVariable('resultsDir',resultsDir);
simIn(casenumber) = simIn(casenumber).setVariable('switchExportDataToPhobos',switchExportDataToPhobos);
% Use a temporary variable for the SimIn element to avoid runaway memory usage when using a preSimFcn with parsim
tmpSimIn = simIn(casenumber);
preSimFcnInputs = {runScriptName, tmpSimIn};
simIn(casenumber)=simIn(casenumber).setPreSimFcn(@(x) parsimMSLPreSimFcn_XLS(preSimFcnInputs));
simIn(casenumber) = simIn(casenumber).setPostSimFcn(@(y) parsimMSLPostSimFcn_XLS(simOut, tmpSimIn));
end
Then I have setup the PostSimFcn as
function simOut = parsimMSLPostSimFcn_XLS(simOut, simIn)
% Extract required variables from simIn to the function's workspace
varList = {'Experiment', 'toolGitStatus', 'configGitStatus', ...
'toolConfigVersion', 'configGitInfo', 'extract', ...
'parallel_casenumber', 'resultsDir', 'switchExportDataToPhobos', 'switchBatchMode'};
nVarsAssigned = 0;
for iVar = 1:length(simIn.Variables)
if nVarsAssigned == length(varList)
break
elseif ~isempty(find(strcmp(simIn.Variables(iVar).Name,varList), 1))
eval(strcat(simIn.Variables(iVar).Name,'=simIn.Variables(iVar).Value;'));
nVarsAssigned = nVarsAssigned + 1;
end
end
%% generate MSL results files
CurveName = char(extract(parallel_casenumber+1,2));
enginACT = simOut.get('enginACT');
enginUSR = simOut.get('enginUSR');
engout = simOut.get('engout');
engEEC = simOut.get('engEEC');
engInData = simOut.get('engInData');
engOutData= simOut.get('engOutData');
EEC_Log = simOut.get('EEC_Log');
MODE = simOut.get('MODE');
HNSI = simOut.get('HNSI');
save(fullfile(resultsDir,[CurveName,'.mat']), 'enginACT', 'enginUSR', 'engout', 'engEEC', 'engInData', 'engOutData', 'EEC_Log', 'MODE', 'HNSI')
%% Write logged MSL data to csv files
writeMSLDataToCSVFile;
% Clear simOut
simOut.enginACT = [];
simOut.enginUSR = [];
simOut.engout = [];
simOut.engEEC = [];
simOut.engInData = [];
simOut.engOutData = [];
simOut.EEC_Log = [];
simOut.MODE = [];
simOut.HNSI = [];
end
However, this implementation does not seem to work as expected. I get the error below when trying to run a single case as
simOut = sim(simIn(1));
Error executing 'PostSimFcn' on SimulationInput object.
Caused by:
Error using @(y)parsimMSLPostSimFcn_XLS(simOut,tmpSimIn) (line
119)
Unrecognized function or variable 'simOut'. - Show complete stack trace

Respuesta aceptada

Paul
Paul el 23 de Mayo de 2024
Hi Afzal,
I think that the simOut from the simulation being called with sim is the argument sent into the PostSimFcn. Does the following change work?
%simIn(casenumber) = simIn(casenumber).setPostSimFcn(@(y) parsimMSLPostSimFcn_XLS(simOut, tmpSimIn));
simIn(casenumber) = simIn(casenumber).setPostSimFcn(@(y) parsimMSLPostSimFcn_XLS(y, tmpSimIn));

Más respuestas (0)

Categorías

Más información sobre Run Multiple Simulations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by