Run simulink model in for loop

86 views (last 30 days)
SREEJITH V S
SREEJITH V S on 17 Sep 2020
Edited: SREEJITH V S on 18 Sep 2020
Hi,
I would like to run a simulink model and save results as .mat files in a for loop. I do this with for loop and working fine. Now, I would like to use parfor function. But I could not figure it out how it can be done. I am describing a simple model to explain my problem.
Matlab verson is : R2020a 64 bit in Windows pc, I have license for parallel computing tool box also.
-Simulink model consist of a variable voltage source connected across a resistor.
-Resistance of the resistor (R) changes in each for loop.
-Variable voltage source block creates sinusoidal voltage. The frequency of the voltage changes in each loop.
-Resistance and input to variable voltage source are defined in matlab work space. Simulink reads these parameters during simulation. In every occurance of the for loop these parameters vary.
-During simulation a scope records voltage across the resistor. After simulation it logs data to workspace as an array named 'volt'.
-This array volt is saved in the current directory as a .mat file with different file names.
With for loop the simulation goes perfectly fine. Code for for loop is given below.
clc
clear all
close all
for k=1:1:10;
f=k; %frequency of sine wave
R=50*k; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
voltage=4e-4*sin(2*pi*f*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
sim('Test.slx');
foldern=(['f',int2str(k)]);
save(foldern,'volt'); %Output from simscape scope 'volt' is saved as mat file with different names
Simulink.sdi.clear;
end
For using parfor, I tried different ways mentioned in the website. But I could not simulate. One of the try is given below. How can I modify my above code with for loop for using parfor?
clc
clear all
close all
% model = 'Test.slx';
myDictionaryObj = Simulink.data.dictionary.create('testdd2.sldd');
dd = 'testdd2.sldd';
spmd
Simulink.data.dictionary.setupWorkerCache
end
parfor k=1:1:10;
dictObj = Simulink.data.dictionary.open(dd);
sectObj = getSection(dictObj,'Design Data');
entryObj = getEntry(sectObj,'MODE');
temp = getValue(entryObj);
temp.Value = CtrlValues(index);
setValue(entryObj,temp);
%
f=k; %frequency of sine wave
R=50*k; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
voltage=4e-4*sin(2*pi*f*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
[volt]=runsimulation;
foldern=(['f',int2str(k)]);
save(foldern,'volt'); %Output from simscape scope 'volt' is saved as mat file with different names
discardChanges(dictObj);
close(dictObj);
end
spmd
bdclose(model)
Simulink.data.dictionary.cleanupWorkerCache
end
Please help me with this.
I am attaching .m file and .slx file of simulink model.
  2 Comments
Raymond Norris
Raymond Norris on 17 Sep 2020
Edited: Raymond Norris on 17 Sep 2020
Hi Sreejith,
Could you provide more info about how the model could not simulate? Are you getting errors, warnings, incorrect results, etc.?
I'm not a Simulink user and can't provide an answer, but if you haven't already, you might consider looking at parsim
Raymond
SREEJITH V S
SREEJITH V S on 18 Sep 2020
Hi Raymond,
Thank you for your suggestion. I have used parsim function and I could do the simulation by defining each variables using setVariable.
Now I have two more doubts remaining.
-I would like to bring a time series structure defined as 'wave' below in matlab workspace to simulink environment as I brought other variables using setVariables function. How can we do it?
-If I defined a constant 'A=10' in matlab workspace, is there any method other than setVariable to incorporate it in simulink variable list. I wanted to call A inside some simulink blocks. I am looking for some function which make all constants in the matlab workspace available to simulink during parsim simulation.
Thanks again.
Sreejith
model = 'Test';
load_system(model);
f=1:2:20;
numSims=length(f);
R=50*f; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
for k=1:length(f)
ff=f(k);
voltage=4e-4*sin(2*pi*ff*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
st(k)=wave;
end
clear wave
simIn(1:10) = Simulink.SimulationInput(model);
for idx = 1:10
simIn(idx) = setVariable(simIn(idx),'R',R(idx));
simIn(idx) = setVariable(simIn(idx),'x_v',1e4);
simIn(idx) = setVariable(simIn(idx),'ff',f(idx));
simIn(idx) = setVariable(simIn(idx),'dt',1e-4);
end
simOut = parsim(simIn)

Sign in to comment.

Answers (1)

Joel Van Sickel
Joel Van Sickel on 17 Sep 2020
Hello Sreejith,
please use the parsim function for this type of application. If you want to use parfor, there are ways to do it, but they are more complicated.
If you want to use parfor, here is some example code. This code uses the controls toolbox to linearize a model: You will need to configure the pool and multiple directories for each one to run in.
Regards,
Joel
  1 Comment
SREEJITH V S
SREEJITH V S on 18 Sep 2020
Edited: SREEJITH V S on 18 Sep 2020
Hi Joel,
Thank you for your suggestion. I have used parsim function and I could do the simulation by defining each variables using setVariable.
Now I have two more doubts remaining.
-I would like to bring a time series structure defined as 'wave' below in matlab workspace to simulink environment as I brought other variables using setVariables function. How can we do it?
-If I defined a constant 'A=10' in matlab workspace, is there any method other than setVariable to incorporate it in simulink variable list. I wanted to call A inside some simulink blocks. I am looking for some function which make all constants in the matlab workspace available to simulink during parsim simulation.
Thanks again.
Sreejith
model = 'Test';
load_system(model);
f=1:2:20;
numSims=length(f);
R=50*f; %value of resistor
dt=1e-4; %time step
t=0:dt:10; %time vector
for k=1:length(f)
ff=f(k);
voltage=4e-4*sin(2*pi*ff*t); %sinusoidal voltage
wave = struct(); %input to simulink wave block
wave.time = t';
wave.signals.values = [voltage'];
wave.signals.dimensions =1;
st(k)=wave;
end
clear wave
simIn(1:10) = Simulink.SimulationInput(model);
for idx = 1:10
simIn(idx) = setVariable(simIn(idx),'R',R(idx));
simIn(idx) = setVariable(simIn(idx),'x_v',1e4);
simIn(idx) = setVariable(simIn(idx),'ff',f(idx));
simIn(idx) = setVariable(simIn(idx),'dt',1e-4);
end
simOut = parsim(simIn)

Sign in to comment.

Categories

Find more on Variable Initialization 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!