xref: /petsc/src/sys/classes/viewer/interface/view.c (revision 01311c95a4eb0c722f7a49062585955469f8d6c4)
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 
14d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscFinalize()`
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 
41d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscInitialize()`
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 
815c6c1daeSBarry Smith    Input Parameters:
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:
121811af0c4SBarry Smith    This increases the reference count of the viewer so you can destroy the viewer object after this call
122811af0c4SBarry Smith 
123811af0c4SBarry Smith    This is used as the context variable for many of the `TS`, `SNES`, and `KSP` monitor functions
124d7cbc13eSBarry Smith 
125d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
126d7cbc13eSBarry Smith @*/
127d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
128d71ae5a4SJacob Faibussowitsch {
129d7cbc13eSBarry Smith   PetscFunctionBegin;
1309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)viewer));
1319566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
132d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
133d7cbc13eSBarry Smith   (*vf)->format = format;
134798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
135798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
1363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
137d7cbc13eSBarry Smith }
138d7cbc13eSBarry Smith 
139fe01d993SBarry Smith /*@C
140811af0c4SBarry Smith    PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct.
141fe01d993SBarry Smith 
142c3339decSBarry Smith    Collective
143fe01d993SBarry Smith 
144fe01d993SBarry Smith    Input Parameters:
145811af0c4SBarry Smith .  vf - the `PetscViewerAndFormat` to be destroyed.
146fe01d993SBarry Smith 
147d7cbc13eSBarry Smith    Level: developer
148fe01d993SBarry Smith 
149d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatCreate()`
150fe01d993SBarry Smith @*/
151d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
152d71ae5a4SJacob Faibussowitsch {
153fe01d993SBarry Smith   PetscFunctionBegin;
1549566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
1559566063dSJacob Faibussowitsch   PetscCall(PetscDrawLGDestroy(&(*vf)->lg));
1569566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
1573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
158fe01d993SBarry Smith }
159fe01d993SBarry Smith 
1605c6c1daeSBarry Smith /*@C
161811af0c4SBarry Smith    PetscViewerGetType - Returns the type of a `PetscViewer`.
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith    Not Collective
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith    Input Parameter:
166811af0c4SBarry Smith .   viewer - the `PetscViewer`
1675c6c1daeSBarry Smith 
1685c6c1daeSBarry Smith    Output Parameter:
169*01311c95SBarry Smith .  type - `PetscViewerType` (see below)
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith    Available Types Include:
172811af0c4SBarry Smith +  `PETSCVIEWERSOCKET` - Socket PetscViewer
173811af0c4SBarry Smith .  `PETSCVIEWERASCII` - ASCII PetscViewer
174811af0c4SBarry Smith .  `PETSCVIEWERBINARY` - binary file PetscViewer
175811af0c4SBarry Smith .  `PETSCVIEWERSTRING` - string PetscViewer
176811af0c4SBarry Smith -  `PETSCVIEWERDRAW` - drawing PetscViewer
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith    Level: intermediate
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith    Note:
181811af0c4SBarry Smith    See include/petscviewer.h for a complete list of `PetscViewer`s.
1825c6c1daeSBarry Smith 
183811af0c4SBarry Smith    `PetscViewerType` is actually a string
1845c6c1daeSBarry Smith 
185d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerType`, `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
1865c6c1daeSBarry Smith @*/
187d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type)
188d71ae5a4SJacob Faibussowitsch {
1895c6c1daeSBarry Smith   PetscFunctionBegin;
1905c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1915c6c1daeSBarry Smith   PetscValidPointer(type, 2);
1925c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1945c6c1daeSBarry Smith }
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith /*@C
1975c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
198811af0c4SBarry Smith    `PetscViewer` options in the database.
1995c6c1daeSBarry Smith 
200c3339decSBarry Smith    Logically Collective
2015c6c1daeSBarry Smith 
202d8d19677SJose E. Roman    Input Parameters:
203811af0c4SBarry Smith +  viewer - the `PetscViewer` context
2045c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2055c6c1daeSBarry Smith 
206811af0c4SBarry Smith    Note:
2075c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2085c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2095c6c1daeSBarry Smith 
2105c6c1daeSBarry Smith    Level: advanced
2115c6c1daeSBarry Smith 
212d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSetFromOptions()`
2135c6c1daeSBarry Smith @*/
214d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[])
215d71ae5a4SJacob Faibussowitsch {
2165c6c1daeSBarry Smith   PetscFunctionBegin;
2175c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2189566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
2193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2205c6c1daeSBarry Smith }
2215c6c1daeSBarry Smith 
2225c6c1daeSBarry Smith /*@C
2235c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
224811af0c4SBarry Smith    `PetscViewer` options in the database.
2255c6c1daeSBarry Smith 
226c3339decSBarry Smith    Logically Collective
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith    Input Parameters:
229*01311c95SBarry Smith +  viewer - the `PetscViewer` context
2305c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2315c6c1daeSBarry Smith 
232*01311c95SBarry Smith    Level: advanced
233*01311c95SBarry Smith 
234811af0c4SBarry Smith    Note:
2355c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2365c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2375c6c1daeSBarry Smith 
238d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerGetOptionsPrefix()`
2395c6c1daeSBarry Smith @*/
240d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[])
241d71ae5a4SJacob Faibussowitsch {
2425c6c1daeSBarry Smith   PetscFunctionBegin;
2435c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2449566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
2453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2465c6c1daeSBarry Smith }
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith /*@C
2495c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
250*01311c95SBarry Smith    `PetscViewer` options in the database.
2515c6c1daeSBarry Smith 
2525c6c1daeSBarry Smith    Not Collective
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith    Input Parameter:
255811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith    Output Parameter:
2585c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2595c6c1daeSBarry Smith 
260*01311c95SBarry Smith    Level: advanced
261*01311c95SBarry Smith 
262811af0c4SBarry Smith    Fortran Note:
263811af0c4SBarry Smith    The user should pass in a string 'prefix' of sufficient length to hold the prefix.
2645c6c1daeSBarry Smith 
265d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerAppendOptionsPrefix()`
2665c6c1daeSBarry Smith @*/
267d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[])
268d71ae5a4SJacob Faibussowitsch {
2695c6c1daeSBarry Smith   PetscFunctionBegin;
2705c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
2723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2735c6c1daeSBarry Smith }
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith /*@
2765c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2775c6c1daeSBarry Smith 
278c3339decSBarry Smith    Collective
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith    Input Parameters:
281811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2825c6c1daeSBarry Smith 
283*01311c95SBarry Smith    Level: advanced
284*01311c95SBarry Smith 
285811af0c4SBarry Smith    Note:
286811af0c4SBarry Smith    For basic use of the `PetscViewer` classes the user need not explicitly call
287811af0c4SBarry Smith    `PetscViewerSetUp()`, since these actions will happen automatically.
2885c6c1daeSBarry Smith 
289d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
2905c6c1daeSBarry Smith @*/
291d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
292d71ae5a4SJacob Faibussowitsch {
2935c6c1daeSBarry Smith   PetscFunctionBegin;
2945c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2953ba16761SJacob Faibussowitsch   if (viewer->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
296dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
297c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
2983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2995c6c1daeSBarry Smith }
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith /*@C
302811af0c4SBarry Smith    PetscViewerViewFromOptions - View from the viewer based on the options database values
303fe2efc57SMark 
304c3339decSBarry Smith    Collective
305fe2efc57SMark 
306fe2efc57SMark    Input Parameters:
307811af0c4SBarry Smith +  A - the `PetscViewer` context
308811af0c4SBarry Smith .  obj - Optional object that provides the prefix for the option names
309736c3998SJose E. Roman -  name - command line option
310fe2efc57SMark 
311fe2efc57SMark    Level: intermediate
312811af0c4SBarry Smith 
313d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
314fe2efc57SMark @*/
315d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[])
316d71ae5a4SJacob Faibussowitsch {
317fe2efc57SMark   PetscFunctionBegin;
318fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_VIEWER_CLASSID, 1);
3199566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
3203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
321fe2efc57SMark }
322fe2efc57SMark 
323fe2efc57SMark /*@C
3245c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3255c6c1daeSBarry Smith 
326c3339decSBarry Smith    Collective
3275c6c1daeSBarry Smith 
3285c6c1daeSBarry Smith    Input Parameters:
329f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3305c6c1daeSBarry Smith -  viewer - visualization context
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith    Level: beginner
3335c6c1daeSBarry Smith 
334d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
335db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3365c6c1daeSBarry Smith @*/
337d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer)
338d71ae5a4SJacob Faibussowitsch {
3395c6c1daeSBarry Smith   PetscBool         iascii;
3405c6c1daeSBarry Smith   PetscViewerFormat format;
341e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
342536b137fSBarry Smith   PetscBool issaws;
3430076e027SBarry Smith #endif
3445c6c1daeSBarry Smith 
3455c6c1daeSBarry Smith   PetscFunctionBegin;
3465c6c1daeSBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
3475c6c1daeSBarry Smith   PetscValidType(v, 1);
34848a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
3495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3505c6c1daeSBarry Smith   PetscCheckSameComm(v, 1, viewer, 2);
3515c6c1daeSBarry Smith 
3529566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
353e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3549566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
3550076e027SBarry Smith #endif
3565c6c1daeSBarry Smith   if (iascii) {
3579566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
3589566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
35998c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
36048a46eb9SPierre Jolivet       if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, "  Viewer format = %s\n", PetscViewerFormats[v->format]));
3619566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
362dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3639566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3645c6c1daeSBarry Smith     }
365e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
366536b137fSBarry Smith   } else if (issaws) {
3670076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3689566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
369dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3700076e027SBarry Smith     }
3710076e027SBarry Smith #endif
3725c6c1daeSBarry Smith   }
3733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3745c6c1daeSBarry Smith }
3751d641e7bSMichael Lange 
3761d641e7bSMichael Lange /*@C
377811af0c4SBarry Smith    PetscViewerRead - Reads data from a `PetscViewer`
3781d641e7bSMichael Lange 
379d083f849SBarry Smith    Collective
3801d641e7bSMichael Lange 
3811d641e7bSMichael Lange    Input Parameters:
3821d641e7bSMichael Lange +  viewer   - The viewer
3831d641e7bSMichael Lange .  data     - Location to write the data
384060da220SMatthew G. Knepley .  num      - Number of items of data to read
3851d641e7bSMichael Lange -  datatype - Type of data to read
3861d641e7bSMichael Lange 
387f8e4bde8SMatthew G. Knepley    Output Parameters:
388*01311c95SBarry Smith .  count - number of items of data actually read, or `NULL`
389*01311c95SBarry Smith 
390*01311c95SBarry Smith    Level: beginner
391f8e4bde8SMatthew G. Knepley 
392811af0c4SBarry Smith    Note:
393811af0c4SBarry Smith    If datatype is `PETSC_STRING` and num is negative, reads until a newline character is found,
394632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
395632e26b4SStefano Zampini 
396*01311c95SBarry Smith    Only certain viewers, such as `PETSCVIEWERBINARY` can be read from, see `PetscViewerReadable()`
3971d641e7bSMichael Lange 
398d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
399*01311c95SBarry Smith           `PetscViewerReadable()`, `PetscViewerBinaryGetDescriptor()`,
400db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
4011d641e7bSMichael Lange @*/
402d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
403d71ae5a4SJacob Faibussowitsch {
4041d641e7bSMichael Lange   PetscFunctionBegin;
4051d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4061d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
407060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4081d641e7bSMichael Lange     char    *s = (char *)data;
409632e26b4SStefano Zampini     if (num >= 0) {
410060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4111d641e7bSMichael Lange         /* Skip leading whitespaces */
4129371c9d4SSatish Balay         do {
4139371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR));
4149371c9d4SSatish Balay           if (!cnt) break;
4159371c9d4SSatish Balay         } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4161d641e7bSMichael Lange         i++;
4171d641e7bSMichael Lange         /* Read strings one char at a time */
4189371c9d4SSatish Balay         do {
4199371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4209371c9d4SSatish Balay           if (!cnt) break;
4219371c9d4SSatish 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');
4221d641e7bSMichael Lange         /* Terminate final string */
423060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4241d641e7bSMichael Lange       }
425632e26b4SStefano Zampini     } else {
426632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4279371c9d4SSatish Balay       do {
4289371c9d4SSatish Balay         PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4299371c9d4SSatish Balay         if (i == -num || !cnt) break;
4309371c9d4SSatish Balay       } while (s[i - 1] != '\n');
431632e26b4SStefano Zampini       /* Terminate final string */
432632e26b4SStefano Zampini       s[i - 1] = '\0';
433632e26b4SStefano Zampini       c        = i;
434632e26b4SStefano Zampini     }
435060da220SMatthew G. Knepley     if (count) *count = c;
43608401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
437dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4391d641e7bSMichael Lange }
440e24fdd67SVaclav Hapla 
441af684e28SVaclav Hapla /*@
442af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
443af684e28SVaclav Hapla 
444af684e28SVaclav Hapla    Not Collective
445af684e28SVaclav Hapla 
446af684e28SVaclav Hapla    Input Parameters:
447811af0c4SBarry Smith .  viewer - the `PetscViewer` context
448af684e28SVaclav Hapla 
449af684e28SVaclav Hapla    Output Parameters:
450811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
451af684e28SVaclav Hapla 
452*01311c95SBarry Smith    Level: intermediate
453*01311c95SBarry Smith 
454811af0c4SBarry Smith    Note:
455811af0c4SBarry Smith    `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading (this holds e.g. for `PETSCVIEWERBINARY`)
456811af0c4SBarry Smith    and viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
457811af0c4SBarry Smith    returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
458af684e28SVaclav Hapla 
459*01311c95SBarry Smith .seealso: [](sec_viewers), `PetscViewerRead()`, `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
460af684e28SVaclav Hapla @*/
461d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
462d71ae5a4SJacob Faibussowitsch {
463e24fdd67SVaclav Hapla   PetscFileMode mode;
464e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
465e24fdd67SVaclav Hapla 
466e24fdd67SVaclav Hapla   PetscFunctionBegin;
467e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
468534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
4699566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
470e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
4713ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
4729566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
473e24fdd67SVaclav Hapla   switch (mode) {
474e24fdd67SVaclav Hapla   case FILE_MODE_READ:
475e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
476d71ae5a4SJacob Faibussowitsch   case FILE_MODE_APPEND_UPDATE:
477d71ae5a4SJacob Faibussowitsch     *flg = PETSC_TRUE;
478d71ae5a4SJacob Faibussowitsch   default:
479d71ae5a4SJacob Faibussowitsch     break;
480e24fdd67SVaclav Hapla   }
4813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
482e24fdd67SVaclav Hapla }
483e24fdd67SVaclav Hapla 
484af684e28SVaclav Hapla /*@
485af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
486af684e28SVaclav Hapla 
487af684e28SVaclav Hapla    Not Collective
488af684e28SVaclav Hapla 
489af684e28SVaclav Hapla    Input Parameters:
490811af0c4SBarry Smith .  viewer - the `PetscViewer` context
491af684e28SVaclav Hapla 
492af684e28SVaclav Hapla    Output Parameters:
493811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
494af684e28SVaclav Hapla 
495*01311c95SBarry Smith    Level: intermediate
496*01311c95SBarry Smith 
497811af0c4SBarry Smith    Note:
498811af0c4SBarry Smith    `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
499811af0c4SBarry Smith    returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
500af684e28SVaclav Hapla 
501d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
502af684e28SVaclav Hapla @*/
503d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
504d71ae5a4SJacob Faibussowitsch {
505e24fdd67SVaclav Hapla   PetscFileMode mode;
506e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
507e24fdd67SVaclav Hapla 
508e24fdd67SVaclav Hapla   PetscFunctionBegin;
509e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
510534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
5119566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
512e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
5133ba16761SJacob Faibussowitsch   if (!f) PetscFunctionReturn(PETSC_SUCCESS);
5149566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
515e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
5163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517e24fdd67SVaclav Hapla }
518e24fdd67SVaclav Hapla 
519af684e28SVaclav Hapla /*@
520811af0c4SBarry Smith    PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
521af684e28SVaclav Hapla 
522af684e28SVaclav Hapla    Collective
523af684e28SVaclav Hapla 
524af684e28SVaclav Hapla    Input Parameters:
525811af0c4SBarry Smith .  viewer - the `PetscViewer` context
526af684e28SVaclav Hapla 
527af684e28SVaclav Hapla    Level: intermediate
528af684e28SVaclav Hapla 
529d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
530af684e28SVaclav Hapla @*/
531d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
532d71ae5a4SJacob Faibussowitsch {
533d01f05b1SVaclav Hapla   PetscBool flg;
534d01f05b1SVaclav Hapla 
535d01f05b1SVaclav Hapla   PetscFunctionBegin;
5360af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5379566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
53828b400f6SJacob 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)");
5393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
540d01f05b1SVaclav Hapla }
541d01f05b1SVaclav Hapla 
542af684e28SVaclav Hapla /*@
543811af0c4SBarry Smith    PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
544af684e28SVaclav Hapla 
545af684e28SVaclav Hapla    Collective
546af684e28SVaclav Hapla 
547af684e28SVaclav Hapla    Input Parameters:
548811af0c4SBarry Smith .  viewer - the `PetscViewer` context
549af684e28SVaclav Hapla 
550af684e28SVaclav Hapla    Level: intermediate
551af684e28SVaclav Hapla 
552d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
553af684e28SVaclav Hapla @*/
554d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
555d71ae5a4SJacob Faibussowitsch {
556d01f05b1SVaclav Hapla   PetscBool flg;
557d01f05b1SVaclav Hapla 
558d01f05b1SVaclav Hapla   PetscFunctionBegin;
5590af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5609566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
56128b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
5623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
563d01f05b1SVaclav Hapla }
564