xref: /petsc/src/sys/classes/viewer/interface/view.c (revision 2fe279fdf3e687a416e4eadb7d3c7a82d60442c6)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
3798534f6SMatthew G. Knepley #include <petscdraw.h>
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith PetscClassId PETSC_VIEWER_CLASSID;
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
85c6c1daeSBarry Smith /*@C
9b6dade52SBarry Smith   PetscViewerFinalizePackage - This function destroys any global objects created in the 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 @*/
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 @*/
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 
81*2fe279fdSBarry Smith    Input Parameter:
82811af0c4SBarry Smith .  viewer - the `PetscViewer` to be destroyed.
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith    Level: beginner
855c6c1daeSBarry Smith 
86d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
875c6c1daeSBarry Smith @*/
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));
959371c9d4SSatish Balay   if (--((PetscObject)(*viewer))->refct > 0) {
969371c9d4SSatish Balay     *viewer = NULL;
973ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
989371c9d4SSatish Balay   }
995c6c1daeSBarry Smith 
1009566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
10148a46eb9SPierre Jolivet   if ((*viewer)->ops->destroy) PetscCall((*(*viewer)->ops->destroy)(*viewer));
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 
1303f423023SBarry Smith .seealso: [](sec_viewers), `PetscViewerFormat`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`,
1313f423023SBarry Smith           `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
132d7cbc13eSBarry Smith @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
134d71ae5a4SJacob Faibussowitsch {
135d7cbc13eSBarry Smith   PetscFunctionBegin;
1369566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)viewer));
1379566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
138d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
139d7cbc13eSBarry Smith   (*vf)->format = format;
140798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
141798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
1423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143d7cbc13eSBarry Smith }
144d7cbc13eSBarry Smith 
145fe01d993SBarry Smith /*@C
1463f423023SBarry Smith    PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct created with `PetscViewerAndFormatCreate()`
147fe01d993SBarry Smith 
148c3339decSBarry Smith    Collective
149fe01d993SBarry Smith 
150*2fe279fdSBarry Smith    Input Parameter:
151811af0c4SBarry Smith .  vf - the `PetscViewerAndFormat` to be destroyed.
152fe01d993SBarry Smith 
153d7cbc13eSBarry Smith    Level: developer
154fe01d993SBarry Smith 
1553f423023SBarry Smith .seealso: [](sec_viewers), `PetscViewerAndFormatCreate()`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`,
1563f423023SBarry Smith           `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
157fe01d993SBarry Smith @*/
158d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
159d71ae5a4SJacob Faibussowitsch {
160fe01d993SBarry Smith   PetscFunctionBegin;
1619566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
1629566063dSJacob Faibussowitsch   PetscCall(PetscDrawLGDestroy(&(*vf)->lg));
1639566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
1643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
165fe01d993SBarry Smith }
166fe01d993SBarry Smith 
1675c6c1daeSBarry Smith /*@C
168811af0c4SBarry Smith    PetscViewerGetType - Returns the type of a `PetscViewer`.
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith    Not Collective
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith    Input Parameter:
173811af0c4SBarry Smith .   viewer - the `PetscViewer`
1745c6c1daeSBarry Smith 
1755c6c1daeSBarry Smith    Output Parameter:
1763f423023SBarry Smith .  type - `PetscViewerType`
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith    Available Types Include:
179811af0c4SBarry Smith +  `PETSCVIEWERSOCKET` - Socket PetscViewer
180811af0c4SBarry Smith .  `PETSCVIEWERASCII` - ASCII PetscViewer
181811af0c4SBarry Smith .  `PETSCVIEWERBINARY` - binary file PetscViewer
182811af0c4SBarry Smith .  `PETSCVIEWERSTRING` - string PetscViewer
183811af0c4SBarry Smith -  `PETSCVIEWERDRAW` - drawing PetscViewer
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith    Level: intermediate
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith    Note:
188811af0c4SBarry Smith    `PetscViewerType` is actually a string
1895c6c1daeSBarry Smith 
190d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerType`, `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
1915c6c1daeSBarry Smith @*/
192d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type)
193d71ae5a4SJacob Faibussowitsch {
1945c6c1daeSBarry Smith   PetscFunctionBegin;
1955c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1965c6c1daeSBarry Smith   PetscValidPointer(type, 2);
1975c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1995c6c1daeSBarry Smith }
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith /*@C
2025c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
203811af0c4SBarry Smith    `PetscViewer` options in the database.
2045c6c1daeSBarry Smith 
205c3339decSBarry Smith    Logically Collective
2065c6c1daeSBarry Smith 
207d8d19677SJose E. Roman    Input Parameters:
208811af0c4SBarry Smith +  viewer - the `PetscViewer` context
2095c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2105c6c1daeSBarry Smith 
211811af0c4SBarry Smith    Note:
2125c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2135c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith    Level: advanced
2165c6c1daeSBarry Smith 
217d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSetFromOptions()`
2185c6c1daeSBarry Smith @*/
219d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[])
220d71ae5a4SJacob Faibussowitsch {
2215c6c1daeSBarry Smith   PetscFunctionBegin;
2225c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2239566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
2243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2255c6c1daeSBarry Smith }
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith /*@C
2285c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
229811af0c4SBarry Smith    `PetscViewer` options in the database.
2305c6c1daeSBarry Smith 
231c3339decSBarry Smith    Logically Collective
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith    Input Parameters:
23401311c95SBarry Smith +  viewer - the `PetscViewer` context
2355c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2365c6c1daeSBarry Smith 
23701311c95SBarry Smith    Level: advanced
23801311c95SBarry Smith 
239811af0c4SBarry Smith    Note:
2405c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2415c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2425c6c1daeSBarry Smith 
243d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerGetOptionsPrefix()`
2445c6c1daeSBarry Smith @*/
245d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[])
246d71ae5a4SJacob Faibussowitsch {
2475c6c1daeSBarry Smith   PetscFunctionBegin;
2485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2499566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
2503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2515c6c1daeSBarry Smith }
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith /*@C
2545c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
25501311c95SBarry Smith    `PetscViewer` options in the database.
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith    Not Collective
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith    Input Parameter:
260811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith    Output Parameter:
2635c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2645c6c1daeSBarry Smith 
26501311c95SBarry Smith    Level: advanced
26601311c95SBarry Smith 
267811af0c4SBarry Smith    Fortran Note:
268811af0c4SBarry Smith    The user should pass in a string 'prefix' of sufficient length to hold the prefix.
2695c6c1daeSBarry Smith 
270d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAppendOptionsPrefix()`
2715c6c1daeSBarry Smith @*/
272d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[])
273d71ae5a4SJacob Faibussowitsch {
2745c6c1daeSBarry Smith   PetscFunctionBegin;
2755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
2773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2785c6c1daeSBarry Smith }
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith /*@
2815c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2825c6c1daeSBarry Smith 
283c3339decSBarry Smith    Collective
2845c6c1daeSBarry Smith 
285*2fe279fdSBarry Smith    Input Parameter:
286811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2875c6c1daeSBarry Smith 
28801311c95SBarry Smith    Level: advanced
28901311c95SBarry Smith 
290811af0c4SBarry Smith    Note:
291811af0c4SBarry Smith    For basic use of the `PetscViewer` classes the user need not explicitly call
292811af0c4SBarry Smith    `PetscViewerSetUp()`, since these actions will happen automatically.
2935c6c1daeSBarry Smith 
294d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
2955c6c1daeSBarry Smith @*/
296d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
297d71ae5a4SJacob Faibussowitsch {
2985c6c1daeSBarry Smith   PetscFunctionBegin;
2995c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
3003ba16761SJacob Faibussowitsch   if (viewer->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
301dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
302c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
3033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3045c6c1daeSBarry Smith }
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith /*@C
3073f423023SBarry Smith    PetscViewerViewFromOptions - View from the viewer based on options in the options database
308fe2efc57SMark 
309c3339decSBarry Smith    Collective
310fe2efc57SMark 
311fe2efc57SMark    Input Parameters:
312811af0c4SBarry Smith +  A - the `PetscViewer` context
313811af0c4SBarry Smith .  obj - Optional object that provides the prefix for the option names
314736c3998SJose E. Roman -  name - command line option
315fe2efc57SMark 
316fe2efc57SMark    Level: intermediate
317811af0c4SBarry Smith 
3183f423023SBarry Smith    Note:
3193f423023SBarry Smith    See `PetscObjectViewFromOptions()` for details on the viewers and formats support via this interface
3203f423023SBarry Smith 
321d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
322fe2efc57SMark @*/
323d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[])
324d71ae5a4SJacob Faibussowitsch {
325fe2efc57SMark   PetscFunctionBegin;
326fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_VIEWER_CLASSID, 1);
3279566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
3283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
329fe2efc57SMark }
330fe2efc57SMark 
331fe2efc57SMark /*@C
3325c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3335c6c1daeSBarry Smith 
334c3339decSBarry Smith    Collective
3355c6c1daeSBarry Smith 
3365c6c1daeSBarry Smith    Input Parameters:
337f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3385c6c1daeSBarry Smith -  viewer - visualization context
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith    Level: beginner
3415c6c1daeSBarry Smith 
342d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
343db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3445c6c1daeSBarry Smith @*/
345d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer)
346d71ae5a4SJacob Faibussowitsch {
3475c6c1daeSBarry Smith   PetscBool         iascii;
3485c6c1daeSBarry Smith   PetscViewerFormat format;
349e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
350536b137fSBarry Smith   PetscBool issaws;
3510076e027SBarry Smith #endif
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith   PetscFunctionBegin;
3545c6c1daeSBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
3555c6c1daeSBarry Smith   PetscValidType(v, 1);
35648a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
3575c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3585c6c1daeSBarry Smith   PetscCheckSameComm(v, 1, viewer, 2);
3595c6c1daeSBarry Smith 
3609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
361e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3629566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
3630076e027SBarry Smith #endif
3645c6c1daeSBarry Smith   if (iascii) {
3659566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
3669566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
36798c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
36848a46eb9SPierre Jolivet       if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, "  Viewer format = %s\n", PetscViewerFormats[v->format]));
3699566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
370dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3719566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3725c6c1daeSBarry Smith     }
373e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
374536b137fSBarry Smith   } else if (issaws) {
3750076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3769566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
377dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3780076e027SBarry Smith     }
3790076e027SBarry Smith #endif
3805c6c1daeSBarry Smith   }
3813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3825c6c1daeSBarry Smith }
3831d641e7bSMichael Lange 
3841d641e7bSMichael Lange /*@C
385811af0c4SBarry Smith    PetscViewerRead - Reads data from a `PetscViewer`
3861d641e7bSMichael Lange 
387d083f849SBarry Smith    Collective
3881d641e7bSMichael Lange 
3891d641e7bSMichael Lange    Input Parameters:
3901d641e7bSMichael Lange +  viewer   - The viewer
3911d641e7bSMichael Lange .  data     - Location to write the data
392060da220SMatthew G. Knepley .  num      - Number of items of data to read
3931d641e7bSMichael Lange -  datatype - Type of data to read
3941d641e7bSMichael Lange 
395*2fe279fdSBarry Smith    Output Parameter:
39601311c95SBarry Smith .  count - number of items of data actually read, or `NULL`
39701311c95SBarry Smith 
39801311c95SBarry Smith    Level: beginner
399f8e4bde8SMatthew G. Knepley 
4003f423023SBarry Smith    Notes:
401811af0c4SBarry Smith    If datatype is `PETSC_STRING` and num is negative, reads until a newline character is found,
402632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
403632e26b4SStefano Zampini 
40401311c95SBarry Smith    Only certain viewers, such as `PETSCVIEWERBINARY` can be read from, see `PetscViewerReadable()`
4051d641e7bSMichael Lange 
406d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
40701311c95SBarry Smith           `PetscViewerReadable()`, `PetscViewerBinaryGetDescriptor()`,
408db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
4091d641e7bSMichael Lange @*/
410d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
411d71ae5a4SJacob Faibussowitsch {
4121d641e7bSMichael Lange   PetscFunctionBegin;
4131d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4141d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
415060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4161d641e7bSMichael Lange     char    *s = (char *)data;
417632e26b4SStefano Zampini     if (num >= 0) {
418060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4191d641e7bSMichael Lange         /* Skip leading whitespaces */
4209371c9d4SSatish Balay         do {
4219371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR));
4229371c9d4SSatish Balay           if (!cnt) break;
4239371c9d4SSatish Balay         } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4241d641e7bSMichael Lange         i++;
4251d641e7bSMichael Lange         /* Read strings one char at a time */
4269371c9d4SSatish Balay         do {
4279371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4289371c9d4SSatish Balay           if (!cnt) break;
4299371c9d4SSatish 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');
4301d641e7bSMichael Lange         /* Terminate final string */
431060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4321d641e7bSMichael Lange       }
433632e26b4SStefano Zampini     } else {
434632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4359371c9d4SSatish Balay       do {
4369371c9d4SSatish Balay         PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4379371c9d4SSatish Balay         if (i == -num || !cnt) break;
4389371c9d4SSatish Balay       } while (s[i - 1] != '\n');
439632e26b4SStefano Zampini       /* Terminate final string */
440632e26b4SStefano Zampini       s[i - 1] = '\0';
441632e26b4SStefano Zampini       c        = i;
442632e26b4SStefano Zampini     }
443060da220SMatthew G. Knepley     if (count) *count = c;
44408401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
445dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4471d641e7bSMichael Lange }
448e24fdd67SVaclav Hapla 
449af684e28SVaclav Hapla /*@
450af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
451af684e28SVaclav Hapla 
452af684e28SVaclav Hapla    Not Collective
453af684e28SVaclav Hapla 
454*2fe279fdSBarry Smith    Input Parameter:
455811af0c4SBarry Smith .  viewer - the `PetscViewer` context
456af684e28SVaclav Hapla 
457*2fe279fdSBarry Smith    Output Parameter:
458811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
459af684e28SVaclav Hapla 
46001311c95SBarry Smith    Level: intermediate
46101311c95SBarry Smith 
462811af0c4SBarry Smith    Note:
463811af0c4SBarry Smith    `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading (this holds e.g. for `PETSCVIEWERBINARY`)
464811af0c4SBarry Smith    and viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
465811af0c4SBarry Smith    returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
466af684e28SVaclav Hapla 
46701311c95SBarry Smith .seealso: [](sec_viewers), `PetscViewerRead()`, `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
468af684e28SVaclav Hapla @*/
469d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
470d71ae5a4SJacob Faibussowitsch {
471e24fdd67SVaclav Hapla   PetscFileMode mode;
472e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
473e24fdd67SVaclav Hapla 
474e24fdd67SVaclav Hapla   PetscFunctionBegin;
475e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
476534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
4779566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
478e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
4793ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
4809566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
481e24fdd67SVaclav Hapla   switch (mode) {
482e24fdd67SVaclav Hapla   case FILE_MODE_READ:
483e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
484d71ae5a4SJacob Faibussowitsch   case FILE_MODE_APPEND_UPDATE:
485d71ae5a4SJacob Faibussowitsch     *flg = PETSC_TRUE;
486d71ae5a4SJacob Faibussowitsch   default:
487d71ae5a4SJacob Faibussowitsch     break;
488e24fdd67SVaclav Hapla   }
4893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490e24fdd67SVaclav Hapla }
491e24fdd67SVaclav Hapla 
492af684e28SVaclav Hapla /*@
493af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
494af684e28SVaclav Hapla 
495af684e28SVaclav Hapla    Not Collective
496af684e28SVaclav Hapla 
497*2fe279fdSBarry Smith    Input Parameter:
498811af0c4SBarry Smith .  viewer - the `PetscViewer` context
499af684e28SVaclav Hapla 
500*2fe279fdSBarry Smith    Output Parameter:
501811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
502af684e28SVaclav Hapla 
50301311c95SBarry Smith    Level: intermediate
50401311c95SBarry Smith 
505811af0c4SBarry Smith    Note:
506811af0c4SBarry Smith    `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
507811af0c4SBarry Smith    returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
508af684e28SVaclav Hapla 
509d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
510af684e28SVaclav Hapla @*/
511d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
512d71ae5a4SJacob Faibussowitsch {
513e24fdd67SVaclav Hapla   PetscFileMode mode;
514e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
515e24fdd67SVaclav Hapla 
516e24fdd67SVaclav Hapla   PetscFunctionBegin;
517e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
518534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
5199566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
520e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
5213ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
5229566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
523e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
5243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
525e24fdd67SVaclav Hapla }
526e24fdd67SVaclav Hapla 
527af684e28SVaclav Hapla /*@
528811af0c4SBarry Smith    PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
529af684e28SVaclav Hapla 
530af684e28SVaclav Hapla    Collective
531af684e28SVaclav Hapla 
532*2fe279fdSBarry Smith    Input Parameter:
533811af0c4SBarry Smith .  viewer - the `PetscViewer` context
534af684e28SVaclav Hapla 
535af684e28SVaclav Hapla    Level: intermediate
536af684e28SVaclav Hapla 
537d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
538af684e28SVaclav Hapla @*/
539d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
540d71ae5a4SJacob Faibussowitsch {
541d01f05b1SVaclav Hapla   PetscBool flg;
542d01f05b1SVaclav Hapla 
543d01f05b1SVaclav Hapla   PetscFunctionBegin;
5440af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5459566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
54628b400f6SJacob 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)");
5473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
548d01f05b1SVaclav Hapla }
549d01f05b1SVaclav Hapla 
550af684e28SVaclav Hapla /*@
551811af0c4SBarry Smith    PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
552af684e28SVaclav Hapla 
553af684e28SVaclav Hapla    Collective
554af684e28SVaclav Hapla 
555*2fe279fdSBarry Smith    Input Parameter:
556811af0c4SBarry Smith .  viewer - the `PetscViewer` context
557af684e28SVaclav Hapla 
558af684e28SVaclav Hapla    Level: intermediate
559af684e28SVaclav Hapla 
560d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
561af684e28SVaclav Hapla @*/
562d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
563d71ae5a4SJacob Faibussowitsch {
564d01f05b1SVaclav Hapla   PetscBool flg;
565d01f05b1SVaclav Hapla 
566d01f05b1SVaclav Hapla   PetscFunctionBegin;
5670af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5689566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
56928b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
5703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
571d01f05b1SVaclav Hapla }
572