Help needed with code running parallel simulations of Simulink model

1 view (last 30 days)
Hi there!
I have created a Simulink model of a grid-connected photovoltaic system and I now need to run a large number of simulations for different values of some input parameters and record the steady state AC power output for each simulation. Therefore, I have written the following code which runs the simulations using the sim command inside a parfor loop:
clear all;
clc;
% Import T,G,Rs,Rp data for the day to be simulated from the Excel file
% ---------------------------------------------------------------------
filename='sim_day.xlsx';
data=xlsread(filename);
T=data(:,1);
G=data(:,2);
Rs=data(:,3);
Rp=data(:,4);
Rser=Rs*7;
Rpar=Rs*7;
num=length(T);
% Initialise the vector which will store the values of the output power of
% the PV system for each pair of (T,G) measurements
Pout=zeros(num,1);
model='my_PV_system_validation2';
load_system(model);
matlabpool('open');
spmd
addpath(pwd);
currDir=pwd;
addpath(currDir);
tmpDir=tempname;
mkdir(tmpDir);
cd(tmpDir);
load_system(model);
end
% Execute the model simulation for each measurement
% -------------------------------------------------
parfor i=1:num
set_param([model '/T (oC)'],'Value',num2str(T(i)));
set_param([model '/G (W//m2)'],'Value',num2str(G(i)));
set_param([model '/PV ARRAY/Rs'],'Value',num2str(Rs(i)));
set_param([model '/PV ARRAY/Rp'],'Value',num2str(Rp(i)));
set_param([model '/PV ARRAY/Rs*Nser//Npar'],'Resistance',num2str(Rser(i)));
set_param([model '/PV ARRAY/Rp*Nser//Npar'],'Resistance',num2str(Rpar(i)));
simOut=sim(model);
Pout(i)=Pac;
end
spmd
cd(currDir);
rmdir(tmpDir,'s');
rmpath(currDir);
end
close_system(model);
matlabpool('close');
% Export the calculated values of the output power of the PV system to the
% Excel file
% ------------------------------------------------------------------------
xlswrite(filename, Pout, 'F1:F4');
So the code basically imports the values of the parameters from an Excel file, creates a pool of workers and a separate directory for each, loads the Simulink model on each worker and then inside the parfor loop it passes the parameter values to the Simulink model and runs the simulation. The only thing I am really interested in getting as a result from each simulation is a single value i.e. the AC power output Pac. In the Simulink model I am using a 'To Workspace' block to get the value of Pac and i have configured it to keep only the last value of Pac by choosing 'Limit data points to last'=1 (for the same purpose I have also tried using a Scope with the 'Save data to workspace' field checked). Afterwards, the values of Pac obtained from each simulation should be stored to the array Pout and written back to the Excel file.
When I run the above code though I get the following error:
Analyzing and transferring files to the workers ...
Error using FINAL_VALIDATION (line 41)
An UndefinedFunction error was thrown on the workers for 'Pac'. This might be because the
file containing 'Pac' is not accessible on the workers. Use addAttachedFiles(pool, files)
to specify the required files to be attached. See the documentation for
'parallel.Pool/addAttachedFiles' for more details.
Caused by:
Undefined function or variable 'Pac'
I know that the simulations themselves run fine and the error occurs right after the end of a simulation. For some reason it seems that it can't find Pac but I don't know why. When I try to run a single simulation of the Simulink model from the MATLAB command line using simOut=sim('my_PV_system_validation2'); then Pac is there and with the correct value... So I'm guessing it has to do with the workers but I can't figure out how exactly to correct it...
So any help would be much appreciated!
George

Answers (0)

Categories

Find more on Schedule Model Components in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!