Actual source code: viewreg.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
  3: #if defined(PETSC_HAVE_SAWS)
  4: #include <petscviewersaws.h>
  5: #endif

  7: PetscFunctionList PetscViewerList = 0;

 11: /*@C
 12:    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user

 14:    Collective on MPI_Comm

 16:    Input Parameters:
 17: +  comm - the communicator to own the viewer
 18: .  pre - the string to prepend to the name or NULL
 19: -  name - the option one is seeking

 21:    Output Parameter:
 22: +  viewer - the viewer
 23: .  format - the PetscViewerFormat requested by the user
 24: -  set - PETSC_TRUE if found, else PETSC_FALSE

 26:    Level: intermediate

 28:    Notes: If no value is provided ascii:stdout is used
 29: $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab, 
 30:                                                   for example ascii::ascii_info prints just the information about the object not all details
 31:                                                   unless :append is given filename opens in write mode, overwriting what was already there
 32: $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
 33: $       draw[:drawtype]                           for example, draw:tikz  or draw:x
 34: $       socket[:port]                             defaults to the standard output port
 35: $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)

 37:    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur

 39: .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
 40:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
 41:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
 42:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
 43:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
 44:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
 45:           PetscOptionsFList(), PetscOptionsEList()
 46: @*/
 47: PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
 48: {
 49:   char           *value;
 51:   PetscBool      flag,hashelp;


 56:   PetscOptionsHasName(NULL,"-help",&hashelp);
 57:   if (hashelp) {
 58:     (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers display of a PETSc object to screen or ASCII file","PetscOptionsGetViewer");
 59:     (*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers saving of a PETSc object to a binary file","PetscOptionsGetViewer");
 60:     (*PetscHelpPrintf)(comm,"  -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Triggers drawing of a PETSc object","PetscOptionsGetViewer");
 61:     (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Triggers push of a PETSc object to a Unix socket","PetscOptionsGetViewer");
 62:     (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Triggers publishing of a PETSc object to SAWs","PetscOptionsGetViewer");
 63:   }

 65:   if (format) *format = PETSC_VIEWER_DEFAULT;
 66:   if (set) *set = PETSC_FALSE;
 67:   PetscOptionsFindPair_Private(pre,name,&value,&flag);
 68:   if (flag) {
 69:     if (set) *set = PETSC_TRUE;
 70:     if (!value) {
 71:       PetscViewerASCIIGetStdout(comm,viewer);
 72:       PetscObjectReference((PetscObject)*viewer);
 73:     } else {
 74:       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
 75:       PetscInt   cnt;
 76:       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};

 78:       PetscStrallocpy(value,&loc0_vtype);
 79:       PetscStrchr(loc0_vtype,':',&loc1_fname);
 80:       if (loc1_fname) {
 81:         *loc1_fname++ = 0;
 82:         PetscStrchr(loc1_fname,':',&loc2_fmt);
 83:       }
 84:       if (loc2_fmt) {
 85:         *loc2_fmt++ = 0;
 86:         PetscStrchr(loc2_fmt,':',&loc3_fmode);
 87:       }
 88:       if (loc3_fmode) *loc3_fmode++ = 0;
 89:       PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);
 90:       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
 91:       if (!loc1_fname) {
 92:         switch (cnt) {
 93:         case 0:
 94:           PetscViewerASCIIGetStdout(comm,viewer);
 95:           break;
 96:         case 1:
 97:           if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
 98:           break;
 99:         case 2:
100:           if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
101:           break;
102: #if defined(PETSC_USE_SOCKET_VIEWER)
103:         case 3:
104:           if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
105:           break;
106: #endif
107: #if defined(PETSC_HAVE_MATLAB_ENGINE)
108:         case 4:
109:           if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
110:           break;
111: #endif
112: #if defined(PETSC_HAVE_SAWS)
113:         case 5:
114:           if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
115:           break;
116: #endif
117: #if defined(PETSC_HAVE_HDF5)
118:         case 7:
119:           if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
120:           break;
121: #endif
122:         default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
123:         }
124:         PetscObjectReference((PetscObject)*viewer);
125:       } else {
126:         if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
127:           PetscViewerASCIIGetStdout(comm,viewer);
128:           PetscObjectReference((PetscObject)*viewer);
129:         } else {
130:           PetscFileMode fmode;
131:           PetscViewerCreate(comm,viewer);
132:           PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");
133:           fmode = FILE_MODE_WRITE;
134:           if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
135:             PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);
136:             if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
137:           }
138:           PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
139:           PetscViewerFileSetName(*viewer,loc1_fname);
140:           PetscViewerDrawSetDrawType(*viewer,loc1_fname);
141:         }
142:       }
143:       if (loc2_fmt && *loc2_fmt) {
144:         PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
145:         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
146:       }
147:       PetscViewerSetUp(*viewer);
148:       PetscFree(loc0_vtype);
149:     }
150:   }
151:   return(0);
152: }

156: /*@
157:    PetscViewerCreate - Creates a viewing context

159:    Collective on MPI_Comm

161:    Input Parameter:
162: .  comm - MPI communicator

164:    Output Parameter:
165: .  inviewer - location to put the PetscViewer context

167:    Level: advanced

169:    Concepts: graphics^creating PetscViewer
170:    Concepts: file input/output^creating PetscViewer
171:    Concepts: sockets^creating PetscViewer

173: .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType

175: @*/
176: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
177: {
178:   PetscViewer    viewer;

182:   *inviewer = 0;
183:   PetscViewerInitializePackage();
184:   PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);
185:   *inviewer    = viewer;
186:   viewer->data = 0;
187:   return(0);
188: }

192: /*@C
193:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

195:    Collective on PetscViewer

197:    Input Parameter:
198: +  viewer      - the PetscViewer context
199: -  type        - for example, PETSCVIEWERASCII

201:    Options Database Command:
202: .  -draw_type  <type> - Sets the type; use -help for a list
203:     of available methods (for instance, ascii)

205:    Level: advanced

207:    Notes:
208:    See "include/petscviewer.h" for available methods (for instance,
209:    PETSCVIEWERSOCKET)

211: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat()
212: @*/
213: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
214: {
215:   PetscErrorCode ierr,(*r)(PetscViewer);
216:   PetscBool      match;

221:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
222:   if (match) return(0);

224:   /* cleanup any old type that may be there */
225:   if (viewer->data) {
226:     (*viewer->ops->destroy)(viewer);

228:     viewer->ops->destroy = NULL;
229:     viewer->data         = 0;
230:   }
231:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

233:    PetscFunctionListFind(PetscViewerList,type,&r);
234:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);

236:   PetscObjectChangeTypeName((PetscObject)viewer,type);
237:   (*r)(viewer);
238:   return(0);
239: }

243: /*@C
244:    PetscViewerRegister - Adds a viewer

246:    Not Collective

248:    Input Parameters:
249: +  name_solver - name of a new user-defined viewer
250: -  routine_create - routine to create method context

252:    Level: developer
253:    Notes:
254:    PetscViewerRegister() may be called multiple times to add several user-defined viewers.

256:    Sample usage:
257: .vb
258:    PetscViewerRegister("my_viewer_type",MyViewerCreate);
259: .ve

261:    Then, your solver can be chosen with the procedural interface via
262: $     PetscViewerSetType(viewer,"my_viewer_type")
263:    or at runtime via the option
264: $     -viewer_type my_viewer_type

266:   Concepts: registering^Viewers

268: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
269:  @*/
270: PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
271: {

275:   PetscFunctionListAdd(&PetscViewerList,sname,function);
276:   return(0);
277: }

281: /*@C
282:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
283:       Defaults to a PETSc X windows graphics.

285:    Collective on PetscViewer

287:    Input Parameter:
288: .     PetscViewer - the graphics context

290:    Level: intermediate

292:    Notes:
293:     Must be called after PetscViewerCreate() before the PetscViewer is used.

295:   Concepts: PetscViewer^setting options

297: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType

299: @*/
300: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
301: {
302:   PetscErrorCode    ierr;
303:   char              vtype[256];
304:   PetscBool         flg;


309:   if (!PetscViewerList) {
310:     PetscViewerRegisterAll();
311:   }
312:   PetscObjectOptionsBegin((PetscObject)viewer);
313:   PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
314:   if (flg) {
315:     PetscViewerSetType(viewer,vtype);
316:   }
317:   /* type has not been set? */
318:   if (!((PetscObject)viewer)->type_name) {
319:     PetscViewerSetType(viewer,PETSCVIEWERASCII);
320:   }
321:   if (viewer->ops->setfromoptions) {
322:     (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);
323:   }

325:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
326:   PetscObjectProcessOptionsHandlers((PetscObject)viewer);
327:   PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
328:   PetscOptionsEnd();
329:   return(0);
330: }