Actual source code: viewreg.c

petsc-dev 2014-02-02
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,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:         default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
109:         }
110:         PetscObjectReference((PetscObject)*viewer);
111:       } else {
112:         if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
113:           PetscViewerASCIIGetStdout(comm,viewer);
114:           PetscObjectReference((PetscObject)*viewer);
115:         } else {
116:           PetscFileMode fmode;
117:           PetscViewerCreate(comm,viewer);
118:           PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");
119:           fmode = FILE_MODE_WRITE;
120:           if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
121:             PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);
122:             if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
123:           }
124:           PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
125:           PetscViewerFileSetName(*viewer,loc1_fname);
126:           PetscViewerDrawSetDrawType(*viewer,loc1_fname);
127:         }
128:       }
129:       if (loc2_fmt && *loc2_fmt) {
130:         PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
131:         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
132:       }
133:       PetscViewerSetUp(*viewer);
134:       PetscFree(loc0_vtype);
135:     }
136:   }
137:   return(0);
138: }

142: /*@
143:    PetscViewerCreate - Creates a viewing context

145:    Collective on MPI_Comm

147:    Input Parameter:
148: .  comm - MPI communicator

150:    Output Parameter:
151: .  inviewer - location to put the PetscViewer context

153:    Level: advanced

155:    Concepts: graphics^creating PetscViewer
156:    Concepts: file input/output^creating PetscViewer
157:    Concepts: sockets^creating PetscViewer

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

161: @*/
162: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
163: {
164:   PetscViewer    viewer;

168:   *inviewer = 0;
169:   PetscViewerInitializePackage();
170:   PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);
171:   *inviewer    = viewer;
172:   viewer->data = 0;
173:   return(0);
174: }

178: /*@C
179:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

181:    Collective on PetscViewer

183:    Input Parameter:
184: +  viewer      - the PetscViewer context
185: -  type        - for example, PETSCVIEWERASCII

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

191:    Level: advanced

193:    Notes:
194:    See "include/petscviewer.h" for available methods (for instance,
195:    PETSCVIEWERSOCKET)

197: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat()
198: @*/
199: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
200: {
201:   PetscErrorCode ierr,(*r)(PetscViewer);
202:   PetscBool      match;

207:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
208:   if (match) return(0);

210:   /* cleanup any old type that may be there */
211:   if (viewer->data) {
212:     (*viewer->ops->destroy)(viewer);

214:     viewer->ops->destroy = NULL;
215:     viewer->data         = 0;
216:   }
217:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

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

222:   PetscObjectChangeTypeName((PetscObject)viewer,type);
223:   (*r)(viewer);
224:   return(0);
225: }

229: /*@C
230:    PetscViewerRegister - Adds a viewer

232:    Not Collective

234:    Input Parameters:
235: +  name_solver - name of a new user-defined viewer
236: -  routine_create - routine to create method context

238:    Level: developer
239:    Notes:
240:    PetscViewerRegister() may be called multiple times to add several user-defined viewers.

242:    Sample usage:
243: .vb
244:    PetscViewerRegister("my_viewer_type",MyViewerCreate);
245: .ve

247:    Then, your solver can be chosen with the procedural interface via
248: $     PetscViewerSetType(viewer,"my_viewer_type")
249:    or at runtime via the option
250: $     -viewer_type my_viewer_type

252:   Concepts: registering^Viewers

254: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
255:  @*/
256: PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
257: {

261:   PetscFunctionListAdd(&PetscViewerList,sname,function);
262:   return(0);
263: }

267: /*@C
268:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
269:       Defaults to a PETSc X windows graphics.

271:    Collective on PetscViewer

273:    Input Parameter:
274: .     PetscViewer - the graphics context

276:    Level: intermediate

278:    Notes:
279:     Must be called after PetscViewerCreate() before the PetscViewer is used.

281:   Concepts: PetscViewer^setting options

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

285: @*/
286: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
287: {
288:   PetscErrorCode    ierr;
289:   char              vtype[256];
290:   PetscBool         flg;


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

311:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
312:   PetscObjectProcessOptionsHandlers((PetscObject)viewer);
313:   PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
314:   PetscOptionsEnd();
315:   return(0);
316: }