Actual source code: viewreg.c

petsc-3.5.4 2015-05-23
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: $       ams[: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;


 56:   if (format) *format = PETSC_VIEWER_DEFAULT;
 57:   if (set) *set = PETSC_FALSE;
 58:   PetscOptionsFindPair_Private(pre,name,&value,&flag);
 59:   if (flag) {
 60:     if (set) *set = PETSC_TRUE;
 61:     if (!value) {
 62:       PetscViewerASCIIGetStdout(comm,viewer);
 63:       PetscObjectReference((PetscObject)*viewer);
 64:     } else {
 65:       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
 66:       PetscInt   cnt;
 67:       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};

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

147: /*@
148:    PetscViewerCreate - Creates a viewing context

150:    Collective on MPI_Comm

152:    Input Parameter:
153: .  comm - MPI communicator

155:    Output Parameter:
156: .  inviewer - location to put the PetscViewer context

158:    Level: advanced

160:    Concepts: graphics^creating PetscViewer
161:    Concepts: file input/output^creating PetscViewer
162:    Concepts: sockets^creating PetscViewer

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

166: @*/
167: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
168: {
169:   PetscViewer    viewer;

173:   *inviewer = 0;
174:   PetscViewerInitializePackage();
175:   PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);
176:   *inviewer    = viewer;
177:   viewer->data = 0;
178:   return(0);
179: }

183: /*@C
184:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

186:    Collective on PetscViewer

188:    Input Parameter:
189: +  viewer      - the PetscViewer context
190: -  type        - for example, PETSCVIEWERASCII

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

196:    Level: advanced

198:    Notes:
199:    See "include/petscviewer.h" for available methods (for instance,
200:    PETSCVIEWERSOCKET)

202: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat()
203: @*/
204: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
205: {
206:   PetscErrorCode ierr,(*r)(PetscViewer);
207:   PetscBool      match;

212:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
213:   if (match) return(0);

215:   /* cleanup any old type that may be there */
216:   if (viewer->data) {
217:     (*viewer->ops->destroy)(viewer);

219:     viewer->ops->destroy = NULL;
220:     viewer->data         = 0;
221:   }
222:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

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

227:   PetscObjectChangeTypeName((PetscObject)viewer,type);
228:   (*r)(viewer);
229:   return(0);
230: }

234: /*@C
235:    PetscViewerRegister - Adds a viewer

237:    Not Collective

239:    Input Parameters:
240: +  name_solver - name of a new user-defined viewer
241: -  routine_create - routine to create method context

243:    Level: developer
244:    Notes:
245:    PetscViewerRegister() may be called multiple times to add several user-defined viewers.

247:    Sample usage:
248: .vb
249:    PetscViewerRegister("my_viewer_type",MyViewerCreate);
250: .ve

252:    Then, your solver can be chosen with the procedural interface via
253: $     PetscViewerSetType(viewer,"my_viewer_type")
254:    or at runtime via the option
255: $     -viewer_type my_viewer_type

257:   Concepts: registering^Viewers

259: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
260:  @*/
261: PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
262: {

266:   PetscFunctionListAdd(&PetscViewerList,sname,function);
267:   return(0);
268: }

272: /*@C
273:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
274:       Defaults to a PETSc X windows graphics.

276:    Collective on PetscViewer

278:    Input Parameter:
279: .     PetscViewer - the graphics context

281:    Level: intermediate

283:    Notes:
284:     Must be called after PetscViewerCreate() before the PetscViewer is used.

286:   Concepts: PetscViewer^setting options

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

290: @*/
291: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
292: {
293:   PetscErrorCode    ierr;
294:   char              vtype[256];
295:   PetscBool         flg;


300:   if (!PetscViewerList) {
301:     PetscViewerRegisterAll();
302:   }
303:   PetscObjectOptionsBegin((PetscObject)viewer);
304:   PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
305:   if (flg) {
306:     PetscViewerSetType(viewer,vtype);
307:   }
308:   /* type has not been set? */
309:   if (!((PetscObject)viewer)->type_name) {
310:     PetscViewerSetType(viewer,PETSCVIEWERASCII);
311:   }
312:   if (viewer->ops->setfromoptions) {
313:     (*viewer->ops->setfromoptions)(viewer);
314:   }

316:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
317:   PetscObjectProcessOptionsHandlers((PetscObject)viewer);
318:   PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
319:   PetscOptionsEnd();
320:   return(0);
321: }