Using eval function for execute a power flow with DC system embedded
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-10-29
"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.).
采纳的回答
Walter Roberson
2020-10-27
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 个评论
Walter Roberson
2020-10-29
%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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Estimate Parameters and States 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!