Actual source code: glvis.c
petsc-3.9.4 2018-09-11
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fdopen() */
3: #include <petsc/private/viewerimpl.h>
4: #include <petsc/private/petscimpl.h>
5: #include <petsc/private/glvisviewerimpl.h>
7: /* we may eventually make this function public */
8: static PetscErrorCode PetscViewerASCIISocketOpen(MPI_Comm,const char*,PetscInt,PetscViewer*);
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 *spacedim; /* geometrical space dimension (just used to initialize the scene) */
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: char* fmt; /* format string for FP values */
29: };
30: typedef struct _n_PetscViewerGLVis *PetscViewerGLVis;
32: /*@
33: PetscViewerGLVisSetPrecision - Set the number of digits for floating point values
35: Not Collective
37: Input Parameters:
38: + viewer - the PetscViewer
39: - prec - the number of digits required
41: Level: beginner
43: .seealso: PetscViewerGLVisOpen(), PetscViewerGLVisSetFields(), PetscViewerCreate(), PetscViewerSetType()
44: @*/
45: PetscErrorCode PetscViewerGLVisSetPrecision(PetscViewer viewer, PetscInt prec)
46: {
51: PetscTryMethod(viewer,"PetscViewerGLVisSetPrecision_C",(PetscViewer,PetscInt),(viewer,prec));
52: return(0);
53: }
55: static PetscErrorCode PetscViewerGLVisSetPrecision_GLVis(PetscViewer viewer, PetscInt prec)
56: {
57: PetscErrorCode ierr;
58: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
61: PetscFree(socket->fmt);
62: if (prec > 0) {
63: PetscMalloc1(16,&socket->fmt);
64: PetscSNPrintf(socket->fmt,16," %%.%De",prec);
65: } else {
66: PetscStrallocpy(" %g",&socket->fmt);
67: }
68: return(0);
69: }
71: /*@
72: PetscViewerGLVisSetSnapId - Set the snapshot id. Only relevant when the viewer is of type PETSC_VIEWER_GLVIS_DUMP
74: Logically Collective on PetscViewer
76: Input Parameters:
77: + viewer - the PetscViewer
78: - id - the current snapshot id in a time-dependent simulation
80: Level: beginner
82: .seealso: PetscViewerGLVisOpen(), PetscViewerGLVisSetFields(), PetscViewerCreate(), PetscViewerSetType()
83: @*/
84: PetscErrorCode PetscViewerGLVisSetSnapId(PetscViewer viewer, PetscInt id)
85: {
91: PetscTryMethod(viewer,"PetscViewerGLVisSetSnapId_C",(PetscViewer,PetscInt),(viewer,id));
92: return(0);
93: }
95: static PetscErrorCode PetscViewerGLVisSetSnapId_GLVis(PetscViewer viewer, PetscInt id)
96: {
97: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
100: socket->snapid = id;
101: return(0);
102: }
104: /*@C
105: PetscViewerGLVisSetFields - Sets the required information to visualize different fields from a vector.
107: Logically Collective on PetscViewer
109: Input Parameters:
110: + viewer - the PetscViewer
111: . nf - number of fields to be visualized
112: . fec_type - the type of finite element to be used to visualize the data (see FiniteElementCollection::Name() in MFEM)
113: . dim - array of space dimension for field vectors (used to initialize the scene)
114: . g2lfields - User routine to compute the local field vectors to be visualized; PetscObject is used in place of Vec on the prototype
115: . Vfield - array of work vectors, one for each field
116: . ctx - User context to store the relevant data to apply g2lfields
117: - destroyctx - Destroy function for userctx
119: Notes: g2lfields is called on the vector V to be visualized in order to extract the relevant dofs to be put in Vfield[], as
120: .vb
121: g2lfields((PetscObject)V,nfields,(PetscObject*)Vfield[],ctx).
122: .ve
123: For vector spaces, the block size of Vfield[i] represents the vector dimension. It misses the Fortran bindings.
124: The names of the Vfield vectors will be displayed in the window title.
126: Level: intermediate
128: .seealso: PetscViewerGLVisOpen(), PetscViewerCreate(), PetscViewerSetType(), PetscObjectSetName()
129: @*/
130: PetscErrorCode PetscViewerGLVisSetFields(PetscViewer viewer, PetscInt nf, const char* fec_type[], PetscInt dim[], PetscErrorCode(*g2l)(PetscObject,PetscInt,PetscObject[],void*), PetscObject Vfield[], void* ctx, PetscErrorCode(*destroyctx)(void*))
131: {
137: if (!fec_type) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"You need to provide the FiniteElementCollection names for the fields");
141: PetscTryMethod(viewer,"PetscViewerGLVisSetFields_C",(PetscViewer,PetscInt,const char*[],PetscInt[],PetscErrorCode(*)(PetscObject,PetscInt,PetscObject[],void*),PetscObject[],void*,PetscErrorCode(*)(void*)),(viewer,nf,fec_type,dim,g2l,Vfield,ctx,destroyctx));
142: return(0);
143: }
145: static PetscErrorCode PetscViewerGLVisSetFields_GLVis(PetscViewer viewer, PetscInt nfields, const char* fec_type[], PetscInt dim[], PetscErrorCode(*g2l)(PetscObject,PetscInt,PetscObject[],void*), PetscObject Vfield[], void* ctx, PetscErrorCode(*destroyctx)(void*))
146: {
147: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
148: PetscInt i;
149: PetscErrorCode ierr;
152: 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);
153: if (!socket->nwindow) {
154: socket->nwindow = nfields;
156: PetscCalloc5(nfields,&socket->window,nfields,&socket->windowtitle,nfields,&socket->fec_type,nfields,&socket->spacedim,nfields,&socket->Ufield);
157: for (i=0;i<nfields;i++) {
158: const char *name;
160: PetscObjectGetName(Vfield[i],&name);
161: PetscStrallocpy(name,&socket->windowtitle[i]);
162: PetscStrallocpy(fec_type[i],&socket->fec_type[i]);
163: PetscObjectReference(Vfield[i]);
164: socket->Ufield[i] = Vfield[i];
165: socket->spacedim[i] = dim[i];
166: }
167: }
168: /* number of fields are not allowed to vary */
169: if (nfields != socket->nwindow) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot visualize %D fields using %D socket windows",nfields,socket->nwindow);
170: socket->g2lfield = g2l;
171: if (socket->destroyctx && socket->userctx) { (*socket->destroyctx)(socket->userctx); }
172: socket->userctx = ctx;
173: socket->destroyctx = destroyctx;
174: return(0);
175: }
177: static PetscErrorCode PetscViewerGLVisInfoDestroy_Private(void *ptr)
178: {
179: PetscViewerGLVisInfo info = (PetscViewerGLVisInfo)ptr;
180: PetscErrorCode ierr;
183: PetscFree(info->fmt);
184: PetscFree(info);
185: return(0);
186: }
188: /* we can decide to prevent specific processes from using the viewer */
189: static PetscErrorCode PetscViewerGLVisAttachInfo_Private(PetscViewer viewer, PetscViewer window)
190: {
191: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
192: PetscErrorCode ierr;
193: PetscContainer container;
194: PetscViewerGLVisInfo info;
197: PetscObjectQuery((PetscObject)window,"_glvis_info_container",(PetscObject*)&container);
198: if (!container) {
199: PetscNew(&info);
200: info->enabled = PETSC_TRUE;
201: info->init = PETSC_FALSE;
202: info->pause = socket->pause;
203: PetscContainerCreate(PetscObjectComm((PetscObject)window),&container);
204: PetscContainerSetPointer(container,(void*)info);
205: PetscContainerSetUserDestroy(container,PetscViewerGLVisInfoDestroy_Private);
206: PetscObjectCompose((PetscObject)window,"_glvis_info_container",(PetscObject)container);
207: PetscContainerDestroy(&container);
208: } else {
209: PetscContainerGetPointer(container,(void**)&info);
210: }
211: PetscFree(info->fmt);
212: PetscStrallocpy(socket->fmt,&info->fmt);
213: return(0);
214: }
216: static PetscErrorCode PetscViewerGLVisGetNewWindow_Private(PetscViewer viewer,PetscViewer *view)
217: {
218: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
219: PetscViewer window = NULL;
220: PetscBool ldis,dis;
221: PetscErrorCode ierr;
224: PetscViewerASCIISocketOpen(PETSC_COMM_SELF,socket->name,socket->port,&window);
225: /* if we could not estabilish a connection the first time,
226: we disable the socket viewer */
227: ldis = ierr ? PETSC_TRUE : PETSC_FALSE;
228: MPI_Allreduce(&ldis,&dis,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)viewer));
229: if (dis) {
230: socket->status = PETSCVIEWERGLVIS_DISABLED;
231: PetscViewerDestroy(&window);
232: }
233: *view = window;
234: return(0);
235: }
237: PetscErrorCode PetscViewerGLVisPause_Private(PetscViewer viewer)
238: {
239: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
240: PetscErrorCode ierr;
243: if (socket->pause > 0) {
244: PetscSleep(socket->pause);
245: }
246: return(0);
247: }
249: /* DM specific support */
250: PetscErrorCode PetscViewerGLVisSetDM_Private(PetscViewer viewer, PetscObject dm)
251: {
252: PetscErrorCode ierr;
253: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
256: if (socket->dm && socket->dm != dm) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot change DM associated with the GLVis viewer");
257: if (!socket->dm) {
258: PetscErrorCode (*setupwithdm)(PetscObject,PetscViewer) = NULL;
260: PetscObjectQueryFunction(dm,"DMSetUpGLVisViewer_C",&setupwithdm);
261: if (setupwithdm) {
262: (*setupwithdm)(dm,viewer);
263: } else SETERRQ1(PetscObjectComm(dm),PETSC_ERR_SUP,"No support for DM type %s",dm->type_name);
264: PetscObjectReference(dm);
265: socket->dm = dm;
266: }
267: return(0);
268: }
270: PetscErrorCode PetscViewerGLVisGetDMWindow_Private(PetscViewer viewer,PetscViewer* view)
271: {
272: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
273: PetscErrorCode ierr;
276: if (!socket->meshwindow) {
277: if (socket->type == PETSC_VIEWER_GLVIS_SOCKET) {
278: PetscViewerGLVisGetNewWindow_Private(viewer,&socket->meshwindow);
279: } else {
280: size_t len;
281: PetscBool isstdout;
283: PetscStrlen(socket->name,&len);
284: PetscStrcmp(socket->name,"stdout",&isstdout);
285: if (!socket->name || !len || isstdout) {
286: PetscViewerASCIIOpen(PETSC_COMM_SELF,"stdout",&socket->meshwindow);
287: } else {
288: PetscMPIInt rank;
289: char filename[PETSC_MAX_PATH_LEN];
290: MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
291: PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s-mesh.%06d",socket->name,rank);
292: PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&socket->meshwindow);
293: }
294: }
295: if (socket->meshwindow) {
296: PetscViewerPushFormat(socket->meshwindow,PETSC_VIEWER_ASCII_GLVIS);
297: }
298: }
299: if (socket->meshwindow) {
300: PetscViewerGLVisAttachInfo_Private(viewer,socket->meshwindow);
301: }
302: *view = socket->meshwindow;
303: return(0);
304: }
306: PetscErrorCode PetscViewerGLVisGetType_Private(PetscViewer viewer,PetscViewerGLVisType *type)
307: {
308: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
312: *type = socket->type;
313: return(0);
314: }
316: /* 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 */
317: PetscErrorCode PetscViewerGLVisGetStatus_Private(PetscViewer viewer, PetscViewerGLVisStatus *sockstatus)
318: {
319: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
323: if (socket->type == PETSC_VIEWER_GLVIS_DUMP) {
324: socket->status = PETSCVIEWERGLVIS_DISCONNECTED;
325: } else if (socket->status == PETSCVIEWERGLVIS_DISCONNECTED && socket->nwindow) {
326: PetscInt i;
327: PetscBool lconn,conn;
330: for (i=0,lconn=PETSC_TRUE;i<socket->nwindow;i++)
331: if (!socket->window[i])
332: lconn = PETSC_FALSE;
334: MPI_Allreduce(&lconn,&conn,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)viewer));
335: if (conn) socket->status = PETSCVIEWERGLVIS_CONNECTED;
336: }
337: *sockstatus = socket->status;
338: return(0);
339: }
341: PetscErrorCode PetscViewerGLVisGetDM_Private(PetscViewer viewer, PetscObject* dm)
342: {
343: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
346: *dm = socket->dm;
347: return(0);
348: }
350: PetscErrorCode PetscViewerGLVisGetFields_Private(PetscViewer viewer, PetscInt* nfield, const char **fec[], PetscInt *spacedim[], PetscErrorCode(**g2lfield)(PetscObject,PetscInt,PetscObject[],void*), PetscObject *Ufield[], void **userctx)
351: {
352: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
355: if (nfield) *nfield = socket->nwindow;
356: if (fec) *fec = (const char**)socket->fec_type;
357: if (spacedim) *spacedim = socket->spacedim;
358: if (g2lfield) *g2lfield = socket->g2lfield;
359: if (Ufield) *Ufield = socket->Ufield;
360: if (userctx) *userctx = socket->userctx;
361: return(0);
362: }
364: /* accessor routines for the viewer windows:
365: PETSC_VIEWER_GLVIS_DUMP : it returns a new viewer every time
366: PETSC_VIEWER_GLVIS_SOCKET : it returns the socket, and creates it if not yet done.
367: */
368: PetscErrorCode PetscViewerGLVisGetWindow_Private(PetscViewer viewer,PetscInt wid,PetscViewer* view)
369: {
370: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
371: PetscViewerGLVisStatus status;
372: PetscErrorCode ierr;
377: 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);
378: status = socket->status;
379: if (socket->type == PETSC_VIEWER_GLVIS_DUMP && socket->window[wid]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Window %D is already in use",wid);
380: switch (status) {
381: case PETSCVIEWERGLVIS_DISCONNECTED:
382: if (socket->window[wid]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"This should not happen");
383: if (socket->type == PETSC_VIEWER_GLVIS_DUMP) {
384: size_t len;
385: PetscBool isstdout;
387: PetscStrlen(socket->name,&len);
388: PetscStrcmp(socket->name,"stdout",&isstdout);
389: if (!socket->name || !len || isstdout) {
390: PetscViewerASCIIOpen(PETSC_COMM_SELF,"stdout",&socket->window[wid]);
391: } else {
392: PetscMPIInt rank;
393: char filename[PETSC_MAX_PATH_LEN];
395: MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
396: PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s-%s-%d.%06d",socket->name,socket->windowtitle[wid],socket->snapid,rank);
397: PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&socket->window[wid]);
398: }
399: } else {
400: PetscViewerGLVisGetNewWindow_Private(viewer,&socket->window[wid]);
401: }
402: if (socket->window[wid]) {
403: PetscViewerPushFormat(socket->window[wid],PETSC_VIEWER_ASCII_GLVIS);
404: }
405: *view = socket->window[wid];
406: break;
407: case PETSCVIEWERGLVIS_CONNECTED:
408: *view = socket->window[wid];
409: break;
410: case PETSCVIEWERGLVIS_DISABLED:
411: *view = NULL;
412: break;
413: default:
414: SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unhandled socket status %d\n",(int)status);
415: break;
416: }
417: if (*view) {
418: PetscViewerGLVisAttachInfo_Private(viewer,*view);
419: }
420: return(0);
421: }
423: /* Restore the window viewer
424: PETSC_VIEWER_GLVIS_DUMP : destroys the temporary created ASCII viewer used for dumping
425: PETSC_VIEWER_GLVIS_SOCKET: - if the returned window viewer is not NULL, just zeros the pointer.
426: - it the returned window viewer is NULL, assumes something went wrong
427: with the socket (i.e. SIGPIPE when a user closes the popup window)
428: and that the caller already handled it (see VecView_GLVis).
429: */
430: PetscErrorCode PetscViewerGLVisRestoreWindow_Private(PetscViewer viewer,PetscInt wid, PetscViewer* view)
431: {
432: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
433: PetscErrorCode ierr;
438: 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);
439: if (*view && *view != socket->window[wid]) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_USER,"Viewer was not obtained from PetscViewerGLVisGetWindow");
440: if (*view) {
441: PetscViewerFlush(*view);
442: PetscBarrier((PetscObject)viewer);
443: }
444: if (socket->type == PETSC_VIEWER_GLVIS_DUMP) { /* destroy the viewer, as it is associated with a single time step */
445: PetscViewerDestroy(&socket->window[wid]);
446: } else if (!*view) { /* something went wrong (SIGPIPE) so we just zero the private pointer */
447: socket->window[wid] = NULL;
448: }
449: *view = NULL;
450: return(0);
451: }
453: /* default window appearance in the PETSC_VIEWER_GLVIS_SOCKET case */
454: PetscErrorCode PetscViewerGLVisInitWindow_Private(PetscViewer viewer, PetscBool mesh, PetscInt dim, const char *name)
455: {
456: PetscErrorCode ierr;
457: PetscViewerGLVisInfo info;
458: PetscContainer container;
461: PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&container);
462: if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Viewer was not obtained from PetscGLVisViewerGetNewWindow_Private");
463: PetscContainerGetPointer(container,(void**)&info);
464: if (info->init) {
465: if (info->pause < 0) {
466: PetscViewerASCIIPrintf(viewer,"pause\n"); /* pause */
467: }
468: return(0);
469: }
470: PetscViewerASCIIPrintf(viewer,"window_size 800 800\n");
471: if (name) {
472: PetscViewerASCIIPrintf(viewer,"window_title '%s'\n",name);
473: }
474: if (mesh) {
475: switch (dim) {
476: case 1:
477: break;
478: case 2:
479: PetscViewerASCIIPrintf(viewer,"keys cmeeppppp\n"); /* show colorbar, mesh and ranks */
480: break;
481: case 3: /* TODO: decide default view in 3D */
482: break;
483: }
484: } else {
485: PetscViewerASCIIPrintf(viewer,"keys cm\n"); /* show colorbar and mesh */
486: switch (dim) {
487: case 1:
488: PetscViewerASCIIPrintf(viewer,"keys RRj\n"); /* set to 1D (side view) and turn off perspective */
489: break;
490: case 2:
491: PetscViewerASCIIPrintf(viewer,"keys Rjl\n"); /* set to 2D (top view), turn off perspective and light */
492: break;
493: case 3:
494: break;
495: }
496: PetscViewerASCIIPrintf(viewer,"autoscale value\n"); /* update value-range; keep mesh-extents fixed */
497: if (info->pause == 0) {
498: PetscViewerASCIIPrintf(viewer,"pause\n"); /* pause */
499: }
500: }
501: info->init = PETSC_TRUE;
502: return(0);
503: }
505: static PetscErrorCode PetscViewerDestroy_GLVis(PetscViewer viewer)
506: {
507: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
508: PetscInt i;
509: PetscErrorCode ierr;
512: for (i=0;i<socket->nwindow;i++) {
513: PetscViewerDestroy(&socket->window[i]);
514: PetscFree(socket->windowtitle[i]);
515: PetscFree(socket->fec_type[i]);
516: PetscObjectDestroy(&socket->Ufield[i]);
517: }
518: PetscFree(socket->name);
519: PetscFree5(socket->window,socket->windowtitle,socket->fec_type,socket->spacedim,socket->Ufield);
520: PetscFree(socket->fmt);
521: PetscViewerDestroy(&socket->meshwindow);
522: PetscObjectDestroy(&socket->dm);
523: if (socket->destroyctx && socket->userctx) { (*socket->destroyctx)(socket->userctx); }
525: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetPrecision_C",NULL);
526: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetSnapId_C",NULL);
527: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetFields_C",NULL);
528: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
529: PetscFree(socket);
530: viewer->data = NULL;
531: return(0);
532: }
534: static PetscErrorCode PetscViewerSetFromOptions_GLVis(PetscOptionItems *PetscOptionsObject,PetscViewer v)
535: {
536: PetscErrorCode ierr;
537: PetscViewerGLVis socket = (PetscViewerGLVis)v->data;
540: PetscOptionsHead(PetscOptionsObject,"GLVis PetscViewer Options");
541: PetscOptionsReal("-viewer_glvis_pause","-1 to pause after each visualization, otherwise sleeps for given seconds",NULL,socket->pause,&socket->pause,NULL);
542: PetscOptionsTail();
543: return(0);
544: }
546: static PetscErrorCode PetscViewerSetFileName_GLVis(PetscViewer viewer, const char name[])
547: {
548: char *sport;
549: PetscViewerGLVis socket = (PetscViewerGLVis)viewer->data;
550: PetscErrorCode ierr;
553: socket->type = PETSC_VIEWER_GLVIS_DUMP;
554: /* we accept localhost^port */
555: PetscFree(socket->name);
556: PetscStrallocpy(name,&socket->name);
557: PetscStrchr(socket->name,'^',&sport);
558: if (sport) {
559: PetscInt port = 19916;
560: size_t len;
562: *sport++ = 0;
563: PetscStrlen(sport,&len);
564: if (len) PetscOptionsStringToInt(sport,&port);
565: if (!ierr) {
566: socket->port = (port != PETSC_DECIDE && port != PETSC_DEFAULT) ? port : 19916;
567: } else {
568: socket->port = 19916;
569: }
570: socket->type = PETSC_VIEWER_GLVIS_SOCKET;
571: }
572: return(0);
573: }
575: /*
576: PetscViewerGLVisOpen - Opens a GLVis type viewer
578: Collective on comm
580: Input Parameters:
581: + comm - the MPI communicator
582: . type - the viewer type: PETSC_VIEWER_GLVIS_SOCKET for real-time visualization or PETSC_VIEWER_GLVIS_DUMP for dumping to disk
583: . name - either the hostname where the GLVis server is running or the base filename for dumping the data for subsequent visualizations
584: - port - socket port where the GLVis server is listening. Not referenced when type is PETSC_VIEWER_GLVIS_DUMP
586: Output Parameters:
587: - viewer - the PetscViewer object
589: Notes: misses Fortran binding
591: Level: beginner
593: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerGLVisType
594: */
595: PETSC_EXTERN PetscErrorCode PetscViewerGLVisOpen(MPI_Comm comm, PetscViewerGLVisType type, const char* name, PetscInt port, PetscViewer* viewer)
596: {
597: PetscViewerGLVis socket;
598: PetscErrorCode ierr;
601: PetscViewerCreate(comm,viewer);
602: PetscViewerSetType(*viewer,PETSCVIEWERGLVIS);
604: socket = (PetscViewerGLVis)((*viewer)->data);
605: socket->type = type;
606: if (type == PETSC_VIEWER_GLVIS_DUMP || name) {
607: PetscFree(socket->name);
608: PetscStrallocpy(name,&socket->name);
609: }
610: socket->port = (!port || port == PETSC_DETERMINE || port == PETSC_DECIDE) ? 19916 : port;
612: PetscViewerSetFromOptions(*viewer);
613: return(0);
614: }
616: /*
617: PETSC_VIEWER_GLVIS_ - Creates an GLVIS PetscViewer shared by all processors in a communicator.
619: Collective on MPI_Comm
621: Input Parameter:
622: . comm - the MPI communicator to share the GLVIS PetscViewer
624: Level: intermediate
626: Notes: misses Fortran bindings
628: Environmental variables:
629: + PETSC_VIEWER_GLVIS_FILENAME : output filename (if specified dump to disk, and takes precedence on PETSC_VIEWER_GLVIS_HOSTNAME)
630: . PETSC_VIEWER_GLVIS_HOSTNAME : machine where the GLVis server is listening (defaults to localhost)
631: - PETSC_VIEWER_GLVIS_PORT : port opened by the GLVis server (defaults to 19916)
633: Notes:
634: Unlike almost all other PETSc routines, PETSC_VIEWER_GLVIS_ does not return
635: an error code. The GLVIS PetscViewer is usually used in the form
636: $ XXXView(XXX object, PETSC_VIEWER_GLVIS_(comm));
638: .seealso: PetscViewerGLVISOpen(), PetscViewerGLVisType, PetscViewerCreate(), PetscViewerDestroy()
639: */
640: PETSC_EXTERN PetscViewer PETSC_VIEWER_GLVIS_(MPI_Comm comm)
641: {
642: PetscErrorCode ierr;
643: PetscBool flg;
644: PetscViewer viewer;
645: PetscViewerGLVisType type;
646: char fname[PETSC_MAX_PATH_LEN],sport[16];
647: PetscInt port = 19916; /* default for GLVis */
650: PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
651: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
652: if (!flg) {
653: type = PETSC_VIEWER_GLVIS_SOCKET;
654: PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_HOSTNAME",fname,PETSC_MAX_PATH_LEN,&flg);
655: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
656: if (!flg) {
657: PetscStrcpy(fname,"localhost");
658: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
659: }
660: PetscOptionsGetenv(comm,"PETSC_VIEWER_GLVIS_PORT",sport,16,&flg);
661: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
662: if (flg) {
663: PetscOptionsStringToInt(sport,&port);
664: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
665: }
666: } else {
667: type = PETSC_VIEWER_GLVIS_DUMP;
668: }
669: PetscViewerGLVisOpen(comm,type,fname,port,&viewer);
670: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
671: PetscObjectRegisterDestroy((PetscObject)viewer);
672: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_GLVIS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
673: PetscFunctionReturn(viewer);
674: }
676: PETSC_EXTERN PetscErrorCode PetscViewerCreate_GLVis(PetscViewer viewer)
677: {
678: PetscViewerGLVis socket;
679: PetscErrorCode ierr;
682: PetscNewLog(viewer,&socket);
684: /* defaults to socket viewer */
685: PetscStrallocpy("localhost",&socket->name);
686: socket->port = 19916; /* GLVis default listening port */
687: socket->type = PETSC_VIEWER_GLVIS_SOCKET;
688: socket->pause = 0; /* just pause the first time */
690: /* defaults to full precision */
691: PetscStrallocpy(" %g",&socket->fmt);
693: viewer->data = (void*)socket;
694: viewer->ops->destroy = PetscViewerDestroy_GLVis;
695: viewer->ops->setfromoptions = PetscViewerSetFromOptions_GLVis;
697: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetPrecision_C",PetscViewerGLVisSetPrecision_GLVis);
698: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetSnapId_C",PetscViewerGLVisSetSnapId_GLVis);
699: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerGLVisSetFields_C",PetscViewerGLVisSetFields_GLVis);
700: PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerSetFileName_GLVis);
701: return(0);
702: }
704: /* this is a private implementation of a SOCKET with ASCII data format
705: GLVis does not currently handle binary socket streams */
706: #if defined(PETSC_HAVE_UNISTD_H)
707: #include <unistd.h>
708: #endif
710: #if !defined(PETSC_HAVE_WINDOWS_H)
711: static PetscErrorCode (*PetscViewerDestroy_ASCII)(PetscViewer);
713: static PetscErrorCode PetscViewerDestroy_ASCII_Socket(PetscViewer viewer)
714: {
715: FILE *stream;
716: PetscErrorCode 0;
718: PetscViewerASCIIGetPointer(viewer,&stream);
719: if (stream) {
720: fclose(stream);
721: if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on stream");
722: }
723: PetscViewerDestroy_ASCII(viewer);
724: return(0);
725: }
726: #endif
728: static PetscErrorCode PetscViewerASCIISocketOpen(MPI_Comm comm,const char* hostname,PetscInt port,PetscViewer* viewer)
729: {
730: #if defined(PETSC_HAVE_WINDOWS_H)
732: SETERRQ(comm,PETSC_ERR_SUP,"Not implemented for Windows");
733: #else
734: FILE *stream = NULL;
735: int fd;
741: #if defined(PETSC_USE_SOCKET_VIEWER)
742: PetscOpenSocket(hostname,port,&fd);
743: #else
744: SETERRQ(comm,PETSC_ERR_SUP,"Missing Socket viewer");
745: #endif
746: if (ierr) {
747: PetscInt sierr;
748: char err[1024];
750: PetscSNPrintf(err,1024,"Cannot connect to socket on %s:%D. Socket visualization is disabled\n",hostname,port);
751: PetscInfo(NULL,err);
752: *viewer = NULL;
753: PetscFunctionReturn(sierr);
754: } else {
755: char msg[1024];
757: PetscSNPrintf(msg,1024,"Successfully connect to socket on %s:%D. Socket visualization is enabled\n",hostname,port);
758: PetscInfo(NULL,msg);
759: }
760: stream = fdopen(fd,"w"); /* Not possible on Windows */
761: if (!stream) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SYS,"Cannot open stream from socket %s:%d",hostname,port);
762: PetscViewerASCIIOpenWithFILE(PETSC_COMM_SELF,stream,viewer);
763: PetscViewerDestroy_ASCII = (*viewer)->ops->destroy;
764: (*viewer)->ops->destroy = PetscViewerDestroy_ASCII_Socket;
765: #endif
766: return(0);
767: }