Actual source code: power2.c

      1: static char help[] = "This example demonstrates the use of DMNetwork interface with subnetworks for solving a nonlinear electric power grid problem.\n\
      2:                       The available solver options are in the poweroptions file and the data files are in the datafiles directory.\n\
      3:                       The data file format used is from the MatPower package (http://www.pserc.cornell.edu//matpower/).\n\
      4:                       This example shows the use of subnetwork feature in DMNetwork. It creates duplicates of the same network which are treated as subnetworks.\n\
      5:                       Run this program: mpiexec -n <n> ./pf2\n\
      6:                       mpiexec -n <n> ./pf2 \n";

      8: /* T
      9:    Concepts: DMNetwork
     10:    Concepts: PETSc SNES solver
     11: */

     13: #include "power.h"
     14: #include <petscdmnetwork.h>

     16: PetscErrorCode FormFunction_Subnet(DM networkdm,Vec localX, Vec localF,PetscInt nv,PetscInt ne,const PetscInt* vtx,const PetscInt* edges,void* appctx)
     17: {
     18:   PetscErrorCode    ierr;
     19:   UserCtx_Power     *User = (UserCtx_Power*)appctx;
     20:   PetscInt          e,v,vfrom,vto;
     21:   const PetscScalar *xarr;
     22:   PetscScalar       *farr;
     23:   PetscInt          offsetfrom,offsetto,offset;

     26:   VecGetArrayRead(localX,&xarr);
     27:   VecGetArray(localF,&farr);

     29:   for (v=0; v<nv; v++) {
     30:     PetscInt      i,j,key;
     31:     PetscScalar   Vm;
     32:     PetscScalar   Sbase = User->Sbase;
     33:     VERTEX_Power  bus = NULL;
     34:     GEN           gen;
     35:     LOAD          load;
     36:     PetscBool     ghostvtex;
     37:     PetscInt      numComps;
     38:     void*         component;

     40:     DMNetworkIsGhostVertex(networkdm,vtx[v],&ghostvtex);
     41:     DMNetworkGetNumComponents(networkdm,vtx[v],&numComps);
     42:     DMNetworkGetLocalVecOffset(networkdm,vtx[v],ALL_COMPONENTS,&offset);
     43:     for (j = 0; j < numComps; j++) {
     44:       DMNetworkGetComponent(networkdm,vtx[v],j,&key,&component,NULL);
     45:       if (key == 1) {
     46:         PetscInt       nconnedges;
     47:         const PetscInt *connedges;

     49:         bus = (VERTEX_Power)(component);
     50:         /* Handle reference bus constrained dofs */
     51:         if (bus->ide == REF_BUS || bus->ide == ISOLATED_BUS) {
     52:           farr[offset] = xarr[offset] - bus->va*PETSC_PI/180.0;
     53:           farr[offset+1] = xarr[offset+1] - bus->vm;
     54:           break;
     55:         }

     57:         if (!ghostvtex) {
     58:           Vm = xarr[offset+1];

     60:           /* Shunt injections */
     61:           farr[offset] += Vm*Vm*bus->gl/Sbase;
     62:           if (bus->ide != PV_BUS) farr[offset+1] += -Vm*Vm*bus->bl/Sbase;
     63:         }

     65:         DMNetworkGetSupportingEdges(networkdm,vtx[v],&nconnedges,&connedges);
     66:         for (i=0; i < nconnedges; i++) {
     67:           EDGE_Power     branch;
     68:           PetscInt       keye;
     69:           PetscScalar    Gff,Bff,Gft,Bft,Gtf,Btf,Gtt,Btt;
     70:           const PetscInt *cone;
     71:           PetscScalar    Vmf,Vmt,thetaf,thetat,thetaft,thetatf;

     73:           e = connedges[i];
     74:           DMNetworkGetComponent(networkdm,e,0,&keye,(void**)&branch,NULL);
     75:           if (!branch->status) continue;
     76:           Gff = branch->yff[0];
     77:           Bff = branch->yff[1];
     78:           Gft = branch->yft[0];
     79:           Bft = branch->yft[1];
     80:           Gtf = branch->ytf[0];
     81:           Btf = branch->ytf[1];
     82:           Gtt = branch->ytt[0];
     83:           Btt = branch->ytt[1];

     85:           DMNetworkGetConnectedVertices(networkdm,e,&cone);
     86:           vfrom = cone[0];
     87:           vto   = cone[1];

     89:           DMNetworkGetLocalVecOffset(networkdm,vfrom,ALL_COMPONENTS,&offsetfrom);
     90:           DMNetworkGetLocalVecOffset(networkdm,vto,ALL_COMPONENTS,&offsetto);

     92:           thetaf = xarr[offsetfrom];
     93:           Vmf     = xarr[offsetfrom+1];
     94:           thetat  = xarr[offsetto];
     95:           Vmt     = xarr[offsetto+1];
     96:           thetaft = thetaf - thetat;
     97:           thetatf = thetat - thetaf;

     99:           if (vfrom == vtx[v]) {
    100:             farr[offsetfrom]   += Gff*Vmf*Vmf + Vmf*Vmt*(Gft*PetscCosScalar(thetaft) + Bft*PetscSinScalar(thetaft));
    101:             farr[offsetfrom+1] += -Bff*Vmf*Vmf + Vmf*Vmt*(-Bft*PetscCosScalar(thetaft) + Gft*PetscSinScalar(thetaft));
    102:           } else {
    103:             farr[offsetto]   += Gtt*Vmt*Vmt + Vmt*Vmf*(Gtf*PetscCosScalar(thetatf) + Btf*PetscSinScalar(thetatf));
    104:             farr[offsetto+1] += -Btt*Vmt*Vmt + Vmt*Vmf*(-Btf*PetscCosScalar(thetatf) + Gtf*PetscSinScalar(thetatf));
    105:           }
    106:         }
    107:       } else if (key == 2) {
    108:         if (!ghostvtex) {
    109:           gen = (GEN)(component);
    110:           if (!gen->status) continue;
    111:           farr[offset] += -gen->pg/Sbase;
    112:           farr[offset+1] += -gen->qg/Sbase;
    113:         }
    114:       } else if (key == 3) {
    115:         if (!ghostvtex) {
    116:           load = (LOAD)(component);
    117:           farr[offset] += load->pl/Sbase;
    118:           farr[offset+1] += load->ql/Sbase;
    119:         }
    120:       }
    121:     }
    122:     if (bus && bus->ide == PV_BUS) {
    123:       farr[offset+1] = xarr[offset+1] - bus->vm;
    124:     }
    125:   }
    126:   VecRestoreArrayRead(localX,&xarr);
    127:   VecRestoreArray(localF,&farr);
    128:   return(0);
    129: }

    132: PetscErrorCode FormFunction(SNES snes,Vec X, Vec F,void *appctx)
    133: {
    135:   DM             networkdm;
    136:   Vec            localX,localF;
    137:   PetscInt       nv,ne;
    138:   const PetscInt *vtx,*edges;

    141:   SNESGetDM(snes,&networkdm);
    142:   DMGetLocalVector(networkdm,&localX);
    143:   DMGetLocalVector(networkdm,&localF);
    144:   VecSet(F,0.0);

    146:   DMGlobalToLocalBegin(networkdm,X,INSERT_VALUES,localX);
    147:   DMGlobalToLocalEnd(networkdm,X,INSERT_VALUES,localX);

    149:   DMGlobalToLocalBegin(networkdm,F,INSERT_VALUES,localF);
    150:   DMGlobalToLocalEnd(networkdm,F,INSERT_VALUES,localF);

    152:   /* Form Function for first subnetwork */
    153:   DMNetworkGetSubnetwork(networkdm,0,&nv,&ne,&vtx,&edges);
    154:   FormFunction_Subnet(networkdm,localX,localF,nv,ne,vtx,edges,appctx);

    156:   /* Form Function for second subnetwork */
    157:   DMNetworkGetSubnetwork(networkdm,1,&nv,&ne,&vtx,&edges);
    158:   FormFunction_Subnet(networkdm,localX,localF,nv,ne,vtx,edges,appctx);

    160:   DMRestoreLocalVector(networkdm,&localX);

    162:   DMLocalToGlobalBegin(networkdm,localF,ADD_VALUES,F);
    163:   DMLocalToGlobalEnd(networkdm,localF,ADD_VALUES,F);
    164:   DMRestoreLocalVector(networkdm,&localF);
    165:   return(0);
    166: }

    168: PetscErrorCode FormJacobian_Subnet(DM networkdm,Vec localX, Mat J, Mat Jpre, PetscInt nv, PetscInt ne, const PetscInt *vtx, const PetscInt *edges, void *appctx)
    169: {
    170:   PetscErrorCode    ierr;
    171:   UserCtx_Power     *User=(UserCtx_Power*)appctx;
    172:   PetscInt          e,v,vfrom,vto;
    173:   const PetscScalar *xarr;
    174:   PetscInt          offsetfrom,offsetto,goffsetfrom,goffsetto;
    175:   PetscInt          row[2],col[8];
    176:   PetscScalar       values[8];

    179:   VecGetArrayRead(localX,&xarr);

    181:   for (v=0; v<nv; v++) {
    182:     PetscInt    i,j,key;
    183:     PetscInt    offset,goffset;
    184:     PetscScalar Vm;
    185:     PetscScalar Sbase=User->Sbase;
    186:     VERTEX_Power bus;
    187:     PetscBool   ghostvtex;
    188:     PetscInt    numComps;
    189:     void*       component;

    191:     DMNetworkIsGhostVertex(networkdm,vtx[v],&ghostvtex);
    192:     DMNetworkGetNumComponents(networkdm,vtx[v],&numComps);
    193:     for (j = 0; j < numComps; j++) {
    194:       DMNetworkGetLocalVecOffset(networkdm,vtx[v],ALL_COMPONENTS,&offset);
    195:       DMNetworkGetGlobalVecOffset(networkdm,vtx[v],ALL_COMPONENTS,&goffset);
    196:       DMNetworkGetComponent(networkdm,vtx[v],j,&key,&component,NULL);
    197:       if (key == 1) {
    198:         PetscInt       nconnedges;
    199:         const PetscInt *connedges;

    201:         bus = (VERTEX_Power)(component);
    202:         if (!ghostvtex) {
    203:           /* Handle reference bus constrained dofs */
    204:           if (bus->ide == REF_BUS || bus->ide == ISOLATED_BUS) {
    205:             row[0] = goffset; row[1] = goffset+1;
    206:             col[0] = goffset; col[1] = goffset+1; col[2] = goffset; col[3] = goffset+1;
    207:             values[0] = 1.0; values[1] = 0.0; values[2] = 0.0; values[3] = 1.0;
    208:             MatSetValues(J,2,row,2,col,values,ADD_VALUES);
    209:             break;
    210:           }

    212:           Vm = xarr[offset+1];

    214:           /* Shunt injections */
    215:           row[0] = goffset; row[1] = goffset+1;
    216:           col[0] = goffset; col[1] = goffset+1;
    217:           values[0] = values[1] = values[2] = values[3] = 0.0;
    218:           if (bus->ide != PV_BUS) {
    219:             values[1] = 2.0*Vm*bus->gl/Sbase;
    220:             values[3] = -2.0*Vm*bus->bl/Sbase;
    221:           }
    222:           MatSetValues(J,2,row,2,col,values,ADD_VALUES);
    223:         }

    225:         DMNetworkGetSupportingEdges(networkdm,vtx[v],&nconnedges,&connedges);
    226:         for (i=0; i < nconnedges; i++) {
    227:           EDGE_Power       branch;
    228:           VERTEX_Power     busf,bust;
    229:           PetscInt       keyf,keyt;
    230:           PetscScalar    Gff,Bff,Gft,Bft,Gtf,Btf,Gtt,Btt;
    231:           const PetscInt *cone;
    232:           PetscScalar    Vmf,Vmt,thetaf,thetat,thetaft,thetatf;

    234:           e = connedges[i];
    235:           DMNetworkGetComponent(networkdm,e,0,&key,(void**)&branch,NULL);
    236:           if (!branch->status) continue;

    238:           Gff = branch->yff[0];
    239:           Bff = branch->yff[1];
    240:           Gft = branch->yft[0];
    241:           Bft = branch->yft[1];
    242:           Gtf = branch->ytf[0];
    243:           Btf = branch->ytf[1];
    244:           Gtt = branch->ytt[0];
    245:           Btt = branch->ytt[1];

    247:           DMNetworkGetConnectedVertices(networkdm,e,&cone);
    248:           vfrom = cone[0];
    249:           vto   = cone[1];

    251:           DMNetworkGetLocalVecOffset(networkdm,vfrom,ALL_COMPONENTS,&offsetfrom);
    252:           DMNetworkGetLocalVecOffset(networkdm,vto,ALL_COMPONENTS,&offsetto);
    253:           DMNetworkGetGlobalVecOffset(networkdm,vfrom,ALL_COMPONENTS,&goffsetfrom);
    254:           DMNetworkGetGlobalVecOffset(networkdm,vto,ALL_COMPONENTS,&goffsetto);

    256:           if (goffsetto < 0) goffsetto = -goffsetto - 1;

    258:           thetaf = xarr[offsetfrom];
    259:           Vmf     = xarr[offsetfrom+1];
    260:           thetat = xarr[offsetto];
    261:           Vmt     = xarr[offsetto+1];
    262:           thetaft = thetaf - thetat;
    263:           thetatf = thetat - thetaf;

    265:           DMNetworkGetComponent(networkdm,vfrom,0,&keyf,(void**)&busf,NULL);
    266:           DMNetworkGetComponent(networkdm,vto,0,&keyt,(void**)&bust,NULL);

    268:           if (vfrom == vtx[v]) {
    269:             if (busf->ide != REF_BUS) {
    270:               /*    farr[offsetfrom]   += Gff*Vmf*Vmf + Vmf*Vmt*(Gft*PetscCosScalar(thetaft) + Bft*PetscSinScalar(thetaft));  */
    271:               row[0]  = goffsetfrom;
    272:               col[0]  = goffsetfrom; col[1] = goffsetfrom+1; col[2] = goffsetto; col[3] = goffsetto+1;
    273:               values[0] =  Vmf*Vmt*(Gft*-PetscSinScalar(thetaft) + Bft*PetscCosScalar(thetaft)); /* df_dthetaf */
    274:               values[1] =  2.0*Gff*Vmf + Vmt*(Gft*PetscCosScalar(thetaft) + Bft*PetscSinScalar(thetaft)); /* df_dVmf */
    275:               values[2] =  Vmf*Vmt*(Gft*PetscSinScalar(thetaft) + Bft*-PetscCosScalar(thetaft)); /* df_dthetat */
    276:               values[3] =  Vmf*(Gft*PetscCosScalar(thetaft) + Bft*PetscSinScalar(thetaft)); /* df_dVmt */

    278:               MatSetValues(J,1,row,4,col,values,ADD_VALUES);
    279:             }
    280:             if (busf->ide != PV_BUS && busf->ide != REF_BUS) {
    281:               row[0] = goffsetfrom+1;
    282:               col[0]  = goffsetfrom; col[1] = goffsetfrom+1; col[2] = goffsetto; col[3] = goffsetto+1;
    283:               /*    farr[offsetfrom+1] += -Bff*Vmf*Vmf + Vmf*Vmt*(-Bft*PetscCosScalar(thetaft) + Gft*PetscSinScalar(thetaft)); */
    284:               values[0] =  Vmf*Vmt*(Bft*PetscSinScalar(thetaft) + Gft*PetscCosScalar(thetaft));
    285:               values[1] =  -2.0*Bff*Vmf + Vmt*(-Bft*PetscCosScalar(thetaft) + Gft*PetscSinScalar(thetaft));
    286:               values[2] =  Vmf*Vmt*(-Bft*PetscSinScalar(thetaft) + Gft*-PetscCosScalar(thetaft));
    287:               values[3] =  Vmf*(-Bft*PetscCosScalar(thetaft) + Gft*PetscSinScalar(thetaft));

    289:               MatSetValues(J,1,row,4,col,values,ADD_VALUES);
    290:             }
    291:           } else {
    292:             if (bust->ide != REF_BUS) {
    293:               row[0] = goffsetto;
    294:               col[0] = goffsetto; col[1] = goffsetto+1; col[2] = goffsetfrom; col[3] = goffsetfrom+1;
    295:               /*    farr[offsetto]   += Gtt*Vmt*Vmt + Vmt*Vmf*(Gtf*PetscCosScalar(thetatf) + Btf*PetscSinScalar(thetatf)); */
    296:               values[0] =  Vmt*Vmf*(Gtf*-PetscSinScalar(thetatf) + Btf*PetscCosScalar(thetaft)); /* df_dthetat */
    297:               values[1] =  2.0*Gtt*Vmt + Vmf*(Gtf*PetscCosScalar(thetatf) + Btf*PetscSinScalar(thetatf)); /* df_dVmt */
    298:               values[2] =  Vmt*Vmf*(Gtf*PetscSinScalar(thetatf) + Btf*-PetscCosScalar(thetatf)); /* df_dthetaf */
    299:               values[3] =  Vmt*(Gtf*PetscCosScalar(thetatf) + Btf*PetscSinScalar(thetatf)); /* df_dVmf */

    301:               MatSetValues(J,1,row,4,col,values,ADD_VALUES);
    302:             }
    303:             if (bust->ide != PV_BUS && bust->ide != REF_BUS) {
    304:               row[0] = goffsetto+1;
    305:               col[0] = goffsetto; col[1] = goffsetto+1; col[2] = goffsetfrom; col[3] = goffsetfrom+1;
    306:               /*    farr[offsetto+1] += -Btt*Vmt*Vmt + Vmt*Vmf*(-Btf*PetscCosScalar(thetatf) + Gtf*PetscSinScalar(thetatf)); */
    307:               values[0] =  Vmt*Vmf*(Btf*PetscSinScalar(thetatf) + Gtf*PetscCosScalar(thetatf));
    308:               values[1] =  -2.0*Btt*Vmt + Vmf*(-Btf*PetscCosScalar(thetatf) + Gtf*PetscSinScalar(thetatf));
    309:               values[2] =  Vmt*Vmf*(-Btf*PetscSinScalar(thetatf) + Gtf*-PetscCosScalar(thetatf));
    310:               values[3] =  Vmt*(-Btf*PetscCosScalar(thetatf) + Gtf*PetscSinScalar(thetatf));

    312:               MatSetValues(J,1,row,4,col,values,ADD_VALUES);
    313:             }
    314:           }
    315:         }
    316:         if (!ghostvtex && bus->ide == PV_BUS) {
    317:           row[0] = goffset+1; col[0] = goffset+1;
    318:           values[0]  = 1.0;
    319:           MatSetValues(J,1,row,1,col,values,ADD_VALUES);
    320:         }
    321:       }
    322:     }
    323:   }
    324:   VecRestoreArrayRead(localX,&xarr);
    325:   return(0);
    326: }

    328: PetscErrorCode FormJacobian(SNES snes,Vec X, Mat J,Mat Jpre,void *appctx)
    329: {
    331:   DM             networkdm;
    332:   Vec            localX;
    333:   PetscInt       ne,nv;
    334:   const PetscInt *vtx,*edges;

    337:   MatZeroEntries(J);

    339:   SNESGetDM(snes,&networkdm);
    340:   DMGetLocalVector(networkdm,&localX);

    342:   DMGlobalToLocalBegin(networkdm,X,INSERT_VALUES,localX);
    343:   DMGlobalToLocalEnd(networkdm,X,INSERT_VALUES,localX);

    345:   /* Form Jacobian for first subnetwork */
    346:   DMNetworkGetSubnetwork(networkdm,0,&nv,&ne,&vtx,&edges);
    347:   FormJacobian_Subnet(networkdm,localX,J,Jpre,nv,ne,vtx,edges,appctx);

    349:   /* Form Jacobian for second subnetwork */
    350:   DMNetworkGetSubnetwork(networkdm,1,&nv,&ne,&vtx,&edges);
    351:   FormJacobian_Subnet(networkdm,localX,J,Jpre,nv,ne,vtx,edges,appctx);

    353:   DMRestoreLocalVector(networkdm,&localX);

    355:   MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
    356:   MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
    357:   return(0);
    358: }

    360: PetscErrorCode SetInitialValues_Subnet(DM networkdm,Vec localX,PetscInt nv,PetscInt ne, const PetscInt *vtx, const PetscInt *edges,void* appctx)
    361: {
    363:   VERTEX_Power   bus;
    364:   PetscInt       i;
    365:   GEN            gen;
    366:   PetscBool      ghostvtex;
    367:   PetscScalar    *xarr;
    368:   PetscInt       key,numComps,j,offset;
    369:   void*          component;

    372:   VecGetArray(localX,&xarr);
    373:   for (i = 0; i < nv; i++) {
    374:     DMNetworkIsGhostVertex(networkdm,vtx[i],&ghostvtex);
    375:     if (ghostvtex) continue;

    377:     DMNetworkGetLocalVecOffset(networkdm,vtx[i],ALL_COMPONENTS,&offset);
    378:     DMNetworkGetNumComponents(networkdm,vtx[i],&numComps);
    379:     for (j=0; j < numComps; j++) {
    380:       DMNetworkGetComponent(networkdm,vtx[i],j,&key,&component,NULL);
    381:       if (key == 1) {
    382:         bus = (VERTEX_Power)(component);
    383:         xarr[offset] = bus->va*PETSC_PI/180.0;
    384:         xarr[offset+1] = bus->vm;
    385:       } else if (key == 2) {
    386:         gen = (GEN)(component);
    387:         if (!gen->status) continue;
    388:         xarr[offset+1] = gen->vs;
    389:         break;
    390:       }
    391:     }
    392:   }
    393:   VecRestoreArray(localX,&xarr);
    394:   return(0);
    395: }

    397: PetscErrorCode SetInitialValues(DM networkdm, Vec X,void* appctx)
    398: {
    400:   PetscInt       nv,ne;
    401:   const PetscInt *vtx,*edges;
    402:   Vec            localX;

    405:   DMGetLocalVector(networkdm,&localX);

    407:   VecSet(X,0.0);
    408:   DMGlobalToLocalBegin(networkdm,X,INSERT_VALUES,localX);
    409:   DMGlobalToLocalEnd(networkdm,X,INSERT_VALUES,localX);

    411:   /* Set initial guess for first subnetwork */
    412:   DMNetworkGetSubnetwork(networkdm,0,&nv,&ne,&vtx,&edges);
    413:   SetInitialValues_Subnet(networkdm,localX,nv,ne,vtx,edges,appctx);

    415:   /* Set initial guess for second subnetwork */
    416:   DMNetworkGetSubnetwork(networkdm,1,&nv,&ne,&vtx,&edges);
    417:   SetInitialValues_Subnet(networkdm,localX,nv,ne,vtx,edges,appctx);

    419:   DMLocalToGlobalBegin(networkdm,localX,ADD_VALUES,X);
    420:   DMLocalToGlobalEnd(networkdm,localX,ADD_VALUES,X);
    421:   DMRestoreLocalVector(networkdm,&localX);
    422:   return(0);
    423: }

    425: int main(int argc,char ** argv)
    426: {
    427:   PetscErrorCode   ierr;
    428:   char             pfdata_file[PETSC_MAX_PATH_LEN]="case9.m";
    429:   PFDATA           *pfdata1,*pfdata2;
    430:   PetscInt         numEdges1=0,numVertices1=0,numEdges2=0,numVertices2=0;
    431:   PetscInt         *edgelist1 = NULL,*edgelist2 = NULL,componentkey[4];
    432:   DM               networkdm;
    433:   UserCtx_Power    User;
    434: #if defined(PETSC_USE_LOG)
    435:   PetscLogStage    stage1,stage2;
    436: #endif
    437:   PetscMPIInt      rank;
    438:   PetscInt         nsubnet = 2,nv,ne,i,j,genj,loadj;
    439:   const PetscInt   *vtx,*edges;
    440:   Vec              X,F;
    441:   Mat              J;
    442:   SNES             snes;

    445:   PetscInitialize(&argc,&argv,"poweroptions",help);if (ierr) return ierr;
    446:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
    447:   {
    448:     /* introduce the const crank so the clang static analyzer realizes that if it enters any of the if (crank) then it must have entered the first */
    449:     /* this is an experiment to see how the analyzer reacts */
    450:     const PetscMPIInt crank = rank;

    452:     /* Create an empty network object */
    453:     DMNetworkCreate(PETSC_COMM_WORLD,&networkdm);

    455:     /* Register the components in the network */
    456:     DMNetworkRegisterComponent(networkdm,"branchstruct",sizeof(struct _p_EDGE_Power),&componentkey[0]);
    457:     DMNetworkRegisterComponent(networkdm,"busstruct",sizeof(struct _p_VERTEX_Power),&componentkey[1]);
    458:     DMNetworkRegisterComponent(networkdm,"genstruct",sizeof(struct _p_GEN),&componentkey[2]);
    459:     DMNetworkRegisterComponent(networkdm,"loadstruct",sizeof(struct _p_LOAD),&componentkey[3]);

    461:     PetscLogStageRegister("Read Data",&stage1);
    462:     PetscLogStagePush(stage1);
    463:     /* READ THE DATA */
    464:     if (!crank) {
    465:       /* Only rank 0 reads the data */
    466:       PetscOptionsGetString(NULL,NULL,"-pfdata",pfdata_file,sizeof(pfdata_file),NULL);
    467:       /* HERE WE CREATE COPIES OF THE SAME NETWORK THAT WILL BE TREATED AS SUBNETWORKS */

    469:       /*    READ DATA FOR THE FIRST SUBNETWORK */
    470:       PetscNew(&pfdata1);
    471:       PFReadMatPowerData(pfdata1,pfdata_file);
    472:       User.Sbase = pfdata1->sbase;

    474:       numEdges1 = pfdata1->nbranch;
    475:       numVertices1 = pfdata1->nbus;

    477:       PetscMalloc1(2*numEdges1,&edgelist1);
    478:       GetListofEdges_Power(pfdata1,edgelist1);

    480:       /*    READ DATA FOR THE SECOND SUBNETWORK */
    481:       PetscNew(&pfdata2);
    482:       PFReadMatPowerData(pfdata2,pfdata_file);
    483:       User.Sbase = pfdata2->sbase;

    485:       numEdges2 = pfdata2->nbranch;
    486:       numVertices2 = pfdata2->nbus;

    488:       PetscMalloc1(2*numEdges2,&edgelist2);
    489:       GetListofEdges_Power(pfdata2,edgelist2);
    490:     }

    492:     PetscLogStagePop();
    493:     MPI_Barrier(PETSC_COMM_WORLD);
    494:     PetscLogStageRegister("Create network",&stage2);
    495:     PetscLogStagePush(stage2);

    497:     /* Set number of nodes/edges and edge connectivity */
    498:     DMNetworkSetNumSubNetworks(networkdm,PETSC_DECIDE,nsubnet);
    499:     DMNetworkAddSubnetwork(networkdm,"",numVertices1,numEdges1,edgelist1,NULL);
    500:     DMNetworkAddSubnetwork(networkdm,"",numVertices2,numEdges2,edgelist2,NULL);

    502:     /* Set up the network layout */
    503:     DMNetworkLayoutSetUp(networkdm);

    505:     /* Add network components only process 0 has any data to add*/
    506:     if (!crank) {
    507:       genj=0; loadj=0;

    509:       /* ADD VARIABLES AND COMPONENTS FOR THE FIRST SUBNETWORK */
    510:       DMNetworkGetSubnetwork(networkdm,0,&nv,&ne,&vtx,&edges);

    512:       for (i = 0; i < ne; i++) {
    513:         DMNetworkAddComponent(networkdm,edges[i],componentkey[0],&pfdata1->branch[i],0);
    514:       }

    516:       for (i = 0; i < nv; i++) {
    517:         DMNetworkAddComponent(networkdm,vtx[i],componentkey[1],&pfdata1->bus[i],2);
    518:         if (pfdata1->bus[i].ngen) {
    519:           for (j = 0; j < pfdata1->bus[i].ngen; j++) {
    520:             DMNetworkAddComponent(networkdm,vtx[i],componentkey[2],&pfdata1->gen[genj++],0);
    521:           }
    522:         }
    523:         if (pfdata1->bus[i].nload) {
    524:           for (j=0; j < pfdata1->bus[i].nload; j++) {
    525:             DMNetworkAddComponent(networkdm,vtx[i],componentkey[3],&pfdata1->load[loadj++],0);
    526:           }
    527:         }
    528:       }

    530:       genj=0; loadj=0;

    532:       /* ADD VARIABLES AND COMPONENTS FOR THE SECOND SUBNETWORK */
    533:       DMNetworkGetSubnetwork(networkdm,1,&nv,&ne,&vtx,&edges);

    535:       for (i = 0; i < ne; i++) {
    536:         DMNetworkAddComponent(networkdm,edges[i],componentkey[0],&pfdata2->branch[i],0);
    537:       }

    539:       for (i = 0; i < nv; i++) {
    540:         DMNetworkAddComponent(networkdm,vtx[i],componentkey[1],&pfdata2->bus[i],2);
    541:         if (pfdata2->bus[i].ngen) {
    542:           for (j = 0; j < pfdata2->bus[i].ngen; j++) {
    543:             DMNetworkAddComponent(networkdm,vtx[i],componentkey[2],&pfdata2->gen[genj++],0);
    544:           }
    545:         }
    546:         if (pfdata2->bus[i].nload) {
    547:           for (j=0; j < pfdata2->bus[i].nload; j++) {
    548:             DMNetworkAddComponent(networkdm,vtx[i],componentkey[3],&pfdata2->load[loadj++],0);
    549:           }
    550:         }
    551:       }
    552:     }

    554:     /* Set up DM for use */
    555:     DMSetUp(networkdm);

    557:     if (!crank) {
    558:       PetscFree(edgelist1);
    559:       PetscFree(edgelist2);
    560:     }

    562:     if (!crank) {
    563:       PetscFree(pfdata1->bus);
    564:       PetscFree(pfdata1->gen);
    565:       PetscFree(pfdata1->branch);
    566:       PetscFree(pfdata1->load);
    567:       PetscFree(pfdata1);

    569:       PetscFree(pfdata2->bus);
    570:       PetscFree(pfdata2->gen);
    571:       PetscFree(pfdata2->branch);
    572:       PetscFree(pfdata2->load);
    573:       PetscFree(pfdata2);
    574:     }

    576:     /* Distribute networkdm to multiple processes */
    577:     DMNetworkDistribute(&networkdm,0);

    579:     PetscLogStagePop();

    581:     /* Broadcast Sbase to all processors */
    582:     MPI_Bcast(&User.Sbase,1,MPIU_SCALAR,0,PETSC_COMM_WORLD);

    584:     DMCreateGlobalVector(networkdm,&X);
    585:     VecDuplicate(X,&F);

    587:     DMCreateMatrix(networkdm,&J);
    588:     MatSetOption(J,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

    590:     SetInitialValues(networkdm,X,&User);

    592:     /* HOOK UP SOLVER */
    593:     SNESCreate(PETSC_COMM_WORLD,&snes);
    594:     SNESSetDM(snes,networkdm);
    595:     SNESSetFunction(snes,F,FormFunction,&User);
    596:     SNESSetJacobian(snes,J,J,FormJacobian,&User);
    597:     SNESSetFromOptions(snes);

    599:     SNESSolve(snes,NULL,X);
    600:     /* VecView(X,PETSC_VIEWER_STDOUT_WORLD); */

    602:     VecDestroy(&X);
    603:     VecDestroy(&F);
    604:     MatDestroy(&J);

    606:     SNESDestroy(&snes);
    607:     DMDestroy(&networkdm);
    608:   }
    609:   PetscFinalize();
    610:   return ierr;
    611: }

    613: /*TEST

    615:    build:
    616:      depends: PFReadData.c pffunctions.c
    617:      requires: !complex double define(PETSC_HAVE_ATTRIBUTEALIGNED)

    619:    test:
    620:      args: -snes_rtol 1.e-3
    621:      localrunfiles: poweroptions case9.m
    622:      output_file: output/power_1.out

    624:    test:
    625:      suffix: 2
    626:      args: -snes_rtol 1.e-3 -petscpartitioner_type simple
    627:      nsize: 4
    628:      localrunfiles: poweroptions case9.m
    629:      output_file: output/power_1.out

    631: TEST*/