xref: /petsc/src/sys/classes/viewer/interface/view.c (revision bcda9346efad4e5ba2d553af84eb238771ba1e25)
1af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
2798534f6SMatthew G. Knepley #include <petscdraw.h>
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith PetscClassId PETSC_VIEWER_CLASSID;
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
766976f2fSJacob Faibussowitsch 
85c6c1daeSBarry Smith /*@C
9c410d8ccSBarry Smith   PetscViewerFinalizePackage - This function destroys any global objects created in PETSc viewers. It is
10811af0c4SBarry Smith   called from `PetscFinalize()`.
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith   Level: developer
135c6c1daeSBarry Smith 
143f423023SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscFinalize()`, `PetscViewerInitializePackage()`
155c6c1daeSBarry Smith @*/
PetscViewerFinalizePackage(void)16d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFinalizePackage(void)
17d71ae5a4SJacob Faibussowitsch {
185c6c1daeSBarry Smith   PetscFunctionBegin;
1948a46eb9SPierre Jolivet   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_keyval));
2048a46eb9SPierre Jolivet   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval));
2148a46eb9SPierre Jolivet   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval));
2248a46eb9SPierre Jolivet   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval));
2348a46eb9SPierre Jolivet   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval));
24b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5)
2548a46eb9SPierre Jolivet   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval));
26b6dade52SBarry Smith #endif
27b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER)
2848a46eb9SPierre Jolivet   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval));
29b6dade52SBarry Smith #endif
309566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PetscViewerList));
315c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_FALSE;
320f51fdf8SToby Isaac   PetscViewerRegisterAllCalled  = PETSC_FALSE;
333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
345c6c1daeSBarry Smith }
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith /*@C
37811af0c4SBarry Smith   PetscViewerInitializePackage - This function initializes everything in the `PetscViewer` package.
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith   Level: developer
405c6c1daeSBarry Smith 
413f423023SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscInitialize()`, `PetscViewerFinalizePackage()`
425c6c1daeSBarry Smith @*/
PetscViewerInitializePackage(void)43d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerInitializePackage(void)
44d71ae5a4SJacob Faibussowitsch {
455c6c1daeSBarry Smith   char      logList[256];
468e81d068SLisandro Dalcin   PetscBool opt, pkg;
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith   PetscFunctionBegin;
493ba16761SJacob Faibussowitsch   if (PetscViewerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
505c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
515c6c1daeSBarry Smith   /* Register Classes */
529566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("Viewer", &PETSC_VIEWER_CLASSID));
535c6c1daeSBarry Smith   /* Register Constructors */
549566063dSJacob Faibussowitsch   PetscCall(PetscViewerRegisterAll());
55e94e781bSJacob Faibussowitsch   /* Process Info */
56e94e781bSJacob Faibussowitsch   {
57e94e781bSJacob Faibussowitsch     PetscClassId classids[1];
58e94e781bSJacob Faibussowitsch 
59e94e781bSJacob Faibussowitsch     classids[0] = PETSC_VIEWER_CLASSID;
609566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("viewer", 1, classids));
615c6c1daeSBarry Smith   }
625c6c1daeSBarry Smith   /* Process summary exclusions */
639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
645c6c1daeSBarry Smith   if (opt) {
659566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("viewer", logList, ',', &pkg));
669566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID));
675c6c1daeSBarry Smith   }
685c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
699566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaInitializePackage());
705c6c1daeSBarry Smith #endif
718e81d068SLisandro Dalcin   /* Register package finalizer */
729566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PetscViewerFinalizePackage));
733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
745c6c1daeSBarry Smith }
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith /*@
77811af0c4SBarry Smith   PetscViewerDestroy - Destroys a `PetscViewer`.
785c6c1daeSBarry Smith 
79c3339decSBarry Smith   Collective
805c6c1daeSBarry Smith 
812fe279fdSBarry Smith   Input Parameter:
82811af0c4SBarry Smith . viewer - the `PetscViewer` to be destroyed.
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith   Level: beginner
855c6c1daeSBarry Smith 
86aec76313SJacob Faibussowitsch .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`
875c6c1daeSBarry Smith @*/
PetscViewerDestroy(PetscViewer * viewer)88d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDestroy(PetscViewer *viewer)
89d71ae5a4SJacob Faibussowitsch {
905c6c1daeSBarry Smith   PetscFunctionBegin;
913ba16761SJacob Faibussowitsch   if (!*viewer) PetscFunctionReturn(PETSC_SUCCESS);
925c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer, PETSC_VIEWER_CLASSID, 1);
935c6c1daeSBarry Smith 
949566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(*viewer));
95f4f49eeaSPierre Jolivet   if (--((PetscObject)*viewer)->refct > 0) {
969371c9d4SSatish Balay     *viewer = NULL;
973ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
989371c9d4SSatish Balay   }
995c6c1daeSBarry Smith 
1009566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
101213acdd3SPierre Jolivet   PetscTryTypeMethod(*viewer, destroy);
1029566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(viewer));
1033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1045c6c1daeSBarry Smith }
1055c6c1daeSBarry Smith 
106d7cbc13eSBarry Smith /*@C
107811af0c4SBarry Smith   PetscViewerAndFormatCreate - Creates a `PetscViewerAndFormat` struct.
108d7cbc13eSBarry Smith 
109c3339decSBarry Smith   Collective
110d7cbc13eSBarry Smith 
111d7cbc13eSBarry Smith   Input Parameters:
112d7cbc13eSBarry Smith + viewer - the viewer
113d7cbc13eSBarry Smith - format - the format
114d7cbc13eSBarry Smith 
115d7cbc13eSBarry Smith   Output Parameter:
116d7cbc13eSBarry Smith . vf - viewer and format object
117d7cbc13eSBarry Smith 
118d7cbc13eSBarry Smith   Level: developer
119d7cbc13eSBarry Smith 
120811af0c4SBarry Smith   Notes:
1213f423023SBarry Smith   This increases the reference count of the viewer.
1223f423023SBarry Smith 
1233f423023SBarry Smith   Use `PetscViewerAndFormatDestroy()` to free the struct
124811af0c4SBarry Smith 
125811af0c4SBarry Smith   This is used as the context variable for many of the `TS`, `SNES`, and `KSP` monitor functions
126d7cbc13eSBarry Smith 
1273f423023SBarry Smith   This construct exists because it allows one to keep track of the use of a `PetscViewerFormat` without requiring the
1283f423023SBarry Smith   format in the viewer to be permanently changed.
1293f423023SBarry Smith 
130c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAndFormat`, `PetscViewerFormat`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`,
1313f423023SBarry Smith           `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
132d7cbc13eSBarry Smith @*/
PetscViewerAndFormatCreate(PetscViewer viewer,PetscViewerFormat format,PetscViewerAndFormat ** vf)133d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
134d71ae5a4SJacob Faibussowitsch {
135d7cbc13eSBarry Smith   PetscFunctionBegin;
136648c30bcSBarry Smith   PetscCall(PetscObjectReference((PetscObject)viewer));
1379566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
138d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
139d7cbc13eSBarry Smith   (*vf)->format = format;
140798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
1413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
142d7cbc13eSBarry Smith }
143d7cbc13eSBarry Smith 
144fe01d993SBarry Smith /*@C
1453f423023SBarry Smith   PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct created with `PetscViewerAndFormatCreate()`
146fe01d993SBarry Smith 
147c3339decSBarry Smith   Collective
148fe01d993SBarry Smith 
1492fe279fdSBarry Smith   Input Parameter:
150811af0c4SBarry Smith . vf - the `PetscViewerAndFormat` to be destroyed.
151fe01d993SBarry Smith 
152d7cbc13eSBarry Smith   Level: developer
153fe01d993SBarry Smith 
154c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAndFormat`, `PetscViewerFormat`, `PetscViewerAndFormatCreate()`, `PetscViewerSocketOpen()`,
15542747ad1SJacob Faibussowitsch           `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
156fe01d993SBarry Smith @*/
PetscViewerAndFormatDestroy(PetscViewerAndFormat ** vf)157d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
158d71ae5a4SJacob Faibussowitsch {
159fe01d993SBarry Smith   PetscFunctionBegin;
160648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
161e15058bcSJames Wright   if ((*vf)->data_destroy) PetscCall((*vf)->data_destroy(&(*vf)->data));
1629566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
1633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
164fe01d993SBarry Smith }
165fe01d993SBarry Smith 
1665d83a8b1SBarry Smith /*@
167811af0c4SBarry Smith   PetscViewerGetType - Returns the type of a `PetscViewer`.
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith   Not Collective
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith   Input Parameter:
172811af0c4SBarry Smith . viewer - the `PetscViewer`
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   Output Parameter:
17510450e9eSJacob Faibussowitsch . type - `PetscViewerType`
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith   Level: intermediate
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   Note:
180811af0c4SBarry Smith   `PetscViewerType` is actually a string
1815c6c1daeSBarry Smith 
182aec76313SJacob Faibussowitsch .seealso: [](sec_viewers), `PetscViewerType`, `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`
1835c6c1daeSBarry Smith @*/
PetscViewerGetType(PetscViewer viewer,PetscViewerType * type)184d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type)
185d71ae5a4SJacob Faibussowitsch {
1865c6c1daeSBarry Smith   PetscFunctionBegin;
1875c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1884f572ea9SToby Isaac   PetscAssertPointer(type, 2);
1895c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1915c6c1daeSBarry Smith }
1925c6c1daeSBarry Smith 
1935d83a8b1SBarry Smith /*@
194c410d8ccSBarry Smith   PetscViewerSetOptionsPrefix - Sets the prefix used for searching for
195c410d8ccSBarry Smith   `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
1965c6c1daeSBarry Smith 
197c3339decSBarry Smith   Logically Collective
1985c6c1daeSBarry Smith 
199d8d19677SJose E. Roman   Input Parameters:
200811af0c4SBarry Smith + viewer - the `PetscViewer` context
2015c6c1daeSBarry Smith - prefix - the prefix to prepend to all option names
2025c6c1daeSBarry Smith 
203811af0c4SBarry Smith   Note:
2045c6c1daeSBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
2055c6c1daeSBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith   Level: advanced
2085c6c1daeSBarry Smith 
209c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSetFromOptions()`, `PetscViewerAppendOptionsPrefix()`
2105c6c1daeSBarry Smith @*/
PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])211d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[])
212d71ae5a4SJacob Faibussowitsch {
2135c6c1daeSBarry Smith   PetscFunctionBegin;
2145c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2159566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
2163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2175c6c1daeSBarry Smith }
2185c6c1daeSBarry Smith 
2195d83a8b1SBarry Smith /*@
220c410d8ccSBarry Smith   PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for
221c410d8ccSBarry Smith   `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
2225c6c1daeSBarry Smith 
223c3339decSBarry Smith   Logically Collective
2245c6c1daeSBarry Smith 
2255c6c1daeSBarry Smith   Input Parameters:
22601311c95SBarry Smith + viewer - the `PetscViewer` context
2275c6c1daeSBarry Smith - prefix - the prefix to prepend to all option names
2285c6c1daeSBarry Smith 
22901311c95SBarry Smith   Level: advanced
23001311c95SBarry Smith 
231811af0c4SBarry Smith   Note:
2325c6c1daeSBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
2335c6c1daeSBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
2345c6c1daeSBarry Smith 
235c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerGetOptionsPrefix()`, `PetscViewerSetOptionsPrefix()`
2365c6c1daeSBarry Smith @*/
PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])237d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[])
238d71ae5a4SJacob Faibussowitsch {
2395c6c1daeSBarry Smith   PetscFunctionBegin;
2405c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2419566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
2423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2435c6c1daeSBarry Smith }
2445c6c1daeSBarry Smith 
245cc4c1da9SBarry Smith /*@
246c410d8ccSBarry Smith   PetscViewerGetOptionsPrefix - Gets the prefix used for searching for
247c410d8ccSBarry Smith   `PetscViewer` options in the database during `PetscViewerSetFromOptions()`.
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith   Not Collective
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith   Input Parameter:
252811af0c4SBarry Smith . viewer - the `PetscViewer` context
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith   Output Parameter:
2555c6c1daeSBarry Smith . prefix - pointer to the prefix string used
2565c6c1daeSBarry Smith 
25701311c95SBarry Smith   Level: advanced
25801311c95SBarry Smith 
259c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAppendOptionsPrefix()`, `PetscViewerSetOptionsPrefix()`
2605c6c1daeSBarry Smith @*/
PetscViewerGetOptionsPrefix(PetscViewer viewer,const char * prefix[])261d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[])
262d71ae5a4SJacob Faibussowitsch {
2635c6c1daeSBarry Smith   PetscFunctionBegin;
2645c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
2663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2675c6c1daeSBarry Smith }
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith /*@
2705c6c1daeSBarry Smith   PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2715c6c1daeSBarry Smith 
272c3339decSBarry Smith   Collective
2735c6c1daeSBarry Smith 
2742fe279fdSBarry Smith   Input Parameter:
275811af0c4SBarry Smith . viewer - the `PetscViewer` context
2765c6c1daeSBarry Smith 
27701311c95SBarry Smith   Level: advanced
27801311c95SBarry Smith 
279811af0c4SBarry Smith   Note:
280811af0c4SBarry Smith   For basic use of the `PetscViewer` classes the user need not explicitly call
281811af0c4SBarry Smith   `PetscViewerSetUp()`, since these actions will happen automatically.
2825c6c1daeSBarry Smith 
283d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
2845c6c1daeSBarry Smith @*/
PetscViewerSetUp(PetscViewer viewer)285d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
286d71ae5a4SJacob Faibussowitsch {
2875c6c1daeSBarry Smith   PetscFunctionBegin;
2885c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2893ba16761SJacob Faibussowitsch   if (viewer->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
290dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
291c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
2923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2935c6c1daeSBarry Smith }
2945c6c1daeSBarry Smith 
295ffeef943SBarry Smith /*@
2963f423023SBarry Smith   PetscViewerViewFromOptions - View from the viewer based on options in the options database
297fe2efc57SMark 
298c3339decSBarry Smith   Collective
299fe2efc57SMark 
300fe2efc57SMark   Input Parameters:
301811af0c4SBarry Smith + A    - the `PetscViewer` context
302811af0c4SBarry Smith . obj  - Optional object that provides the prefix for the option names
303736c3998SJose E. Roman - name - command line option
304fe2efc57SMark 
305fe2efc57SMark   Level: intermediate
306811af0c4SBarry Smith 
3073f423023SBarry Smith   Note:
3083f423023SBarry Smith   See `PetscObjectViewFromOptions()` for details on the viewers and formats support via this interface
3093f423023SBarry Smith 
310d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
311fe2efc57SMark @*/
PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])312d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[])
313d71ae5a4SJacob Faibussowitsch {
314fe2efc57SMark   PetscFunctionBegin;
315fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_VIEWER_CLASSID, 1);
3169566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
3173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
318fe2efc57SMark }
319fe2efc57SMark 
320ffeef943SBarry Smith /*@
3215c6c1daeSBarry Smith   PetscViewerView - Visualizes a viewer object.
3225c6c1daeSBarry Smith 
323c3339decSBarry Smith   Collective
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith   Input Parameters:
326f0d4698bSVaclav Hapla + v      - the viewer to be viewed
3275c6c1daeSBarry Smith - viewer - visualization context
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith   Level: beginner
3305c6c1daeSBarry Smith 
331d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
332db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3335c6c1daeSBarry Smith @*/
PetscViewerView(PetscViewer v,PetscViewer viewer)334d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer)
335d71ae5a4SJacob Faibussowitsch {
336*9f196a02SMartin Diehl   PetscBool         isascii;
3375c6c1daeSBarry Smith   PetscViewerFormat format;
338e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
339536b137fSBarry Smith   PetscBool issaws;
3400076e027SBarry Smith #endif
3415c6c1daeSBarry Smith 
3425c6c1daeSBarry Smith   PetscFunctionBegin;
3435c6c1daeSBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
3445c6c1daeSBarry Smith   PetscValidType(v, 1);
34548a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
3465c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3475c6c1daeSBarry Smith   PetscCheckSameComm(v, 1, viewer, 2);
3485c6c1daeSBarry Smith 
349*9f196a02SMartin Diehl   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
350e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
3520076e027SBarry Smith #endif
353*9f196a02SMartin Diehl   if (isascii) {
3549566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
3559566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
35698c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
35748a46eb9SPierre Jolivet       if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, "  Viewer format = %s\n", PetscViewerFormats[v->format]));
3589566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
359dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3609566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3615c6c1daeSBarry Smith     }
362e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
363536b137fSBarry Smith   } else if (issaws) {
3640076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3659566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
366dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3670076e027SBarry Smith     }
3680076e027SBarry Smith #endif
3695c6c1daeSBarry Smith   }
3703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3715c6c1daeSBarry Smith }
3721d641e7bSMichael Lange 
3731d641e7bSMichael Lange /*@C
374811af0c4SBarry Smith   PetscViewerRead - Reads data from a `PetscViewer`
3751d641e7bSMichael Lange 
376d083f849SBarry Smith   Collective
3771d641e7bSMichael Lange 
3781d641e7bSMichael Lange   Input Parameters:
3791d641e7bSMichael Lange + viewer - The viewer
380c410d8ccSBarry Smith . data   - Location to write the data, treated as an array of the type defined by `datatype`
381060da220SMatthew G. Knepley . num    - Number of items of data to read
382aec76313SJacob Faibussowitsch - dtype  - Type of data to read
3831d641e7bSMichael Lange 
3842fe279fdSBarry Smith   Output Parameter:
38501311c95SBarry Smith . count - number of items of data actually read, or `NULL`
38601311c95SBarry Smith 
38701311c95SBarry Smith   Level: beginner
388f8e4bde8SMatthew G. Knepley 
3893f423023SBarry Smith   Notes:
390c410d8ccSBarry Smith   If datatype is `PETSC_STRING` and `num` is negative, reads until a newline character is found,
391632e26b4SStefano Zampini   until a maximum of (-num - 1) chars.
392632e26b4SStefano Zampini 
39301311c95SBarry Smith   Only certain viewers, such as `PETSCVIEWERBINARY` can be read from, see `PetscViewerReadable()`
3941d641e7bSMichael Lange 
395d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
39601311c95SBarry Smith           `PetscViewerReadable()`, `PetscViewerBinaryGetDescriptor()`,
397aec76313SJacob Faibussowitsch           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`
3981d641e7bSMichael Lange @*/
PetscViewerRead(PetscViewer viewer,void * data,PetscInt num,PetscInt * count,PetscDataType dtype)399d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
400d71ae5a4SJacob Faibussowitsch {
4011d641e7bSMichael Lange   PetscFunctionBegin;
4021d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4031d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
404060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4051d641e7bSMichael Lange     char    *s = (char *)data;
406632e26b4SStefano Zampini     if (num >= 0) {
407060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4081d641e7bSMichael Lange         /* Skip leading whitespaces */
4099371c9d4SSatish Balay         do {
410f4f49eeaSPierre Jolivet           PetscUseTypeMethod(viewer, read, &s[i], 1, &cnt, PETSC_CHAR);
4119371c9d4SSatish Balay           if (!cnt) break;
4129371c9d4SSatish Balay         } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4131d641e7bSMichael Lange         i++;
4141d641e7bSMichael Lange         /* Read strings one char at a time */
4159371c9d4SSatish Balay         do {
416f4f49eeaSPierre Jolivet           PetscUseTypeMethod(viewer, read, &s[i++], 1, &cnt, PETSC_CHAR);
4179371c9d4SSatish Balay           if (!cnt) break;
4189371c9d4SSatish Balay         } 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');
4191d641e7bSMichael Lange         /* Terminate final string */
420060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4211d641e7bSMichael Lange       }
422632e26b4SStefano Zampini     } else {
423632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4249371c9d4SSatish Balay       do {
425f4f49eeaSPierre Jolivet         PetscUseTypeMethod(viewer, read, &s[i++], 1, &cnt, PETSC_CHAR);
4269371c9d4SSatish Balay         if (i == -num || !cnt) break;
4279371c9d4SSatish Balay       } while (s[i - 1] != '\n');
428632e26b4SStefano Zampini       /* Terminate final string */
429632e26b4SStefano Zampini       s[i - 1] = '\0';
430632e26b4SStefano Zampini       c        = i;
431632e26b4SStefano Zampini     }
432060da220SMatthew G. Knepley     if (count) *count = c;
43308401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
434dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4361d641e7bSMichael Lange }
437e24fdd67SVaclav Hapla 
438af684e28SVaclav Hapla /*@
439c410d8ccSBarry Smith   PetscViewerReadable - Return a flag whether the viewer can be read from with `PetscViewerRead()`
440af684e28SVaclav Hapla 
441af684e28SVaclav Hapla   Not Collective
442af684e28SVaclav Hapla 
4432fe279fdSBarry Smith   Input Parameter:
444811af0c4SBarry Smith . viewer - the `PetscViewer` context
445af684e28SVaclav Hapla 
4462fe279fdSBarry Smith   Output Parameter:
447811af0c4SBarry Smith . flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
448af684e28SVaclav Hapla 
44901311c95SBarry Smith   Level: intermediate
45001311c95SBarry Smith 
451811af0c4SBarry Smith   Note:
452c410d8ccSBarry Smith   `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading, that is `PetscViewerRead()`, (this holds e.g. for `PETSCVIEWERBINARY`)
453c410d8ccSBarry Smith   and the viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
454811af0c4SBarry Smith   returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
455af684e28SVaclav Hapla 
45601311c95SBarry Smith .seealso: [](sec_viewers), `PetscViewerRead()`, `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
457af684e28SVaclav Hapla @*/
PetscViewerReadable(PetscViewer viewer,PetscBool * flg)458d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
459d71ae5a4SJacob Faibussowitsch {
460e24fdd67SVaclav Hapla   PetscFileMode mode;
461e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
462e24fdd67SVaclav Hapla 
463e24fdd67SVaclav Hapla   PetscFunctionBegin;
464e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4654f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
4669566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
467e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
4683ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
4699566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
470e24fdd67SVaclav Hapla   switch (mode) {
471e24fdd67SVaclav Hapla   case FILE_MODE_READ:
472e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
473d71ae5a4SJacob Faibussowitsch   case FILE_MODE_APPEND_UPDATE:
474d71ae5a4SJacob Faibussowitsch     *flg = PETSC_TRUE;
475d71ae5a4SJacob Faibussowitsch   default:
476d71ae5a4SJacob Faibussowitsch     break;
477e24fdd67SVaclav Hapla   }
4783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
479e24fdd67SVaclav Hapla }
480e24fdd67SVaclav Hapla 
481af684e28SVaclav Hapla /*@
482c410d8ccSBarry Smith   PetscViewerWritable - Return a flag whether the viewer can be written to with `PetscViewerWrite()`
483af684e28SVaclav Hapla 
484af684e28SVaclav Hapla   Not Collective
485af684e28SVaclav Hapla 
4862fe279fdSBarry Smith   Input Parameter:
487811af0c4SBarry Smith . viewer - the `PetscViewer` context
488af684e28SVaclav Hapla 
4892fe279fdSBarry Smith   Output Parameter:
490811af0c4SBarry Smith . flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
491af684e28SVaclav Hapla 
49201311c95SBarry Smith   Level: intermediate
49301311c95SBarry Smith 
494811af0c4SBarry Smith   Note:
495811af0c4SBarry Smith   `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
496811af0c4SBarry Smith   returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
497af684e28SVaclav Hapla 
498d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
499af684e28SVaclav Hapla @*/
PetscViewerWritable(PetscViewer viewer,PetscBool * flg)500d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
501d71ae5a4SJacob Faibussowitsch {
502e24fdd67SVaclav Hapla   PetscFileMode mode;
503e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
504e24fdd67SVaclav Hapla 
505e24fdd67SVaclav Hapla   PetscFunctionBegin;
506e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5074f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
5089566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
509e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
5103ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
5119566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
512e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
5133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
514e24fdd67SVaclav Hapla }
515e24fdd67SVaclav Hapla 
516af684e28SVaclav Hapla /*@
517811af0c4SBarry Smith   PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
518af684e28SVaclav Hapla 
519af684e28SVaclav Hapla   Collective
520af684e28SVaclav Hapla 
5212fe279fdSBarry Smith   Input Parameter:
522811af0c4SBarry Smith . viewer - the `PetscViewer` context
523af684e28SVaclav Hapla 
524af684e28SVaclav Hapla   Level: intermediate
525af684e28SVaclav Hapla 
526d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
527af684e28SVaclav Hapla @*/
PetscViewerCheckReadable(PetscViewer viewer)528d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
529d71ae5a4SJacob Faibussowitsch {
530d01f05b1SVaclav Hapla   PetscBool flg;
531d01f05b1SVaclav Hapla 
532d01f05b1SVaclav Hapla   PetscFunctionBegin;
5330af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5349566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
53528b400f6SJacob Faibussowitsch   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)");
5363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
537d01f05b1SVaclav Hapla }
538d01f05b1SVaclav Hapla 
539af684e28SVaclav Hapla /*@
540811af0c4SBarry Smith   PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
541af684e28SVaclav Hapla 
542af684e28SVaclav Hapla   Collective
543af684e28SVaclav Hapla 
5442fe279fdSBarry Smith   Input Parameter:
545811af0c4SBarry Smith . viewer - the `PetscViewer` context
546af684e28SVaclav Hapla 
547af684e28SVaclav Hapla   Level: intermediate
548af684e28SVaclav Hapla 
549d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
550af684e28SVaclav Hapla @*/
PetscViewerCheckWritable(PetscViewer viewer)551d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
552d71ae5a4SJacob Faibussowitsch {
553d01f05b1SVaclav Hapla   PetscBool flg;
554d01f05b1SVaclav Hapla 
555d01f05b1SVaclav Hapla   PetscFunctionBegin;
5560af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5579566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
55828b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
5593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
560d01f05b1SVaclav Hapla }
561