xref: /petsc/src/sys/classes/viewer/interface/view.c (revision dbbe0bcd3f3a8fbab5a45420dc06f8387e5764c6)
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 @*/
165c6c1daeSBarry Smith PetscErrorCode  PetscViewerFinalizePackage(void)
175c6c1daeSBarry Smith {
185c6c1daeSBarry Smith   PetscFunctionBegin;
19b6dade52SBarry Smith   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
209566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_keyval));
21b6dade52SBarry Smith   }
22b6dade52SBarry Smith   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
239566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval));
24b6dade52SBarry Smith   }
25b6dade52SBarry Smith   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
269566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval));
27b6dade52SBarry Smith   }
28b6dade52SBarry Smith   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
299566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval));
30b6dade52SBarry Smith   }
31b6dade52SBarry Smith   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
329566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval));
33b6dade52SBarry Smith   }
34b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5)
35b6dade52SBarry Smith   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
369566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval));
37b6dade52SBarry Smith   }
38b6dade52SBarry Smith #endif
39b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER)
40b6dade52SBarry Smith   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
419566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval));
42b6dade52SBarry Smith   }
43b6dade52SBarry Smith #endif
449566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PetscViewerList));
455c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_FALSE;
460f51fdf8SToby Isaac   PetscViewerRegisterAllCalled  = PETSC_FALSE;
475c6c1daeSBarry Smith   PetscFunctionReturn(0);
485c6c1daeSBarry Smith }
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith /*@C
515c6c1daeSBarry Smith   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith   Level: developer
545c6c1daeSBarry Smith 
55db781477SPatrick Sanan .seealso: `PetscInitialize()`
565c6c1daeSBarry Smith @*/
57607a6623SBarry Smith PetscErrorCode  PetscViewerInitializePackage(void)
585c6c1daeSBarry Smith {
595c6c1daeSBarry Smith   char           logList[256];
608e81d068SLisandro Dalcin   PetscBool      opt,pkg;
615c6c1daeSBarry Smith 
625c6c1daeSBarry Smith   PetscFunctionBegin;
635c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
645c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
655c6c1daeSBarry Smith   /* Register Classes */
669566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID));
675c6c1daeSBarry Smith   /* Register Constructors */
689566063dSJacob Faibussowitsch   PetscCall(PetscViewerRegisterAll());
69e94e781bSJacob Faibussowitsch   /* Process Info */
70e94e781bSJacob Faibussowitsch   {
71e94e781bSJacob Faibussowitsch     PetscClassId  classids[1];
72e94e781bSJacob Faibussowitsch 
73e94e781bSJacob Faibussowitsch     classids[0] = PETSC_VIEWER_CLASSID;
749566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("viewer", 1, classids));
755c6c1daeSBarry Smith   }
765c6c1daeSBarry Smith   /* Process summary exclusions */
779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt));
785c6c1daeSBarry Smith   if (opt) {
799566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("viewer",logList,',',&pkg));
809566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID));
815c6c1daeSBarry Smith   }
825c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
839566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaInitializePackage());
845c6c1daeSBarry Smith #endif
858e81d068SLisandro Dalcin   /* Register package finalizer */
869566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PetscViewerFinalizePackage));
875c6c1daeSBarry Smith   PetscFunctionReturn(0);
885c6c1daeSBarry Smith }
895c6c1daeSBarry Smith 
905c6c1daeSBarry Smith /*@
915c6c1daeSBarry Smith    PetscViewerDestroy - Destroys a PetscViewer.
925c6c1daeSBarry Smith 
935c6c1daeSBarry Smith    Collective on PetscViewer
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith    Input Parameters:
965c6c1daeSBarry Smith .  viewer - the PetscViewer to be destroyed.
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith    Level: beginner
995c6c1daeSBarry Smith 
100db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith @*/
1035c6c1daeSBarry Smith PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
1045c6c1daeSBarry Smith {
1055c6c1daeSBarry Smith   PetscFunctionBegin;
1065c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
1075c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1);
1085c6c1daeSBarry Smith 
1099566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(*viewer));
11002c9f0b5SLisandro Dalcin   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = NULL; PetscFunctionReturn(0);}
1115c6c1daeSBarry Smith 
1129566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
1135c6c1daeSBarry Smith   if ((*viewer)->ops->destroy) {
1149566063dSJacob Faibussowitsch     PetscCall((*(*viewer)->ops->destroy)(*viewer));
1155c6c1daeSBarry Smith   }
1169566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(viewer));
1175c6c1daeSBarry Smith   PetscFunctionReturn(0);
1185c6c1daeSBarry Smith }
1195c6c1daeSBarry Smith 
120d7cbc13eSBarry Smith /*@C
121d7cbc13eSBarry Smith    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
122d7cbc13eSBarry Smith 
123d7cbc13eSBarry Smith    Collective on PetscViewer
124d7cbc13eSBarry Smith 
125d7cbc13eSBarry Smith    Input Parameters:
126d7cbc13eSBarry Smith +  viewer - the viewer
127d7cbc13eSBarry Smith -  format - the format
128d7cbc13eSBarry Smith 
129d7cbc13eSBarry Smith    Output Parameter:
130d7cbc13eSBarry Smith .   vf - viewer and format object
131d7cbc13eSBarry Smith 
13295452b02SPatrick Sanan    Notes:
13395452b02SPatrick Sanan     This increases the reference count of the viewer so you can destroy the viewer object after this call
134d7cbc13eSBarry Smith    Level: developer
135d7cbc13eSBarry Smith 
136d7cbc13eSBarry Smith    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
137d7cbc13eSBarry Smith 
138db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatDestroy()`
139d7cbc13eSBarry Smith 
140d7cbc13eSBarry Smith @*/
141d7cbc13eSBarry Smith PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
142d7cbc13eSBarry Smith {
143d7cbc13eSBarry Smith   PetscFunctionBegin;
1449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)viewer));
1459566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
146d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
147d7cbc13eSBarry Smith   (*vf)->format = format;
148798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
149798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
150d7cbc13eSBarry Smith   PetscFunctionReturn(0);
151d7cbc13eSBarry Smith }
152d7cbc13eSBarry Smith 
153fe01d993SBarry Smith /*@C
154fe01d993SBarry Smith    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
155fe01d993SBarry Smith 
156fe01d993SBarry Smith    Collective on PetscViewer
157fe01d993SBarry Smith 
158fe01d993SBarry Smith    Input Parameters:
159798534f6SMatthew G. Knepley .  vf - the PetscViewerAndFormat to be destroyed.
160fe01d993SBarry Smith 
161d7cbc13eSBarry Smith    Level: developer
162fe01d993SBarry Smith 
163db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatCreate()`
164fe01d993SBarry Smith @*/
165fe01d993SBarry Smith PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
166fe01d993SBarry Smith {
167fe01d993SBarry Smith   PetscFunctionBegin;
1689566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
1699566063dSJacob Faibussowitsch   PetscCall(PetscDrawLGDestroy(&(*vf)->lg));
1709566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
171fe01d993SBarry Smith   PetscFunctionReturn(0);
172fe01d993SBarry Smith }
173fe01d993SBarry Smith 
1745c6c1daeSBarry Smith /*@C
1755c6c1daeSBarry Smith    PetscViewerGetType - Returns the type of a PetscViewer.
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith    Not Collective
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith    Input Parameter:
1805c6c1daeSBarry Smith .   viewer - the PetscViewer
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith    Output Parameter:
1835c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith    Available Types Include:
186a2b725a8SWilliam Gropp +  PETSCVIEWERSOCKET - Socket PetscViewer
1875c6c1daeSBarry Smith .  PETSCVIEWERASCII - ASCII PetscViewer
1885c6c1daeSBarry Smith .  PETSCVIEWERBINARY - binary file PetscViewer
1895c6c1daeSBarry Smith .  PETSCVIEWERSTRING - string PetscViewer
190a2b725a8SWilliam Gropp -  PETSCVIEWERDRAW - drawing PetscViewer
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith    Level: intermediate
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith    Note:
1955c6c1daeSBarry Smith    See include/petscviewer.h for a complete list of PetscViewers.
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith    PetscViewerType is actually a string
1985c6c1daeSBarry Smith 
199db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith @*/
2025c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
2035c6c1daeSBarry Smith {
2045c6c1daeSBarry Smith   PetscFunctionBegin;
2055c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2065c6c1daeSBarry Smith   PetscValidPointer(type,2);
2075c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
2085c6c1daeSBarry Smith   PetscFunctionReturn(0);
2095c6c1daeSBarry Smith }
2105c6c1daeSBarry Smith 
2115c6c1daeSBarry Smith /*@C
2125c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
2135c6c1daeSBarry Smith    PetscViewer options in the database.
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith    Logically Collective on PetscViewer
2165c6c1daeSBarry Smith 
217d8d19677SJose E. Roman    Input Parameters:
2185c6c1daeSBarry Smith +  viewer - the PetscViewer context
2195c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith    Notes:
2225c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2235c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2245c6c1daeSBarry Smith 
2255c6c1daeSBarry Smith    Level: advanced
2265c6c1daeSBarry Smith 
227db781477SPatrick Sanan .seealso: `PetscViewerSetFromOptions()`
2285c6c1daeSBarry Smith @*/
2295c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
2305c6c1daeSBarry Smith {
2315c6c1daeSBarry Smith   PetscFunctionBegin;
2325c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2339566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix));
2345c6c1daeSBarry Smith   PetscFunctionReturn(0);
2355c6c1daeSBarry Smith }
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith /*@C
2385c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
2395c6c1daeSBarry Smith    PetscViewer options in the database.
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith    Logically Collective on PetscViewer
2425c6c1daeSBarry Smith 
2435c6c1daeSBarry Smith    Input Parameters:
2445c6c1daeSBarry Smith +  viewer - the PetscViewer context
2455c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2465c6c1daeSBarry Smith 
2475c6c1daeSBarry Smith    Notes:
2485c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2495c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith    Level: advanced
2525c6c1daeSBarry Smith 
253db781477SPatrick Sanan .seealso: `PetscViewerGetOptionsPrefix()`
2545c6c1daeSBarry Smith @*/
2555c6c1daeSBarry Smith PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
2565c6c1daeSBarry Smith {
2575c6c1daeSBarry Smith   PetscFunctionBegin;
2585c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2599566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix));
2605c6c1daeSBarry Smith   PetscFunctionReturn(0);
2615c6c1daeSBarry Smith }
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith /*@C
2645c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2655c6c1daeSBarry Smith    PetscViewer options in the database.
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith    Not Collective
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith    Input Parameter:
2705c6c1daeSBarry Smith .  viewer - the PetscViewer context
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith    Output Parameter:
2735c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2745c6c1daeSBarry Smith 
27595452b02SPatrick Sanan    Notes:
27695452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
2775c6c1daeSBarry Smith    sufficient length to hold the prefix.
2785c6c1daeSBarry Smith 
2795c6c1daeSBarry Smith    Level: advanced
2805c6c1daeSBarry Smith 
281db781477SPatrick Sanan .seealso: `PetscViewerAppendOptionsPrefix()`
2825c6c1daeSBarry Smith @*/
2835c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
2845c6c1daeSBarry Smith {
2855c6c1daeSBarry Smith   PetscFunctionBegin;
2865c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix));
2885c6c1daeSBarry Smith   PetscFunctionReturn(0);
2895c6c1daeSBarry Smith }
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith /*@
2925c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith    Collective on PetscViewer
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith    Input Parameters:
2975c6c1daeSBarry Smith .  viewer - the PetscViewer context
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith    Notes:
3005c6c1daeSBarry Smith    For basic use of the PetscViewer classes the user need not explicitly call
3015c6c1daeSBarry Smith    PetscViewerSetUp(), since these actions will happen automatically.
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith    Level: advanced
3045c6c1daeSBarry Smith 
305db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerDestroy()`
3065c6c1daeSBarry Smith @*/
3075c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
3085c6c1daeSBarry Smith {
3095c6c1daeSBarry Smith   PetscFunctionBegin;
3105c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
311c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
312*dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer,setup);
313c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
3145c6c1daeSBarry Smith   PetscFunctionReturn(0);
3155c6c1daeSBarry Smith }
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith /*@C
318fe2efc57SMark    PetscViewerViewFromOptions - View from Options
319fe2efc57SMark 
320fe2efc57SMark    Collective on PetscViewer
321fe2efc57SMark 
322fe2efc57SMark    Input Parameters:
323fe2efc57SMark +  A - the PetscViewer context
324736c3998SJose E. Roman .  obj - Optional object
325736c3998SJose E. Roman -  name - command line option
326fe2efc57SMark 
327fe2efc57SMark    Level: intermediate
328db781477SPatrick Sanan .seealso: `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
329fe2efc57SMark @*/
330fe2efc57SMark PetscErrorCode  PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])
331fe2efc57SMark {
332fe2efc57SMark   PetscFunctionBegin;
333fe2efc57SMark   PetscValidHeaderSpecific(A,PETSC_VIEWER_CLASSID,1);
3349566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name));
335fe2efc57SMark   PetscFunctionReturn(0);
336fe2efc57SMark }
337fe2efc57SMark 
338fe2efc57SMark /*@C
3395c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith    Collective on PetscViewer
3425c6c1daeSBarry Smith 
3435c6c1daeSBarry Smith    Input Parameters:
344f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3455c6c1daeSBarry Smith -  viewer - visualization context
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith   Notes:
3485c6c1daeSBarry Smith   The available visualization contexts include
3495c6c1daeSBarry Smith +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
3505c6c1daeSBarry Smith .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
3515c6c1daeSBarry Smith         output where only the first processor opens
3525c6c1daeSBarry Smith         the file.  All other processors send their
3535c6c1daeSBarry Smith         data to the first processor to print.
3545c6c1daeSBarry Smith -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
3555c6c1daeSBarry Smith 
3565c6c1daeSBarry Smith    Level: beginner
3575c6c1daeSBarry Smith 
358db781477SPatrick Sanan .seealso: `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
359db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3605c6c1daeSBarry Smith @*/
3615c6c1daeSBarry Smith PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
3625c6c1daeSBarry Smith {
3635c6c1daeSBarry Smith   PetscBool         iascii;
3645c6c1daeSBarry Smith   PetscViewerFormat format;
365e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
366536b137fSBarry Smith   PetscBool         issaws;
3670076e027SBarry Smith #endif
3685c6c1daeSBarry Smith 
3695c6c1daeSBarry Smith   PetscFunctionBegin;
3705c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3715c6c1daeSBarry Smith   PetscValidType(v,1);
3725c6c1daeSBarry Smith   if (!viewer) {
3739566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer));
3745c6c1daeSBarry Smith   }
3755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3765c6c1daeSBarry Smith   PetscCheckSameComm(v,1,viewer,2);
3775c6c1daeSBarry Smith 
3789566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii));
379e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3809566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws));
3810076e027SBarry Smith #endif
3825c6c1daeSBarry Smith   if (iascii) {
3839566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer,&format));
3849566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer));
38598c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
3862f234a98SBarry Smith       if (v->format) {
3879566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]));
3882f234a98SBarry Smith       }
3899566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
390*dbbe0bcdSBarry Smith       PetscTryTypeMethod(v,view,viewer);
3919566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3925c6c1daeSBarry Smith     }
393e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
394536b137fSBarry Smith   } else if (issaws) {
3950076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3969566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v,viewer));
397*dbbe0bcdSBarry Smith       PetscTryTypeMethod(v,view,viewer);
3980076e027SBarry Smith     }
3990076e027SBarry Smith #endif
4005c6c1daeSBarry Smith   }
4015c6c1daeSBarry Smith   PetscFunctionReturn(0);
4025c6c1daeSBarry Smith }
4031d641e7bSMichael Lange 
4041d641e7bSMichael Lange /*@C
4051d641e7bSMichael Lange    PetscViewerRead - Reads data from a PetscViewer
4061d641e7bSMichael Lange 
407d083f849SBarry Smith    Collective
4081d641e7bSMichael Lange 
4091d641e7bSMichael Lange    Input Parameters:
4101d641e7bSMichael Lange +  viewer   - The viewer
4111d641e7bSMichael Lange .  data     - Location to write the data
412060da220SMatthew G. Knepley .  num      - Number of items of data to read
4131d641e7bSMichael Lange -  datatype - Type of data to read
4141d641e7bSMichael Lange 
415f8e4bde8SMatthew G. Knepley    Output Parameters:
416060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
417f8e4bde8SMatthew G. Knepley 
418632e26b4SStefano Zampini    Notes:
419632e26b4SStefano Zampini    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
420632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
421632e26b4SStefano Zampini 
4221d641e7bSMichael Lange    Level: beginner
4231d641e7bSMichael Lange 
424db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
425db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`,
426db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
4271d641e7bSMichael Lange @*/
428060da220SMatthew G. Knepley PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
4291d641e7bSMichael Lange {
4301d641e7bSMichael Lange   PetscFunctionBegin;
4311d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4321d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
433060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4341d641e7bSMichael Lange     char *s = (char *)data;
435632e26b4SStefano Zampini     if (num >= 0) {
436060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4371d641e7bSMichael Lange         /* Skip leading whitespaces */
4389566063dSJacob Faibussowitsch         do {PetscCall((*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR)); if (!cnt) break;}
439eb2700f0SMichael Lange         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
4401d641e7bSMichael Lange         i++;
4411d641e7bSMichael Lange         /* Read strings one char at a time */
4429566063dSJacob Faibussowitsch         do {PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR)); if (!cnt) break;}
443eb2700f0SMichael Lange         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');
4441d641e7bSMichael Lange         /* Terminate final string */
445060da220SMatthew G. Knepley         if (c == num-1) s[i-1] = '\0';
4461d641e7bSMichael Lange       }
447632e26b4SStefano Zampini     } else {
448632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4499566063dSJacob Faibussowitsch       do {PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR)); if (i == -num || !cnt) break;}
450632e26b4SStefano Zampini       while (s[i-1]!='\n');
451632e26b4SStefano Zampini       /* Terminate final string */
452632e26b4SStefano Zampini       s[i-1] = '\0';
453632e26b4SStefano Zampini       c      = i;
454632e26b4SStefano Zampini     }
455060da220SMatthew G. Knepley     if (count) *count = c;
45608401ef6SPierre Jolivet     else PetscCheck(c >= num,PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
457*dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer,read , data, num, count, dtype);
4581d641e7bSMichael Lange   PetscFunctionReturn(0);
4591d641e7bSMichael Lange }
460e24fdd67SVaclav Hapla 
461af684e28SVaclav Hapla /*@
462af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
463af684e28SVaclav Hapla 
464af684e28SVaclav Hapla    Not Collective
465af684e28SVaclav Hapla 
466af684e28SVaclav Hapla    Input Parameters:
467af684e28SVaclav Hapla .  viewer - the PetscViewer context
468af684e28SVaclav Hapla 
469af684e28SVaclav Hapla    Output Parameters:
470af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise
471af684e28SVaclav Hapla 
472af684e28SVaclav Hapla    Notes:
473af684e28SVaclav Hapla    PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
474af684e28SVaclav Hapla    and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
475af684e28SVaclav Hapla    returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
476af684e28SVaclav Hapla 
477af684e28SVaclav Hapla    Level: intermediate
478af684e28SVaclav Hapla 
479db781477SPatrick Sanan .seealso: `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
480af684e28SVaclav Hapla @*/
481e24fdd67SVaclav Hapla PetscErrorCode  PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
482e24fdd67SVaclav Hapla {
483e24fdd67SVaclav Hapla   PetscFileMode     mode;
484e24fdd67SVaclav Hapla   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;
485e24fdd67SVaclav Hapla 
486e24fdd67SVaclav Hapla   PetscFunctionBegin;
487e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
488534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
4899566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
490e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
491e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
4929566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
493e24fdd67SVaclav Hapla   switch (mode) {
494e24fdd67SVaclav Hapla     case FILE_MODE_READ:
495e24fdd67SVaclav Hapla     case FILE_MODE_UPDATE:
496e24fdd67SVaclav Hapla     case FILE_MODE_APPEND_UPDATE:
497e24fdd67SVaclav Hapla       *flg = PETSC_TRUE;
498e24fdd67SVaclav Hapla     default: break;
499e24fdd67SVaclav Hapla   }
500e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
501e24fdd67SVaclav Hapla }
502e24fdd67SVaclav Hapla 
503af684e28SVaclav Hapla /*@
504af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
505af684e28SVaclav Hapla 
506af684e28SVaclav Hapla    Not Collective
507af684e28SVaclav Hapla 
508af684e28SVaclav Hapla    Input Parameters:
509af684e28SVaclav Hapla .  viewer - the PetscViewer context
510af684e28SVaclav Hapla 
511af684e28SVaclav Hapla    Output Parameters:
512af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise
513af684e28SVaclav Hapla 
514af684e28SVaclav Hapla    Notes:
515af684e28SVaclav Hapla    PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
516af684e28SVaclav Hapla    returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
517af684e28SVaclav Hapla 
518af684e28SVaclav Hapla    Level: intermediate
519af684e28SVaclav Hapla 
520db781477SPatrick Sanan .seealso: `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
521af684e28SVaclav Hapla @*/
522e24fdd67SVaclav Hapla PetscErrorCode  PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
523e24fdd67SVaclav Hapla {
524e24fdd67SVaclav Hapla   PetscFileMode     mode;
525e24fdd67SVaclav Hapla   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;
526e24fdd67SVaclav Hapla 
527e24fdd67SVaclav Hapla   PetscFunctionBegin;
528e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
529534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
5309566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
531e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
532e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
5339566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
534e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
535e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
536e24fdd67SVaclav Hapla }
537e24fdd67SVaclav Hapla 
538af684e28SVaclav Hapla /*@
539af684e28SVaclav Hapla    PetscViewerCheckReadable - Check whether the viewer can be read from
540af684e28SVaclav Hapla 
541af684e28SVaclav Hapla    Collective
542af684e28SVaclav Hapla 
543af684e28SVaclav Hapla    Input Parameters:
544af684e28SVaclav Hapla .  viewer - the PetscViewer context
545af684e28SVaclav Hapla 
546af684e28SVaclav Hapla    Level: intermediate
547af684e28SVaclav Hapla 
548db781477SPatrick Sanan .seealso: `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
549af684e28SVaclav Hapla @*/
550d01f05b1SVaclav Hapla PetscErrorCode  PetscViewerCheckReadable(PetscViewer viewer)
551d01f05b1SVaclav Hapla {
552d01f05b1SVaclav Hapla   PetscBool         flg;
553d01f05b1SVaclav Hapla 
554d01f05b1SVaclav Hapla   PetscFunctionBegin;
5550af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5569566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
55728b400f6SJacob 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)");
558d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
559d01f05b1SVaclav Hapla }
560d01f05b1SVaclav Hapla 
561af684e28SVaclav Hapla /*@
562af684e28SVaclav Hapla    PetscViewerCheckWritable - Check whether the viewer can be written to
563af684e28SVaclav Hapla 
564af684e28SVaclav Hapla    Collective
565af684e28SVaclav Hapla 
566af684e28SVaclav Hapla    Input Parameters:
567af684e28SVaclav Hapla .  viewer - the PetscViewer context
568af684e28SVaclav Hapla 
569af684e28SVaclav Hapla    Level: intermediate
570af684e28SVaclav Hapla 
571db781477SPatrick Sanan .seealso: `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
572af684e28SVaclav Hapla @*/
573d01f05b1SVaclav Hapla PetscErrorCode  PetscViewerCheckWritable(PetscViewer viewer)
574d01f05b1SVaclav Hapla {
575d01f05b1SVaclav Hapla   PetscBool         flg;
576d01f05b1SVaclav Hapla 
577d01f05b1SVaclav Hapla   PetscFunctionBegin;
5780af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5799566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
58028b400f6SJacob Faibussowitsch   PetscCheck(flg,PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
581d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
582d01f05b1SVaclav Hapla }
583