Date: 2022-03-04T17:38:28+00:00

Actual source code: plexglvis.c

      1: #include <petsc/private/glvisviewerimpl.h>
      2: #include <petsc/private/petscimpl.h>
      3: #include <petsc/private/dmpleximpl.h>
      4: #include <petscbt.h>
      5: #include <petscdmplex.h>
      6: #include <petscsf.h>
      7: #include <petscds.h>

      9: typedef struct {
     10:   PetscInt   nf;
     11:   VecScatter *scctx;
     12: } GLVisViewerCtx;

     14: static PetscErrorCode DestroyGLVisViewerCtx_Private(void *vctx)
     15: {
     16:   GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx;
     17:   PetscInt       i;

     21:   for (i=0;i<ctx->nf;i++) {
     22:     VecScatterDestroy(&ctx->scctx[i]);
     23:   }
     24:   PetscFree(ctx->scctx);
     25:   PetscFree(vctx);
     26:   return(0);
     27: }

     29: static PetscErrorCode DMPlexSampleGLVisFields_Private(PetscObject oX, PetscInt nf, PetscObject oXfield[], void *vctx)
     30: {
     31:   GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx;
     32:   PetscInt       f;

     36:   for (f=0;f<nf;f++) {
     37:     VecScatterBegin(ctx->scctx[f],(Vec)oX,(Vec)oXfield[f],INSERT_VALUES,SCATTER_FORWARD);
     38:     VecScatterEnd(ctx->scctx[f],(Vec)oX,(Vec)oXfield[f],INSERT_VALUES,SCATTER_FORWARD);
     39:   }
     40:   return(0);
     41: }

     43: /* for FEM, it works for H1 fields only and extracts dofs at cell vertices, discarding any other dof */
     44: PetscErrorCode DMSetUpGLVisViewer_Plex(PetscObject odm, PetscViewer viewer)
     45: {
     46:   DM             dm = (DM)odm;
     47:   Vec            xlocal,xfield,*Ufield;
     48:   PetscDS        ds;
     49:   IS             globalNum,isfield;
     50:   PetscBT        vown;
     51:   char           **fieldname = NULL,**fec_type = NULL;
     52:   const PetscInt *gNum;
     53:   PetscInt       *nlocal,*bs,*idxs,*dims;
     54:   PetscInt       f,maxfields,nfields,c,totc,totdofs,Nv,cum,i;
     55:   PetscInt       dim,cStart,cEnd,vStart,vEnd;
     56:   GLVisViewerCtx *ctx;
     57:   PetscSection   s;

     61:   DMGetDimension(dm,&dim);
     62:   DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);
     63:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
     64:   PetscObjectQuery((PetscObject)dm,"_glvis_plex_gnum",(PetscObject*)&globalNum);
     65:   if (!globalNum) {
     66:     DMPlexCreateCellNumbering_Internal(dm,PETSC_TRUE,&globalNum);
     67:     PetscObjectCompose((PetscObject)dm,"_glvis_plex_gnum",(PetscObject)globalNum);
     68:     PetscObjectDereference((PetscObject)globalNum);
     69:   }
     70:   ISGetIndices(globalNum,&gNum);
     71:   PetscBTCreate(vEnd-vStart,&vown);
     72:   for (c = cStart, totc = 0; c < cEnd; c++) {
     73:     if (gNum[c-cStart] >= 0) {
     74:       PetscInt i,numPoints,*points = NULL;

     76:       totc++;
     77:       DMPlexGetTransitiveClosure(dm,c,PETSC_TRUE,&numPoints,&points);
     78:       for (i=0;i<numPoints*2;i+= 2) {
     79:         if ((points[i] >= vStart) && (points[i] < vEnd)) {
     80:           PetscBTSet(vown,points[i]-vStart);
     81:         }
     82:       }
     83:       DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&numPoints,&points);
     84:     }
     85:   }
     86:   for (f=0,Nv=0;f<vEnd-vStart;f++) if (PetscLikely(PetscBTLookup(vown,f))) Nv++;

     88:   DMCreateLocalVector(dm,&xlocal);
     89:   VecGetLocalSize(xlocal,&totdofs);
     90:   DMGetLocalSection(dm,&s);
     91:   PetscSectionGetNumFields(s,&nfields);
     92:   for (f=0,maxfields=0;f<nfields;f++) {
     93:     PetscInt bs;

     95:     PetscSectionGetFieldComponents(s,f,&bs);
     96:     maxfields += bs;
     97:   }
     98:   PetscCalloc7(maxfields,&fieldname,maxfields,&nlocal,maxfields,&bs,maxfields,&dims,maxfields,&fec_type,totdofs,&idxs,maxfields,&Ufield);
     99:   PetscNew(&ctx);
    100:   PetscCalloc1(maxfields,&ctx->scctx);
    101:   DMGetDS(dm,&ds);
    102:   if (ds) {
    103:     for (f=0;f<nfields;f++) {
    104:       const char* fname;
    105:       char        name[256];
    106:       PetscObject disc;
    107:       size_t      len;

    109:       PetscSectionGetFieldName(s,f,&fname);
    110:       PetscStrlen(fname,&len);
    111:       if (len) {
    112:         PetscStrcpy(name,fname);
    113:       } else {
    114:         PetscSNPrintf(name,256,"Field%D",f);
    115:       }
    116:       PetscDSGetDiscretization(ds,f,&disc);
    117:       if (disc) {
    118:         PetscClassId id;
    119:         PetscInt     Nc;
    120:         char         fec[64];

    122:         PetscObjectGetClassId(disc, &id);
    123:         if (id == PETSCFE_CLASSID) {
    124:           PetscFE            fem = (PetscFE)disc;
    125:           PetscDualSpace     sp;
    126:           PetscDualSpaceType spname;
    127:           PetscInt           order;
    128:           PetscBool          islag,continuous,H1 = PETSC_TRUE;

    130:           PetscFEGetNumComponents(fem,&Nc);
    131:           PetscFEGetDualSpace(fem,&sp);
    132:           PetscDualSpaceGetType(sp,&spname);
    133:           PetscStrcmp(spname,PETSCDUALSPACELAGRANGE,&islag);
    134:           if (!islag) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Unsupported dual space");
    135:           PetscDualSpaceLagrangeGetContinuity(sp,&continuous);
    136:           PetscDualSpaceGetOrder(sp,&order);
    137:           if (continuous && order > 0) { /* no support for high-order viz, still have to figure out the numbering */
    138:             PetscSNPrintf(fec,64,"FiniteElementCollection: H1_%DD_P1",dim);
    139:           } else {
    140:             if (!continuous && order) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Discontinuous space visualization currently unsupported for order %D",order);
    141:             H1   = PETSC_FALSE;
    142:             PetscSNPrintf(fec,64,"FiniteElementCollection: L2_%DD_P%D",dim,order);
    143:           }
    144:           PetscStrallocpy(name,&fieldname[ctx->nf]);
    145:           bs[ctx->nf]   = Nc;
    146:           dims[ctx->nf] = dim;
    147:           if (H1) {
    148:             nlocal[ctx->nf] = Nc * Nv;
    149:             PetscStrallocpy(fec,&fec_type[ctx->nf]);
    150:             VecCreateSeq(PETSC_COMM_SELF,Nv*Nc,&xfield);
    151:             for (i=0,cum=0;i<vEnd-vStart;i++) {
    152:               PetscInt j,off;

    154:               if (PetscUnlikely(!PetscBTLookup(vown,i))) continue;
    155:               PetscSectionGetFieldOffset(s,i+vStart,f,&off);
    156:               for (j=0;j<Nc;j++) idxs[cum++] = off + j;
    157:             }
    158:             ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),Nv*Nc,idxs,PETSC_USE_POINTER,&isfield);
    159:           } else {
    160:             nlocal[ctx->nf] = Nc * totc;
    161:             PetscStrallocpy(fec,&fec_type[ctx->nf]);
    162:             VecCreateSeq(PETSC_COMM_SELF,Nc*totc,&xfield);
    163:             for (i=0,cum=0;i<cEnd-cStart;i++) {
    164:               PetscInt j,off;

    166:               if (PetscUnlikely(gNum[i] < 0)) continue;
    167:               PetscSectionGetFieldOffset(s,i+cStart,f,&off);
    168:               for (j=0;j<Nc;j++) idxs[cum++] = off + j;
    169:             }
    170:             ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),totc*Nc,idxs,PETSC_USE_POINTER,&isfield);
    171:           }
    172:           VecScatterCreate(xlocal,isfield,xfield,NULL,&ctx->scctx[ctx->nf]);
    173:           VecDestroy(&xfield);
    174:           ISDestroy(&isfield);
    175:           ctx->nf++;
    176:         } else if (id == PETSCFV_CLASSID) {
    177:           PetscInt c;

    179:           PetscFVGetNumComponents((PetscFV)disc,&Nc);
    180:           PetscSNPrintf(fec,64,"FiniteElementCollection: L2_%DD_P0",dim);
    181:           for (c = 0; c < Nc; c++) {
    182:             char comp[256];
    183:             PetscSNPrintf(comp,256,"%s-Comp%D",name,c);
    184:             PetscStrallocpy(comp,&fieldname[ctx->nf]);
    185:             bs[ctx->nf] = 1; /* Does PetscFV support components with different block size? */
    186:             nlocal[ctx->nf] = totc;
    187:             dims[ctx->nf] = dim;
    188:             PetscStrallocpy(fec,&fec_type[ctx->nf]);
    189:             VecCreateSeq(PETSC_COMM_SELF,totc,&xfield);
    190:             for (i=0,cum=0;i<cEnd-cStart;i++) {
    191:               PetscInt off;

    193:               if (PetscUnlikely(gNum[i])<0) continue;
    194:               PetscSectionGetFieldOffset(s,i+cStart,f,&off);
    195:               idxs[cum++] = off + c;
    196:             }
    197:             ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),totc,idxs,PETSC_USE_POINTER,&isfield);
    198:             VecScatterCreate(xlocal,isfield,xfield,NULL,&ctx->scctx[ctx->nf]);
    199:             VecDestroy(&xfield);
    200:             ISDestroy(&isfield);
    201:             ctx->nf++;
    202:           }
    203:         } else SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",f);
    204:       } else SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing discretization for field %D",f);
    205:     }
    206:   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Needs a DS attached to the DM");
    207:   PetscBTDestroy(&vown);
    208:   VecDestroy(&xlocal);
    209:   ISRestoreIndices(globalNum,&gNum);

    211:   /* create work vectors */
    212:   for (f=0;f<ctx->nf;f++) {
    213:     VecCreateMPI(PetscObjectComm((PetscObject)dm),nlocal[f],PETSC_DECIDE,&Ufield[f]);
    214:     PetscObjectSetName((PetscObject)Ufield[f],fieldname[f]);
    215:     VecSetBlockSize(Ufield[f],bs[f]);
    216:     VecSetDM(Ufield[f],dm);
    217:   }

    219:   /* customize the viewer */
    220:   PetscViewerGLVisSetFields(viewer,ctx->nf,(const char**)fec_type,dims,DMPlexSampleGLVisFields_Private,(PetscObject*)Ufield,ctx,DestroyGLVisViewerCtx_Private);
    221:   for (f=0;f<ctx->nf;f++) {
    222:     PetscFree(fieldname[f]);
    223:     PetscFree(fec_type[f]);
    224:     VecDestroy(&Ufield[f]);
    225:   }
    226:   PetscFree7(fieldname,nlocal,bs,dims,fec_type,idxs,Ufield);
    227:   return(0);
    228: }

    230: typedef enum {MFEM_POINT=0,MFEM_SEGMENT,MFEM_TRIANGLE,MFEM_SQUARE,MFEM_TETRAHEDRON,MFEM_CUBE,MFEM_PRISM,MFEM_UNDEF} MFEM_cid;

    232: MFEM_cid mfem_table_cid[4][7]       = { {MFEM_POINT,MFEM_UNDEF,MFEM_UNDEF  ,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF,MFEM_UNDEF},
    233:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF,MFEM_UNDEF},
    234:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_TRIANGLE,MFEM_SQUARE     ,MFEM_UNDEF,MFEM_UNDEF},
    235:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_TETRAHEDRON,MFEM_PRISM,MFEM_CUBE } };

    237: MFEM_cid mfem_table_cid_unint[4][9] = { {MFEM_POINT,MFEM_UNDEF,MFEM_UNDEF  ,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF,MFEM_PRISM,MFEM_UNDEF,MFEM_UNDEF},
    238:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF,MFEM_PRISM,MFEM_UNDEF,MFEM_UNDEF},
    239:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_TRIANGLE,MFEM_SQUARE     ,MFEM_UNDEF,MFEM_PRISM,MFEM_UNDEF,MFEM_UNDEF},
    240:                                         {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_TETRAHEDRON,MFEM_UNDEF,MFEM_PRISM,MFEM_UNDEF,MFEM_CUBE } };

    242: static PetscErrorCode DMPlexGetPointMFEMCellID_Internal(DM dm, DMLabel label, PetscInt minl, PetscInt p, PetscInt *mid, PetscInt *cid)
    243: {
    244:   DMLabel        dlabel;
    245:   PetscInt       depth,csize,pdepth,dim;

    249:   DMPlexGetDepthLabel(dm,&dlabel);
    250:   DMLabelGetValue(dlabel,p,&pdepth);
    251:   DMPlexGetConeSize(dm,p,&csize);
    252:   DMPlexGetDepth(dm,&depth);
    253:   DMGetDimension(dm,&dim);
    254:   if (label) {
    255:     DMLabelGetValue(label,p,mid);
    256:     *mid = *mid - minl + 1; /* MFEM does not like negative markers */
    257:   } else *mid = 1;
    258:   if (depth >=0 && dim != depth) { /* not interpolated, it assumes cell-vertex mesh */
    259:     if (dim < 0 || dim > 3) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D",dim);
    260:     if (csize > 8) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Found cone size %D for point %D",csize,p);
    261:     if (depth != 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Found depth %D for point %D. You should interpolate the mesh first",depth,p);
    262:     *cid = mfem_table_cid_unint[dim][csize];
    263:   } else {
    264:     if (csize > 6) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cone size %D for point %D",csize,p);
    265:     if (pdepth < 0 || pdepth > 3) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Depth %D for point %D",csize,p);
    266:     *cid = mfem_table_cid[pdepth][csize];
    267:   }
    268:   return(0);
    269: }

    271: static PetscErrorCode DMPlexGetPointMFEMVertexIDs_Internal(DM dm, PetscInt p, PetscSection csec, PetscInt *nv, PetscInt vids[])
    272: {
    273:   PetscInt       dim,sdim,dof = 0,off = 0,i,q,vStart,vEnd,numPoints,*points = NULL;

    277:   DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);
    278:   DMGetDimension(dm,&dim);
    279:   sdim = dim;
    280:   if (csec) {
    281:     PetscInt sStart,sEnd;

    283:     DMGetCoordinateDim(dm,&sdim);
    284:     PetscSectionGetChart(csec,&sStart,&sEnd);
    285:     PetscSectionGetOffset(csec,vStart,&off);
    286:     off  = off/sdim;
    287:     if (p >= sStart && p < sEnd) {
    288:       PetscSectionGetDof(csec,p,&dof);
    289:     }
    290:   }
    291:   if (!dof) {
    292:     DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);
    293:     for (i=0,q=0;i<numPoints*2;i+= 2)
    294:       if ((points[i] >= vStart) && (points[i] < vEnd))
    295:         vids[q++] = points[i]-vStart+off;
    296:     DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);
    297:   } else {
    298:     PetscSectionGetOffset(csec,p,&off);
    299:     PetscSectionGetDof(csec,p,&dof);
    300:     for (q=0;q<dof/sdim;q++) vids[q] = off/sdim + q;
    301:   }
    302:   *nv = q;
    303:   return(0);
    304: }

    306: static PetscErrorCode GLVisCreateFE(PetscFE femIn,char name[32],PetscFE *fem)
    307: {
    308:   DM              K;
    309:   PetscSpace      P;
    310:   PetscDualSpace  Q;
    311:   PetscQuadrature q,fq;
    312:   PetscInt        dim,deg,dof;
    313:   DMPolytopeType  ptype;
    314:   PetscBool       isSimplex,isTensor;
    315:   PetscBool       continuity = PETSC_FALSE;
    316:   PetscDTNodeType nodeType   = PETSCDTNODES_GAUSSJACOBI;
    317:   PetscBool       endpoint   = PETSC_TRUE;
    318:   MPI_Comm        comm;
    319:   PetscErrorCode  ierr;

    322:   comm = PetscObjectComm((PetscObject)femIn);
    323:   PetscFEGetBasisSpace(femIn,&P);
    324:   PetscFEGetDualSpace(femIn,&Q);
    325:   PetscDualSpaceGetDM(Q,&K);
    326:   DMGetDimension(K,&dim);
    327:   PetscSpaceGetDegree(P,&deg,NULL);
    328:   PetscSpaceGetNumComponents(P,&dof);
    329:   DMPlexGetCellType(K,0,&ptype);
    330:   switch (ptype) {
    331:   case DM_POLYTOPE_QUADRILATERAL:
    332:   case DM_POLYTOPE_HEXAHEDRON:
    333:     isSimplex = PETSC_FALSE; break;
    334:   default:
    335:     isSimplex = PETSC_TRUE; break;
    336:   }
    337:   isTensor = isSimplex ? PETSC_FALSE : PETSC_TRUE;
    338:   /* Create space */
    339:   PetscSpaceCreate(comm,&P);
    340:   PetscSpaceSetType(P,PETSCSPACEPOLYNOMIAL);
    341:   PetscSpacePolynomialSetTensor(P,isTensor);
    342:   PetscSpaceSetNumComponents(P,dof);
    343:   PetscSpaceSetNumVariables(P,dim);
    344:   PetscSpaceSetDegree(P,deg,PETSC_DETERMINE);
    345:   PetscSpaceSetUp(P);
    346:   /* Create dual space */
    347:   PetscDualSpaceCreate(comm,&Q);
    348:   PetscDualSpaceSetType(Q,PETSCDUALSPACELAGRANGE);
    349:   PetscDualSpaceLagrangeSetTensor(Q,isTensor);
    350:   PetscDualSpaceLagrangeSetContinuity(Q,continuity);
    351:   PetscDualSpaceLagrangeSetNodeType(Q,nodeType,endpoint,0);
    352:   PetscDualSpaceSetNumComponents(Q,dof);
    353:   PetscDualSpaceSetOrder(Q,deg);
    354:   PetscDualSpaceCreateReferenceCell(Q,dim,isSimplex,&K);
    355:   PetscDualSpaceSetDM(Q,K);
    356:   DMDestroy(&K);
    357:   PetscDualSpaceSetUp(Q);
    358:   /* Create quadrature */
    359:   if (isSimplex) {
    360:     PetscDTStroudConicalQuadrature(dim,  1,deg+1,-1,+1,&q);
    361:     PetscDTStroudConicalQuadrature(dim-1,1,deg+1,-1,+1,&fq);
    362:   } else {
    363:     PetscDTGaussTensorQuadrature(dim,  1,deg+1,-1,+1,&q);
    364:     PetscDTGaussTensorQuadrature(dim-1,1,deg+1,-1,+1,&fq);
    365:   }
    366:   /* Create finite element */
    367:   PetscFECreate(comm,fem);
    368:   PetscSNPrintf(name,32,"L2_T1_%DD_P%D",dim,deg);
    369:   PetscObjectSetName((PetscObject)*fem,name);
    370:   PetscFESetType(*fem,PETSCFEBASIC);
    371:   PetscFESetNumComponents(*fem,dof);
    372:   PetscFESetBasisSpace(*fem,P);
    373:   PetscFESetDualSpace(*fem,Q);
    374:   PetscFESetQuadrature(*fem,q);
    375:   PetscFESetFaceQuadrature(*fem,fq);
    376:   PetscFESetUp(*fem);
    377:   /* Cleanup */
    378:   PetscSpaceDestroy(&P);
    379:   PetscDualSpaceDestroy(&Q);
    380:   PetscQuadratureDestroy(&q);
    381:   PetscQuadratureDestroy(&fq);
    382:   return(0);
    383: }

    385: /*
    386:    ASCII visualization/dump: full support for simplices and tensor product cells. It supports AMR
    387:    Higher order meshes are also supported
    388: */
    389: static PetscErrorCode DMPlexView_GLVis_ASCII(DM dm, PetscViewer viewer)
    390: {
    391:   DMLabel              label;
    392:   PetscSection         coordSection,parentSection;
    393:   Vec                  coordinates,hovec;
    394:   const PetscScalar    *array;
    395:   PetscInt             bf,p,sdim,dim,depth,novl,minl;
    396:   PetscInt             cStart,cEnd,vStart,vEnd,nvert;
    397:   PetscMPIInt          size;
    398:   PetscBool            localized,isascii;
    399:   PetscBool            enable_mfem,enable_boundary,enable_ncmesh,view_ovl = PETSC_FALSE;
    400:   PetscBT              pown,vown;
    401:   PetscErrorCode       ierr;
    402:   PetscContainer       glvis_container;
    403:   PetscBool            cellvertex = PETSC_FALSE, periodic, enabled = PETSC_TRUE;
    404:   PetscBool            enable_emark,enable_bmark;
    405:   const char           *fmt;
    406:   char                 emark[64] = "",bmark[64] = "";

    411:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
    412:   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERASCII");
    413:   MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&size);
    414:   if (size > 1) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Use single sequential viewers for parallel visualization");
    415:   DMGetDimension(dm,&dim);

    417:   /* get container: determines if a process visualizes is portion of the data or not */
    418:   PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&glvis_container);
    419:   if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis container");
    420:   {
    421:     PetscViewerGLVisInfo glvis_info;
    422:     PetscContainerGetPointer(glvis_container,(void**)&glvis_info);
    423:     enabled = glvis_info->enabled;
    424:     fmt     = glvis_info->fmt;
    425:   }

    427:   /* Users can attach a coordinate vector to the DM in case they have a higher-order mesh
    428:      DMPlex does not currently support HO meshes, so there's no API for this */
    429:   PetscObjectQuery((PetscObject)dm,"_glvis_mesh_coords",(PetscObject*)&hovec);
    430:   PetscObjectReference((PetscObject)hovec);
    431:   if (!hovec) {
    432:     DM           cdm;
    433:     PetscFE      disc;
    434:     PetscClassId classid;

    436:     DMGetCoordinateDM(dm,&cdm);
    437:     DMGetField(cdm,0,NULL,(PetscObject*)&disc);
    438:     PetscObjectGetClassId((PetscObject)disc,&classid);
    439:     if (classid == PETSCFE_CLASSID) {
    440:       DM      hocdm;
    441:       PetscFE hodisc;
    442:       Vec     vec;
    443:       Mat     mat;
    444:       char    name[32],fec_type[64];

    446:       GLVisCreateFE(disc,name,&hodisc);
    447:       DMClone(cdm,&hocdm);
    448:       DMSetField(hocdm,0,NULL,(PetscObject)hodisc);
    449:       PetscFEDestroy(&hodisc);
    450:       DMCreateDS(hocdm);

    452:       DMGetCoordinates(dm,&vec);
    453:       DMCreateGlobalVector(hocdm,&hovec);
    454:       DMCreateInterpolation(cdm,hocdm,&mat,NULL);
    455:       MatInterpolate(mat,vec,hovec);
    456:       MatDestroy(&mat);
    457:       DMDestroy(&hocdm);

    459:       PetscSNPrintf(fec_type,sizeof(fec_type),"FiniteElementCollection: %s", name);
    460:       PetscObjectSetName((PetscObject)hovec,fec_type);
    461:     }
    462:   }

    464:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
    465:   DMPlexGetGhostCellStratum(dm,&p,NULL);
    466:   if (p >= 0) cEnd = p;
    467:   DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);
    468:   DMGetPeriodicity(dm,&periodic,NULL,NULL,NULL);
    469:   DMGetCoordinatesLocalized(dm,&localized);
    470:   if (periodic && !localized && !hovec) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Coordinates need to be localized");
    471:   DMGetCoordinateSection(dm,&coordSection);
    472:   DMGetCoordinateDim(dm,&sdim);
    473:   DMGetCoordinatesLocal(dm,&coordinates);
    474:   if (!coordinates && !hovec) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");

    476:   /*
    477:      a couple of sections of the mesh specification are disabled
    478:        - boundary: the boundary is not needed for proper mesh visualization unless we want to visualize boundary attributes or we have high-order coordinates in 3D (topologically)
    479:        - vertex_parents: used for non-conforming meshes only when we want to use MFEM as a discretization package
    480:                          and be able to derefine the mesh (MFEM does not currently have to ability to read ncmeshes in parallel)
    481:   */
    482:   enable_boundary = PETSC_FALSE;
    483:   enable_ncmesh   = PETSC_FALSE;
    484:   enable_mfem     = PETSC_FALSE;
    485:   enable_emark    = PETSC_FALSE;
    486:   enable_bmark    = PETSC_FALSE;
    487:   /* I'm tired of problems with negative values in the markers, disable them */
    488:   PetscOptionsBegin(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,"GLVis PetscViewer DMPlex Options","PetscViewer");
    489:   PetscOptionsBool("-viewer_glvis_dm_plex_enable_boundary","Enable boundary section in mesh representation",NULL,enable_boundary,&enable_boundary,NULL);
    490:   PetscOptionsBool("-viewer_glvis_dm_plex_enable_ncmesh","Enable vertex_parents section in mesh representation (allows derefinement)",NULL,enable_ncmesh,&enable_ncmesh,NULL);
    491:   PetscOptionsBool("-viewer_glvis_dm_plex_enable_mfem","Dump a mesh that can be used with MFEM's FiniteElementSpaces",NULL,enable_mfem,&enable_mfem,NULL);
    492:   PetscOptionsBool("-viewer_glvis_dm_plex_overlap","Include overlap region in local meshes",NULL,view_ovl,&view_ovl,NULL);
    493:   PetscOptionsString("-viewer_glvis_dm_plex_emarker","String for the material id label",NULL,emark,emark,sizeof(emark),&enable_emark);
    494:   PetscOptionsString("-viewer_glvis_dm_plex_bmarker","String for the boundary id label",NULL,bmark,bmark,sizeof(bmark),&enable_bmark);
    495:   PetscOptionsEnd();
    496:   if (enable_bmark) enable_boundary = PETSC_TRUE;

    498:   MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);
    499:   if (enable_ncmesh && size > 1) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Not supported in parallel");
    500:   DMPlexGetDepth(dm,&depth);
    501:   if (enable_boundary && depth >= 0 && dim != depth) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated. "
    502:                                                              "Alternatively, run with -viewer_glvis_dm_plex_enable_boundary 0");
    503:   if (enable_ncmesh && depth >= 0 && dim != depth) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated. "
    504:                                                            "Alternatively, run with -viewer_glvis_dm_plex_enable_ncmesh 0");
    505:   if (depth >=0 && dim != depth) { /* not interpolated, it assumes cell-vertex mesh */
    506:     if (depth != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported depth %D. You should interpolate the mesh first",depth);
    507:     cellvertex = PETSC_TRUE;
    508:   }

    510:   /* Identify possible cells in the overlap */
    511:   novl = 0;
    512:   pown = NULL;
    513:   if (size > 1) {
    514:     IS             globalNum = NULL;
    515:     const PetscInt *gNum;
    516:     PetscBool      ovl  = PETSC_FALSE;

    518:     PetscObjectQuery((PetscObject)dm,"_glvis_plex_gnum",(PetscObject*)&globalNum);
    519:     if (!globalNum) {
    520:       if (view_ovl) {
    521:         ISCreateStride(PetscObjectComm((PetscObject)dm),cEnd-cStart,0,1,&globalNum);
    522:       } else {
    523:         DMPlexCreateCellNumbering_Internal(dm,PETSC_TRUE,&globalNum);
    524:       }
    525:       PetscObjectCompose((PetscObject)dm,"_glvis_plex_gnum",(PetscObject)globalNum);
    526:       PetscObjectDereference((PetscObject)globalNum);
    527:     }
    528:     ISGetIndices(globalNum,&gNum);
    529:     for (p=cStart; p<cEnd; p++) {
    530:       if (gNum[p-cStart] < 0) {
    531:         ovl = PETSC_TRUE;
    532:         novl++;
    533:       }
    534:     }
    535:     if (ovl) {
    536:       /* it may happen that pown get not destroyed, if the user closes the window while this function is running.
    537:          TODO: garbage collector? attach pown to dm?  */
    538:       PetscBTCreate(cEnd-cStart,&pown);
    539:       for (p=cStart; p<cEnd; p++) {
    540:         if (gNum[p-cStart] < 0) continue;
    541:         else {
    542:           PetscBTSet(pown,p-cStart);
    543:         }
    544:       }
    545:     }
    546:     ISRestoreIndices(globalNum,&gNum);
    547:   }

    549:   /* vertex_parents (Non-conforming meshes) */
    550:   parentSection  = NULL;
    551:   if (enable_ncmesh) {
    552:     DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);
    553:     enable_ncmesh = (PetscBool)(enable_ncmesh && parentSection);
    554:   }
    555:   /* return if this process is disabled */
    556:   if (!enabled) {
    557:     PetscViewerASCIIPrintf(viewer,"MFEM mesh %s\n",enable_ncmesh ? "v1.1" : "v1.0");
    558:     PetscViewerASCIIPrintf(viewer,"\ndimension\n");
    559:     PetscViewerASCIIPrintf(viewer,"%D\n",dim);
    560:     PetscViewerASCIIPrintf(viewer,"\nelements\n");
    561:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
    562:     PetscViewerASCIIPrintf(viewer,"\nboundary\n");
    563:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
    564:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
    565:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
    566:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
    567:     PetscBTDestroy(&pown);
    568:     VecDestroy(&hovec);
    569:     return(0);
    570:   }

    572:   if (enable_mfem) {
    573:     if (periodic && !hovec) { /* we need to generate a vector of L2 coordinates, as this is how MFEM handles periodic meshes */
    574:       PetscInt    vpc = 0;
    575:       char        fec[64];
    576:       PetscInt    vids[8] = {0,1,2,3,4,5,6,7};
    577:       PetscInt    hexv[8] = {0,1,3,2,4,5,7,6}, tetv[4] = {0,1,2,3};
    578:       PetscInt    quadv[8] = {0,1,3,2}, triv[3] = {0,1,2};
    579:       PetscInt    *dof = NULL;
    580:       PetscScalar *array,*ptr;

    582:       PetscSNPrintf(fec,sizeof(fec),"FiniteElementCollection: L2_T1_%DD_P1",dim);
    583:       if (cEnd-cStart) {
    584:         PetscInt fpc;

    586:         DMPlexGetConeSize(dm,cStart,&fpc);
    587:         switch(dim) {
    588:           case 1:
    589:             vpc = 2;
    590:             dof = hexv;
    591:             break;
    592:           case 2:
    593:             switch (fpc) {
    594:               case 3:
    595:                 vpc = 3;
    596:                 dof = triv;
    597:                 break;
    598:               case 4:
    599:                 vpc = 4;
    600:                 dof = quadv;
    601:                 break;
    602:               default:
    603:                 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: faces per cell %D",fpc);
    604:             }
    605:             break;
    606:           case 3:
    607:             switch (fpc) {
    608:               case 4: /* TODO: still need to understand L2 ordering for tets */
    609:                 vpc = 4;
    610:                 dof = tetv;
    611:                 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled tethraedral case");
    612:               case 6:
    613:                 if (cellvertex) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: vertices per cell %D",fpc);
    614:                 vpc = 8;
    615:                 dof = hexv;
    616:                 break;
    617:               case 8:
    618:                 if (!cellvertex) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: faces per cell %D",fpc);
    619:                 vpc = 8;
    620:                 dof = hexv;
    621:                 break;
    622:               default:
    623:                 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: faces per cell %D",fpc);
    624:             }
    625:             break;
    626:           default:
    627:             SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Unhandled dim");
    628:         }
    629:         DMPlexReorderCell(dm,cStart,vids);
    630:       }
    631:       if (!dof) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing dofs");
    632:       VecCreateSeq(PETSC_COMM_SELF,(cEnd-cStart-novl)*vpc*sdim,&hovec);
    633:       PetscObjectSetName((PetscObject)hovec,fec);
    634:       VecGetArray(hovec,&array);
    635:       ptr  = array;
    636:       for (p=cStart;p<cEnd;p++) {
    637:         PetscInt    csize,v,d;
    638:         PetscScalar *vals = NULL;

    640:         if (PetscUnlikely(pown && !PetscBTLookup(pown,p-cStart))) continue;
    641:         DMPlexVecGetClosure(dm,coordSection,coordinates,p,&csize,&vals);
    642:         if (csize != vpc*sdim && csize != vpc*sdim*2) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported closure size %D (vpc %D, sdim %D)",csize,vpc,sdim);
    643:         for (v=0;v<vpc;v++) {
    644:           for (d=0;d<sdim;d++) {
    645:             ptr[sdim*dof[v]+d] = vals[sdim*vids[v]+d];
    646:           }
    647:         }
    648:         ptr += vpc*sdim;
    649:         DMPlexVecRestoreClosure(dm,coordSection,coordinates,p,&csize,&vals);
    650:       }
    651:       VecRestoreArray(hovec,&array);
    652:     }
    653:   }
    654:   /* if we have high-order coordinates in 3D, we need to specify the boundary */
    655:   if (hovec && dim == 3) enable_boundary = PETSC_TRUE;

    657:   /* header */
    658:   PetscViewerASCIIPrintf(viewer,"MFEM mesh %s\n",enable_ncmesh ? "v1.1" : "v1.0");

    660:   /* topological dimension */
    661:   PetscViewerASCIIPrintf(viewer,"\ndimension\n");
    662:   PetscViewerASCIIPrintf(viewer,"%D\n",dim);

    664:   /* elements */
    665:   minl = 1;
    666:   label = NULL;
    667:   if (enable_emark) {
    668:     PetscInt lminl = PETSC_MAX_INT;

    670:     DMGetLabel(dm,emark,&label);
    671:     if (label) {
    672:       IS       vals;
    673:       PetscInt ldef;

    675:       DMLabelGetDefaultValue(label,&ldef);
    676:       DMLabelGetValueIS(label,&vals);
    677:       ISGetMinMax(vals,&lminl,NULL);
    678:       ISDestroy(&vals);
    679:       lminl = PetscMin(ldef,lminl);
    680:     }
    681:     MPIU_Allreduce(&lminl,&minl,1,MPIU_INT,MPI_MIN,PetscObjectComm((PetscObject)dm));
    682:     if (minl == PETSC_MAX_INT) minl = 1;
    683:   }
    684:   PetscViewerASCIIPrintf(viewer,"\nelements\n");
    685:   PetscViewerASCIIPrintf(viewer,"%D\n",cEnd-cStart-novl);
    686:   for (p=cStart;p<cEnd;p++) {
    687:     PetscInt       vids[8];
    688:     PetscInt       i,nv = 0,cid = -1,mid = 1;

    690:     if (PetscUnlikely(pown && !PetscBTLookup(pown,p-cStart))) continue;
    691:     DMPlexGetPointMFEMCellID_Internal(dm,label,minl,p,&mid,&cid);
    692:     DMPlexGetPointMFEMVertexIDs_Internal(dm,p,(localized && !hovec) ? coordSection : NULL,&nv,vids);
    693:     DMPlexReorderCell(dm,p,vids);
    694:     PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);
    695:     for (i=0;i<nv;i++) {
    696:       PetscViewerASCIIPrintf(viewer," %D",vids[i]);
    697:     }
    698:     PetscViewerASCIIPrintf(viewer,"\n");
    699:   }

    701:   /* boundary */
    702:   PetscViewerASCIIPrintf(viewer,"\nboundary\n");
    703:   if (!enable_boundary) {
    704:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
    705:   } else {
    706:     DMLabel  perLabel;
    707:     PetscBT  bfaces;
    708:     PetscInt fStart,fEnd,*fcells;

    710:     DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);
    711:     PetscBTCreate(fEnd-fStart,&bfaces);
    712:     DMPlexGetMaxSizes(dm,NULL,&p);
    713:     PetscMalloc1(p,&fcells);
    714:     DMGetLabel(dm,"glvis_periodic_cut",&perLabel);
    715:     if (!perLabel && localized) { /* this periodic cut can be moved up to DMPlex setup */
    716:       DMCreateLabel(dm,"glvis_periodic_cut");
    717:       DMGetLabel(dm,"glvis_periodic_cut",&perLabel);
    718:       DMLabelSetDefaultValue(perLabel,1);
    719:       for (p=cStart;p<cEnd;p++) {
    720:         DMPolytopeType cellType;
    721:         PetscInt       dof;

    723:         DMPlexGetCellType(dm,p,&cellType);
    724:         PetscSectionGetDof(coordSection,p,&dof);
    725:         if (dof) {
    726:           PetscInt    uvpc, v,csize,cellClosureSize,*cellClosure = NULL,*vidxs = NULL;
    727:           PetscScalar *vals = NULL;

    729:           uvpc = DMPolytopeTypeGetNumVertices(cellType);
    730:           if (dof%sdim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Incompatible number of cell dofs %D and space dimension %D",dof,sdim);
    731:           DMPlexVecGetClosure(dm,coordSection,coordinates,p,&csize,&vals);
    732:           DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);
    733:           for (v=0;v<cellClosureSize;v++)
    734:             if (cellClosure[2*v] >= vStart && cellClosure[2*v] < vEnd) {
    735:               vidxs = cellClosure + 2*v;
    736:               break;
    737:             }
    738:           if (!vidxs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing vertices");
    739:           for (v=0;v<uvpc;v++) {
    740:             PetscInt s;

    742:             for (s=0;s<sdim;s++) {
    743:               if (PetscAbsScalar(vals[v*sdim+s]-vals[v*sdim+s+uvpc*sdim])>PETSC_MACHINE_EPSILON) {
    744:                 DMLabelSetValue(perLabel,vidxs[2*v],2);
    745:               }
    746:             }
    747:           }
    748:           DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);
    749:           DMPlexVecRestoreClosure(dm,coordSection,coordinates,p,&csize,&vals);
    750:         }
    751:       }
    752:       if (dim > 1) {
    753:         PetscInt eEnd,eStart;

    755:         DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);
    756:         for (p=eStart;p<eEnd;p++) {
    757:           const PetscInt *cone;
    758:           PetscInt       coneSize,i;
    759:           PetscBool      ispe = PETSC_TRUE;

    761:           DMPlexGetCone(dm,p,&cone);
    762:           DMPlexGetConeSize(dm,p,&coneSize);
    763:           for (i=0;i<coneSize;i++) {
    764:             PetscInt v;

    766:             DMLabelGetValue(perLabel,cone[i],&v);
    767:             ispe = (PetscBool)(ispe && (v==2));
    768:           }
    769:           if (ispe && coneSize) {
    770:             PetscInt       ch, numChildren;
    771:             const PetscInt *children;

    773:             DMLabelSetValue(perLabel,p,2);
    774:             DMPlexGetTreeChildren(dm,p,&numChildren,&children);
    775:             for (ch = 0; ch < numChildren; ch++) {
    776:               DMLabelSetValue(perLabel,children[ch],2);
    777:             }
    778:           }
    779:         }
    780:         if (dim > 2) {
    781:           for (p=fStart;p<fEnd;p++) {
    782:             const PetscInt *cone;
    783:             PetscInt       coneSize,i;
    784:             PetscBool      ispe = PETSC_TRUE;

    786:             DMPlexGetCone(dm,p,&cone);
    787:             DMPlexGetConeSize(dm,p,&coneSize);
    788:             for (i=0;i<coneSize;i++) {
    789:               PetscInt v;

    791:               DMLabelGetValue(perLabel,cone[i],&v);
    792:               ispe = (PetscBool)(ispe && (v==2));
    793:             }
    794:             if (ispe && coneSize) {
    795:               PetscInt       ch, numChildren;
    796:               const PetscInt *children;

    798:               DMLabelSetValue(perLabel,p,2);
    799:               DMPlexGetTreeChildren(dm,p,&numChildren,&children);
    800:               for (ch = 0; ch < numChildren; ch++) {
    801:                 DMLabelSetValue(perLabel,children[ch],2);
    802:               }
    803:             }
    804:           }
    805:         }
    806:       }
    807:     }
    808:     for (p=fStart;p<fEnd;p++) {
    809:       const PetscInt *support;
    810:       PetscInt       supportSize;
    811:       PetscBool      isbf = PETSC_FALSE;

    813:       DMPlexGetSupportSize(dm,p,&supportSize);
    814:       if (pown) {
    815:         PetscBool has_owned = PETSC_FALSE, has_ghost = PETSC_FALSE;
    816:         PetscInt  i;

    818:         DMPlexGetSupport(dm,p,&support);
    819:         for (i=0;i<supportSize;i++) {
    820:           if (PetscLikely(PetscBTLookup(pown,support[i]-cStart))) has_owned = PETSC_TRUE;
    821:           else has_ghost = PETSC_TRUE;
    822:         }
    823:         isbf = (PetscBool)((supportSize == 1 && has_owned) || (supportSize > 1 && has_owned && has_ghost));
    824:       } else {
    825:         isbf = (PetscBool)(supportSize == 1);
    826:       }
    827:       if (!isbf && perLabel) {
    828:         const PetscInt *cone;
    829:         PetscInt       coneSize,i;

    831:         DMPlexGetCone(dm,p,&cone);
    832:         DMPlexGetConeSize(dm,p,&coneSize);
    833:         isbf = PETSC_TRUE;
    834:         for (i=0;i<coneSize;i++) {
    835:           PetscInt v,d;

    837:           DMLabelGetValue(perLabel,cone[i],&v);
    838:           DMLabelGetDefaultValue(perLabel,&d);
    839:           isbf = (PetscBool)(isbf && v != d);
    840:         }
    841:       }
    842:       if (isbf) {
    843:         PetscBTSet(bfaces,p-fStart);
    844:       }
    845:     }
    846:     /* count boundary faces */
    847:     for (p=fStart,bf=0;p<fEnd;p++) {
    848:       if (PetscUnlikely(PetscBTLookup(bfaces,p-fStart))) {
    849:         const PetscInt *support;
    850:         PetscInt       supportSize,c;

    852:         DMPlexGetSupportSize(dm,p,&supportSize);
    853:         DMPlexGetSupport(dm,p,&support);
    854:         for (c=0;c<supportSize;c++) {
    855:           const    PetscInt *cone;
    856:           PetscInt cell,cl,coneSize;

    858:           cell = support[c];
    859:           if (pown && PetscUnlikely(!PetscBTLookup(pown,cell-cStart))) continue;
    860:           DMPlexGetCone(dm,cell,&cone);
    861:           DMPlexGetConeSize(dm,cell,&coneSize);
    862:           for (cl=0;cl<coneSize;cl++) {
    863:             if (cone[cl] == p) {
    864:               bf += 1;
    865:               break;
    866:             }
    867:           }
    868:         }
    869:       }
    870:     }
    871:     minl = 1;
    872:     label = NULL;
    873:     if (enable_bmark) {
    874:       PetscInt lminl = PETSC_MAX_INT;

    876:       DMGetLabel(dm,bmark,&label);
    877:       if (label) {
    878:         IS       vals;
    879:         PetscInt ldef;

    881:         DMLabelGetDefaultValue(label,&ldef);
    882:         DMLabelGetValueIS(label,&vals);
    883:         ISGetMinMax(vals,&lminl,NULL);
    884:         ISDestroy(&vals);
    885:         lminl = PetscMin(ldef,lminl);
    886:       }
    887:       MPIU_Allreduce(&lminl,&minl,1,MPIU_INT,MPI_MIN,PetscObjectComm((PetscObject)dm));
    888:       if (minl == PETSC_MAX_INT) minl = 1;
    889:     }
    890:     PetscViewerASCIIPrintf(viewer,"%D\n",bf);
    891:     for (p=fStart;p<fEnd;p++) {
    892:       if (PetscUnlikely(PetscBTLookup(bfaces,p-fStart))) {
    893:         const PetscInt *support;
    894:         PetscInt       supportSize,c,nc = 0;

    896:         DMPlexGetSupportSize(dm,p,&supportSize);
    897:         DMPlexGetSupport(dm,p,&support);
    898:         if (pown) {
    899:           for (c=0;c<supportSize;c++) {
    900:             if (PetscLikely(PetscBTLookup(pown,support[c]-cStart))) {
    901:               fcells[nc++] = support[c];
    902:             }
    903:           }
    904:         } else for (c=0;c<supportSize;c++) fcells[nc++] = support[c];
    905:         for (c=0;c<nc;c++) {
    906:           const DMPolytopeType *faceTypes;
    907:           DMPolytopeType       cellType;
    908:           const PetscInt       *faceSizes,*cone;
    909:           PetscInt             vids[8],*faces,st,i,coneSize,cell,cl,nv,cid = -1,mid = -1;

    911:           cell = fcells[c];
    912:           DMPlexGetCone(dm,cell,&cone);
    913:           DMPlexGetConeSize(dm,cell,&coneSize);
    914:           for (cl=0;cl<coneSize;cl++)
    915:             if (cone[cl] == p)
    916:               break;
    917:           if (cl == coneSize) continue;

    919:           /* face material id and type */
    920:           DMPlexGetPointMFEMCellID_Internal(dm,label,minl,p,&mid,&cid);
    921:           PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);
    922:           /* vertex ids */
    923:           DMPlexGetCellType(dm,cell,&cellType);
    924:           DMPlexGetPointMFEMVertexIDs_Internal(dm,cell,(localized && !hovec) ? coordSection : NULL,&nv,vids);
    925:           DMPlexGetRawFaces_Internal(dm,cellType,vids,NULL,&faceTypes,&faceSizes,(const PetscInt**)&faces);
    926:           st = 0;
    927:           for (i=0;i<cl;i++) st += faceSizes[i];
    928:           DMPlexInvertCell(faceTypes[cl],faces + st);
    929:           for (i=0;i<faceSizes[cl];i++) {
    930:             PetscViewerASCIIPrintf(viewer," %d",faces[st+i]);
    931:           }
    932:           PetscViewerASCIIPrintf(viewer,"\n");
    933:           DMPlexRestoreRawFaces_Internal(dm,cellType,vids,NULL,&faceTypes,&faceSizes,(const PetscInt**)&faces);
    934:           bf -= 1;
    935:         }
    936:       }
    937:     }
    938:     if (bf) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Remaining boundary faces %D",bf);
    939:     PetscBTDestroy(&bfaces);
    940:     PetscFree(fcells);
    941:   }

    943:   /* mark owned vertices */
    944:   vown = NULL;
    945:   if (pown) {
    946:     PetscBTCreate(vEnd-vStart,&vown);
    947:     for (p=cStart;p<cEnd;p++) {
    948:       PetscInt i,closureSize,*closure = NULL;

    950:       if (PetscUnlikely(!PetscBTLookup(pown,p-cStart))) continue;
    951:       DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);
    952:       for (i=0;i<closureSize;i++) {
    953:         const PetscInt pp = closure[2*i];

    955:         if (pp >= vStart && pp < vEnd) {
    956:           PetscBTSet(vown,pp-vStart);
    957:         }
    958:       }
    959:       DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);
    960:     }
    961:   }

    963:   if (parentSection) {
    964:     PetscInt vp,gvp;

    966:     for (vp=0,p=vStart;p<vEnd;p++) {
    967:       DMLabel  dlabel;
    968:       PetscInt parent,depth;

    970:       if (PetscUnlikely(vown && !PetscBTLookup(vown,p-vStart))) continue;
    971:       DMPlexGetDepthLabel(dm,&dlabel);
    972:       DMLabelGetValue(dlabel,p,&depth);
    973:       DMPlexGetTreeParent(dm,p,&parent,NULL);
    974:       if (parent != p) vp++;
    975:     }
    976:     MPIU_Allreduce(&vp,&gvp,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)dm));
    977:     if (gvp) {
    978:       PetscInt  maxsupp;
    979:       PetscBool *skip = NULL;

    981:       PetscViewerASCIIPrintf(viewer,"\nvertex_parents\n");
    982:       PetscViewerASCIIPrintf(viewer,"%D\n",vp);
    983:       DMPlexGetMaxSizes(dm,NULL,&maxsupp);
    984:       PetscMalloc1(maxsupp,&skip);
    985:       for (p=vStart;p<vEnd;p++) {
    986:         DMLabel  dlabel;
    987:         PetscInt parent;

    989:         if (PetscUnlikely(vown && !PetscBTLookup(vown,p-vStart))) continue;
    990:         DMPlexGetDepthLabel(dm,&dlabel);
    991:         DMPlexGetTreeParent(dm,p,&parent,NULL);
    992:         if (parent != p) {
    993:           PetscInt       vids[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* silent overzealous clang static analyzer */
    994:           PetscInt       i,nv,ssize,n,numChildren,depth = -1;
    995:           const PetscInt *children;

    997:           DMPlexGetConeSize(dm,parent,&ssize);
    998:           switch (ssize) {
    999:             case 2: /* edge */
    1000:               nv   = 0;
    1001:               DMPlexGetPointMFEMVertexIDs_Internal(dm,parent,localized ? coordSection : NULL,&nv,vids);
    1002:               PetscViewerASCIIPrintf(viewer,"%D",p-vStart);
    1003:               for (i=0;i<nv;i++) {
    1004:                 PetscViewerASCIIPrintf(viewer," %D",vids[i]);
    1005:               }
    1006:               PetscViewerASCIIPrintf(viewer,"\n");
    1007:               vp--;
    1008:               break;
    1009:             case 4: /* face */
    1010:               DMPlexGetTreeChildren(dm,parent,&numChildren,&children);
    1011:               for (n=0;n<numChildren;n++) {
    1012:                 DMLabelGetValue(dlabel,children[n],&depth);
    1013:                 if (!depth) {
    1014:                   const PetscInt *hvsupp,*hesupp,*cone;
    1015:                   PetscInt       hvsuppSize,hesuppSize,coneSize;
    1016:                   PetscInt       hv = children[n],he = -1,f;

    1018:                   PetscArrayzero(skip,maxsupp);
    1019:                   DMPlexGetSupportSize(dm,hv,&hvsuppSize);
    1020:                   DMPlexGetSupport(dm,hv,&hvsupp);
    1021:                   for (i=0;i<hvsuppSize;i++) {
    1022:                     PetscInt ep;
    1023:                     DMPlexGetTreeParent(dm,hvsupp[i],&ep,NULL);
    1024:                     if (ep != hvsupp[i]) {
    1025:                       he = hvsupp[i];
    1026:                     } else {
    1027:                       skip[i] = PETSC_TRUE;
    1028:                     }
    1029:                   }
    1030:                   if (he == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vertex %D support size %D: hanging edge not found",hv,hvsuppSize);
    1031:                   DMPlexGetCone(dm,he,&cone);
    1032:                   vids[0] = (cone[0] == hv) ? cone[1] : cone[0];
    1033:                   DMPlexGetSupportSize(dm,he,&hesuppSize);
    1034:                   DMPlexGetSupport(dm,he,&hesupp);
    1035:                   for (f=0;f<hesuppSize;f++) {
    1036:                     PetscInt j;

    1038:                     DMPlexGetCone(dm,hesupp[f],&cone);
    1039:                     DMPlexGetConeSize(dm,hesupp[f],&coneSize);
    1040:                     for (j=0;j<coneSize;j++) {
    1041:                       PetscInt k;
    1042:                       for (k=0;k<hvsuppSize;k++) {
    1043:                         if (hvsupp[k] == cone[j]) {
    1044:                           skip[k] = PETSC_TRUE;
    1045:                           break;
    1046:                         }
    1047:                       }
    1048:                     }
    1049:                   }
    1050:                   for (i=0;i<hvsuppSize;i++) {
    1051:                     if (!skip[i]) {
    1052:                       DMPlexGetCone(dm,hvsupp[i],&cone);
    1053:                       vids[1] = (cone[0] == hv) ? cone[1] : cone[0];
    1054:                     }
    1055:                   }
    1056:                   PetscViewerASCIIPrintf(viewer,"%D",hv-vStart);
    1057:                   for (i=0;i<2;i++) {
    1058:                     PetscViewerASCIIPrintf(viewer," %D",vids[i]-vStart);
    1059:                   }
    1060:                   PetscViewerASCIIPrintf(viewer,"\n");
    1061:                   vp--;
    1062:                 }
    1063:               }
    1064:               break;
    1065:             default:
    1066:               SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Don't know how to deal with support size %D",ssize);
    1067:           }
    1068:         }
    1069:       }
    1070:       PetscFree(skip);
    1071:     }
    1072:     if (vp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D hanging vertices",vp);
    1073:   }
    1074:   PetscBTDestroy(&pown);
    1075:   PetscBTDestroy(&vown);

    1077:   /* vertices */
    1078:   if (hovec) { /* higher-order meshes */
    1079:     const char *fec;
    1080:     PetscInt   i,n,s;

    1082:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
    1083:     PetscViewerASCIIPrintf(viewer,"%D\n",vEnd-vStart);
    1084:     PetscViewerASCIIPrintf(viewer,"nodes\n");
    1085:     PetscObjectGetName((PetscObject)hovec,&fec);
    1086:     PetscViewerASCIIPrintf(viewer,"FiniteElementSpace\n");
    1087:     PetscViewerASCIIPrintf(viewer,"%s\n",fec);
    1088:     PetscViewerASCIIPrintf(viewer,"VDim: %D\n",sdim);
    1089:     PetscViewerASCIIPrintf(viewer,"Ordering: 1\n\n"); /*Ordering::byVDIM*/
    1090:     VecGetArrayRead(hovec,&array);
    1091:     VecGetLocalSize(hovec,&n);
    1092:     if (n%sdim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Size of local coordinate vector %D incompatible with space dimension %D",n,sdim);
    1093:     for (i=0;i<n/sdim;i++) {
    1094:       for (s=0;s<sdim;s++) {
    1095:         PetscViewerASCIIPrintf(viewer,fmt,PetscRealPart(array[i*sdim+s]));
    1096:       }
    1097:       PetscViewerASCIIPrintf(viewer,"\n");
    1098:     }
    1099:     VecRestoreArrayRead(hovec,&array);
    1100:   } else {
    1101:     VecGetLocalSize(coordinates,&nvert);
    1102:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
    1103:     PetscViewerASCIIPrintf(viewer,"%D\n",nvert/sdim);
    1104:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
    1105:     VecGetArrayRead(coordinates,&array);
    1106:     for (p=0;p<nvert/sdim;p++) {
    1107:       PetscInt s;
    1108:       for (s=0;s<sdim;s++) {
    1109:         PetscReal v = PetscRealPart(array[p*sdim+s]);

    1111:         PetscViewerASCIIPrintf(viewer,fmt,PetscIsInfOrNanReal(v) ? 0.0 : v);
    1112:       }
    1113:       PetscViewerASCIIPrintf(viewer,"\n");
    1114:     }
    1115:     VecRestoreArrayRead(coordinates,&array);
    1116:   }
    1117:   VecDestroy(&hovec);
    1118:   return(0);
    1119: }

    1121: PetscErrorCode DMPlexView_GLVis(DM dm, PetscViewer viewer)
    1122: {
    1125:   DMView_GLVis(dm,viewer,DMPlexView_GLVis_ASCII);
    1126:   return(0);
    1127: }