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

12 views (last 30 days)
David Bye
David Bye on 24 Dec 2020
Commented: Tong Zhao on 23 Apr 2022
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));
  2 Comments
Tong Zhao
Tong Zhao on 23 Apr 2022
Rahul: Actually you can grab values from SimulationInput object during PreSimFcn, by accessing the wanted SimulationInput object properties during the PreSimFcn call. This is a nice workaround that worked for me. For example:
function in = my_presimfun(in)
assignin('base','SatCount',in.Variables(1).Value);
end

Sign in to comment.


Nitin Kapgate
Nitin Kapgate on 13 Jan 2021
You can refer to a similar question answered here to resolve your issue. The issue described in this question is very similar to the one that is described in the link provided.

Community Treasure Hunt

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

Start Hunting!

Translated by