xref: /petsc/src/sys/classes/viewer/interface/view.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
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
105c6c1daeSBarry Smith   called from PetscFinalize().
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith   Level: developer
135c6c1daeSBarry Smith 
14db781477SPatrick Sanan .seealso: `PetscFinalize()`
155c6c1daeSBarry Smith @*/
169371c9d4SSatish Balay PetscErrorCode   PetscViewerFinalizePackage(void) {
175c6c1daeSBarry Smith     PetscFunctionBegin;
18*48a46eb9SPierre Jolivet     if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_keyval));
19*48a46eb9SPierre Jolivet   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval));
20*48a46eb9SPierre Jolivet   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval));
21*48a46eb9SPierre Jolivet   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval));
22*48a46eb9SPierre Jolivet   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval));
23b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5)
24*48a46eb9SPierre Jolivet   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval));
25b6dade52SBarry Smith #endif
26b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER)
27*48a46eb9SPierre Jolivet   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval));
28b6dade52SBarry Smith #endif
299566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PetscViewerList));
305c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_FALSE;
310f51fdf8SToby Isaac   PetscViewerRegisterAllCalled  = PETSC_FALSE;
325c6c1daeSBarry Smith   PetscFunctionReturn(0);
335c6c1daeSBarry Smith }
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith /*@C
365c6c1daeSBarry Smith   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith   Level: developer
395c6c1daeSBarry Smith 
40db781477SPatrick Sanan .seealso: `PetscInitialize()`
415c6c1daeSBarry Smith @*/
429371c9d4SSatish Balay PetscErrorCode PetscViewerInitializePackage(void) {
435c6c1daeSBarry Smith   char      logList[256];
448e81d068SLisandro Dalcin   PetscBool opt, pkg;
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith   PetscFunctionBegin;
475c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
485c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
495c6c1daeSBarry Smith   /* Register Classes */
509566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("Viewer", &PETSC_VIEWER_CLASSID));
515c6c1daeSBarry Smith   /* Register Constructors */
529566063dSJacob Faibussowitsch   PetscCall(PetscViewerRegisterAll());
53e94e781bSJacob Faibussowitsch   /* Process Info */
54e94e781bSJacob Faibussowitsch   {
55e94e781bSJacob Faibussowitsch     PetscClassId classids[1];
56e94e781bSJacob Faibussowitsch 
57e94e781bSJacob Faibussowitsch     classids[0] = PETSC_VIEWER_CLASSID;
589566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("viewer", 1, classids));
595c6c1daeSBarry Smith   }
605c6c1daeSBarry Smith   /* Process summary exclusions */
619566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
625c6c1daeSBarry Smith   if (opt) {
639566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("viewer", logList, ',', &pkg));
649566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID));
655c6c1daeSBarry Smith   }
665c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
679566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaInitializePackage());
685c6c1daeSBarry Smith #endif
698e81d068SLisandro Dalcin   /* Register package finalizer */
709566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PetscViewerFinalizePackage));
715c6c1daeSBarry Smith   PetscFunctionReturn(0);
725c6c1daeSBarry Smith }
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith /*@
755c6c1daeSBarry Smith    PetscViewerDestroy - Destroys a PetscViewer.
765c6c1daeSBarry Smith 
775c6c1daeSBarry Smith    Collective on PetscViewer
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith    Input Parameters:
805c6c1daeSBarry Smith .  viewer - the PetscViewer to be destroyed.
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith    Level: beginner
835c6c1daeSBarry Smith 
84db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith @*/
879371c9d4SSatish Balay PetscErrorCode PetscViewerDestroy(PetscViewer *viewer) {
885c6c1daeSBarry Smith   PetscFunctionBegin;
895c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
905c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer, PETSC_VIEWER_CLASSID, 1);
915c6c1daeSBarry Smith 
929566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(*viewer));
939371c9d4SSatish Balay   if (--((PetscObject)(*viewer))->refct > 0) {
949371c9d4SSatish Balay     *viewer = NULL;
959371c9d4SSatish Balay     PetscFunctionReturn(0);
969371c9d4SSatish Balay   }
975c6c1daeSBarry Smith 
989566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
99*48a46eb9SPierre Jolivet   if ((*viewer)->ops->destroy) PetscCall((*(*viewer)->ops->destroy)(*viewer));
1009566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(viewer));
1015c6c1daeSBarry Smith   PetscFunctionReturn(0);
1025c6c1daeSBarry Smith }
1035c6c1daeSBarry Smith 
104d7cbc13eSBarry Smith /*@C
105d7cbc13eSBarry Smith    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
106d7cbc13eSBarry Smith 
107d7cbc13eSBarry Smith    Collective on PetscViewer
108d7cbc13eSBarry Smith 
109d7cbc13eSBarry Smith    Input Parameters:
110d7cbc13eSBarry Smith +  viewer - the viewer
111d7cbc13eSBarry Smith -  format - the format
112d7cbc13eSBarry Smith 
113d7cbc13eSBarry Smith    Output Parameter:
114d7cbc13eSBarry Smith .   vf - viewer and format object
115d7cbc13eSBarry Smith 
11695452b02SPatrick Sanan    Notes:
11795452b02SPatrick Sanan     This increases the reference count of the viewer so you can destroy the viewer object after this call
118d7cbc13eSBarry Smith    Level: developer
119d7cbc13eSBarry Smith 
120d7cbc13eSBarry Smith    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
121d7cbc13eSBarry Smith 
122db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
123d7cbc13eSBarry Smith 
124d7cbc13eSBarry Smith @*/
1259371c9d4SSatish Balay PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf) {
126d7cbc13eSBarry Smith   PetscFunctionBegin;
1279566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)viewer));
1289566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
129d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
130d7cbc13eSBarry Smith   (*vf)->format = format;
131798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
132798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
133d7cbc13eSBarry Smith   PetscFunctionReturn(0);
134d7cbc13eSBarry Smith }
135d7cbc13eSBarry Smith 
136fe01d993SBarry Smith /*@C
137fe01d993SBarry Smith    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
138fe01d993SBarry Smith 
139fe01d993SBarry Smith    Collective on PetscViewer
140fe01d993SBarry Smith 
141fe01d993SBarry Smith    Input Parameters:
142798534f6SMatthew G. Knepley .  vf - the PetscViewerAndFormat to be destroyed.
143fe01d993SBarry Smith 
144d7cbc13eSBarry Smith    Level: developer
145fe01d993SBarry Smith 
146db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatCreate()`
147fe01d993SBarry Smith @*/
1489371c9d4SSatish Balay PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf) {
149fe01d993SBarry Smith   PetscFunctionBegin;
1509566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
1519566063dSJacob Faibussowitsch   PetscCall(PetscDrawLGDestroy(&(*vf)->lg));
1529566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
153fe01d993SBarry Smith   PetscFunctionReturn(0);
154fe01d993SBarry Smith }
155fe01d993SBarry Smith 
1565c6c1daeSBarry Smith /*@C
1575c6c1daeSBarry Smith    PetscViewerGetType - Returns the type of a PetscViewer.
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith    Not Collective
1605c6c1daeSBarry Smith 
1615c6c1daeSBarry Smith    Input Parameter:
1625c6c1daeSBarry Smith .   viewer - the PetscViewer
1635c6c1daeSBarry Smith 
1645c6c1daeSBarry Smith    Output Parameter:
1655c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith    Available Types Include:
168a2b725a8SWilliam Gropp +  PETSCVIEWERSOCKET - Socket PetscViewer
1695c6c1daeSBarry Smith .  PETSCVIEWERASCII - ASCII PetscViewer
1705c6c1daeSBarry Smith .  PETSCVIEWERBINARY - binary file PetscViewer
1715c6c1daeSBarry Smith .  PETSCVIEWERSTRING - string PetscViewer
172a2b725a8SWilliam Gropp -  PETSCVIEWERDRAW - drawing PetscViewer
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith    Level: intermediate
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith    Note:
1775c6c1daeSBarry Smith    See include/petscviewer.h for a complete list of PetscViewers.
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith    PetscViewerType is actually a string
1805c6c1daeSBarry Smith 
181db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith @*/
1849371c9d4SSatish Balay PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type) {
1855c6c1daeSBarry Smith   PetscFunctionBegin;
1865c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1875c6c1daeSBarry Smith   PetscValidPointer(type, 2);
1885c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1895c6c1daeSBarry Smith   PetscFunctionReturn(0);
1905c6c1daeSBarry Smith }
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith /*@C
1935c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
1945c6c1daeSBarry Smith    PetscViewer options in the database.
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith    Logically Collective on PetscViewer
1975c6c1daeSBarry Smith 
198d8d19677SJose E. Roman    Input Parameters:
1995c6c1daeSBarry Smith +  viewer - the PetscViewer context
2005c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith    Notes:
2035c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2045c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith    Level: advanced
2075c6c1daeSBarry Smith 
208db781477SPatrick Sanan .seealso: `PetscViewerSetFromOptions()`
2095c6c1daeSBarry Smith @*/
2109371c9d4SSatish Balay PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[]) {
2115c6c1daeSBarry Smith   PetscFunctionBegin;
2125c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2139566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
2145c6c1daeSBarry Smith   PetscFunctionReturn(0);
2155c6c1daeSBarry Smith }
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith /*@C
2185c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
2195c6c1daeSBarry Smith    PetscViewer options in the database.
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith    Logically Collective on PetscViewer
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith    Input Parameters:
2245c6c1daeSBarry Smith +  viewer - the PetscViewer context
2255c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith    Notes:
2285c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2295c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith    Level: advanced
2325c6c1daeSBarry Smith 
233db781477SPatrick Sanan .seealso: `PetscViewerGetOptionsPrefix()`
2345c6c1daeSBarry Smith @*/
2359371c9d4SSatish Balay PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[]) {
2365c6c1daeSBarry Smith   PetscFunctionBegin;
2375c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2389566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
2395c6c1daeSBarry Smith   PetscFunctionReturn(0);
2405c6c1daeSBarry Smith }
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith /*@C
2435c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2445c6c1daeSBarry Smith    PetscViewer options in the database.
2455c6c1daeSBarry Smith 
2465c6c1daeSBarry Smith    Not Collective
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith    Input Parameter:
2495c6c1daeSBarry Smith .  viewer - the PetscViewer context
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith    Output Parameter:
2525c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2535c6c1daeSBarry Smith 
25495452b02SPatrick Sanan    Notes:
25595452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
2565c6c1daeSBarry Smith    sufficient length to hold the prefix.
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith    Level: advanced
2595c6c1daeSBarry Smith 
260db781477SPatrick Sanan .seealso: `PetscViewerAppendOptionsPrefix()`
2615c6c1daeSBarry Smith @*/
2629371c9d4SSatish Balay PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[]) {
2635c6c1daeSBarry Smith   PetscFunctionBegin;
2645c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2659566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
2665c6c1daeSBarry Smith   PetscFunctionReturn(0);
2675c6c1daeSBarry Smith }
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith /*@
2705c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith    Collective on PetscViewer
2735c6c1daeSBarry Smith 
2745c6c1daeSBarry Smith    Input Parameters:
2755c6c1daeSBarry Smith .  viewer - the PetscViewer context
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith    Notes:
2785c6c1daeSBarry Smith    For basic use of the PetscViewer classes the user need not explicitly call
2795c6c1daeSBarry Smith    PetscViewerSetUp(), since these actions will happen automatically.
2805c6c1daeSBarry Smith 
2815c6c1daeSBarry Smith    Level: advanced
2825c6c1daeSBarry Smith 
283db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerDestroy()`
2845c6c1daeSBarry Smith @*/
2859371c9d4SSatish Balay PetscErrorCode PetscViewerSetUp(PetscViewer viewer) {
2865c6c1daeSBarry Smith   PetscFunctionBegin;
2875c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
288c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
289dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
290c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
2915c6c1daeSBarry Smith   PetscFunctionReturn(0);
2925c6c1daeSBarry Smith }
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith /*@C
295fe2efc57SMark    PetscViewerViewFromOptions - View from Options
296fe2efc57SMark 
297fe2efc57SMark    Collective on PetscViewer
298fe2efc57SMark 
299fe2efc57SMark    Input Parameters:
300fe2efc57SMark +  A - the PetscViewer context
301736c3998SJose E. Roman .  obj - Optional object
302736c3998SJose E. Roman -  name - command line option
303fe2efc57SMark 
304fe2efc57SMark    Level: intermediate
305db781477SPatrick Sanan .seealso: `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
306fe2efc57SMark @*/
3079371c9d4SSatish Balay PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[]) {
308fe2efc57SMark   PetscFunctionBegin;
309fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_VIEWER_CLASSID, 1);
3109566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
311fe2efc57SMark   PetscFunctionReturn(0);
312fe2efc57SMark }
313fe2efc57SMark 
314fe2efc57SMark /*@C
3155c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith    Collective on PetscViewer
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith    Input Parameters:
320f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3215c6c1daeSBarry Smith -  viewer - visualization context
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith   Notes:
3245c6c1daeSBarry Smith   The available visualization contexts include
3255c6c1daeSBarry Smith +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
3265c6c1daeSBarry Smith .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
3275c6c1daeSBarry Smith         output where only the first processor opens
3285c6c1daeSBarry Smith         the file.  All other processors send their
3295c6c1daeSBarry Smith         data to the first processor to print.
3305c6c1daeSBarry Smith -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith    Level: beginner
3335c6c1daeSBarry Smith 
334db781477SPatrick Sanan .seealso: `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
335db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3365c6c1daeSBarry Smith @*/
3379371c9d4SSatish Balay PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer) {
3385c6c1daeSBarry Smith   PetscBool         iascii;
3395c6c1daeSBarry Smith   PetscViewerFormat format;
340e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
341536b137fSBarry Smith   PetscBool issaws;
3420076e027SBarry Smith #endif
3435c6c1daeSBarry Smith 
3445c6c1daeSBarry Smith   PetscFunctionBegin;
3455c6c1daeSBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
3465c6c1daeSBarry Smith   PetscValidType(v, 1);
347*48a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
3485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3495c6c1daeSBarry Smith   PetscCheckSameComm(v, 1, viewer, 2);
3505c6c1daeSBarry Smith 
3519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
352e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3539566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
3540076e027SBarry Smith #endif
3555c6c1daeSBarry Smith   if (iascii) {
3569566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
3579566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
35898c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
359*48a46eb9SPierre Jolivet       if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, "  Viewer format = %s\n", PetscViewerFormats[v->format]));
3609566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
361dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3629566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3635c6c1daeSBarry Smith     }
364e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
365536b137fSBarry Smith   } else if (issaws) {
3660076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3679566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
368dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3690076e027SBarry Smith     }
3700076e027SBarry Smith #endif
3715c6c1daeSBarry Smith   }
3725c6c1daeSBarry Smith   PetscFunctionReturn(0);
3735c6c1daeSBarry Smith }
3741d641e7bSMichael Lange 
3751d641e7bSMichael Lange /*@C
3761d641e7bSMichael Lange    PetscViewerRead - Reads data from a PetscViewer
3771d641e7bSMichael Lange 
378d083f849SBarry Smith    Collective
3791d641e7bSMichael Lange 
3801d641e7bSMichael Lange    Input Parameters:
3811d641e7bSMichael Lange +  viewer   - The viewer
3821d641e7bSMichael Lange .  data     - Location to write the data
383060da220SMatthew G. Knepley .  num      - Number of items of data to read
3841d641e7bSMichael Lange -  datatype - Type of data to read
3851d641e7bSMichael Lange 
386f8e4bde8SMatthew G. Knepley    Output Parameters:
387060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
388f8e4bde8SMatthew G. Knepley 
389632e26b4SStefano Zampini    Notes:
390632e26b4SStefano Zampini    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
391632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
392632e26b4SStefano Zampini 
3931d641e7bSMichael Lange    Level: beginner
3941d641e7bSMichael Lange 
395db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
396db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`,
397db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
3981d641e7bSMichael Lange @*/
3999371c9d4SSatish Balay PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype) {
4001d641e7bSMichael Lange   PetscFunctionBegin;
4011d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4021d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
403060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4041d641e7bSMichael Lange     char    *s = (char *)data;
405632e26b4SStefano Zampini     if (num >= 0) {
406060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4071d641e7bSMichael Lange         /* Skip leading whitespaces */
4089371c9d4SSatish Balay         do {
4099371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR));
4109371c9d4SSatish Balay           if (!cnt) break;
4119371c9d4SSatish Balay         } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4121d641e7bSMichael Lange         i++;
4131d641e7bSMichael Lange         /* Read strings one char at a time */
4149371c9d4SSatish Balay         do {
4159371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4169371c9d4SSatish Balay           if (!cnt) break;
4179371c9d4SSatish 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');
4181d641e7bSMichael Lange         /* Terminate final string */
419060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4201d641e7bSMichael Lange       }
421632e26b4SStefano Zampini     } else {
422632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4239371c9d4SSatish Balay       do {
4249371c9d4SSatish Balay         PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4259371c9d4SSatish Balay         if (i == -num || !cnt) break;
4269371c9d4SSatish Balay       } while (s[i - 1] != '\n');
427632e26b4SStefano Zampini       /* Terminate final string */
428632e26b4SStefano Zampini       s[i - 1] = '\0';
429632e26b4SStefano Zampini       c        = i;
430632e26b4SStefano Zampini     }
431060da220SMatthew G. Knepley     if (count) *count = c;
43208401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
433dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4341d641e7bSMichael Lange   PetscFunctionReturn(0);
4351d641e7bSMichael Lange }
436e24fdd67SVaclav Hapla 
437af684e28SVaclav Hapla /*@
438af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
439af684e28SVaclav Hapla 
440af684e28SVaclav Hapla    Not Collective
441af684e28SVaclav Hapla 
442af684e28SVaclav Hapla    Input Parameters:
443af684e28SVaclav Hapla .  viewer - the PetscViewer context
444af684e28SVaclav Hapla 
445af684e28SVaclav Hapla    Output Parameters:
446af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise
447af684e28SVaclav Hapla 
448af684e28SVaclav Hapla    Notes:
449af684e28SVaclav Hapla    PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
450af684e28SVaclav Hapla    and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
451af684e28SVaclav Hapla    returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
452af684e28SVaclav Hapla 
453af684e28SVaclav Hapla    Level: intermediate
454af684e28SVaclav Hapla 
455db781477SPatrick Sanan .seealso: `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
456af684e28SVaclav Hapla @*/
4579371c9d4SSatish Balay PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg) {
458e24fdd67SVaclav Hapla   PetscFileMode mode;
459e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
460e24fdd67SVaclav Hapla 
461e24fdd67SVaclav Hapla   PetscFunctionBegin;
462e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
463534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
4649566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
465e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
466e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
4679566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
468e24fdd67SVaclav Hapla   switch (mode) {
469e24fdd67SVaclav Hapla   case FILE_MODE_READ:
470e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
4719371c9d4SSatish Balay   case FILE_MODE_APPEND_UPDATE: *flg = PETSC_TRUE;
472e24fdd67SVaclav Hapla   default: break;
473e24fdd67SVaclav Hapla   }
474e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
475e24fdd67SVaclav Hapla }
476e24fdd67SVaclav Hapla 
477af684e28SVaclav Hapla /*@
478af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
479af684e28SVaclav Hapla 
480af684e28SVaclav Hapla    Not Collective
481af684e28SVaclav Hapla 
482af684e28SVaclav Hapla    Input Parameters:
483af684e28SVaclav Hapla .  viewer - the PetscViewer context
484af684e28SVaclav Hapla 
485af684e28SVaclav Hapla    Output Parameters:
486af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise
487af684e28SVaclav Hapla 
488af684e28SVaclav Hapla    Notes:
489af684e28SVaclav Hapla    PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
490af684e28SVaclav Hapla    returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
491af684e28SVaclav Hapla 
492af684e28SVaclav Hapla    Level: intermediate
493af684e28SVaclav Hapla 
494db781477SPatrick Sanan .seealso: `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
495af684e28SVaclav Hapla @*/
4969371c9d4SSatish Balay PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg) {
497e24fdd67SVaclav Hapla   PetscFileMode mode;
498e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
499e24fdd67SVaclav Hapla 
500e24fdd67SVaclav Hapla   PetscFunctionBegin;
501e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
502534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
5039566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
504e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
505e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
5069566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
507e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
508e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
509e24fdd67SVaclav Hapla }
510e24fdd67SVaclav Hapla 
511af684e28SVaclav Hapla /*@
512af684e28SVaclav Hapla    PetscViewerCheckReadable - Check whether the viewer can be read from
513af684e28SVaclav Hapla 
514af684e28SVaclav Hapla    Collective
515af684e28SVaclav Hapla 
516af684e28SVaclav Hapla    Input Parameters:
517af684e28SVaclav Hapla .  viewer - the PetscViewer context
518af684e28SVaclav Hapla 
519af684e28SVaclav Hapla    Level: intermediate
520af684e28SVaclav Hapla 
521db781477SPatrick Sanan .seealso: `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
522af684e28SVaclav Hapla @*/
5239371c9d4SSatish Balay PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer) {
524d01f05b1SVaclav Hapla   PetscBool flg;
525d01f05b1SVaclav Hapla 
526d01f05b1SVaclav Hapla   PetscFunctionBegin;
5270af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5289566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
52928b400f6SJacob 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)");
530d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
531d01f05b1SVaclav Hapla }
532d01f05b1SVaclav Hapla 
533af684e28SVaclav Hapla /*@
534af684e28SVaclav Hapla    PetscViewerCheckWritable - Check whether the viewer can be written to
535af684e28SVaclav Hapla 
536af684e28SVaclav Hapla    Collective
537af684e28SVaclav Hapla 
538af684e28SVaclav Hapla    Input Parameters:
539af684e28SVaclav Hapla .  viewer - the PetscViewer context
540af684e28SVaclav Hapla 
541af684e28SVaclav Hapla    Level: intermediate
542af684e28SVaclav Hapla 
543db781477SPatrick Sanan .seealso: `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
544af684e28SVaclav Hapla @*/
5459371c9d4SSatish Balay PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer) {
546d01f05b1SVaclav Hapla   PetscBool flg;
547d01f05b1SVaclav Hapla 
548d01f05b1SVaclav Hapla   PetscFunctionBegin;
5490af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5509566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
55128b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
552d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
553d01f05b1SVaclav Hapla }
554