xref: /petsc/src/sys/classes/viewer/interface/view.c (revision c3339decea92175325d9368fa13196bcd0e0e58b)
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 
14db781477SPatrick Sanan .seealso: `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;
335c6c1daeSBarry Smith   PetscFunctionReturn(0);
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 
41db781477SPatrick Sanan .seealso: `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;
495c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
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));
735c6c1daeSBarry Smith   PetscFunctionReturn(0);
745c6c1daeSBarry Smith }
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith /*@
77811af0c4SBarry Smith    PetscViewerDestroy - Destroys a `PetscViewer`.
785c6c1daeSBarry Smith 
79*c3339decSBarry Smith    Collective
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith    Input Parameters:
82811af0c4SBarry Smith .  viewer - the `PetscViewer` to be destroyed.
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith    Level: beginner
855c6c1daeSBarry Smith 
86811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
875c6c1daeSBarry Smith @*/
88d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDestroy(PetscViewer *viewer)
89d71ae5a4SJacob Faibussowitsch {
905c6c1daeSBarry Smith   PetscFunctionBegin;
915c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
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;
979371c9d4SSatish Balay     PetscFunctionReturn(0);
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));
1035c6c1daeSBarry Smith   PetscFunctionReturn(0);
1045c6c1daeSBarry Smith }
1055c6c1daeSBarry Smith 
106d7cbc13eSBarry Smith /*@C
107811af0c4SBarry Smith    PetscViewerAndFormatCreate - Creates a `PetscViewerAndFormat` struct.
108d7cbc13eSBarry Smith 
109*c3339decSBarry 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 
125db781477SPatrick Sanan .seealso: `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;
136d7cbc13eSBarry Smith   PetscFunctionReturn(0);
137d7cbc13eSBarry Smith }
138d7cbc13eSBarry Smith 
139fe01d993SBarry Smith /*@C
140811af0c4SBarry Smith    PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct.
141fe01d993SBarry Smith 
142*c3339decSBarry Smith    Collective
143fe01d993SBarry Smith 
144fe01d993SBarry Smith    Input Parameters:
145811af0c4SBarry Smith .  vf - the `PetscViewerAndFormat` to be destroyed.
146fe01d993SBarry Smith 
147d7cbc13eSBarry Smith    Level: developer
148fe01d993SBarry Smith 
149db781477SPatrick Sanan .seealso: `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));
157fe01d993SBarry Smith   PetscFunctionReturn(0);
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:
1695c6c1daeSBarry Smith .  type - PetscViewer type (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 
185811af0c4SBarry Smith .seealso: `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;
1935c6c1daeSBarry Smith   PetscFunctionReturn(0);
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 
200*c3339decSBarry 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 
212811af0c4SBarry Smith .seealso: `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));
2195c6c1daeSBarry Smith   PetscFunctionReturn(0);
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 
226*c3339decSBarry Smith    Logically Collective
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith    Input Parameters:
2295c6c1daeSBarry Smith +  viewer - the PetscViewer context
2305c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2315c6c1daeSBarry Smith 
232811af0c4SBarry Smith    Note:
2335c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2345c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2355c6c1daeSBarry Smith 
2365c6c1daeSBarry Smith    Level: advanced
2375c6c1daeSBarry Smith 
238811af0c4SBarry Smith .seealso: `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));
2455c6c1daeSBarry Smith   PetscFunctionReturn(0);
2465c6c1daeSBarry Smith }
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith /*@C
2495c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2505c6c1daeSBarry 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 
260811af0c4SBarry Smith    Fortran Note:
261811af0c4SBarry Smith    The user should pass in a string 'prefix' of sufficient length to hold the prefix.
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith    Level: advanced
2645c6c1daeSBarry Smith 
265811af0c4SBarry Smith .seealso: `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));
2725c6c1daeSBarry Smith   PetscFunctionReturn(0);
2735c6c1daeSBarry Smith }
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith /*@
2765c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2775c6c1daeSBarry Smith 
278*c3339decSBarry Smith    Collective
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith    Input Parameters:
281811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2825c6c1daeSBarry Smith 
283811af0c4SBarry Smith    Note:
284811af0c4SBarry Smith    For basic use of the `PetscViewer` classes the user need not explicitly call
285811af0c4SBarry Smith    `PetscViewerSetUp()`, since these actions will happen automatically.
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith    Level: advanced
2885c6c1daeSBarry Smith 
289811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
2905c6c1daeSBarry Smith @*/
291d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
292d71ae5a4SJacob Faibussowitsch {
2935c6c1daeSBarry Smith   PetscFunctionBegin;
2945c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
295c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
296dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
297c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
2985c6c1daeSBarry Smith   PetscFunctionReturn(0);
2995c6c1daeSBarry Smith }
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith /*@C
302811af0c4SBarry Smith    PetscViewerViewFromOptions - View from the viewer based on the options database values
303fe2efc57SMark 
304*c3339decSBarry 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 
313db781477SPatrick Sanan .seealso: `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));
320fe2efc57SMark   PetscFunctionReturn(0);
321fe2efc57SMark }
322fe2efc57SMark 
323fe2efc57SMark /*@C
3245c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3255c6c1daeSBarry Smith 
326*c3339decSBarry Smith    Collective
3275c6c1daeSBarry Smith 
3285c6c1daeSBarry Smith    Input Parameters:
329f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3305c6c1daeSBarry Smith -  viewer - visualization context
3315c6c1daeSBarry Smith 
332811af0c4SBarry Smith   Note:
3335c6c1daeSBarry Smith   The available visualization contexts include
334811af0c4SBarry Smith .vb
335811af0c4SBarry Smith   PETSC_VIEWER_STDOUT_SELF - standard output (default)
336811af0c4SBarry Smith   PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first rank opens the file. Other processors send their data to the first rank
337811af0c4SBarry Smith   PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
338811af0c4SBarry Smith .ve
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith    Level: beginner
3415c6c1daeSBarry Smith 
342811af0c4SBarry Smith .seealso: `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   }
3815c6c1daeSBarry Smith   PetscFunctionReturn(0);
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 
395f8e4bde8SMatthew G. Knepley    Output Parameters:
396060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
397f8e4bde8SMatthew G. Knepley 
398811af0c4SBarry Smith    Note:
399811af0c4SBarry Smith    If datatype is `PETSC_STRING` and num is negative, reads until a newline character is found,
400632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
401632e26b4SStefano Zampini 
4021d641e7bSMichael Lange    Level: beginner
4031d641e7bSMichael Lange 
404811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
405db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`,
406db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
4071d641e7bSMichael Lange @*/
408d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
409d71ae5a4SJacob Faibussowitsch {
4101d641e7bSMichael Lange   PetscFunctionBegin;
4111d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4121d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
413060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4141d641e7bSMichael Lange     char    *s = (char *)data;
415632e26b4SStefano Zampini     if (num >= 0) {
416060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4171d641e7bSMichael Lange         /* Skip leading whitespaces */
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] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4221d641e7bSMichael Lange         i++;
4231d641e7bSMichael Lange         /* Read strings one char at a time */
4249371c9d4SSatish Balay         do {
4259371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4269371c9d4SSatish Balay           if (!cnt) break;
4279371c9d4SSatish 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');
4281d641e7bSMichael Lange         /* Terminate final string */
429060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4301d641e7bSMichael Lange       }
431632e26b4SStefano Zampini     } else {
432632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4339371c9d4SSatish Balay       do {
4349371c9d4SSatish Balay         PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4359371c9d4SSatish Balay         if (i == -num || !cnt) break;
4369371c9d4SSatish Balay       } while (s[i - 1] != '\n');
437632e26b4SStefano Zampini       /* Terminate final string */
438632e26b4SStefano Zampini       s[i - 1] = '\0';
439632e26b4SStefano Zampini       c        = i;
440632e26b4SStefano Zampini     }
441060da220SMatthew G. Knepley     if (count) *count = c;
44208401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
443dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4441d641e7bSMichael Lange   PetscFunctionReturn(0);
4451d641e7bSMichael Lange }
446e24fdd67SVaclav Hapla 
447af684e28SVaclav Hapla /*@
448af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
449af684e28SVaclav Hapla 
450af684e28SVaclav Hapla    Not Collective
451af684e28SVaclav Hapla 
452af684e28SVaclav Hapla    Input Parameters:
453811af0c4SBarry Smith .  viewer - the `PetscViewer` context
454af684e28SVaclav Hapla 
455af684e28SVaclav Hapla    Output Parameters:
456811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
457af684e28SVaclav Hapla 
458811af0c4SBarry Smith    Note:
459811af0c4SBarry Smith    `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading (this holds e.g. for `PETSCVIEWERBINARY`)
460811af0c4SBarry Smith    and viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
461811af0c4SBarry Smith    returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
462af684e28SVaclav Hapla 
463af684e28SVaclav Hapla    Level: intermediate
464af684e28SVaclav Hapla 
465811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
466af684e28SVaclav Hapla @*/
467d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
468d71ae5a4SJacob Faibussowitsch {
469e24fdd67SVaclav Hapla   PetscFileMode mode;
470e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
471e24fdd67SVaclav Hapla 
472e24fdd67SVaclav Hapla   PetscFunctionBegin;
473e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
474534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
4759566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
476e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
477e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
4789566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
479e24fdd67SVaclav Hapla   switch (mode) {
480e24fdd67SVaclav Hapla   case FILE_MODE_READ:
481e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
482d71ae5a4SJacob Faibussowitsch   case FILE_MODE_APPEND_UPDATE:
483d71ae5a4SJacob Faibussowitsch     *flg = PETSC_TRUE;
484d71ae5a4SJacob Faibussowitsch   default:
485d71ae5a4SJacob Faibussowitsch     break;
486e24fdd67SVaclav Hapla   }
487e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
488e24fdd67SVaclav Hapla }
489e24fdd67SVaclav Hapla 
490af684e28SVaclav Hapla /*@
491af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
492af684e28SVaclav Hapla 
493af684e28SVaclav Hapla    Not Collective
494af684e28SVaclav Hapla 
495af684e28SVaclav Hapla    Input Parameters:
496811af0c4SBarry Smith .  viewer - the `PetscViewer` context
497af684e28SVaclav Hapla 
498af684e28SVaclav Hapla    Output Parameters:
499811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
500af684e28SVaclav Hapla 
501811af0c4SBarry Smith    Note:
502811af0c4SBarry Smith    `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
503811af0c4SBarry Smith    returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
504af684e28SVaclav Hapla 
505af684e28SVaclav Hapla    Level: intermediate
506af684e28SVaclav Hapla 
507811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
508af684e28SVaclav Hapla @*/
509d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
510d71ae5a4SJacob Faibussowitsch {
511e24fdd67SVaclav Hapla   PetscFileMode mode;
512e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
513e24fdd67SVaclav Hapla 
514e24fdd67SVaclav Hapla   PetscFunctionBegin;
515e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
516534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
5179566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
518e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
519e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
5209566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
521e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
522e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
523e24fdd67SVaclav Hapla }
524e24fdd67SVaclav Hapla 
525af684e28SVaclav Hapla /*@
526811af0c4SBarry Smith    PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
527af684e28SVaclav Hapla 
528af684e28SVaclav Hapla    Collective
529af684e28SVaclav Hapla 
530af684e28SVaclav Hapla    Input Parameters:
531811af0c4SBarry Smith .  viewer - the `PetscViewer` context
532af684e28SVaclav Hapla 
533af684e28SVaclav Hapla    Level: intermediate
534af684e28SVaclav Hapla 
535811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
536af684e28SVaclav Hapla @*/
537d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
538d71ae5a4SJacob Faibussowitsch {
539d01f05b1SVaclav Hapla   PetscBool flg;
540d01f05b1SVaclav Hapla 
541d01f05b1SVaclav Hapla   PetscFunctionBegin;
5420af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5439566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
54428b400f6SJacob 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)");
545d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
546d01f05b1SVaclav Hapla }
547d01f05b1SVaclav Hapla 
548af684e28SVaclav Hapla /*@
549811af0c4SBarry Smith    PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
550af684e28SVaclav Hapla 
551af684e28SVaclav Hapla    Collective
552af684e28SVaclav Hapla 
553af684e28SVaclav Hapla    Input Parameters:
554811af0c4SBarry Smith .  viewer - the `PetscViewer` context
555af684e28SVaclav Hapla 
556af684e28SVaclav Hapla    Level: intermediate
557af684e28SVaclav Hapla 
558811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
559af684e28SVaclav Hapla @*/
560d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
561d71ae5a4SJacob Faibussowitsch {
562d01f05b1SVaclav Hapla   PetscBool flg;
563d01f05b1SVaclav Hapla 
564d01f05b1SVaclav Hapla   PetscFunctionBegin;
5650af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5669566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
56728b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
568d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
569d01f05b1SVaclav Hapla }
570