-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhFunctionJacob.m
256 lines (179 loc) · 7.21 KB
/
hFunctionJacob.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
function [ hx, ha,hu ] = hFunctionJacob( z,...
xq_vec, xprime_vec )
%HFUNCTIONJACOB calculates the jacobian of g(x,a,u);
% [ hx, ha,hu ] = hFunctionJacob( z,...
% xq_vec, xprime_vec ) calculates the jacobian of h.
%
% Description of Outputs:
% 1. hx: the jacobian of h with respect to x, size(2*N+2*G,3*G);
% 2. ha: the jacobian of h with respect to a, size(2*N+2*G, 2*N+2*G)
% 3. hu: the jacobian of h with respect to u, size(2*N+2*G, 2*G)
%
% Description of Inputs:
% 1. z: vector of z=(x,a,u) combining states, algebraic and control
% variables, size(2*N+7*G,1).
% 5. xq_vec: vector of quadrature axis synchronous reactance (pu) size(G,1)
% 6. xprime_vec: direct axis transient reactance pu, size(G,1).
%
% See also hFunctionJacobVectorized
global N G L gen_set load_set Gmat Bmat ...
deltaIdx omegaIdx eIdx ...
thetaIdx vIdx pgIdx qgIdx mIdx fIdx
h1Idx=1:G;
h2Idx=h1Idx(end)+1:h1Idx(end)+G;
h3Idx=h2Idx(end)+1:h2Idx(end)+G;
h4Idx=h3Idx(end)+1:h3Idx(end)+G;
h5Idx=h4Idx(end)+1:h4Idx(end)+L;
h6Idx=h5Idx(end)+1:h5Idx(end)+L;
V=z(vIdx);
theta=z(thetaIdx);
Vg=V(gen_set);
thetag=theta(gen_set);
VgIdx=vIdx(gen_set);
thetagIdx=thetaIdx(gen_set);
delta=z(deltaIdx);
e=z(eIdx);
hz=zeros(2*N+2*G, 2*N+7*G);
hznRows=2*N+2*G;
hznCols=2*N+7*G;
hz(sub2ind([hznRows hznCols], h1Idx, deltaIdx)) = (1./xprime_vec).*(e.*Vg.*cos(delta-thetag))...
+ (1./xq_vec).*(1./xprime_vec).*(xprime_vec-xq_vec).*Vg.*Vg.*cos(2*(delta-thetag));
hz(sub2ind([hznRows hznCols], h1Idx,eIdx)) = (1./xprime_vec).*Vg.*sin(delta-thetag);
hz(sub2ind([hznRows hznCols], h1Idx,pgIdx))= -1;
hz(sub2ind([hznRows hznCols], h1Idx, VgIdx)) = (1./xprime_vec).*e.*sin(delta-thetag) +...
(1./xq_vec).*(1./xprime_vec).*(xprime_vec-xq_vec).*Vg.*sin(2*(delta-thetag));
hz(sub2ind([hznRows hznCols], h1Idx, thetagIdx)) = -(1./xprime_vec).*e.*Vg.*cos(delta-thetag)...
-(1./xq_vec).*(1./xprime_vec).* (xprime_vec-xq_vec).*Vg.*Vg.*cos(2*(delta-thetag));
hz(sub2ind([hznRows hznCols], h2Idx, deltaIdx)) = - (1./xprime_vec).*(e.*Vg.*sin(delta-thetag))...
- (1./xq_vec).*(1./xprime_vec).*(xprime_vec-xq_vec).*Vg.*Vg.*sin(2*(delta-thetag));
hz(sub2ind([hznRows hznCols], h2Idx,eIdx)) = (1./xprime_vec).*Vg.*cos(delta-thetag);
hz(sub2ind([hznRows hznCols], h2Idx,qgIdx))= -1;
hz(sub2ind([hznRows hznCols], h2Idx, VgIdx)) = (1./xprime_vec).*e.*cos(delta-thetag) ...
- (1./xq_vec).*(1./xprime_vec).*(xprime_vec+xq_vec).*Vg+...
(1./xq_vec).*(1./xprime_vec).*(xprime_vec-xq_vec).*Vg.*cos(2*(delta-thetag));
hz(sub2ind([hznRows hznCols], h2Idx, thetagIdx)) = (1./xprime_vec).*e.*Vg.*sin(delta-thetag)...
+(1./xq_vec).*(1./xprime_vec).* (xprime_vec-xq_vec).*Vg.*Vg.*sin(2*(delta-thetag));
h3v=zeros(G,N); % only as big as V
for ii=1:G
mIndex=gen_set(ii);
for jj=1:N
if mIndex==jj
h3v(ii,jj)= Gmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta)+...
Bmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
V(mIndex)*Gmat(mIndex,mIndex);
else
h3v(ii, jj)= V(mIndex)*( Gmat(mIndex,jj)*cos(theta(mIndex)-theta(jj))+...
Bmat(mIndex,jj)*sin(theta(mIndex)-theta(jj)));
end
end
end
h3theta=zeros(G,N); % only as big as theta
for ii=1:G
mIndex=gen_set(ii);
for jj=1:N
if mIndex==jj
h3theta(ii,jj)=V(mIndex)* (-Gmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
Bmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta))-Bmat(mIndex,mIndex)*V(mIndex)^2;
else
h3theta(ii, jj)= V(mIndex)*( Gmat(mIndex,jj)*V(jj)*sin(theta(mIndex)-theta(jj))-...
Bmat(mIndex,jj)*V(jj)*cos(theta(mIndex)-theta(jj)));
end
end
end
hz(h3Idx,vIdx)=h3v;
hz(h3Idx,thetaIdx)=h3theta;
hz(sub2ind([hznRows hznCols], h3Idx,pgIdx))=-1;
h4v=zeros(G,N); % only as big as V
for ii=1:G
mIndex=gen_set(ii);
for jj=1:N
if mIndex==jj
h4v(ii,jj)= -Bmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta)+...
Gmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)...
-V(mIndex)*Bmat(mIndex,mIndex);
else
h4v(ii, jj)= V(mIndex)*( - Bmat(mIndex,jj)*cos(theta(mIndex)-theta(jj))+...
Gmat(mIndex,jj)*sin(theta(mIndex)-theta(jj)));
end
end
end
h4theta=zeros(G,N); % only as big as theta
for ii=1:G
mIndex=gen_set(ii);
for jj=1:N
if mIndex==jj
h4theta(ii,jj)=V(mIndex)* (Bmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
Gmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta))-Gmat(mIndex,mIndex)*V(mIndex)^2;
else
h4theta(ii, jj)= V(mIndex)*( - Bmat(mIndex,jj)*V(jj)*sin(theta(mIndex)-theta(jj))-...
Gmat(mIndex,jj)*V(jj)*cos(theta(mIndex)-theta(jj)));
end
end
end
hz(h4Idx,vIdx)=h4v;
hz(h4Idx,thetaIdx)=h4theta;
hz(sub2ind([hznRows hznCols], h4Idx,qgIdx))=-1;
%% h5
h5v=zeros(L,N); % only as big as V
for ii=1:L
mIndex=load_set(ii);
for jj=1:N
if mIndex==jj
h5v(ii,jj)= Gmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta)+...
Bmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
V(mIndex)*Gmat(mIndex,mIndex);
else
h5v(ii, jj)= V(mIndex)*( Gmat(mIndex,jj)*cos(theta(mIndex)-theta(jj))+...
Bmat(mIndex,jj)*sin(theta(mIndex)-theta(jj)));
end
end
end
h5theta=zeros(L,N); % only as big as theta
for ii=1:L
mIndex=load_set(ii);
for jj=1:N
if mIndex==jj
h5theta(ii,jj)=V(mIndex)* (-Gmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
Bmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta))-Bmat(mIndex,mIndex)*V(mIndex)^2;
else
h5theta(ii, jj)= V(mIndex)*( Gmat(mIndex,jj)*V(jj)*sin(theta(mIndex)-theta(jj))-...
Bmat(mIndex,jj)*V(jj)*cos(theta(mIndex)-theta(jj)));
end
end
end
hz(h5Idx,vIdx)=h5v;
hz(h5Idx,thetaIdx)=h5theta;
%%
h6v=zeros(L,N); % only as big as V
for ii=1:L
mIndex=load_set(ii);
for jj=1:N
if mIndex==jj
h6v(ii,jj)= -Bmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta)+...
Gmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)...
-V(mIndex)*Bmat(mIndex,mIndex);
else
h6v(ii, jj)= V(mIndex)*( - Bmat(mIndex,jj)*cos(theta(mIndex)-theta(jj))+...
Gmat(mIndex,jj)*sin(theta(mIndex)-theta(jj)));
end
end
end
h6theta=zeros(L,N); % only as big as theta
for ii=1:L
mIndex=load_set(ii);
for jj=1:N
if mIndex==jj
h6theta(ii,jj)=V(mIndex)* (Bmat(mIndex,:)*diag(V)*sin(theta(mIndex)-theta)+...
Gmat(mIndex,:)*diag(V)*cos(theta(mIndex)-theta))-Gmat(mIndex,mIndex)*V(mIndex)^2;
else
h6theta(ii, jj)= V(mIndex)*( -Bmat(mIndex,jj)*V(jj)*sin(theta(mIndex)-theta(jj))-...
Gmat(mIndex,jj)*V(jj)*cos(theta(mIndex)-theta(jj)));
end
end
end
hz(h6Idx,vIdx)=h6v;
hz(h6Idx,thetaIdx)=h6theta;
hx=hz(:, [deltaIdx,omegaIdx,eIdx]);
ha=hz(:,[vIdx,thetaIdx,pgIdx,qgIdx]);
hu=hz(:,[mIdx,fIdx]);
end