Accessing workspace variables with parallel workers
24 ビュー (過去 30 日間)
古いコメントを表示
I have a simulink model that I would like to run a number of times over a range of 2 different variables. In my simulink model I was using model callbacks to take the 2 random initial variables and calculate the initial conditions to some of my integrators but this wasn't working and now I am doing this in the for loops. However when I run the simulation it throws an error saying "Warning: Error in the simulation was caused by missing variable 'q_e2b0'. Set "TransferBaseWorkspaceVariables" option to "on" to fix the issue." I tried setting that in setModelParameters but that doesn't work either.
Below is a pseduo code of my script:
num_sims = length(phi_range)*length(theta_range);
simIn(1:num_sims) = Simulink.SimulationInput('SixDOFSimulink');
sim_itr = 1;
for i = 1:length(phi_range)
for j = 1:length(theta_range)
phi0 = phi_range(i);
theta0 = theta_range(j);
% do calculations %
pos0 = %value%
v_b0 = %value%
w0 = %value%
q_e2b_0 = %value%
simIn(sim_itr) = simIn(sim_itr).setModelParameter('SimulationMode', 'normal', ...
'SaveTime', 'on', ...
'SaveOutput', 'on');
simIn(sim_itr) = simIn(sim_itr).setVariable('theta0', theta0, 'workspace', 'SixDOFSimulink');
simIn(sim_itr) = simIn(sim_itr).setVariable('phi0', phi0, 'workspace', 'SixDOFSimunlink');
simIn(sim_itr) = simIn(sim_itr).setVariable('q_e2b0', q_e2b0, 'workspace', 'SixDOFSimunlink');
simIn(sim_itr) = simIn(sim_itr).setVariable('v_b0', v_b0, 'workspace', 'SixDOFSimunlink');
simIn(sim_itr) = simIn(sim_itr).setVariable('pos0', pos0, 'workspace', 'SixDOFSimunlink');
simIn(sim_itr) = simIn(sim_itr).setVariable('w0', w0, 'workspace', 'SixDOFSimunlink');
sim_itr = sim_itr+1;
end
end
out = parsim(simIn, 'ShowProgress', 'on');
delete(gcp('nocreate'))
2 件のコメント
Paul
2024 年 11 月 11 日
Is the variable q_e2b_0 as specified prior to the simIn assignments, or is it q_e2b0 (w/o the second underbar) as in the third simIn assignment, (both for the variable name and the value)?
回答 (1 件)
Paul
2024 年 11 月 12 日
It sounds like q_e2b0 is supposed to be a base workspace variable that's not really a variable that's used in the simulation, like as a block parameter. Maybe q_e2b0 is used in an InitFcn callback or something similar (I'm only assuming that variables used an InitFcn don't get applied via setVariable).
Anyway, the TransferBaseWorkspaceVariables is a name/value directive in the parsim command, though the linked doc page suggests there might be better alternatives. And, of course, make sure the variable with the correct name is defined in the base workspace to be transferred.
2 件のコメント
Guillermo Rubio
2025 年 4 月 29 日
編集済み: Guillermo Rubio
2025 年 4 月 29 日
I will use this thread to ask my question here, since I'm having more or less the same problem as @George.
Why "variables used in an InitFcn don't get applied via setVariable"?
Is there any way of setting variables so that they can be used by the initFcn callback when using parsim?
Im using the setVariable method of Simulink.SimulationInput to define all the variables required by the model I'm using with parsim, but for some reason the model callback functions can't recognise them.
Paul
2025 年 4 月 29 日
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 use the 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. For example, I have a simulation inittest.slx that has an InitFcn with the line
gain = wsgain;
and "gain" is a block parameter in the model. The following worked for me
s(1:2) = Simulink.SimulationInput('inittest');
s(1) = setPreSimFcn(s(1),@(s) assignin('base','wsgain',10));
s(2) = setPreSimFcn(s(2),@(s) assignin('base','wsgain',100));
out = parsim(s)
Of course, because "gain" is a block parameter, I could have used setVariable, but I wanted to work through the InitFcn for purposes of this discussion.
参考
カテゴリ
Help Center および File Exchange で General Applications についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!