Date: 2015-10-02T21:58:34+00:00

Actual source code: view.c
petsc-3.6.2 2015-10-02
Report Typos and Errors

      2: #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/

      4: PetscClassId PETSC_VIEWER_CLASSID;

      6: static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
      9: /*@C
     10:   PetscViewerFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
     11:   called from PetscFinalize().

     13:   Level: developer

     15: .keywords: Petsc, destroy, package, mathematica
     16: .seealso: PetscFinalize()
     17: @*/
     18: PetscErrorCode  PetscViewerFinalizePackage(void)
     19: {

     23:   PetscFunctionListDestroy(&PetscViewerList);
     24:   PetscViewerPackageInitialized = PETSC_FALSE;
     25:   PetscViewerRegisterAllCalled  = PETSC_FALSE;
     26:   return(0);
     27: }

     31: /*@C
     32:   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.

     34:   Level: developer

     36: .keywords: Petsc, initialize, package
     37: .seealso: PetscInitialize()
     38: @*/
     39: PetscErrorCode  PetscViewerInitializePackage(void)
     40: {
     41:   char           logList[256];
     42:   char           *className;
     43:   PetscBool      opt;

     47:   if (PetscViewerPackageInitialized) return(0);
     48:   PetscViewerPackageInitialized = PETSC_TRUE;
     49:   /* Register Classes */
     50:   PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);

     52:   /* Register Constructors */
     53:   PetscViewerRegisterAll();

     55:   /* Process info exclusions */
     56:   PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);
     57:   if (opt) {
     58:     PetscStrstr(logList, "viewer", &className);
     59:     if (className) {
     60:       PetscInfoDeactivateClass(0);
     61:     }
     62:   }
     63:   /* Process summary exclusions */
     64:   PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);
     65:   if (opt) {
     66:     PetscStrstr(logList, "viewer", &className);
     67:     if (className) {
     68:       PetscLogEventDeactivateClass(0);
     69:     }
     70:   }
     71: #if defined(PETSC_HAVE_MATHEMATICA)
     72:   PetscViewerMathematicaInitializePackage();
     73: #endif
     74:   PetscRegisterFinalize(PetscViewerFinalizePackage);
     75:   return(0);
     76: }

     80: /*@
     81:    PetscViewerDestroy - Destroys a PetscViewer.

     83:    Collective on PetscViewer

     85:    Input Parameters:
     86: .  viewer - the PetscViewer to be destroyed.

     88:    Level: beginner

     90: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()

     92: @*/
     93: PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
     94: {

     98:   if (!*viewer) return(0);

    101:   PetscViewerFlush(*viewer);
    102:   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; return(0);}

    104:   PetscObjectSAWsViewOff((PetscObject)*viewer);
    105:   if ((*viewer)->ops->destroy) {
    106:     (*(*viewer)->ops->destroy)(*viewer);
    107:   }
    108:   PetscHeaderDestroy(viewer);
    109:   return(0);
    110: }

    114: /*@C
    115:    PetscViewerGetType - Returns the type of a PetscViewer.

    117:    Not Collective

    119:    Input Parameter:
    120: .   viewer - the PetscViewer

    122:    Output Parameter:
    123: .  type - PetscViewer type (see below)

    125:    Available Types Include:
    126: .  PETSCVIEWERSOCKET - Socket PetscViewer
    127: .  PETSCVIEWERASCII - ASCII PetscViewer
    128: .  PETSCVIEWERBINARY - binary file PetscViewer
    129: .  PETSCVIEWERSTRING - string PetscViewer
    130: .  PETSCVIEWERDRAW - drawing PetscViewer

    132:    Level: intermediate

    134:    Note:
    135:    See include/petscviewer.h for a complete list of PetscViewers.

    137:    PetscViewerType is actually a string

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

    141: @*/
    142: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
    143: {
    147:   *type = ((PetscObject)viewer)->type_name;
    148:   return(0);
    149: }

    153: /*@C
    154:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
    155:    PetscViewer options in the database.

    157:    Logically Collective on PetscViewer

    159:    Input Parameter:
    160: +  viewer - the PetscViewer context
    161: -  prefix - the prefix to prepend to all option names

    163:    Notes:
    164:    A hyphen (-) must NOT be given at the beginning of the prefix name.
    165:    The first character of all runtime options is AUTOMATICALLY the hyphen.

    167:    Level: advanced

    169: .keywords: PetscViewer, set, options, prefix, database

    171: .seealso: PetscViewerSetFromOptions()
    172: @*/
    173: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
    174: {

    179:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
    180:   return(0);
    181: }

    185: /*@C
    186:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
    187:    PetscViewer options in the database.

    189:    Logically Collective on PetscViewer

    191:    Input Parameters:
    192: +  viewer - the PetscViewer context
    193: -  prefix - the prefix to prepend to all option names

    195:    Notes:
    196:    A hyphen (-) must NOT be given at the beginning of the prefix name.
    197:    The first character of all runtime options is AUTOMATICALLY the hyphen.

    199:    Level: advanced

    201: .keywords: PetscViewer, append, options, prefix, database

    203: .seealso: PetscViewerGetOptionsPrefix()
    204: @*/
    205: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
    206: {

    211:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
    212:   return(0);
    213: }

    217: /*@C
    218:    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
    219:    PetscViewer options in the database.

    221:    Not Collective

    223:    Input Parameter:
    224: .  viewer - the PetscViewer context

    226:    Output Parameter:
    227: .  prefix - pointer to the prefix string used

    229:    Notes: On the fortran side, the user should pass in a string 'prefix' of
    230:    sufficient length to hold the prefix.

    232:    Level: advanced

    234: .keywords: PetscViewer, get, options, prefix, database

    236: .seealso: PetscViewerAppendOptionsPrefix()
    237: @*/
    238: PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
    239: {

    244:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
    245:   return(0);
    246: }

    250: /*@
    251:    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.

    253:    Collective on PetscViewer

    255:    Input Parameters:
    256: .  viewer - the PetscViewer context

    258:    Notes:
    259:    For basic use of the PetscViewer classes the user need not explicitly call
    260:    PetscViewerSetUp(), since these actions will happen automatically.

    262:    Level: advanced

    264: .keywords: PetscViewer, setup

    266: .seealso: PetscViewerCreate(), PetscViewerDestroy()
    267: @*/
    268: PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
    269: {

    274:   if (viewer->setupcalled) return(0);
    275:   if (viewer->ops->setup) {
    276:     (*viewer->ops->setup)(viewer);
    277:   }
    278:   viewer->setupcalled = PETSC_TRUE;
    279:   return(0);
    280: }

    284: /*@C
    285:    PetscViewerView - Visualizes a viewer object.

    287:    Collective on PetscViewer

    289:    Input Parameters:
    290: +  v - the viewer
    291: -  viewer - visualization context

    293:   Notes:
    294:   The available visualization contexts include
    295: +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
    296: .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
    297:         output where only the first processor opens
    298:         the file.  All other processors send their
    299:         data to the first processor to print.
    300: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

    302:    Level: beginner

    304: .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
    305:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
    306: @*/
    307: PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
    308: {
    309:   PetscErrorCode    ierr;
    310:   PetscBool         iascii;
    311:   PetscViewerFormat format;
    312: #if defined(PETSC_HAVE_SAWS)
    313:   PetscBool         issaws;
    314: #endif

    319:   if (!viewer) {
    320:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
    321:   }

    325:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    326: #if defined(PETSC_HAVE_SAWS)
    327:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
    328: #endif
    329:   if (iascii) {
    330:     PetscViewerGetFormat(viewer,&format);
    331:     PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
    332:     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
    333:       if (v->format) {
    334:         PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);
    335:       }
    336:       PetscViewerASCIIPushTab(viewer);
    337:       if (v->ops->view) {
    338:         (*v->ops->view)(v,viewer);
    339:       }
    340:       PetscViewerASCIIPopTab(viewer);
    341:     }
    342: #if defined(PETSC_HAVE_SAWS)
    343:   } else if (issaws) {
    344:     if (!((PetscObject)v)->amsmem) {
    345:       PetscObjectViewSAWs((PetscObject)v,viewer);
    346:       if (v->ops->view) {
    347:         (*v->ops->view)(v,viewer);
    348:       }
    349:     }
    350: #endif
    351:   }
    352:   return(0);
    353: }

    357: /*@C
    358:    PetscViewerRead - Reads data from a PetscViewer

    360:    Collective on MPI_Comm

    362:    Input Parameters:
    363: +  viewer   - The viewer
    364: .  data     - Location to write the data
    365: .  num      - Number of items of data to read
    366: -  datatype - Type of data to read

    368:    Output Parameters:
    369: .  count - number of items of data actually read, or NULL

    371:    Level: beginner

    373:    Concepts: binary files, ascii files

    375: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
    376:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
    377:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
    378: @*/
    379: PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
    380: {

    385:   if (dtype == PETSC_STRING) {
    386:     PetscInt c, i = 0, cnt;
    387:     char *s = (char *)data;
    388:     for (c = 0; c < num; c++) {
    389:       /* Skip leading whitespaces */
    390:       do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
    391:       while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
    392:       i++;
    393:       /* Read strings one char at a time */
    394:       do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
    395:       while (s[i-1]!='\n' && s[i-1]!='\t' && s[i-1]!=' ' && s[i-1]!='\0' && s[i-1]!='\v' && s[i-1]!='\f' && s[i-1]!='\r');
    396:       /* Terminate final string */
    397:       if (c == num-1) s[i-1] = '\0';
    398:     }
    399:     if (count) *count = c;
    400:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
    401:   } else {
    402:     (*viewer->ops->read)(viewer, data, num, count, dtype);
    403:   }
    404:   return(0);
    405: }