Date: 2022-03-04T17:39:43+00:00

Actual source code: ex9busadj.c

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

      9: /*
     10:    The equations for the stability analysis are described by the DAE. See ex9bus.c for details.
     11:    The system has 'jumps' due to faults, thus the time interval is split into multiple sections, and TSSolve is called for each of them. But TSAdjointSolve only needs to be called once since the whole trajectory has been saved in the forward run.
     12:    The code computes the sensitivity of a final state w.r.t. initial conditions.
     13: */

     15: #include <petscts.h>
     16: #include <petscdm.h>
     17: #include <petscdmda.h>
     18: #include <petscdmcomposite.h>

     20: #define freq 60
     21: #define w_s (2*PETSC_PI*freq)

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

     30: /* Generator real and reactive powers (found via loadflow) */
     31: const PetscScalar PG[3] = {0.716786142395021,1.630000000000000,0.850000000000000};
     32: const PetscScalar QG[3] = {0.270702180178785,0.066120127797275,-0.108402221791588};
     33: /* Generator constants */
     34: const PetscScalar H[3]    = {23.64,6.4,3.01};   /* Inertia constant */
     35: const PetscScalar Rs[3]   = {0.0,0.0,0.0}; /* Stator Resistance */
     36: const PetscScalar Xd[3]   = {0.146,0.8958,1.3125};  /* d-axis reactance */
     37: const PetscScalar Xdp[3]  = {0.0608,0.1198,0.1813}; /* d-axis transient reactance */
     38: 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 */
     39: const PetscScalar Xqp[3]  = {0.0969,0.1969,0.25}; /* q-axis transient reactance */
     40: const PetscScalar Td0p[3] = {8.96,6.0,5.89}; /* d-axis open circuit time constant */
     41: const PetscScalar Tq0p[3] = {0.31,0.535,0.6}; /* q-axis open circuit time constant */
     42: PetscScalar M[3]; /* M = 2*H/w_s */
     43: PetscScalar D[3]; /* D = 0.1*M */

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

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

     70:     Note: All loads have the same characteristic currently.
     71: */
     72: const PetscScalar PD0[3] = {1.25,0.9,1.0};
     73: const PetscScalar QD0[3] = {0.5,0.3,0.35};
     74: const PetscInt    ld_nsegsp[3] = {3,3,3};
     75: const PetscScalar ld_alphap[3] = {1.0,0.0,0.0};
     76: const PetscScalar ld_betap[3]  = {2.0,1.0,0.0};
     77: const PetscInt    ld_nsegsq[3] = {3,3,3};
     78: const PetscScalar ld_alphaq[3] = {1.0,0.0,0.0};
     79: const PetscScalar ld_betaq[3]  = {2.0,1.0,0.0};

     81: typedef struct {
     82:   DM          dmgen, dmnet; /* DMs to manage generator and network subsystem */
     83:   DM          dmpgrid; /* Composite DM to manage the entire power grid */
     84:   Mat         Ybus; /* Network admittance matrix */
     85:   Vec         V0;  /* Initial voltage vector (Power flow solution) */
     86:   PetscReal   tfaulton,tfaultoff; /* Fault on and off times */
     87:   PetscInt    faultbus; /* Fault bus */
     88:   PetscScalar Rfault;
     89:   PetscReal   t0,tmax;
     90:   PetscInt    neqs_gen,neqs_net,neqs_pgrid;
     91:   PetscBool   alg_flg;
     92:   PetscReal   t;
     93:   IS          is_diff; /* indices for differential equations */
     94:   IS          is_alg; /* indices for algebraic equations */
     95: } Userctx;

     97: /* Converts from machine frame (dq) to network (phase a real,imag) reference frame */
     98: PetscErrorCode dq2ri(PetscScalar Fd,PetscScalar Fq,PetscScalar delta,PetscScalar *Fr, PetscScalar *Fi)
     99: {
    101:   *Fr =  Fd*PetscSinScalar(delta) + Fq*PetscCosScalar(delta);
    102:   *Fi = -Fd*PetscCosScalar(delta) + Fq*PetscSinScalar(delta);
    103:   return(0);
    104: }

    106: /* Converts from network frame ([phase a real,imag) to machine (dq) reference frame */
    107: PetscErrorCode ri2dq(PetscScalar Fr,PetscScalar Fi,PetscScalar delta,PetscScalar *Fd, PetscScalar *Fq)
    108: {
    110:   *Fd =  Fr*PetscSinScalar(delta) - Fi*PetscCosScalar(delta);
    111:   *Fq =  Fr*PetscCosScalar(delta) + Fi*PetscSinScalar(delta);
    112:   return(0);
    113: }

    115: PetscErrorCode SetInitialGuess(Vec X,Userctx *user)
    116: {
    118:   Vec            Xgen,Xnet;
    119:   PetscScalar    *xgen,*xnet;
    120:   PetscInt       i,idx=0;
    121:   PetscScalar    Vr,Vi,IGr,IGi,Vm,Vm2;
    122:   PetscScalar    Eqp,Edp,delta;
    123:   PetscScalar    Efd,RF,VR; /* Exciter variables */
    124:   PetscScalar    Id,Iq;  /* Generator dq axis currents */
    125:   PetscScalar    theta,Vd,Vq,SE;

    128:   M[0] = 2*H[0]/w_s; M[1] = 2*H[1]/w_s; M[2] = 2*H[2]/w_s;
    129:   D[0] = 0.1*M[0]; D[1] = 0.1*M[1]; D[2] = 0.1*M[2];

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

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

    136:   /* Generator subsystem initialization */
    137:   VecGetArray(Xgen,&xgen);
    138:   VecGetArray(Xnet,&xnet);

    140:   for (i=0; i < ngen; i++) {
    141:     Vr  = xnet[2*gbus[i]]; /* Real part of generator terminal voltage */
    142:     Vi  = xnet[2*gbus[i]+1]; /* Imaginary part of the generator terminal voltage */
    143:     Vm  = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm;
    144:     IGr = (Vr*PG[i] + Vi*QG[i])/Vm2;
    145:     IGi = (Vi*PG[i] - Vr*QG[i])/Vm2;

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

    149:     theta = PETSC_PI/2.0 - delta;

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

    154:     Vd = Vr*PetscCosScalar(theta) - Vi*PetscSinScalar(theta);
    155:     Vq = Vr*PetscSinScalar(theta) + Vi*PetscCosScalar(theta);

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

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

    162:     /* The generator variables are ordered as [Eqp,Edp,delta,w,Id,Iq] */
    163:     xgen[idx]   = Eqp;
    164:     xgen[idx+1] = Edp;
    165:     xgen[idx+2] = delta;
    166:     xgen[idx+3] = w_s;

    168:     idx = idx + 4;

    170:     xgen[idx]   = Id;
    171:     xgen[idx+1] = Iq;

    173:     idx = idx + 2;

    175:     /* Exciter */
    176:     Efd = Eqp + (Xd[i] - Xdp[i])*Id;
    177:     SE  = k1[i]*PetscExpScalar(k2[i]*Efd);
    178:     VR  =  KE[i]*Efd + SE;
    179:     RF  =  KF[i]*Efd/TF[i];

    181:     xgen[idx]   = Efd;
    182:     xgen[idx+1] = RF;
    183:     xgen[idx+2] = VR;

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

    187:     idx = idx + 3;
    188:   }

    190:   VecRestoreArray(Xgen,&xgen);
    191:   VecRestoreArray(Xnet,&xnet);

    193:   /* VecView(Xgen,0); */
    194:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,X,Xgen,Xnet);
    195:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
    196:   return(0);
    197: }

    199: /* Computes F = [f(x,y);g(x,y)] */
    200: PetscErrorCode ResidualFunction(SNES snes,Vec X, Vec F, Userctx *user)
    201: {
    203:   Vec            Xgen,Xnet,Fgen,Fnet;
    204:   PetscScalar    *xgen,*xnet,*fgen,*fnet;
    205:   PetscInt       i,idx=0;
    206:   PetscScalar    Vr,Vi,Vm,Vm2;
    207:   PetscScalar    Eqp,Edp,delta,w; /* Generator variables */
    208:   PetscScalar    Efd,RF,VR; /* Exciter variables */
    209:   PetscScalar    Id,Iq;  /* Generator dq axis currents */
    210:   PetscScalar    Vd,Vq,SE;
    211:   PetscScalar    IGr,IGi,IDr,IDi;
    212:   PetscScalar    Zdq_inv[4],det;
    213:   PetscScalar    PD,QD,Vm0,*v0;
    214:   PetscInt       k;

    217:   VecZeroEntries(F);
    218:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
    219:   DMCompositeGetLocalVectors(user->dmpgrid,&Fgen,&Fnet);
    220:   DMCompositeScatter(user->dmpgrid,X,Xgen,Xnet);
    221:   DMCompositeScatter(user->dmpgrid,F,Fgen,Fnet);

    223:   /* Network current balance residual IG + Y*V + IL = 0. Only YV is added here.
    224:      The generator current injection, IG, and load current injection, ID are added later
    225:   */
    226:   /* Note that the values in Ybus are stored assuming the imaginary current balance
    227:      equation is ordered first followed by real current balance equation for each bus.
    228:      Thus imaginary current contribution goes in location 2*i, and
    229:      real current contribution in 2*i+1
    230:   */
    231:   MatMult(user->Ybus,Xnet,Fnet);

    233:   VecGetArray(Xgen,&xgen);
    234:   VecGetArray(Xnet,&xnet);
    235:   VecGetArray(Fgen,&fgen);
    236:   VecGetArray(Fnet,&fnet);

    238:   /* Generator subsystem */
    239:   for (i=0; i < ngen; i++) {
    240:     Eqp   = xgen[idx];
    241:     Edp   = xgen[idx+1];
    242:     delta = xgen[idx+2];
    243:     w     = xgen[idx+3];
    244:     Id    = xgen[idx+4];
    245:     Iq    = xgen[idx+5];
    246:     Efd   = xgen[idx+6];
    247:     RF    = xgen[idx+7];
    248:     VR    = xgen[idx+8];

    250:     /* Generator differential equations */
    251:     fgen[idx]   = (Eqp + (Xd[i] - Xdp[i])*Id - Efd)/Td0p[i];
    252:     fgen[idx+1] = (Edp - (Xq[i] - Xqp[i])*Iq)/Tq0p[i];
    253:     fgen[idx+2] = -w + w_s;
    254:     fgen[idx+3] = (-TM[i] + Edp*Id + Eqp*Iq + (Xqp[i] - Xdp[i])*Id*Iq + D[i]*(w - w_s))/M[i];

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

    259:     ri2dq(Vr,Vi,delta,&Vd,&Vq);
    260:     /* Algebraic equations for stator currents */

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

    264:     Zdq_inv[0] = Rs[i]/det;
    265:     Zdq_inv[1] = Xqp[i]/det;
    266:     Zdq_inv[2] = -Xdp[i]/det;
    267:     Zdq_inv[3] = Rs[i]/det;

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

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

    275:     fnet[2*gbus[i]]   -= IGi;
    276:     fnet[2*gbus[i]+1] -= IGr;

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

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

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

    287:     idx = idx + 9;
    288:   }

    290:   VecGetArray(user->V0,&v0);
    291:   for (i=0; i < nload; i++) {
    292:     Vr  = xnet[2*lbus[i]]; /* Real part of load bus voltage */
    293:     Vi  = xnet[2*lbus[i]+1]; /* Imaginary part of the load bus voltage */
    294:     Vm  = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm;
    295:     Vm0 = PetscSqrtScalar(v0[2*lbus[i]]*v0[2*lbus[i]] + v0[2*lbus[i]+1]*v0[2*lbus[i]+1]);
    296:     PD  = QD = 0.0;
    297:     for (k=0; k < ld_nsegsp[i]; k++) PD += ld_alphap[k]*PD0[i]*PetscPowScalar((Vm/Vm0),ld_betap[k]);
    298:     for (k=0; k < ld_nsegsq[i]; k++) QD += ld_alphaq[k]*QD0[i]*PetscPowScalar((Vm/Vm0),ld_betaq[k]);

    300:     /* Load currents */
    301:     IDr = (PD*Vr + QD*Vi)/Vm2;
    302:     IDi = (-QD*Vr + PD*Vi)/Vm2;

    304:     fnet[2*lbus[i]]   += IDi;
    305:     fnet[2*lbus[i]+1] += IDr;
    306:   }
    307:   VecRestoreArray(user->V0,&v0);

    309:   VecRestoreArray(Xgen,&xgen);
    310:   VecRestoreArray(Xnet,&xnet);
    311:   VecRestoreArray(Fgen,&fgen);
    312:   VecRestoreArray(Fnet,&fnet);

    314:   DMCompositeGather(user->dmpgrid,INSERT_VALUES,F,Fgen,Fnet);
    315:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);
    316:   DMCompositeRestoreLocalVectors(user->dmpgrid,&Fgen,&Fnet);
    317:   return(0);
    318: }

    320: /* \dot{x} - f(x,y)
    321:      g(x,y) = 0
    322:  */
    323: PetscErrorCode IFunction(TS ts,PetscReal t, Vec X, Vec Xdot, Vec F, Userctx *user)
    324: {
    325:   PetscErrorCode    ierr;
    326:   SNES              snes;
    327:   PetscScalar       *f;
    328:   const PetscScalar *xdot;
    329:   PetscInt          i;

    332:   user->t = t;

    334:   TSGetSNES(ts,&snes);
    335:   ResidualFunction(snes,X,F,user);
    336:   VecGetArray(F,&f);
    337:   VecGetArrayRead(Xdot,&xdot);
    338:   for (i=0;i < ngen;i++) {
    339:     f[9*i]   += xdot[9*i];
    340:     f[9*i+1] += xdot[9*i+1];
    341:     f[9*i+2] += xdot[9*i+2];
    342:     f[9*i+3] += xdot[9*i+3];
    343:     f[9*i+6] += xdot[9*i+6];
    344:     f[9*i+7] += xdot[9*i+7];
    345:     f[9*i+8] += xdot[9*i+8];
    346:   }
    347:   VecRestoreArray(F,&f);
    348:   VecRestoreArrayRead(Xdot,&xdot);
    349:   return(0);
    350: }

    352: /* This function is used for solving the algebraic system only during fault on and
    353:    off times. It computes the entire F and then zeros out the part corresponding to
    354:    differential equations
    355:  F = [0;g(y)];
    356: */
    357: PetscErrorCode AlgFunction(SNES snes, Vec X, Vec F, void *ctx)
    358: {
    360:   Userctx        *user=(Userctx*)ctx;
    361:   PetscScalar    *f;
    362:   PetscInt       i;

    365:   ResidualFunction(snes,X,F,user);
    366:   VecGetArray(F,&f);
    367:   for (i=0; i < ngen; i++) {
    368:     f[9*i]   = 0;
    369:     f[9*i+1] = 0;
    370:     f[9*i+2] = 0;
    371:     f[9*i+3] = 0;
    372:     f[9*i+6] = 0;
    373:     f[9*i+7] = 0;
    374:     f[9*i+8] = 0;
    375:   }
    376:   VecRestoreArray(F,&f);
    377:   return(0);
    378: }

    380: PetscErrorCode PreallocateJacobian(Mat J, Userctx *user)
    381: {
    383:   PetscInt       *d_nnz;
    384:   PetscInt       i,idx=0,start=0;
    385:   PetscInt       ncols;

    388:   PetscMalloc1(user->neqs_pgrid,&d_nnz);
    389:   for (i=0; i<user->neqs_pgrid; i++) d_nnz[i] = 0;
    390:   /* Generator subsystem */
    391:   for (i=0; i < ngen; i++) {

    393:     d_nnz[idx]   += 3;
    394:     d_nnz[idx+1] += 2;
    395:     d_nnz[idx+2] += 2;
    396:     d_nnz[idx+3] += 5;
    397:     d_nnz[idx+4] += 6;
    398:     d_nnz[idx+5] += 6;

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

    403:     d_nnz[idx+6] += 2;
    404:     d_nnz[idx+7] += 2;
    405:     d_nnz[idx+8] += 5;

    407:     idx = idx + 9;
    408:   }

    410:   start = user->neqs_gen;

    412:   for (i=0; i < nbus; i++) {
    413:     MatGetRow(user->Ybus,2*i,&ncols,NULL,NULL);
    414:     d_nnz[start+2*i]   += ncols;
    415:     d_nnz[start+2*i+1] += ncols;
    416:     MatRestoreRow(user->Ybus,2*i,&ncols,NULL,NULL);
    417:   }

    419:   MatSeqAIJSetPreallocation(J,0,d_nnz);

    421:   PetscFree(d_nnz);
    422:   return(0);
    423: }

    425: /*
    426:    J = [-df_dx, -df_dy
    427:         dg_dx, dg_dy]
    428: */
    429: PetscErrorCode ResidualJacobian(SNES snes,Vec X,Mat J,Mat B,void *ctx)
    430: {
    431:   PetscErrorCode    ierr;
    432:   Userctx           *user=(Userctx*)ctx;
    433:   Vec               Xgen,Xnet;
    434:   PetscScalar       *xgen,*xnet;
    435:   PetscInt          i,idx=0;
    436:   PetscScalar       Vr,Vi,Vm,Vm2;
    437:   PetscScalar       Eqp,Edp,delta; /* Generator variables */
    438:   PetscScalar       Efd; /* Exciter variables */
    439:   PetscScalar       Id,Iq;  /* Generator dq axis currents */
    440:   PetscScalar       Vd,Vq;
    441:   PetscScalar       val[10];
    442:   PetscInt          row[2],col[10];
    443:   PetscInt          net_start=user->neqs_gen;
    444:   PetscScalar       Zdq_inv[4],det;
    445:   PetscScalar       dVd_dVr,dVd_dVi,dVq_dVr,dVq_dVi,dVd_ddelta,dVq_ddelta;
    446:   PetscScalar       dIGr_ddelta,dIGi_ddelta,dIGr_dId,dIGr_dIq,dIGi_dId,dIGi_dIq;
    447:   PetscScalar       dSE_dEfd;
    448:   PetscScalar       dVm_dVd,dVm_dVq,dVm_dVr,dVm_dVi;
    449:   PetscInt          ncols;
    450:   const PetscInt    *cols;
    451:   const PetscScalar *yvals;
    452:   PetscInt          k;
    453:   PetscScalar       PD,QD,Vm0,*v0,Vm4;
    454:   PetscScalar       dPD_dVr,dPD_dVi,dQD_dVr,dQD_dVi;
    455:   PetscScalar       dIDr_dVr,dIDr_dVi,dIDi_dVr,dIDi_dVi;

    458:   MatZeroEntries(B);
    459:   DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);
    460:   DMCompositeScatter(user->dmpgrid,X,Xgen,Xnet);

    462:   VecGetArray(Xgen,&xgen);
    463:   VecGetArray(Xnet,&xnet);

    465:   /* Generator subsystem */
    466:   for (i=0; i < ngen; i++) {
    467:     Eqp   = xgen[idx];
    468:     Edp   = xgen[idx+1];
    469:     delta = xgen[idx+2];
    470:     Id    = xgen[idx+4];
    471:     Iq    = xgen[idx+5];
    472:     Efd   = xgen[idx+6];

    474:     /*    fgen[idx]   = (Eqp + (Xd[i] - Xdp[i])*Id - Efd)/Td0p[i]; */
    475:     row[0] = idx;
    476:     col[0] = idx;           col[1] = idx+4;          col[2] = idx+6;
    477:     val[0] = 1/ Td0p[i]; val[1] = (Xd[i] - Xdp[i])/ Td0p[i]; val[2] = -1/Td0p[i];

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

    481:     /*    fgen[idx+1] = (Edp - (Xq[i] - Xqp[i])*Iq)/Tq0p[i]; */
    482:     row[0] = idx + 1;
    483:     col[0] = idx + 1;       col[1] = idx+5;
    484:     val[0] = 1/Tq0p[i]; val[1] = -(Xq[i] - Xqp[i])/Tq0p[i];
    485:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

    487:     /*    fgen[idx+2] = - w + w_s; */
    488:     row[0] = idx + 2;
    489:     col[0] = idx + 2; col[1] = idx + 3;
    490:     val[0] = 0;       val[1] = -1;
    491:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

    493:     /*    fgen[idx+3] = (-TM[i] + Edp*Id + Eqp*Iq + (Xqp[i] - Xdp[i])*Id*Iq + D[i]*(w - w_s))/M[i]; */
    494:     row[0] = idx + 3;
    495:     col[0] = idx; col[1] = idx + 1; col[2] = idx + 3;       col[3] = idx + 4;                  col[4] = idx + 5;
    496:     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];
    497:     MatSetValues(J,1,row,5,col,val,INSERT_VALUES);

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

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

    505:     Zdq_inv[0] = Rs[i]/det;
    506:     Zdq_inv[1] = Xqp[i]/det;
    507:     Zdq_inv[2] = -Xdp[i]/det;
    508:     Zdq_inv[3] = Rs[i]/det;

    510:     dVd_dVr    = PetscSinScalar(delta); dVd_dVi = -PetscCosScalar(delta);
    511:     dVq_dVr    = PetscCosScalar(delta); dVq_dVi = PetscSinScalar(delta);
    512:     dVd_ddelta = Vr*PetscCosScalar(delta) + Vi*PetscSinScalar(delta);
    513:     dVq_ddelta = -Vr*PetscSinScalar(delta) + Vi*PetscCosScalar(delta);

    515:     /*    fgen[idx+4] = Zdq_inv[0]*(-Edp + Vd) + Zdq_inv[1]*(-Eqp + Vq) + Id; */
    516:     row[0] = idx+4;
    517:     col[0] = idx;         col[1] = idx+1;        col[2] = idx + 2;
    518:     val[0] = -Zdq_inv[1]; val[1] = -Zdq_inv[0];  val[2] = Zdq_inv[0]*dVd_ddelta + Zdq_inv[1]*dVq_ddelta;
    519:     col[3] = idx + 4; col[4] = net_start+2*gbus[i];                     col[5] = net_start + 2*gbus[i]+1;
    520:     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;
    521:     MatSetValues(J,1,row,6,col,val,INSERT_VALUES);

    523:     /*  fgen[idx+5] = Zdq_inv[2]*(-Edp + Vd) + Zdq_inv[3]*(-Eqp + Vq) + Iq; */
    524:     row[0] = idx+5;
    525:     col[0] = idx;         col[1] = idx+1;        col[2] = idx + 2;
    526:     val[0] = -Zdq_inv[3]; val[1] = -Zdq_inv[2];  val[2] = Zdq_inv[2]*dVd_ddelta + Zdq_inv[3]*dVq_ddelta;
    527:     col[3] = idx + 5; col[4] = net_start+2*gbus[i];                     col[5] = net_start + 2*gbus[i]+1;
    528:     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;
    529:     MatSetValues(J,1,row,6,col,val,INSERT_VALUES);

    531:     dIGr_ddelta = Id*PetscCosScalar(delta) - Iq*PetscSinScalar(delta);
    532:     dIGi_ddelta = Id*PetscSinScalar(delta) + Iq*PetscCosScalar(delta);
    533:     dIGr_dId    = PetscSinScalar(delta);  dIGr_dIq = PetscCosScalar(delta);
    534:     dIGi_dId    = -PetscCosScalar(delta); dIGi_dIq = PetscSinScalar(delta);

    536:     /* fnet[2*gbus[i]]   -= IGi; */
    537:     row[0] = net_start + 2*gbus[i];
    538:     col[0] = idx+2;        col[1] = idx + 4;   col[2] = idx + 5;
    539:     val[0] = -dIGi_ddelta; val[1] = -dIGi_dId; val[2] = -dIGi_dIq;
    540:     MatSetValues(J,1,row,3,col,val,INSERT_VALUES);

    542:     /* fnet[2*gbus[i]+1]   -= IGr; */
    543:     row[0] = net_start + 2*gbus[i]+1;
    544:     col[0] = idx+2;        col[1] = idx + 4;   col[2] = idx + 5;
    545:     val[0] = -dIGr_ddelta; val[1] = -dIGr_dId; val[2] = -dIGr_dIq;
    546:     MatSetValues(J,1,row,3,col,val,INSERT_VALUES);

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

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

    554:     row[0] = idx + 6;
    555:     col[0] = idx + 6;                     col[1] = idx + 8;
    556:     val[0] = (KE[i] + dSE_dEfd)/TE[i];  val[1] = -1/TE[i];
    557:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

    559:     /* Exciter differential equations */

    561:     /*    fgen[idx+7] = (RF - KF[i]*Efd/TF[i])/TF[i]; */
    562:     row[0] = idx + 7;
    563:     col[0] = idx + 6;       col[1] = idx + 7;
    564:     val[0] = (-KF[i]/TF[i])/TF[i];  val[1] = 1/TF[i];
    565:     MatSetValues(J,1,row,2,col,val,INSERT_VALUES);

    567:     /*    fgen[idx+8] = (VR - KA[i]*RF + KA[i]*KF[i]*Efd/TF[i] - KA[i]*(Vref[i] - Vm))/TA[i]; */
    568:     /* Vm = (Vd^2 + Vq^2)^0.5; */
    569:     dVm_dVd    = Vd/Vm; dVm_dVq = Vq/Vm;
    570:     dVm_dVr    = dVm_dVd*dVd_dVr + dVm_dVq*dVq_dVr;
    571:     dVm_dVi    = dVm_dVd*dVd_dVi + dVm_dVq*dVq_dVi;
    572:     row[0]     = idx + 8;
    573:     col[0]     = idx + 6;           col[1] = idx + 7; col[2] = idx + 8;
    574:     val[0]     = (KA[i]*KF[i]/TF[i])/TA[i]; val[1] = -KA[i]/TA[i];  val[2] = 1/TA[i];
    575:     col[3]     = net_start + 2*gbus[i]; col[4] = net_start + 2*gbus[i]+1;
    576:     val[3]     = KA[i]*dVm_dVr/TA[i];         val[4] = KA[i]*dVm_dVi/TA[i];
    577:     MatSetValues(J,1,row,5,col,val,INSERT_VALUES);
    578:     idx        = idx + 9;
    579:   }

    581:   for (i=0; i<nbus; i++) {
    582:     MatGetRow(user->Ybus,2*i,&ncols,&cols,&yvals);
    583:     row[0] = net_start + 2*i;
    584:     for (k=0; k<ncols; k++) {
    585:       col[k] = net_start + cols[k];
    586:       val[k] = yvals[k];
    587:     }
    588:     MatSetValues(J,1,row,ncols,col,val,INSERT_VALUES);
    589:     MatRestoreRow(user->Ybus,2*i,&ncols,&cols,&yvals);

    591:     MatGetRow(user->Ybus,2*i+1,&ncols,&cols,&yvals);
    592:     row[0] = net_start + 2*i+1;
    593:     for (k=0; k<ncols; k++) {
    594:       col[k] = net_start + cols[k];
    595:       val[k] = yvals[k];
    596:     }
    597:     MatSetValues(J,1,row,ncols,col,val,INSERT_VALUES);
    598:     MatRestoreRow(user->Ybus,2*i+1,&ncols,&cols,&yvals);
    599:   }

    601:   MatAssemblyBegin(J,MAT_FLUSH_ASSEMBLY);
    602:   MatAssemblyEnd(J,MAT_FLUSH_ASSEMBLY);

    604:   VecGetArray(user->V0,&v0);
    605:   for (i=0; i < nload; i++) {
    606:     Vr      = xnet[2*lbus[i]]; /* Real part of load bus voltage */
    607:     Vi      = xnet[2*lbus[i]+1]; /* Imaginary part of the load bus voltage */
    608:     Vm      = PetscSqrtScalar(Vr*Vr + Vi*Vi); Vm2 = Vm*Vm; Vm4 = Vm2*Vm2;
    609:     Vm0     = PetscSqrtScalar(v0[2*lbus[i]]*v0[2*lbus[i]] + v0[2*lbus[i]+1]*v0[2*lbus[i]+1]);
    610:     PD      = QD = 0.0;
    611:     dPD_dVr = dPD_dVi = dQD_dVr = dQD_dVi = 0.0;
    612:     for (k=0; k < ld_nsegsp[i]; k++) {
    613:       PD      += ld_alphap[k]*PD0[i]*PetscPowScalar((Vm/Vm0),ld_betap[k]);
    614:       dPD_dVr += ld_alphap[k]*ld_betap[k]*PD0[i]*PetscPowScalar((1/Vm0),ld_betap[k])*Vr*PetscPowScalar(Vm,(ld_betap[k]-2));
    615:       dPD_dVi += ld_alphap[k]*ld_betap[k]*PD0[i]*PetscPowScalar((1/Vm0),ld_betap[k])*Vi*PetscPowScalar(Vm,(ld_betap[k]-2));
    616:     }
    617:     for (k=0; k < ld_nsegsq[i]; k++) {
    618:       QD      += ld_alphaq[k]*QD0[i]*PetscPowScalar((Vm/Vm0),ld_betaq[k]);
    619:       dQD_dVr += ld_alphaq[k]*ld_betaq[k]*QD0[i]*PetscPowScalar((1/Vm0),ld_betaq[k])*Vr*PetscPowScalar(Vm,(ld_betaq[k]-2));
    620:       dQD_dVi += ld_alphaq[k]*ld_betaq[k]*QD0[i]*PetscPowScalar((1/Vm0),ld_betaq[k])*Vi*PetscPowScalar(Vm,(ld_betaq[k]-2));
    621:     }

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

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

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

    632:     /*    fnet[2*lbus[i]]   += IDi; */
    633:     row[0] = net_start + 2*lbus[i];
    634:     col[0] = net_start + 2*lbus[i];  col[1] = net_start + 2*lbus[i]+1;
    635:     val[0] = dIDi_dVr;               val[1] = dIDi_dVi;
    636:     MatSetValues(J,1,row,2,col,val,ADD_VALUES);
    637:     /*    fnet[2*lbus[i]+1] += IDr; */
    638:     row[0] = net_start + 2*lbus[i]+1;
    639:     col[0] = net_start + 2*lbus[i];  col[1] = net_start + 2*lbus[i]+1;
    640:     val[0] = dIDr_dVr;               val[1] = dIDr_dVi;
    641:     MatSetValues(J,1,row,2,col,val,ADD_VALUES);
    642:   }
    643:   VecRestoreArray(user->V0,&v0);

    645:   VecRestoreArray(Xgen,&xgen);
    646:   VecRestoreArray(Xnet,&xnet);

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

    650:   MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
    651:   MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
    652:   return(0);
    653: }

    655: /*
    656:    J = [I, 0
    657:         dg_dx, dg_dy]
    658: */
    659: PetscErrorCode AlgJacobian(SNES snes,Vec X,Mat A,Mat B,void *ctx)
    660: {
    662:   Userctx        *user=(Userctx*)ctx;

    665:   ResidualJacobian(snes,X,A,B,ctx);
    666:   MatSetOption(A,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE);
    667:   MatZeroRowsIS(A,user->is_diff,1.0,NULL,NULL);
    668:   return(0);
    669: }

    671: /*
    672:    J = [a*I-df_dx, -df_dy
    673:         dg_dx, dg_dy]
    674: */

    676: PetscErrorCode IJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat A,Mat B,Userctx *user)
    677: {
    679:   SNES           snes;
    680:   PetscScalar    atmp = (PetscScalar) a;
    681:   PetscInt       i,row;

    684:   user->t = t;

    686:   TSGetSNES(ts,&snes);
    687:   ResidualJacobian(snes,X,A,B,user);
    688:   for (i=0;i < ngen;i++) {
    689:     row = 9*i;
    690:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    691:     row  = 9*i+1;
    692:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    693:     row  = 9*i+2;
    694:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    695:     row  = 9*i+3;
    696:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    697:     row  = 9*i+6;
    698:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    699:     row  = 9*i+7;
    700:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    701:     row  = 9*i+8;
    702:     MatSetValues(A,1,&row,1,&row,&atmp,ADD_VALUES);
    703:   }
    704:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
    705:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
    706:   return(0);
    707: }

    709: int main(int argc,char **argv)
    710: {
    711:   TS             ts;
    712:   SNES           snes_alg;
    714:   PetscMPIInt    size;
    715:   Userctx        user;
    716:   PetscViewer    Xview,Ybusview;
    717:   Vec            X;
    718:   Mat            J;
    719:   PetscInt       i;
    720:   /* sensitivity context */
    721:   PetscScalar    *y_ptr;
    722:   Vec            lambda[1];
    723:   PetscInt       *idx2;
    724:   Vec            Xdot;
    725:   Vec            F_alg;
    726:   PetscInt       row_loc,col_loc;
    727:   PetscScalar    val;

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

    733:   user.neqs_gen   = 9*ngen; /* # eqs. for generator subsystem */
    734:   user.neqs_net   = 2*nbus; /* # eqs. for network subsystem   */
    735:   user.neqs_pgrid = user.neqs_gen + user.neqs_net;

    737:   /* Create indices for differential and algebraic equations */
    738:   PetscMalloc1(7*ngen,&idx2);
    739:   for (i=0; i<ngen; i++) {
    740:     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;
    741:     idx2[7*i+4] = 9*i+6; idx2[7*i+5] = 9*i+7; idx2[7*i+6] = 9*i+8;
    742:   }
    743:   ISCreateGeneral(PETSC_COMM_WORLD,7*ngen,idx2,PETSC_COPY_VALUES,&user.is_diff);
    744:   ISComplement(user.is_diff,0,user.neqs_pgrid,&user.is_alg);
    745:   PetscFree(idx2);

    747:   /* Read initial voltage vector and Ybus */
    748:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"X.bin",FILE_MODE_READ,&Xview);
    749:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"Ybus.bin",FILE_MODE_READ,&Ybusview);

    751:   VecCreate(PETSC_COMM_WORLD,&user.V0);
    752:   VecSetSizes(user.V0,PETSC_DECIDE,user.neqs_net);
    753:   VecLoad(user.V0,Xview);

    755:   MatCreate(PETSC_COMM_WORLD,&user.Ybus);
    756:   MatSetSizes(user.Ybus,PETSC_DECIDE,PETSC_DECIDE,user.neqs_net,user.neqs_net);
    757:   MatSetType(user.Ybus,MATBAIJ);
    758:   /*  MatSetBlockSize(user.Ybus,2); */
    759:   MatLoad(user.Ybus,Ybusview);

    761:   /* Set run time options */
    762:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Transient stability fault options","");
    763:   {
    764:     user.tfaulton  = 1.0;
    765:     user.tfaultoff = 1.2;
    766:     user.Rfault    = 0.0001;
    767:     user.faultbus  = 8;
    768:     PetscOptionsReal("-tfaulton","","",user.tfaulton,&user.tfaulton,NULL);
    769:     PetscOptionsReal("-tfaultoff","","",user.tfaultoff,&user.tfaultoff,NULL);
    770:     PetscOptionsInt("-faultbus","","",user.faultbus,&user.faultbus,NULL);
    771:     user.t0        = 0.0;
    772:     user.tmax      = 5.0;
    773:     PetscOptionsReal("-t0","","",user.t0,&user.t0,NULL);
    774:     PetscOptionsReal("-tmax","","",user.tmax,&user.tmax,NULL);
    775:   }
    776:   PetscOptionsEnd();

    778:   PetscViewerDestroy(&Xview);
    779:   PetscViewerDestroy(&Ybusview);

    781:   /* Create DMs for generator and network subsystems */
    782:   DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,user.neqs_gen,1,1,NULL,&user.dmgen);
    783:   DMSetOptionsPrefix(user.dmgen,"dmgen_");
    784:   DMSetFromOptions(user.dmgen);
    785:   DMSetUp(user.dmgen);
    786:   DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,user.neqs_net,1,1,NULL,&user.dmnet);
    787:   DMSetOptionsPrefix(user.dmnet,"dmnet_");
    788:   DMSetFromOptions(user.dmnet);
    789:   DMSetUp(user.dmnet);
    790:   /* Create a composite DM packer and add the two DMs */
    791:   DMCompositeCreate(PETSC_COMM_WORLD,&user.dmpgrid);
    792:   DMSetOptionsPrefix(user.dmpgrid,"pgrid_");
    793:   DMCompositeAddDM(user.dmpgrid,user.dmgen);
    794:   DMCompositeAddDM(user.dmpgrid,user.dmnet);

    796:   DMCreateGlobalVector(user.dmpgrid,&X);

    798:   MatCreate(PETSC_COMM_WORLD,&J);
    799:   MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,user.neqs_pgrid,user.neqs_pgrid);
    800:   MatSetFromOptions(J);
    801:   PreallocateJacobian(J,&user);

    803:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    804:      Create timestepping solver context
    805:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    806:   TSCreate(PETSC_COMM_WORLD,&ts);
    807:   TSSetProblemType(ts,TS_NONLINEAR);
    808:   TSSetType(ts,TSCN);
    809:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&user);
    810:   TSSetIJacobian(ts,J,J,(TSIJacobian)IJacobian,&user);
    811:   TSSetApplicationContext(ts,&user);

    813:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    814:      Set initial conditions
    815:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    816:   SetInitialGuess(X,&user);
    817:   /* Just to set up the Jacobian structure */
    818:   VecDuplicate(X,&Xdot);
    819:   IJacobian(ts,0.0,X,Xdot,0.0,J,J,&user);
    820:   VecDestroy(&Xdot);

    822:   /*
    823:     Save trajectory of solution so that TSAdjointSolve() may be used
    824:   */
    825:   TSSetSaveTrajectory(ts);

    827:   TSSetMaxTime(ts,user.tmax);
    828:   TSSetTimeStep(ts,0.01);
    829:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);
    830:   TSSetFromOptions(ts);

    832:   user.alg_flg = PETSC_FALSE;
    833:   /* Prefault period */
    834:   TSSolve(ts,X);

    836:   /* Create the nonlinear solver for solving the algebraic system */
    837:   /* Note that although the algebraic system needs to be solved only for
    838:      Idq and V, we reuse the entire system including xgen. The xgen
    839:      variables are held constant by setting their residuals to 0 and
    840:      putting a 1 on the Jacobian diagonal for xgen rows
    841:   */
    842:   VecDuplicate(X,&F_alg);
    843:   SNESCreate(PETSC_COMM_WORLD,&snes_alg);
    844:   SNESSetFunction(snes_alg,F_alg,AlgFunction,&user);
    845:   MatZeroEntries(J);
    846:   SNESSetJacobian(snes_alg,J,J,AlgJacobian,&user);
    847:   SNESSetOptionsPrefix(snes_alg,"alg_");
    848:   SNESSetFromOptions(snes_alg);

    850:   /* Apply disturbance - resistive fault at user.faultbus */
    851:   /* This is done by adding shunt conductance to the diagonal location
    852:      in the Ybus matrix */
    853:   row_loc = 2*user.faultbus; col_loc = 2*user.faultbus+1; /* Location for G */
    854:   val     = 1/user.Rfault;
    855:   MatSetValues(user.Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
    856:   row_loc = 2*user.faultbus+1; col_loc = 2*user.faultbus; /* Location for G */
    857:   val     = 1/user.Rfault;
    858:   MatSetValues(user.Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

    860:   MatAssemblyBegin(user.Ybus,MAT_FINAL_ASSEMBLY);
    861:   MatAssemblyEnd(user.Ybus,MAT_FINAL_ASSEMBLY);

    863:   user.alg_flg = PETSC_TRUE;
    864:   /* Solve the algebraic equations */
    865:   SNESSolve(snes_alg,NULL,X);

    867:   /* Disturbance period */
    868:   user.alg_flg = PETSC_FALSE;
    869:   TSSetTime(ts,user.tfaulton);
    870:   TSSetMaxTime(ts,user.tfaultoff);
    871:   TSSolve(ts,X);

    873:   /* Remove the fault */
    874:   row_loc = 2*user.faultbus; col_loc = 2*user.faultbus+1;
    875:   val     = -1/user.Rfault;
    876:   MatSetValues(user.Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);
    877:   row_loc = 2*user.faultbus+1; col_loc = 2*user.faultbus;
    878:   val     = -1/user.Rfault;
    879:   MatSetValues(user.Ybus,1,&row_loc,1,&col_loc,&val,ADD_VALUES);

    881:   MatAssemblyBegin(user.Ybus,MAT_FINAL_ASSEMBLY);
    882:   MatAssemblyEnd(user.Ybus,MAT_FINAL_ASSEMBLY);

    884:   MatZeroEntries(J);

    886:   user.alg_flg = PETSC_TRUE;

    888:   /* Solve the algebraic equations */
    889:   SNESSolve(snes_alg,NULL,X);

    891:   /* Post-disturbance period */
    892:   user.alg_flg = PETSC_TRUE;
    893:   TSSetTime(ts,user.tfaultoff);
    894:   TSSetMaxTime(ts,user.tmax);
    895:   TSSolve(ts,X);

    897:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    898:      Adjoint model starts here
    899:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    900:   TSSetPostStep(ts,NULL);
    901:   MatCreateVecs(J,&lambda[0],NULL);
    902:   /*   Set initial conditions for the adjoint integration */
    903:   VecZeroEntries(lambda[0]);
    904:   VecGetArray(lambda[0],&y_ptr);
    905:   y_ptr[0] = 1.0;
    906:   VecRestoreArray(lambda[0],&y_ptr);
    907:   TSSetCostGradients(ts,1,lambda,NULL);

    909:   TSAdjointSolve(ts);

    911:   PetscPrintf(PETSC_COMM_WORLD,"\n sensitivity wrt initial conditions: \n");
    912:   VecView(lambda[0],PETSC_VIEWER_STDOUT_WORLD);
    913:   VecDestroy(&lambda[0]);

    915:   SNESDestroy(&snes_alg);
    916:   VecDestroy(&F_alg);
    917:   MatDestroy(&J);
    918:   MatDestroy(&user.Ybus);
    919:   VecDestroy(&X);
    920:   VecDestroy(&user.V0);
    921:   DMDestroy(&user.dmgen);
    922:   DMDestroy(&user.dmnet);
    923:   DMDestroy(&user.dmpgrid);
    924:   ISDestroy(&user.is_diff);
    925:   ISDestroy(&user.is_alg);
    926:   TSDestroy(&ts);
    927:   PetscFinalize();
    928:   return ierr;
    929: }

    931: /*TEST

    933:    build:
    934:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)

    936:    test:
    937:       args: -viewer_binary_skip_info
    938:       localrunfiles: petscoptions X.bin Ybus.bin

    940:    test:
    941:       suffix: 2
    942:       args: -viewer_binary_skip_info -ts_type beuler
    943:       localrunfiles: petscoptions X.bin Ybus.bin

    945: TEST*/