mpc=load ('grid_data_24bus.mat');
profile_gen_rand=[1.0057;1.027;0.98;0.967;0.9837;0.971;1.001;1.04063;1.0129;0.961;0.989;0.9554;1.000129;0.9937;1.0497;1.0316;0.9986;1.03944;0.963;0.989;1.0427;1.041749;1.0214;1.0118];
n_bus = size(mpc.BusData,1);
n_brnc = size(mpc.branches,1);
mpc.BusData = repmat (mpc.BusData, [1,1,n_time]);
mpc.Gen = repmat (mpc.Gen, [1,1,n_time]);
mpc.BusData(:,2,t)=profile_L(t).*mpc.BusData(:,2,t);
mpc.Gen(g,3:4,t)=profile_gen_rand(t).*( mpc.Gen(g,3:4,t));
SOC_0 = 0.2*SOC_max/mpc.Sbase;
wind_profile = mpc.WD(:, 2);
wind_gen = Wcap * wind_profile';
TT{tt}=['t',num2str(tt)];
bus = linspace(1,n_bus,n_bus)';
GG{gg}=['g',num2str(gg)];
for ii=1:size(branches,1)
branches=vertcat(branches, zeros(4,4));
branches((ii-1)*4+1:ii*4,:)=...
[ branches(ii,1) branches(ii,2) 1 branches(ii,3); ...
branches(ii,1) branches(ii,2) 2 branches(ii,4); ...
branches(ii,1) branches(ii,2) 3 branches(ii,5); ...
branches(ii,1) branches(ii,2) 4 branches(ii,6) ]; ...
Branches.name='branches';
Branches.type='parameter';
Branches.val=mpc.Branches;
Branches.uels={Bus.uels, Bus.uels, {'r','x','b','limit'}};
gb.uels={gen.uels,{1:24}};
GenData=[mpc.Gen(:,5)'; mpc.Gen(:,4)'; mpc.Gen(:,3)'; mpc.Gen(:,8)'; mpc.Gen(:,9)']';
Gen_Data.type='parameter';
Gen_Data.uels={GG,{'b','pmin','pmax','RU','RD'}};
Bus_Data.type='parameter';
Bus_Data.val=[mpc.BusData(:,1) ones(size(mpc.BusData,1),1) mpc.BusData(:,2) mpc.BusData(:,1) 2*ones(size(mpc.BusData,1),1) mpc.BusData(:,3)];
Bus_Data.val=reshape(Bus_Data.val', [size(Bus_Data.val',1)/2,size(Bus_Data.val',2)*2]);
Bus_Data.val=Bus_Data.val';
Bus_Data.uels={Bus.uels,{'Pd','Qd'}};
wgdx(wgdxName,y,gen,Bus,Slack,S_base,VoLL,Vwc,Eta_c,Eta_d,socmax,socini,wd,gb,Gen_Data,WCap,Bus_Data,Branches);
gams([ 'dcopf_bess' ' lo=2' ]);
solGDX=[GAMSfolder 'dcopf_bess_Solution.gdx'];
rs = struct('name','returnStat','compress','true');
disp(['Best possible: ',num2str(stats.val(1,2))]);
disp(['Objective: ',num2str(stats.val(2,2))]);
disp(['Optimality gap: ',num2str((stats.val(2,2)-stats.val(1,2))/(stats.val(1,2))*100),' %']);
disp(['Time: ',num2str(stats.val(3,2)), ' s']);
disp(['Model status: ',num2str(stats.val(4,2)), '(1: optimal, 8: integer solution model found)']);
P_c = struct('name','Pc','form','sparse','compress','true');
P_c = zeros(n_bus,n_time);
for ijk=1:length(sP_c.val(:,3))
P_c(sP_c.val(ijk,1),sP_c.val(ijk,2)) = sP_c.val(ijk,3);
P_d = struct('name','Pd','form','sparse','compress','true');
P_d = zeros(n_bus,n_time);
for ijk=1:length(sP_d.val(:,3))
P_d(sP_d.val(ijk,1),sP_d.val(ijk,2)) = sP_d.val(ijk,3);
SoC_batt=struct('name','SOC','form','full','compress','true');
sSoC_B=rgdx(solGDX,SoC_batt);
SoC = zeros(n_bus,n_time);
NESS=struct('name','NESS','form','full','compress','true');
idx = str2double(sNESS.uels{1});
P_gen=struct('name','Pg','form','full','compress','true');
sP_gen=rgdx(solGDX,P_gen);
P_gen = zeros(n_gen,n_time);
P_cut=struct('name','pwc','form','full','compress','true');
sP_Cut=rgdx(solGDX,P_cut);
P_cur = zeros(n_bus,n_time);
P_sh=struct('name','lsh','form','full','compress','true');
Psh = zeros(n_bus,n_time);
ylabel('Wind curtailment and load shedding')
legend('Curtailment','Load shedding')
P_ij = struct('name','Pij','form','sparse','compress','true');
P_ij = zeros(n_bus, n_bus, n_time);
for ijk=1:length(sP_ij.val(:,3))
P_ij(sP_ij.val(ijk,1), sP_ij.val(ijk,2), sP_ij.val(ijk,3)) = sP_ij.val(ijk,4);
stairs(1:n_time, squeeze(P_ij(i,j,:)));
ylabel(['Power flow from bus ', num2str(i), ' to bus ', num2str(j)]);
title('Power Flow between Buses over Time');
$set matout1 "'dcopf_bess_Solution.gdx',returnStat,t,OF,Pd,Pc,SOC,Pij,Pg,lsh,pwc"
GB(Gen,bus) generators' bus location
eta_d discharge efficiency
VWC Value of Wind curtailment
VOLL Value of Loss of Load;
SOCmax battery size [MWh]
SOC0 initial SoC [per unit]
branches(bus,node,*) r(pu) x(pu) b(pu) limit(MVA)
WD(t,*) Wind-demand daily variation patterns: 'w' wind 'd' demand
Wcap(bus) Wind capacities
BusData(bus,*) Demands of each bus in MW;
$gdxin "C:\Users\user\Desktop\matpower7.1\data\dcopf_bess.gdx"
$load t Gen sbase bus slack VOLL VWC eta_c eta_d SOCmax SOC0 WD GD GB WCap BusData branches
branches(bus,node,'x')$(branches(bus,node,'x')=0)=branches(node,bus,'x');
branches(bus,node,'limit')$(branches(bus,node,'limit')=0)=branches(node,bus,'limit');
branches(bus,node,'bij')$(conex(bus,node))=1/branches(node,bus,'x');
Parameter conex(bus,node) Bus connectivity matrix;
conex(bus,node)$(conex(node,bus))=1;
conex(bus,node)$(branches(bus,node,'limit') and branches(node,bus,'limit')) =1;
Variables OF, Pij(bus,node,t),Pg(Gen,t),delta(bus,t), pw(bus,t),lsh(bus,t),SOC(bus,t),pwc(bus,t),Pd(bus,t),Pc(bus,t);
Integer variable NESS(bus);
Equations objfnc, balance,const1,const2,const3,const4,constESS,constESS2,constESS3(bus,t),constESS4(bus,t),constESS5(bus,t),constESS6(bus,t);
balance(bus,t)..sum(node$(conex(node,bus)),Pij(bus,node,t))=e=sum(Gen$(GB(Gen,bus)),Pg(Gen,t))+lsh(bus,t)$busdata(bus,'Pd')+pw(bus,t)$Wcap(bus)-Pc(bus,t)$SOCmax(bus)+Pd(bus,t)$SOCmax(bus)-WD(t,'d')*busdata(bus,'Pd')/sbase;
const1(bus,node,t)$conex(bus,node)..Pij(bus,node,t)=e=branches(bus,node,'bij')*(delta(bus,t)-delta(node,t));
const2(Gen,t)..Pg(Gen,t+1)-Pg(Gen,t)=l=Gendata(Gen,'RU')/sbase;
const3(Gen,t)..Pg(Gen,t-1)-Pg(Gen,t)=l=Gendata(Gen,'RD')/sbase;
const4(bus,t)$Wcap(bus)..pwc(bus,t)=e=WD(t,'w')*Wcap(bus)/sbase-pw(bus,t);
constESS(bus,t)$SOCmax(bus)..SOC(bus,t)=e=(0.2*NESS(bus)*SOCmax/sbase)$(ord(t)=1)+SOC(bus,t-1)$(ord (t)>1)+(Pc(bus,t)$SOCmax(bus)*eta_c)-(Pd(bus,t)$SOCmax(bus)/eta_d);
constESS2(bus,t) .. SOC(bus,t)=l=NESS(bus)*SOCmax/sbase ;
constESS3(bus,t) .. Pc(bus,t)=l=0.2*NESS(bus)*SOCmax/sbase ;
constESS4(bus,t) .. Pd(bus,t)=l=0.2*NESS(bus)*SOCmax/sbase ;
constESS5 .. sum(bus,NESS(bus))=l=Nmax;
*final SoC imposition (SoC0=0.2*SOCmax)
constESS6(bus) .. SOC(bus,'t24')=e=0.2*NESS(bus)*SOCmax/sbase ;
objfnc..OF=e=sum((bus,Gen,t)$GB(Gen,bus),Pg(Gen,t)*Gendata(Gen,'b')*sbase)+sum((bus,t),VOLL*lsh(bus,t)*sbase$(busdata(bus,'Pd'))+VOLW*pwc(bus,t)*Sbase$(Wcap(bus)));
Pij.lo(bus,node,t)$(conex(bus,node))=-1*branches(bus,node,'limit')/sbase;
Pij.up(bus,node,t)$(conex(bus,node))=1*branches(bus,node,'limit')/sbase;
Pg.lo(Gen,t)=GD(Gen,'Pmin')/sbase;
Pg.up(Gen,t)=GD(Gen,'Pmax')/sbase;
pw.up(bus,t)=WD(t,'w')*Wcap(bus)/Sbase;
pwc.up(bus,t)=WD(t,'w')*Wcap(bus)/Sbase;
lsh.up(bus,t)=WD(t,'d')*busdata(bus,'Pd')/Sbase;
Solve load_flow using MIP minimizing OF;
set stat /bestpossible,objective,tgams,tsolver,modelstat,solvestat/;
parameter returnStat(stat);
returnStat('bestpossible') = loadflow.objest;
returnStat('objective') = loadflow.objval;
returnStat('tsolver') = loadflow.resusd;
returnStat('modelstat') = loadflow.modelstat;
returnStat('solvestat') = loadflow.solvestat;
display Pg.l,Pij.l,delta.l, pwc.l,lsh.l,Pc.l,Pd.l;