Array of SimulationInput objects gets modified upon running with sim command

5 visualizaciones (últimos 30 días)
I am creating an array of SimulationInput objects, 'simIn'. If I try to run a single simulation, for example using simOut(1) = sim(simIn(1)) to run the first case, why does simIn lose all other array elementes. It changes to a 1x1 SimulationInput object, whereas before the sim command it was a 1x28 object
for casenumber = 1:NumFile
if ~isempty(char(file{FileIdx(casenumber),1}))
if exist(strrep(char(file{FileIdx(casenumber),1}), ';', ''), 'file')
%% Setup parallel Simulation
simIn(casenumber)=Simulink.SimulationInput('XWB_MSL_EEC');
simIn(casenumber) = simIn(casenumber).setVariable('Experiment',Experiment);
simIn(casenumber) = simIn(casenumber).setVariable('Enable_MSL_Validation_Logging',Enable_MSL_Validation_Logging);
simIn(casenumber) = simIn(casenumber).setVariable('SimStartTime',0);
simIn(casenumber) = simIn(casenumber).setVariable('TimeMode1',TimeMode1);
simIn(casenumber) = simIn(casenumber).setVariable('TimeMode2',TimeMode2);
simIn(casenumber) = simIn(casenumber).setVariable('parallel_casenumber',casenumber);
simIn(casenumber) = simIn(casenumber).setVariable('ModelPath',ModelPath);
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('resultsDir',resultsDir);
simIn(casenumber) = simIn(casenumber).setVariable('switchExportDataToPhobos',switchExportDataToPhobos);
if ~isempty(Suffix)
simIn(casenumber) = simIn(casenumber).setVariable('CurveName',sprintf('%s_%s',file{FileIdx(casenumber),1},Suffix));
else
simIn(casenumber) = simIn(casenumber).setVariable('CurveName',file{FileIdx(casenumber),1});
end
% Use a temporary variable for the SimIn element to avoid runaway memory usage when using a preSimFcn with parsim
tmpSimIn = simIn(casenumber);
preSimFcnInputs = {file{FileIdx(casenumber)}, tmpSimIn};
simIn(casenumber)=simIn(casenumber).setPreSimFcn(@(x) parsimMSLPreSimFcn(preSimFcnInputs));
simIn(casenumber) = simIn(casenumber).setPostSimFcn(@(y) parsimMSLPostSimFcn(y, tmpSimIn));
else
error(['Could not find Case "',strrep(char(file{FileIdx(casenumber),1}), ';', ''),'"!'])
end
end
end
  4 comentarios
Swastik Sarkar
Swastik Sarkar el 20 de Ag. de 2024
Hi @Afzal,
Could you please attach your model, along with the complete code for the PreSim and PostSim functions?
From my understanding, the SimIn variable is local to the PreSimFcn function's workspace.
Sahas
Sahas el 26 de Ag. de 2024
Hi @Afzal,
As per my understanding, the "parsimMSLPreFcn" function takes "simIn" as an input but does not modify it.
I can offer better assistance if the model and code are shared.

Iniciar sesión para comentar.

Respuestas (1)

Githin George
Githin George el 3 de Dic. de 2024
Editada: Githin George el 3 de Dic. de 2024
Hi Afzal,
Without looking at the implementation for the “PreSimFcn” and “PostSimFcn” it is difficult to predict how the “simIn” variable in base workspace is getting affected.
One thing you can try is to wrap this code in a MATLAB class and set a “PostSet” event listener to the property containing the “simIn” object. Then you can use breakpoints in the listener function and check the call stack every time it is being hit to root cause the issue. I’ve tested this out with an example below. I hope this debugging trick comes in handy.
classdef SimulationManager < handle
properties (SetObservable)
% Array of Simulink.SimulationInput objects
SimulationInputs (1,:) Simulink.SimulationInput = Simulink.SimulationInput.empty
end
methods
function obj = SimulationManager()
% Constructor: Add listener for postset event
addlistener(obj, 'SimulationInputs', 'PostSet', @(src, evt) obj.onSimulationInputsChanged());
end
function runSimulation(obj)
% Run simulation with pre and post simulation functions
for i = 1:length(obj.SimulationInputs)
% Call the pre-simulation function
obj.preSimulation(obj.SimulationInputs(i));
% Here you would typically run the simulation
% For example: simOut = sim(obj.SimulationInputs(i));
fprintf('Running simulation for input %d...\n', i);
% Call the post-simulation function
obj.postSimulation(obj.SimulationInputs(i));
end
end
end
methods (Access = private)
function onSimulationInputsChanged(obj)
% Callback function for postset event
fprintf('SimulationInputs property changed.\n');
% You can add more debug information here
disp(obj.SimulationInputs);
end
function preSimulation(obj, simIn)
% Dummy pre-simulation function
fprintf('Pre-simulation setup for model: %s\n', simIn.ModelName);
% Insert pre-simulation logic here
end
function postSimulation(obj, simIn)
% Dummy post-simulation function
fprintf('Post-simulation cleanup for model: %s\n', simIn.ModelName);
% Insert post-simulation logic here
end
end
end
For more information refer to the documentation below:
  1 comentario
Afzal
Afzal el 3 de Dic. de 2024
I want to close this question. The issue was due to a assignin base command I was using in the PreSimFcn. This was also assigining the simIn from the inputs to the PreSimFcn (tmpSimIn)to the base workspace. This was used to assign variables required by the model to the base workspace. I just added a check to skip assigning simIn.
% Assign the variables to the parallel simulation instance base workspace
varList=whos;
for iVar=1:length(varList)
if ~strcmp(varList(iVar).name,'simIn') % Do not overwrite base 'simIn'
assignin('base',varList(iVar).name,eval(varList(iVar).name))
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Modeling 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