MATLAB Answers

Block callback function in Simulink model cannot access workspace variable when executed with parsim

6 views (last 30 days)
David Bye
David Bye on 24 Dec 2020
Commented: David Bye on 14 Jan 2021
I have a Simulink model 'My Model', which uses a 'Check Static Range' block. In this block, assertion is enabled, and a callback function is defined. This function simply has the line of code
SatCount = SatCount+1;
The purpose is to count how many times a signal exceeds bounds.
The model is executed N times in parallel using parsim in wich all of the variables required to run the model are loaded in to the model workspace using code like the following
for p = 1:N
in(p) = Simulink.SimulationInput('My Model');
in(p) = in(p).setVariable('Var1',Var1,'Workspace','My Model');
in(p) = in(p).setVariable('Var2',Var2,'Workspace','My Model');
in(p) = in(p).setVariable('SatCount',0,'Workspace','My Model');
in(p) = in(p).setModelParameter('StopTime',1);
end
out = parsim(in,'ShowProgress', 'on');
When this is executed on a cluster machine using 12 workers, this fails and the following error messages are received
Error evaluating 'AssertionFcn' callback of Checks_SRange block (mask) 'My Model/Saturation Check'.
Callback string is 'evalin('base',callback)'
Caused by:
Unrecognized function or variable 'SatCount'.
I then tried moving the variable SatCount in to the base workspace using,
assignin('base','SatCount',0)
for p = 1:N
in(p) = Simulink.SimulationInput('My Model');
in(p) = in(p).setVariable('Var1',Var1,'Workspace','My Model');
in(p) = in(p).setVariable('Var2',Var2,'Workspace','My Model');
in(p) = in(p).setModelParameter('StopTime',1);
end
out = parsim(in,'ShowProgress', 'on');
But the same error message is received implying that the callback function cannot access the SatCount variable in either the base or model workspaces.
If I execute the same code above on my local workstation which does not have a Parallel Toolbox licence, the code executes correctly and the SatCount variable is updated, implying my local execution is using the base workspace to access SatCount.
Does anyone have an explaination as to why this does not run correctly with the Parallel Toobox?

Answers (2)

Rahul Kumar
Rahul Kumar on 11 Jan 2021
H David,
Variables specified on the SimulationInput object are not available in model or block callbacks. I would suggest using a PreSimFcn to set SatCount to zero before every simulation
in(p) = in(p).setPreSimFcn(@(~) assignin('base','SatCount', 0));
  1 Comment
David Bye
David Bye on 14 Jan 2021
Rahul,
Thanks for your comment. However when running parsim, it seems the block callback cannot access the variables in either the base or model workspace. I don't know whether you suggestion will work as I have not had time to try it.
I have now decided to disable the callback in the block and instead output an assertion signal. This assert signal feeds in to a 32-bit counter, and the final value is then saved to the model workspace. This works well.
David

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by