Actual source code: view.c

petsc-3.9.2 2018-05-20
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) {PetscLogEventDeactivateClass(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: This increases the reference count of the viewer so you can destroy the viewer object after this call
138:    Level: developer

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

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

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

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


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

161:    Collective on PetscViewer

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

166:    Level: developer

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

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

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

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

184:    Not Collective

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

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

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

199:    Level: intermediate

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

204:    PetscViewerType is actually a string

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

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

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

222:    Logically Collective on PetscViewer

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

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

232:    Level: advanced

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

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

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

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

252:    Logically Collective on PetscViewer

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

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

262:    Level: advanced

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

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

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

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

282:    Not Collective

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

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

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

293:    Level: advanced

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

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

305:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
306:   return(0);
307: }

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

312:    Collective on PetscViewer

314:    Input Parameters:
315: .  viewer - the PetscViewer context

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

321:    Level: advanced

323: .keywords: PetscViewer, setup

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

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

341: /*@C
342:    PetscViewerView - Visualizes a viewer object.

344:    Collective on PetscViewer

346:    Input Parameters:
347: +  v - the viewer
348: -  viewer - visualization context

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

359:    Level: beginner

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

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

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

412: /*@C
413:    PetscViewerRead - Reads data from a PetscViewer

415:    Collective on MPI_Comm

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

423:    Output Parameters:
424: .  count - number of items of data actually read, or NULL

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

430:    Level: beginner

432:    Concepts: binary files, ascii files

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

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