Actual source code: view.c

petsc-master 2017-03-25
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 everything in the Petsc interface to Mathematica. 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:   PetscFunctionListDestroy(&PetscViewerList);
 22:   PetscViewerPackageInitialized = PETSC_FALSE;
 23:   PetscViewerRegisterAllCalled  = PETSC_FALSE;
 24:   return(0);
 25: }

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

 30:   Level: developer

 32: .keywords: Petsc, initialize, package
 33: .seealso: PetscInitialize()
 34: @*/
 35: PetscErrorCode  PetscViewerInitializePackage(void)
 36: {
 37:   char           logList[256];
 38:   char           *className;
 39:   PetscBool      opt;

 43:   if (PetscViewerPackageInitialized) return(0);
 44:   PetscViewerPackageInitialized = PETSC_TRUE;
 45:   /* Register Classes */
 46:   PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);

 48:   /* Register Constructors */
 49:   PetscViewerRegisterAll();

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

 74: /*@
 75:    PetscViewerDestroy - Destroys a PetscViewer.

 77:    Collective on PetscViewer

 79:    Input Parameters:
 80: .  viewer - the PetscViewer to be destroyed.

 82:    Level: beginner

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

 86: @*/
 87: PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
 88: {

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

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

 98:   PetscObjectSAWsViewOff((PetscObject)*viewer);
 99:   if ((*viewer)->ops->destroy) {
100:     (*(*viewer)->ops->destroy)(*viewer);
101:   }
102:   PetscHeaderDestroy(viewer);
103:   return(0);
104: }

106: /*@C
107:    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.

109:    Collective on PetscViewer

111:    Input Parameters:
112: +  viewer - the viewer
113: -  format - the format 

115:    Output Parameter:
116: .   vf - viewer and format object

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

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

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

125: @*/
126: PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
127: {

131:   PetscObjectReference((PetscObject)viewer);
132:   PetscNew(vf);
133:   (*vf)->viewer = viewer;
134:   (*vf)->format = format;
135:   return(0);
136: }


139: /*@C
140:    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.

142:    Collective on PetscViewer

144:    Input Parameters:
145: .  viewer - the PetscViewerAndFormat to be destroyed.

147:    Level: developer

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

151: @*/
152: PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
153: {

157:   PetscViewerDestroy(&(*vf)->viewer);
158:   PetscFree(*vf);
159:   return(0);
160: }

162: /*@C
163:    PetscViewerGetType - Returns the type of a PetscViewer.

165:    Not Collective

167:    Input Parameter:
168: .   viewer - the PetscViewer

170:    Output Parameter:
171: .  type - PetscViewer type (see below)

173:    Available Types Include:
174: .  PETSCVIEWERSOCKET - Socket PetscViewer
175: .  PETSCVIEWERASCII - ASCII PetscViewer
176: .  PETSCVIEWERBINARY - binary file PetscViewer
177: .  PETSCVIEWERSTRING - string PetscViewer
178: .  PETSCVIEWERDRAW - drawing PetscViewer

180:    Level: intermediate

182:    Note:
183:    See include/petscviewer.h for a complete list of PetscViewers.

185:    PetscViewerType is actually a string

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

189: @*/
190: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
191: {
195:   *type = ((PetscObject)viewer)->type_name;
196:   return(0);
197: }

199: /*@C
200:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
201:    PetscViewer options in the database.

203:    Logically Collective on PetscViewer

205:    Input Parameter:
206: +  viewer - the PetscViewer context
207: -  prefix - the prefix to prepend to all option names

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

213:    Level: advanced

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

217: .seealso: PetscViewerSetFromOptions()
218: @*/
219: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
220: {

225:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
226:   return(0);
227: }

229: /*@C
230:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
231:    PetscViewer options in the database.

233:    Logically Collective on PetscViewer

235:    Input Parameters:
236: +  viewer - the PetscViewer context
237: -  prefix - the prefix to prepend to all option names

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

243:    Level: advanced

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

247: .seealso: PetscViewerGetOptionsPrefix()
248: @*/
249: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
250: {

255:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
256:   return(0);
257: }

259: /*@C
260:    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
261:    PetscViewer options in the database.

263:    Not Collective

265:    Input Parameter:
266: .  viewer - the PetscViewer context

268:    Output Parameter:
269: .  prefix - pointer to the prefix string used

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

274:    Level: advanced

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

278: .seealso: PetscViewerAppendOptionsPrefix()
279: @*/
280: PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
281: {

286:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
287:   return(0);
288: }

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

293:    Collective on PetscViewer

295:    Input Parameters:
296: .  viewer - the PetscViewer context

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

302:    Level: advanced

304: .keywords: PetscViewer, setup

306: .seealso: PetscViewerCreate(), PetscViewerDestroy()
307: @*/
308: PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
309: {

314:   if (viewer->setupcalled) return(0);
315:   if (viewer->ops->setup) {
316:     (*viewer->ops->setup)(viewer);
317:   }
318:   viewer->setupcalled = PETSC_TRUE;
319:   return(0);
320: }

322: /*@C
323:    PetscViewerView - Visualizes a viewer object.

325:    Collective on PetscViewer

327:    Input Parameters:
328: +  v - the viewer
329: -  viewer - visualization context

331:   Notes:
332:   The available visualization contexts include
333: +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
334: .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
335:         output where only the first processor opens
336:         the file.  All other processors send their
337:         data to the first processor to print.
338: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

340:    Level: beginner

342: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
343:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
344: @*/
345: PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
346: {
347:   PetscErrorCode    ierr;
348:   PetscBool         iascii;
349:   PetscViewerFormat format;
350: #if defined(PETSC_HAVE_SAWS)
351:   PetscBool         issaws;
352: #endif

357:   if (!viewer) {
358:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
359:   }

363:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
364: #if defined(PETSC_HAVE_SAWS)
365:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
366: #endif
367:   if (iascii) {
368:     PetscViewerGetFormat(viewer,&format);
369:     PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
370:     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
371:       if (v->format) {
372:         PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);
373:       }
374:       PetscViewerASCIIPushTab(viewer);
375:       if (v->ops->view) {
376:         (*v->ops->view)(v,viewer);
377:       }
378:       PetscViewerASCIIPopTab(viewer);
379:     }
380: #if defined(PETSC_HAVE_SAWS)
381:   } else if (issaws) {
382:     if (!((PetscObject)v)->amsmem) {
383:       PetscObjectViewSAWs((PetscObject)v,viewer);
384:       if (v->ops->view) {
385:         (*v->ops->view)(v,viewer);
386:       }
387:     }
388: #endif
389:   }
390:   return(0);
391: }

393: /*@C
394:    PetscViewerRead - Reads data from a PetscViewer

396:    Collective on MPI_Comm

398:    Input Parameters:
399: +  viewer   - The viewer
400: .  data     - Location to write the data
401: .  num      - Number of items of data to read
402: -  datatype - Type of data to read

404:    Output Parameters:
405: .  count - number of items of data actually read, or NULL

407:    Level: beginner

409:    Concepts: binary files, ascii files

411: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
412:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
413:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
414: @*/
415: PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
416: {

421:   if (dtype == PETSC_STRING) {
422:     PetscInt c, i = 0, cnt;
423:     char *s = (char *)data;
424:     for (c = 0; c < num; c++) {
425:       /* Skip leading whitespaces */
426:       do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
427:       while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
428:       i++;
429:       /* Read strings one char at a time */
430:       do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (count && !cnt) break;}
431:       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');
432:       /* Terminate final string */
433:       if (c == num-1) s[i-1] = '\0';
434:     }
435:     if (count) *count = c;
436:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
437:   } else {
438:     (*viewer->ops->read)(viewer, data, num, count, dtype);
439:   }
440:   return(0);
441: }