xref: /petsc/src/sys/classes/viewer/interface/view.c (revision d8d19677bbccf95218448bee62e6b87f4513e133)
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 
145c6c1daeSBarry Smith .seealso: PetscFinalize()
155c6c1daeSBarry Smith @*/
165c6c1daeSBarry Smith PetscErrorCode  PetscViewerFinalizePackage(void)
175c6c1daeSBarry Smith {
1837e93019SBarry Smith   PetscErrorCode ierr;
1937e93019SBarry Smith 
205c6c1daeSBarry Smith   PetscFunctionBegin;
21b6dade52SBarry Smith   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
22ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_keyval);CHKERRMPI(ierr);
23b6dade52SBarry Smith   }
24b6dade52SBarry Smith   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
25ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);CHKERRMPI(ierr);
26b6dade52SBarry Smith   }
27b6dade52SBarry Smith   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
28ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);CHKERRMPI(ierr);
29b6dade52SBarry Smith   }
30b6dade52SBarry Smith   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
31ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);CHKERRMPI(ierr);
32b6dade52SBarry Smith   }
33b6dade52SBarry Smith   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
34ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);CHKERRMPI(ierr);
35b6dade52SBarry Smith   }
36b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5)
37b6dade52SBarry Smith   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
38ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);CHKERRMPI(ierr);
39b6dade52SBarry Smith   }
40b6dade52SBarry Smith #endif
41b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER)
42b6dade52SBarry Smith   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
43ffc4695bSBarry Smith     ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);CHKERRMPI(ierr);
44b6dade52SBarry Smith   }
45b6dade52SBarry 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 .seealso: PetscInitialize()
585c6c1daeSBarry Smith @*/
59607a6623SBarry Smith PetscErrorCode  PetscViewerInitializePackage(void)
605c6c1daeSBarry Smith {
615c6c1daeSBarry Smith   char           logList[256];
628e81d068SLisandro Dalcin   PetscBool      opt,pkg;
635c6c1daeSBarry Smith   PetscErrorCode ierr;
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith   PetscFunctionBegin;
665c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
675c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
685c6c1daeSBarry Smith   /* Register Classes */
695c6c1daeSBarry Smith   ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr);
705c6c1daeSBarry Smith   /* Register Constructors */
71607a6623SBarry Smith   ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
72e94e781bSJacob Faibussowitsch   /* Process Info */
73e94e781bSJacob Faibussowitsch   {
74e94e781bSJacob Faibussowitsch     PetscClassId  classids[1];
75e94e781bSJacob Faibussowitsch 
76e94e781bSJacob Faibussowitsch     classids[0] = PETSC_VIEWER_CLASSID;
77e94e781bSJacob Faibussowitsch     ierr = PetscInfoProcessClass("viewer", 1, classids);CHKERRQ(ierr);
785c6c1daeSBarry Smith   }
795c6c1daeSBarry Smith   /* Process summary exclusions */
808e81d068SLisandro Dalcin   ierr = PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr);
815c6c1daeSBarry Smith   if (opt) {
828e81d068SLisandro Dalcin     ierr = PetscStrInList("viewer",logList,',',&pkg);CHKERRQ(ierr);
83fa2bb9feSLisandro Dalcin     if (pkg) {ierr = PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);CHKERRQ(ierr);}
845c6c1daeSBarry Smith   }
855c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
86607a6623SBarry Smith   ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr);
875c6c1daeSBarry Smith #endif
888e81d068SLisandro Dalcin   /* Register package finalizer */
895c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr);
905c6c1daeSBarry Smith   PetscFunctionReturn(0);
915c6c1daeSBarry Smith }
925c6c1daeSBarry Smith 
935c6c1daeSBarry Smith /*@
945c6c1daeSBarry Smith    PetscViewerDestroy - Destroys a PetscViewer.
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith    Collective on PetscViewer
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith    Input Parameters:
995c6c1daeSBarry Smith .  viewer - the PetscViewer to be destroyed.
1005c6c1daeSBarry Smith 
1015c6c1daeSBarry Smith    Level: beginner
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
1045c6c1daeSBarry Smith 
1055c6c1daeSBarry Smith @*/
1065c6c1daeSBarry Smith PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
1075c6c1daeSBarry Smith {
1085c6c1daeSBarry Smith   PetscErrorCode ierr;
1095c6c1daeSBarry Smith 
1105c6c1daeSBarry Smith   PetscFunctionBegin;
1115c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
1125c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1);
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith   ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr);
11502c9f0b5SLisandro Dalcin   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = NULL; PetscFunctionReturn(0);}
1165c6c1daeSBarry Smith 
117e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr);
1185c6c1daeSBarry Smith   if ((*viewer)->ops->destroy) {
1195c6c1daeSBarry Smith     ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr);
1205c6c1daeSBarry Smith   }
1215c6c1daeSBarry Smith   ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr);
1225c6c1daeSBarry Smith   PetscFunctionReturn(0);
1235c6c1daeSBarry Smith }
1245c6c1daeSBarry Smith 
125d7cbc13eSBarry Smith /*@C
126d7cbc13eSBarry Smith    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
127d7cbc13eSBarry Smith 
128d7cbc13eSBarry Smith    Collective on PetscViewer
129d7cbc13eSBarry Smith 
130d7cbc13eSBarry Smith    Input Parameters:
131d7cbc13eSBarry Smith +  viewer - the viewer
132d7cbc13eSBarry Smith -  format - the format
133d7cbc13eSBarry Smith 
134d7cbc13eSBarry Smith    Output Parameter:
135d7cbc13eSBarry Smith .   vf - viewer and format object
136d7cbc13eSBarry Smith 
13795452b02SPatrick Sanan    Notes:
13895452b02SPatrick Sanan     This increases the reference count of the viewer so you can destroy the viewer object after this call
139d7cbc13eSBarry Smith    Level: developer
140d7cbc13eSBarry Smith 
141d7cbc13eSBarry Smith    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
142d7cbc13eSBarry Smith 
143d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
144d7cbc13eSBarry Smith 
145d7cbc13eSBarry Smith @*/
146d7cbc13eSBarry Smith PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
147d7cbc13eSBarry Smith {
148d7cbc13eSBarry Smith   PetscErrorCode ierr;
149d7cbc13eSBarry Smith 
150d7cbc13eSBarry Smith   PetscFunctionBegin;
151d7cbc13eSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
152d7cbc13eSBarry Smith   ierr = PetscNew(vf);CHKERRQ(ierr);
153d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
154d7cbc13eSBarry Smith   (*vf)->format = format;
155798534f6SMatthew G. Knepley   (*vf)->lg     = NULL;
156798534f6SMatthew G. Knepley   (*vf)->data   = NULL;
157d7cbc13eSBarry Smith   PetscFunctionReturn(0);
158d7cbc13eSBarry Smith }
159d7cbc13eSBarry Smith 
160fe01d993SBarry Smith /*@C
161fe01d993SBarry Smith    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
162fe01d993SBarry Smith 
163fe01d993SBarry Smith    Collective on PetscViewer
164fe01d993SBarry Smith 
165fe01d993SBarry Smith    Input Parameters:
166798534f6SMatthew G. Knepley .  vf - the PetscViewerAndFormat to be destroyed.
167fe01d993SBarry Smith 
168d7cbc13eSBarry Smith    Level: developer
169fe01d993SBarry Smith 
170d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
171fe01d993SBarry Smith @*/
172fe01d993SBarry Smith PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
173fe01d993SBarry Smith {
174fe01d993SBarry Smith   PetscErrorCode ierr;
175fe01d993SBarry Smith 
176fe01d993SBarry Smith   PetscFunctionBegin;
177fe01d993SBarry Smith   ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr);
178798534f6SMatthew G. Knepley   ierr = PetscDrawLGDestroy(&(*vf)->lg);CHKERRQ(ierr);
179fe01d993SBarry Smith   ierr = PetscFree(*vf);CHKERRQ(ierr);
180fe01d993SBarry Smith   PetscFunctionReturn(0);
181fe01d993SBarry Smith }
182fe01d993SBarry Smith 
1835c6c1daeSBarry Smith /*@C
1845c6c1daeSBarry Smith    PetscViewerGetType - Returns the type of a PetscViewer.
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith    Not Collective
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith    Input Parameter:
1895c6c1daeSBarry Smith .   viewer - the PetscViewer
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith    Output Parameter:
1925c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith    Available Types Include:
195a2b725a8SWilliam Gropp +  PETSCVIEWERSOCKET - Socket PetscViewer
1965c6c1daeSBarry Smith .  PETSCVIEWERASCII - ASCII PetscViewer
1975c6c1daeSBarry Smith .  PETSCVIEWERBINARY - binary file PetscViewer
1985c6c1daeSBarry Smith .  PETSCVIEWERSTRING - string PetscViewer
199a2b725a8SWilliam Gropp -  PETSCVIEWERDRAW - drawing PetscViewer
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith    Level: intermediate
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith    Note:
2045c6c1daeSBarry Smith    See include/petscviewer.h for a complete list of PetscViewers.
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith    PetscViewerType is actually a string
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
2095c6c1daeSBarry Smith 
2105c6c1daeSBarry Smith @*/
2115c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
2125c6c1daeSBarry Smith {
2135c6c1daeSBarry Smith   PetscFunctionBegin;
2145c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2155c6c1daeSBarry Smith   PetscValidPointer(type,2);
2165c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
2175c6c1daeSBarry Smith   PetscFunctionReturn(0);
2185c6c1daeSBarry Smith }
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith /*@C
2215c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
2225c6c1daeSBarry Smith    PetscViewer options in the database.
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith    Logically Collective on PetscViewer
2255c6c1daeSBarry Smith 
226*d8d19677SJose E. Roman    Input Parameters:
2275c6c1daeSBarry Smith +  viewer - the PetscViewer context
2285c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith    Notes:
2315c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2325c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith    Level: advanced
2355c6c1daeSBarry Smith 
2365c6c1daeSBarry Smith .seealso: PetscViewerSetFromOptions()
2375c6c1daeSBarry Smith @*/
2385c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
2395c6c1daeSBarry Smith {
2405c6c1daeSBarry Smith   PetscErrorCode ierr;
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith   PetscFunctionBegin;
2435c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2445c6c1daeSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2455c6c1daeSBarry Smith   PetscFunctionReturn(0);
2465c6c1daeSBarry Smith }
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith /*@C
2495c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
2505c6c1daeSBarry Smith    PetscViewer options in the database.
2515c6c1daeSBarry Smith 
2525c6c1daeSBarry Smith    Logically Collective on PetscViewer
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith    Input Parameters:
2555c6c1daeSBarry Smith +  viewer - the PetscViewer context
2565c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith    Notes:
2595c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2605c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith    Level: advanced
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith .seealso: PetscViewerGetOptionsPrefix()
2655c6c1daeSBarry Smith @*/
2665c6c1daeSBarry Smith PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
2675c6c1daeSBarry Smith {
2685c6c1daeSBarry Smith   PetscErrorCode ierr;
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith   PetscFunctionBegin;
2715c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2725c6c1daeSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2735c6c1daeSBarry Smith   PetscFunctionReturn(0);
2745c6c1daeSBarry Smith }
2755c6c1daeSBarry Smith 
2765c6c1daeSBarry Smith /*@C
2775c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2785c6c1daeSBarry Smith    PetscViewer options in the database.
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith    Not Collective
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith    Input Parameter:
2835c6c1daeSBarry Smith .  viewer - the PetscViewer context
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith    Output Parameter:
2865c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2875c6c1daeSBarry Smith 
28895452b02SPatrick Sanan    Notes:
28995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
2905c6c1daeSBarry Smith    sufficient length to hold the prefix.
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith    Level: advanced
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith .seealso: PetscViewerAppendOptionsPrefix()
2955c6c1daeSBarry Smith @*/
2965c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
2975c6c1daeSBarry Smith {
2985c6c1daeSBarry Smith   PetscErrorCode ierr;
2995c6c1daeSBarry Smith 
3005c6c1daeSBarry Smith   PetscFunctionBegin;
3015c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3025c6c1daeSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
3035c6c1daeSBarry Smith   PetscFunctionReturn(0);
3045c6c1daeSBarry Smith }
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith /*@
3075c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith    Collective on PetscViewer
3105c6c1daeSBarry Smith 
3115c6c1daeSBarry Smith    Input Parameters:
3125c6c1daeSBarry Smith .  viewer - the PetscViewer context
3135c6c1daeSBarry Smith 
3145c6c1daeSBarry Smith    Notes:
3155c6c1daeSBarry Smith    For basic use of the PetscViewer classes the user need not explicitly call
3165c6c1daeSBarry Smith    PetscViewerSetUp(), since these actions will happen automatically.
3175c6c1daeSBarry Smith 
3185c6c1daeSBarry Smith    Level: advanced
3195c6c1daeSBarry Smith 
3205c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerDestroy()
3215c6c1daeSBarry Smith @*/
3225c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
3235c6c1daeSBarry Smith {
324c98fd787SBarry Smith   PetscErrorCode ierr;
325c98fd787SBarry Smith 
3265c6c1daeSBarry Smith   PetscFunctionBegin;
3275c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
328c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
329c98fd787SBarry Smith   if (viewer->ops->setup) {
330c98fd787SBarry Smith     ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr);
331c98fd787SBarry Smith   }
332c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
3335c6c1daeSBarry Smith   PetscFunctionReturn(0);
3345c6c1daeSBarry Smith }
3355c6c1daeSBarry Smith 
3365c6c1daeSBarry Smith /*@C
337fe2efc57SMark    PetscViewerViewFromOptions - View from Options
338fe2efc57SMark 
339fe2efc57SMark    Collective on PetscViewer
340fe2efc57SMark 
341fe2efc57SMark    Input Parameters:
342fe2efc57SMark +  A - the PetscViewer context
343736c3998SJose E. Roman .  obj - Optional object
344736c3998SJose E. Roman -  name - command line option
345fe2efc57SMark 
346fe2efc57SMark    Level: intermediate
347fe2efc57SMark .seealso:  PetscViewer, PetscViewerView, PetscObjectViewFromOptions(), PetscViewerCreate()
348fe2efc57SMark @*/
349fe2efc57SMark PetscErrorCode  PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])
350fe2efc57SMark {
351fe2efc57SMark   PetscErrorCode ierr;
352fe2efc57SMark 
353fe2efc57SMark   PetscFunctionBegin;
354fe2efc57SMark   PetscValidHeaderSpecific(A,PETSC_VIEWER_CLASSID,1);
355fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
356fe2efc57SMark   PetscFunctionReturn(0);
357fe2efc57SMark }
358fe2efc57SMark 
359fe2efc57SMark /*@C
3605c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3615c6c1daeSBarry Smith 
3625c6c1daeSBarry Smith    Collective on PetscViewer
3635c6c1daeSBarry Smith 
3645c6c1daeSBarry Smith    Input Parameters:
365f0d4698bSVaclav Hapla +  v - the viewer to be viewed
3665c6c1daeSBarry Smith -  viewer - visualization context
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith   Notes:
3695c6c1daeSBarry Smith   The available visualization contexts include
3705c6c1daeSBarry Smith +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
3715c6c1daeSBarry Smith .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
3725c6c1daeSBarry Smith         output where only the first processor opens
3735c6c1daeSBarry Smith         the file.  All other processors send their
3745c6c1daeSBarry Smith         data to the first processor to print.
3755c6c1daeSBarry Smith -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
3765c6c1daeSBarry Smith 
3775c6c1daeSBarry Smith    Level: beginner
3785c6c1daeSBarry Smith 
3796a9046bcSBarry Smith .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
3805c6c1daeSBarry Smith           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
3815c6c1daeSBarry Smith @*/
3825c6c1daeSBarry Smith PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
3835c6c1daeSBarry Smith {
3845c6c1daeSBarry Smith   PetscErrorCode    ierr;
3855c6c1daeSBarry Smith   PetscBool         iascii;
3865c6c1daeSBarry Smith   PetscViewerFormat format;
387e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
388536b137fSBarry Smith   PetscBool         issaws;
3890076e027SBarry Smith #endif
3905c6c1daeSBarry Smith 
3915c6c1daeSBarry Smith   PetscFunctionBegin;
3925c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3935c6c1daeSBarry Smith   PetscValidType(v,1);
3945c6c1daeSBarry Smith   if (!viewer) {
395ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
3965c6c1daeSBarry Smith   }
3975c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3985c6c1daeSBarry Smith   PetscCheckSameComm(v,1,viewer,2);
3995c6c1daeSBarry Smith 
4005c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
401e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
402536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
4030076e027SBarry Smith #endif
4045c6c1daeSBarry Smith   if (iascii) {
4055c6c1daeSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
406dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr);
40798c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
4082f234a98SBarry Smith       if (v->format) {
4092f234a98SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
4102f234a98SBarry Smith       }
4115c6c1daeSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
4122bf49c77SBarry Smith       if (v->ops->view) {
4132bf49c77SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
4145c6c1daeSBarry Smith       }
4155c6c1daeSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
4165c6c1daeSBarry Smith     }
417e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
418536b137fSBarry Smith   } else if (issaws) {
4190076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
420e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr);
4210076e027SBarry Smith       if (v->ops->view) {
4220076e027SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
4230076e027SBarry Smith       }
4240076e027SBarry Smith     }
4250076e027SBarry Smith #endif
4265c6c1daeSBarry Smith   }
4275c6c1daeSBarry Smith   PetscFunctionReturn(0);
4285c6c1daeSBarry Smith }
4291d641e7bSMichael Lange 
4301d641e7bSMichael Lange /*@C
4311d641e7bSMichael Lange    PetscViewerRead - Reads data from a PetscViewer
4321d641e7bSMichael Lange 
433d083f849SBarry Smith    Collective
4341d641e7bSMichael Lange 
4351d641e7bSMichael Lange    Input Parameters:
4361d641e7bSMichael Lange +  viewer   - The viewer
4371d641e7bSMichael Lange .  data     - Location to write the data
438060da220SMatthew G. Knepley .  num      - Number of items of data to read
4391d641e7bSMichael Lange -  datatype - Type of data to read
4401d641e7bSMichael Lange 
441f8e4bde8SMatthew G. Knepley    Output Parameters:
442060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
443f8e4bde8SMatthew G. Knepley 
444632e26b4SStefano Zampini    Notes:
445632e26b4SStefano Zampini    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
446632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
447632e26b4SStefano Zampini 
4481d641e7bSMichael Lange    Level: beginner
4491d641e7bSMichael Lange 
4506a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
4511d641e7bSMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
4521d641e7bSMichael Lange           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
4531d641e7bSMichael Lange @*/
454060da220SMatthew G. Knepley PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
4551d641e7bSMichael Lange {
4561d641e7bSMichael Lange   PetscErrorCode ierr;
4571d641e7bSMichael Lange 
4581d641e7bSMichael Lange   PetscFunctionBegin;
4591d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4601d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
461060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4621d641e7bSMichael Lange     char *s = (char *)data;
463632e26b4SStefano Zampini     if (num >= 0) {
464060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4651d641e7bSMichael Lange         /* Skip leading whitespaces */
4669860990eSLisandro Dalcin         do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (!cnt) break;}
467eb2700f0SMichael Lange         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
4681d641e7bSMichael Lange         i++;
4691d641e7bSMichael Lange         /* Read strings one char at a time */
4709860990eSLisandro Dalcin         do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (!cnt) break;}
471eb2700f0SMichael 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');
4721d641e7bSMichael Lange         /* Terminate final string */
473060da220SMatthew G. Knepley         if (c == num-1) s[i-1] = '\0';
4741d641e7bSMichael Lange       }
475632e26b4SStefano Zampini     } else {
476632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
4779860990eSLisandro Dalcin       do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (i == -num || !cnt) break;}
478632e26b4SStefano Zampini       while (s[i-1]!='\n');
479632e26b4SStefano Zampini       /* Terminate final string */
480632e26b4SStefano Zampini       s[i-1] = '\0';
481632e26b4SStefano Zampini       c      = i;
482632e26b4SStefano Zampini     }
483060da220SMatthew G. Knepley     if (count) *count = c;
484060da220SMatthew G. Knepley     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
4851d641e7bSMichael Lange   } else {
486060da220SMatthew G. Knepley     ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr);
4871d641e7bSMichael Lange   }
4881d641e7bSMichael Lange   PetscFunctionReturn(0);
4891d641e7bSMichael Lange }
490e24fdd67SVaclav Hapla 
491af684e28SVaclav Hapla /*@
492af684e28SVaclav Hapla    PetscViewerReadable - Return a flag whether the viewer can be read from
493af684e28SVaclav Hapla 
494af684e28SVaclav Hapla    Not Collective
495af684e28SVaclav Hapla 
496af684e28SVaclav Hapla    Input Parameters:
497af684e28SVaclav Hapla .  viewer - the PetscViewer context
498af684e28SVaclav Hapla 
499af684e28SVaclav Hapla    Output Parameters:
500af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise
501af684e28SVaclav Hapla 
502af684e28SVaclav Hapla    Notes:
503af684e28SVaclav Hapla    PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
504af684e28SVaclav Hapla    and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
505af684e28SVaclav Hapla    returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
506af684e28SVaclav Hapla 
507af684e28SVaclav Hapla    Level: intermediate
508af684e28SVaclav Hapla 
509af684e28SVaclav Hapla .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
510af684e28SVaclav Hapla @*/
511e24fdd67SVaclav Hapla PetscErrorCode  PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
512e24fdd67SVaclav Hapla {
513e24fdd67SVaclav Hapla   PetscErrorCode    ierr;
514e24fdd67SVaclav Hapla   PetscFileMode     mode;
515e24fdd67SVaclav Hapla   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;
516e24fdd67SVaclav Hapla 
517e24fdd67SVaclav Hapla   PetscFunctionBegin;
518e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
519534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
520e24fdd67SVaclav Hapla   ierr = PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);CHKERRQ(ierr);
521e24fdd67SVaclav Hapla   *flg = PETSC_FALSE;
522e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
523e24fdd67SVaclav Hapla   ierr = (*f)(viewer, &mode);CHKERRQ(ierr);
524e24fdd67SVaclav Hapla   switch (mode) {
525e24fdd67SVaclav Hapla     case FILE_MODE_READ:
526e24fdd67SVaclav Hapla     case FILE_MODE_UPDATE:
527e24fdd67SVaclav Hapla     case FILE_MODE_APPEND_UPDATE:
528e24fdd67SVaclav Hapla       *flg = PETSC_TRUE;
529e24fdd67SVaclav Hapla     default: break;
530e24fdd67SVaclav Hapla   }
531e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
532e24fdd67SVaclav Hapla }
533e24fdd67SVaclav Hapla 
534af684e28SVaclav Hapla /*@
535af684e28SVaclav Hapla    PetscViewerWritable - Return a flag whether the viewer can be written to
536af684e28SVaclav Hapla 
537af684e28SVaclav Hapla    Not Collective
538af684e28SVaclav Hapla 
539af684e28SVaclav Hapla    Input Parameters:
540af684e28SVaclav Hapla .  viewer - the PetscViewer context
541af684e28SVaclav Hapla 
542af684e28SVaclav Hapla    Output Parameters:
543af684e28SVaclav Hapla .  flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise
544af684e28SVaclav Hapla 
545af684e28SVaclav Hapla    Notes:
546af684e28SVaclav Hapla    PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
547af684e28SVaclav Hapla    returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
548af684e28SVaclav Hapla 
549af684e28SVaclav Hapla    Level: intermediate
550af684e28SVaclav Hapla 
551af684e28SVaclav Hapla .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
552af684e28SVaclav Hapla @*/
553e24fdd67SVaclav Hapla PetscErrorCode  PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
554e24fdd67SVaclav Hapla {
555e24fdd67SVaclav Hapla   PetscErrorCode    ierr;
556e24fdd67SVaclav Hapla   PetscFileMode     mode;
557e24fdd67SVaclav Hapla   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;
558e24fdd67SVaclav Hapla 
559e24fdd67SVaclav Hapla   PetscFunctionBegin;
560e24fdd67SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
561534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
562e24fdd67SVaclav Hapla   ierr = PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);CHKERRQ(ierr);
563e24fdd67SVaclav Hapla   *flg = PETSC_TRUE;
564e24fdd67SVaclav Hapla   if (!f) PetscFunctionReturn(0);
565e24fdd67SVaclav Hapla   ierr = (*f)(viewer, &mode);CHKERRQ(ierr);
566e24fdd67SVaclav Hapla   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
567e24fdd67SVaclav Hapla   PetscFunctionReturn(0);
568e24fdd67SVaclav Hapla }
569e24fdd67SVaclav Hapla 
570af684e28SVaclav Hapla /*@
571af684e28SVaclav Hapla    PetscViewerCheckReadable - Check whether the viewer can be read from
572af684e28SVaclav Hapla 
573af684e28SVaclav Hapla    Collective
574af684e28SVaclav Hapla 
575af684e28SVaclav Hapla    Input Parameters:
576af684e28SVaclav Hapla .  viewer - the PetscViewer context
577af684e28SVaclav Hapla 
578af684e28SVaclav Hapla    Level: intermediate
579af684e28SVaclav Hapla 
580af684e28SVaclav Hapla .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
581af684e28SVaclav Hapla @*/
582d01f05b1SVaclav Hapla PetscErrorCode  PetscViewerCheckReadable(PetscViewer viewer)
583d01f05b1SVaclav Hapla {
584d01f05b1SVaclav Hapla   PetscBool         flg;
585d01f05b1SVaclav Hapla   PetscErrorCode    ierr;
586d01f05b1SVaclav Hapla 
587d01f05b1SVaclav Hapla   PetscFunctionBegin;
5880af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
589d01f05b1SVaclav Hapla   ierr = PetscViewerReadable(viewer, &flg);CHKERRQ(ierr);
590d01f05b1SVaclav Hapla   if (!flg) SETERRQ(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)");
591d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
592d01f05b1SVaclav Hapla }
593d01f05b1SVaclav Hapla 
594af684e28SVaclav Hapla /*@
595af684e28SVaclav Hapla    PetscViewerCheckWritable - Check whether the viewer can be written to
596af684e28SVaclav Hapla 
597af684e28SVaclav Hapla    Collective
598af684e28SVaclav Hapla 
599af684e28SVaclav Hapla    Input Parameters:
600af684e28SVaclav Hapla .  viewer - the PetscViewer context
601af684e28SVaclav Hapla 
602af684e28SVaclav Hapla    Level: intermediate
603af684e28SVaclav Hapla 
604af684e28SVaclav Hapla .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
605af684e28SVaclav Hapla @*/
606d01f05b1SVaclav Hapla PetscErrorCode  PetscViewerCheckWritable(PetscViewer viewer)
607d01f05b1SVaclav Hapla {
608d01f05b1SVaclav Hapla   PetscBool         flg;
609d01f05b1SVaclav Hapla   PetscErrorCode    ierr;
610d01f05b1SVaclav Hapla 
611d01f05b1SVaclav Hapla   PetscFunctionBegin;
6120af448b7SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
613d01f05b1SVaclav Hapla   ierr = PetscViewerWritable(viewer, &flg);CHKERRQ(ierr);
614d01f05b1SVaclav Hapla   if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
615d01f05b1SVaclav Hapla   PetscFunctionReturn(0);
616d01f05b1SVaclav Hapla }
617