xref: /petsc/src/sys/classes/viewer/impls/glvis/glvis.c (revision 252ecd3142ba3ca0b326827f24f26bd5c1233270)
1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fdopen */
2 
3 #include <petsc/private/viewerimpl.h> /*I   "petscviewer.h" I*/
4 #include <petsc/private/petscimpl.h>  /*I   "petscsys.h"    I*/
5 #include <petsc/private/glvisviewerimpl.h>
6 
7 /* we may eventually make this function public */
8 static PetscErrorCode PetscViewerASCIISocketOpen(MPI_Comm,const char*,PetscInt,PetscViewer*);
9 
10 struct _n_PetscViewerGLVis {
11   PetscViewerGLVisStatus status;
12   PetscViewerGLVisType   type;                                                  /* either PETSC_VIEWER_GLVIS_DUMP or PETSC_VIEWER_GLVIS_SOCKET */
13   char                   *name;                                                 /* prefix for filename, or hostname, depending on the type */
14   PetscInt               port;                                                  /* used just for the socket case */
15   PetscReal              pause;                                                 /* if positive, calls PetscSleep(pause) after each VecView_GLVis call */
16   PetscViewer            meshwindow;                                            /* used just by the ASCII dumping */
17   PetscObject            dm;                                                    /* DM as passed by PetscViewerGLVisSetDM_Private(): should contain discretization info */
18   PetscInt               nwindow;                                               /* number of windows/fields to be visualized */
19   PetscViewer            *window;
20   char                   **windowtitle;
21   char                   **fec_type;                                            /* type of elements to be used for visualization, see FiniteElementCollection::Name() */
22   PetscErrorCode         (*g2lfield)(PetscObject,PetscInt,PetscObject[],void*); /* global to local operation for generating dofs to be visualized */
23   PetscInt               *locandbs;                                             /* local and block sizes for work vectors */
24   PetscObject            *Ufield;                                               /* work vectors for visualization */
25   PetscInt               snapid;                                                /* snapshot id, use PetscViewerGLVisSetSnapId to change this value*/
26   void                   *userctx;                                              /* User context, used by g2lfield */
27   PetscErrorCode         (*destroyctx)(void*);                                  /* destroy routine for userctx */
28 };
29 typedef struct _n_PetscViewerGLVis *PetscViewerGLVis;
30 
31 /*@
32      PetscViewerGLVisSetSnapId - Set the snapshot id. Only relevant when the viewer is of type PETSC_VIEWER_GLVIS_DUMP
33 
34   Logically Collective on PetscViewer
35 
36   Input Parameters:
37 +  viewer - the PetscViewer
38 -  id     - the current snapshot id in a time-dependent simulation
39 
40   Level: beginner
41 
42 .seealso: PetscViewerGLVisOpen(), PetscViewerCreate(), PetscViewerSetType()
43 @*/
44 PetscErrorCode PetscViewerGLVisSetSnapId(PetscViewer viewer, PetscInt id)
45 {
46   PetscErrorCode ierr;
47 
48   PetscFunctionBegin;
49   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
50   PetscValidLogicalCollectiveInt(viewer,id,2);
51   ierr = PetscTryMethod(viewer,"PetscViewerGLVisSetSnapId_C",(PetscViewer,PetscInt),(viewer,id));CHKERRQ(ierr);
52   PetscFunctionReturn(0);
53 }
54 
55 static PetscErrorCode PetscViewerGLVisSetSnapId_GLVis(PetscViewer viewer, PetscInt id)
56 {
57   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
58 
59   PetscFunctionBegin;
60   socket->snapid = id;;
61   PetscFunctionReturn(0);
62 }
63 
64 /*
65      PetscViewerGLVisSetFields - Sets the required information to visualize different fields from within a vector.
66 
67   Logically Collective on PetscViewer
68 
69   Input Parameters:
70 +  viewer     - the PetscViewer
71 .  nf         - number of fields to be visualized
72 .  namefield  - optional name for each field
73 .  fec_type   - the type of finite element to be used to visualize the data (see FiniteElementCollection::Name() in MFEM)
74 .  nlocal     - array of local sizes for field vectors
75 .  bs         - array of block sizes for field vectors
76 .  dim        - array of topological dimension for field vectors
77 .  g2lfields  - User routine to compute the local field vectors to be visualized; PetscObject is used in place of Vec on the prototype
78 .  ctx        - User context to store the relevant data to apply g2lfields
79 -  destroyctx - Destroy function for userctx
80 
81   Notes: g2lfields is called on the Vec V to be visualized, in order to extract the relevant dofs to be put in Vfield[], as
82 .vb
83   g2lfields((PetscObject)V,nfields,(PetscObject*)Vfield[],ctx). Misses Fortran binding
84 .ve
85 
86   Level: intermediate
87 
88 .seealso: PetscViewerGLVisOpen(), PetscViewerCreate(), PetscViewerSetType()
89 */
90 PetscErrorCode PetscViewerGLVisSetFields(PetscViewer viewer, PetscInt nf, const char* namefield[], const char* fec_type[], PetscInt nlocal[], PetscInt bs[], PetscInt dim[], PetscErrorCode(*g2l)(PetscObject,PetscInt,PetscObject[],void*), void* ctx, PetscErrorCode(*destroyctx)(void*))
91 {
92   PetscErrorCode ierr;
93 
94   PetscFunctionBegin;
95   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
96   PetscValidLogicalCollectiveInt(viewer,nf,2);
97   if (namefield) PetscValidPointer(namefield,3);
98   if (!fec_type) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"You need to provide the FiniteElementCollection names for the fields");
99   PetscValidPointer(fec_type,4);
100   PetscValidPointer(nlocal,5);
101   PetscValidPointer(bs,6);
102   PetscValidPointer(dim,7);
103   ierr = PetscTryMethod(viewer,"PetscViewerGLVisSetFields_C",(PetscViewer,PetscInt,const char*[],const char*[],PetscInt[],PetscInt[],PetscInt[],PetscErrorCode(*)(PetscObject,PetscInt,PetscObject[],void*),void*,PetscErrorCode(*)(void*)),(viewer,nf,namefield,fec_type,nlocal,bs,dim,g2l,ctx,destroyctx));CHKERRQ(ierr);
104   PetscFunctionReturn(0);
105 }
106 
107 static PetscErrorCode PetscViewerGLVisSetFields_GLVis(PetscViewer viewer, PetscInt nfields, const char* namefield[], const char* fec_type[], PetscInt nlocal[], PetscInt bs[], PetscInt dim[], PetscErrorCode(*g2l)(PetscObject,PetscInt,PetscObject[],void*),void* ctx, PetscErrorCode(*destroyctx)(void*))
108 {
109   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
110   PetscInt         i;
111   PetscErrorCode   ierr;
112 
113   PetscFunctionBegin;
114   if (socket->nwindow && socket->nwindow != nfields) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_USER,"Cannot set number of fields %D with number of windows %D",nfields,socket->nwindow);
115   if (!socket->nwindow) {
116     socket->nwindow = nfields;
117 
118     ierr = PetscCalloc5(nfields,&socket->window,nfields,&socket->windowtitle,nfields,&socket->fec_type,3*nfields,&socket->locandbs,nfields,&socket->Ufield);CHKERRQ(ierr);
119     for (i=0;i<nfields;i++) {
120       if (!namefield || !namefield[i]) {
121         char name[16];
122 
123         ierr = PetscSNPrintf(name,16,"Field%d",i);CHKERRQ(ierr);
124         ierr = PetscStrallocpy(name,&socket->windowtitle[i]);CHKERRQ(ierr);
125       } else {
126         ierr = PetscStrallocpy(namefield[i],&socket->windowtitle[i]);CHKERRQ(ierr);
127       }
128       ierr = PetscStrallocpy(fec_type[i],&socket->fec_type[i]);CHKERRQ(ierr);
129       socket->locandbs[3*i  ] = nlocal[i];
130       socket->locandbs[3*i+1] = bs[i];
131       socket->locandbs[3*i+2] = dim[i];
132     }
133   }
134   /* number of fields are not allowed to vary */
135   if (nfields != socket->nwindow) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot visualize %D fields using %D socket windows",nfields,socket->nwindow);
136   socket->g2lfield = g2l;
137   if (socket->destroyctx && socket->userctx) { ierr = (*socket->destroyctx)(socket->userctx);CHKERRQ(ierr); }
138   socket->userctx = ctx;
139   socket->destroyctx = destroyctx;
140   PetscFunctionReturn(0);
141 }
142 
143 static PetscErrorCode PetscViewerGLVisInfoDestroy_Private(void *ptr)
144 {
145   PetscViewerGLVisInfo info = (PetscViewerGLVisInfo)ptr;
146   PetscErrorCode       ierr;
147 
148   PetscFunctionBegin;
149   ierr = PetscFree(info);CHKERRQ(ierr);
150   PetscFunctionReturn(0);
151 }
152 
153 /* we can decide to prevent specific processes from using the viewer */
154 static PetscErrorCode PetscViewerGLVisAttachInfo_Private(PetscViewer viewer, PetscViewer window)
155 {
156   PetscViewerGLVis     socket = (PetscViewerGLVis)viewer->data;
157   PetscErrorCode       ierr;
158   PetscContainer       container;
159   PetscViewerGLVisInfo info;
160 
161   PetscFunctionBegin;
162   ierr = PetscNew(&info);CHKERRQ(ierr);
163   info->enabled = PETSC_TRUE;
164   info->init    = PETSC_FALSE;
165   info->pause   = socket->pause;
166   ierr = PetscContainerCreate(PetscObjectComm((PetscObject)window),&container);CHKERRQ(ierr);
167   ierr = PetscContainerSetPointer(container,(void*)info);CHKERRQ(ierr);
168   ierr = PetscContainerSetUserDestroy(container,PetscViewerGLVisInfoDestroy_Private);CHKERRQ(ierr);
169   ierr = PetscObjectCompose((PetscObject)window,"_glvis_info_container",(PetscObject)container);CHKERRQ(ierr);
170   ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
171   PetscFunctionReturn(0);
172 }
173 
174 static PetscErrorCode PetscViewerGLVisGetNewWindow_Private(PetscViewer viewer,PetscViewer *view)
175 {
176   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
177   PetscViewer      window = NULL;
178   PetscBool        ldis,dis;
179   PetscErrorCode   ierr;
180 
181   PetscFunctionBegin;
182   ierr = PetscViewerASCIISocketOpen(PETSC_COMM_SELF,socket->name,socket->port,&window);
183   /* if we could not estabilish a connection the first time,
184      we disable the socket viewer */
185   ldis = ierr ? PETSC_TRUE : PETSC_FALSE;
186   ierr = MPI_Allreduce(&ldis,&dis,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
187   if (dis) {
188     socket->status = PETSCVIEWERGLVIS_DISABLED;
189     ierr  = PetscViewerDestroy(&window);CHKERRQ(ierr);
190   }
191   *view = window;
192   PetscFunctionReturn(0);
193 }
194 
195 PetscErrorCode PetscViewerGLVisPause_Private(PetscViewer viewer)
196 {
197   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
198   PetscErrorCode   ierr;
199 
200   PetscFunctionBegin;
201   if (socket->pause > 0) {
202     ierr = PetscSleep(socket->pause);CHKERRQ(ierr);
203   }
204   PetscFunctionReturn(0);
205 }
206 
207 /* DM specific support */
208 PetscErrorCode PetscViewerGLVisSetDM_Private(PetscViewer viewer, PetscObject dm)
209 {
210   PetscErrorCode   ierr;
211   PetscViewerGLVis socket  = (PetscViewerGLVis)viewer->data;
212 
213   PetscFunctionBegin;
214   if (socket->dm && socket->dm != dm) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot change DM associated with the GLVis viewer");
215   if (!socket->dm) {
216     PetscErrorCode (*setupwithdm)(PetscObject,PetscViewer) = NULL;
217 
218     ierr = PetscObjectQueryFunction(dm,"DMSetUpGLVisViewer_C",&setupwithdm);CHKERRQ(ierr);
219     if (setupwithdm) {
220       ierr = (*setupwithdm)(dm,viewer);CHKERRQ(ierr);
221     } else SETERRQ1(PetscObjectComm(dm),PETSC_ERR_SUP,"No support for DM type %s",dm->type_name);
222     ierr = PetscObjectReference(dm);CHKERRQ(ierr);
223     socket->dm = dm;
224   }
225   PetscFunctionReturn(0);
226 }
227 
228 PetscErrorCode PetscViewerGLVisGetDMWindow_Private(PetscViewer viewer,PetscViewer* view)
229 {
230   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
231   PetscErrorCode   ierr;
232 
233   PetscFunctionBegin;
234   if (!socket->meshwindow) {
235     if (socket->type == PETSC_VIEWER_GLVIS_SOCKET) {
236       ierr = PetscViewerGLVisGetNewWindow_Private(viewer,&socket->meshwindow);CHKERRQ(ierr);
237     } else {
238       PetscMPIInt rank;
239       char        filename[PETSC_MAX_PATH_LEN];
240 
241       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
242       ierr = PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s-mesh.%06d",socket->name,rank);CHKERRQ(ierr);
243       ierr = PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&socket->meshwindow);CHKERRQ(ierr);
244     }
245     if (socket->meshwindow) {
246       ierr = PetscViewerGLVisAttachInfo_Private(viewer,socket->meshwindow);CHKERRQ(ierr);
247       ierr = PetscViewerPushFormat(socket->meshwindow,PETSC_VIEWER_ASCII_GLVIS);CHKERRQ(ierr);
248     }
249   }
250   *view = socket->meshwindow;
251   PetscFunctionReturn(0);
252 }
253 
254 PetscErrorCode PetscViewerGLVisGetType_Private(PetscViewer viewer,PetscViewerGLVisType *type)
255 {
256   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
257 
258   PetscFunctionBegin;
259   PetscValidPointer(type,2);
260   *type = socket->type;
261   PetscFunctionReturn(0);
262 }
263 
264 /* This function is only relevant in the SOCKET_GLIVS case. The status is computed the first time it is requested, as GLVis currently has issues when connecting the first time through the socket */
265 PetscErrorCode PetscViewerGLVisGetStatus_Private(PetscViewer viewer, PetscViewerGLVisStatus *sockstatus)
266 {
267   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
268 
269   PetscFunctionBegin;
270   PetscValidPointer(sockstatus,2);
271   if (socket->type == PETSC_VIEWER_GLVIS_DUMP) {
272     socket->status = PETSCVIEWERGLVIS_DISCONNECTED;
273   } else if (socket->status == PETSCVIEWERGLVIS_DISCONNECTED && socket->nwindow) {
274     PetscInt       i;
275     PetscBool      lconn,conn;
276     PetscErrorCode ierr;
277 
278     for (i=0,lconn=PETSC_TRUE;i<socket->nwindow;i++)
279       if (!socket->window[i])
280         lconn = PETSC_FALSE;
281 
282     ierr = MPI_Allreduce(&lconn,&conn,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
283     if (conn) socket->status = PETSCVIEWERGLVIS_CONNECTED;
284   }
285   *sockstatus = socket->status;
286   PetscFunctionReturn(0);
287 }
288 
289 PetscErrorCode PetscViewerGLVisGetDM_Private(PetscViewer viewer, PetscObject* dm)
290 {
291   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
292 
293   PetscFunctionBegin;
294   *dm = socket->dm;
295   PetscFunctionReturn(0);
296 }
297 
298 PetscErrorCode PetscViewerGLVisGetFields_Private(PetscViewer viewer, PetscInt* nfield, const char** names[], const char **fec[], PetscInt *locandbs[], PetscErrorCode(**g2lfield)(PetscObject,PetscInt,PetscObject[],void*), PetscObject *Ufield[], void **userctx)
299 {
300   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
301 
302   PetscFunctionBegin;
303   if (nfield)   *nfield   = socket->nwindow;
304   if (names)    *names    = (const char**)socket->windowtitle;
305   if (fec)      *fec      = (const char**)socket->fec_type;
306   if (locandbs) *locandbs = socket->locandbs;
307   if (g2lfield) *g2lfield = socket->g2lfield;
308   if (Ufield)   *Ufield   = socket->Ufield;
309   if (userctx)  *userctx  = socket->userctx;
310   PetscFunctionReturn(0);
311 }
312 
313 /* accessor routines for the viewer windows:
314    PETSC_VIEWER_GLVIS_DUMP   : it returns a new viewer every time
315    PETSC_VIEWER_GLVIS_SOCKET : it returns the socket, and creates it if not yet done.
316 */
317 PetscErrorCode PetscViewerGLVisGetWindow_Private(PetscViewer viewer,PetscInt wid,PetscViewer* view)
318 {
319   PetscViewerGLVis       socket = (PetscViewerGLVis)viewer->data;
320   PetscViewerGLVisStatus status;
321   PetscErrorCode         ierr;
322 
323   PetscFunctionBegin;
324   PetscValidLogicalCollectiveInt(viewer,wid,2);
325   PetscValidPointer(view,3);
326   if (wid < 0 || wid > socket->nwindow-1) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_USER,"Cannot get window id %D: allowed range [0,%D)",wid,socket->nwindow-1);
327   status = socket->status;
328   if (socket->type == PETSC_VIEWER_GLVIS_DUMP && socket->window[wid]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Window %D is already in use",wid);
329   switch (status) {
330     case PETSCVIEWERGLVIS_DISCONNECTED:
331       if (socket->window[wid]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"This should not happen");
332       if (socket->type == PETSC_VIEWER_GLVIS_DUMP) {
333         PetscMPIInt rank;
334         char        filename[PETSC_MAX_PATH_LEN];
335 
336         ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
337         ierr = PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s-%s-%d.%06d",socket->name,socket->windowtitle[wid],socket->snapid,rank);CHKERRQ(ierr);
338         ierr = PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&socket->window[wid]);CHKERRQ(ierr);
339       } else {
340         ierr = PetscViewerGLVisGetNewWindow_Private(viewer,&socket->window[wid]);CHKERRQ(ierr);
341       }
342       if (socket->window[wid]) {
343         ierr = PetscViewerGLVisAttachInfo_Private(viewer,socket->window[wid]);CHKERRQ(ierr);
344         ierr = PetscViewerPushFormat(socket->window[wid],PETSC_VIEWER_ASCII_GLVIS);CHKERRQ(ierr);
345       }
346       *view = socket->window[wid];
347       break;
348     case PETSCVIEWERGLVIS_CONNECTED:
349       *view = socket->window[wid];
350       break;
351     case PETSCVIEWERGLVIS_DISABLED:
352       *view = NULL;
353       break;
354     default:
355       SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unhandled socket status %d\n",(int)status);
356       break;
357   }
358   PetscFunctionReturn(0);
359 }
360 
361 /* Restore the window viewer
362    PETSC_VIEWER_GLVIS_DUMP  : destroys the temporary created ASCII viewer used for dumping
363    PETSC_VIEWER_GLVIS_SOCKET: - if the returned window viewer is not NULL, just zeros the pointer.
364                  - it the returned window viewer is NULL, assumes something went wrong
365                    with the socket (i.e. SIGPIPE when a user closes the popup window)
366                    and that the caller already handled it (see VecView_GLVis).
367 */
368 PetscErrorCode PetscViewerGLVisRestoreWindow_Private(PetscViewer viewer,PetscInt wid, PetscViewer* view)
369 {
370   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
371   PetscErrorCode   ierr;
372 
373   PetscFunctionBegin;
374   PetscValidLogicalCollectiveInt(viewer,wid,2);
375   PetscValidPointer(view,3);
376   if (wid < 0 || wid > socket->nwindow-1) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_USER,"Cannot restore window id %D: allowed range [0,%D)",wid,socket->nwindow);
377   if (*view && *view != socket->window[wid]) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_USER,"Viewer was not obtained from PetscViewerGLVisGetWindow");
378   if (*view) {
379     ierr = PetscViewerFlush(*view);CHKERRQ(ierr);
380     ierr = PetscBarrier((PetscObject)viewer);CHKERRQ(ierr);
381   }
382   if (socket->type == PETSC_VIEWER_GLVIS_DUMP) { /* destroy the viewer, as it is associated with a single time step */
383     ierr = PetscViewerDestroy(&socket->window[wid]);CHKERRQ(ierr);
384   } else if (!*view) { /* something went wrong (SIGPIPE) so we just zero the private pointer */
385     socket->window[wid] = NULL;
386   }
387   *view = NULL;
388   PetscFunctionReturn(0);
389 }
390 
391 /* default window appearance in the PETSC_VIEWER_GLVIS_SOCKET case */
392 PetscErrorCode PetscViewerGLVisInitWindow_Private(PetscViewer viewer, PetscBool mesh, PetscInt dim, const char *name)
393 {
394   PetscErrorCode       ierr;
395   PetscViewerGLVisInfo info;
396   PetscContainer       container;
397 
398   PetscFunctionBegin;
399   ierr = PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&container);CHKERRQ(ierr);
400   if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Viewer was not obtained from PetscGLVisViewerGetNewWindow_Private");
401   ierr = PetscContainerGetPointer(container,(void**)&info);CHKERRQ(ierr);
402   if (info->init) {
403     if (info->pause < 0) {
404       ierr = PetscViewerASCIIPrintf(viewer,"pause\n");CHKERRQ(ierr); /* pause */
405     }
406     PetscFunctionReturn(0);
407   }
408   ierr = PetscViewerASCIIPrintf(viewer,"window_size 800 800\n");CHKERRQ(ierr);
409   if (name) {
410     ierr = PetscViewerASCIIPrintf(viewer,"window_title '%s'\n",name);CHKERRQ(ierr);
411   }
412   if (mesh) {
413     switch (dim) {
414     case 1:
415       break;
416     case 2:
417       ierr = PetscViewerASCIIPrintf(viewer,"keys cmeeppppp\n");CHKERRQ(ierr); /* show colorbar, mesh and ranks */
418       break;
419     case 3: /* TODO: decide default view in 3D */
420       break;
421     }
422   } else {
423     ierr = PetscViewerASCIIPrintf(viewer,"keys cm\n");CHKERRQ(ierr); /* show colorbar and mesh */
424     switch (dim) {
425     case 1:
426       ierr = PetscViewerASCIIPrintf(viewer,"keys RRj\n");CHKERRQ(ierr); /* set to 1D (side view) and turn off perspective */
427       break;
428     case 2:
429       ierr = PetscViewerASCIIPrintf(viewer,"keys Rjl\n");CHKERRQ(ierr); /* set to 2D (top view), turn off perspective and light */
430       break;
431     case 3:
432       break;
433     }
434     ierr = PetscViewerASCIIPrintf(viewer,"autoscale value\n");CHKERRQ(ierr); /* update value-range; keep mesh-extents fixed */
435     if (info->pause == 0) {
436       ierr = PetscViewerASCIIPrintf(viewer,"pause\n");CHKERRQ(ierr); /* pause */
437     }
438   }
439   info->init = PETSC_TRUE;
440   PetscFunctionReturn(0);
441 }
442 
443 static PetscErrorCode PetscViewerDestroy_GLVis(PetscViewer viewer)
444 {
445   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
446   PetscInt         i;
447   PetscErrorCode   ierr;
448 
449   PetscFunctionBegin;
450   for (i=0;i<socket->nwindow;i++) {
451     ierr = PetscViewerDestroy(&socket->window[i]);CHKERRQ(ierr);
452     ierr = PetscFree(socket->windowtitle[i]);CHKERRQ(ierr);
453     ierr = PetscFree(socket->fec_type[i]);CHKERRQ(ierr);
454     ierr = PetscObjectDestroy(&socket->Ufield[i]);CHKERRQ(ierr);
455   }
456   ierr = PetscFree(socket->name);CHKERRQ(ierr);
457   ierr = PetscFree5(socket->window,socket->windowtitle,socket->fec_type,socket->locandbs,socket->Ufield);CHKERRQ(ierr);
458   ierr = PetscViewerDestroy(&socket->meshwindow);CHKERRQ(ierr);
459   ierr = PetscObjectDestroy(&socket->dm);CHKERRQ(ierr);
460   if (socket->destroyctx && socket->userctx) { ierr = (*socket->destroyctx)(socket->userctx);CHKERRQ(ierr); }
461 
462   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetSnapId_C",NULL);CHKERRQ(ierr);
463   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetFields_C",NULL);CHKERRQ(ierr);
464   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr);
465   ierr = PetscFree(socket);CHKERRQ(ierr);
466   viewer->data = NULL;
467   PetscFunctionReturn(0);
468 }
469 
470 static PetscErrorCode PetscViewerSetFromOptions_GLVis(PetscOptionItems *PetscOptionsObject,PetscViewer v)
471 {
472   PetscErrorCode   ierr;
473   PetscViewerGLVis socket = (PetscViewerGLVis)v->data;
474 
475   PetscFunctionBegin;
476   ierr = PetscOptionsHead(PetscOptionsObject,"GLVis PetscViewer Options");CHKERRQ(ierr);
477   ierr = PetscOptionsReal("-viewer_glvis_pause","-1 to pause after each visualization, otherwise sleeps for given seconds",NULL,socket->pause,&socket->pause,NULL);CHKERRQ(ierr);
478   ierr = PetscOptionsTail();CHKERRQ(ierr);
479   PetscFunctionReturn(0);
480 }
481 
482 static PetscErrorCode PetscViewerSetFileName_GLVis(PetscViewer viewer, const char name[])
483 {
484   char             *sport;
485   PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
486   PetscErrorCode   ierr;
487 
488   PetscFunctionBegin;
489   socket->type = PETSC_VIEWER_GLVIS_DUMP;
490   /* we accept localhost^port */
491   ierr = PetscFree(socket->name);CHKERRQ(ierr);
492   ierr = PetscStrallocpy(name,&socket->name);CHKERRQ(ierr);
493   ierr = PetscStrchr(socket->name,'^',&sport);CHKERRQ(ierr);
494   if (sport) {
495     PetscInt port = 19916;
496     size_t   len;
497 
498     *sport++ = 0;
499     ierr = PetscStrlen(sport,&len);CHKERRQ(ierr);
500     if (len) ierr = PetscOptionsStringToInt(sport,&port);
501     if (!ierr) {
502       socket->port = (port != PETSC_DECIDE && port != PETSC_DEFAULT) ? port : 19916;
503     } else {
504       socket->port = 19916;
505     }
506     socket->type = PETSC_VIEWER_GLVIS_SOCKET;
507   }
508   PetscFunctionReturn(0);
509 }
510 
511 /*
512      PetscViewerGLVisOpen - Opens a GLVis type viewer
513 
514   Collective on comm
515 
516   Input Parameters:
517 +  comm      - the MPI communicator
518 .  type      - the viewer type: PETSC_VIEWER_GLVIS_SOCKET for real-time visualization or PETSC_VIEWER_GLVIS_DUMP for dumping to disk
519 .  name      - either the hostname where the GLVis server is running or the base filename for dumping the data for subsequent visualizations
520 -  port      - socket port where the GLVis server is listening. Not referenced when type is PETSC_VIEWER_GLVIS_DUMP
521 
522   Output Parameters:
523 -  viewer    - the PetscViewer object
524 
525   Notes: misses Fortran binding
526 
527   Level: beginner
528 
529 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerGLVisType
530 */
531 PETSC_EXTERN PetscErrorCode PetscViewerGLVisOpen(MPI_Comm comm, PetscViewerGLVisType type, const char* name, PetscInt port, PetscViewer* viewer)
532 {
533   PetscViewerGLVis socket;
534   PetscErrorCode   ierr;
535 
536   PetscFunctionBegin;
537   ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
538   ierr = PetscViewerSetType(*viewer,PETSCVIEWERGLVIS);CHKERRQ(ierr);
539 
540   socket       = (PetscViewerGLVis)((*viewer)->data);
541   ierr         = PetscFree(socket->name);CHKERRQ(ierr);
542   ierr         = PetscStrallocpy(name,&socket->name);CHKERRQ(ierr);
543   socket->type = type;
544   socket->port = port;
545 
546   ierr = PetscViewerSetFromOptions(*viewer);CHKERRQ(ierr);
547   PetscFunctionReturn(0);
548 }
549 
550 /*
551   PETSC_VIEWER_GLVIS_ - Creates an GLVIS PetscViewer shared by all processors in a communicator.
552 
553   Collective on MPI_Comm
554 
555   Input Parameter:
556 . comm - the MPI communicator to share the GLVIS PetscViewer
557 
558   Level: intermediate
559 
560   Notes: misses Fortran bindings
561 
562   Environmental variables:
563 + PETSC_VIEWER_GLVIS_FILENAME : output filename (if specified dump to disk, and takes precedence on PETSC_VIEWER_GLVIS_HOSTNAME)
564 . PETSC_VIEWER_GLVIS_HOSTNAME : machine where the GLVis server is listening (defaults to localhost)
565 - PETSC_VIEWER_GLVIS_PORT     : port opened by the GLVis server (defaults to 19916)
566 
567   Notes:
568   Unlike almost all other PETSc routines, PETSC_VIEWER_GLVIS_ does not return
569   an error code.  The GLVIS PetscViewer is usually used in the form
570 $       XXXView(XXX object, PETSC_VIEWER_GLVIS_(comm));
571 
572 .seealso: PetscViewerGLVISOpen(), PetscViewerGLVisType, PetscViewerCreate(), PetscViewerDestroy()
573 */
574 PETSC_EXTERN PetscViewer PETSC_VIEWER_GLVIS_(MPI_Comm comm)
575 {
576   PetscErrorCode       ierr;
577   PetscBool            flg;
578   PetscViewer          viewer;
579   PetscViewerGLVisType type;
580   char                 fname[PETSC_MAX_PATH_LEN],sport[16];
581   PetscInt             port = 19916; /* default for GLVis */
582 
583   PetscFunctionBegin;
584   ierr = PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
585   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
586   if (!flg) {
587     type = PETSC_VIEWER_GLVIS_SOCKET;
588     ierr = PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_HOSTNAME",fname,PETSC_MAX_PATH_LEN,&flg);
589     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
590     if (!flg) {
591       ierr = PetscStrcpy(fname,"localhost");
592       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
593     }
594     ierr = PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_PORT",sport,16,&flg);
595     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
596     if (flg) {
597       ierr = PetscOptionsStringToInt(sport,&port);
598       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
599     }
600   } else {
601     type = PETSC_VIEWER_GLVIS_DUMP;
602   }
603   ierr = PetscViewerGLVisOpen(comm,type,fname,port,&viewer);
604   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
605   ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
606   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
607   PetscFunctionReturn(viewer);
608 }
609 
610 PETSC_EXTERN PetscErrorCode PetscViewerCreate_GLVis(PetscViewer viewer)
611 {
612   PetscViewerGLVis socket;
613   PetscErrorCode   ierr;
614 
615   PetscFunctionBegin;
616   ierr = PetscNewLog(viewer,&socket);CHKERRQ(ierr);
617 
618   /* defaults to socket viewer */
619   ierr = PetscStrallocpy("localhost",&socket->name);CHKERRQ(ierr);
620   socket->port  = 19916; /* GLVis default listening port */
621   socket->type  = PETSC_VIEWER_GLVIS_SOCKET;
622   socket->pause = 0; /* just pause the first time */
623 
624   viewer->data                = (void*)socket;
625   viewer->ops->destroy        = PetscViewerDestroy_GLVis;
626   viewer->ops->setfromoptions = PetscViewerSetFromOptions_GLVis;
627 
628   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetSnapId_C",PetscViewerGLVisSetSnapId_GLVis);CHKERRQ(ierr);
629   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetFields_C",PetscViewerGLVisSetFields_GLVis);CHKERRQ(ierr);
630   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerSetFileName_GLVis);CHKERRQ(ierr);
631   PetscFunctionReturn(0);
632 }
633 
634 /* this is a private implementation of a SOCKET with ASCII data format
635    GLVis does not currently handle binary socket streams */
636 #if defined(PETSC_HAVE_UNISTD_H)
637 #include <unistd.h>
638 #endif
639 
640 #if !defined(PETSC_HAVE_WINDOWS_H)
641 static PetscErrorCode (*PetscViewerDestroy_ASCII)(PetscViewer);
642 
643 static PetscErrorCode PetscViewerDestroy_ASCII_Socket(PetscViewer viewer)
644 {
645   FILE *stream;
646   PetscErrorCode ierr = 0;
647   PetscFunctionBegin;
648   ierr = PetscViewerASCIIGetPointer(viewer,&stream);CHKERRQ(ierr);
649   if (stream) {
650     ierr = fclose(stream);
651     if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on stream");
652   }
653   ierr = PetscViewerDestroy_ASCII(viewer);CHKERRQ(ierr);
654   PetscFunctionReturn(0);
655 }
656 #endif
657 
658 static PetscErrorCode PetscViewerASCIISocketOpen(MPI_Comm comm,const char* hostname,PetscInt port,PetscViewer* viewer)
659 {
660 #if defined(PETSC_HAVE_WINDOWS_H)
661   PetscFunctionBegin;
662   SETERRQ(comm,PETSC_ERR_SUP,"Not implemented for Windows");
663 #else
664   FILE           *stream = NULL;
665   int            fd;
666   PetscErrorCode ierr;
667 
668   PetscFunctionBegin;
669   PetscValidPointer(hostname,2);
670   PetscValidPointer(viewer,4);
671 #if defined(PETSC_USE_SOCKET_VIEWER)
672   ierr = PetscOpenSocket(hostname,port,&fd);
673 #else
674   SETERRQ(comm,PETSC_ERR_SUP,"Missing Socket viewer");
675 #endif
676   if (ierr) {
677     PetscInt sierr = ierr;
678     char     err[1024];
679 
680     ierr    = PetscSNPrintf(err,1024,"Cannot connect to socket on %s:%D. Socket visualization is disabled\n",hostname,port);CHKERRQ(ierr);
681     ierr    = PetscInfo(NULL,err);CHKERRQ(ierr);
682     *viewer = NULL;
683     PetscFunctionReturn(sierr);
684   } else {
685     char msg[1024];
686 
687     ierr = PetscSNPrintf(msg,1024,"Successfully connect to socket on %s:%D. Socket visualization is enabled\n",hostname,port);CHKERRQ(ierr);
688     ierr = PetscInfo(NULL,msg);CHKERRQ(ierr);
689   }
690   stream = fdopen(fd,"w"); /* Not possible on Windows */
691   if (!stream) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SYS,"Cannot open stream from socket %s:%d",hostname,port);
692   ierr = PetscViewerASCIIOpenWithFILE(PETSC_COMM_SELF,stream,viewer);CHKERRQ(ierr);
693   PetscViewerDestroy_ASCII = (*viewer)->ops->destroy;
694   (*viewer)->ops->destroy = PetscViewerDestroy_ASCII_Socket;
695 #endif
696   PetscFunctionReturn(0);
697 }
698