Actual source code: view.c
petsc-3.10.5 2019-03-28
Report Typos and Errors

      2:  #include <petsc/private/viewerimpl.h>

      4: PetscClassId PETSC_VIEWER_CLASSID;

      6: static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
      7: /*@C
      8:   PetscViewerFinalizePackage - This function destroys any global objects created in the Petsc viewers. It is
      9:   called from PetscFinalize().

     11:   Level: developer

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

     21:   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
     22:     MPI_Comm_free_keyval(&Petsc_Viewer_keyval);
     23:   }
     24:   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
     25:     MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);
     26:   }
     27:   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
     28:     MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);
     29:   }
     30:   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
     31:     MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);
     32:   }
     33:   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
     34:     MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);
     35:   }
     36: #if defined(PETSC_HAVE_HDF5)
     37:   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
     38:     MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);
     39:   }
     40: #endif
     41: #if defined(PETSC_USE_SOCKETVIEWER)
     42:   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
     43:     MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);
     44:   }
     45: #endif
     46:   PetscFunctionListDestroy(&PetscViewerList);
     47:   PetscViewerPackageInitialized = PETSC_FALSE;
     48:   PetscViewerRegisterAllCalled  = PETSC_FALSE;
     49:   return(0);
     50: }

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

     55:   Level: developer

     57: .keywords: Petsc, initialize, package
     58: .seealso: PetscInitialize()
     59: @*/
     60: PetscErrorCode  PetscViewerInitializePackage(void)
     61: {
     62:   char           logList[256];
     63:   PetscBool      opt,pkg;

     67:   if (PetscViewerPackageInitialized) return(0);
     68:   PetscViewerPackageInitialized = PETSC_TRUE;
     69:   /* Register Classes */
     70:   PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);
     71:   /* Register Constructors */
     72:   PetscViewerRegisterAll();
     73:   /* Process info exclusions */
     74:   PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);
     75:   if (opt) {
     76:     PetscStrInList("viewer",logList,',',&pkg);
     77:     if (pkg) {PetscInfoDeactivateClass(PETSC_VIEWER_CLASSID);}
     78:   }
     79:   /* Process summary exclusions */
     80:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
     81:   if (opt) {
     82:     PetscStrInList("viewer",logList,',',&pkg);
     83:     if (pkg) {PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);}
     84:   }
     85: #if defined(PETSC_HAVE_MATHEMATICA)
     86:   PetscViewerMathematicaInitializePackage();
     87: #endif
     88:   /* Register package finalizer */
     89:   PetscRegisterFinalize(PetscViewerFinalizePackage);
     90:   return(0);
     91: }

     93: /*@
     94:    PetscViewerDestroy - Destroys a PetscViewer.

     96:    Collective on PetscViewer

     98:    Input Parameters:
     99: .  viewer - the PetscViewer to be destroyed.

    101:    Level: beginner

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

    105: @*/
    106: PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
    107: {

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

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

    117:   PetscObjectSAWsViewOff((PetscObject)*viewer);
    118:   if ((*viewer)->ops->destroy) {
    119:     (*(*viewer)->ops->destroy)(*viewer);
    120:   }
    121:   PetscHeaderDestroy(viewer);
    122:   return(0);
    123: }

    125: /*@C
    126:    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.

    128:    Collective on PetscViewer

    130:    Input Parameters:
    131: +  viewer - the viewer
    132: -  format - the format

    134:    Output Parameter:
    135: .   vf - viewer and format object

    137:    Notes:
    138:     This increases the reference count of the viewer so you can destroy the viewer object after this call
    139:    Level: developer

    141:    This is used as the context variable for many of the TS, SNES, and KSP monitor functions

    143: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()

    145: @*/
    146: PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
    147: {

    151:   PetscObjectReference((PetscObject)viewer);
    152:   PetscNew(vf);
    153:   (*vf)->viewer = viewer;
    154:   (*vf)->format = format;
    155:   return(0);
    156: }

    159: /*@C
    160:    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.

    162:    Collective on PetscViewer

    164:    Input Parameters:
    165: .  viewer - the PetscViewerAndFormat to be destroyed.

    167:    Level: developer

    169: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()

    171: @*/
    172: PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
    173: {

    177:   PetscViewerDestroy(&(*vf)->viewer);
    178:   PetscFree(*vf);
    179:   return(0);
    180: }

    182: /*@C
    183:    PetscViewerGetType - Returns the type of a PetscViewer.

    185:    Not Collective

    187:    Input Parameter:
    188: .   viewer - the PetscViewer

    190:    Output Parameter:
    191: .  type - PetscViewer type (see below)

    193:    Available Types Include:
    194: .  PETSCVIEWERSOCKET - Socket PetscViewer
    195: .  PETSCVIEWERASCII - ASCII PetscViewer
    196: .  PETSCVIEWERBINARY - binary file PetscViewer
    197: .  PETSCVIEWERSTRING - string PetscViewer
    198: .  PETSCVIEWERDRAW - drawing PetscViewer

    200:    Level: intermediate

    202:    Note:
    203:    See include/petscviewer.h for a complete list of PetscViewers.

    205:    PetscViewerType is actually a string

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

    209: @*/
    210: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
    211: {
    215:   *type = ((PetscObject)viewer)->type_name;
    216:   return(0);
    217: }

    219: /*@C
    220:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
    221:    PetscViewer options in the database.

    223:    Logically Collective on PetscViewer

    225:    Input Parameter:
    226: +  viewer - the PetscViewer context
    227: -  prefix - the prefix to prepend to all option names

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

    233:    Level: advanced

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

    237: .seealso: PetscViewerSetFromOptions()
    238: @*/
    239: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
    240: {

    245:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
    246:   return(0);
    247: }

    249: /*@C
    250:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
    251:    PetscViewer options in the database.

    253:    Logically Collective on PetscViewer

    255:    Input Parameters:
    256: +  viewer - the PetscViewer context
    257: -  prefix - the prefix to prepend to all option names

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

    263:    Level: advanced

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

    267: .seealso: PetscViewerGetOptionsPrefix()
    268: @*/
    269: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
    270: {

    275:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
    276:   return(0);
    277: }

    279: /*@C
    280:    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
    281:    PetscViewer options in the database.

    283:    Not Collective

    285:    Input Parameter:
    286: .  viewer - the PetscViewer context

    288:    Output Parameter:
    289: .  prefix - pointer to the prefix string used

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

    295:    Level: advanced

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

    299: .seealso: PetscViewerAppendOptionsPrefix()
    300: @*/
    301: PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
    302: {

    307:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
    308:   return(0);
    309: }

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

    314:    Collective on PetscViewer

    316:    Input Parameters:
    317: .  viewer - the PetscViewer context

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

    323:    Level: advanced

    325: .keywords: PetscViewer, setup

    327: .seealso: PetscViewerCreate(), PetscViewerDestroy()
    328: @*/
    329: PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
    330: {

    335:   if (viewer->setupcalled) return(0);
    336:   if (viewer->ops->setup) {
    337:     (*viewer->ops->setup)(viewer);
    338:   }
    339:   viewer->setupcalled = PETSC_TRUE;
    340:   return(0);
    341: }

    343: /*@C
    344:    PetscViewerView - Visualizes a viewer object.

    346:    Collective on PetscViewer

    348:    Input Parameters:
    349: +  v - the viewer
    350: -  viewer - visualization context

    352:   Notes:
    353:   The available visualization contexts include
    354: +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
    355: .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
    356:         output where only the first processor opens
    357:         the file.  All other processors send their
    358:         data to the first processor to print.
    359: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

    361:    Level: beginner

    363: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
    364:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
    365: @*/
    366: PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
    367: {
    368:   PetscErrorCode    ierr;
    369:   PetscBool         iascii;
    370:   PetscViewerFormat format;
    371: #if defined(PETSC_HAVE_SAWS)
    372:   PetscBool         issaws;
    373: #endif

    378:   if (!viewer) {
    379:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
    380:   }

    384:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    385: #if defined(PETSC_HAVE_SAWS)
    386:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
    387: #endif
    388:   if (iascii) {
    389:     PetscViewerGetFormat(viewer,&format);
    390:     PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
    391:     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
    392:       if (v->format) {
    393:         PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);
    394:       }
    395:       PetscViewerASCIIPushTab(viewer);
    396:       if (v->ops->view) {
    397:         (*v->ops->view)(v,viewer);
    398:       }
    399:       PetscViewerASCIIPopTab(viewer);
    400:     }
    401: #if defined(PETSC_HAVE_SAWS)
    402:   } else if (issaws) {
    403:     if (!((PetscObject)v)->amsmem) {
    404:       PetscObjectViewSAWs((PetscObject)v,viewer);
    405:       if (v->ops->view) {
    406:         (*v->ops->view)(v,viewer);
    407:       }
    408:     }
    409: #endif
    410:   }
    411:   return(0);
    412: }

    414: /*@C
    415:    PetscViewerRead - Reads data from a PetscViewer

    417:    Collective on MPI_Comm

    419:    Input Parameters:
    420: +  viewer   - The viewer
    421: .  data     - Location to write the data
    422: .  num      - Number of items of data to read
    423: -  datatype - Type of data to read

    425:    Output Parameters:
    426: .  count - number of items of data actually read, or NULL

    428:    Notes:
    429:    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
    430:    until a maximum of (-num - 1) chars.

    432:    Level: beginner

    434:    Concepts: binary files, ascii files

    436: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
    437:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
    438:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
    439: @*/
    440: PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
    441: {

    446:   if (dtype == PETSC_STRING) {
    447:     PetscInt c, i = 0, cnt;
    448:     char *s = (char *)data;
    449:     if (num >= 0) {
    450:       for (c = 0; c < num; c++) {
    451:         /* Skip leading whitespaces */
    452:         do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
    453:         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
    454:         i++;
    455:         /* Read strings one char at a time */
    456:         do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
    457:         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');
    458:         /* Terminate final string */
    459:         if (c == num-1) s[i-1] = '\0';
    460:       }
    461:     } else {
    462:       /* Read until a \n is encountered (-num is the max size allowed) */
    463:       do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (i == -num && !cnt) break;}
    464:       while (s[i-1]!='\n');
    465:       /* Terminate final string */
    466:       s[i-1] = '\0';
    467:       c      = i;
    468:     }
    469:     if (count) *count = c;
    470:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
    471:   } else {
    472:     (*viewer->ops->read)(viewer, data, num, count, dtype);
    473:   }
    474:   return(0);
    475: }