xref: /petsc/src/sys/classes/viewer/interface/view.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
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
10*811af0c4SBarry 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;
1848a46eb9SPierre Jolivet     if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_keyval));
1948a46eb9SPierre Jolivet   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval));
2048a46eb9SPierre Jolivet   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval));
2148a46eb9SPierre Jolivet   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval));
2248a46eb9SPierre 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)
2448a46eb9SPierre 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)
2748a46eb9SPierre 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
36*811af0c4SBarry Smith   PetscViewerInitializePackage - This function initializes everything in the `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 /*@
75*811af0c4SBarry Smith    PetscViewerDestroy - Destroys a `PetscViewer`.
765c6c1daeSBarry Smith 
77*811af0c4SBarry Smith    Collective on viewer
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith    Input Parameters:
80*811af0c4SBarry Smith .  viewer - the `PetscViewer` to be destroyed.
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith    Level: beginner
835c6c1daeSBarry Smith 
84*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`
855c6c1daeSBarry Smith @*/
869371c9d4SSatish Balay PetscErrorCode PetscViewerDestroy(PetscViewer *viewer) {
875c6c1daeSBarry Smith   PetscFunctionBegin;
885c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
895c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer, PETSC_VIEWER_CLASSID, 1);
905c6c1daeSBarry Smith 
919566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(*viewer));
929371c9d4SSatish Balay   if (--((PetscObject)(*viewer))->refct > 0) {
939371c9d4SSatish Balay     *viewer = NULL;
949371c9d4SSatish Balay     PetscFunctionReturn(0);
959371c9d4SSatish Balay   }
965c6c1daeSBarry Smith 
979566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*viewer));
9848a46eb9SPierre Jolivet   if ((*viewer)->ops->destroy) PetscCall((*(*viewer)->ops->destroy)(*viewer));
999566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(viewer));
1005c6c1daeSBarry Smith   PetscFunctionReturn(0);
1015c6c1daeSBarry Smith }
1025c6c1daeSBarry Smith 
103d7cbc13eSBarry Smith /*@C
104*811af0c4SBarry Smith    PetscViewerAndFormatCreate - Creates a `PetscViewerAndFormat` struct.
105d7cbc13eSBarry Smith 
106*811af0c4SBarry Smith    Collective on viewer
107d7cbc13eSBarry Smith 
108d7cbc13eSBarry Smith    Input Parameters:
109d7cbc13eSBarry Smith +  viewer - the viewer
110d7cbc13eSBarry Smith -  format - the format
111d7cbc13eSBarry Smith 
112d7cbc13eSBarry Smith    Output Parameter:
113d7cbc13eSBarry Smith .   vf - viewer and format object
114d7cbc13eSBarry Smith 
115d7cbc13eSBarry Smith    Level: developer
116d7cbc13eSBarry Smith 
117*811af0c4SBarry Smith    Notes:
118*811af0c4SBarry Smith    This increases the reference count of the viewer so you can destroy the viewer object after this call
119*811af0c4SBarry Smith 
120*811af0c4SBarry 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 @*/
1249371c9d4SSatish Balay PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf) {
125d7cbc13eSBarry Smith   PetscFunctionBegin;
1269566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)viewer));
1279566063dSJacob Faibussowitsch   PetscCall(PetscNew(vf));
128d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
129d7cbc13eSBarry Smith   (*vf)->format = format;
130798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
131798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
132d7cbc13eSBarry Smith   PetscFunctionReturn(0);
133d7cbc13eSBarry Smith }
134d7cbc13eSBarry Smith 
135fe01d993SBarry Smith /*@C
136*811af0c4SBarry Smith    PetscViewerAndFormatDestroy - Destroys a `PetscViewerAndFormat` struct.
137fe01d993SBarry Smith 
138*811af0c4SBarry Smith    Collective on vf
139fe01d993SBarry Smith 
140fe01d993SBarry Smith    Input Parameters:
141*811af0c4SBarry Smith .  vf - the `PetscViewerAndFormat` to be destroyed.
142fe01d993SBarry Smith 
143d7cbc13eSBarry Smith    Level: developer
144fe01d993SBarry Smith 
145db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewerAndFormatCreate()`
146fe01d993SBarry Smith @*/
1479371c9d4SSatish Balay PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf) {
148fe01d993SBarry Smith   PetscFunctionBegin;
1499566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*vf)->viewer));
1509566063dSJacob Faibussowitsch   PetscCall(PetscDrawLGDestroy(&(*vf)->lg));
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree(*vf));
152fe01d993SBarry Smith   PetscFunctionReturn(0);
153fe01d993SBarry Smith }
154fe01d993SBarry Smith 
1555c6c1daeSBarry Smith /*@C
156*811af0c4SBarry Smith    PetscViewerGetType - Returns the type of a `PetscViewer`.
1575c6c1daeSBarry Smith 
1585c6c1daeSBarry Smith    Not Collective
1595c6c1daeSBarry Smith 
1605c6c1daeSBarry Smith    Input Parameter:
161*811af0c4SBarry Smith .   viewer - the `PetscViewer`
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith    Output Parameter:
1645c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith    Available Types Include:
167*811af0c4SBarry Smith +  `PETSCVIEWERSOCKET` - Socket PetscViewer
168*811af0c4SBarry Smith .  `PETSCVIEWERASCII` - ASCII PetscViewer
169*811af0c4SBarry Smith .  `PETSCVIEWERBINARY` - binary file PetscViewer
170*811af0c4SBarry Smith .  `PETSCVIEWERSTRING` - string PetscViewer
171*811af0c4SBarry Smith -  `PETSCVIEWERDRAW` - drawing PetscViewer
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith    Level: intermediate
1745c6c1daeSBarry Smith 
1755c6c1daeSBarry Smith    Note:
176*811af0c4SBarry Smith    See include/petscviewer.h for a complete list of `PetscViewer`s.
1775c6c1daeSBarry Smith 
178*811af0c4SBarry Smith    `PetscViewerType` is actually a string
1795c6c1daeSBarry Smith 
180*811af0c4SBarry Smith .seealso: `PetscViewerType`, `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
1815c6c1daeSBarry Smith @*/
1829371c9d4SSatish Balay PetscErrorCode PetscViewerGetType(PetscViewer viewer, PetscViewerType *type) {
1835c6c1daeSBarry Smith   PetscFunctionBegin;
1845c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1855c6c1daeSBarry Smith   PetscValidPointer(type, 2);
1865c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1875c6c1daeSBarry Smith   PetscFunctionReturn(0);
1885c6c1daeSBarry Smith }
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith /*@C
1915c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
192*811af0c4SBarry Smith    `PetscViewer` options in the database.
1935c6c1daeSBarry Smith 
194*811af0c4SBarry Smith    Logically Collective on viewer
1955c6c1daeSBarry Smith 
196d8d19677SJose E. Roman    Input Parameters:
197*811af0c4SBarry Smith +  viewer - the `PetscViewer` context
1985c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
1995c6c1daeSBarry Smith 
200*811af0c4SBarry Smith    Note:
2015c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2025c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith    Level: advanced
2055c6c1daeSBarry Smith 
206*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerSetFromOptions()`
2075c6c1daeSBarry Smith @*/
2089371c9d4SSatish Balay PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer, const char prefix[]) {
2095c6c1daeSBarry Smith   PetscFunctionBegin;
2105c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)viewer, prefix));
2125c6c1daeSBarry Smith   PetscFunctionReturn(0);
2135c6c1daeSBarry Smith }
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith /*@C
2165c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
217*811af0c4SBarry Smith    `PetscViewer` options in the database.
2185c6c1daeSBarry Smith 
219*811af0c4SBarry Smith    Logically Collective on viewer
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith    Input Parameters:
2225c6c1daeSBarry Smith +  viewer - the PetscViewer context
2235c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2245c6c1daeSBarry Smith 
225*811af0c4SBarry Smith    Note:
2265c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2275c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith    Level: advanced
2305c6c1daeSBarry Smith 
231*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerGetOptionsPrefix()`
2325c6c1daeSBarry Smith @*/
2339371c9d4SSatish Balay PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer, const char prefix[]) {
2345c6c1daeSBarry Smith   PetscFunctionBegin;
2355c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2369566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)viewer, prefix));
2375c6c1daeSBarry Smith   PetscFunctionReturn(0);
2385c6c1daeSBarry Smith }
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith /*@C
2415c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2425c6c1daeSBarry Smith    PetscViewer options in the database.
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith    Not Collective
2455c6c1daeSBarry Smith 
2465c6c1daeSBarry Smith    Input Parameter:
247*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith    Output Parameter:
2505c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2515c6c1daeSBarry Smith 
252*811af0c4SBarry Smith    Fortran Note:
253*811af0c4SBarry Smith    The user should pass in a string 'prefix' of sufficient length to hold the prefix.
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith    Level: advanced
2565c6c1daeSBarry Smith 
257*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerAppendOptionsPrefix()`
2585c6c1daeSBarry Smith @*/
2599371c9d4SSatish Balay PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer, const char *prefix[]) {
2605c6c1daeSBarry Smith   PetscFunctionBegin;
2615c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2629566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)viewer, prefix));
2635c6c1daeSBarry Smith   PetscFunctionReturn(0);
2645c6c1daeSBarry Smith }
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith /*@
2675c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2685c6c1daeSBarry Smith 
269*811af0c4SBarry Smith    Collective on viewer
2705c6c1daeSBarry Smith 
2715c6c1daeSBarry Smith    Input Parameters:
272*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
2735c6c1daeSBarry Smith 
274*811af0c4SBarry Smith    Note:
275*811af0c4SBarry Smith    For basic use of the `PetscViewer` classes the user need not explicitly call
276*811af0c4SBarry Smith    `PetscViewerSetUp()`, since these actions will happen automatically.
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith    Level: advanced
2795c6c1daeSBarry Smith 
280*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerCreate()`, `PetscViewerDestroy()`
2815c6c1daeSBarry Smith @*/
2829371c9d4SSatish Balay PetscErrorCode PetscViewerSetUp(PetscViewer viewer) {
2835c6c1daeSBarry Smith   PetscFunctionBegin;
2845c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
285c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
286dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setup);
287c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
2885c6c1daeSBarry Smith   PetscFunctionReturn(0);
2895c6c1daeSBarry Smith }
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith /*@C
292*811af0c4SBarry Smith    PetscViewerViewFromOptions - View from the viewer based on the options database values
293fe2efc57SMark 
294*811af0c4SBarry Smith    Collective on A
295fe2efc57SMark 
296fe2efc57SMark    Input Parameters:
297*811af0c4SBarry Smith +  A - the `PetscViewer` context
298*811af0c4SBarry Smith .  obj - Optional object that provides the prefix for the option names
299736c3998SJose E. Roman -  name - command line option
300fe2efc57SMark 
301fe2efc57SMark    Level: intermediate
302*811af0c4SBarry Smith 
303db781477SPatrick Sanan .seealso: `PetscViewer`, `PetscViewerView`, `PetscObjectViewFromOptions()`, `PetscViewerCreate()`
304fe2efc57SMark @*/
3059371c9d4SSatish Balay PetscErrorCode PetscViewerViewFromOptions(PetscViewer A, PetscObject obj, const char name[]) {
306fe2efc57SMark   PetscFunctionBegin;
307fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_VIEWER_CLASSID, 1);
3089566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
309fe2efc57SMark   PetscFunctionReturn(0);
310fe2efc57SMark }
311fe2efc57SMark 
312fe2efc57SMark /*@C
3135c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3145c6c1daeSBarry Smith 
315*811af0c4SBarry Smith    Collective on v
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith    Input Parameters:
318f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3195c6c1daeSBarry Smith -  viewer - visualization context
3205c6c1daeSBarry Smith 
321*811af0c4SBarry Smith   Note:
3225c6c1daeSBarry Smith   The available visualization contexts include
323*811af0c4SBarry Smith .vb
324*811af0c4SBarry Smith   PETSC_VIEWER_STDOUT_SELF - standard output (default)
325*811af0c4SBarry 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
326*811af0c4SBarry Smith   PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
327*811af0c4SBarry Smith .ve
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith    Level: beginner
3305c6c1daeSBarry Smith 
331*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`,
332db781477SPatrick Sanan           `PetscViewerSocketOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerLoad()`
3335c6c1daeSBarry Smith @*/
3349371c9d4SSatish Balay PetscErrorCode PetscViewerView(PetscViewer v, PetscViewer viewer) {
3355c6c1daeSBarry Smith   PetscBool         iascii;
3365c6c1daeSBarry Smith   PetscViewerFormat format;
337e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
338536b137fSBarry Smith   PetscBool issaws;
3390076e027SBarry Smith #endif
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith   PetscFunctionBegin;
3425c6c1daeSBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
3435c6c1daeSBarry Smith   PetscValidType(v, 1);
34448a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v), &viewer));
3455c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3465c6c1daeSBarry Smith   PetscCheckSameComm(v, 1, viewer, 2);
3475c6c1daeSBarry Smith 
3489566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
349e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
3509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
3510076e027SBarry Smith #endif
3525c6c1daeSBarry Smith   if (iascii) {
3539566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
3549566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)v, viewer));
35598c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
35648a46eb9SPierre Jolivet       if (v->format) PetscCall(PetscViewerASCIIPrintf(viewer, "  Viewer format = %s\n", PetscViewerFormats[v->format]));
3579566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
358dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3599566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
3605c6c1daeSBarry Smith     }
361e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
362536b137fSBarry Smith   } else if (issaws) {
3630076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
3649566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)v, viewer));
365dbbe0bcdSBarry Smith       PetscTryTypeMethod(v, view, viewer);
3660076e027SBarry Smith     }
3670076e027SBarry Smith #endif
3685c6c1daeSBarry Smith   }
3695c6c1daeSBarry Smith   PetscFunctionReturn(0);
3705c6c1daeSBarry Smith }
3711d641e7bSMichael Lange 
3721d641e7bSMichael Lange /*@C
373*811af0c4SBarry Smith    PetscViewerRead - Reads data from a `PetscViewer`
3741d641e7bSMichael Lange 
375d083f849SBarry Smith    Collective
3761d641e7bSMichael Lange 
3771d641e7bSMichael Lange    Input Parameters:
3781d641e7bSMichael Lange +  viewer   - The viewer
3791d641e7bSMichael Lange .  data     - Location to write the data
380060da220SMatthew G. Knepley .  num      - Number of items of data to read
3811d641e7bSMichael Lange -  datatype - Type of data to read
3821d641e7bSMichael Lange 
383f8e4bde8SMatthew G. Knepley    Output Parameters:
384060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
385f8e4bde8SMatthew G. Knepley 
386*811af0c4SBarry Smith    Note:
387*811af0c4SBarry Smith    If datatype is `PETSC_STRING` and num is negative, reads until a newline character is found,
388632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
389632e26b4SStefano Zampini 
3901d641e7bSMichael Lange    Level: beginner
3911d641e7bSMichael Lange 
392*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
393db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`,
394db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`
3951d641e7bSMichael Lange @*/
3969371c9d4SSatish Balay PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype) {
3971d641e7bSMichael Lange   PetscFunctionBegin;
3981d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
3991d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
400060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4011d641e7bSMichael Lange     char    *s = (char *)data;
402632e26b4SStefano Zampini     if (num >= 0) {
403060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4041d641e7bSMichael Lange         /* Skip leading whitespaces */
4059371c9d4SSatish Balay         do {
4069371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR));
4079371c9d4SSatish Balay           if (!cnt) break;
4089371c9d4SSatish Balay         } while (s[i] == '\n' || s[i] == '\t' || s[i] == ' ' || s[i] == '\0' || s[i] == '\v' || s[i] == '\f' || s[i] == '\r');
4091d641e7bSMichael Lange         i++;
4101d641e7bSMichael Lange         /* Read strings one char at a time */
4119371c9d4SSatish Balay         do {
4129371c9d4SSatish Balay           PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4139371c9d4SSatish Balay           if (!cnt) break;
4149371c9d4SSatish 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');
4151d641e7bSMichael Lange         /* Terminate final string */
416060da220SMatthew G. Knepley         if (c == num - 1) s[i - 1] = '\0';
4171d641e7bSMichael Lange       }
418632e26b4SStefano Zampini     } else {
419632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4209371c9d4SSatish Balay       do {
4219371c9d4SSatish Balay         PetscCall((*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR));
4229371c9d4SSatish Balay         if (i == -num || !cnt) break;
4239371c9d4SSatish Balay       } while (s[i - 1] != '\n');
424632e26b4SStefano Zampini       /* Terminate final string */
425632e26b4SStefano Zampini       s[i - 1] = '\0';
426632e26b4SStefano Zampini       c        = i;
427632e26b4SStefano Zampini     }
428060da220SMatthew G. Knepley     if (count) *count = c;
42908401ef6SPierre Jolivet     else PetscCheck(c >= num, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %" PetscInt_FMT " < %" PetscInt_FMT " strings", c, num);
430dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(viewer, read, data, num, count, dtype);
4311d641e7bSMichael Lange   PetscFunctionReturn(0);
4321d641e7bSMichael Lange }
433e24fdd67SVaclav Hapla 
434af684e28SVaclav Hapla /*@
435af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
436af684e28SVaclav Hapla 
437af684e28SVaclav Hapla    Not Collective
438af684e28SVaclav Hapla 
439af684e28SVaclav Hapla    Input Parameters:
440*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
441af684e28SVaclav Hapla 
442af684e28SVaclav Hapla    Output Parameters:
443*811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is readable, `PETSC_FALSE` otherwise
444af684e28SVaclav Hapla 
445*811af0c4SBarry Smith    Note:
446*811af0c4SBarry Smith    `PETSC_TRUE` means that viewer's `PetscViewerType` supports reading (this holds e.g. for `PETSCVIEWERBINARY`)
447*811af0c4SBarry Smith    and viewer is in a mode allowing reading, i.e. `PetscViewerFileGetMode()`
448*811af0c4SBarry Smith    returns one of `FILE_MODE_READ`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
449af684e28SVaclav Hapla 
450af684e28SVaclav Hapla    Level: intermediate
451af684e28SVaclav Hapla 
452*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
453af684e28SVaclav Hapla @*/
4549371c9d4SSatish Balay PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg) {
455e24fdd67SVaclav Hapla   PetscFileMode mode;
456e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
457e24fdd67SVaclav Hapla 
458e24fdd67SVaclav Hapla   PetscFunctionBegin;
459e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
460534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
4619566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
462e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
463e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
4649566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
465e24fdd67SVaclav Hapla   switch (mode) {
466e24fdd67SVaclav Hapla   case FILE_MODE_READ:
467e24fdd67SVaclav Hapla   case FILE_MODE_UPDATE:
4689371c9d4SSatish Balay   case FILE_MODE_APPEND_UPDATE: *flg = PETSC_TRUE;
469e24fdd67SVaclav Hapla   default: break;
470e24fdd67SVaclav Hapla   }
471e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
472e24fdd67SVaclav Hapla }
473e24fdd67SVaclav Hapla 
474af684e28SVaclav Hapla /*@
475af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
476af684e28SVaclav Hapla 
477af684e28SVaclav Hapla    Not Collective
478af684e28SVaclav Hapla 
479af684e28SVaclav Hapla    Input Parameters:
480*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
481af684e28SVaclav Hapla 
482af684e28SVaclav Hapla    Output Parameters:
483*811af0c4SBarry Smith .  flg - `PETSC_TRUE` if the viewer is writable, `PETSC_FALSE` otherwise
484af684e28SVaclav Hapla 
485*811af0c4SBarry Smith    Note:
486*811af0c4SBarry Smith    `PETSC_TRUE` means viewer is in a mode allowing writing, i.e. `PetscViewerFileGetMode()`
487*811af0c4SBarry Smith    returns one of `FILE_MODE_WRITE`, `FILE_MODE_APPEND`, `FILE_MODE_UPDATE`, `FILE_MODE_APPEND_UPDATE`.
488af684e28SVaclav Hapla 
489af684e28SVaclav Hapla    Level: intermediate
490af684e28SVaclav Hapla 
491*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
492af684e28SVaclav Hapla @*/
4939371c9d4SSatish Balay PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg) {
494e24fdd67SVaclav Hapla   PetscFileMode mode;
495e24fdd67SVaclav Hapla   PetscErrorCode (*f)(PetscViewer, PetscFileMode *) = NULL;
496e24fdd67SVaclav Hapla 
497e24fdd67SVaclav Hapla   PetscFunctionBegin;
498e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
499534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
5009566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f));
501e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
502e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
5039566063dSJacob Faibussowitsch   PetscCall((*f)(viewer, &mode));
504e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
505e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
506e24fdd67SVaclav Hapla }
507e24fdd67SVaclav Hapla 
508af684e28SVaclav Hapla /*@
509*811af0c4SBarry Smith    PetscViewerCheckReadable - Check whether the viewer can be read from, generates an error if not
510af684e28SVaclav Hapla 
511af684e28SVaclav Hapla    Collective
512af684e28SVaclav Hapla 
513af684e28SVaclav Hapla    Input Parameters:
514*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
515af684e28SVaclav Hapla 
516af684e28SVaclav Hapla    Level: intermediate
517af684e28SVaclav Hapla 
518*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerReadable()`, `PetscViewerCheckWritable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
519af684e28SVaclav Hapla @*/
5209371c9d4SSatish Balay PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer) {
521d01f05b1SVaclav Hapla   PetscBool flg;
522d01f05b1SVaclav Hapla 
523d01f05b1SVaclav Hapla   PetscFunctionBegin;
5240af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5259566063dSJacob Faibussowitsch   PetscCall(PetscViewerReadable(viewer, &flg));
52628b400f6SJacob 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)");
527d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
528d01f05b1SVaclav Hapla }
529d01f05b1SVaclav Hapla 
530af684e28SVaclav Hapla /*@
531*811af0c4SBarry Smith    PetscViewerCheckWritable - Check whether the viewer can be written to, generates an error if not
532af684e28SVaclav Hapla 
533af684e28SVaclav Hapla    Collective
534af684e28SVaclav Hapla 
535af684e28SVaclav Hapla    Input Parameters:
536*811af0c4SBarry Smith .  viewer - the `PetscViewer` context
537af684e28SVaclav Hapla 
538af684e28SVaclav Hapla    Level: intermediate
539af684e28SVaclav Hapla 
540*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerWritable()`, `PetscViewerCheckReadable()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetType()`
541af684e28SVaclav Hapla @*/
5429371c9d4SSatish Balay PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer) {
543d01f05b1SVaclav Hapla   PetscBool flg;
544d01f05b1SVaclav Hapla 
545d01f05b1SVaclav Hapla   PetscFunctionBegin;
5460af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5479566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &flg));
54828b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
549d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
550d01f05b1SVaclav Hapla }
551