Using eval function for execute a power flow with DC system embedded

I use the eval function to store the matpower case that its control variable has been assigned and call matpower to run the power flow
....
eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);
eval(['initial_results_',num2str(i),'=runpf(''case_ieee30_test',num2str(i), '.mat'')']);
eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);
....
All of the code works great,but when I want to execute another power flow which DC system embedded I use 'runacdcpf' function. But it didn't work at all.. What could the problem? . I am using the following lines of code:
eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);
eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test','case5_stagg_MTDCslack' ,num2str(i), '.mat'')']);
eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);
the error were:
Error using loadcase (line 246)
loadcase: specified MAT file does not exist
Error in runacdcpf (line 109)
[baseMVA, bus, gen, branch] = loadcase(caseac);
Error in pso_orpd_edit (line 63)
eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test','case5_stagg_MTDCslack' ,num2str(i),
'.mat'')']);
Your help greatly appreciated!!

1 Kommentar

"What could the problem?"
Numbered variables, i.e. initial_results_1, initial_results_2, etc., are a sign that you are doing something wrong.
Putting meta-data (e.g. pseudo-indices) into variable names is a sign that you are doing something wrong.
Both of these mean that you force yourself into writing slow, complex, inefficient, buggy code to access your data:
The neat, simple, and very efficient approach is to use indexing with one array (e.g. numeric, cell, table, etc.).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Don't do that!
casefile = sprintf('case_ieee30_test%d.mat', i);
savecase(casefile, baseMVA, bus, gen, branch);
And do not use dynamic variable names: use cell arrays.
staggfile = sprintf('case5_stagg_MTDCslack%d.mat', i) ;
initial_results_{i} = runacdcpf(casefile, staggfile);
initial_losses_{i} = sum(real(get_losses(initial_results_{i})));

6 Kommentare

Sorry for the late reponse, thank you very much for your kind suggestion. It worked very well, but the graph is the new issue now..I don't know why..but the graph is not run dinamically (I use the case for AC only before plugging the DC system). Everything is going well when I use the 'eval' function, Can you give me another suggestion please.. Thanks in advance...
This is the graph which use the 'eval' function;
and this is the graph after editing the code;
We would need to see more code fo diagnose that -- the original eval code through to plotting, and the revised version.
Actually this is a conventional PSO algorithm that employs Matpower in power flow execution. so, I only describe the core parts of the discussion
%Initilization Parameters
iter=0;
iteration=100;
nvars = 12; %Number of variables
N = 50; %Number of Particles or Swarm size
%Acceleration constants
c1 = 2.05;
c2 = 2.05;
%Inertia Weight
w_max=0.9;
w_min=0.4;
w_temp(1)=w_max;
%load Data
[baseMVA, bus, gen, branch]=loadcase(case_ieee30);
%% Initialization of Swarm & velocity
Swarm=[unifrnd(0.95,1.10,N,6),unifrnd(0.90,1.10,N,4),unifrnd(0.00,0.20,N,2)];
%Initialize velocity
Velocity =[unifrnd(-0.003,0.003,N,6),unifrnd(-0.003,0.003,N,4), unifrnd(0.003,0.003,N,2)];
for i=1:N
v1=Swarm(i,1); %v1
bus(1,8)=v1; %Vm, column 8 is voltage magnitude (p.u.)
gen(1,6)=v1; %Vg, column 6 is voltage magnitude setpoint (p.u.)
...
qc24=Swarm(i,12); %Shunt capacitor 10, column 6 is BS
bus(24,6)=qc24;
eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);
eval(['initial_results_',num2str(i),'=runpf(''case_ieee30_test',num2str(i), '.mat'')']);
eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);
%Penalty for bus voltage violation
....
penalty_Vl_violation=sum(penalty_Vl);
...
%%Penalty for shunt violation
penalty_Qc_violation=sum(penalty_Qc);
%%Penalty for tap position violation
penalty_Tk_violation=sum(penalty_Tk);
%objective function=sum of active power losses of the transmission lines
losses(i)=eval(['initial_losses_',num2str(i)]); %sum of real power losses of all branches
Obj_fun_initial(i)=losses(i)+penalty_Vl_violation+penalty_Qc_violation+penalty_Tk_violation; %augumented objective function with penalty function
end
%% Initialize best position (Pbest) and global best postion (Gbest) matrix
Pbest=Swarm;
Val_Pbest=Obj_fun_initial;
%finding best particle in initial population
[Val_Gbest,m]=min(Val_Pbest);
Gbest=Swarm(m,:); %used to keep track of the best particle ever
Gbest_calc=repmat(Swarm(m,:),N,1);
%% PSO LOOP
figure('NumberTitle', 'off', 'Name', 'PSO Algorithm Based Optimal Reactive Power Dispatch');
title('ACTIVE POWER LOSS MINIMIZATION');
ylabel('Total Active Power Loss (MW)');
xlabel('Iteration Number');
grid on;
hold on
for iter=1:iteration
.....
....
.....
end
and this is the revised code (what is written is only revised part, the rest is the same):
casefile = sprintf('case_ieee30_test%d.mat', i);
savecase(casefile, baseMVA, bus, gen, branch);
initial_results_{i} = runpf(casefile);
initial_losses_{i} = sum(real(get_losses(initial_results_{i}));
.....
.....
losses(i)=initial_losses_{i}; %sum of real power losses of all branches
hopefully this can represent the whole code..Thanks in advance
NB:
Embedding DC parameters even worse, MATLAB can not read the MAT-file even I save the case by using 'sprintf' syntax.
This what I wrote:
casefile = sprintf('case_ieee30_test%d.mat', i);
savecase(casefile, baseMVA, bus, gen, branch);
staggfile = sprintf('case5_stagg_MTDCslack_test%d.mat', i) ;
savecase(casefile, baseMVAdc, busdc, convdc, branchdc);
initial_results_{i} = runacdcpf(casefile,staggfile);
initial_losses_{i} = sum(real(get_losses(initial_results_{i}));
....
....
losses(i)=initial_losses_{i}; %sum of real power losses of all branches
and this is the result
Error using loadcase (line 246)
loadcase: specified MAT file does not exist
Error in runacdcpf (line 109)
[baseMVA, bus, gen, branch] = loadcase(caseac);
Error in pso_orpd (line 64)
eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test',num2str(i), '.mat','case_ieee30_test',num2str(i), '.mat'')']);
I don't know why the 'savecase' didn't work for saving the case file.. once again please give me some suggestion..thanks a million..
%Initilization Parameters
iteration=100;
nvars = 12; %Number of variables
N = 50; %Number of Particles or Swarm size
%Acceleration constants
c1 = 2.05;
c2 = 2.05;
%Inertia Weight
w_max=0.9;
w_min=0.4;
w_temp(1)=w_max;
%load Data
[baseMVA, bus, gen, branch]=loadcase('case_ieee30.mat'); %CHANGED
%% Initialization of Swarm & velocity
Swarm=[unifrnd(0.95,1.10,N,6),unifrnd(0.90,1.10,N,4),unifrnd(0.00,0.20,N,2)];
%Initialize velocity
Velocity =[unifrnd(-0.003,0.003,N,6),unifrnd(-0.003,0.003,N,4), unifrnd(0.003,0.003,N,2)];
initial_losses_ = cell(N,1); %NEW
initial_results_ = cell(N,1); %NEW
losses = zeros(N,1); %NEW
Obj_fun_initial = zeros(N,1); %NEW
for i=1:N
v1=Swarm(i,1); %v1
bus(1,8)=v1; %Vm, column 8 is voltage magnitude (p.u.)
gen(1,6)=v1; %Vg, column 6 is voltage magnitude setpoint (p.u.)
...
qc24=Swarm(i,12); %Shunt capacitor 10, column 6 is BS
bus(24,6)=qc24;
casefile = sprintf('case_ieee30_test%d.mat', i); %CHANGED
savecase(casefile, baseMVA, bus, gen, branch); %CHANGED
staggfile = sprintf('case5_stagg_MTDCslack_test%d.mat', i) ; %CHANGED
savecase(staggfile, baseMVAdc, busdc, convdc, branchdc); %CHANGED
initial_results_{i} = runacdcpf(casefile,staggfile); %CHANGED
initial_losses_{i} = sum(real(get_losses(initial_results_{i}))); %CHANGED
%Penalty for bus voltage violation
....
penalty_Vl_violation=sum(penalty_Vl);
...
%%Penalty for shunt violation
penalty_Qc_violation=sum(penalty_Qc);
%%Penalty for tap position violation
penalty_Tk_violation=sum(penalty_Tk);
%objective function=sum of active power losses of the transmission lines
losses(i)=initial_losses_{i}; %sum of real power losses of all branches %CHANGED
Obj_fun_initial(i)=losses(i)+penalty_Vl_violation+penalty_Qc_violation+penalty_Tk_violation; %augumented objective function with penalty function %CHANGED
end
%% Initialize best position (Pbest) and global best postion (Gbest) matrix
Pbest=Swarm;
Val_Pbest=Obj_fun_initial;
%finding best particle in initial population
[Val_Gbest,m]=min(Val_Pbest);
Gbest=Swarm(m,:); %used to keep track of the best particle ever
Gbest_calc=repmat(Swarm(m,:),N,1);
%% PSO LOOP
figure('NumberTitle', 'off', 'Name', 'PSO Algorithm Based Optimal Reactive Power Dispatch');
title('ACTIVE POWER LOSS MINIMIZATION');
ylabel('Total Active Power Loss (MW)');
xlabel('Iteration Number');
grid on;
hold on
for iter=1:iteration
.....
....
.....
end
Thank you so much.. it works..

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by