Actual source code: view.c
1: #include <petsc/private/viewerimpl.h>
2: #include <petscdraw.h>
4: PetscClassId PETSC_VIEWER_CLASSID;
6: static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
8: /*@C
9: PetscViewerFinalizePackage - This function destroys any global objects created in PETSc viewers. It is
10: called from `PetscFinalize()`.
12: Level: developer
14: .seealso: [](sec_viewers), `PetscViewer`, `PetscFinalize()`, `PetscViewerInitializePackage()`
15: @*/
16: PetscErrorCode PetscViewerFinalizePackage(void)
17: {
18: PetscFunctionBegin;
19: if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_keyval));
20: if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval));
21: if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval));
22: if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval));
23: if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval));
24: #if defined(PETSC_HAVE_HDF5)
25: if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval));
26: #endif
27: #if defined(PETSC_USE_SOCKETVIEWER)
28: if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval));
29: #endif
30: PetscCall(PetscFunctionListDestroy(&PetscViewerList));
31: PetscViewerPackageInitialized = PETSC_FALSE;
32: PetscViewerRegisterAllCalled = PETSC_FALSE;
33: PetscFunctionReturn(PETSC_SUCCESS);
34: }
36: /*@C
37: PetscViewerInitializePackage - This function initializes everything in the `PetscViewer` package.
39: Level: developer
41: .seealso: [](sec_viewers), `PetscViewer`, `PetscInitialize()`, `PetscViewerFinalizePackage()`
42: @*/
43: PetscErrorCode PetscViewerInitializePackage(void)
44: {
45: char logList[256];
46: PetscBool opt, pkg;
48: PetscFunctionBegin;
49: if (PetscViewerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
50: PetscViewerPackageInitialized = PETSC_TRUE;
51: /* Register Classes */
52: PetscCall(PetscClassIdRegister("Viewer", &PETSC_VIEWER_CLASSID));
53: /* Register Constructors */
54: PetscCall(PetscViewerRegisterAll());
55: /* Process Info */
56: {
57: PetscClassId classids[1];
59: classids[0] = PETSC_VIEWER_CLASSID;
60: PetscCall(PetscInfoProcessClass("viewer", 1, classids));
61: }
62: /* Process summary exclusions */
63: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
64: if (opt) {
65: PetscCall(PetscStrInList("viewer", logList, ',', &pkg));
66: if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID));
67: }
68: #if defined(PETSC_HAVE_MATHEMATICA)
69: PetscCall(PetscViewerMathematicaInitializePackage());
70: #endif
71: /* Register package finalizer */
72: PetscCall(PetscRegisterFinalize(PetscViewerFinalizePackage));
73: PetscFunctionReturn(PETSC_SUCCESS);
74: }
76: /*@
77: PetscViewerDestroy - Destroys a `PetscViewer`.
79: Collective
81: Input Parameter:
82: . viewer - the `PetscViewer` to be destroyed.
84: Level: beginner
86: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`
87: @*/
88: PetscErrorCode PetscViewerDestroy(PetscViewer *viewer)
89: {
90: PetscFunctionBegin;
91: if (!*viewer) PetscFunctionReturn(PETSC_SUCCESS);
94: PetscCall(PetscViewerFlush(*viewer));
95: if (--((PetscObject)*viewer)->refct > 0) {
96: *viewer = NULL;
97: PetscFunctionReturn(PETSC_SUCCESS);
98: }
100: PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
101: PetscTryTypeMethod(*viewer, destroy);
102: PetscCall(PetscHeaderDestroy(viewer));
103: PetscFunctionReturn(PETSC_SUCCESS);
104: }
106: /*@C
107: PetscViewerAndFormatCreate - Creates a `PetscViewerAndFormat` struct.
109: Collective
111: Input Parameters:
112: + viewer - the viewer
113: - format - the format
115: Output Parameter:
116: . vf - viewer and format object
118: Level: developer
120: Notes:
121: This increases the reference count of the viewer.
123: Use `PetscViewerAndFormatDestroy()` to free the struct
125: This is used as the context variable for many of the `TS`, `SNES`, and `KSP` monitor functions
127: This construct exists because it allows one to keep track of the use of a `PetscViewerFormat` without requiring the
128: format in the viewer to be permanently changed.
130: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAndFormat`, `PetscViewerFormat`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`,
131: `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
132: @*/
133: PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
134: {
135: PetscFunctionBegin;
136: PetscCall(PetscObjectReference((PetscObject)viewer));
137: PetscCall(PetscNew(vf));
138: (*vf)->viewer = viewer;
139: (*vf)->format = format;
140: (*vf)->data = NULL;
141: PetscFunctionReturn(PETSC_SUCCESS);
142: }
144: /*@C
145: PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct created with `PetscViewerAndFormatCreate()`
147: Collective
149: Input Parameter:
150: . vf - the `PetscViewerAndFormat` to be destroyed.
152: Level: developer
154: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAndFormat`, `PetscViewerFormat`, `PetscViewerAndFormatCreate()`, `PetscViewerSocketOpen()`,
155: `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
156: @*/
157: PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
158: {
159: PetscFunctionBegin;
160: if (!*vf) PetscFunctionReturn(PETSC_SUCCESS);
161: PetscCall(PetscViewerDestroy(&(*vf)->viewer));
162: if ((*vf)->data_destroy) PetscCall((*vf)->data_destroy(&(*vf)->data));
163: PetscCall(PetscFree(*vf));
164: PetscFunctionReturn(PETSC_SUCCESS);
165: }
167: /*@
168: PetscViewerGetType - Returns the type of a `PetscViewer`.
170: Not Collective
172: Input Parameter:
173: . viewer - the `PetscViewer`
175: Output Parameter:
176: . type - `PetscViewerType`
178: Level: intermediate
180: Note:
181: `PetscViewerType` is actually a string
183: .seealso: [](sec_viewers), `PetscViewerType`, `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`
184: @*/
185: PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type)
186: {
187: PetscFunctionBegin;
189: PetscAssertPointer(type, 2);
190: *type = ((PetscObject)viewer)->type_name;
191: PetscFunctionReturn(PETSC_SUCCESS);
192: }
194: /*@
195: PetscViewerSetOptionsPrefix - Sets the prefix used for searching for
196: `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
198: Logically Collective
200: Input Parameters:
201: + viewer - the `PetscViewer` context
202: - prefix - the prefix to prepend to all option names
204: Note:
205: A hyphen (-) must NOT be given at the beginning of the prefix name.
206: The first character of all runtime options is AUTOMATICALLY the hyphen.
208: Level: advanced
210: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSetFromOptions()`, `PetscViewerAppendOptionsPrefix()`
211: @*/
212: PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[])
213: {
214: PetscFunctionBegin;
216: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
217: PetscFunctionReturn(PETSC_SUCCESS);
218: }
220: /*@
221: PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for
222: `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
224: Logically Collective
226: Input Parameters:
227: + viewer - the `PetscViewer` context
228: - prefix - the prefix to prepend to all option names
230: Level: advanced
232: Note:
233: A hyphen (-) must NOT be given at the beginning of the prefix name.
234: The first character of all runtime options is AUTOMATICALLY the hyphen.
236: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerGetOptionsPrefix()`, `PetscViewerSetOptionsPrefix()`
237: @*/
238: PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[])
239: {
240: PetscFunctionBegin;
242: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
243: PetscFunctionReturn(PETSC_SUCCESS);
244: }
246: /*@
247: PetscViewerGetOptionsPrefix - Gets the prefix used for searching for
248: `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
250: Not Collective
252: Input Parameter:
253: . viewer - the `PetscViewer` context
255: Output Parameter:
256: . prefix - pointer to the prefix string used
258: Level: advanced
260: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAppendOptionsPrefix()`, `PetscViewerSetOptionsPrefix()`
261: @*/
262: PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[])
263: {
264: PetscFunctionBegin;
266: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
267: PetscFunctionReturn(PETSC_SUCCESS);
268: }
270: /*@
271: PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
273: Collective
275: Input Parameter:
276: . viewer - the `PetscViewer` context
278: Level: advanced
280: Note:
281: For basic use of the `PetscViewer` classes the user need not explicitly call
282: `PetscViewerSetUp()`, since these actions will happen automatically.
284: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
285: @*/
286: PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
287: {
288: PetscFunctionBegin;
290: if (viewer->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
291: PetscTryTypeMethod(viewer, setup);
292: viewer->setupcalled = PETSC_TRUE;
293: PetscFunctionReturn(PETSC_SUCCESS);
294: }
296: /*@
297: PetscViewerViewFromOptions - View from the viewer based on options in the options database
299: Collective
301: Input Parameters:
302: + A - the `PetscViewer` context
303: . obj - Optional object that provides the prefix for the option names
304: - name - command line option
306: Level: intermediate
308: Note:
309: See `PetscObjectViewFromOptions()` for details on the viewers and formats support via this interface
311: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
312: @*/
313: PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[])
314: {
315: PetscFunctionBegin;
317: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
318: PetscFunctionReturn(PETSC_SUCCESS);
319: }
321: /*@
322: PetscViewerView - Visualizes a viewer object.
324: Collective
326: Input Parameters:
327: + v - the viewer to be viewed
328: - viewer - visualization context
330: Level: beginner
332: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
333: `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
334: @*/
335: PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer)
336: {
337: PetscBool isascii;
338: PetscViewerFormat format;
339: #if defined(PETSC_HAVE_SAWS)
340: PetscBool issaws;
341: #endif
343: PetscFunctionBegin;
346: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
348: PetscCheckSameComm(v, 1, viewer, 2);
350: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
351: #if defined(PETSC_HAVE_SAWS)
352: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
353: #endif
354: if (isascii) {
355: PetscCall(PetscViewerGetFormat(viewer, &format));
356: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
357: if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
358: if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, " Viewer format = %s\n", PetscViewerFormats[v->format]));
359: PetscCall(PetscViewerASCIIPushTab(viewer));
360: PetscTryTypeMethod(v, view, viewer);
361: PetscCall(PetscViewerASCIIPopTab(viewer));
362: }
363: #if defined(PETSC_HAVE_SAWS)
364: } else if (issaws) {
365: if (!((PetscObject)v)->amsmem) {
366: PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
367: PetscTryTypeMethod(v, view, viewer);
368: }
369: #endif
370: }
371: PetscFunctionReturn(PETSC_SUCCESS);
372: }
374: /*@C
375: PetscViewerRead - Reads data from a `PetscViewer`
377: Collective
379: Input Parameters:
380: + viewer - The viewer
381: . data - Location to write the data, treated as an array of the type defined by `datatype`
382: . num - Number of items of data to read
383: - dtype - Type of data to read
385: Output Parameter:
386: . count - number of items of data actually read, or `NULL`
388: Level: beginner
390: Notes:
391: If datatype is `PETSC_STRING` and `num` is negative, reads until a newline character is found,
392: until a maximum of (-num - 1) chars.
394: Only certain viewers, such as `PETSCVIEWERBINARY` can be read from, see `PetscViewerReadable()`
396: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
397: `PetscViewerReadable()`, `PetscViewerBinaryGetDescriptor()`,
398: `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`
399: @*/
400: PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
401: {
402: PetscFunctionBegin;
404: if (dtype == PETSC_STRING) {
405: PetscInt c, i = 0, cnt;
406: char *s = (char *)data;
407: if (num >= 0) {
408: for (c = 0; c < num; c++) {
409: /* Skip leading whitespaces */
410: do {
411: PetscUseTypeMethod(viewer, read, &s[i], 1, &cnt, PETSC_CHAR);
412: if (!cnt) break;
413: } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
414: i++;
415: /* Read strings one char at a time */
416: do {
417: PetscUseTypeMethod(viewer, read, &s[i++], 1, &cnt, PETSC_CHAR);
418: if (!cnt) break;
419: } 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');
420: /* Terminate final string */
421: if (c == num - 1) s[i - 1] = '\0';
422: }
423: } else {
424: /* Read until a \n is encountered (-num is the max size allowed) */
425: do {
426: PetscUseTypeMethod(viewer, read, &s[i++], 1, &cnt, PETSC_CHAR);
427: if (i == -num || !cnt) break;
428: } while (s[i - 1] != '\n');
429: /* Terminate final string */
430: s[i - 1] = '\0';
431: c = i;
432: }
433: if (count) *count = c;
434: else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
435: } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
436: PetscFunctionReturn(PETSC_SUCCESS);
437: }
439: /*@
440: PetscViewerReadable - Return a flag whether the viewer can be read from with `PetscViewerRead()`
442: Not Collective
444: Input Parameter:
445: . viewer - the `PetscViewer` context
447: Output Parameter:
448: . flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
450: Level: intermediate
452: Note:
453: `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading, that is `PetscViewerRead()`, (this holds e.g. for `PETSCVIEWERBINARY`)
454: and the viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
455: returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
457: .seealso: [](sec_viewers), `PetscViewerRead()`, `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
458: @*/
459: PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
460: {
461: PetscFileMode mode;
462: PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
464: PetscFunctionBegin;
466: PetscAssertPointer(flg, 2);
467: PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
468: *flg = PETSC_FALSE;
469: if (!f) PetscFunctionReturn(PETSC_SUCCESS);
470: PetscCall((*f)(viewer, &mode));
471: switch (mode) {
472: case FILE_MODE_READ:
473: case FILE_MODE_UPDATE:
474: case FILE_MODE_APPEND_UPDATE:
475: *flg = PETSC_TRUE;
476: default:
477: break;
478: }
479: PetscFunctionReturn(PETSC_SUCCESS);
480: }
482: /*@
483: PetscViewerWritable - Return a flag whether the viewer can be written to with `PetscViewerWrite()`
485: Not Collective
487: Input Parameter:
488: . viewer - the `PetscViewer` context
490: Output Parameter:
491: . flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
493: Level: intermediate
495: Note:
496: `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
497: returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
499: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
500: @*/
501: PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
502: {
503: PetscFileMode mode;
504: PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
506: PetscFunctionBegin;
508: PetscAssertPointer(flg, 2);
509: PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
510: *flg = PETSC_TRUE;
511: if (!f) PetscFunctionReturn(PETSC_SUCCESS);
512: PetscCall((*f)(viewer, &mode));
513: if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
514: PetscFunctionReturn(PETSC_SUCCESS);
515: }
517: /*@
518: PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
520: Collective
522: Input Parameter:
523: . viewer - the `PetscViewer` context
525: Level: intermediate
527: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
528: @*/
529: PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
530: {
531: PetscBool flg;
533: PetscFunctionBegin;
535: PetscCall(PetscViewerReadable(viewer, &flg));
536: PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support reading, or is not in reading mode (FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE)");
537: PetscFunctionReturn(PETSC_SUCCESS);
538: }
540: /*@
541: PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
543: Collective
545: Input Parameter:
546: . viewer - the `PetscViewer` context
548: Level: intermediate
550: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
551: @*/
552: PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
553: {
554: PetscBool flg;
556: PetscFunctionBegin;
558: PetscCall(PetscViewerWritable(viewer, &flg));
559: PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
560: PetscFunctionReturn(PETSC_SUCCESS);
561: }