Actual source code: viewreg.c

petsc-3.3-p7 2013-05-11
  2: #include <petsc-private/viewerimpl.h>  /*I "petscsys.h" I*/  

  4: PetscFList PetscViewerList              = 0;

  8: /*@
  9:    PetscViewerCreate - Creates a viewing context

 11:    Collective on MPI_Comm

 13:    Input Parameter:
 14: .  comm - MPI communicator

 16:    Output Parameter:
 17: .  inviewer - location to put the PetscViewer context

 19:    Level: advanced

 21:    Concepts: graphics^creating PetscViewer
 22:    Concepts: file input/output^creating PetscViewer
 23:    Concepts: sockets^creating PetscViewer

 25: .seealso: PetscViewerDestroy(), PetscViewerSetType()

 27: @*/
 28: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
 29: {
 30:   PetscViewer    viewer;

 34:   *inviewer = 0;
 35: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 36:   PetscViewerInitializePackage(PETSC_NULL);
 37: #endif
 38:   PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);
 39:   *inviewer           = viewer;
 40:   viewer->data        = 0;
 41:   return(0);
 42: }
 43: 
 46: /*@C
 47:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

 49:    Collective on PetscViewer

 51:    Input Parameter:
 52: +  viewer      - the PetscViewer context
 53: -  type        - for example, "ASCII"

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

 59:    Level: advanced

 61:    Notes:  
 62:    See "include/petscviewer.h" for available methods (for instance,
 63:    PETSC_VIEWER_SOCKET)

 65: .seealso: PetscViewerCreate(), PetscViewerGetType()
 66: @*/
 67: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,const PetscViewerType type)
 68: {
 69:   PetscErrorCode ierr,(*r)(PetscViewer);
 70:   PetscBool      match;

 75:   CHKMEMQ;
 76:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
 77:   if (match) return(0);

 79:   /* cleanup any old type that may be there */
 80:   if (viewer->data) {
 81:     (*viewer->ops->destroy)(viewer);
 82:     viewer->ops->destroy = PETSC_NULL;
 83:     viewer->data = 0;
 84:   }
 85:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

 87:    PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);
 88:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);

 90:   PetscObjectChangeTypeName((PetscObject)viewer,type);
 91:   (*r)(viewer);
 92:   return(0);
 93: }

 97: /*@C
 98:    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
 99:    registered by PetscViewerRegisterDynamic().

101:    Not Collective

103:    Level: developer

105: .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
106: @*/
107: PetscErrorCode  PetscViewerRegisterDestroy(void)
108: {

112:   PetscFListDestroy(&PetscViewerList);
113:   return(0);
114: }

118: PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
119: {
121:   char fullname[PETSC_MAX_PATH_LEN];

124:   PetscFListConcat(path,name,fullname);
125:   PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);
126:   return(0);
127: }

131: /*@C
132:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
133:       Defaults to a PETSc X windows graphics.

135:    Collective on PetscViewer

137:    Input Parameter:
138: .     PetscViewer - the graphics context

140:    Level: intermediate

142:    Notes: 
143:     Must be called after PetscViewerCreate() before the PetscViewer is used.

145:   Concepts: PetscViewer^setting options

147: .seealso: PetscViewerCreate(), PetscViewerSetType()

149: @*/
150: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
151: {
153:   char       vtype[256];
154:   PetscBool  flg;


159:   if (!PetscViewerList) {
160:     PetscViewerRegisterAll(PETSC_NULL);
161:   }
162:   PetscObjectOptionsBegin((PetscObject)viewer);
163:     PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(((PetscObject)viewer)->type_name?((PetscObject)viewer)->type_name:PETSCVIEWERASCII),vtype,256,&flg);
164:     if (flg) {
165:       PetscViewerSetType(viewer,vtype);
166:     }
167:     /* type has not been set? */
168:     if (!((PetscObject)viewer)->type_name) {
169:       PetscViewerSetType(viewer,PETSCVIEWERASCII);
170:     }
171:     if (viewer->ops->setfromoptions) {
172:       (*viewer->ops->setfromoptions)(viewer);
173:     }

175:     /* process any options handlers added with PetscObjectAddOptionsHandler() */
176:     PetscObjectProcessOptionsHandlers((PetscObject)viewer);
177:   PetscOptionsEnd();

179:   return(0);
180: }