Run simulink model in for loop

64 ビュー (過去 30 日間)
SREEJITH V S
SREEJITH V S 2020 年 9 月 17 日
編集済み: SREEJITH V S 2020 年 9 月 18 日
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 件のコメント
Raymond Norris
Raymond Norris 2020 年 9 月 17 日
編集済み: Raymond Norris 2020 年 9 月 17 日
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 2020 年 9 月 18 日
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)

サインインしてコメントする。

回答 (1 件)

Joel Van Sickel
Joel Van Sickel 2020 年 9 月 17 日
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 件のコメント
SREEJITH V S
SREEJITH V S 2020 年 9 月 18 日
編集済み: SREEJITH V S 2020 年 9 月 18 日
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)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Logging についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by