Actual source code: plexglvis.c

petsc-3.8.3 2017-12-09
Report Typos and Errors
  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;
 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,cEndInterior,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:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
 65:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
 66:   DMPlexGetCellNumbering(dm,&globalNum);
 67:   ISGetIndices(globalNum,&gNum);
 68:   PetscBTCreate(vEnd-vStart,&vown);
 69:   for (c = cStart, totc = 0; c < cEnd; c++) {
 70:     if (gNum[c-cStart] >= 0) {
 71:       PetscInt i,numPoints,*points = NULL;

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

 85:   DMCreateLocalVector(dm,&xlocal);
 86:   VecGetLocalSize(xlocal,&totdofs);
 87:   DMGetDefaultSection(dm,&s);
 88:   PetscSectionGetNumFields(s,&nfields);
 89:   for (f=0,maxfields=0;f<nfields;f++) {
 90:     PetscInt bs;

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

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

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

127:           PetscFEGetNumComponents(fem,&Nc);
128:           PetscFEGetDualSpace(fem,&sp);
129:           PetscDualSpaceGetType(sp,&spname);
130:           PetscStrcmp(spname,PETSCDUALSPACELAGRANGE,&islag);
131:           if (!islag) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Unsupported dual space");
132:           PetscDualSpaceLagrangeGetContinuity(sp,&continuous);
133:           if (!continuous) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Discontinuous space visualization currently unsupported");
134:           PetscDualSpaceGetOrder(sp,&order);
135:           if (continuous && order > 0) {
136:             PetscSNPrintf(fec,64,"FiniteElementCollection: H1_%dD_P1",dim);
137:           } else {
138:             H1   = PETSC_FALSE;
139:             PetscSNPrintf(fec,64,"FiniteElementCollection: L2_%dD_P%d",dim,order);
140:           }
141:           PetscStrallocpy(name,&fieldname[ctx->nf]);
142:           bs[ctx->nf]   = Nc;
143:           dims[ctx->nf] = dim;
144:           if (H1) {
145:             nlocal[ctx->nf] = Nc * Nv;
146:             PetscStrallocpy(fec,&fec_type[ctx->nf]);
147:             VecCreateSeq(PETSC_COMM_SELF,Nv*Nc,&xfield);
148:             for (i=0,cum=0;i<vEnd-vStart;i++) {
149:               PetscInt j,off;

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

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

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

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

208:   /* customize the viewer */
209:   PetscViewerGLVisSetFields(viewer,ctx->nf,(const char**)fieldname,(const char**)fec_type,nlocal,bs,dims,DMPlexSampleGLVisFields_Private,ctx,DestroyGLVisViewerCtx_Private);
210:   for (f=0;f<ctx->nf;f++) {
211:     PetscFree(fieldname[f]);
212:     PetscFree(fec_type[f]);
213:   }
214:   PetscFree6(fieldname,nlocal,bs,dims,fec_type,idxs);
215:   return(0);
216: }

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

220: MFEM_cid mfem_table_cid[4][7] = { {MFEM_POINT,MFEM_UNDEF,MFEM_UNDEF  ,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF, MFEM_UNDEF},
221:                                   {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_UNDEF      ,MFEM_UNDEF, MFEM_UNDEF},
222:                                   {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_TRIANGLE,MFEM_SQUARE     ,MFEM_UNDEF, MFEM_UNDEF},
223:                                   {MFEM_POINT,MFEM_UNDEF,MFEM_SEGMENT,MFEM_UNDEF   ,MFEM_TETRAHEDRON,MFEM_UNDEF, MFEM_CUBE } };

225: static PetscErrorCode DMPlexGetPointMFEMCellID_Internal(DM dm, DMLabel label, PetscInt p, PetscInt *mid, PetscInt *cid)
226: {
227:   DMLabel        dlabel;
228:   PetscInt       depth,csize;

232:   DMPlexGetDepthLabel(dm,&dlabel);
233:   DMLabelGetValue(dlabel,p,&depth);
234:   DMPlexGetConeSize(dm,p,&csize);
235:   if (label) {
236:     DMLabelGetValue(label,p,mid);
237:   } else *mid = 1;
238:   *cid = mfem_table_cid[depth][csize];
239:   return(0);
240: }

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

248:   DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);
249:   DMGetDimension(dm,&dim);
250:   sdim = dim;
251:   if (csec) {
252:     DMGetCoordinateDim(dm,&sdim);
253:     PetscSectionGetOffset(csec,vStart,&off);
254:     off  = off/sdim;
255:     PetscSectionGetDof(csec,p,&dof);
256:   }
257:   if (!dof) {
258:     DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);
259:     for (i=0,q=0;i<numPoints*2;i+= 2)
260:       if ((points[i] >= vStart) && (points[i] < vEnd))
261:         vids[q++] = points[i]-vStart+off;
262:     DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);
263:   } else {
264:     PetscSectionGetOffset(csec,p,&off);
265:     PetscSectionGetDof(csec,p,&dof);
266:     for (q=0;q<dof/sdim;q++) vids[q] = off/sdim + q;
267:   }
268:   *nv  = q;
269:   return(0);
270: }

272: /*
273:    ASCII visualization/dump: full support for simplices and tensor product cells. It supports AMR
274:    Higher order meshes are also supported
275: */
276: static PetscErrorCode DMPlexView_GLVis_ASCII(DM dm, PetscViewer viewer)
277: {
278:   DMLabel              label;
279:   PetscSection         coordSection,parentSection;
280:   Vec                  coordinates,hovec;
281:   IS                   globalNum = NULL;
282:   const PetscScalar    *array;
283:   const PetscInt       *gNum;
284:   PetscInt             bf,p,sdim,dim,depth,novl;
285:   PetscInt             cStart,cEnd,cEndInterior,vStart,vEnd,nvert;
286:   PetscMPIInt          commsize;
287:   PetscBool            localized,ovl,isascii,enable_boundary,enable_ncmesh;
288:   PetscBT              pown;
289:   PetscErrorCode       ierr;
290:   PetscContainer       glvis_container;
291:   PetscBool            periodic, enabled = PETSC_TRUE;
292:   const char           *fmt;

297:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
298:   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERASCII");
299:   MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&commsize);
300:   if (commsize > 1) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Use single sequential viewers for parallel visualization");
301:   DMGetDimension(dm,&dim);
302:   DMPlexGetDepth(dm,&depth);
303:   if (depth >= 0 && dim != depth) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");

305:   /* get container: determines if a process visualizes is portion of the data or not */
306:   PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&glvis_container);
307:   if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis container");
308:   {
309:     PetscViewerGLVisInfo glvis_info;
310:     PetscContainerGetPointer(glvis_container,(void**)&glvis_info);
311:     enabled = glvis_info->enabled;
312:     fmt     = glvis_info->fmt;
313:   }
314:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
315:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
316:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
317:   DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);
318:   DMGetPeriodicity(dm,&periodic,NULL,NULL,NULL);
319:   DMGetCoordinatesLocalized(dm,&localized);
320:   if (periodic && !localized) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Coordinates need to be localized");
321:   DMGetCoordinateSection(dm,&coordSection);
322:   DMGetCoordinateDim(dm,&sdim);
323:   DMGetCoordinatesLocal(dm,&coordinates);

325:   /* Users can attach a coordinate vector to the DM in case they have a higher-order mesh
326:      DMPlex does not currently support HO meshes, so there's no API for this */
327:   PetscObjectQuery((PetscObject)dm,"_glvis_mesh_coords",(PetscObject*)&hovec);

329:   /*
330:      a couple of sections of the mesh specification are disabled
331:        - boundary: unless we want to visualize boundary attributes or we have a periodic mesh
332:                    the boundary is not needed for proper mesh visualization
333:        - vertex_parents: used for non-conforming meshes only when we want to use MFEM as a discretization package
334:                          and be able to derefine the mesh
335:   */
336:   enable_boundary = periodic;
337:   enable_ncmesh = PETSC_FALSE;
338:   PetscOptionsBegin(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,"GLVis PetscViewer DMPlex Options","PetscViewer");
339:   PetscOptionsBool("-viewer_glvis_dm_plex_enable_boundary","Enable boundary section in mesh representation; useful for debugging purposes",NULL,enable_boundary,&enable_boundary,NULL);
340:   PetscOptionsBool("-viewer_glvis_dm_plex_enable_ncmesh","Enable vertex_parents section in mesh representation; useful for debugging purposes",NULL,enable_ncmesh,&enable_ncmesh,NULL);
341:   PetscOptionsEnd();

343:   /* Identify possible cells in the overlap */
344:   gNum = NULL;
345:   novl = 0;
346:   ovl  = PETSC_FALSE;
347:   pown = NULL;
348:   MPI_Comm_size(PetscObjectComm((PetscObject)dm),&commsize);
349:   if (commsize > 1) {
350:     DMPlexGetCellNumbering(dm,&globalNum);
351:     ISGetIndices(globalNum,&gNum);
352:     for (p=cStart; p<cEnd; p++) {
353:       if (gNum[p-cStart] < 0) {
354:         ovl = PETSC_TRUE;
355:         novl++;
356:       }
357:     }
358:     if (ovl) {
359:       /* it may happen that pown get not destroyed, if the user closes the window while this function is running.
360:          TODO: garbage collector? attach pown to dm?  */
361:       PetscBTCreate(PetscMax(cEnd-cStart,vEnd-vStart),&pown);
362:     }
363:   }

365:   if (!enabled) {
366:     PetscViewerASCIIPrintf(viewer,"MFEM mesh v1.1\n");
367:     PetscViewerASCIIPrintf(viewer,"\ndimension\n");
368:     PetscViewerASCIIPrintf(viewer,"%D\n",dim);
369:     PetscViewerASCIIPrintf(viewer,"\nelements\n");
370:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
371:     PetscViewerASCIIPrintf(viewer,"\nboundary\n");
372:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
373:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
374:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
375:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
376:     PetscBTDestroy(&pown);
377:     if (globalNum) {
378:       ISRestoreIndices(globalNum,&gNum);
379:     }
380:     return(0);
381:   }

383:   /* header */
384:   PetscViewerASCIIPrintf(viewer,"MFEM mesh v1.1\n");

386:   /* topological dimension */
387:   PetscViewerASCIIPrintf(viewer,"\ndimension\n");
388:   PetscViewerASCIIPrintf(viewer,"%D\n",dim);

390:   /* elements */
391:   /* TODO: is this the label we want to use for marking material IDs?
392:      We should decide to have a single marker for these stuff
393:      Proposal: DMSetMaterialLabel?
394:   */
395:   DMGetLabel(dm,"Cell Sets",&label);
396:   PetscViewerASCIIPrintf(viewer,"\nelements\n");
397:   PetscViewerASCIIPrintf(viewer,"%D\n",cEnd-cStart-novl);
398:   for (p=cStart;p<cEnd;p++) {
399:     int      vids[8];
400:     PetscInt i,nv = 0,cid = -1,mid = 1;

402:     if (ovl) {
403:       if (gNum[p-cStart] < 0) continue;
404:       else {
405:         PetscBTSet(pown,p-cStart);
406:       }
407:     }
408:     DMPlexGetPointMFEMCellID_Internal(dm,label,p,&mid,&cid);
409:     DMPlexGetPointMFEMVertexIDs_Internal(dm,p,(localized && !hovec) ? coordSection : NULL,&nv,vids);
410:     DMPlexInvertCell(dim,nv,vids);
411:     PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);
412:     for (i=0;i<nv;i++) {
413:       PetscViewerASCIIPrintf(viewer," %d",vids[i]);
414:     }
415:     PetscViewerASCIIPrintf(viewer,"\n");
416:   }

418:   /* boundary */
419:   PetscViewerASCIIPrintf(viewer,"\nboundary\n");
420:   if (!enable_boundary) {
421:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
422:   } else {
423:     DMLabel  perLabel;
424:     PetscBT  bfaces;
425:     PetscInt fStart,fEnd,fEndInterior,*fcells;
426:     PetscInt *faces = NULL,fpc = 0,vpf = 0, vpc = 0;
427:     PetscInt fv1[]     = {0,1},
428:              fv2tri[]  = {0,1,
429:                           1,2,
430:                           2,0},
431:              fv2quad[] = {0,1,
432:                           1,2,
433:                           2,3,
434:                           3,0},
435:              fv3tet[]  = {0,1,2,
436:                           0,3,1,
437:                           0,2,3,
438:                           2,1,3},
439:              fv3hex[]  = {0,1,2,3,
440:                        4,5,6,7,
441:                        0,3,5,4,
442:                        2,1,7,6,
443:                        3,2,6,5,
444:                        0,4,7,1};

446:     /* determine orientation of boundary mesh */
447:     if (cEnd-cStart) {
448:       DMPlexGetConeSize(dm,cStart,&fpc);
449:       switch(dim) {
450:         case 1:
451:           if (fpc != 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case faces per cell %D",fpc);
452:           faces = fv1;
453:           vpf = 1;
454:           vpc = 2;
455:           break;
456:         case 2:
457:           switch (fpc) {
458:             case 3:
459:               faces = fv2tri;
460:               vpf   = 2;
461:               vpc   = 3;
462:               break;
463:             case 4:
464:               faces = fv2quad;
465:               vpf   = 2;
466:               vpc   = 4;
467:               break;
468:             default:
469:               SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: faces per cell %D",fpc);
470:               break;
471:           }
472:           break;
473:         case 3:
474:           switch (fpc) {
475:             case 4:
476:               faces = fv3tet;
477:               vpf   = 3;
478:               vpc   = 4;
479:               break;
480:             case 6:
481:               faces = fv3hex;
482:               vpf   = 4;
483:               vpc   = 8;
484:               break;
485:             default:
486:               SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled case: faces per cell %D",fpc);
487:               break;
488:           }
489:           break;
490:         default:
491:           SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Unhandled dim");
492:           break;
493:       }
494:     }
495:     DMPlexGetHybridBounds(dm,NULL,&fEndInterior,NULL,NULL);
496:     DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);
497:     fEnd = fEndInterior < 0 ? fEnd : fEndInterior;
498:     PetscBTCreate(fEnd-fStart,&bfaces);
499:     DMPlexGetMaxSizes(dm,NULL,&p);
500:     PetscMalloc1(p,&fcells);
501:     DMGetLabel(dm,"glvis_periodic_cut",&perLabel);
502:     if (!perLabel && localized) { /* this periodic cut can be moved up to DMPlex setup */
503:       DMCreateLabel(dm,"glvis_periodic_cut");
504:       DMGetLabel(dm,"glvis_periodic_cut",&perLabel);
505:       DMLabelSetDefaultValue(perLabel,1);
506:       for (p=cStart;p<cEnd;p++) {
507:         PetscInt dof;
508:         PetscSectionGetDof(coordSection,p,&dof);
509:         if (dof) {
510:           PetscInt    v,csize,cellClosureSize,*cellClosure = NULL,*vidxs = NULL;
511:           PetscScalar *vals = NULL;

513:           if (dof%sdim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Incompatible number of cell dofs %D and space dimension %D",dof,sdim);
514:           if (dof/sdim != vpc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Incompatible number of cell dofs %D and vertices %D",dof/sdim,vpc);
515:           DMPlexVecGetClosure(dm,coordSection,coordinates,p,&csize,&vals);
516:           DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);
517:           for (v=0;v<cellClosureSize;v++)
518:             if (cellClosure[2*v] >= vStart && cellClosure[2*v] < vEnd) {
519:               vidxs = cellClosure + 2*v;
520:               break;
521:             }
522:           if (!vidxs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing vertices");
523:           for (v=0;v<vpc;v++) {
524:             PetscInt s;
525:             for (s=0;s<sdim;s++) {
526:               if (PetscAbsScalar(vals[v*sdim+s]-vals[v*sdim+s+vpc*sdim])>PETSC_MACHINE_EPSILON) {
527:                 DMLabelSetValue(perLabel,vidxs[2*v],2);
528:               }
529:             }
530:           }
531:           DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);
532:           DMPlexVecRestoreClosure(dm,coordSection,coordinates,p,&csize,&vals);
533:         }
534:       }
535:       if (dim > 1) {
536:         PetscInt eEnd,eStart,eEndInterior;
537:         DMPlexGetHybridBounds(dm,NULL,NULL,&eEndInterior,NULL);
538:         DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);
539:         eEnd = eEndInterior < 0 ? eEnd : eEndInterior;
540:         for (p=eStart;p<eEnd;p++) {
541:           const PetscInt *cone;
542:           PetscInt       coneSize,i;
543:           PetscBool      ispe = PETSC_TRUE;

545:           DMPlexGetCone(dm,p,&cone);
546:           DMPlexGetConeSize(dm,p,&coneSize);
547:           for (i=0;i<coneSize;i++) {
548:             PetscInt v;

550:             DMLabelGetValue(perLabel,cone[i],&v);
551:             ispe = (PetscBool)(ispe && (v==2));
552:           }
553:           if (ispe && coneSize) {
554:             DMLabelSetValue(perLabel,p,2);
555:           }
556:         }
557:         if (dim > 2) {
558:           for (p=fStart;p<fEnd;p++) {
559:             const PetscInt *cone;
560:             PetscInt       coneSize,i;
561:             PetscBool      ispe = PETSC_TRUE;

563:             DMPlexGetCone(dm,p,&cone);
564:             DMPlexGetConeSize(dm,p,&coneSize);
565:             for (i=0;i<coneSize;i++) {
566:               PetscInt v;

568:               DMLabelGetValue(perLabel,cone[i],&v);
569:               ispe = (PetscBool)(ispe && (v==2));
570:             }
571:             if (ispe && coneSize) {
572:               DMLabelSetValue(perLabel,p,2);
573:             }
574:           }
575:         }
576:       }
577:     }
578:     for (p=fStart;p<fEnd;p++) {
579:       const PetscInt *support;
580:       PetscInt       supportSize;
581:       PetscBool      isbf = PETSC_FALSE;

583:       DMPlexGetSupportSize(dm,p,&supportSize);
584:       if (ovl) {
585:         PetscBool has_owned = PETSC_FALSE, has_ghost = PETSC_FALSE;
586:         PetscInt  i;

588:         DMPlexGetSupport(dm,p,&support);
589:         for (i=0;i<supportSize;i++) {
590:           if (PetscLikely(PetscBTLookup(pown,support[i]-cStart))) has_owned = PETSC_TRUE;
591:           else has_ghost = PETSC_TRUE;
592:         }
593:         isbf = (PetscBool)((supportSize == 1 && has_owned) || (supportSize > 1 && has_owned && has_ghost));
594:       } else {
595:         isbf = (PetscBool)(supportSize == 1);
596:       }
597:       if (!isbf && perLabel) {
598:         const PetscInt *cone;
599:         PetscInt       coneSize,i;

601:         DMPlexGetCone(dm,p,&cone);
602:         DMPlexGetConeSize(dm,p,&coneSize);
603:         isbf = PETSC_TRUE;
604:         for (i=0;i<coneSize;i++) {
605:           PetscInt v,d;

607:           DMLabelGetValue(perLabel,cone[i],&v);
608:           DMLabelGetDefaultValue(perLabel,&d);
609:           isbf = (PetscBool)(isbf && v != d);
610:         }
611:       }
612:       if (isbf) {
613:         PetscBTSet(bfaces,p-fStart);
614:       }
615:     }
616:     /* count boundary faces */
617:     for (p=fStart,bf=0;p<fEnd;p++) {
618:       if (PetscUnlikely(PetscBTLookup(bfaces,p-fStart))) {
619:         const PetscInt *support;
620:         PetscInt       supportSize,c;

622:         DMPlexGetSupportSize(dm,p,&supportSize);
623:         DMPlexGetSupport(dm,p,&support);
624:         if (ovl) {
625:           for (c=0;c<supportSize;c++) {
626:             if (PetscLikely(PetscBTLookup(pown,support[c]-cStart))) {
627:               bf++;
628:             }
629:           }
630:         } else bf += supportSize;
631:       }
632:     }
633:     /* TODO: is this the label we want to use for marking boundary facets?
634:        We should decide to have a single marker for these stuff
635:        Proposal: DMSetBoundaryLabel?
636:     */
637:     DMGetLabel(dm,"marker",&label);
638:     PetscViewerASCIIPrintf(viewer,"%D\n",bf);
639:     for (p=fStart;p<fEnd;p++) {
640:       if (PetscUnlikely(PetscBTLookup(bfaces,p-fStart))) {
641:         const PetscInt *support;
642:         PetscInt       supportSize,c,nc = 0;

644:         DMPlexGetSupportSize(dm,p,&supportSize);
645:         DMPlexGetSupport(dm,p,&support);
646:         if (ovl) {
647:           for (c=0;c<supportSize;c++) {
648:             if (PetscLikely(PetscBTLookup(pown,support[c]-cStart))) {
649:               fcells[nc++] = support[c];
650:             }
651:           }
652:         } else for (c=0;c<supportSize;c++) fcells[nc++] = support[c];
653:         for (c=0;c<nc;c++) {
654:           const    PetscInt *cone;
655:           int      vids[8];
656:           PetscInt i,cell,cl,nv,cid = -1,mid = -1;

658:           cell = fcells[c];
659:           DMPlexGetCone(dm,cell,&cone);
660:           for (cl=0;cl<fpc;cl++)
661:             if (cone[cl] == p)
662:               break;

664:           /* face material id and type */
665:           DMPlexGetPointMFEMCellID_Internal(dm,label,p,&mid,&cid);
666:           PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);
667:           /* vertex ids */
668:           DMPlexGetPointMFEMVertexIDs_Internal(dm,cell,(localized && !hovec) ? coordSection : NULL,&nv,vids);
669:           for (i=0;i<vpf;i++) {
670:             PetscViewerASCIIPrintf(viewer," %D",vids[faces[cl*vpf+i]]);
671:           }
672:           PetscViewerASCIIPrintf(viewer,"\n");
673:         }
674:         bf = bf-nc;
675:       }
676:     }
677:     if (bf) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Remaining boundary faces %D",bf);
678:     PetscBTDestroy(&bfaces);
679:     PetscFree(fcells);
680:   }

682:   /* mark owned vertices */
683:   if (ovl) {
684:     PetscBTMemzero(vEnd-vStart,pown);
685:     for (p=cStart;p<cEnd;p++) {
686:       PetscInt i,closureSize,*closure = NULL;

688:       if (gNum[p-cStart] < 0) continue;
689:       DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);
690:       for (i=0;i<closureSize;i++) {
691:         const PetscInt pp = closure[2*i];

693:         if (pp >= vStart && pp < vEnd) {
694:           PetscBTSet(pown,pp-vStart);
695:         }
696:       }
697:       DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);
698:     }
699:   }
700:   if (globalNum) {
701:     ISRestoreIndices(globalNum,&gNum);
702:   }

704:   /* vertex_parents (Non-conforming meshes) */
705:   parentSection  = NULL;
706:   if (enable_ncmesh) {
707:     DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);
708:   }
709:   if (parentSection) {
710:     PetscInt vp,gvp;

712:     for (vp=0,p=vStart;p<vEnd;p++) {
713:       DMLabel  dlabel;
714:       PetscInt parent,depth;

716:       if (PetscUnlikely(ovl && !PetscBTLookup(pown,p-vStart))) continue;
717:       DMPlexGetDepthLabel(dm,&dlabel);
718:       DMLabelGetValue(dlabel,p,&depth);
719:       DMPlexGetTreeParent(dm,p,&parent,NULL);
720:       if (parent != p) vp++;
721:     }
722:     MPIU_Allreduce(&vp,&gvp,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)dm));
723:     if (gvp) {
724:       PetscInt  maxsupp;
725:       PetscBool *skip = NULL;

727:       PetscViewerASCIIPrintf(viewer,"\nvertex_parents\n");
728:       PetscViewerASCIIPrintf(viewer,"%D\n",vp);
729:       DMPlexGetMaxSizes(dm,NULL,&maxsupp);
730:       PetscMalloc1(maxsupp,&skip);
731:       for (p=vStart;p<vEnd;p++) {
732:         DMLabel  dlabel;
733:         PetscInt parent;

735:         if (PetscUnlikely(ovl && !PetscBTLookup(pown,p-vStart))) continue;
736:         DMPlexGetDepthLabel(dm,&dlabel);
737:         DMPlexGetTreeParent(dm,p,&parent,NULL);
738:         if (parent != p) {
739:           int            vids[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* silent overzealous clang static analyzer */
740:           PetscInt       i,nv,size,n,numChildren,depth = -1;
741:           const PetscInt *children;
742:           DMPlexGetConeSize(dm,parent,&size);
743:           switch (size) {
744:             case 2: /* edge */
745:               nv   = 0;
746:               DMPlexGetPointMFEMVertexIDs_Internal(dm,parent,localized ? coordSection : NULL,&nv,vids);
747:               DMPlexInvertCell(dim,nv,vids);
748:               PetscViewerASCIIPrintf(viewer,"%D",p-vStart);
749:               for (i=0;i<nv;i++) {
750:                 PetscViewerASCIIPrintf(viewer," %d",vids[i]);
751:               }
752:               PetscViewerASCIIPrintf(viewer,"\n");
753:               vp--;
754:               break;
755:             case 4: /* face */
756:               DMPlexGetTreeChildren(dm,parent,&numChildren,&children);
757:               for (n=0;n<numChildren;n++) {
758:                 DMLabelGetValue(dlabel,children[n],&depth);
759:                 if (!depth) {
760:                   const PetscInt *hvsupp,*hesupp,*cone;
761:                   PetscInt       hvsuppSize,hesuppSize,coneSize;
762:                   PetscInt       hv = children[n],he = -1,f;

764:                   PetscMemzero(skip,maxsupp*sizeof(PetscBool));
765:                   DMPlexGetSupportSize(dm,hv,&hvsuppSize);
766:                   DMPlexGetSupport(dm,hv,&hvsupp);
767:                   for (i=0;i<hvsuppSize;i++) {
768:                     PetscInt ep;
769:                     DMPlexGetTreeParent(dm,hvsupp[i],&ep,NULL);
770:                     if (ep != hvsupp[i]) {
771:                       he = hvsupp[i];
772:                     } else {
773:                       skip[i] = PETSC_TRUE;
774:                     }
775:                   }
776:                   if (he == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vertex %D support size %D: hanging edge not found",hv,hvsuppSize);
777:                   DMPlexGetCone(dm,he,&cone);
778:                   vids[0] = (int)((cone[0] == hv) ? cone[1] : cone[0]);
779:                   DMPlexGetSupportSize(dm,he,&hesuppSize);
780:                   DMPlexGetSupport(dm,he,&hesupp);
781:                   for (f=0;f<hesuppSize;f++) {
782:                     PetscInt j;

784:                     DMPlexGetCone(dm,hesupp[f],&cone);
785:                     DMPlexGetConeSize(dm,hesupp[f],&coneSize);
786:                     for (j=0;j<coneSize;j++) {
787:                       PetscInt k;
788:                       for (k=0;k<hvsuppSize;k++) {
789:                         if (hvsupp[k] == cone[j]) {
790:                           skip[k] = PETSC_TRUE;
791:                           break;
792:                         }
793:                       }
794:                     }
795:                   }
796:                   for (i=0;i<hvsuppSize;i++) {
797:                     if (!skip[i]) {
798:                       DMPlexGetCone(dm,hvsupp[i],&cone);
799:                       vids[1] = (int)((cone[0] == hv) ? cone[1] : cone[0]);
800:                     }
801:                   }
802:                   PetscViewerASCIIPrintf(viewer,"%D",hv-vStart);
803:                   for (i=0;i<2;i++) {
804:                     PetscViewerASCIIPrintf(viewer," %d",vids[i]-vStart);
805:                   }
806:                   PetscViewerASCIIPrintf(viewer,"\n");
807:                   vp--;
808:                 }
809:               }
810:               break;
811:             default:
812:               SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Don't know how to deal with support size %d",size);
813:           }
814:         }
815:       }
816:       PetscFree(skip);
817:     }
818:     if (vp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D hanging vertices",vp);
819:   }
820:   PetscBTDestroy(&pown);

822:   /* vertices */
823:   if (hovec) { /* higher-order meshes */
824:     const char *fec;
825:     PetscInt   i,n;

827:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
828:     PetscViewerASCIIPrintf(viewer,"%D\n",vEnd-vStart);
829:     PetscViewerASCIIPrintf(viewer,"nodes\n");
830:     PetscObjectGetName((PetscObject)hovec,&fec);
831:     PetscViewerASCIIPrintf(viewer,"FiniteElementSpace\n");
832:     PetscViewerASCIIPrintf(viewer,"%s\n",fec);
833:     PetscViewerASCIIPrintf(viewer,"VDim: %D\n",sdim);
834:     PetscViewerASCIIPrintf(viewer,"Ordering: 1\n\n"); /*Ordering::byVDIM*/
835:     VecGetArrayRead(hovec,&array);
836:     VecGetLocalSize(hovec,&n);
837:     if (n%sdim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Size of local coordinate vector %D incompatible with space dimension %D",n,sdim);
838:     for (i=0;i<n/sdim;i++) {
839:       PetscInt s;
840:       for (s=0;s<sdim;s++) {
841:         PetscViewerASCIIPrintf(viewer,fmt,PetscRealPart(array[i*sdim+s]));
842:       }
843:       PetscViewerASCIIPrintf(viewer,"\n");
844:     }
845:     VecRestoreArrayRead(hovec,&array);
846:   } else {
847:     VecGetLocalSize(coordinates,&nvert);
848:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
849:     PetscViewerASCIIPrintf(viewer,"%D\n",nvert/sdim);
850:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
851:     VecGetArrayRead(coordinates,&array);
852:     for (p=0;p<nvert/sdim;p++) {
853:       PetscInt s;
854:       for (s=0;s<sdim;s++) {
855:         PetscViewerASCIIPrintf(viewer,fmt,PetscRealPart(array[p*sdim+s]));
856:       }
857:       PetscViewerASCIIPrintf(viewer,"\n");
858:     }
859:     VecRestoreArrayRead(coordinates,&array);
860:   }
861:   return(0);
862: }

864: /* dispatching, prints through the socket by prepending the mesh keyword to the usual ASCII dump */
865: PETSC_INTERN PetscErrorCode DMPlexView_GLVis(DM dm, PetscViewer viewer)
866: {
868:   PetscBool      isglvis,isascii;

873:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERGLVIS,&isglvis);
874:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
875:   if (!isglvis && !isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERGLVIS or VIEWERASCII");
876:   if (isglvis) {
877:     PetscViewer          view;
878:     PetscViewerGLVisType type;

880:     PetscViewerGLVisGetType_Private(viewer,&type);
881:     PetscViewerGLVisGetDMWindow_Private(viewer,&view);
882:     if (view) { /* in the socket case, it may happen that the connection failed */
883:       if (type == PETSC_VIEWER_GLVIS_SOCKET) {
884:         PetscMPIInt size,rank;
885:         MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);
886:         MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);
887:         PetscViewerASCIIPrintf(view,"parallel %D %D\nmesh\n",size,rank);
888:       }
889:       DMPlexView_GLVis_ASCII(dm,view);
890:       PetscViewerFlush(view);
891:       if (type == PETSC_VIEWER_GLVIS_SOCKET) {
892:         PetscInt    dim;
893:         const char* name;

895:         PetscObjectGetName((PetscObject)dm,&name);
896:         DMGetDimension(dm,&dim);
897:         PetscViewerGLVisInitWindow_Private(view,PETSC_TRUE,dim,name);
898:         PetscBarrier((PetscObject)dm);
899:       }
900:     }
901:   } else {
902:     DMPlexView_GLVis_ASCII(dm,viewer);
903:   }
904:   return(0);
905: }