Accessing variables from a Simulink model initFcn callback with parsim
    13 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Guillermo Rubio
 le 30 Avr 2025
  
    
    
    
    
    Commenté : Guillermo Rubio
 le 5 Mai 2025
            I'm running a model in parallel using parsim. I define all the variables used by the model with the setVariable method of a Simulink.SimulationInput object. All works fine until I try to access one of this variables from the initFcn callback. 
Aparently, the variables defined in the model workspace, that are accesible from the model itself, are not accessible from the callbacks. Is there any way of defining a variable for the SimulationInput object, that can be accessed from the model callbacks when using parsim?
This is a dummy example of what I'm trying to do:
%% Create an array of input data
n  = 10;
Av = (1:n)*50;
Bv = (1:n)*100;
t = 0:1e-3:10;
for i =1:n
    simInput.A = Av(i);
    simInput.B = Bv(i);
    in(i) = Simulink.SimulationInput('parsimTest');
    in(i) = in(i).setVariable('simInput',simInput,'Workspace','parsimTest');
    in(i) = in(i).setModelParameter('StopTime','7.5');
end
%% Run in paralell
out = parsim(in,'ShowSimulationManager','on');
And this is the model and the initFcn callback:

I already tried to use the option
'TransferBaseWorkspaceVariables','on'
within the parsim function. That would work if the variable I try to access in the initFcn is constant over the parallel simulations, but I need it to change.
Thanks in advance!
Guillermo
0 commentaires
Réponse acceptée
  Paul
      
      
 le 30 Avr 2025
        Hi Guillermo,
In my opinion, the documentation is very poor in explaining the interaction between workspaces, models, and model callback functions (particuarly the InitFcn) particuarly when running with Simulink.SimulationInput objects and parsim. My understanding, based primarily on observation, is as follows:
When running with Simulink.SimulationInput objects, the workspace precedence for model execution (not accounting for masks or perhaps other special cases) is:
Model Workspace -> Global Workspace -> Base Workspace.
Note that Global Workspace is unique to using Simulink.SimulationInput. When we use setVariable, "By default, when you do not specify the Workspace argument, variables on a SimulationInput ... object are scoped to a global workspace specific to each object."  However, the model InitFcn runs in the Base Workspace (just like it runs in the Base Workspace when not using Simulink.SimulationInput) as best as I can observe.
One solution that seems to work is to use the PreSimFcn to assign a variable in the Base Workspace of the worker, which will be visible to the InitFcn.
%% Create an array of input data
n  = 10;
Av = (1:n)*50;
Bv = (1:n)*100;
t = 0:1e-3:10;
for i =1:n
    simInput.A = Av(i);
    simInput.B = Bv(i);
    in(i) = Simulink.SimulationInput('parsimTest');
    in(i) = in(i).setVariable('simInput',simInput,'Workspace','parsimTest');
    in(i) = in(i).setModelParameter('StopTime','7.5');
    in(i) = in(i).setPreSimFcn(@(s) assignin('base','simInput',simInput));
end
%% Run in paralell
out = parsim(in,'ShowSimulationManager','on');
In this particular case, the same variable is being set in the Base Workspace of the worker and in the Model Workspace. My understanding is that the InitFcn will use the former and block parameters will use the latter (though they both have the same value in this case). Might be interesting to change value of simInput.A after the call to setVariable and before the call to setPreSimFcn to verify it works that way.
1 commentaire
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Run Individual Simulations dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

