Actual source code: ex9busopt.c

  1: static char help[] = "Application of adjoint sensitivity analysis for power grid stability analysis of WECC 9 bus system.\n\
  2: This example is based on the 9-bus (node) example given in the book Power\n\
  3: Systems Dynamics and Stability (Chapter 7) by P. Sauer and M. A. Pai.\n\
  4: The power grid in this example consists of 9 buses (nodes), 3 generators,\n\
  5: 3 loads, and 9 transmission lines. The network equations are written\n\
  6: in current balance form using rectangular coordinates.\n\n";

  8: /*
  9:   This code demonstrates how to solve a DAE-constrained optimization problem with TAO, TSAdjoint and TS.
 10:   The objectivie is to find optimal parameter PG for each generator to minizie the frequency violations due to faults.
 11:   The problem features discontinuities and a cost function in integral form.
 12:   The gradient is computed with the discrete adjoint of an implicit theta method, see ex9busadj.c for details.
 13: */

 15: #include <petsctao.h>
 16: #include <petscts.h>
 17: #include <petscdm.h>
 18: #include <petscdmda.h>
 19: #include <petscdmcomposite.h>
 20: #include <petsctime.h>

 22: PetscErrorCode FormFunctionGradient(Tao,Vec,PetscReal*,Vec,void*);

 24: #define freq 60
 25: #define w_s (2*PETSC_PI*freq)

 27: /* Sizes and indices */
 28: const PetscInt nbus    = 9; /* Number of network buses */
 29: const PetscInt ngen    = 3; /* Number of generators */
 30: const PetscInt nload   = 3; /* Number of loads */
 31: const PetscInt gbus[3] = {0,1,2}; /* Buses at which generators are incident */
 32: const PetscInt lbus[3] = {4,5,7}; /* Buses at which loads are incident */

 34: /* Generator real and reactive powers (found via loadflow) */
 35: PetscScalar PG[3] = { 0.69,1.59,0.69};
 36: /* PetscScalar PG[3] = {0.716786142395021,1.630000000000000,0.850000000000000};*/

 38: const PetscScalar QG[3] = {0.270702180178785,0.066120127797275,-0.108402221791588};
 39: /* Generator constants */
 40: const PetscScalar H[3]    = {23.64,6.4,3.01};   /* Inertia constant */
 41: const PetscScalar Rs[3]   = {0.0,0.0,0.0}; /* Stator Resistance */
 42: const PetscScalar Xd[3]   = {0.146,0.8958,1.3125};  /* d-axis reactance */
 43: const PetscScalar Xdp[3]  = {0.0608,0.1198,0.1813}; /* d-axis transient reactance */
 44: const PetscScalar Xq[3]   = {0.4360,0.8645,1.2578}; /* q-axis reactance Xq(1) set to 0.4360, value given in text 0.0969 */
 45: const PetscScalar Xqp[3]  = {0.0969,0.1969,0.25}; /* q-axis transient reactance */
 46: const PetscScalar Td0p[3] = {8.96,6.0,5.89}; /* d-axis open circuit time constant */
 47: const PetscScalar Tq0p[3] = {0.31,0.535,0.6}; /* q-axis open circuit time constant */
 48: PetscScalar M[3]; /* M = 2*H/w_s */
 49: PetscScalar D[3]; /* D = 0.1*M */

 51: PetscScalar TM[3]; /* Mechanical Torque */
 52: /* Exciter system constants */
 53: const PetscScalar KA[3] = {20.0,20.0,20.0};  /* Voltage regulartor gain constant */
 54: const PetscScalar TA[3] = {0.2,0.2,0.2};     /* Voltage regulator time constant */
 55: const PetscScalar KE[3] = {1.0,1.0,1.0};     /* Exciter gain constant */
 56: const PetscScalar TE[3] = {0.314,0.314,0.314}; /* Exciter time constant */
 57: const PetscScalar KF[3] = {0.063,0.063,0.063};  /* Feedback stabilizer gain constant */
 58: const PetscScalar TF[3] = {0.35,0.35,0.35};    /* Feedback stabilizer time constant */
 59: const PetscScalar k1[3] = {0.0039,0.0039,0.0039};
 60: const PetscScalar k2[3] = {1.555,1.555,1.555};  /* k1 and k2 for calculating the saturation function SE = k1*exp(k2*Efd) */

 62: PetscScalar Vref[3];
 63: /* Load constants
 64:   We use a composite load model that describes the load and reactive powers at each time instant as follows
 65:   P(t) = \sum\limits_{i=0}^ld_nsegsp \ld_alphap_i*P_D0(\frac{V_m(t)}{V_m0})^\ld_betap_i
 66:   Q(t) = \sum\limits_{i=0}^ld_nsegsq \ld_alphaq_i*Q_D0(\frac{V_m(t)}{V_m0})^\ld_betaq_i
 67:   where
 68:     ld_nsegsp,ld_nsegsq - Number of individual load models for real and reactive power loads
 69:     ld_alphap,ld_alphap - Percentage contribution (weights) or loads
 70:     P_D0                - Real power load
 71:     Q_D0                - Reactive power load
 72:     V_m(t)              - Voltage magnitude at time t
 73:     V_m0                - Voltage magnitude at t = 0
 74:     ld_betap, ld_betaq  - exponents describing the load model for real and reactive part

 76:     Note: All loads have the same characteristic currently.
 77: */
 78: const PetscScalar PD0[3] = {1.25,0.9,1.0};
 79: const PetscScalar QD0[3] = {0.5,0.3,0.35};
 80: const PetscInt    ld_nsegsp[3] = {3,3,3};
 81: const PetscScalar ld_alphap[3] = {1.0,0.0,0.0};
 82: const PetscScalar ld_betap[3]  = {2.0,1.0,0.0};
 83: const PetscInt    ld_nsegsq[3] = {3,3,3};
 84: const PetscScalar ld_alphaq[3] = {1.0,0.0,0.0};
 85: const PetscScalar ld_betaq[3]  = {2.0,1.0,0.0};

 87: typedef struct {
 88:   DM          dmgen, dmnet; /* DMs to manage generator and network subsystem */
 89:   DM          dmpgrid; /* Composite DM to manage the entire power grid */
 90:   Mat         Ybus; /* Network admittance matrix */
 91:   Vec         V0;  /* Initial voltage vector (Power flow solution) */
 92:   PetscReal   tfaulton,tfaultoff; /* Fault on and off times */
 93:   PetscInt    faultbus; /* Fault bus */
 94:   PetscScalar Rfault;
 95:   PetscReal   t0,tmax;
 96:   PetscInt    neqs_gen,neqs_net,neqs_pgrid;
 97:   Mat         Sol; /* Matrix to save solution at each time step */
 98:   PetscInt    stepnum;
 99:   PetscBool   alg_flg;
100:   PetscReal   t;
101:   IS          is_diff; /* indices for differential equations */
102:   IS          is_alg; /* indices for algebraic equations */
103:   PetscReal   freq_u,freq_l; /* upper and lower frequency limit */
104:   PetscInt    pow; /* power coefficient used in the cost function */
105:   PetscBool   jacp_flg;
106:   Mat         J,Jacp;
107:   Mat         DRDU,DRDP;
108: } Userctx;

110: /* Converts from machine frame (dq) to network (phase a real,imag) reference frame */
111: PetscErrorCode dq2ri(PetscScalar Fd,PetscScalar Fq,PetscScalar delta,PetscScalar *Fr, PetscScalar *Fi)
112: {
114:   *Fr =  Fd*PetscSinScalar(delta) + Fq*PetscCosScalar(delta);
115:   *Fi = -Fd*PetscCosScalar(delta) + Fq*PetscSinScalar(delta);
116:   return(0);
117: }

119: /* Converts from network frame ([phase a real,imag) to machine (dq) reference frame */
120: PetscErrorCode ri2dq(PetscScalar Fr,PetscScalar Fi,PetscScalar delta,PetscScalar *Fd, PetscScalar *Fq)
121: {
123:   *Fd =  Fr*PetscSinScalar(delta) - Fi*PetscCosScalar(delta);
124:   *Fq =  Fr*PetscCosScalar(delta) + Fi*PetscSinScalar(delta);
125:   return(0);
126: }

128: /* Saves the solution at each time to a matrix */
129: PetscErrorCode SaveSolution(TS ts)
130: {
131:   PetscErrorCode    ierr;
132:   Userctx           *user;
133:   Vec               X;
134:   PetscScalar       *mat;
135:   const PetscScalar *x;
136:   PetscInt          idx;
137:   PetscReal         t;

140:   TSGetApplicationContext(ts,&user);
141:   TSGetTime(ts,&t);
142:   TSGetSolution(ts,&X);
143:   idx      = user->stepnum*(user->neqs_pgrid+1);
144:   MatDenseGetArray(user->Sol,&mat);
145:   VecGetArrayRead(X,&x);
146:   mat[idx] = t;
147:   PetscArraycpy(mat+idx+1,x,user->neqs_pgrid);
148:   MatDenseRestoreArray(user->Sol,&mat);
149:   VecRestoreArrayRead(X,&x);
150:   user->stepnum++;
151:   return(0);
152: }

154: PetscErrorCode SetInitialGuess(Vec X,Userctx *user)
155: {
157:   Vec            Xgen,Xnet;
158:   PetscScalar    *xgen,*xnet;
159:   PetscInt       i,idx=0;
160:   PetscScalar    Vr,Vi,IGr,IGi,Vm,Vm2;
161:   PetscScalar    Eqp,Edp,delta;
162:   PetscScalar    Efd,RF,VR; /* Exciter variables */
163:   PetscScalar    Id,Iq;  /* Generator dq axis currents */
164:   PetscScalar    theta,Vd,Vq,SE;

167:   M[0] = 2*H[0]/w_s; M[1] = 2*H[1]/w_s; M[2] = 2*H[2]/w_s;
168:   D[0] = 0.1*M[0]; D[1] = 0.1*M[1]; D[2] = 0.1*M[2];

170:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);

172:   /* Network subsystem initialization */
173:   VecCopy(user->V0,Xnet);

175:   /* Generator subsystem initialization */
176:   VecGetArray(Xgen,&xgen);
177:   VecGetArray(Xnet,&xnet);

179:   for (i=0; i < ngen; i++) {
180:     Vr  = xnet[2*gbus[i]]; /* Real part of generator terminal voltage */
181:     Vi  = xnet[2*gbus[i]+1]; /* Imaginary part of the generator terminal voltage */
182:     Vm  = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm;
183:     IGr = (Vr*PG[i] + Vi*QG[i])/Vm2;
184:     IGi = (Vi*PG[i] - Vr*QG[i])/Vm2;

186:     delta = PetscAtan2Real(Vi+Xq[i]*IGr,Vr-Xq[i]*IGi); /* Machine angle */

188:     theta = PETSC_PI/2.0 - delta;

190:     Id = IGr*PetscCosScalar(theta) - IGi*PetscSinScalar(theta); /* d-axis stator current */
191:     Iq = IGr*PetscSinScalar(theta) + IGi*PetscCosScalar(theta); /* q-axis stator current */

193:     Vd = Vr*PetscCosScalar(theta) - Vi*PetscSinScalar(theta);
194:     Vq = Vr*PetscSinScalar(theta) + Vi*PetscCosScalar(theta);

196:     Edp = Vd + Rs[i]*Id - Xqp[i]*Iq; /* d-axis transient EMF */
197:     Eqp = Vq + Rs[i]*Iq + Xdp[i]*Id; /* q-axis transient EMF */

199:     TM[i] = PG[i];

201:     /* The generator variables are ordered as [Eqp,Edp,delta,w,Id,Iq] */
202:     xgen[idx]   = Eqp;
203:     xgen[idx+1] = Edp;
204:     xgen[idx+2] = delta;
205:     xgen[idx+3] = w_s;

207:     idx = idx + 4;

209:     xgen[idx]   = Id;
210:     xgen[idx+1] = Iq;

212:     idx = idx + 2;

214:     /* Exciter */
215:     Efd = Eqp + (Xd[i] - Xdp[i])*Id;
216:     SE  = k1[i]*PetscExpScalar(k2[i]*Efd);
217:     VR  =  KE[i]*Efd + SE;
218:     RF  =  KF[i]*Efd/TF[i];

220:     xgen[idx]   = Efd;
221:     xgen[idx+1] = RF;
222:     xgen[idx+2] = VR;

224:     Vref[i] = Vm + (VR/KA[i]);

226:     idx = idx + 3;
227:   }

229:   VecRestoreArray(Xgen,&xgen);
230:   VecRestoreArray(Xnet,&xnet);

232:   /* VecView(Xgen,0); */
233:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,X,Xgen,Xnet);
234:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
235:   return(0);
236: }

238: PetscErrorCode InitialGuess(Vec X,Userctx *user, const PetscScalar PGv[])
239: {
241:   Vec            Xgen,Xnet;
242:   PetscScalar    *xgen,*xnet;
243:   PetscInt       i,idx=0;
244:   PetscScalar    Vr,Vi,IGr,IGi,Vm,Vm2;
245:   PetscScalar    Eqp,Edp,delta;
246:   PetscScalar    Efd,RF,VR; /* Exciter variables */
247:   PetscScalar    Id,Iq;  /* Generator dq axis currents */
248:   PetscScalar    theta,Vd,Vq,SE;

251:   M[0] = 2*H[0]/w_s; M[1] = 2*H[1]/w_s; M[2] = 2*H[2]/w_s;
252:   D[0] = 0.1*M[0]; D[1] = 0.1*M[1]; D[2] = 0.1*M[2];

254:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);

256:   /* Network subsystem initialization */
257:   VecCopy(user->V0,Xnet);

259:   /* Generator subsystem initialization */
260:   VecGetArray(Xgen,&xgen);
261:   VecGetArray(Xnet,&xnet);

263:   for (i=0; i < ngen; i++) {
264:     Vr  = xnet[2*gbus[i]]; /* Real part of generator terminal voltage */
265:     Vi  = xnet[2*gbus[i]+1]; /* Imaginary part of the generator terminal voltage */
266:     Vm  = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm;
267:     IGr = (Vr*PGv[i] + Vi*QG[i])/Vm2;
268:     IGi = (Vi*PGv[i] - Vr*QG[i])/Vm2;

270:     delta = PetscAtan2Real(Vi+Xq[i]*IGr,Vr-Xq[i]*IGi); /* Machine angle */

272:     theta = PETSC_PI/2.0 - delta;

274:     Id = IGr*PetscCosScalar(theta) - IGi*PetscSinScalar(theta); /* d-axis stator current */
275:     Iq = IGr*PetscSinScalar(theta) + IGi*PetscCosScalar(theta); /* q-axis stator current */

277:     Vd = Vr*PetscCosScalar(theta) - Vi*PetscSinScalar(theta);
278:     Vq = Vr*PetscSinScalar(theta) + Vi*PetscCosScalar(theta);

280:     Edp = Vd + Rs[i]*Id - Xqp[i]*Iq; /* d-axis transient EMF */
281:     Eqp = Vq + Rs[i]*Iq + Xdp[i]*Id; /* q-axis transient EMF */

283:     /* The generator variables are ordered as [Eqp,Edp,delta,w,Id,Iq] */
284:     xgen[idx]   = Eqp;
285:     xgen[idx+1] = Edp;
286:     xgen[idx+2] = delta;
287:     xgen[idx+3] = w_s;

289:     idx = idx + 4;

291:     xgen[idx]   = Id;
292:     xgen[idx+1] = Iq;

294:     idx = idx + 2;

296:     /* Exciter */
297:     Efd = Eqp + (Xd[i] - Xdp[i])*Id;
298:     SE  = k1[i]*PetscExpScalar(k2[i]*Efd);
299:     VR  =  KE[i]*Efd + SE;
300:     RF  =  KF[i]*Efd/TF[i];

302:     xgen[idx]   = Efd;
303:     xgen[idx+1] = RF;
304:     xgen[idx+2] = VR;

306:     idx = idx + 3;
307:   }

309:   VecRestoreArray(Xgen,&xgen);
310:   VecRestoreArray(Xnet,&xnet);

312:   /* VecView(Xgen,0); */
313:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,X,Xgen,Xnet);
314:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
315:   return(0);
316: }

318: PetscErrorCode DICDPFiniteDifference(Vec X,Vec *DICDP, Userctx *user)
319: {
320:   Vec            Y;
321:   PetscScalar    PGv[3],eps;
323:   PetscInt       i,j;

326:   eps = 1.e-7;
327:   VecDuplicate(X,&Y);

329:   for (i=0;i<ngen;i++) {
330:     for (j=0;j<3;j++) PGv[j] = PG[j];
331:     PGv[i] = PG[i]+eps;
332:     InitialGuess(Y,user,PGv);
333:     InitialGuess(X,user,PG);

335:     VecAXPY(Y,-1.0,X);
336:     VecScale(Y,1./eps);
337:     VecCopy(Y,DICDP[i]);
338:   }
339:   VecDestroy(&Y);
340:   return(0);
341: }

343: /* Computes F = [-f(x,y);g(x,y)] */
344: PetscErrorCode ResidualFunction(SNES snes,Vec X, Vec F, Userctx *user)
345: {
347:   Vec            Xgen,Xnet,Fgen,Fnet;
348:   PetscScalar    *xgen,*xnet,*fgen,*fnet;
349:   PetscInt       i,idx=0;
350:   PetscScalar    Vr,Vi,Vm,Vm2;
351:   PetscScalar    Eqp,Edp,delta,w; /* Generator variables */
352:   PetscScalar    Efd,RF,VR; /* Exciter variables */
353:   PetscScalar    Id,Iq;  /* Generator dq axis currents */
354:   PetscScalar    Vd,Vq,SE;
355:   PetscScalar    IGr,IGi,IDr,IDi;
356:   PetscScalar    Zdq_inv[4],det;
357:   PetscScalar    PD,QD,Vm0,*v0;
358:   PetscInt       k;

361:   VecZeroEntries(F);
362:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
363:   DMCompositeGetLocalVectors(user->dmpgrid,&Fgen,&Fnet);
364:   DMCompositeScatter(user->dmpgrid,X,Xgen,Xnet);
365:   DMCompositeScatter(user->dmpgrid,F,Fgen,Fnet);

367:   /* Network current balance residual IG + Y*V + IL = 0. Only YV is added here.
368:      The generator current injection, IG, and load current injection, ID are added later
369:   */
370:   /* Note that the values in Ybus are stored assuming the imaginary current balance
371:      equation is ordered first followed by real current balance equation for each bus.
372:      Thus imaginary current contribution goes in location 2*i, and
373:      real current contribution in 2*i+1
374:   */
375:   MatMult(user->Ybus,Xnet,Fnet);

377:   VecGetArray(Xgen,&xgen);
378:   VecGetArray(Xnet,&xnet);
379:   VecGetArray(Fgen,&fgen);
380:   VecGetArray(Fnet,&fnet);

382:   /* Generator subsystem */
383:   for (i=0; i < ngen; i++) {
384:     Eqp   = xgen[idx];
385:     Edp   = xgen[idx+1];
386:     delta = xgen[idx+2];
387:     w     = xgen[idx+3];
388:     Id    = xgen[idx+4];
389:     Iq    = xgen[idx+5];
390:     Efd   = xgen[idx+6];
391:     RF    = xgen[idx+7];
392:     VR    = xgen[idx+8];

394:     /* Generator differential equations */
395:     fgen[idx]   = (Eqp + (Xd[i] - Xdp[i])*Id - Efd)/Td0p[i];
396:     fgen[idx+1] = (Edp - (Xq[i] - Xqp[i])*Iq)/Tq0p[i];
397:     fgen[idx+2] = -w + w_s;
398:     fgen[idx+3] = (-TM[i] + Edp*Id + Eqp*Iq + (Xqp[i] - Xdp[i])*Id*Iq + D[i]*(w - w_s))/M[i];

400:     Vr = xnet[2*gbus[i]]; /* Real part of generator terminal voltage */
401:     Vi = xnet[2*gbus[i]+1]; /* Imaginary part of the generator terminal voltage */

403:     ri2dq(Vr,Vi,delta,&Vd,&Vq);
404:     /* Algebraic equations for stator currents */
405:     det = Rs[i]*Rs[i] + Xdp[i]*Xqp[i];

407:     Zdq_inv[0] = Rs[i]/det;
408:     Zdq_inv[1] = Xqp[i]/det;
409:     Zdq_inv[2] = -Xdp[i]/det;
410:     Zdq_inv[3] = Rs[i]/det;

412:     fgen[idx+4] = Zdq_inv[0]*(-Edp + Vd) + Zdq_inv[1]*(-Eqp + Vq) + Id;
413:     fgen[idx+5] = Zdq_inv[2]*(-Edp + Vd) + Zdq_inv[3]*(-Eqp + Vq) + Iq;

415:     /* Add generator current injection to network */
416:     dq2ri(Id,Iq,delta,&IGr,&IGi);

418:     fnet[2*gbus[i]]   -= IGi;
419:     fnet[2*gbus[i]+1] -= IGr;

421:     Vm = PetscSqrtScalar(Vd*Vd + Vq*Vq);

423:     SE = k1[i]*PetscExpScalar(k2[i]*Efd);

425:     /* Exciter differential equations */
426:     fgen[idx+6] = (KE[i]*Efd + SE - VR)/TE[i];
427:     fgen[idx+7] = (RF - KF[i]*Efd/TF[i])/TF[i];
428:     fgen[idx+8] = (VR - KA[i]*RF + KA[i]*KF[i]*Efd/TF[i] - KA[i]*(Vref[i] - Vm))/TA[i];

430:     idx = idx + 9;
431:   }

433:   VecGetArray(user->V0,&v0);
434:   for (i=0; i < nload; i++) {
435:     Vr  = xnet[2*lbus[i]]; /* Real part of load bus voltage */
436:     Vi  = xnet[2*lbus[i]+1]; /* Imaginary part of the load bus voltage */
437:     Vm  = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm;
438:     Vm0 = PetscSqrtScalar(v0[2*lbus[i]]*v0[2*lbus[i]] + v0[2*lbus[i]+1]*v0[2*lbus[i]+1]);
439:     PD  = QD = 0.0;
440:     for (k=0; k < ld_nsegsp[i]; k++) PD += ld_alphap[k]*PD0[i]*PetscPowScalar((Vm/Vm0),ld_betap[k]);
441:     for (k=0; k < ld_nsegsq[i]; k++) QD += ld_alphaq[k]*QD0[i]*PetscPowScalar((Vm/Vm0),ld_betaq[k]);

443:     /* Load currents */
444:     IDr = (PD*Vr + QD*Vi)/Vm2;
445:     IDi = (-QD*Vr + PD*Vi)/Vm2;

447:     fnet[2*lbus[i]]   += IDi;
448:     fnet[2*lbus[i]+1] += IDr;
449:   }
450:   VecRestoreArray(user->V0,&v0);

452:   VecRestoreArray(Xgen,&xgen);
453:   VecRestoreArray(Xnet,&xnet);
454:   VecRestoreArray(Fgen,&fgen);
455:   VecRestoreArray(Fnet,&fnet);

457:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,F,Fgen,Fnet);
458:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
459:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Fgen,&Fnet);
460:   return(0);
461: }

463: /* \dot{x} - f(x,y)
464:      g(x,y) = 0
465:  */
466: PetscErrorCode IFunction(TS ts,PetscReal t, Vec X, Vec Xdot, Vec F, Userctx *user)
467: {
468:   PetscErrorCode    ierr;
469:   SNES              snes;
470:   PetscScalar       *f;
471:   const PetscScalar *xdot;
472:   PetscInt          i;

475:   user->t = t;

477:   TSGetSNES(ts,&snes);
478:   ResidualFunction(snes,X,F,user);
479:   VecGetArray(F,&f);
480:   VecGetArrayRead(Xdot,&xdot);
481:   for (i=0;i < ngen;i++) {
482:     f[9*i]   += xdot[9*i];
483:     f[9*i+1] += xdot[9*i+1];
484:     f[9*i+2] += xdot[9*i+2];
485:     f[9*i+3] += xdot[9*i+3];
486:     f[9*i+6] += xdot[9*i+6];
487:     f[9*i+7] += xdot[9*i+7];
488:     f[9*i+8] += xdot[9*i+8];
489:   }
490:   VecRestoreArray(F,&f);
491:   VecRestoreArrayRead(Xdot,&xdot);
492:   return(0);
493: }

495: /* This function is used for solving the algebraic system only during fault on and
496:    off times. It computes the entire F and then zeros out the part corresponding to
497:    differential equations
498:  F = [0;g(y)];
499: */
500: PetscErrorCode AlgFunction(SNES snes, Vec X, Vec F, void *ctx)
501: {
503:   Userctx        *user=(Userctx*)ctx;
504:   PetscScalar    *f;
505:   PetscInt       i;

508:   ResidualFunction(snes,X,F,user);
509:   VecGetArray(F,&f);
510:   for (i=0; i < ngen; i++) {
511:     f[9*i]   = 0;
512:     f[9*i+1] = 0;
513:     f[9*i+2] = 0;
514:     f[9*i+3] = 0;
515:     f[9*i+6] = 0;
516:     f[9*i+7] = 0;
517:     f[9*i+8] = 0;
518:   }
519:   VecRestoreArray(F,&f);
520:   return(0);
521: }

523: PetscErrorCode PreallocateJacobian(Mat J, Userctx *user)
524: {
526:   PetscInt       *d_nnz;
527:   PetscInt       i,idx=0,start=0;
528:   PetscInt       ncols;

531:   PetscMalloc1(user->neqs_pgrid,&d_nnz);
532:   for (i=0; i<user->neqs_pgrid; i++) d_nnz[i] = 0;
533:   /* Generator subsystem */
534:   for (i=0; i < ngen; i++) {

536:     d_nnz[idx]   += 3;
537:     d_nnz[idx+1] += 2;
538:     d_nnz[idx+2] += 2;
539:     d_nnz[idx+3] += 5;
540:     d_nnz[idx+4] += 6;
541:     d_nnz[idx+5] += 6;

543:     d_nnz[user->neqs_gen+2*gbus[i]]   += 3;
544:     d_nnz[user->neqs_gen+2*gbus[i]+1] += 3;

546:     d_nnz[idx+6] += 2;
547:     d_nnz[idx+7] += 2;
548:     d_nnz[idx+8] += 5;

550:     idx = idx + 9;
551:   }

553:   start = user->neqs_gen;
554:   for (i=0; i < nbus; i++) {
555:     MatGetRow(user->Ybus,2*i,&ncols,NULL,NULL);
556:     d_nnz[start+2*i]   += ncols;
557:     d_nnz[start+2*i+1] += ncols;
558:     MatRestoreRow(user->Ybus,2*i,&ncols,NULL,NULL);
559:   }

561:   MatSeqAIJSetPreallocation(J,0,d_nnz);
562:   PetscFree(d_nnz);
563:   return(0);
564: }

566: /*
567:    J = [-df_dx, -df_dy
568:         dg_dx, dg_dy]
569: */
570: PetscErrorCode ResidualJacobian(SNES snes,Vec X,Mat J,Mat B,void *ctx)
571: {
572:   PetscErrorCode    ierr;
573:   Userctx           *user=(Userctx*)ctx;
574:   Vec               Xgen,Xnet;
575:   PetscScalar       *xgen,*xnet;
576:   PetscInt          i,idx=0;
577:   PetscScalar       Vr,Vi,Vm,Vm2;
578:   PetscScalar       Eqp,Edp,delta; /* Generator variables */
579:   PetscScalar       Efd; /* Exciter variables */
580:   PetscScalar       Id,Iq;  /* Generator dq axis currents */
581:   PetscScalar       Vd,Vq;
582:   PetscScalar       val[10];
583:   PetscInt          row[2],col[10];
584:   PetscInt          net_start=user->neqs_gen;
585:   PetscInt          ncols;
586:   const PetscInt    *cols;
587:   const PetscScalar *yvals;
588:   PetscInt          k;
589:   PetscScalar       Zdq_inv[4],det;
590:   PetscScalar       dVd_dVr,dVd_dVi,dVq_dVr,dVq_dVi,dVd_ddelta,dVq_ddelta;
591:   PetscScalar       dIGr_ddelta,dIGi_ddelta,dIGr_dId,dIGr_dIq,dIGi_dId,dIGi_dIq;
592:   PetscScalar       dSE_dEfd;
593:   PetscScalar       dVm_dVd,dVm_dVq,dVm_dVr,dVm_dVi;
594:   PetscScalar       PD,QD,Vm0,*v0,Vm4;
595:   PetscScalar       dPD_dVr,dPD_dVi,dQD_dVr,dQD_dVi;
596:   PetscScalar       dIDr_dVr,dIDr_dVi,dIDi_dVr,dIDi_dVi;

599:   MatZeroEntries(B);
600:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
601:   DMCompositeScatter(user->dmpgrid,X,Xgen,Xnet);

603:   VecGetArray(Xgen,&xgen);
604:   VecGetArray(Xnet,&xnet);

606:   /* Generator subsystem */
607:   for (i=0; i < ngen; i++) {
608:     Eqp   = xgen[idx];
609:     Edp   = xgen[idx+1];
610:     delta = xgen[idx+2];
611:     Id    = xgen[idx+4];
612:     Iq    = xgen[idx+5];
613:     Efd   = xgen[idx+6];

615:     /*    fgen[idx]   = (Eqp + (Xd[i] - Xdp[i])*Id - Efd)/Td0p[i]; */
616:     row[0] = idx;
617:     col[0] = idx;           col[1] = idx+4;          col[2] = idx+6;
618:     val[0] = 1/ Td0p[i]; val[1] = (Xd[i] - Xdp[i])/ Td0p[i]; val[2] = -1/Td0p[i];

620:     MatSetValues(J,1,row,3,col,val,INSERT_VALUES);

622:     /*    fgen[idx+1] = (Edp - (Xq[i] - Xqp[i])*Iq)/Tq0p[i]; */
623:     row[0] = idx + 1;
624:     col[0] = idx + 1;       col[1] = idx+5;
625:     val[0] = 1/Tq0p[i]; val[1] = -(Xq[i] - Xqp[i])/Tq0p[i];
626:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

628:     /*    fgen[idx+2] = - w + w_s; */
629:     row[0] = idx + 2;
630:     col[0] = idx + 2; col[1] = idx + 3;
631:     val[0] = 0;       val[1] = -1;
632:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

634:     /*    fgen[idx+3] = (-TM[i] + Edp*Id + Eqp*Iq + (Xqp[i] - Xdp[i])*Id*Iq + D[i]*(w - w_s))/M[i]; */
635:     row[0] = idx + 3;
636:     col[0] = idx; col[1] = idx + 1; col[2] = idx + 3;       col[3] = idx + 4;                  col[4] = idx + 5;
637:     val[0] = Iq/M[i];  val[1] = Id/M[i];      val[2] = D[i]/M[i]; val[3] = (Edp + (Xqp[i]-Xdp[i])*Iq)/M[i]; val[4] = (Eqp + (Xqp[i] - Xdp[i])*Id)/M[i];
638:     MatSetValues(J,1,row,5,col,val,INSERT_VALUES);

640:     Vr   = xnet[2*gbus[i]]; /* Real part of generator terminal voltage */
641:     Vi   = xnet[2*gbus[i]+1]; /* Imaginary part of the generator terminal voltage */
642:     ri2dq(Vr,Vi,delta,&Vd,&Vq);

644:     det = Rs[i]*Rs[i] + Xdp[i]*Xqp[i];

646:     Zdq_inv[0] = Rs[i]/det;
647:     Zdq_inv[1] = Xqp[i]/det;
648:     Zdq_inv[2] = -Xdp[i]/det;
649:     Zdq_inv[3] = Rs[i]/det;

651:     dVd_dVr    = PetscSinScalar(delta); dVd_dVi = -PetscCosScalar(delta);
652:     dVq_dVr    = PetscCosScalar(delta); dVq_dVi = PetscSinScalar(delta);
653:     dVd_ddelta = Vr*PetscCosScalar(delta) + Vi*PetscSinScalar(delta);
654:     dVq_ddelta = -Vr*PetscSinScalar(delta) + Vi*PetscCosScalar(delta);

656:     /*    fgen[idx+4] = Zdq_inv[0]*(-Edp + Vd) + Zdq_inv[1]*(-Eqp + Vq) + Id; */
657:     row[0] = idx+4;
658:     col[0] = idx;         col[1] = idx+1;        col[2] = idx + 2;
659:     val[0] = -Zdq_inv[1]; val[1] = -Zdq_inv[0];  val[2] = Zdq_inv[0]*dVd_ddelta + Zdq_inv[1]*dVq_ddelta;
660:     col[3] = idx + 4; col[4] = net_start+2*gbus[i];                     col[5] = net_start + 2*gbus[i]+1;
661:     val[3] = 1;       val[4] = Zdq_inv[0]*dVd_dVr + Zdq_inv[1]*dVq_dVr; val[5] = Zdq_inv[0]*dVd_dVi + Zdq_inv[1]*dVq_dVi;
662:     MatSetValues(J,1,row,6,col,val,INSERT_VALUES);

664:     /*  fgen[idx+5] = Zdq_inv[2]*(-Edp + Vd) + Zdq_inv[3]*(-Eqp + Vq) + Iq; */
665:     row[0] = idx+5;
666:     col[0] = idx;         col[1] = idx+1;        col[2] = idx + 2;
667:     val[0] = -Zdq_inv[3]; val[1] = -Zdq_inv[2];  val[2] = Zdq_inv[2]*dVd_ddelta + Zdq_inv[3]*dVq_ddelta;
668:     col[3] = idx + 5; col[4] = net_start+2*gbus[i];                     col[5] = net_start + 2*gbus[i]+1;
669:     val[3] = 1;       val[4] = Zdq_inv[2]*dVd_dVr + Zdq_inv[3]*dVq_dVr; val[5] = Zdq_inv[2]*dVd_dVi + Zdq_inv[3]*dVq_dVi;
670:     MatSetValues(J,1,row,6,col,val,INSERT_VALUES);

672:     dIGr_ddelta = Id*PetscCosScalar(delta) - Iq*PetscSinScalar(delta);
673:     dIGi_ddelta = Id*PetscSinScalar(delta) + Iq*PetscCosScalar(delta);
674:     dIGr_dId    = PetscSinScalar(delta);  dIGr_dIq = PetscCosScalar(delta);
675:     dIGi_dId    = -PetscCosScalar(delta); dIGi_dIq = PetscSinScalar(delta);

677:     /* fnet[2*gbus[i]]   -= IGi; */
678:     row[0] = net_start + 2*gbus[i];
679:     col[0] = idx+2;        col[1] = idx + 4;   col[2] = idx + 5;
680:     val[0] = -dIGi_ddelta; val[1] = -dIGi_dId; val[2] = -dIGi_dIq;
681:     MatSetValues(J,1,row,3,col,val,INSERT_VALUES);

683:     /* fnet[2*gbus[i]+1]   -= IGr; */
684:     row[0] = net_start + 2*gbus[i]+1;
685:     col[0] = idx+2;        col[1] = idx + 4;   col[2] = idx + 5;
686:     val[0] = -dIGr_ddelta; val[1] = -dIGr_dId; val[2] = -dIGr_dIq;
687:     MatSetValues(J,1,row,3,col,val,INSERT_VALUES);

689:     Vm = PetscSqrtScalar(Vd*Vd + Vq*Vq);

691:     /*    fgen[idx+6] = (KE[i]*Efd + SE - VR)/TE[i]; */
692:     /*    SE  = k1[i]*PetscExpScalar(k2[i]*Efd); */
693:     dSE_dEfd = k1[i]*k2[i]*PetscExpScalar(k2[i]*Efd);

695:     row[0] = idx + 6;
696:     col[0] = idx + 6;                     col[1] = idx + 8;
697:     val[0] = (KE[i] + dSE_dEfd)/TE[i];  val[1] = -1/TE[i];
698:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

700:     /* Exciter differential equations */

702:     /*    fgen[idx+7] = (RF - KF[i]*Efd/TF[i])/TF[i]; */
703:     row[0] = idx + 7;
704:     col[0] = idx + 6;       col[1] = idx + 7;
705:     val[0] = (-KF[i]/TF[i])/TF[i];  val[1] = 1/TF[i];
706:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

708:     /*    fgen[idx+8] = (VR - KA[i]*RF + KA[i]*KF[i]*Efd/TF[i] - KA[i]*(Vref[i] - Vm))/TA[i]; */
709:     /* Vm = (Vd^2 + Vq^2)^0.5; */
710:     dVm_dVd    = Vd/Vm; dVm_dVq = Vq/Vm;
711:     dVm_dVr    = dVm_dVd*dVd_dVr + dVm_dVq*dVq_dVr;
712:     dVm_dVi    = dVm_dVd*dVd_dVi + dVm_dVq*dVq_dVi;
713:     row[0]     = idx + 8;
714:     col[0]     = idx + 6;           col[1] = idx + 7; col[2] = idx + 8;
715:     val[0]     = (KA[i]*KF[i]/TF[i])/TA[i]; val[1] = -KA[i]/TA[i];  val[2] = 1/TA[i];
716:     col[3]     = net_start + 2*gbus[i]; col[4] = net_start + 2*gbus[i]+1;
717:     val[3]     = KA[i]*dVm_dVr/TA[i];         val[4] = KA[i]*dVm_dVi/TA[i];
718:     MatSetValues(J,1,row,5,col,val,INSERT_VALUES);
719:     idx        = idx + 9;
720:   }

722:   for (i=0; i<nbus; i++) {
723:     MatGetRow(user->Ybus,2*i,&ncols,&cols,&yvals);
724:     row[0] = net_start + 2*i;
725:     for (k=0; k<ncols; k++) {
726:       col[k] = net_start + cols[k];
727:       val[k] = yvals[k];
728:     }
729:     MatSetValues(J,1,row,ncols,col,val,INSERT_VALUES);
730:     MatRestoreRow(user->Ybus,2*i,&ncols,&cols,&yvals);

732:     MatGetRow(user->Ybus,2*i+1,&ncols,&cols,&yvals);
733:     row[0] = net_start + 2*i+1;
734:     for (k=0; k<ncols; k++) {
735:       col[k] = net_start + cols[k];
736:       val[k] = yvals[k];
737:     }
738:     MatSetValues(J,1,row,ncols,col,val,INSERT_VALUES);
739:     MatRestoreRow(user->Ybus,2*i+1,&ncols,&cols,&yvals);
740:   }

742:   MatAssemblyBegin(J,MAT_FLUSH_ASSEMBLY);
743:   MatAssemblyEnd(J,MAT_FLUSH_ASSEMBLY);

745:   VecGetArray(user->V0,&v0);
746:   for (i=0; i < nload; i++) {
747:     Vr      = xnet[2*lbus[i]]; /* Real part of load bus voltage */
748:     Vi      = xnet[2*lbus[i]+1]; /* Imaginary part of the load bus voltage */
749:     Vm      = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2= Vm*Vm; Vm4 = Vm2*Vm2;
750:     Vm0     = PetscSqrtScalar(v0[2*lbus[i]]*v0[2*lbus[i]] + v0[2*lbus[i]+1]*v0[2*lbus[i]+1]);
751:     PD      = QD = 0.0;
752:     dPD_dVr = dPD_dVi = dQD_dVr = dQD_dVi = 0.0;
753:     for (k=0; k < ld_nsegsp[i]; k++) {
754:       PD      += ld_alphap[k]*PD0[i]*PetscPowScalar((Vm/Vm0),ld_betap[k]);
755:       dPD_dVr += ld_alphap[k]*ld_betap[k]*PD0[i]*PetscPowScalar((1/Vm0),ld_betap[k])*Vr*PetscPowScalar(Vm,(ld_betap[k]-2));
756:       dPD_dVi += ld_alphap[k]*ld_betap[k]*PD0[i]*PetscPowScalar((1/Vm0),ld_betap[k])*Vi*PetscPowScalar(Vm,(ld_betap[k]-2));
757:     }
758:     for (k=0; k < ld_nsegsq[i]; k++) {
759:       QD      += ld_alphaq[k]*QD0[i]*PetscPowScalar((Vm/Vm0),ld_betaq[k]);
760:       dQD_dVr += ld_alphaq[k]*ld_betaq[k]*QD0[i]*PetscPowScalar((1/Vm0),ld_betaq[k])*Vr*PetscPowScalar(Vm,(ld_betaq[k]-2));
761:       dQD_dVi += ld_alphaq[k]*ld_betaq[k]*QD0[i]*PetscPowScalar((1/Vm0),ld_betaq[k])*Vi*PetscPowScalar(Vm,(ld_betaq[k]-2));
762:     }

764:     /*    IDr = (PD*Vr + QD*Vi)/Vm2; */
765:     /*    IDi = (-QD*Vr + PD*Vi)/Vm2; */

767:     dIDr_dVr = (dPD_dVr*Vr + dQD_dVr*Vi + PD)/Vm2 - ((PD*Vr + QD*Vi)*2*Vr)/Vm4;
768:     dIDr_dVi = (dPD_dVi*Vr + dQD_dVi*Vi + QD)/Vm2 - ((PD*Vr + QD*Vi)*2*Vi)/Vm4;

770:     dIDi_dVr = (-dQD_dVr*Vr + dPD_dVr*Vi - QD)/Vm2 - ((-QD*Vr + PD*Vi)*2*Vr)/Vm4;
771:     dIDi_dVi = (-dQD_dVi*Vr + dPD_dVi*Vi + PD)/Vm2 - ((-QD*Vr + PD*Vi)*2*Vi)/Vm4;

773:     /*    fnet[2*lbus[i]]   += IDi; */
774:     row[0] = net_start + 2*lbus[i];
775:     col[0] = net_start + 2*lbus[i];  col[1] = net_start + 2*lbus[i]+1;
776:     val[0] = dIDi_dVr;               val[1] = dIDi_dVi;
777:     MatSetValues(J,1,row,2,col,val,ADD_VALUES);
778:     /*    fnet[2*lbus[i]+1] += IDr; */
779:     row[0] = net_start + 2*lbus[i]+1;
780:     col[0] = net_start + 2*lbus[i];  col[1] = net_start + 2*lbus[i]+1;
781:     val[0] = dIDr_dVr;               val[1] = dIDr_dVi;
782:     MatSetValues(J,1,row,2,col,val,ADD_VALUES);
783:   }
784:   VecRestoreArray(user->V0,&v0);

786:   VecRestoreArray(Xgen,&xgen);
787:   VecRestoreArray(Xnet,&xnet);

789:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);

791:   MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
792:   MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
793:   return(0);
794: }

796: /*
797:    J = [I, 0
798:         dg_dx, dg_dy]
799: */
800: PetscErrorCode AlgJacobian(SNES snes,Vec X,Mat A,Mat B,void *ctx)
801: {
803:   Userctx        *user=(Userctx*)ctx;

806:   ResidualJacobian(snes,X,A,B,ctx);
807:   MatSetOption(A,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE);
808:   MatZeroRowsIS(A,user->is_diff,1.0,NULL,NULL);
809:   return(0);
810: }

812: /*
813:    J = [a*I-df_dx, -df_dy
814:         dg_dx, dg_dy]
815: */

817: PetscErrorCode IJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat A,Mat B,Userctx *user)
818: {
820:   SNES           snes;
821:   PetscScalar    atmp = (PetscScalar) a;
822:   PetscInt       i,row;

825:   user->t = t;

827:   TSGetSNES(ts,&snes);
828:   ResidualJacobian(snes,X,A,B,user);
829:   for (i=0;i < ngen;i++) {
830:     row = 9*i;
831:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
832:     row  = 9*i+1;
833:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
834:     row  = 9*i+2;
835:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
836:     row  = 9*i+3;
837:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
838:     row  = 9*i+6;
839:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
840:     row  = 9*i+7;
841:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
842:     row  = 9*i+8;
843:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
844:   }
845:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
846:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
847:   return(0);
848: }

850: /* Matrix JacobianP is constant so that it only needs to be evaluated once */
851: static PetscErrorCode RHSJacobianP(TS ts,PetscReal t,Vec X,Mat A, void *ctx0)
852: {
854:   PetscScalar    a;
855:   PetscInt       row,col;
856:   Userctx        *ctx=(Userctx*)ctx0;


860:   if (ctx->jacp_flg) {
861:     MatZeroEntries(A);

863:     for (col=0;col<3;col++) {
864:       a    = 1.0/M[col];
865:       row  = 9*col+3;
866:       MatSetValues(A,1,&row,1,&col,&a,INSERT_VALUES);
867:     }

869:     ctx->jacp_flg = PETSC_FALSE;

871:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
872:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
873:   }
874:   return(0);
875: }

877: static PetscErrorCode CostIntegrand(TS ts,PetscReal t,Vec U,Vec R,Userctx *user)
878: {
879:   PetscErrorCode    ierr;
880:   const PetscScalar *u;
881:   PetscInt          idx;
882:   Vec               Xgen,Xnet;
883:   PetscScalar       *r,*xgen;
884:   PetscInt          i;

887:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
888:   DMCompositeScatter(user->dmpgrid,U,Xgen,Xnet);

890:   VecGetArray(Xgen,&xgen);

892:   VecGetArrayRead(U,&u);
893:   VecGetArray(R,&r);
894:   r[0] = 0.;
895:   idx = 0;
896:   for (i=0;i<ngen;i++) {
897:     r[0] += PetscPowScalarInt(PetscMax(0.,PetscMax(xgen[idx+3]/(2.*PETSC_PI)-user->freq_u,user->freq_l-xgen[idx+3]/(2.*PETSC_PI))),user->pow);
898:     idx  += 9;
899:   }
900:   VecRestoreArrayRead(U,&u);
901:   VecRestoreArray(R,&r);
902:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
903:   return(0);
904: }

906: static PetscErrorCode DRDUJacobianTranspose(TS ts,PetscReal t,Vec U,Mat DRDU,Mat B,Userctx *user)
907: {
909:   Vec            Xgen,Xnet,Dgen,Dnet;
910:   PetscScalar    *xgen,*dgen;
911:   PetscInt       i;
912:   PetscInt       idx;
913:   Vec            drdu_col;
914:   PetscScalar    *xarr;

917:   VecDuplicate(U,&drdu_col);
918:   MatDenseGetColumn(DRDU,0,&xarr);
919:   VecPlaceArray(drdu_col,xarr);
920:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
921:   DMCompositeGetLocalVectors(user->dmpgrid,&Dgen,&Dnet);
922:   DMCompositeScatter(user->dmpgrid,U,Xgen,Xnet);
923:   DMCompositeScatter(user->dmpgrid,drdu_col,Dgen,Dnet);

925:   VecGetArray(Xgen,&xgen);
926:   VecGetArray(Dgen,&dgen);

928:   idx = 0;
929:   for (i=0;i<ngen;i++) {
930:     dgen[idx+3] = 0.;
931:     if (xgen[idx+3]/(2.*PETSC_PI) > user->freq_u) dgen[idx+3] = user->pow*PetscPowScalarInt(xgen[idx+3]/(2.*PETSC_PI)-user->freq_u,user->pow-1)/(2.*PETSC_PI);
932:     if (xgen[idx+3]/(2.*PETSC_PI) < user->freq_l) dgen[idx+3] = user->pow*PetscPowScalarInt(user->freq_l-xgen[idx+3]/(2.*PETSC_PI),user->pow-1)/(-2.*PETSC_PI);
933:     idx += 9;
934:   }

936:   VecRestoreArray(Dgen,&dgen);
937:   VecRestoreArray(Xgen,&xgen);
938:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,drdu_col,Dgen,Dnet);
939:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Dgen,&Dnet);
940:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
941:   VecResetArray(drdu_col);
942:   MatDenseRestoreColumn(DRDU,&xarr);
943:   VecDestroy(&drdu_col);
944:   return(0);
945: }

947: static PetscErrorCode DRDPJacobianTranspose(TS ts,PetscReal t,Vec U,Mat drdp,Userctx *user)
948: {
950:   return(0);
951: }

953: PetscErrorCode ComputeSensiP(Vec lambda,Vec mu,Vec *DICDP,Userctx *user)
954: {
956:   PetscScalar    *x,*y,sensip;
957:   PetscInt       i;

960:   VecGetArray(lambda,&x);
961:   VecGetArray(mu,&y);

963:   for (i=0;i<3;i++) {
964:     VecDot(lambda,DICDP[i],&sensip);
965:     sensip = sensip+y[i];
966:     /* PetscPrintf(PETSC_COMM_WORLD,"\n sensitivity wrt %D th parameter: %g \n",i,(double)sensip); */
967:      y[i] = sensip;
968:   }
969:   VecRestoreArray(mu,&y);
970:   return(0);
971: }

973: int main(int argc,char **argv)
974: {
975:   Userctx            user;
976:   Vec                p;
977:   PetscScalar        *x_ptr;
978:   PetscErrorCode     ierr;
979:   PetscMPIInt        size;
980:   PetscInt           i;
981:   PetscViewer        Xview,Ybusview;
982:   PetscInt           *idx2;
983:   Tao                tao;
984:   KSP                ksp;
985:   PC                 pc;
986:   Vec                lowerb,upperb;

988:   PetscInitialize(&argc,&argv,"petscoptions",help);if (ierr) return ierr;
989:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
990:   if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");

992:   user.jacp_flg   = PETSC_TRUE;
993:   user.neqs_gen   = 9*ngen; /* # eqs. for generator subsystem */
994:   user.neqs_net   = 2*nbus; /* # eqs. for network subsystem   */
995:   user.neqs_pgrid = user.neqs_gen + user.neqs_net;

997:   /* Create indices for differential and algebraic equations */
998:   PetscMalloc1(7*ngen,&idx2);
999:   for (i=0; i<ngen; i++) {
1000:     idx2[7*i]   = 9*i;   idx2[7*i+1] = 9*i+1; idx2[7*i+2] = 9*i+2; idx2[7*i+3] = 9*i+3;
1001:     idx2[7*i+4] = 9*i+6; idx2[7*i+5] = 9*i+7; idx2[7*i+6] = 9*i+8;
1002:   }
1003:   ISCreateGeneral(PETSC_COMM_WORLD,7*ngen,idx2,PETSC_COPY_VALUES,&user.is_diff);
1004:   ISComplement(user.is_diff,0,user.neqs_pgrid,&user.is_alg);
1005:   PetscFree(idx2);

1007:   /* Set run time options */
1008:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Transient stability fault options","");
1009:   {
1010:     user.tfaulton  = 1.0;
1011:     user.tfaultoff = 1.2;
1012:     user.Rfault    = 0.0001;
1013:     user.faultbus  = 8;
1014:     PetscOptionsReal("-tfaulton","","",user.tfaulton,&user.tfaulton,NULL);
1015:     PetscOptionsReal("-tfaultoff","","",user.tfaultoff,&user.tfaultoff,NULL);
1016:     PetscOptionsInt("-faultbus","","",user.faultbus,&user.faultbus,NULL);
1017:     user.t0        = 0.0;
1018:     user.tmax      = 1.3;
1019:     PetscOptionsReal("-t0","","",user.t0,&user.t0,NULL);
1020:     PetscOptionsReal("-tmax","","",user.tmax,&user.tmax,NULL);
1021:     user.freq_u    = 61.0;
1022:     user.freq_l    = 59.0;
1023:     user.pow       = 2;
1024:     PetscOptionsReal("-frequ","","",user.freq_u,&user.freq_u,NULL);
1025:     PetscOptionsReal("-freql","","",user.freq_l,&user.freq_l,NULL);
1026:     PetscOptionsInt("-pow","","",user.pow,&user.pow,NULL);

1028:   }
1029:   PetscOptionsEnd();

1031:   /* Create DMs for generator and network subsystems */
1032:   DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,user.neqs_gen,1,1,NULL,&user.dmgen);
1033:   DMSetOptionsPrefix(user.dmgen,"dmgen_");
1034:   DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,user.neqs_net,1,1,NULL,&user.dmnet);
1035:   DMSetOptionsPrefix(user.dmnet,"dmnet_");
1036:   DMSetFromOptions(user.dmnet);
1037:   DMSetUp(user.dmnet);
1038:   /* Create a composite DM packer and add the two DMs */
1039:   DMCompositeCreate(PETSC_COMM_WORLD,&user.dmpgrid);
1040:   DMSetOptionsPrefix(user.dmpgrid,"pgrid_");
1041:   DMSetFromOptions(user.dmgen);
1042:   DMSetUp(user.dmgen);
1043:   DMCompositeAddDM(user.dmpgrid,user.dmgen);
1044:   DMCompositeAddDM(user.dmpgrid,user.dmnet);

1046:   /* Read initial voltage vector and Ybus */
1047:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"X.bin",FILE_MODE_READ,&Xview);
1048:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"Ybus.bin",FILE_MODE_READ,&Ybusview);

1050:   VecCreate(PETSC_COMM_WORLD,&user.V0);
1051:   VecSetSizes(user.V0,PETSC_DECIDE,user.neqs_net);
1052:   VecLoad(user.V0,Xview);

1054:   MatCreate(PETSC_COMM_WORLD,&user.Ybus);
1055:   MatSetSizes(user.Ybus,PETSC_DECIDE,PETSC_DECIDE,user.neqs_net,user.neqs_net);
1056:   MatSetType(user.Ybus,MATBAIJ);
1057:   /*  MatSetBlockSize(ctx->Ybus,2); */
1058:   MatLoad(user.Ybus,Ybusview);

1060:   PetscViewerDestroy(&Xview);
1061:   PetscViewerDestroy(&Ybusview);

1063:   /* Allocate space for Jacobians */
1064:   MatCreate(PETSC_COMM_WORLD,&user.J);
1065:   MatSetSizes(user.J,PETSC_DECIDE,PETSC_DECIDE,user.neqs_pgrid,user.neqs_pgrid);
1066:   MatSetFromOptions(user.J);
1067:   PreallocateJacobian(user.J,&user);

1069:   MatCreate(PETSC_COMM_WORLD,&user.Jacp);
1070:   MatSetSizes(user.Jacp,PETSC_DECIDE,PETSC_DECIDE,user.neqs_pgrid,3);
1071:   MatSetFromOptions(user.Jacp);
1072:   MatSetUp(user.Jacp);
1073:   MatZeroEntries(user.Jacp); /* initialize to zeros */

1075:   MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,3,1,NULL,&user.DRDP);
1076:   MatSetUp(user.DRDP);
1077:   MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,user.neqs_pgrid,1,NULL,&user.DRDU);
1078:   MatSetUp(user.DRDU);

1080:   /* Create TAO solver and set desired solution method */
1081:   TaoCreate(PETSC_COMM_WORLD,&tao);
1082:   TaoSetType(tao,TAOBLMVM);
1083:   /*
1084:      Optimization starts
1085:   */
1086:   /* Set initial solution guess */
1087:   VecCreateSeq(PETSC_COMM_WORLD,3,&p);
1088:   VecGetArray(p,&x_ptr);
1089:   x_ptr[0] = PG[0]; x_ptr[1] = PG[1]; x_ptr[2] = PG[2];
1090:   VecRestoreArray(p,&x_ptr);

1092:   TaoSetInitialVector(tao,p);
1093:   /* Set routine for function and gradient evaluation */
1094:   TaoSetObjectiveAndGradientRoutine(tao,FormFunctionGradient,&user);

1096:   /* Set bounds for the optimization */
1097:   VecDuplicate(p,&lowerb);
1098:   VecDuplicate(p,&upperb);
1099:   VecGetArray(lowerb,&x_ptr);
1100:   x_ptr[0] = 0.5; x_ptr[1] = 0.5; x_ptr[2] = 0.5;
1101:   VecRestoreArray(lowerb,&x_ptr);
1102:   VecGetArray(upperb,&x_ptr);
1103:   x_ptr[0] = 2.0; x_ptr[1] = 2.0; x_ptr[2] = 2.0;
1104:   VecRestoreArray(upperb,&x_ptr);
1105:   TaoSetVariableBounds(tao,lowerb,upperb);

1107:   /* Check for any TAO command line options */
1108:   TaoSetFromOptions(tao);
1109:   TaoGetKSP(tao,&ksp);
1110:   if (ksp) {
1111:     KSPGetPC(ksp,&pc);
1112:     PCSetType(pc,PCNONE);
1113:   }

1115:   /* SOLVE THE APPLICATION */
1116:   TaoSolve(tao);

1118:   VecView(p,PETSC_VIEWER_STDOUT_WORLD);
1119:   /* Free TAO data structures */
1120:   TaoDestroy(&tao);

1122:   DMDestroy(&user.dmgen);
1123:   DMDestroy(&user.dmnet);
1124:   DMDestroy(&user.dmpgrid);
1125:   ISDestroy(&user.is_diff);
1126:   ISDestroy(&user.is_alg);

1128:   MatDestroy(&user.J);
1129:   MatDestroy(&user.Jacp);
1130:   MatDestroy(&user.Ybus);
1131:   /* MatDestroy(&user.Sol); */
1132:   VecDestroy(&user.V0);
1133:   VecDestroy(&p);
1134:   VecDestroy(&lowerb);
1135:   VecDestroy(&upperb);
1136:   MatDestroy(&user.DRDU);
1137:   MatDestroy(&user.DRDP);
1138:   PetscFinalize();
1139:   return ierr;
1140: }

1142: /* ------------------------------------------------------------------ */
1143: /*
1144:    FormFunction - Evaluates the function and corresponding gradient.

1146:    Input Parameters:
1147:    tao - the Tao context
1148:    X   - the input vector
1149:    ptr - optional user-defined context, as set by TaoSetObjectiveAndGradientRoutine()

1151:    Output Parameters:
1152:    f   - the newly evaluated function
1153:    G   - the newly evaluated gradient
1154: */
1155: PetscErrorCode FormFunctionGradient(Tao tao,Vec P,PetscReal *f,Vec G,void *ctx0)
1156: {
1157:   TS             ts,quadts;
1158:   SNES           snes_alg;
1160:   Userctx        *ctx = (Userctx*)ctx0;
1161:   Vec            X;
1162:   PetscInt       i;
1163:   /* sensitivity context */
1164:   PetscScalar    *x_ptr;
1165:   Vec            lambda[1],q;
1166:   Vec            mu[1];
1167:   PetscInt       steps1,steps2,steps3;
1168:   Vec            DICDP[3];
1169:   Vec            F_alg;
1170:   PetscInt       row_loc,col_loc;
1171:   PetscScalar    val;
1172:   Vec            Xdot;

1175:   VecGetArrayRead(P,(const PetscScalar**)&x_ptr);
1176:   PG[0] = x_ptr[0];
1177:   PG[1] = x_ptr[1];
1178:   PG[2] = x_ptr[2];
1179:   VecRestoreArrayRead(P,(const PetscScalar**)&x_ptr);

1181:   ctx->stepnum = 0;

1183:   DMCreateGlobalVector(ctx->dmpgrid,&X);

1185:   /* Create matrix to save solutions at each time step */
1186:   /* MatCreateSeqDense(PETSC_COMM_SELF,ctx->neqs_pgrid+1,1002,NULL,&ctx->Sol); */
1187:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1188:      Create timestepping solver context
1189:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1190:   TSCreate(PETSC_COMM_WORLD,&ts);
1191:   TSSetProblemType(ts,TS_NONLINEAR);
1192:   TSSetType(ts,TSCN);
1193:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,ctx);
1194:   TSSetIJacobian(ts,ctx->J,ctx->J,(TSIJacobian)IJacobian,ctx);
1195:   TSSetApplicationContext(ts,ctx);
1196:   /*   Set RHS JacobianP */
1197:   TSSetRHSJacobianP(ts,ctx->Jacp,RHSJacobianP,ctx);

1199:   TSCreateQuadratureTS(ts,PETSC_FALSE,&quadts);
1200:   TSSetRHSFunction(quadts,NULL,(TSRHSFunction)CostIntegrand,ctx);
1201:   TSSetRHSJacobian(quadts,ctx->DRDU,ctx->DRDU,(TSRHSJacobian)DRDUJacobianTranspose,ctx);
1202:   TSSetRHSJacobianP(quadts,ctx->DRDP,(TSRHSJacobianP)DRDPJacobianTranspose,ctx);

1204:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1205:      Set initial conditions
1206:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1207:   SetInitialGuess(X,ctx);

1209:   /* Approximate DICDP with finite difference, we want to zero out network variables */
1210:   for (i=0;i<3;i++) {
1211:     VecDuplicate(X,&DICDP[i]);
1212:   }
1213:   DICDPFiniteDifference(X,DICDP,ctx);

1215:   VecDuplicate(X,&F_alg);
1216:   SNESCreate(PETSC_COMM_WORLD,&snes_alg);
1217:   SNESSetFunction(snes_alg,F_alg,AlgFunction,ctx);
1218:   MatZeroEntries(ctx->J);
1219:   SNESSetJacobian(snes_alg,ctx->J,ctx->J,AlgJacobian,ctx);
1220:   SNESSetOptionsPrefix(snes_alg,"alg_");
1221:   SNESSetFromOptions(snes_alg);
1222:   ctx->alg_flg = PETSC_TRUE;
1223:   /* Solve the algebraic equations */
1224:   SNESSolve(snes_alg,NULL,X);

1226:   /* Just to set up the Jacobian structure */
1227:   VecDuplicate(X,&Xdot);
1228:   IJacobian(ts,0.0,X,Xdot,0.0,ctx->J,ctx->J,ctx);
1229:   VecDestroy(&Xdot);

1231:   ctx->stepnum++;

1233:   /*
1234:     Save trajectory of solution so that TSAdjointSolve() may be used
1235:   */
1236:   TSSetSaveTrajectory(ts);

1238:   TSSetTimeStep(ts,0.01);
1239:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);
1240:   TSSetFromOptions(ts);
1241:   /* TSSetPostStep(ts,SaveSolution); */

1243:   /* Prefault period */
1244:   ctx->alg_flg = PETSC_FALSE;
1245:   TSSetTime(ts,0.0);
1246:   TSSetMaxTime(ts,ctx->tfaulton);
1247:   TSSolve(ts,X);
1248:   TSGetStepNumber(ts,&steps1);

1250:   /* Create the nonlinear solver for solving the algebraic system */
1251:   /* Note that although the algebraic system needs to be solved only for
1252:      Idq and V, we reuse the entire system including xgen. The xgen
1253:      variables are held constant by setting their residuals to 0 and
1254:      putting a 1 on the Jacobian diagonal for xgen rows
1255:   */
1256:   MatZeroEntries(ctx->J);

1258:   /* Apply disturbance - resistive fault at ctx->faultbus */
1259:   /* This is done by adding shunt conductance to the diagonal location
1260:      in the Ybus matrix */
1261:   row_loc = 2*ctx->faultbus; col_loc = 2*ctx->faultbus+1; /* Location for G */
1262:   val     = 1/ctx->Rfault;
1263:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
1264:   row_loc = 2*ctx->faultbus+1; col_loc = 2*ctx->faultbus; /* Location for G */
1265:   val     = 1/ctx->Rfault;
1266:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

1268:   MatAssemblyBegin(ctx->Ybus,MAT_FINAL_ASSEMBLY);
1269:   MatAssemblyEnd(ctx->Ybus,MAT_FINAL_ASSEMBLY);

1271:   ctx->alg_flg = PETSC_TRUE;
1272:   /* Solve the algebraic equations */
1273:   SNESSolve(snes_alg,NULL,X);

1275:   ctx->stepnum++;

1277:   /* Disturbance period */
1278:   ctx->alg_flg = PETSC_FALSE;
1279:   TSSetTime(ts,ctx->tfaulton);
1280:   TSSetMaxTime(ts,ctx->tfaultoff);
1281:   TSSolve(ts,X);
1282:   TSGetStepNumber(ts,&steps2);
1283:   steps2 -= steps1;

1285:   /* Remove the fault */
1286:   row_loc = 2*ctx->faultbus; col_loc = 2*ctx->faultbus+1;
1287:   val     = -1/ctx->Rfault;
1288:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
1289:   row_loc = 2*ctx->faultbus+1; col_loc = 2*ctx->faultbus;
1290:   val     = -1/ctx->Rfault;
1291:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

1293:   MatAssemblyBegin(ctx->Ybus,MAT_FINAL_ASSEMBLY);
1294:   MatAssemblyEnd(ctx->Ybus,MAT_FINAL_ASSEMBLY);

1296:   MatZeroEntries(ctx->J);

1298:   ctx->alg_flg = PETSC_TRUE;

1300:   /* Solve the algebraic equations */
1301:   SNESSolve(snes_alg,NULL,X);

1303:   ctx->stepnum++;

1305:   /* Post-disturbance period */
1306:   ctx->alg_flg = PETSC_TRUE;
1307:   TSSetTime(ts,ctx->tfaultoff);
1308:   TSSetMaxTime(ts,ctx->tmax);
1309:   TSSolve(ts,X);
1310:   TSGetStepNumber(ts,&steps3);
1311:   steps3 -= steps2;
1312:   steps3 -= steps1;

1314:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1315:      Adjoint model starts here
1316:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1317:   TSSetPostStep(ts,NULL);
1318:   MatCreateVecs(ctx->J,&lambda[0],NULL);
1319:   /*   Set initial conditions for the adjoint integration */
1320:   VecZeroEntries(lambda[0]);

1322:   MatCreateVecs(ctx->Jacp,&mu[0],NULL);
1323:   VecZeroEntries(mu[0]);
1324:   TSSetCostGradients(ts,1,lambda,mu);

1326:   TSAdjointSetSteps(ts,steps3);
1327:   TSAdjointSolve(ts);

1329:   MatZeroEntries(ctx->J);
1330:   /* Applying disturbance - resistive fault at ctx->faultbus */
1331:   /* This is done by deducting shunt conductance to the diagonal location
1332:      in the Ybus matrix */
1333:   row_loc = 2*ctx->faultbus; col_loc = 2*ctx->faultbus+1; /* Location for G */
1334:   val     = 1./ctx->Rfault;
1335:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
1336:   row_loc = 2*ctx->faultbus+1; col_loc = 2*ctx->faultbus; /* Location for G */
1337:   val     = 1./ctx->Rfault;
1338:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

1340:   MatAssemblyBegin(ctx->Ybus,MAT_FINAL_ASSEMBLY);
1341:   MatAssemblyEnd(ctx->Ybus,MAT_FINAL_ASSEMBLY);

1343:   /*   Set number of steps for the adjoint integration */
1344:   TSAdjointSetSteps(ts,steps2);
1345:   TSAdjointSolve(ts);

1347:   MatZeroEntries(ctx->J);
1348:   /* remove the fault */
1349:   row_loc = 2*ctx->faultbus; col_loc = 2*ctx->faultbus+1; /* Location for G */
1350:   val     = -1./ctx->Rfault;
1351:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
1352:   row_loc = 2*ctx->faultbus+1; col_loc = 2*ctx->faultbus; /* Location for G */
1353:   val     = -1./ctx->Rfault;
1354:   MatSetValues(ctx->Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

1356:   MatAssemblyBegin(ctx->Ybus,MAT_FINAL_ASSEMBLY);
1357:   MatAssemblyEnd(ctx->Ybus,MAT_FINAL_ASSEMBLY);

1359:   /*   Set number of steps for the adjoint integration */
1360:   TSAdjointSetSteps(ts,steps1);
1361:   TSAdjointSolve(ts);

1363:   ComputeSensiP(lambda[0],mu[0],DICDP,ctx);
1364:   VecCopy(mu[0],G);

1366:   TSGetQuadratureTS(ts,NULL,&quadts);
1367:   TSGetSolution(quadts,&q);
1368:   VecGetArray(q,&x_ptr);
1369:   *f   = x_ptr[0];
1370:   x_ptr[0] = 0;
1371:   VecRestoreArray(q,&x_ptr);

1373:   VecDestroy(&lambda[0]);
1374:   VecDestroy(&mu[0]);

1376:   SNESDestroy(&snes_alg);
1377:   VecDestroy(&F_alg);
1378:   VecDestroy(&X);
1379:   TSDestroy(&ts);
1380:   for (i=0;i<3;i++) {
1381:     VecDestroy(&DICDP[i]);
1382:   }
1383:   return(0);
1384: }

1386: /*TEST

1388:    build:
1389:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)

1391:    test:
1392:       args: -viewer_binary_skip_info -tao_monitor -tao_gttol .2
1393:       localrunfiles: petscoptions X.bin Ybus.bin

1395: TEST*/