Actual source code: view.c

petsc-3.7.5 2017-01-01
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,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,NULL, "-log_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:    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.

117:    Collective on PetscViewer

119:    Input Parameters:
120: +  viewer - the viewer
121: -  format - the format 

123:    Output Parameter:
124: .   vf - viewer and format object

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

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

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

133: @*/
134: PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
135: {

139:   PetscObjectReference((PetscObject)viewer);
140:   PetscNew(vf);
141:   (*vf)->viewer = viewer;
142:   (*vf)->format = format;
143:   return(0);
144: }


149: /*@C
150:    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.

152:    Collective on PetscViewer

154:    Input Parameters:
155: .  viewer - the PetscViewerAndFormat to be destroyed.

157:    Level: developer

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

161: @*/
162: PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
163: {

167:   PetscViewerDestroy(&(*vf)->viewer);
168:   PetscFree(*vf);
169:   return(0);
170: }

174: /*@C
175:    PetscViewerGetType - Returns the type of a PetscViewer.

177:    Not Collective

179:    Input Parameter:
180: .   viewer - the PetscViewer

182:    Output Parameter:
183: .  type - PetscViewer type (see below)

185:    Available Types Include:
186: .  PETSCVIEWERSOCKET - Socket PetscViewer
187: .  PETSCVIEWERASCII - ASCII PetscViewer
188: .  PETSCVIEWERBINARY - binary file PetscViewer
189: .  PETSCVIEWERSTRING - string PetscViewer
190: .  PETSCVIEWERDRAW - drawing PetscViewer

192:    Level: intermediate

194:    Note:
195:    See include/petscviewer.h for a complete list of PetscViewers.

197:    PetscViewerType is actually a string

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

201: @*/
202: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
203: {
207:   *type = ((PetscObject)viewer)->type_name;
208:   return(0);
209: }

213: /*@C
214:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
215:    PetscViewer options in the database.

217:    Logically Collective on PetscViewer

219:    Input Parameter:
220: +  viewer - the PetscViewer context
221: -  prefix - the prefix to prepend to all option names

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

227:    Level: advanced

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

231: .seealso: PetscViewerSetFromOptions()
232: @*/
233: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
234: {

239:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
240:   return(0);
241: }

245: /*@C
246:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
247:    PetscViewer options in the database.

249:    Logically Collective on PetscViewer

251:    Input Parameters:
252: +  viewer - the PetscViewer context
253: -  prefix - the prefix to prepend to all option names

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

259:    Level: advanced

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

263: .seealso: PetscViewerGetOptionsPrefix()
264: @*/
265: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
266: {

271:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
272:   return(0);
273: }

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

281:    Not Collective

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

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

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

292:    Level: advanced

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

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

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

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

313:    Collective on PetscViewer

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

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

322:    Level: advanced

324: .keywords: PetscViewer, setup

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

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

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

347:    Collective on PetscViewer

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

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

362:    Level: beginner

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

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

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

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

420:    Collective on MPI_Comm

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

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

431:    Level: beginner

433:    Concepts: binary files, ascii files

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

445:   if (dtype == PETSC_STRING) {
446:     PetscInt c, i = 0, cnt;
447:     char *s = (char *)data;
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:     if (count) *count = c;
460:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
461:   } else {
462:     (*viewer->ops->read)(viewer, data, num, count, dtype);
463:   }
464:   return(0);
465: }