Actual source code: grglvis.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1: /* Routines to visualize DMDAs and fields through GLVis */

  3:  #include <petsc/private/dmdaimpl.h>
  4:  #include <petsc/private/glvisviewerimpl.h>

  6: typedef struct {
  7:   PetscBool ll;
  8: } DMDAGhostedGLVisViewerCtx;

 10: static PetscErrorCode DMDAGhostedDestroyGLVisViewerCtx_Private(void **vctx)
 11: {

 15:   PetscFree(*vctx);
 16:   return(0);
 17: }

 19: typedef struct {
 20:   Vec xlocal;
 21: } DMDAFieldGLVisViewerCtx;

 23: static PetscErrorCode DMDAFieldDestroyGLVisViewerCtx_Private(void *vctx)
 24: {
 25:   DMDAFieldGLVisViewerCtx *ctx = (DMDAFieldGLVisViewerCtx*)vctx;
 26:   PetscErrorCode          ierr;

 29:   VecDestroy(&ctx->xlocal);
 30:   PetscFree(vctx);
 31:   return(0);
 32: }

 34: /*
 35:    dactx->ll is false -> all but the last proc per dimension claim the ghosted node on the right
 36:    dactx->ll is true -> all but the first proc per dimension claim the ghosted node on the left
 37: */
 38: static PetscErrorCode DMDAGetNumElementsGhosted(DM da, PetscInt *nex, PetscInt *ney, PetscInt *nez)
 39: {
 40:   DMDAGhostedGLVisViewerCtx *dactx;
 41:   PetscInt                  sx,sy,sz,ien,jen,ken;
 42:   PetscErrorCode            ierr;

 45:   /* Appease -Wmaybe-uninitialized */
 46:   if (nex) *nex = -1;
 47:   if (ney) *ney = -1;
 48:   if (nez) *nez = -1;
 49:   DMDAGetCorners(da,&sx,&sy,&sz,&ien,&jen,&ken);
 50:   DMGetApplicationContext(da,(void**)&dactx);
 51:   if (dactx->ll) {
 52:     PetscInt dim;

 54:     DMGetDimension(da,&dim);
 55:     if (!sx) ien--;
 56:     if (!sy && dim > 1) jen--;
 57:     if (!sz && dim > 2) ken--;
 58:   } else {
 59:     PetscInt M,N,P;

 61:     DMDAGetInfo(da,NULL,&M,&N,&P,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
 62:     if (sx+ien == M) ien--;
 63:     if (sy+jen == N) jen--;
 64:     if (sz+ken == P) ken--;
 65:   }
 66:   if (nex) *nex = ien;
 67:   if (ney) *ney = jen;
 68:   if (nez) *nez = ken;
 69:   return(0);
 70: }

 72: /* inherits number of vertices from DMDAGetNumElementsGhosted */
 73: static PetscErrorCode DMDAGetNumVerticesGhosted(DM da, PetscInt *nvx, PetscInt *nvy, PetscInt *nvz)
 74: {
 75:   PetscInt       ien,jen,ken,dim;

 79:   DMGetDimension(da,&dim);
 80:   DMDAGetNumElementsGhosted(da,&ien,&jen,&ken);
 81:   ien  = ien+1;
 82:   jen  = dim > 1 ? jen+1 : 1;
 83:   ken  = dim > 2 ? ken+1 : 1;
 84:   if (nvx) *nvx = ien;
 85:   if (nvy) *nvy = jen;
 86:   if (nvz) *nvz = ken;
 87:   return(0);
 88: }

 90: static PetscErrorCode DMDASampleGLVisFields_Private(PetscObject oX, PetscInt nf, PetscObject oXf[], void *vctx)
 91: {
 92:   DM                        da;
 93:   DMDAFieldGLVisViewerCtx   *ctx = (DMDAFieldGLVisViewerCtx*)vctx;
 94:   DMDAGhostedGLVisViewerCtx *dactx;
 95:   const PetscScalar         *array;
 96:   PetscScalar               **arrayf;
 97:   PetscInt                  i,f,ii,ien,jen,ken,ie,je,ke,bs,*bss;
 98:   PetscInt                  sx,sy,sz,gsx,gsy,gsz,ist,jst,kst,gm,gn,gp;
 99:   PetscErrorCode            ierr;

102:   VecGetDM(ctx->xlocal,&da);
103:   if (!da) SETERRQ(PetscObjectComm(oX),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
104:   DMGetApplicationContext(da,(void**)&dactx);
105:   VecGetBlockSize(ctx->xlocal,&bs);
106:   DMGlobalToLocalBegin(da,(Vec)oX,INSERT_VALUES,ctx->xlocal);
107:   DMGlobalToLocalEnd(da,(Vec)oX,INSERT_VALUES,ctx->xlocal);
108:   DMDAGetNumVerticesGhosted(da,&ien,&jen,&ken);
109:   DMDAGetGhostCorners(da,&gsx,&gsy,&gsz,&gm,&gn,&gp);
110:   DMDAGetCorners(da,&sx,&sy,&sz,NULL,NULL,NULL);
111:   if (dactx->ll) {
112:     kst = jst = ist = 0;
113:   } else {
114:     kst  = gsz != sz ? 1 : 0;
115:     jst  = gsy != sy ? 1 : 0;
116:     ist  = gsx != sx ? 1 : 0;
117:   }
118:   PetscMalloc2(nf,&arrayf,nf,&bss);
119:   VecGetArrayRead(ctx->xlocal,&array);
120:   for (f=0;f<nf;f++) {
121:     VecGetBlockSize((Vec)oXf[f],&bss[f]);
122:     VecGetArray((Vec)oXf[f],&arrayf[f]);
123:   }
124:   for (ke = kst, ii = 0; ke < kst + ken; ke++) {
125:     for (je = jst; je < jst + jen; je++) {
126:       for (ie = ist; ie < ist + ien; ie++) {
127:         PetscInt cf,b;
128:         i = ke * gm * gn + je * gm + ie;
129:         for (f=0,cf=0;f<nf;f++)
130:           for (b=0;b<bss[f];b++)
131:             arrayf[f][bss[f]*ii+b] = array[i*bs+cf++];
132:         ii++;
133:       }
134:     }
135:   }
136:   for (f=0;f<nf;f++) { VecRestoreArray((Vec)oXf[f],&arrayf[f]); }
137:   VecRestoreArrayRead(ctx->xlocal,&array);
138:   PetscFree2(arrayf,bss);
139:   return(0);
140: }

142: PETSC_INTERN PetscErrorCode DMSetUpGLVisViewer_DMDA(PetscObject oda, PetscViewer viewer)
143: {
144:   DM                 da = (DM)oda,daview;
145:   PetscErrorCode     ierr;

148:   PetscObjectQuery(oda,"GLVisGraphicsDMDAGhosted",(PetscObject*)&daview);
149:   if (!daview) {
150:     DMDAGhostedGLVisViewerCtx *dactx;
151:     DM                        dacoord = NULL;
152:     Vec                       xcoor,xcoorl;
153:     PetscBool                 hashocoord = PETSC_FALSE;
154:     const PetscInt            *lx,*ly,*lz;
155:     PetscInt                  dim,M,N,P,m,n,p,dof,s,i;

157:     PetscNew(&dactx);
158:     PetscOptionsBegin(PetscObjectComm(oda),oda->prefix,"GLVis PetscViewer DMDA Options","PetscViewer");
159:     PetscOptionsBool("-viewer_glvis_dm_da_ll","Left-looking subdomain view",NULL,dactx->ll,&dactx->ll,NULL);
160:     PetscOptionsEnd();
161:     /* Create a properly ghosted DMDA to visualize the mesh and the fields associated with */
162:     DMDAGetInfo(da,&dim,&M,&N,&P,&m,&n,&p,&dof,&s,NULL,NULL,NULL,NULL);
163:     DMDAGetOwnershipRanges(da,&lx,&ly,&lz);
164:     PetscObjectQuery((PetscObject)da,"_glvis_mesh_coords",(PetscObject*)&xcoor);
165:     if (!xcoor) {
166:       DMGetCoordinates(da,&xcoor);
167:     } else {
168:       hashocoord = PETSC_TRUE;
169:     }
170:     PetscInfo(da,"Creating auxilary DMDA for managing GLVis graphics\n");
171:     switch (dim) {
172:     case 1:
173:       DMDACreate1d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,M,dof,1,lx,&daview);
174:       if (!hashocoord) {
175:         DMDACreate1d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,M,1,1,lx,&dacoord);
176:       }
177:       break;
178:     case 2:
179:       DMDACreate2d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,dof,1,lx,ly,&daview);
180:       if (!hashocoord) {
181:         DMDACreate2d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,2,1,lx,ly,&dacoord);
182:       }
183:       break;
184:     case 3:
185:       DMDACreate3d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,P,m,n,p,dof,1,lx,ly,lz,&daview);
186:       if (!hashocoord) {
187:         DMDACreate3d(PetscObjectComm((PetscObject)da),DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,P,m,n,p,3,1,lx,ly,lz,&dacoord);
188:       }
189:       break;
190:     default:
191:       SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Unsupported dimension %D",dim);
192:       break;
193:     }
194:     DMSetApplicationContext(daview,dactx);
195:     DMSetApplicationContextDestroy(daview,DMDAGhostedDestroyGLVisViewerCtx_Private);
196:     DMSetUp(daview);
197:     if (!xcoor) {
198:       DMDASetUniformCoordinates(daview,0.0,1.0,0.0,1.0,0.0,1.0);
199:       DMGetCoordinates(daview,&xcoor);
200:     }
201:     if (dacoord) {
202:       DMSetUp(dacoord);
203:       DMCreateLocalVector(dacoord,&xcoorl);
204:       DMGlobalToLocalBegin(dacoord,xcoor,INSERT_VALUES,xcoorl);
205:       DMGlobalToLocalEnd(dacoord,xcoor,INSERT_VALUES,xcoorl);
206:       DMDestroy(&dacoord);
207:     } else {
208:       PetscInt ien,jen,ken,nc,nl,cdof,deg;
209:       char     fecmesh[64];

211:       DMDAGetNumElementsGhosted(daview,&ien,&jen,&ken);
212:       nc   = ien*(jen>0 ? jen : 1)*(ken>0 ? ken : 1);

214:       VecGetLocalSize(xcoor,&nl);
215:       if (nc && nl % nc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Incompatible local coordinate size %D and number of cells %D",nl,nc);
216:       VecDuplicate(xcoor,&xcoorl);
217:       VecCopy(xcoor,xcoorl);
218:       VecSetDM(xcoorl,NULL);
219:       cdof = nl/(nc*dim);
220:       deg  = 1;
221:       while (1) {
222:         PetscInt degd = 1;
223:         for (i=0;i<dim;i++) degd *= (deg+1);
224:         if (degd > cdof) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cell dofs %D",cdof);
225:         if (degd == cdof) break;
226:         deg++;
227:       }
228:       PetscSNPrintf(fecmesh,sizeof(fecmesh),"FiniteElementCollection: L2_T1_%dD_P%d",dim,deg);
229:       PetscObjectSetName((PetscObject)xcoorl,fecmesh);
230:     }

232:     /* xcoorl is composed with the ghosted DMDA, the ghosted coordinate DMDA (if present) is only available through this vector */
233:     PetscObjectCompose((PetscObject)daview,"GLVisGraphicsCoordsGhosted",(PetscObject)xcoorl);
234:     PetscObjectDereference((PetscObject)xcoorl);

236:     /* daview is composed with the original DMDA */
237:     PetscObjectCompose(oda,"GLVisGraphicsDMDAGhosted",(PetscObject)daview);
238:     PetscObjectDereference((PetscObject)daview);
239:   }

241:   /* customize the viewer if present */
242:   if (viewer) {
243:     DMDAFieldGLVisViewerCtx   *ctx;
244:     DMDAGhostedGLVisViewerCtx *dactx;
245:     char                      fec[64];
246:     Vec                       xlocal;
247:     const char                **dafieldname;
248:     char                      **fec_type,**fieldname;
249:     PetscInt                  *nlocal,*bss,*dims;
250:     PetscInt                  dim,M,N,P,dof,s,i,nf;
251:     PetscBool                 bsset;

253:     DMGetApplicationContext(daview,(void**)&dactx);
254:     DMCreateLocalVector(daview,&xlocal);
255:     DMDAGetFieldNames(da,(const char * const **)&dafieldname);
256:     DMDAGetNumVerticesGhosted(daview,&M,&N,&P);
257:     DMDAGetInfo(daview,&dim,NULL,NULL,NULL,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL);
258:     PetscSNPrintf(fec,sizeof(fec),"FiniteElementCollection: H1_%dD_P1",dim);
259:     PetscMalloc5(dof,&fec_type,dof,&nlocal,dof,&bss,dof,&dims,dof,&fieldname);
260:     for (i=0;i<dof;i++) bss[i] = 1;
261:     nf = dof;

263:     PetscOptionsBegin(PetscObjectComm(oda),oda->prefix,"GLVis PetscViewer DMDA Field options","PetscViewer");
264:     PetscOptionsIntArray("-viewer_glvis_dm_da_bs","Block sizes for subfields; enable vector representation",NULL,bss,&nf,&bsset);
265:     PetscOptionsEnd();
266:     if (bsset) {
267:       PetscInt t;
268:       for (i=0,t=0;i<nf;i++) t += bss[i];
269:       if (t != dof) SETERRQ2(PetscObjectComm(oda),PETSC_ERR_USER,"Sum of block sizes %D should equal %D",t,dof);
270:     } else nf = dof;

272:     for (i=0,s=0;i<nf;i++) {
273:       PetscStrallocpy(fec,&fec_type[i]);
274:       if (bss[i] == 1) {
275:         PetscStrallocpy(dafieldname[s],&fieldname[i]);
276:       } else {
277:         PetscInt b;
278:         size_t tlen = 9; /* "Vector-" + end */
279:         for (b=0;b<bss[i];b++) {
280:           size_t len;
281:           PetscStrlen(dafieldname[s+b],&len);
282:           tlen += len + 1; /* field + "-" */
283:         }
284:         PetscMalloc1(tlen,&fieldname[i]);
285:         PetscStrcpy(fieldname[i],"Vector-");
286:         for (b=0;b<bss[i]-1;b++) {
287:           PetscStrcat(fieldname[i],dafieldname[s+b]);
288:           PetscStrcat(fieldname[i],"-");
289:         }
290:         PetscStrcat(fieldname[i],dafieldname[s+b]);
291:       }
292:       dims[i] = dim;
293:       nlocal[i] = M*N*P*bss[i];
294:       s += bss[i];
295:     }

297:     /* the viewer context takes ownership of xlocal and destroys it in DMDAFieldDestroyGLVisViewerCtx_Private */
298:     PetscNew(&ctx);
299:     ctx->xlocal = xlocal;

301:     PetscViewerGLVisSetFields(viewer,nf,(const char**)fieldname,(const char**)fec_type,nlocal,bss,dims,DMDASampleGLVisFields_Private,ctx,DMDAFieldDestroyGLVisViewerCtx_Private);
302:     for (i=0;i<nf;i++) {
303:       PetscFree(fec_type[i]);
304:       PetscFree(fieldname[i]);
305:     }
306:     PetscFree5(fec_type,nlocal,bss,dims,fieldname);
307:   }
308:   return(0);
309: }

311: static PetscErrorCode DMDAView_GLVis_ASCII(DM dm, PetscViewer viewer)
312: {
313:   DM                da,cda;
314:   Vec               xcoorl;
315:   PetscMPIInt       commsize;
316:   const PetscScalar *array;
317:   PetscContainer    glvis_container;
318:   PetscInt          dim,sdim,i,vid[8],mid,cid,cdof;
319:   PetscInt          sx,sy,sz,ie,je,ke,ien,jen,ken;
320:   PetscInt          gsx,gsy,gsz,gm,gn,gp,kst,jst,ist;
321:   PetscBool         enabled = PETSC_TRUE, isascii;
322:   PetscErrorCode    ierr;
323:   const char        *fmt;

328:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
329:   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERASCII");
330:   MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&commsize);
331:   if (commsize > 1) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Use single sequential viewers for parallel visualization");
332:   DMGetDimension(dm,&dim);

334:   /* get container: determines if a process visualizes is portion of the data or not */
335:   PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&glvis_container);
336:   if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis container");
337:   {
338:     PetscViewerGLVisInfo glvis_info;
339:     PetscContainerGetPointer(glvis_container,(void**)&glvis_info);
340:     enabled = glvis_info->enabled;
341:     fmt = glvis_info->fmt;
342:   }
343:   /* this can happen if we are calling DMView outside of VecView_GLVis */
344:   PetscObjectQuery((PetscObject)dm,"GLVisGraphicsDMDAGhosted",(PetscObject*)&da);
345:   if (!da) {DMSetUpGLVisViewer_DMDA((PetscObject)dm,NULL);}
346:   PetscObjectQuery((PetscObject)dm,"GLVisGraphicsDMDAGhosted",(PetscObject*)&da);
347:   if (!da) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis ghosted DMDA");
348:   DMGetCoordinateDim(da,&sdim);

350:   PetscViewerASCIIPrintf(viewer,"MFEM mesh v1.1\n");
351:   PetscViewerASCIIPrintf(viewer,"\ndimension\n");
352:   PetscViewerASCIIPrintf(viewer,"%D\n",dim);

354:   if (!enabled) {
355:     PetscViewerASCIIPrintf(viewer,"\nelements\n");
356:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
357:     PetscViewerASCIIPrintf(viewer,"\nboundary\n");
358:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
359:     PetscViewerASCIIPrintf(viewer,"\nvertices\n");
360:     PetscViewerASCIIPrintf(viewer,"%D\n",0);
361:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
362:     return(0);
363:   }

365:   DMDAGetNumElementsGhosted(da,&ien,&jen,&ken);
366:   i    = ien;
367:   if (dim > 1) i *= jen;
368:   if (dim > 2) i *= ken;
369:   PetscViewerASCIIPrintf(viewer,"\nelements\n");
370:   PetscViewerASCIIPrintf(viewer,"%D\n",i);
371:   switch (dim) {
372:   case 1:
373:     for (ie = 0; ie < ien; ie++) {
374:       vid[0] = ie;
375:       vid[1] = ie+1;
376:       mid    = 1; /* material id */
377:       cid    = 1; /* segment */
378:       PetscViewerASCIIPrintf(viewer,"%D %D %D %D\n",mid,cid,vid[0],vid[1]);
379:     }
380:     break;
381:   case 2:
382:     for (je = 0; je < jen; je++) {
383:       for (ie = 0; ie < ien; ie++) {
384:         vid[0] =     je*(ien+1) + ie;
385:         vid[1] =     je*(ien+1) + ie+1;
386:         vid[2] = (je+1)*(ien+1) + ie+1;
387:         vid[3] = (je+1)*(ien+1) + ie;
388:         mid    = 1; /* material id */
389:         cid    = 3; /* quad */
390:         PetscViewerASCIIPrintf(viewer,"%D %D %D %D %D %D\n",mid,cid,vid[0],vid[1],vid[2],vid[3]);
391:       }
392:     }
393:     break;
394:   case 3:
395:     for (ke = 0; ke < ken; ke++) {
396:       for (je = 0; je < jen; je++) {
397:         for (ie = 0; ie < ien; ie++) {
398:           vid[0] =     ke*(jen+1)*(ien+1) +     je*(ien+1) + ie;
399:           vid[1] =     ke*(jen+1)*(ien+1) +     je*(ien+1) + ie+1;
400:           vid[2] =     ke*(jen+1)*(ien+1) + (je+1)*(ien+1) + ie+1;
401:           vid[3] =     ke*(jen+1)*(ien+1) + (je+1)*(ien+1) + ie;
402:           vid[4] = (ke+1)*(jen+1)*(ien+1) +     je*(ien+1) + ie;
403:           vid[5] = (ke+1)*(jen+1)*(ien+1) +     je*(ien+1) + ie+1;
404:           vid[6] = (ke+1)*(jen+1)*(ien+1) + (je+1)*(ien+1) + ie+1;
405:           vid[7] = (ke+1)*(jen+1)*(ien+1) + (je+1)*(ien+1) + ie;
406:           mid    = 1; /* material id */
407:           cid    = 5; /* hex */
408:           PetscViewerASCIIPrintf(viewer,"%D %D %D %D %D %D %D %D %D %D\n",mid,cid,vid[0],vid[1],vid[2],vid[3],vid[4],vid[5],vid[6],vid[7]);
409:         }
410:       }
411:     }
412:     break;
413:   default:
414:     SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Unsupported dimension %D",dim);
415:     break;
416:   }
417:   PetscViewerASCIIPrintf(viewer,"\nboundary\n");
418:   PetscViewerASCIIPrintf(viewer,"%D\n",0);

420:   /* vertex coordinates */
421:   PetscObjectQuery((PetscObject)da,"GLVisGraphicsCoordsGhosted",(PetscObject*)&xcoorl);
422:   if (!xcoorl) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Missing GLVis ghosted coords");
423:   DMDAGetNumVerticesGhosted(da,&ien,&jen,&ken);
424:   PetscViewerASCIIPrintf(viewer,"\nvertices\n");
425:   PetscViewerASCIIPrintf(viewer,"%D\n",ien*jen*ken);
426:   VecGetDM(xcoorl,&cda);
427:   VecGetArrayRead(xcoorl,&array);
428:   if (!cda) { /* HO viz */
429:     const char *fecname;
430:     PetscInt   nc,nl;

432:     PetscObjectGetName((PetscObject)xcoorl,&fecname);
433:     PetscViewerASCIIPrintf(viewer,"nodes\n");
434:     PetscViewerASCIIPrintf(viewer,"FiniteElementSpace\n");
435:     PetscViewerASCIIPrintf(viewer,"FiniteElementCollection: %s\n",fecname);
436:     PetscViewerASCIIPrintf(viewer,"VDim: %D\n",sdim);
437:     PetscViewerASCIIPrintf(viewer,"Ordering: 1\n\n"); /*Ordering::byVDIM*/
438:     /* L2 coordinates */
439:     DMDAGetNumElementsGhosted(da,&ien,&jen,&ken);
440:     VecGetLocalSize(xcoorl,&nl);
441:     nc   = ien*(jen>0 ? jen : 1)*(ken>0 ? ken : 1);
442:     cdof = nl/nc;
443:     if (!ien) ien++;
444:     if (!jen) jen++;
445:     if (!ken) ken++;
446:     ist = jst = kst = 0;
447:     gm = ien;
448:     gn = jen;
449:     gp = ken;
450:   } else {
451:     DMDAGhostedGLVisViewerCtx *dactx;

453:     DMGetApplicationContext(da,(void**)&dactx);
454:     PetscViewerASCIIPrintf(viewer,"%D\n",sdim);
455:     cdof = sdim;
456:     DMDAGetCorners(da,&sx,&sy,&sz,NULL,NULL,NULL);
457:     DMDAGetGhostCorners(da,&gsx,&gsy,&gsz,&gm,&gn,&gp);
458:     if (dactx->ll) {
459:       kst = jst = ist = 0;
460:     } else {
461:       kst  = gsz != sz ? 1 : 0;
462:       jst  = gsy != sy ? 1 : 0;
463:       ist  = gsx != sx ? 1 : 0;
464:     }
465:   }
466:   for (ke = kst; ke < kst + ken; ke++) {
467:     for (je = jst; je < jst + jen; je++) {
468:       for (ie = ist; ie < ist + ien; ie++) {
469:         PetscInt c;

471:         i = ke * gm * gn + je * gm + ie;
472:         for (c=0;c<cdof/sdim;c++) {
473:           PetscInt d;
474:           for (d=0;d<sdim;d++) {
475:             PetscViewerASCIIPrintf(viewer,fmt,PetscRealPart(array[cdof*i+c*sdim+d]));
476:           }
477:           PetscViewerASCIIPrintf(viewer,"\n");
478:         }
479:       }
480:     }
481:   }
482:   VecRestoreArrayRead(xcoorl,&array);
483:   return(0);
484: }

486: /* dispatching, prints through the socket by prepending the mesh keyword to the usual ASCII dump: duplicated code as in plexglvis.c, should be merged together */
487: PETSC_INTERN PetscErrorCode DMView_DA_GLVis(DM dm, PetscViewer viewer)
488: {
490:   PetscBool      isglvis,isascii;

495:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERGLVIS,&isglvis);
496:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
497:   if (!isglvis && !isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Viewer must be of type VIEWERGLVIS or VIEWERASCII");
498:   if (isglvis) {
499:     PetscViewer          view;
500:     PetscViewerGLVisType type;

502:     PetscViewerGLVisGetType_Private(viewer,&type);
503:     PetscViewerGLVisGetDMWindow_Private(viewer,&view);
504:     if (view) { /* in the socket case, it may happen that the connection failed */
505:       if (type == PETSC_VIEWER_GLVIS_SOCKET) {
506:         PetscMPIInt size,rank;
507:         MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);
508:         MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);
509:         PetscViewerASCIIPrintf(view,"parallel %D %D\nmesh\n",size,rank);
510:       }
511:       DMDAView_GLVis_ASCII(dm,view);
512:       PetscViewerFlush(view);
513:       if (type == PETSC_VIEWER_GLVIS_SOCKET) {
514:         PetscInt    dim;
515:         const char* name;

517:         PetscObjectGetName((PetscObject)dm,&name);
518:         DMGetDimension(dm,&dim);
519:         PetscViewerGLVisInitWindow_Private(view,PETSC_TRUE,dim,name);
520:         PetscBarrier((PetscObject)dm);
521:       }
522:     }
523:   } else {
524:     DMDAView_GLVis_ASCII(dm,viewer);
525:   }
526:   return(0);
527: }