%This scripts write MATPOWER file for loop network
%W. A. Bukhsh Oct 2012

clear all

%% Bus Characteristics
n=14;   %size of the loop network
PD=95;  %Real Deamand(MW)
QD=20;   %Reactive Demand (Mvars)
vmin=0.95;
vmax=1.05;
relax=0;        %relax=1: relax voltage lower bound at load buses and relax=0 otherwise
relaxvol=0;
%% Generator Characteristics
Pmax=10000;
Pmin=0;
Qmax=20000;
Qmin=-20000;
Vg=1;%voltage base for generators (Only used for power flow)
%% Generator Cost Characteristics
costmod=2;
startup=0;
shutdown=0;
ncoff=3;
quad=0;
lin=2;
const=0;

%% Transmission Line Characteristics
r=0.01;
x=0.05;
b=0;
rateA=2500;
rateB=2500;
rateC=2500;
ratio=0;
angle=0;
%% Initial Conditions
Vm(1:n)=0.95;
Vm(1:2:n)=0.95;         %Initial Condition for load buses
Va(1:n)=linspace(-360,0,n);%Initial condition for loop solution
Va(1)=0; %zero angle at slack bus
Vmin(1:n)=vmin;
Vmax(1:n)=vmax;
if relax
    Vmin(2:2:n)=relaxvol;
end    
%%
file=['caseloop' num2str(n) '.m'];   %MATPOWER CASE File name
fid=fopen(file,'w');

fprintf(fid,'function mpc = %s\n',file(1:numel(file)-2));
fprintf(fid,'mpc.version = ''2'';\n');
fprintf(fid,'mpc.baseMVA = 100;\n');
%%Write Bus data in mpc.bus
fprintf(fid,'mpc.bus = [\n');
for p=1:n
    fprintf(fid,'%d\t',p);
    if p==1
        fprintf(fid,'%d\t',3);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',1);
        fprintf(fid,'%4.2f\t',Vm(p));  %Vm
        fprintf(fid,'%4.2f\t',Va(p));  %Va
        fprintf(fid,'%d\t',345);%BaseKVA
        fprintf(fid,'%d\t',1);  %Zone
        fprintf(fid,'%4.2f\t',Vmax(p));%Vmax
        fprintf(fid,'%4.2f;\n',Vmin(p));  %Vmin
    elseif rem(p,2)==0
        fprintf(fid,'%d\t',1);
        fprintf(fid,'%d\t',PD);
        fprintf(fid,'%d\t',QD);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',1);
        fprintf(fid,'%4.2f\t',Vm(p));
        fprintf(fid,'%4.2f\t',Va(p));
        fprintf(fid,'%d\t',345);  %BaseKVA
        fprintf(fid,'%d\t',1);    %Zone
        fprintf(fid,'%4.2f\t',Vmax(p));%Vmax
        fprintf(fid,'%4.2f;\n',Vmin(p));  %Vmin
    else
        fprintf(fid,'%d\t',2);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',0);
        fprintf(fid,'%d\t',1);
        fprintf(fid,'%4.2f\t',Vm(p));
        fprintf(fid,'%4.2f\t',Va(p));
        fprintf(fid,'%d\t',345);  %BaseKVA
        fprintf(fid,'%d\t',1);    %Zone
        fprintf(fid,'%4.2f\t',Vmax(p));%Vmax
        fprintf(fid,'%4.2f;\n',Vmin(p));  %Vmin
    end
end
fprintf(fid,'];\n');

%% Write Generator data in mpc.gen
fprintf(fid,'mpc.gen = [\n');
for p=1:2:n
fprintf(fid,'%d\t',p);%Bus Number
fprintf(fid,'%4.2f\t',PD);%PD
fprintf(fid,'%4.2f\t',QD);%QD
fprintf(fid,'%6.2f\t',Qmax);%Qmax
fprintf(fid,'%6.2f\t',Qmin);%Qmin
fprintf(fid,'%4.2f\t',Vg);%Vg Base
fprintf(fid,'%d\t',100);%m Base
fprintf(fid,'%d\t',1);%Status
fprintf(fid,'%d\t',Pmax);%Pmax
fprintf(fid,'%d\t',Pmin);%Pmin

fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d\t',0);
fprintf(fid,'%d;\n',0);
end
fprintf(fid,'];\n');


%% Write Branch data in mpc.branch
fprintf(fid,'mpc.branch = [\n');
for p=1:n
fprintf(fid,'%d\t',p);%From Bus
if p~=n
    fprintf(fid,'%d\t',p+1);%To Bus
else
    fprintf(fid,'%d\t',1);%To Bus
end

fprintf(fid,'%6.5f\t',r);%Resistance
fprintf(fid,'%6.5f\t',x);%Reactance
fprintf(fid,'%6.5f\t',b);%Suspectance
fprintf(fid,'%4.2f\t',rateA);%RateA
fprintf(fid,'%4.2f\t',rateB);%RateB
fprintf(fid,'%4.2f\t',rateC);%RateC
fprintf(fid,'%4.2f\t',ratio);%Ratio
fprintf(fid,'%4.2f\t',angle);%Angle
fprintf(fid,'%d\t',1);%status
fprintf(fid,'%4.2f\t',-360);%Angle min
fprintf(fid,'%4.2f;\n',360);%Angle max
end
fprintf(fid,'];\n');

%%
fprintf(fid,'mpc.areas =[\n');
fprintf(fid,'1 1;\n');
fprintf(fid,'];\n');

%% Write Cost data in mpc.gencost
fprintf(fid,'mpc.gencost = [\n');
for p=1:2:n
fprintf(fid,'%d\t',costmod);%Cost Model
fprintf(fid,'%d\t',startup);%Startup Cost
fprintf(fid,'%d\t',shutdown);%Shutdown Cost
fprintf(fid,'%d\t',ncoff);%No of Coefficients
fprintf(fid,'%d\t',quad);
fprintf(fid,'%d\t',lin);
fprintf(fid,'%d;\n',const);
end
fprintf(fid,'];\n');

fclose(fid)
