MATLAB Answers

parfor error for global SA Morris method using SimBiology

2 views (last 30 days)
emjey
emjey on 26 Aug 2019
Commented: emjey on 26 Aug 2019
hi, I am trying to use parallel toolbox to speed up my global SA with Morris method. Basically it averages elementary effects for a specific parameters across the entire parameter space. An elementary effect is calculated as EE = (f(p) - f(p+delta))/delta. For more details see Wentworth et al. J. UNCERTAINTY QUANTIFICATION, Vol. 4, pp. 266–297 (https://projects.ncsu.edu/crsc/reports/ftp/pdf/crsc-tr15-01.pdf).
So far I failed with my attempts, such as for this code - here using SimBiology 'sbioselect' and 'sbiosimulate'
clear variables; close all; clc;
format shortG
r = 5; % number of trajectories
l = 10; s = 2;
delta = l/(s*(l-1));
disp("Load model & parameter file")
paramsFile = strcat('PARAMETERS/Lotka_parameters.csv');
paramsDF = readtable(paramsFile);
sbioloadproject('SBPROJ/lotka.sbproj','m1')
p = length(paramsDF.parameter);
% Morris ===========================================================
d = zeros(r,p);
for i = 1:r
f = zeros(1,(p+1));
% an example for the Morris C matrix capturing one trajectory in parameter space
C = [15 0.085556 15;
15 0.005 15;
15 0.005 9.4444;
9.4444 0.005 9.4444];
% for each trajectory for p parameters, there are p+1 evaluations to be made
for j = 1:(p+1)
for k = 1:size(C,2) % columns = parameters
blub = sbioselect(m1, 'Name', paramsDF.parameter(k));
blub.Value = C(j,k);
end
[t,y] = sbiosimulate(m1);
f(j) = y(end,varNo); % looking for the endpoint as the sensitivity function
end
% Calculating elementary effects
v = zeros(1,p);
for j = 1:p
Cindex = find(diff(C(:,j)) ~= 0);
v(j) = ((f(Cindex) - f(Cindex+1))/delta;
end
d(i,:) = v;
end
%% disp("Calculating EE statistics")
... based on d(i,j)
Although the code is correct from the syntaxt point of view, at run time I get the error:
Load model & parameter file
Starting parallel pool (parpool) using the 'local' profile ...
connected to 4 workers.
Error using sbiosimulate (line 136)
Expected input number 1, MOBJ, to be one of these types:
SimBiology.Model
Instead its type was double.
Error in test (line 19)
parfor i = 1:r
Any comments would be very appreciated. I attach lotka.sbproj and parametr file as well.
Best, M

  0 Comments

Sign in to comment.

Accepted Answer

Florian Augustin
Florian Augustin on 26 Aug 2019
Hi,
It looks like you are running into an issue that the SimBiology model is not copied to the workers natively. In MATLAB versions R2019a and earlier, you have to use parallel.pool.Constant to send the model to the workers in your parallel pool. The following tweak to your code should do this for you.
m1ParConst = parallel.pool.Constant(m1);
parfor i = 1:r
% ...
% Get the model from the parPool constant as follows:
blub = sbioselect(m1ParConst.Value, 'Name', paramsDF.parameter(k));
% ...
end
Let me know if this doesn't solve your problem.
Best
-Florian

  1 Comment

emjey
emjey on 26 Aug 2019
hi Florian, thanks for your suggestion, it worked.
I had to replace the other m1 as well of course and now it's 3x faster.
Great news, thanks a million!

Sign in to comment.

More Answers (0)

Products

Communities

More Answers in the  SimBiology Community


Translated by