xref: /petsc/src/dm/impls/plex/plexglvis.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
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>
8 
9 typedef struct {
10   PetscInt   nf;
11   VecScatter *scctx;
12 } GLVisViewerCtx;
13 
14 static PetscErrorCode DestroyGLVisViewerCtx_Private(void *vctx)
15 {
16   GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx;
17   PetscInt       i;
18   PetscErrorCode ierr;
19 
20   PetscFunctionBegin;
21   for (i=0;i<ctx->nf;i++) {
22     ierr = VecScatterDestroy(&ctx->scctx[i]);CHKERRQ(ierr);
23   }
24   ierr = PetscFree(ctx->scctx);CHKERRQ(ierr);
25   ierr = PetscFree(vctx);CHKERRQ(ierr);
26   PetscFunctionReturn(0);
27 }
28 
29 static PetscErrorCode DMPlexSampleGLVisFields_Private(PetscObject oX, PetscInt nf, PetscObject oXfield[], void *vctx)
30 {
31   GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx;
32   PetscInt       f;
33   PetscErrorCode ierr;
34 
35   PetscFunctionBegin;
36   for (f=0;f<nf;f++) {
37     ierr = VecScatterBegin(ctx->scctx[f],(Vec)oX,(Vec)oXfield[f],INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
38     ierr = VecScatterEnd(ctx->scctx[f],(Vec)oX,(Vec)oXfield[f],INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
39   }
40   PetscFunctionReturn(0);
41 }
42 
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;
58   PetscErrorCode ierr;
59 
60   PetscFunctionBegin;
61   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
62   ierr = DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);CHKERRQ(ierr);
63   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
64   ierr = PetscObjectQuery((PetscObject)dm,"_glvis_plex_gnum",(PetscObject*)&globalNum);CHKERRQ(ierr);
65   if (!globalNum) {
66     ierr = DMPlexCreateCellNumbering_Internal(dm,PETSC_TRUE,&globalNum);CHKERRQ(ierr);
67     ierr = PetscObjectCompose((PetscObject)dm,"_glvis_plex_gnum",(PetscObject)globalNum);CHKERRQ(ierr);
68     ierr = PetscObjectDereference((PetscObject)globalNum);CHKERRQ(ierr);
69   }
70   ierr = ISGetIndices(globalNum,&gNum);CHKERRQ(ierr);
71   ierr = PetscBTCreate(vEnd-vStart,&vown);CHKERRQ(ierr);
72   for (c = cStart, totc = 0; c < cEnd; c++) {
73     if (gNum[c-cStart] >= 0) {
74       PetscInt i,numPoints,*points = NULL;
75 
76       totc++;
77       ierr = DMPlexGetTransitiveClosure(dm,c,PETSC_TRUE,&numPoints,&points);CHKERRQ(ierr);
78       for (i=0;i<numPoints*2;i+= 2) {
79         if ((points[i] >= vStart) && (points[i] < vEnd)) {
80           ierr = PetscBTSet(vown,points[i]-vStart);CHKERRQ(ierr);
81         }
82       }
83       ierr = DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&numPoints,&points);CHKERRQ(ierr);
84     }
85   }
86   for (f=0,Nv=0;f<vEnd-vStart;f++) if (PetscLikely(PetscBTLookup(vown,f))) Nv++;
87 
88   ierr = DMCreateLocalVector(dm,&xlocal);CHKERRQ(ierr);
89   ierr = VecGetLocalSize(xlocal,&totdofs);CHKERRQ(ierr);
90   ierr = DMGetLocalSection(dm,&s);CHKERRQ(ierr);
91   ierr = PetscSectionGetNumFields(s,&nfields);CHKERRQ(ierr);
92   for (f=0,maxfields=0;f<nfields;f++) {
93     PetscInt bs;
94 
95     ierr = PetscSectionGetFieldComponents(s,f,&bs);CHKERRQ(ierr);
96     maxfields += bs;
97   }
98   ierr = PetscCalloc7(maxfields,&fieldname,maxfields,&nlocal,maxfields,&bs,maxfields,&dims,maxfields,&fec_type,totdofs,&idxs,maxfields,&Ufield);CHKERRQ(ierr);
99   ierr = PetscNew(&ctx);CHKERRQ(ierr);
100   ierr = PetscCalloc1(maxfields,&ctx->scctx);CHKERRQ(ierr);
101   ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
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;
108 
109       ierr = PetscSectionGetFieldName(s,f,&fname);CHKERRQ(ierr);
110       ierr = PetscStrlen(fname,&len);CHKERRQ(ierr);
111       if (len) {
112         ierr = PetscStrcpy(name,fname);CHKERRQ(ierr);
113       } else {
114         ierr = PetscSNPrintf(name,256,"Field%D",f);CHKERRQ(ierr);
115       }
116       ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
117       if (disc) {
118         PetscClassId id;
119         PetscInt     Nc;
120         char         fec[64];
121 
122         ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
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;
129 
130           ierr = PetscFEGetNumComponents(fem,&Nc);CHKERRQ(ierr);
131           ierr = PetscFEGetDualSpace(fem,&sp);CHKERRQ(ierr);
132           ierr = PetscDualSpaceGetType(sp,&spname);CHKERRQ(ierr);
133           ierr = PetscStrcmp(spname,PETSCDUALSPACELAGRANGE,&islag);CHKERRQ(ierr);
134           if (!islag) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Unsupported dual space");
135           ierr = PetscDualSpaceLagrangeGetContinuity(sp,&continuous);CHKERRQ(ierr);
136           ierr = PetscDualSpaceGetOrder(sp,&order);CHKERRQ(ierr);
137           if (continuous && order > 0) { /* no support for high-order viz, still have to figure out the numbering */
138             ierr = PetscSNPrintf(fec,64,"FiniteElementCollection: H1_%DD_P1",dim);CHKERRQ(ierr);
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             ierr = PetscSNPrintf(fec,64,"FiniteElementCollection: L2_%DD_P%D",dim,order);CHKERRQ(ierr);
143           }
144           ierr = PetscStrallocpy(name,&fieldname[ctx->nf]);CHKERRQ(ierr);
145           bs[ctx->nf]   = Nc;
146           dims[ctx->nf] = dim;
147           if (H1) {
148             nlocal[ctx->nf] = Nc * Nv;
149             ierr = PetscStrallocpy(fec,&fec_type[ctx->nf]);CHKERRQ(ierr);
150             ierr = VecCreateSeq(PETSC_COMM_SELF,Nv*Nc,&xfield);CHKERRQ(ierr);
151             for (i=0,cum=0;i<vEnd-vStart;i++) {
152               PetscInt j,off;
153 
154               if (PetscUnlikely(!PetscBTLookup(vown,i))) continue;
155               ierr = PetscSectionGetFieldOffset(s,i+vStart,f,&off);CHKERRQ(ierr);
156               for (j=0;j<Nc;j++) idxs[cum++] = off + j;
157             }
158             ierr = ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),Nv*Nc,idxs,PETSC_USE_POINTER,&isfield);CHKERRQ(ierr);
159           } else {
160             nlocal[ctx->nf] = Nc * totc;
161             ierr = PetscStrallocpy(fec,&fec_type[ctx->nf]);CHKERRQ(ierr);
162             ierr = VecCreateSeq(PETSC_COMM_SELF,Nc*totc,&xfield);CHKERRQ(ierr);
163             for (i=0,cum=0;i<cEnd-cStart;i++) {
164               PetscInt j,off;
165 
166               if (PetscUnlikely(gNum[i] < 0)) continue;
167               ierr = PetscSectionGetFieldOffset(s,i+cStart,f,&off);CHKERRQ(ierr);
168               for (j=0;j<Nc;j++) idxs[cum++] = off + j;
169             }
170             ierr = ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),totc*Nc,idxs,PETSC_USE_POINTER,&isfield);CHKERRQ(ierr);
171           }
172           ierr = VecScatterCreate(xlocal,isfield,xfield,NULL,&ctx->scctx[ctx->nf]);CHKERRQ(ierr);
173           ierr = VecDestroy(&xfield);CHKERRQ(ierr);
174           ierr = ISDestroy(&isfield);CHKERRQ(ierr);
175           ctx->nf++;
176         } else if (id == PETSCFV_CLASSID) {
177           PetscInt c;
178 
179           ierr = PetscFVGetNumComponents((PetscFV)disc,&Nc);CHKERRQ(ierr);
180           ierr = PetscSNPrintf(fec,64,"FiniteElementCollection: L2_%DD_P0",dim);CHKERRQ(ierr);
181           for (c = 0; c < Nc; c++) {
182             char comp[256];
183             ierr = PetscSNPrintf(comp,256,"%s-Comp%D",name,c);CHKERRQ(ierr);
184             ierr = PetscStrallocpy(comp,&fieldname[ctx->nf]);CHKERRQ(ierr);
185             bs[ctx->nf] = 1; /* Does PetscFV support components with different block size? */
186             nlocal[ctx->nf] = totc;
187             dims[ctx->nf] = dim;
188             ierr = PetscStrallocpy(fec,&fec_type[ctx->nf]);CHKERRQ(ierr);
189             ierr = VecCreateSeq(PETSC_COMM_SELF,totc,&xfield);CHKERRQ(ierr);
190             for (i=0,cum=0;i<cEnd-cStart;i++) {
191               PetscInt off;
192 
193               if (PetscUnlikely(gNum[i])<0) continue;
194               ierr = PetscSectionGetFieldOffset(s,i+cStart,f,&off);CHKERRQ(ierr);
195               idxs[cum++] = off + c;
196             }
197             ierr = ISCreateGeneral(PetscObjectComm((PetscObject)xlocal),totc,idxs,PETSC_USE_POINTER,&isfield);CHKERRQ(ierr);
198             ierr = VecScatterCreate(xlocal,isfield,xfield,NULL,&ctx->scctx[ctx->nf]);CHKERRQ(ierr);
199             ierr = VecDestroy(&xfield);CHKERRQ(ierr);
200             ierr = ISDestroy(&isfield);CHKERRQ(ierr);
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   ierr = PetscBTDestroy(&vown);CHKERRQ(ierr);
208   ierr = VecDestroy(&xlocal);CHKERRQ(ierr);
209   ierr = ISRestoreIndices(globalNum,&gNum);CHKERRQ(ierr);
210 
211   /* create work vectors */
212   for (f=0;f<ctx->nf;f++) {
213     ierr = VecCreateMPI(PetscObjectComm((PetscObject)dm),nlocal[f],PETSC_DECIDE,&Ufield[f]);CHKERRQ(ierr);
214     ierr = PetscObjectSetName((PetscObject)Ufield[f],fieldname[f]);CHKERRQ(ierr);
215     ierr = VecSetBlockSize(Ufield[f],bs[f]);CHKERRQ(ierr);
216     ierr = VecSetDM(Ufield[f],dm);CHKERRQ(ierr);
217   }
218 
219   /* customize the viewer */
220   ierr = PetscViewerGLVisSetFields(viewer,ctx->nf,(const char**)fec_type,dims,DMPlexSampleGLVisFields_Private,(PetscObject*)Ufield,ctx,DestroyGLVisViewerCtx_Private);CHKERRQ(ierr);
221   for (f=0;f<ctx->nf;f++) {
222     ierr = PetscFree(fieldname[f]);CHKERRQ(ierr);
223     ierr = PetscFree(fec_type[f]);CHKERRQ(ierr);
224     ierr = VecDestroy(&Ufield[f]);CHKERRQ(ierr);
225   }
226   ierr = PetscFree7(fieldname,nlocal,bs,dims,fec_type,idxs,Ufield);CHKERRQ(ierr);
227   PetscFunctionReturn(0);
228 }
229 
230 typedef enum {MFEM_POINT=0,MFEM_SEGMENT,MFEM_TRIANGLE,MFEM_SQUARE,MFEM_TETRAHEDRON,MFEM_CUBE,MFEM_PRISM,MFEM_UNDEF} MFEM_cid;
231 
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 } };
236 
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 } };
241 
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;
246   PetscErrorCode ierr;
247 
248   PetscFunctionBegin;
249   ierr = DMPlexGetDepthLabel(dm,&dlabel);CHKERRQ(ierr);
250   ierr = DMLabelGetValue(dlabel,p,&pdepth);CHKERRQ(ierr);
251   ierr = DMPlexGetConeSize(dm,p,&csize);CHKERRQ(ierr);
252   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
253   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
254   if (label) {
255     ierr = DMLabelGetValue(label,p,mid);CHKERRQ(ierr);
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   PetscFunctionReturn(0);
269 }
270 
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;
274   PetscErrorCode ierr;
275 
276   PetscFunctionBegin;
277   ierr = DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);CHKERRQ(ierr);
278   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
279   sdim = dim;
280   if (csec) {
281     PetscInt sStart,sEnd;
282 
283     ierr = DMGetCoordinateDim(dm,&sdim);CHKERRQ(ierr);
284     ierr = PetscSectionGetChart(csec,&sStart,&sEnd);CHKERRQ(ierr);
285     ierr = PetscSectionGetOffset(csec,vStart,&off);CHKERRQ(ierr);
286     off  = off/sdim;
287     if (p >= sStart && p < sEnd) {
288       ierr = PetscSectionGetDof(csec,p,&dof);CHKERRQ(ierr);
289     }
290   }
291   if (!dof) {
292     ierr = DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);CHKERRQ(ierr);
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     ierr = DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&numPoints,&points);CHKERRQ(ierr);
297   } else {
298     ierr = PetscSectionGetOffset(csec,p,&off);CHKERRQ(ierr);
299     ierr = PetscSectionGetDof(csec,p,&dof);CHKERRQ(ierr);
300     for (q=0;q<dof/sdim;q++) vids[q] = off/sdim + q;
301   }
302   *nv = q;
303   PetscFunctionReturn(0);
304 }
305 
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;
320 
321   PetscFunctionBegin;
322   comm = PetscObjectComm((PetscObject)femIn);
323   ierr = PetscFEGetBasisSpace(femIn,&P);CHKERRQ(ierr);
324   ierr = PetscFEGetDualSpace(femIn,&Q);CHKERRQ(ierr);
325   ierr = PetscDualSpaceGetDM(Q,&K);CHKERRQ(ierr);
326   ierr = DMGetDimension(K,&dim);CHKERRQ(ierr);
327   ierr = PetscSpaceGetDegree(P,&deg,NULL);CHKERRQ(ierr);
328   ierr = PetscSpaceGetNumComponents(P,&dof);CHKERRQ(ierr);
329   ierr = DMPlexGetCellType(K,0,&ptype);CHKERRQ(ierr);
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   ierr = PetscSpaceCreate(comm,&P);CHKERRQ(ierr);
340   ierr = PetscSpaceSetType(P,PETSCSPACEPOLYNOMIAL);CHKERRQ(ierr);
341   ierr = PetscSpacePolynomialSetTensor(P,isTensor);CHKERRQ(ierr);
342   ierr = PetscSpaceSetNumComponents(P,dof);CHKERRQ(ierr);
343   ierr = PetscSpaceSetNumVariables(P,dim);CHKERRQ(ierr);
344   ierr = PetscSpaceSetDegree(P,deg,PETSC_DETERMINE);CHKERRQ(ierr);
345   ierr = PetscSpaceSetUp(P);CHKERRQ(ierr);
346   /* Create dual space */
347   ierr = PetscDualSpaceCreate(comm,&Q);CHKERRQ(ierr);
348   ierr = PetscDualSpaceSetType(Q,PETSCDUALSPACELAGRANGE);CHKERRQ(ierr);
349   ierr = PetscDualSpaceLagrangeSetTensor(Q,isTensor);CHKERRQ(ierr);
350   ierr = PetscDualSpaceLagrangeSetContinuity(Q,continuity);CHKERRQ(ierr);
351   ierr = PetscDualSpaceLagrangeSetNodeType(Q,nodeType,endpoint,0);CHKERRQ(ierr);
352   ierr = PetscDualSpaceSetNumComponents(Q,dof);CHKERRQ(ierr);
353   ierr = PetscDualSpaceSetOrder(Q,deg);CHKERRQ(ierr);
354   ierr = PetscDualSpaceCreateReferenceCell(Q,dim,isSimplex,&K);CHKERRQ(ierr);
355   ierr = PetscDualSpaceSetDM(Q,K);CHKERRQ(ierr);
356   ierr = DMDestroy(&K);CHKERRQ(ierr);
357   ierr = PetscDualSpaceSetUp(Q);CHKERRQ(ierr);
358   /* Create quadrature */
359   if (isSimplex) {
360     ierr = PetscDTStroudConicalQuadrature(dim,  1,deg+1,-1,+1,&q);CHKERRQ(ierr);
361     ierr = PetscDTStroudConicalQuadrature(dim-1,1,deg+1,-1,+1,&fq);CHKERRQ(ierr);
362   } else {
363     ierr = PetscDTGaussTensorQuadrature(dim,  1,deg+1,-1,+1,&q);CHKERRQ(ierr);
364     ierr = PetscDTGaussTensorQuadrature(dim-1,1,deg+1,-1,+1,&fq);CHKERRQ(ierr);
365   }
366   /* Create finite element */
367   ierr = PetscFECreate(comm,fem);CHKERRQ(ierr);
368   ierr = PetscSNPrintf(name,32,"L2_T1_%DD_P%D",dim,deg);CHKERRQ(ierr);
369   ierr = PetscObjectSetName((PetscObject)*fem,name);CHKERRQ(ierr);
370   ierr = PetscFESetType(*fem,PETSCFEBASIC);CHKERRQ(ierr);
371   ierr = PetscFESetNumComponents(*fem,dof);CHKERRQ(ierr);
372   ierr = PetscFESetBasisSpace(*fem,P);CHKERRQ(ierr);
373   ierr = PetscFESetDualSpace(*fem,Q);CHKERRQ(ierr);
374   ierr = PetscFESetQuadrature(*fem,q);CHKERRQ(ierr);
375   ierr = PetscFESetFaceQuadrature(*fem,fq);CHKERRQ(ierr);
376   ierr = PetscFESetUp(*fem);CHKERRQ(ierr);
377   /* Cleanup */
378   ierr = PetscSpaceDestroy(&P);CHKERRQ(ierr);
379   ierr = PetscDualSpaceDestroy(&Q);CHKERRQ(ierr);
380   ierr = PetscQuadratureDestroy(&q);CHKERRQ(ierr);
381   ierr = PetscQuadratureDestroy(&fq);CHKERRQ(ierr);
382   PetscFunctionReturn(0);
383 }
384 
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] = "";
407 
408   PetscFunctionBegin;
409   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
410   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
411   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
412   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERASCII");
413   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&size);CHKERRMPI(ierr);
414   if (size > 1) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Use single sequential viewers for parallel visualization");
415   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
416 
417   /* get container: determines if a process visualizes is portion of the data or not */
418   ierr = PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&glvis_container);CHKERRQ(ierr);
419   if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis container");
420   {
421     PetscViewerGLVisInfo glvis_info;
422     ierr    = PetscContainerGetPointer(glvis_container,(void**)&glvis_info);CHKERRQ(ierr);
423     enabled = glvis_info->enabled;
424     fmt     = glvis_info->fmt;
425   }
426 
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   ierr = PetscObjectQuery((PetscObject)dm,"_glvis_mesh_coords",(PetscObject*)&hovec);CHKERRQ(ierr);
430   ierr = PetscObjectReference((PetscObject)hovec);CHKERRQ(ierr);
431   if (!hovec) {
432     DM           cdm;
433     PetscFE      disc;
434     PetscClassId classid;
435 
436     ierr = DMGetCoordinateDM(dm,&cdm);CHKERRQ(ierr);
437     ierr = DMGetField(cdm,0,NULL,(PetscObject*)&disc);CHKERRQ(ierr);
438     ierr = PetscObjectGetClassId((PetscObject)disc,&classid);CHKERRQ(ierr);
439     if (classid == PETSCFE_CLASSID) {
440       DM      hocdm;
441       PetscFE hodisc;
442       Vec     vec;
443       Mat     mat;
444       char    name[32],fec_type[64];
445 
446       ierr = GLVisCreateFE(disc,name,&hodisc);CHKERRQ(ierr);
447       ierr = DMClone(cdm,&hocdm);CHKERRQ(ierr);
448       ierr = DMSetField(hocdm,0,NULL,(PetscObject)hodisc);CHKERRQ(ierr);
449       ierr = PetscFEDestroy(&hodisc);CHKERRQ(ierr);
450       ierr = DMCreateDS(hocdm);CHKERRQ(ierr);
451 
452       ierr = DMGetCoordinates(dm,&vec);CHKERRQ(ierr);
453       ierr = DMCreateGlobalVector(hocdm,&hovec);CHKERRQ(ierr);
454       ierr = DMCreateInterpolation(cdm,hocdm,&mat,NULL);CHKERRQ(ierr);
455       ierr = MatInterpolate(mat,vec,hovec);CHKERRQ(ierr);
456       ierr = MatDestroy(&mat);CHKERRQ(ierr);
457       ierr = DMDestroy(&hocdm);CHKERRQ(ierr);
458 
459       ierr = PetscSNPrintf(fec_type,sizeof(fec_type),"FiniteElementCollection: %s", name);CHKERRQ(ierr);
460       ierr = PetscObjectSetName((PetscObject)hovec,fec_type);CHKERRQ(ierr);
461     }
462   }
463 
464   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
465   ierr = DMPlexGetGhostCellStratum(dm,&p,NULL);CHKERRQ(ierr);
466   if (p >= 0) cEnd = p;
467   ierr = DMPlexGetDepthStratum(dm,0,&vStart,&vEnd);CHKERRQ(ierr);
468   ierr = DMGetPeriodicity(dm,&periodic,NULL,NULL,NULL);CHKERRQ(ierr);
469   ierr = DMGetCoordinatesLocalized(dm,&localized);CHKERRQ(ierr);
470   if (periodic && !localized && !hovec) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Coordinates need to be localized");
471   ierr = DMGetCoordinateSection(dm,&coordSection);CHKERRQ(ierr);
472   ierr = DMGetCoordinateDim(dm,&sdim);CHKERRQ(ierr);
473   ierr = DMGetCoordinatesLocal(dm,&coordinates);CHKERRQ(ierr);
474   if (!coordinates && !hovec) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
475 
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   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,"GLVis PetscViewer DMPlex Options","PetscViewer");CHKERRQ(ierr);
489   ierr = PetscOptionsBool("-viewer_glvis_dm_plex_enable_boundary","Enable boundary section in mesh representation",NULL,enable_boundary,&enable_boundary,NULL);CHKERRQ(ierr);
490   ierr = PetscOptionsBool("-viewer_glvis_dm_plex_enable_ncmesh","Enable vertex_parents section in mesh representation (allows derefinement)",NULL,enable_ncmesh,&enable_ncmesh,NULL);CHKERRQ(ierr);
491   ierr = PetscOptionsBool("-viewer_glvis_dm_plex_enable_mfem","Dump a mesh that can be used with MFEM's FiniteElementSpaces",NULL,enable_mfem,&enable_mfem,NULL);CHKERRQ(ierr);
492   ierr = PetscOptionsBool("-viewer_glvis_dm_plex_overlap","Include overlap region in local meshes",NULL,view_ovl,&view_ovl,NULL);CHKERRQ(ierr);
493   ierr = PetscOptionsString("-viewer_glvis_dm_plex_emarker","String for the material id label",NULL,emark,emark,sizeof(emark),&enable_emark);CHKERRQ(ierr);
494   ierr = PetscOptionsString("-viewer_glvis_dm_plex_bmarker","String for the boundary id label",NULL,bmark,bmark,sizeof(bmark),&enable_bmark);CHKERRQ(ierr);
495   ierr = PetscOptionsEnd();CHKERRQ(ierr);
496   if (enable_bmark) enable_boundary = PETSC_TRUE;
497 
498   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
499   if (enable_ncmesh && size > 1) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Not supported in parallel");
500   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
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   }
509 
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;
517 
518     ierr = PetscObjectQuery((PetscObject)dm,"_glvis_plex_gnum",(PetscObject*)&globalNum);CHKERRQ(ierr);
519     if (!globalNum) {
520       if (view_ovl) {
521         ierr = ISCreateStride(PetscObjectComm((PetscObject)dm),cEnd-cStart,0,1,&globalNum);CHKERRQ(ierr);
522       } else {
523         ierr = DMPlexCreateCellNumbering_Internal(dm,PETSC_TRUE,&globalNum);CHKERRQ(ierr);
524       }
525       ierr = PetscObjectCompose((PetscObject)dm,"_glvis_plex_gnum",(PetscObject)globalNum);CHKERRQ(ierr);
526       ierr = PetscObjectDereference((PetscObject)globalNum);CHKERRQ(ierr);
527     }
528     ierr = ISGetIndices(globalNum,&gNum);CHKERRQ(ierr);
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       ierr = PetscBTCreate(cEnd-cStart,&pown);CHKERRQ(ierr);
539       for (p=cStart; p<cEnd; p++) {
540         if (gNum[p-cStart] < 0) continue;
541         else {
542           ierr = PetscBTSet(pown,p-cStart);CHKERRQ(ierr);
543         }
544       }
545     }
546     ierr = ISRestoreIndices(globalNum,&gNum);CHKERRQ(ierr);
547   }
548 
549   /* vertex_parents (Non-conforming meshes) */
550   parentSection  = NULL;
551   if (enable_ncmesh) {
552     ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
553     enable_ncmesh = (PetscBool)(enable_ncmesh && parentSection);
554   }
555   /* return if this process is disabled */
556   if (!enabled) {
557     ierr = PetscViewerASCIIPrintf(viewer,"MFEM mesh %s\n",enable_ncmesh ? "v1.1" : "v1.0");CHKERRQ(ierr);
558     ierr = PetscViewerASCIIPrintf(viewer,"\ndimension\n");CHKERRQ(ierr);
559     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",dim);CHKERRQ(ierr);
560     ierr = PetscViewerASCIIPrintf(viewer,"\nelements\n");CHKERRQ(ierr);
561     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",0);CHKERRQ(ierr);
562     ierr = PetscViewerASCIIPrintf(viewer,"\nboundary\n");CHKERRQ(ierr);
563     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",0);CHKERRQ(ierr);
564     ierr = PetscViewerASCIIPrintf(viewer,"\nvertices\n");CHKERRQ(ierr);
565     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",0);CHKERRQ(ierr);
566     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",sdim);CHKERRQ(ierr);
567     ierr = PetscBTDestroy(&pown);CHKERRQ(ierr);
568     ierr = VecDestroy(&hovec);CHKERRQ(ierr);
569     PetscFunctionReturn(0);
570   }
571 
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;
581 
582       ierr = PetscSNPrintf(fec,sizeof(fec),"FiniteElementCollection: L2_T1_%DD_P1",dim);CHKERRQ(ierr);
583       if (cEnd-cStart) {
584         PetscInt fpc;
585 
586         ierr = DMPlexGetConeSize(dm,cStart,&fpc);CHKERRQ(ierr);
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         ierr = DMPlexReorderCell(dm,cStart,vids);CHKERRQ(ierr);
630       }
631       if (!dof) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing dofs");
632       ierr = VecCreateSeq(PETSC_COMM_SELF,(cEnd-cStart-novl)*vpc*sdim,&hovec);CHKERRQ(ierr);
633       ierr = PetscObjectSetName((PetscObject)hovec,fec);CHKERRQ(ierr);
634       ierr = VecGetArray(hovec,&array);CHKERRQ(ierr);
635       ptr  = array;
636       for (p=cStart;p<cEnd;p++) {
637         PetscInt    csize,v,d;
638         PetscScalar *vals = NULL;
639 
640         if (PetscUnlikely(pown && !PetscBTLookup(pown,p-cStart))) continue;
641         ierr = DMPlexVecGetClosure(dm,coordSection,coordinates,p,&csize,&vals);CHKERRQ(ierr);
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         ierr = DMPlexVecRestoreClosure(dm,coordSection,coordinates,p,&csize,&vals);CHKERRQ(ierr);
650       }
651       ierr = VecRestoreArray(hovec,&array);CHKERRQ(ierr);
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;
656 
657   /* header */
658   ierr = PetscViewerASCIIPrintf(viewer,"MFEM mesh %s\n",enable_ncmesh ? "v1.1" : "v1.0");CHKERRQ(ierr);
659 
660   /* topological dimension */
661   ierr = PetscViewerASCIIPrintf(viewer,"\ndimension\n");CHKERRQ(ierr);
662   ierr = PetscViewerASCIIPrintf(viewer,"%D\n",dim);CHKERRQ(ierr);
663 
664   /* elements */
665   minl = 1;
666   label = NULL;
667   if (enable_emark) {
668     PetscInt lminl = PETSC_MAX_INT;
669 
670     ierr = DMGetLabel(dm,emark,&label);CHKERRQ(ierr);
671     if (label) {
672       IS       vals;
673       PetscInt ldef;
674 
675       ierr = DMLabelGetDefaultValue(label,&ldef);CHKERRQ(ierr);
676       ierr = DMLabelGetValueIS(label,&vals);CHKERRQ(ierr);
677       ierr = ISGetMinMax(vals,&lminl,NULL);CHKERRQ(ierr);
678       ierr = ISDestroy(&vals);CHKERRQ(ierr);
679       lminl = PetscMin(ldef,lminl);
680     }
681     ierr = MPIU_Allreduce(&lminl,&minl,1,MPIU_INT,MPI_MIN,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
682     if (minl == PETSC_MAX_INT) minl = 1;
683   }
684   ierr = PetscViewerASCIIPrintf(viewer,"\nelements\n");CHKERRQ(ierr);
685   ierr = PetscViewerASCIIPrintf(viewer,"%D\n",cEnd-cStart-novl);CHKERRQ(ierr);
686   for (p=cStart;p<cEnd;p++) {
687     PetscInt       vids[8];
688     PetscInt       i,nv = 0,cid = -1,mid = 1;
689 
690     if (PetscUnlikely(pown && !PetscBTLookup(pown,p-cStart))) continue;
691     ierr = DMPlexGetPointMFEMCellID_Internal(dm,label,minl,p,&mid,&cid);CHKERRQ(ierr);
692     ierr = DMPlexGetPointMFEMVertexIDs_Internal(dm,p,(localized && !hovec) ? coordSection : NULL,&nv,vids);CHKERRQ(ierr);
693     ierr = DMPlexReorderCell(dm,p,vids);CHKERRQ(ierr);
694     ierr = PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);CHKERRQ(ierr);
695     for (i=0;i<nv;i++) {
696       ierr = PetscViewerASCIIPrintf(viewer," %D",vids[i]);CHKERRQ(ierr);
697     }
698     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
699   }
700 
701   /* boundary */
702   ierr = PetscViewerASCIIPrintf(viewer,"\nboundary\n");CHKERRQ(ierr);
703   if (!enable_boundary) {
704     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",0);CHKERRQ(ierr);
705   } else {
706     DMLabel  perLabel;
707     PetscBT  bfaces;
708     PetscInt fStart,fEnd,*fcells;
709 
710     ierr = DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);CHKERRQ(ierr);
711     ierr = PetscBTCreate(fEnd-fStart,&bfaces);CHKERRQ(ierr);
712     ierr = DMPlexGetMaxSizes(dm,NULL,&p);CHKERRQ(ierr);
713     ierr = PetscMalloc1(p,&fcells);CHKERRQ(ierr);
714     ierr = DMGetLabel(dm,"glvis_periodic_cut",&perLabel);CHKERRQ(ierr);
715     if (!perLabel && localized) { /* this periodic cut can be moved up to DMPlex setup */
716       ierr = DMCreateLabel(dm,"glvis_periodic_cut");CHKERRQ(ierr);
717       ierr = DMGetLabel(dm,"glvis_periodic_cut",&perLabel);CHKERRQ(ierr);
718       ierr = DMLabelSetDefaultValue(perLabel,1);CHKERRQ(ierr);
719       for (p=cStart;p<cEnd;p++) {
720         DMPolytopeType cellType;
721         PetscInt       dof;
722 
723         ierr = DMPlexGetCellType(dm,p,&cellType);CHKERRQ(ierr);
724         ierr = PetscSectionGetDof(coordSection,p,&dof);CHKERRQ(ierr);
725         if (dof) {
726           PetscInt    uvpc, v,csize,cellClosureSize,*cellClosure = NULL,*vidxs = NULL;
727           PetscScalar *vals = NULL;
728 
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           ierr = DMPlexVecGetClosure(dm,coordSection,coordinates,p,&csize,&vals);CHKERRQ(ierr);
732           ierr = DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);CHKERRQ(ierr);
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;
741 
742             for (s=0;s<sdim;s++) {
743               if (PetscAbsScalar(vals[v*sdim+s]-vals[v*sdim+s+uvpc*sdim])>PETSC_MACHINE_EPSILON) {
744                 ierr = DMLabelSetValue(perLabel,vidxs[2*v],2);CHKERRQ(ierr);
745               }
746             }
747           }
748           ierr = DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&cellClosureSize,&cellClosure);CHKERRQ(ierr);
749           ierr = DMPlexVecRestoreClosure(dm,coordSection,coordinates,p,&csize,&vals);CHKERRQ(ierr);
750         }
751       }
752       if (dim > 1) {
753         PetscInt eEnd,eStart;
754 
755         ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
756         for (p=eStart;p<eEnd;p++) {
757           const PetscInt *cone;
758           PetscInt       coneSize,i;
759           PetscBool      ispe = PETSC_TRUE;
760 
761           ierr = DMPlexGetCone(dm,p,&cone);CHKERRQ(ierr);
762           ierr = DMPlexGetConeSize(dm,p,&coneSize);CHKERRQ(ierr);
763           for (i=0;i<coneSize;i++) {
764             PetscInt v;
765 
766             ierr = DMLabelGetValue(perLabel,cone[i],&v);CHKERRQ(ierr);
767             ispe = (PetscBool)(ispe && (v==2));
768           }
769           if (ispe && coneSize) {
770             PetscInt       ch, numChildren;
771             const PetscInt *children;
772 
773             ierr = DMLabelSetValue(perLabel,p,2);CHKERRQ(ierr);
774             ierr = DMPlexGetTreeChildren(dm,p,&numChildren,&children);CHKERRQ(ierr);
775             for (ch = 0; ch < numChildren; ch++) {
776               ierr = DMLabelSetValue(perLabel,children[ch],2);CHKERRQ(ierr);
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;
785 
786             ierr = DMPlexGetCone(dm,p,&cone);CHKERRQ(ierr);
787             ierr = DMPlexGetConeSize(dm,p,&coneSize);CHKERRQ(ierr);
788             for (i=0;i<coneSize;i++) {
789               PetscInt v;
790 
791               ierr = DMLabelGetValue(perLabel,cone[i],&v);CHKERRQ(ierr);
792               ispe = (PetscBool)(ispe && (v==2));
793             }
794             if (ispe && coneSize) {
795               PetscInt       ch, numChildren;
796               const PetscInt *children;
797 
798               ierr = DMLabelSetValue(perLabel,p,2);CHKERRQ(ierr);
799               ierr = DMPlexGetTreeChildren(dm,p,&numChildren,&children);CHKERRQ(ierr);
800               for (ch = 0; ch < numChildren; ch++) {
801                 ierr = DMLabelSetValue(perLabel,children[ch],2);CHKERRQ(ierr);
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;
812 
813       ierr = DMPlexGetSupportSize(dm,p,&supportSize);CHKERRQ(ierr);
814       if (pown) {
815         PetscBool has_owned = PETSC_FALSE, has_ghost = PETSC_FALSE;
816         PetscInt  i;
817 
818         ierr = DMPlexGetSupport(dm,p,&support);CHKERRQ(ierr);
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;
830 
831         ierr = DMPlexGetCone(dm,p,&cone);CHKERRQ(ierr);
832         ierr = DMPlexGetConeSize(dm,p,&coneSize);CHKERRQ(ierr);
833         isbf = PETSC_TRUE;
834         for (i=0;i<coneSize;i++) {
835           PetscInt v,d;
836 
837           ierr = DMLabelGetValue(perLabel,cone[i],&v);CHKERRQ(ierr);
838           ierr = DMLabelGetDefaultValue(perLabel,&d);CHKERRQ(ierr);
839           isbf = (PetscBool)(isbf && v != d);
840         }
841       }
842       if (isbf) {
843         ierr = PetscBTSet(bfaces,p-fStart);CHKERRQ(ierr);
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;
851 
852         ierr = DMPlexGetSupportSize(dm,p,&supportSize);CHKERRQ(ierr);
853         ierr = DMPlexGetSupport(dm,p,&support);CHKERRQ(ierr);
854         for (c=0;c<supportSize;c++) {
855           const    PetscInt *cone;
856           PetscInt cell,cl,coneSize;
857 
858           cell = support[c];
859           if (pown && PetscUnlikely(!PetscBTLookup(pown,cell-cStart))) continue;
860           ierr = DMPlexGetCone(dm,cell,&cone);CHKERRQ(ierr);
861           ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
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;
875 
876       ierr = DMGetLabel(dm,bmark,&label);CHKERRQ(ierr);
877       if (label) {
878         IS       vals;
879         PetscInt ldef;
880 
881         ierr = DMLabelGetDefaultValue(label,&ldef);CHKERRQ(ierr);
882         ierr = DMLabelGetValueIS(label,&vals);CHKERRQ(ierr);
883         ierr = ISGetMinMax(vals,&lminl,NULL);CHKERRQ(ierr);
884         ierr = ISDestroy(&vals);CHKERRQ(ierr);
885         lminl = PetscMin(ldef,lminl);
886       }
887       ierr = MPIU_Allreduce(&lminl,&minl,1,MPIU_INT,MPI_MIN,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
888       if (minl == PETSC_MAX_INT) minl = 1;
889     }
890     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",bf);CHKERRQ(ierr);
891     for (p=fStart;p<fEnd;p++) {
892       if (PetscUnlikely(PetscBTLookup(bfaces,p-fStart))) {
893         const PetscInt *support;
894         PetscInt       supportSize,c,nc = 0;
895 
896         ierr = DMPlexGetSupportSize(dm,p,&supportSize);CHKERRQ(ierr);
897         ierr = DMPlexGetSupport(dm,p,&support);CHKERRQ(ierr);
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;
910 
911           cell = fcells[c];
912           ierr = DMPlexGetCone(dm,cell,&cone);CHKERRQ(ierr);
913           ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
914           for (cl=0;cl<coneSize;cl++)
915             if (cone[cl] == p)
916               break;
917           if (cl == coneSize) continue;
918 
919           /* face material id and type */
920           ierr = DMPlexGetPointMFEMCellID_Internal(dm,label,minl,p,&mid,&cid);CHKERRQ(ierr);
921           ierr = PetscViewerASCIIPrintf(viewer,"%D %D",mid,cid);CHKERRQ(ierr);
922           /* vertex ids */
923           ierr = DMPlexGetCellType(dm,cell,&cellType);CHKERRQ(ierr);
924           ierr = DMPlexGetPointMFEMVertexIDs_Internal(dm,cell,(localized && !hovec) ? coordSection : NULL,&nv,vids);CHKERRQ(ierr);
925           ierr = DMPlexGetRawFaces_Internal(dm,cellType,vids,NULL,&faceTypes,&faceSizes,(const PetscInt**)&faces);CHKERRQ(ierr);
926           st = 0;
927           for (i=0;i<cl;i++) st += faceSizes[i];
928           ierr = DMPlexInvertCell(faceTypes[cl],faces + st);CHKERRQ(ierr);
929           for (i=0;i<faceSizes[cl];i++) {
930             ierr = PetscViewerASCIIPrintf(viewer," %d",faces[st+i]);CHKERRQ(ierr);
931           }
932           ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
933           ierr = DMPlexRestoreRawFaces_Internal(dm,cellType,vids,NULL,&faceTypes,&faceSizes,(const PetscInt**)&faces);CHKERRQ(ierr);
934           bf -= 1;
935         }
936       }
937     }
938     if (bf) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Remaining boundary faces %D",bf);
939     ierr = PetscBTDestroy(&bfaces);CHKERRQ(ierr);
940     ierr = PetscFree(fcells);CHKERRQ(ierr);
941   }
942 
943   /* mark owned vertices */
944   vown = NULL;
945   if (pown) {
946     ierr = PetscBTCreate(vEnd-vStart,&vown);CHKERRQ(ierr);
947     for (p=cStart;p<cEnd;p++) {
948       PetscInt i,closureSize,*closure = NULL;
949 
950       if (PetscUnlikely(!PetscBTLookup(pown,p-cStart))) continue;
951       ierr = DMPlexGetTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
952       for (i=0;i<closureSize;i++) {
953         const PetscInt pp = closure[2*i];
954 
955         if (pp >= vStart && pp < vEnd) {
956           ierr = PetscBTSet(vown,pp-vStart);CHKERRQ(ierr);
957         }
958       }
959       ierr = DMPlexRestoreTransitiveClosure(dm,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
960     }
961   }
962 
963   if (parentSection) {
964     PetscInt vp,gvp;
965 
966     for (vp=0,p=vStart;p<vEnd;p++) {
967       DMLabel  dlabel;
968       PetscInt parent,depth;
969 
970       if (PetscUnlikely(vown && !PetscBTLookup(vown,p-vStart))) continue;
971       ierr = DMPlexGetDepthLabel(dm,&dlabel);CHKERRQ(ierr);
972       ierr = DMLabelGetValue(dlabel,p,&depth);CHKERRQ(ierr);
973       ierr = DMPlexGetTreeParent(dm,p,&parent,NULL);CHKERRQ(ierr);
974       if (parent != p) vp++;
975     }
976     ierr = MPIU_Allreduce(&vp,&gvp,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
977     if (gvp) {
978       PetscInt  maxsupp;
979       PetscBool *skip = NULL;
980 
981       ierr = PetscViewerASCIIPrintf(viewer,"\nvertex_parents\n");CHKERRQ(ierr);
982       ierr = PetscViewerASCIIPrintf(viewer,"%D\n",vp);CHKERRQ(ierr);
983       ierr = DMPlexGetMaxSizes(dm,NULL,&maxsupp);CHKERRQ(ierr);
984       ierr = PetscMalloc1(maxsupp,&skip);CHKERRQ(ierr);
985       for (p=vStart;p<vEnd;p++) {
986         DMLabel  dlabel;
987         PetscInt parent;
988 
989         if (PetscUnlikely(vown && !PetscBTLookup(vown,p-vStart))) continue;
990         ierr = DMPlexGetDepthLabel(dm,&dlabel);CHKERRQ(ierr);
991         ierr = DMPlexGetTreeParent(dm,p,&parent,NULL);CHKERRQ(ierr);
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;
996 
997           ierr = DMPlexGetConeSize(dm,parent,&ssize);CHKERRQ(ierr);
998           switch (ssize) {
999             case 2: /* edge */
1000               nv   = 0;
1001               ierr = DMPlexGetPointMFEMVertexIDs_Internal(dm,parent,localized ? coordSection : NULL,&nv,vids);CHKERRQ(ierr);
1002               ierr = PetscViewerASCIIPrintf(viewer,"%D",p-vStart);CHKERRQ(ierr);
1003               for (i=0;i<nv;i++) {
1004                 ierr = PetscViewerASCIIPrintf(viewer," %D",vids[i]);CHKERRQ(ierr);
1005               }
1006               ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1007               vp--;
1008               break;
1009             case 4: /* face */
1010               ierr = DMPlexGetTreeChildren(dm,parent,&numChildren,&children);CHKERRQ(ierr);
1011               for (n=0;n<numChildren;n++) {
1012                 ierr = DMLabelGetValue(dlabel,children[n],&depth);CHKERRQ(ierr);
1013                 if (!depth) {
1014                   const PetscInt *hvsupp,*hesupp,*cone;
1015                   PetscInt       hvsuppSize,hesuppSize,coneSize;
1016                   PetscInt       hv = children[n],he = -1,f;
1017 
1018                   ierr = PetscArrayzero(skip,maxsupp);CHKERRQ(ierr);
1019                   ierr = DMPlexGetSupportSize(dm,hv,&hvsuppSize);CHKERRQ(ierr);
1020                   ierr = DMPlexGetSupport(dm,hv,&hvsupp);CHKERRQ(ierr);
1021                   for (i=0;i<hvsuppSize;i++) {
1022                     PetscInt ep;
1023                     ierr = DMPlexGetTreeParent(dm,hvsupp[i],&ep,NULL);CHKERRQ(ierr);
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                   ierr    = DMPlexGetCone(dm,he,&cone);CHKERRQ(ierr);
1032                   vids[0] = (cone[0] == hv) ? cone[1] : cone[0];
1033                   ierr    = DMPlexGetSupportSize(dm,he,&hesuppSize);CHKERRQ(ierr);
1034                   ierr    = DMPlexGetSupport(dm,he,&hesupp);CHKERRQ(ierr);
1035                   for (f=0;f<hesuppSize;f++) {
1036                     PetscInt j;
1037 
1038                     ierr = DMPlexGetCone(dm,hesupp[f],&cone);CHKERRQ(ierr);
1039                     ierr = DMPlexGetConeSize(dm,hesupp[f],&coneSize);CHKERRQ(ierr);
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                       ierr = DMPlexGetCone(dm,hvsupp[i],&cone);CHKERRQ(ierr);
1053                       vids[1] = (cone[0] == hv) ? cone[1] : cone[0];
1054                     }
1055                   }
1056                   ierr = PetscViewerASCIIPrintf(viewer,"%D",hv-vStart);CHKERRQ(ierr);
1057                   for (i=0;i<2;i++) {
1058                     ierr = PetscViewerASCIIPrintf(viewer," %D",vids[i]-vStart);CHKERRQ(ierr);
1059                   }
1060                   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
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       ierr = PetscFree(skip);CHKERRQ(ierr);
1071     }
1072     if (vp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D hanging vertices",vp);
1073   }
1074   ierr = PetscBTDestroy(&pown);CHKERRQ(ierr);
1075   ierr = PetscBTDestroy(&vown);CHKERRQ(ierr);
1076 
1077   /* vertices */
1078   if (hovec) { /* higher-order meshes */
1079     const char *fec;
1080     PetscInt   i,n,s;
1081 
1082     ierr = PetscViewerASCIIPrintf(viewer,"\nvertices\n");CHKERRQ(ierr);
1083     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",vEnd-vStart);CHKERRQ(ierr);
1084     ierr = PetscViewerASCIIPrintf(viewer,"nodes\n");CHKERRQ(ierr);
1085     ierr = PetscObjectGetName((PetscObject)hovec,&fec);CHKERRQ(ierr);
1086     ierr = PetscViewerASCIIPrintf(viewer,"FiniteElementSpace\n");CHKERRQ(ierr);
1087     ierr = PetscViewerASCIIPrintf(viewer,"%s\n",fec);CHKERRQ(ierr);
1088     ierr = PetscViewerASCIIPrintf(viewer,"VDim: %D\n",sdim);CHKERRQ(ierr);
1089     ierr = PetscViewerASCIIPrintf(viewer,"Ordering: 1\n\n");CHKERRQ(ierr); /*Ordering::byVDIM*/
1090     ierr = VecGetArrayRead(hovec,&array);CHKERRQ(ierr);
1091     ierr = VecGetLocalSize(hovec,&n);CHKERRQ(ierr);
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         ierr = PetscViewerASCIIPrintf(viewer,fmt,PetscRealPart(array[i*sdim+s]));CHKERRQ(ierr);
1096       }
1097       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1098     }
1099     ierr = VecRestoreArrayRead(hovec,&array);CHKERRQ(ierr);
1100   } else {
1101     ierr = VecGetLocalSize(coordinates,&nvert);CHKERRQ(ierr);
1102     ierr = PetscViewerASCIIPrintf(viewer,"\nvertices\n");CHKERRQ(ierr);
1103     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",nvert/sdim);CHKERRQ(ierr);
1104     ierr = PetscViewerASCIIPrintf(viewer,"%D\n",sdim);CHKERRQ(ierr);
1105     ierr = VecGetArrayRead(coordinates,&array);CHKERRQ(ierr);
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]);
1110 
1111         ierr = PetscViewerASCIIPrintf(viewer,fmt,PetscIsInfOrNanReal(v) ? 0.0 : v);CHKERRQ(ierr);
1112       }
1113       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1114     }
1115     ierr = VecRestoreArrayRead(coordinates,&array);CHKERRQ(ierr);
1116   }
1117   ierr = VecDestroy(&hovec);CHKERRQ(ierr);
1118   PetscFunctionReturn(0);
1119 }
1120 
1121 PetscErrorCode DMPlexView_GLVis(DM dm, PetscViewer viewer)
1122 {
1123   PetscErrorCode ierr;
1124   PetscFunctionBegin;
1125   ierr = DMView_GLVis(dm,viewer,DMPlexView_GLVis_ASCII);CHKERRQ(ierr);
1126   PetscFunctionReturn(0);
1127 }
1128