-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathALQROPF.m
305 lines (221 loc) · 7.52 KB
/
ALQROPF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
function [vgS,pgS, thetaSSlack,SsObjEst,ModelingTime, Gamma,K...
RiccatiSVec,KVec, SsCostVec,GammaVec,TrCostEstimateVec,ObjValue,...
ItSuccess] = ...
ALQROPF( delta0, omega0, e0, m0,...
v0,theta0, pg0, qg0, ...
pref0, f0,...
NetworkS,...
pdS,qdS,pd0,qd0, Alpha,Tlqr)
%AUGMENTEDOPF implements augmented opf per equation (18) CDC 2016.
% [vs,thetas, pgs,qgs, K ] = augmentedOPF( z0,...
% deltaploadg,deltaqloadg,deltaploadl,deltaqloadl) implements the
% augmetned OPF based on linear approximation of a known equilibrium z0
% for the power systems described by nonlinear equations g(x,a,u) and
% h(x,a,u).
%
% Description of Outputs:
% 1. vs: the calculated optimal steady-state voltage magnitude, size(N,1).
% 2. thetas: the calculated optimal steady-state voltage angle in radians,
% size(N,1).
% 3. pgs: the calculated optimal steady-state real power injection
% (setpoints) in pu Watts, size(G,1).
% 4. qgs: the calculated optimal steady-state reactive power injection
% in pu Vars, size(G,1).
% 5. K: the calculated optimal linear feedback gain, size(2*G,3*G)
% 6. ssCost: is the calculated steady-state cost of real power generation
% 7. trCostEstimate: is the gama---an estimate of the transient cost
%
% Description of Inputs:
% 1. z0: the equilibrium point used for linearization
% 2. deltaploadg: the difference of the new desired real power to the initial load level
% for generator nodes, size(G,1).
% 3. deltaqloadg: the difference of the new desired reactive power to the initial load level
% for generator nodes, size(G,1).
% 4. deltaploadl: the difference of the new desired real power to the initial load level
% for load nodes, size(L,1).
% 5. deltaqloadl: the difference of the new desired reactive power to the initial load level
% for generator nodes, size(L,1)
% 6. alpha: alpha
% 7. Tlqr: is the Tlqr factor for transient control
% See also approxOPF, LQRstep
%
% Required:
%
% system constants [these do not change]
global OMEGAS Sbase N G L NodeSet GenSet LoadSet YMat GMat BMat Cg...
YffVec YftVec YtfVec YttVec
% indices [these do not change]
global deltaIdx omegaIdx eIdx mIdx ...
thetaIdx vIdx pgIdx qgIdx fIdx prefIdx SlackIdx GenNonSlackSet
% machine [these do not change]
global TauVec XdVec XqVec XprimeVec DVec MVec TchVec FreqRVec...
%% Obtaining the jacobians:
[ gx,ga,gu ] = gFunctionJacobVectorized(delta0, omega0, e0, m0, v0, theta0, pg0, qg0);
[ hx, ha] = hFunctionJacobVectorized( delta0, omega0, e0, m0, v0, theta0, pg0, qg0);
haInv=mldivide(ha,speye(size(ha)));
Asys=gx-ga*haInv*hx;
Bsys=gu;
%%
x0=[delta0;omega0;e0;m0];
a0=[v0;theta0;pg0;qg0];
u0=[pref0;f0];
%% disturbance
h1Idx=1:G;
h2Idx=G+1:2*G;
h3Idx=2*G+1:3*G;
h4Idx=3*G+1:4*G;
h5Idx=4*G+1:4*G+L;
h6Idx=4*G+L+1:4*G+2*L;
deltaD=zeros(h6Idx(end),1);
dpgS=pdS(GenSet)-pd0(GenSet);
dqdgS=qdS(GenSet)-qd0(GenSet);
dpdlS=pdS(LoadSet)-pd0(LoadSet);
dqdlS=qdS(LoadSet)-qd0(LoadSet);
deltaD(h3Idx)=-dpgS;
deltaD(h4Idx)=-dqdgS;
deltaD(h5Idx)=-dpdlS;
deltaD(h6Idx)=-dqdlS;
%% Alternate project-minimize algorithm
% setting up maximum number of iterations and initial objectives
MaxIt=2;
ObjValue=-inf+zeros(MaxIt,1);
ZsVec=sparse(length([x0;a0;u0]),MaxIt);
RiccatiSVec=sparse(16*(G^2),MaxIt);
SsCostVec=sparse(1,MaxIt);
TrCostEstimateVec=sparse(1,MaxIt);
GammaVec=sparse(1,MaxIt);
KVec=sparse(2*G*4*G,MaxIt);
tic
ModelingTime=0;
% solve CARE at z0
[Qinv,Rinv]=QinvRinv(pg0,qg0,Alpha, speye(4*G), speye(2*G),NetworkS);
[RiccatiS,EigValues,FeedBackGain,Report]=care(Asys,Bsys,mldivide(Qinv,speye(size(Qinv))),mldivide(Rinv,speye(size(Rinv))));
%solve OPF with P0
cvx_tic
cvx_begin quiet
cvx_solver SDPT3
variables xs(4*G,1) as(2*N+2*G,1) us(2*G,1)
deltas=xs(deltaIdx);
omegas=xs(omegaIdx);
es=xs(eIdx);
ms=xs(mIdx);
vs=as(vIdx);
thetas=as(thetaIdx);
pgs=as(pgIdx);
qgs=as(qgIdx);
prefs=us(prefIdx);
fs=us(fIdx);
SsCost=steadyStateCost(pgs,NetworkS);
minimize( SsCost+ (Tlqr/2)*quad_form((xs-x0),RiccatiS))
subject to:
omegas==omega0;
zeros(4*G,1) == gx*(xs-x0)+ga*(as-a0)+gu*(us-u0);
deltaD==hx*(xs-x0)+ha*(as-a0);
NetworkS.bus(:,13)<= vs<=NetworkS.bus(:,12);
NetworkS.gen(:,5)./Sbase<=qgs<=NetworkS.gen(:,4)./Sbase;
NetworkS.gen(:,10)./Sbase <= pgs <= NetworkS.gen(:,9)./Sbase;
% finds the slack bus
thetas(SlackIdx)==theta0(SlackIdx);
% vs(SlackIdx)==v0(SlackIdx);
cvx_end
TimeElapsed=cvx_toc;
ModelingTime=ModelingTime+TimeElapsed(3);
% solve CARE at zS
[Qinv,Rinv]=QinvRinv(pgs,qgs,Alpha, speye(4*G), speye(2*G),NetworkS);
[RiccatiS,EigValues,FeedBackGain,Report]=care(Asys,Bsys,mldivide(Qinv,speye(size(Qinv))),mldivide(Rinv,speye(size(Rinv))));
ZsVec(:,1)=[xs;as;us];
RiccatiSVec(:,1)=RiccatiS(:);
KVec(:,1)=-FeedBackGain(:);
SsCostVec(:,1)=steadyStateCost(pgs,NetworkS);
GammaVec(:,1)=quad_form((xs-x0),RiccatiS);
TrCostEstimateVec(:,1)=(Tlqr/2)*GammaVec(:,1);
ObjValue(1)=SsCostVec(:,1)+TrCostEstimateVec(:,1);
ItSuccess=1;
BestIt=1;
K=-FeedBackGain;
SsCost=steadyStateCost(pgs,NetworkS);
Gamma=quad_form((xs-x0),RiccatiS);
TrCostEstimate=(Tlqr/2)*Gamma;
SsObjEst=SsCost+TrCostEstimate;
vgS=vs(GenSet);
% pgSNonSlack=pgs(GenNonSlackSet);
pgS=pgs;
thetaSSlack=thetas(SlackIdx);
str=['ObjValue of Alternate minimization at Iter No. ', num2str(1), ' is ', num2str(ObjValue(1)),'\n'];
fprintf(str);
if Alpha ~=0
for ItNo=2:MaxIt
cvx_tic
cvx_begin quiet
cvx_solver SDPT3
variables xs(4*G,1) as(2*N+2*G,1) us(2*G,1)
deltas=xs(deltaIdx);
omegas=xs(omegaIdx);
es=xs(eIdx);
ms=xs(mIdx);
vs=as(vIdx);
thetas=as(thetaIdx);
pgs=as(pgIdx);
qgs=as(qgIdx);
prefs=us(prefIdx);
fs=us(fIdx);
SsCost=steadyStateCost(pgs,NetworkS);
minimize( SsCost+ (Tlqr/2)*quad_form((xs-x0),RiccatiS))
subject to:
omegas==omega0;
% -pi<=thetas<=pi;
zeros(4*G,1) == gx*(xs-x0)+ga*(as-a0)+gu*(us-u0);
deltaD==hx*(xs-x0)+ha*(as-a0);
NetworkS.bus(:,13)<= vs<=NetworkS.bus(:,12);
NetworkS.gen(:,5)./Sbase<=qgs<=NetworkS.gen(:,4)./Sbase;
NetworkS.gen(:,10)./Sbase <= pgs <= NetworkS.gen(:,9)./Sbase;
% finds the slack bus
thetas(SlackIdx)==theta0(SlackIdx);
% vs(SlackIdx)==v0(SlackIdx);
cvx_end
TimeElapsed=cvx_toc;
ModelingTime=ModelingTime+TimeElapsed(3);
% solve CARE at previous zs
[Qinv,Rinv]=QinvRinv(pgs,qgs,Alpha, speye(4*G), speye(2*G),NetworkS);
[RiccatiS,EigValues,FeedBackGain,Report]=care(Asys,Bsys,mldivide(Qinv,speye(size(Qinv))),mldivide(Rinv,speye(size(Rinv))));
%solve OPF with P0
% optional outputs of augmentedOPFAlternateMinimization
ZsVec(:,ItNo)=[xs;as;us];
RiccatiSVec(:,ItNo)=RiccatiS(:);
KVec(:,ItNo)=-FeedBackGain(:);
SsCostVec(:,ItNo)=steadyStateCost(pgs,NetworkS);
GammaVec(:,ItNo)=quad_form((xs-x0),RiccatiS);
TrCostEstimateVec(:,ItNo)=(Tlqr/2)*GammaVec(:,ItNo);
ObjValue(ItNo)=SsCostVec(:,ItNo)+TrCostEstimateVec(:,ItNo);
str=['ObjValue of Alternate minimization at Iter No. ', num2str(ItNo), ' is ', num2str(ObjValue(ItNo)),'\n'];
fprintf(str);
% display progress
if ObjValue(ItNo)<ObjValue(BestIt)
BestIt=ItNo;
K=-FeedBackGain;
SsCost=steadyStateCost(pgs,NetworkS);
Gamma=quad_form((xs-x0),RiccatiS);
TrCostEstimate=(Tlqr/2)*Gamma;
SsObjEst=SsCost+TrCostEstimate;
vgS=vs(GenSet);
% pgSNonSlack=pgs(GenNonSlackSet);
pgS=pgs;
thetaSSlack=thetas(SlackIdx);
end
end
end
% figureAlt=figure('Units','inches',...
% 'Position',[0 1 8 4],...
% 'PaperPositionMode','auto');
% set(figureAlt, 'Name', 'Alt. Min. Progress');
% plot(1:1:MaxIt, ObjValue(1:1:ItSuccess),'o--');
% xlabel('It.', 'FontWeight','bold');
% ylabel('Obj. Value');
%
% set(gca, 'XTick',[1:1:MaxIt]);
% set(gca,'box','on');
% set(gca,'fontSize',22);
% set(gca,'defaulttextinterpreter','latex')
% grid on;
% title('Alt. Min. Progress');
end