Actual source code: viewreg.c

      1: #define PETSC_DLL

      3: #include "src/sys/src/viewer/viewerimpl.h"  /*I "petsc.h" I*/

      5: PetscFList PetscViewerList              = 0;

      9: /*@C
     10:    PetscViewerCreate - Creates a viewing context

     12:    Collective on MPI_Comm

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

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

     20:    Level: advanced

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

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

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

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

     50:    Collective on PetscViewer

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

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

     60:    Level: advanced

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

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

     77:   PetscTypeCompare((PetscObject)viewer,type,&match);
     78:   if (match) return(0);

     80:   if (viewer->data) {
     81:     /* destroy the old private PetscViewer context */
     82:     (*viewer->ops->destroy)(viewer);
     83:     viewer->data      = 0;
     84:   }
     85:   /* Get the function pointers for the graphics method requested */
     86:   if (!PetscViewerList) {
     87:     PetscViewerRegisterAll(PETSC_NULL);
     88:   }

     90:    PetscFListFind(viewer->comm,PetscViewerList,type,(void (**)(void)) &r);
     91:   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
     92:   viewer->data        = 0;
     93:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));
     94:   (*r)(viewer);
     95:   PetscObjectChangeTypeName((PetscObject)viewer,type);
     96:   return(0);
     97: }

    101: /*@C
    102:    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
    103:    registered by PetscViewerRegisterDynamic().

    105:    Not Collective

    107:    Level: developer

    109: .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
    110: @*/
    111: PetscErrorCode PETSC_DLLEXPORT PetscViewerRegisterDestroy(void)
    112: {

    116:   if (PetscViewerList) {
    117:     PetscFListDestroy(&PetscViewerList);
    118:     PetscViewerList = 0;
    119:   }
    120:   return(0);
    121: }

    125: PetscErrorCode PETSC_DLLEXPORT PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
    126: {
    128:   char fullname[PETSC_MAX_PATH_LEN];

    131:   PetscFListConcat(path,name,fullname);
    132:   PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);
    133:   return(0);
    134: }

    138: /*@C
    139:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
    140:       Defaults to a PETSc X windows graphics.

    142:    Collective on PetscViewer

    144:    Input Parameter:
    145: .     PetscViewer - the graphics context

    147:    Level: intermediate

    149:    Notes:
    150:     Must be called after PetscViewerCreate() before the PetscViewer is used.

    152:   Concepts: PetscViewer^setting options

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

    156: @*/
    157: PetscErrorCode PETSC_DLLEXPORT PetscViewerSetFromOptions(PetscViewer viewer)
    158: {
    160:   char       vtype[256];
    161:   PetscTruth flg;

    166:   if (!PetscViewerList) {
    167:     PetscViewerRegisterAll(PETSC_NULL);
    168:   }
    169:   PetscOptionsBegin(viewer->comm,viewer->prefix,"PetscViewer options","PetscViewer");
    170:     PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(viewer->type_name?viewer->type_name:PETSC_VIEWER_ASCII),vtype,256,&flg);
    171:     if (flg) {
    172:       PetscViewerSetType(viewer,vtype);
    173:     }
    174:     /* type has not been set? */
    175:     if (!viewer->type_name) {
    176:       PetscViewerSetType(viewer,PETSC_VIEWER_ASCII);
    177:     }
    178:     if (viewer->ops->setfromoptions) {
    179:       (*viewer->ops->setfromoptions)(viewer);
    180:     }
    181:   PetscOptionsEnd();

    183:   return(0);
    184: }