Actual source code: vecglvis.c
petsc-3.8.3 2017-12-09
1: #include <petsc/private/glvisviewerimpl.h>
2: #include <petsc/private/glvisvecimpl.h>
4: static PetscErrorCode PetscViewerGLVisVecInfoDestroy_Private(void *ptr)
5: {
6: PetscViewerGLVisVecInfo info = (PetscViewerGLVisVecInfo)ptr;
7: PetscErrorCode ierr;
10: PetscFree(info->fec_type);
11: PetscFree(info);
12: return(0);
13: }
15: #if defined(PETSC_HAVE_SETJMP_H) && !defined(PETSC_MISSING_SIGPIPE)
16: #include <setjmp.h>
17: #include <signal.h>
19: #if defined(PETSC_HAVE_WINDOWS_H)
20: #define DEV_NULL "NUL"
21: #else
22: #define DEV_NULL "/dev/null"
23: #endif
25: static jmp_buf PetscGLVisSigPipeJmpBuf;
27: static void PetscGLVisSigPipeHandler(PETSC_UNUSED int sig)
28: {
29: longjmp(PetscGLVisSigPipeJmpBuf,1);
30: }
31: #endif
33: /* the main function to visualize vectors using GLVis */
34: PetscErrorCode VecView_GLVis(Vec U,PetscViewer viewer)
35: {
36: PetscObject dm;
37: PetscErrorCode (*g2lfields)(PetscObject,PetscInt,PetscObject[],void*);
38: Vec *Ufield;
39: const char **fec_type,**name;
40: PetscViewerGLVisStatus sockstatus;
41: PetscViewerGLVisType socktype;
42: void *userctx;
43: PetscInt i,nfields,*locandbs;
44: PetscErrorCode ierr;
47: PetscViewerGLVisGetStatus_Private(viewer,&sockstatus);
48: if (sockstatus == PETSCVIEWERGLVIS_DISABLED) return(0);
49: /* if the user did not customize the viewer through the API, we need extra data that can be attached to the Vec */
50: PetscViewerGLVisGetFields_Private(viewer,&nfields,NULL,NULL,NULL,NULL,NULL,NULL);
51: if (!nfields) {
52: PetscObjectQuery((PetscObject)U, "__PETSc_dm",&dm);
53: if (dm) {
54: PetscViewerGLVisSetDM_Private(viewer,dm);
55: } else SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_SUP,"You need to provide a DM or use PetscViewerGLVisSetFields()");
56: }
57: PetscViewerGLVisGetFields_Private(viewer,&nfields,&name,&fec_type,&locandbs,&g2lfields,(PetscObject**)&Ufield,&userctx);
58: PetscViewerGLVisGetType_Private(viewer,&socktype);
59: PetscViewerGLVisGetDM_Private(viewer,&dm);
60: if (!dm) {
61: PetscObjectQuery((PetscObject)U, "__PETSc_dm",&dm);
62: }
63: if (!dm) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_SUP,"Mesh not present");
65: if (!Ufield[0]) {
66: for (i=0;i<nfields;i++) {
67: PetscViewerGLVisVecInfo info;
68: PetscContainer container;
70: VecCreateMPI(PetscObjectComm((PetscObject)U),locandbs[3*i],PETSC_DECIDE,&Ufield[i]);
71: PetscObjectSetName((PetscObject)Ufield[i],name[i]);
72: VecSetBlockSize(Ufield[i],locandbs[3*i+1]);
74: /* attach visualization info to the vector */
75: PetscNew(&info);
76: PetscStrallocpy(fec_type[i],&info->fec_type);
77: PetscContainerCreate(PetscObjectComm((PetscObject)U),&container);
78: PetscContainerSetPointer(container,(void*)info);
79: PetscContainerSetUserDestroy(container,PetscViewerGLVisVecInfoDestroy_Private);
80: PetscObjectCompose((PetscObject)Ufield[i],"_glvis_info_container",(PetscObject)container);
81: PetscContainerDestroy(&container);
83: /* attach the mesh to the viz vectors */
84: PetscObjectCompose((PetscObject)Ufield[i], "__PETSc_dm",dm);
85: }
86: }
88: /* user-provided sampling */
89: if (g2lfields) {
90: (*g2lfields)((PetscObject)U,nfields,(PetscObject*)Ufield,userctx);
91: } else {
92: VecCopy(U,Ufield[0]);
93: }
95: /* TODO callback to user routine to disable/enable subdomains */
96: for (i=0;i<nfields;i++) {
97: PetscViewer view;
99: PetscObjectQuery((PetscObject)Ufield[i], "__PETSc_dm",&dm);
100: PetscViewerGLVisGetWindow_Private(viewer,i,&view);
101: if (!view) continue; /* socket window has been closed */
102: if (socktype == PETSC_VIEWER_GLVIS_DUMP) {
103: PetscObjectView(dm,view);
104: VecView(Ufield[i],view);
105: } else {
106: /* It may happen that the user has closed the GLVis window */
107: #if defined(PETSC_HAVE_SETJMP_H) && !defined(PETSC_MISSING_SIGPIPE)
108: void (*sighdl)(int) = signal(SIGPIPE,PetscGLVisSigPipeHandler);
109: if (!setjmp(PetscGLVisSigPipeJmpBuf)) {
110: #endif
111: PetscMPIInt size,rank;
113: MPI_Comm_size(PetscObjectComm(dm),&size);
114: MPI_Comm_rank(PetscObjectComm(dm),&rank);
115: PetscViewerASCIIPrintf(view,"parallel %D %D\nsolution\n",size,rank);
116: PetscObjectView(dm,view);
117: VecView(Ufield[i],view);
118: PetscViewerGLVisInitWindow_Private(view,PETSC_FALSE,locandbs[3*i+2],name[i]);
119: #if defined(PETSC_HAVE_SETJMP_H) && !defined(PETSC_MISSING_SIGPIPE)
120: } else {
121: FILE *sock,*null = fopen(DEV_NULL,"w");
122: PetscInt readonly;
124: VecLockGet(Ufield[i],&readonly);
125: if (readonly) {
126: VecLockPop(Ufield[i]);
127: }
128: PetscViewerASCIIGetPointer(view,&sock);
129: PetscViewerASCIISetFILE(view,null);
130: PetscViewerDestroy(&view);
131: (void)fclose(sock);
132: }
133: (void)signal(SIGPIPE,sighdl);
134: #endif
135: }
136: PetscViewerGLVisRestoreWindow_Private(viewer,i,&view);
137: }
138: PetscViewerGLVisPause_Private(viewer);
139: return(0);
140: }