Actual source code: view.c

petsc-3.8.4 2018-03-24
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_Keyval_free(&Petsc_Viewer_keyval);
 23:   }
 24:   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
 25:     MPI_Keyval_free(&Petsc_Viewer_Stdout_keyval);
 26:   }
 27:   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
 28:     MPI_Keyval_free(&Petsc_Viewer_Stderr_keyval);
 29:   }
 30:   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
 31:     MPI_Keyval_free(&Petsc_Viewer_Binary_keyval);
 32:   }
 33:   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
 34:     MPI_Keyval_free(&Petsc_Viewer_Draw_keyval);
 35:   }
 36: #if defined(PETSC_HAVE_HDF5)
 37:   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
 38:     MPI_Keyval_free(&Petsc_Viewer_HDF5_keyval);
 39:   }
 40: #endif
 41: #if defined(PETSC_USE_SOCKETVIEWER)
 42:   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
 43:     MPI_Keyval_free(&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:   char           *className;
 64:   PetscBool      opt;

 68:   if (PetscViewerPackageInitialized) return(0);
 69:   PetscViewerPackageInitialized = PETSC_TRUE;
 70:   /* Register Classes */
 71:   PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);

 73:   /* Register Constructors */
 74:   PetscViewerRegisterAll();

 76:   /* Process info exclusions */
 77:   PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);
 78:   if (opt) {
 79:     PetscStrstr(logList, "viewer", &className);
 80:     if (className) {
 81:       PetscInfoDeactivateClass(0);
 82:     }
 83:   }
 84:   /* Process summary exclusions */
 85:   PetscOptionsGetString(NULL,NULL, "-log_exclude", logList, 256, &opt);
 86:   if (opt) {
 87:     PetscStrstr(logList, "viewer", &className);
 88:     if (className) {
 89:       PetscLogEventDeactivateClass(0);
 90:     }
 91:   }
 92: #if defined(PETSC_HAVE_MATHEMATICA)
 93:   PetscViewerMathematicaInitializePackage();
 94: #endif
 95:   PetscRegisterFinalize(PetscViewerFinalizePackage);
 96:   return(0);
 97: }

 99: /*@
100:    PetscViewerDestroy - Destroys a PetscViewer.

102:    Collective on PetscViewer

104:    Input Parameters:
105: .  viewer - the PetscViewer to be destroyed.

107:    Level: beginner

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

111: @*/
112: PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
113: {

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

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

123:   PetscObjectSAWsViewOff((PetscObject)*viewer);
124:   if ((*viewer)->ops->destroy) {
125:     (*(*viewer)->ops->destroy)(*viewer);
126:   }
127:   PetscHeaderDestroy(viewer);
128:   return(0);
129: }

131: /*@C
132:    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.

134:    Collective on PetscViewer

136:    Input Parameters:
137: +  viewer - the viewer
138: -  format - the format 

140:    Output Parameter:
141: .   vf - viewer and format object

143:    Notes: This increases the reference count of the viewer so you can destroy the viewer object after this call
144:    Level: developer

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

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

150: @*/
151: PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
152: {

156:   PetscObjectReference((PetscObject)viewer);
157:   PetscNew(vf);
158:   (*vf)->viewer = viewer;
159:   (*vf)->format = format;
160:   return(0);
161: }


164: /*@C
165:    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.

167:    Collective on PetscViewer

169:    Input Parameters:
170: .  viewer - the PetscViewerAndFormat to be destroyed.

172:    Level: developer

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

176: @*/
177: PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
178: {

182:   PetscViewerDestroy(&(*vf)->viewer);
183:   PetscFree(*vf);
184:   return(0);
185: }

187: /*@C
188:    PetscViewerGetType - Returns the type of a PetscViewer.

190:    Not Collective

192:    Input Parameter:
193: .   viewer - the PetscViewer

195:    Output Parameter:
196: .  type - PetscViewer type (see below)

198:    Available Types Include:
199: .  PETSCVIEWERSOCKET - Socket PetscViewer
200: .  PETSCVIEWERASCII - ASCII PetscViewer
201: .  PETSCVIEWERBINARY - binary file PetscViewer
202: .  PETSCVIEWERSTRING - string PetscViewer
203: .  PETSCVIEWERDRAW - drawing PetscViewer

205:    Level: intermediate

207:    Note:
208:    See include/petscviewer.h for a complete list of PetscViewers.

210:    PetscViewerType is actually a string

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

214: @*/
215: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
216: {
220:   *type = ((PetscObject)viewer)->type_name;
221:   return(0);
222: }

224: /*@C
225:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
226:    PetscViewer options in the database.

228:    Logically Collective on PetscViewer

230:    Input Parameter:
231: +  viewer - the PetscViewer context
232: -  prefix - the prefix to prepend to all option names

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

238:    Level: advanced

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

242: .seealso: PetscViewerSetFromOptions()
243: @*/
244: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
245: {

250:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
251:   return(0);
252: }

254: /*@C
255:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
256:    PetscViewer options in the database.

258:    Logically Collective on PetscViewer

260:    Input Parameters:
261: +  viewer - the PetscViewer context
262: -  prefix - the prefix to prepend to all option names

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

268:    Level: advanced

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

272: .seealso: PetscViewerGetOptionsPrefix()
273: @*/
274: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
275: {

280:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
281:   return(0);
282: }

284: /*@C
285:    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
286:    PetscViewer options in the database.

288:    Not Collective

290:    Input Parameter:
291: .  viewer - the PetscViewer context

293:    Output Parameter:
294: .  prefix - pointer to the prefix string used

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

299:    Level: advanced

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

303: .seealso: PetscViewerAppendOptionsPrefix()
304: @*/
305: PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
306: {

311:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
312:   return(0);
313: }

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

318:    Collective on PetscViewer

320:    Input Parameters:
321: .  viewer - the PetscViewer context

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

327:    Level: advanced

329: .keywords: PetscViewer, setup

331: .seealso: PetscViewerCreate(), PetscViewerDestroy()
332: @*/
333: PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
334: {

339:   if (viewer->setupcalled) return(0);
340:   if (viewer->ops->setup) {
341:     (*viewer->ops->setup)(viewer);
342:   }
343:   viewer->setupcalled = PETSC_TRUE;
344:   return(0);
345: }

347: /*@C
348:    PetscViewerView - Visualizes a viewer object.

350:    Collective on PetscViewer

352:    Input Parameters:
353: +  v - the viewer
354: -  viewer - visualization context

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

365:    Level: beginner

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

382:   if (!viewer) {
383:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
384:   }

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

418: /*@C
419:    PetscViewerRead - Reads data from a PetscViewer

421:    Collective on MPI_Comm

423:    Input Parameters:
424: +  viewer   - The viewer
425: .  data     - Location to write the data
426: .  num      - Number of items of data to read
427: -  datatype - Type of data to read

429:    Output Parameters:
430: .  count - number of items of data actually read, or NULL

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

436:    Level: beginner

438:    Concepts: binary files, ascii files

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

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