xref: /petsc/src/sys/classes/viewer/interface/view.c (revision b6dade52a37f03b2234fe489e8396dc948111b45)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith PetscClassId PETSC_VIEWER_CLASSID;
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
75c6c1daeSBarry Smith /*@C
8*b6dade52SBarry Smith   PetscViewerFinalizePackage - This function destroys any global objects created in the Petsc viewers. It is
95c6c1daeSBarry Smith   called from PetscFinalize().
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith   Level: developer
125c6c1daeSBarry Smith 
135c6c1daeSBarry Smith .keywords: Petsc, destroy, package, mathematica
145c6c1daeSBarry Smith .seealso: PetscFinalize()
155c6c1daeSBarry Smith @*/
165c6c1daeSBarry Smith PetscErrorCode  PetscViewerFinalizePackage(void)
175c6c1daeSBarry Smith {
1837e93019SBarry Smith   PetscErrorCode ierr;
1937e93019SBarry Smith 
205c6c1daeSBarry Smith   PetscFunctionBegin;
21*b6dade52SBarry Smith   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
22*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_keyval);CHKERRQ(ierr);
23*b6dade52SBarry Smith   }
24*b6dade52SBarry Smith   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
25*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_Stdout_keyval);CHKERRQ(ierr);
26*b6dade52SBarry Smith   }
27*b6dade52SBarry Smith   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
28*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_Stderr_keyval);CHKERRQ(ierr);
29*b6dade52SBarry Smith   }
30*b6dade52SBarry Smith   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
31*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_Binary_keyval);CHKERRQ(ierr);
32*b6dade52SBarry Smith   }
33*b6dade52SBarry Smith   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
34*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_Draw_keyval);CHKERRQ(ierr);
35*b6dade52SBarry Smith   }
36*b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5)
37*b6dade52SBarry Smith   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
38*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_HDF5_keyval);CHKERRQ(ierr);
39*b6dade52SBarry Smith   }
40*b6dade52SBarry Smith #endif
41*b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER)
42*b6dade52SBarry Smith   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
43*b6dade52SBarry Smith     ierr = MPI_Keyval_free(&Petsc_Viewer_Socket_keyval);CHKERRQ(ierr);
44*b6dade52SBarry Smith   }
45*b6dade52SBarry Smith #endif
4637e93019SBarry Smith   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
475c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_FALSE;
480f51fdf8SToby Isaac   PetscViewerRegisterAllCalled  = PETSC_FALSE;
495c6c1daeSBarry Smith   PetscFunctionReturn(0);
505c6c1daeSBarry Smith }
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith /*@C
535c6c1daeSBarry Smith   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith   Level: developer
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith .keywords: Petsc, initialize, package
585c6c1daeSBarry Smith .seealso: PetscInitialize()
595c6c1daeSBarry Smith @*/
60607a6623SBarry Smith PetscErrorCode  PetscViewerInitializePackage(void)
615c6c1daeSBarry Smith {
625c6c1daeSBarry Smith   char           logList[256];
635c6c1daeSBarry Smith   char           *className;
645c6c1daeSBarry Smith   PetscBool      opt;
655c6c1daeSBarry Smith   PetscErrorCode ierr;
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith   PetscFunctionBegin;
685c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
695c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
705c6c1daeSBarry Smith   /* Register Classes */
715c6c1daeSBarry Smith   ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr);
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith   /* Register Constructors */
74607a6623SBarry Smith   ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith   /* Process info exclusions */
77c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
785c6c1daeSBarry Smith   if (opt) {
795c6c1daeSBarry Smith     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
805c6c1daeSBarry Smith     if (className) {
815c6c1daeSBarry Smith       ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr);
825c6c1daeSBarry Smith     }
835c6c1daeSBarry Smith   }
845c6c1daeSBarry Smith   /* Process summary exclusions */
857bf5a629SBarry Smith   ierr = PetscOptionsGetString(NULL,NULL, "-log_exclude", logList, 256, &opt);CHKERRQ(ierr);
865c6c1daeSBarry Smith   if (opt) {
875c6c1daeSBarry Smith     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
885c6c1daeSBarry Smith     if (className) {
895c6c1daeSBarry Smith       ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr);
905c6c1daeSBarry Smith     }
915c6c1daeSBarry Smith   }
925c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
93607a6623SBarry Smith   ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr);
945c6c1daeSBarry Smith #endif
955c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr);
965c6c1daeSBarry Smith   PetscFunctionReturn(0);
975c6c1daeSBarry Smith }
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith /*@
1005c6c1daeSBarry Smith    PetscViewerDestroy - Destroys a PetscViewer.
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith    Collective on PetscViewer
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith    Input Parameters:
1055c6c1daeSBarry Smith .  viewer - the PetscViewer to be destroyed.
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith    Level: beginner
1085c6c1daeSBarry Smith 
1095c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith @*/
1125c6c1daeSBarry Smith PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
1135c6c1daeSBarry Smith {
1145c6c1daeSBarry Smith   PetscErrorCode ierr;
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith   PetscFunctionBegin;
1175c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
1185c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1);
1195c6c1daeSBarry Smith 
1205c6c1daeSBarry Smith   ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr);
1215c6c1daeSBarry Smith   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);}
1225c6c1daeSBarry Smith 
123e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr);
1245c6c1daeSBarry Smith   if ((*viewer)->ops->destroy) {
1255c6c1daeSBarry Smith     ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr);
1265c6c1daeSBarry Smith   }
1275c6c1daeSBarry Smith   ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr);
1285c6c1daeSBarry Smith   PetscFunctionReturn(0);
1295c6c1daeSBarry Smith }
1305c6c1daeSBarry Smith 
131d7cbc13eSBarry Smith /*@C
132d7cbc13eSBarry Smith    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
133d7cbc13eSBarry Smith 
134d7cbc13eSBarry Smith    Collective on PetscViewer
135d7cbc13eSBarry Smith 
136d7cbc13eSBarry Smith    Input Parameters:
137d7cbc13eSBarry Smith +  viewer - the viewer
138d7cbc13eSBarry Smith -  format - the format
139d7cbc13eSBarry Smith 
140d7cbc13eSBarry Smith    Output Parameter:
141d7cbc13eSBarry Smith .   vf - viewer and format object
142d7cbc13eSBarry Smith 
143d7cbc13eSBarry Smith    Notes: This increases the reference count of the viewer so you can destroy the viewer object after this call
144d7cbc13eSBarry Smith    Level: developer
145d7cbc13eSBarry Smith 
146d7cbc13eSBarry Smith    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
147d7cbc13eSBarry Smith 
148d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
149d7cbc13eSBarry Smith 
150d7cbc13eSBarry Smith @*/
151d7cbc13eSBarry Smith PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
152d7cbc13eSBarry Smith {
153d7cbc13eSBarry Smith   PetscErrorCode ierr;
154d7cbc13eSBarry Smith 
155d7cbc13eSBarry Smith   PetscFunctionBegin;
156d7cbc13eSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
157d7cbc13eSBarry Smith   ierr = PetscNew(vf);CHKERRQ(ierr);
158d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
159d7cbc13eSBarry Smith   (*vf)->format = format;
160d7cbc13eSBarry Smith   PetscFunctionReturn(0);
161d7cbc13eSBarry Smith }
162d7cbc13eSBarry Smith 
163d7cbc13eSBarry Smith 
164fe01d993SBarry Smith /*@C
165fe01d993SBarry Smith    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
166fe01d993SBarry Smith 
167fe01d993SBarry Smith    Collective on PetscViewer
168fe01d993SBarry Smith 
169fe01d993SBarry Smith    Input Parameters:
170fe01d993SBarry Smith .  viewer - the PetscViewerAndFormat to be destroyed.
171fe01d993SBarry Smith 
172d7cbc13eSBarry Smith    Level: developer
173fe01d993SBarry Smith 
174d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
175fe01d993SBarry Smith 
176fe01d993SBarry Smith @*/
177fe01d993SBarry Smith PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
178fe01d993SBarry Smith {
179fe01d993SBarry Smith   PetscErrorCode ierr;
180fe01d993SBarry Smith 
181fe01d993SBarry Smith   PetscFunctionBegin;
182fe01d993SBarry Smith   ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr);
183fe01d993SBarry Smith   ierr = PetscFree(*vf);CHKERRQ(ierr);
184fe01d993SBarry Smith   PetscFunctionReturn(0);
185fe01d993SBarry Smith }
186fe01d993SBarry Smith 
1875c6c1daeSBarry Smith /*@C
1885c6c1daeSBarry Smith    PetscViewerGetType - Returns the type of a PetscViewer.
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith    Not Collective
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith    Input Parameter:
1935c6c1daeSBarry Smith .   viewer - the PetscViewer
1945c6c1daeSBarry Smith 
1955c6c1daeSBarry Smith    Output Parameter:
1965c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1975c6c1daeSBarry Smith 
1985c6c1daeSBarry Smith    Available Types Include:
1995c6c1daeSBarry Smith .  PETSCVIEWERSOCKET - Socket PetscViewer
2005c6c1daeSBarry Smith .  PETSCVIEWERASCII - ASCII PetscViewer
2015c6c1daeSBarry Smith .  PETSCVIEWERBINARY - binary file PetscViewer
2025c6c1daeSBarry Smith .  PETSCVIEWERSTRING - string PetscViewer
2035c6c1daeSBarry Smith .  PETSCVIEWERDRAW - drawing PetscViewer
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith    Level: intermediate
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith    Note:
2085c6c1daeSBarry Smith    See include/petscviewer.h for a complete list of PetscViewers.
2095c6c1daeSBarry Smith 
2105c6c1daeSBarry Smith    PetscViewerType is actually a string
2115c6c1daeSBarry Smith 
2125c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
2135c6c1daeSBarry Smith 
2145c6c1daeSBarry Smith @*/
2155c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
2165c6c1daeSBarry Smith {
2175c6c1daeSBarry Smith   PetscFunctionBegin;
2185c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2195c6c1daeSBarry Smith   PetscValidPointer(type,2);
2205c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
2215c6c1daeSBarry Smith   PetscFunctionReturn(0);
2225c6c1daeSBarry Smith }
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith /*@C
2255c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
2265c6c1daeSBarry Smith    PetscViewer options in the database.
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith    Logically Collective on PetscViewer
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith    Input Parameter:
2315c6c1daeSBarry Smith +  viewer - the PetscViewer context
2325c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith    Notes:
2355c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2365c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2375c6c1daeSBarry Smith 
2385c6c1daeSBarry Smith    Level: advanced
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith .keywords: PetscViewer, set, options, prefix, database
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith .seealso: PetscViewerSetFromOptions()
2435c6c1daeSBarry Smith @*/
2445c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
2455c6c1daeSBarry Smith {
2465c6c1daeSBarry Smith   PetscErrorCode ierr;
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith   PetscFunctionBegin;
2495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2505c6c1daeSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2515c6c1daeSBarry Smith   PetscFunctionReturn(0);
2525c6c1daeSBarry Smith }
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith /*@C
2555c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
2565c6c1daeSBarry Smith    PetscViewer options in the database.
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith    Logically Collective on PetscViewer
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith    Input Parameters:
2615c6c1daeSBarry Smith +  viewer - the PetscViewer context
2625c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith    Notes:
2655c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2665c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith    Level: advanced
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith .keywords: PetscViewer, append, options, prefix, database
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith .seealso: PetscViewerGetOptionsPrefix()
2735c6c1daeSBarry Smith @*/
2745c6c1daeSBarry Smith PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
2755c6c1daeSBarry Smith {
2765c6c1daeSBarry Smith   PetscErrorCode ierr;
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith   PetscFunctionBegin;
2795c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2805c6c1daeSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2815c6c1daeSBarry Smith   PetscFunctionReturn(0);
2825c6c1daeSBarry Smith }
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith /*@C
2855c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2865c6c1daeSBarry Smith    PetscViewer options in the database.
2875c6c1daeSBarry Smith 
2885c6c1daeSBarry Smith    Not Collective
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith    Input Parameter:
2915c6c1daeSBarry Smith .  viewer - the PetscViewer context
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith    Output Parameter:
2945c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith    Notes: On the fortran side, the user should pass in a string 'prefix' of
2975c6c1daeSBarry Smith    sufficient length to hold the prefix.
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith    Level: advanced
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith .keywords: PetscViewer, get, options, prefix, database
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith .seealso: PetscViewerAppendOptionsPrefix()
3045c6c1daeSBarry Smith @*/
3055c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
3065c6c1daeSBarry Smith {
3075c6c1daeSBarry Smith   PetscErrorCode ierr;
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith   PetscFunctionBegin;
3105c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3115c6c1daeSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
3125c6c1daeSBarry Smith   PetscFunctionReturn(0);
3135c6c1daeSBarry Smith }
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith /*@
3165c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
3175c6c1daeSBarry Smith 
3185c6c1daeSBarry Smith    Collective on PetscViewer
3195c6c1daeSBarry Smith 
3205c6c1daeSBarry Smith    Input Parameters:
3215c6c1daeSBarry Smith .  viewer - the PetscViewer context
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith    Notes:
3245c6c1daeSBarry Smith    For basic use of the PetscViewer classes the user need not explicitly call
3255c6c1daeSBarry Smith    PetscViewerSetUp(), since these actions will happen automatically.
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith    Level: advanced
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith .keywords: PetscViewer, setup
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerDestroy()
3325c6c1daeSBarry Smith @*/
3335c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
3345c6c1daeSBarry Smith {
335c98fd787SBarry Smith   PetscErrorCode ierr;
336c98fd787SBarry Smith 
3375c6c1daeSBarry Smith   PetscFunctionBegin;
3385c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
339c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
340c98fd787SBarry Smith   if (viewer->ops->setup) {
341c98fd787SBarry Smith     ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr);
342c98fd787SBarry Smith   }
343c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
3445c6c1daeSBarry Smith   PetscFunctionReturn(0);
3455c6c1daeSBarry Smith }
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith /*@C
3485c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3495c6c1daeSBarry Smith 
3505c6c1daeSBarry Smith    Collective on PetscViewer
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith    Input Parameters:
3535c6c1daeSBarry Smith +  v - the viewer
3545c6c1daeSBarry Smith -  viewer - visualization context
3555c6c1daeSBarry Smith 
3565c6c1daeSBarry Smith   Notes:
3575c6c1daeSBarry Smith   The available visualization contexts include
3585c6c1daeSBarry Smith +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
3595c6c1daeSBarry Smith .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
3605c6c1daeSBarry Smith         output where only the first processor opens
3615c6c1daeSBarry Smith         the file.  All other processors send their
3625c6c1daeSBarry Smith         data to the first processor to print.
3635c6c1daeSBarry Smith -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith    Level: beginner
3665c6c1daeSBarry Smith 
3676a9046bcSBarry Smith .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
3685c6c1daeSBarry Smith           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
3695c6c1daeSBarry Smith @*/
3705c6c1daeSBarry Smith PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
3715c6c1daeSBarry Smith {
3725c6c1daeSBarry Smith   PetscErrorCode    ierr;
3735c6c1daeSBarry Smith   PetscBool         iascii;
3745c6c1daeSBarry Smith   PetscViewerFormat format;
375e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
376536b137fSBarry Smith   PetscBool         issaws;
3770076e027SBarry Smith #endif
3785c6c1daeSBarry Smith 
3795c6c1daeSBarry Smith   PetscFunctionBegin;
3805c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3815c6c1daeSBarry Smith   PetscValidType(v,1);
3825c6c1daeSBarry Smith   if (!viewer) {
383ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
3845c6c1daeSBarry Smith   }
3855c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3865c6c1daeSBarry Smith   PetscCheckSameComm(v,1,viewer,2);
3875c6c1daeSBarry Smith 
3885c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
389e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
390536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
3910076e027SBarry Smith #endif
3925c6c1daeSBarry Smith   if (iascii) {
3935c6c1daeSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
394dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr);
39598c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
3962f234a98SBarry Smith       if (v->format) {
3972f234a98SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
3982f234a98SBarry Smith       }
3995c6c1daeSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
4002bf49c77SBarry Smith       if (v->ops->view) {
4012bf49c77SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
4025c6c1daeSBarry Smith       }
4035c6c1daeSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
4045c6c1daeSBarry Smith     }
405e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
406536b137fSBarry Smith   } else if (issaws) {
4070076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
408e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr);
4090076e027SBarry Smith       if (v->ops->view) {
4100076e027SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
4110076e027SBarry Smith       }
4120076e027SBarry Smith     }
4130076e027SBarry Smith #endif
4145c6c1daeSBarry Smith   }
4155c6c1daeSBarry Smith   PetscFunctionReturn(0);
4165c6c1daeSBarry Smith }
4171d641e7bSMichael Lange 
4181d641e7bSMichael Lange /*@C
4191d641e7bSMichael Lange    PetscViewerRead - Reads data from a PetscViewer
4201d641e7bSMichael Lange 
4211d641e7bSMichael Lange    Collective on MPI_Comm
4221d641e7bSMichael Lange 
4231d641e7bSMichael Lange    Input Parameters:
4241d641e7bSMichael Lange +  viewer   - The viewer
4251d641e7bSMichael Lange .  data     - Location to write the data
426060da220SMatthew G. Knepley .  num      - Number of items of data to read
4271d641e7bSMichael Lange -  datatype - Type of data to read
4281d641e7bSMichael Lange 
429f8e4bde8SMatthew G. Knepley    Output Parameters:
430060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
431f8e4bde8SMatthew G. Knepley 
432632e26b4SStefano Zampini    Notes:
433632e26b4SStefano Zampini    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
434632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
435632e26b4SStefano Zampini 
4361d641e7bSMichael Lange    Level: beginner
4371d641e7bSMichael Lange 
4381d641e7bSMichael Lange    Concepts: binary files, ascii files
4391d641e7bSMichael Lange 
4406a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
4411d641e7bSMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
4421d641e7bSMichael Lange           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
4431d641e7bSMichael Lange @*/
444060da220SMatthew G. Knepley PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
4451d641e7bSMichael Lange {
4461d641e7bSMichael Lange   PetscErrorCode ierr;
4471d641e7bSMichael Lange 
4481d641e7bSMichael Lange   PetscFunctionBegin;
4491d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4501d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
451060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4521d641e7bSMichael Lange     char *s = (char *)data;
453632e26b4SStefano Zampini     if (num >= 0) {
454060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4551d641e7bSMichael Lange         /* Skip leading whitespaces */
456060da220SMatthew G. Knepley         do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
457eb2700f0SMichael Lange         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
4581d641e7bSMichael Lange         i++;
4591d641e7bSMichael Lange         /* Read strings one char at a time */
460060da220SMatthew G. Knepley         do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
461eb2700f0SMichael 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');
4621d641e7bSMichael Lange         /* Terminate final string */
463060da220SMatthew G. Knepley         if (c == num-1) s[i-1] = '\0';
4641d641e7bSMichael Lange       }
465632e26b4SStefano Zampini     } else {
466632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
467632e26b4SStefano Zampini       do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (i == -num && !cnt) break;}
468632e26b4SStefano Zampini       while (s[i-1]!='\n');
469632e26b4SStefano Zampini       /* Terminate final string */
470632e26b4SStefano Zampini       s[i-1] = '\0';
471632e26b4SStefano Zampini       c      = i;
472632e26b4SStefano Zampini     }
473060da220SMatthew G. Knepley     if (count) *count = c;
474060da220SMatthew G. Knepley     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
4751d641e7bSMichael Lange   } else {
476060da220SMatthew G. Knepley     ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr);
4771d641e7bSMichael Lange   }
4781d641e7bSMichael Lange   PetscFunctionReturn(0);
4791d641e7bSMichael Lange }
480