xref: /petsc/src/sys/classes/viewer/interface/view.c (revision 632e26b4dbe2f987b673bf58ed705b94d17f0da8)
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
85c6c1daeSBarry Smith   PetscViewerFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. 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;
2137e93019SBarry Smith   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
225c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_FALSE;
230f51fdf8SToby Isaac   PetscViewerRegisterAllCalled  = PETSC_FALSE;
245c6c1daeSBarry Smith   PetscFunctionReturn(0);
255c6c1daeSBarry Smith }
265c6c1daeSBarry Smith 
275c6c1daeSBarry Smith /*@C
285c6c1daeSBarry Smith   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
295c6c1daeSBarry Smith 
305c6c1daeSBarry Smith   Level: developer
315c6c1daeSBarry Smith 
325c6c1daeSBarry Smith .keywords: Petsc, initialize, package
335c6c1daeSBarry Smith .seealso: PetscInitialize()
345c6c1daeSBarry Smith @*/
35607a6623SBarry Smith PetscErrorCode  PetscViewerInitializePackage(void)
365c6c1daeSBarry Smith {
375c6c1daeSBarry Smith   char           logList[256];
385c6c1daeSBarry Smith   char           *className;
395c6c1daeSBarry Smith   PetscBool      opt;
405c6c1daeSBarry Smith   PetscErrorCode ierr;
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith   PetscFunctionBegin;
435c6c1daeSBarry Smith   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
445c6c1daeSBarry Smith   PetscViewerPackageInitialized = PETSC_TRUE;
455c6c1daeSBarry Smith   /* Register Classes */
465c6c1daeSBarry Smith   ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr);
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith   /* Register Constructors */
49607a6623SBarry Smith   ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
505c6c1daeSBarry Smith 
515c6c1daeSBarry Smith   /* Process info exclusions */
52c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
535c6c1daeSBarry Smith   if (opt) {
545c6c1daeSBarry Smith     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
555c6c1daeSBarry Smith     if (className) {
565c6c1daeSBarry Smith       ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr);
575c6c1daeSBarry Smith     }
585c6c1daeSBarry Smith   }
595c6c1daeSBarry Smith   /* Process summary exclusions */
607bf5a629SBarry Smith   ierr = PetscOptionsGetString(NULL,NULL, "-log_exclude", logList, 256, &opt);CHKERRQ(ierr);
615c6c1daeSBarry Smith   if (opt) {
625c6c1daeSBarry Smith     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
635c6c1daeSBarry Smith     if (className) {
645c6c1daeSBarry Smith       ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr);
655c6c1daeSBarry Smith     }
665c6c1daeSBarry Smith   }
675c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA)
68607a6623SBarry Smith   ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr);
695c6c1daeSBarry Smith #endif
705c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr);
715c6c1daeSBarry Smith   PetscFunctionReturn(0);
725c6c1daeSBarry Smith }
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith /*@
755c6c1daeSBarry Smith    PetscViewerDestroy - Destroys a PetscViewer.
765c6c1daeSBarry Smith 
775c6c1daeSBarry Smith    Collective on PetscViewer
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith    Input Parameters:
805c6c1daeSBarry Smith .  viewer - the PetscViewer to be destroyed.
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith    Level: beginner
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith @*/
875c6c1daeSBarry Smith PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
885c6c1daeSBarry Smith {
895c6c1daeSBarry Smith   PetscErrorCode ierr;
905c6c1daeSBarry Smith 
915c6c1daeSBarry Smith   PetscFunctionBegin;
925c6c1daeSBarry Smith   if (!*viewer) PetscFunctionReturn(0);
935c6c1daeSBarry Smith   PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1);
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr);
965c6c1daeSBarry Smith   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);}
975c6c1daeSBarry Smith 
98e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr);
995c6c1daeSBarry Smith   if ((*viewer)->ops->destroy) {
1005c6c1daeSBarry Smith     ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr);
1015c6c1daeSBarry Smith   }
1025c6c1daeSBarry Smith   ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr);
1035c6c1daeSBarry Smith   PetscFunctionReturn(0);
1045c6c1daeSBarry Smith }
1055c6c1daeSBarry Smith 
106d7cbc13eSBarry Smith /*@C
107d7cbc13eSBarry Smith    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
108d7cbc13eSBarry Smith 
109d7cbc13eSBarry Smith    Collective on PetscViewer
110d7cbc13eSBarry Smith 
111d7cbc13eSBarry Smith    Input Parameters:
112d7cbc13eSBarry Smith +  viewer - the viewer
113d7cbc13eSBarry Smith -  format - the format
114d7cbc13eSBarry Smith 
115d7cbc13eSBarry Smith    Output Parameter:
116d7cbc13eSBarry Smith .   vf - viewer and format object
117d7cbc13eSBarry Smith 
118d7cbc13eSBarry Smith    Notes: This increases the reference count of the viewer so you can destroy the viewer object after this call
119d7cbc13eSBarry Smith    Level: developer
120d7cbc13eSBarry Smith 
121d7cbc13eSBarry Smith    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
122d7cbc13eSBarry Smith 
123d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
124d7cbc13eSBarry Smith 
125d7cbc13eSBarry Smith @*/
126d7cbc13eSBarry Smith PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
127d7cbc13eSBarry Smith {
128d7cbc13eSBarry Smith   PetscErrorCode ierr;
129d7cbc13eSBarry Smith 
130d7cbc13eSBarry Smith   PetscFunctionBegin;
131d7cbc13eSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
132d7cbc13eSBarry Smith   ierr = PetscNew(vf);CHKERRQ(ierr);
133d7cbc13eSBarry Smith   (*vf)->viewer = viewer;
134d7cbc13eSBarry Smith   (*vf)->format = format;
135d7cbc13eSBarry Smith   PetscFunctionReturn(0);
136d7cbc13eSBarry Smith }
137d7cbc13eSBarry Smith 
138d7cbc13eSBarry Smith 
139fe01d993SBarry Smith /*@C
140fe01d993SBarry Smith    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
141fe01d993SBarry Smith 
142fe01d993SBarry Smith    Collective on PetscViewer
143fe01d993SBarry Smith 
144fe01d993SBarry Smith    Input Parameters:
145fe01d993SBarry Smith .  viewer - the PetscViewerAndFormat to be destroyed.
146fe01d993SBarry Smith 
147d7cbc13eSBarry Smith    Level: developer
148fe01d993SBarry Smith 
149d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
150fe01d993SBarry Smith 
151fe01d993SBarry Smith @*/
152fe01d993SBarry Smith PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
153fe01d993SBarry Smith {
154fe01d993SBarry Smith   PetscErrorCode ierr;
155fe01d993SBarry Smith 
156fe01d993SBarry Smith   PetscFunctionBegin;
157fe01d993SBarry Smith   ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr);
158fe01d993SBarry Smith   ierr = PetscFree(*vf);CHKERRQ(ierr);
159fe01d993SBarry Smith   PetscFunctionReturn(0);
160fe01d993SBarry Smith }
161fe01d993SBarry Smith 
1625c6c1daeSBarry Smith /*@C
1635c6c1daeSBarry Smith    PetscViewerGetType - Returns the type of a PetscViewer.
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith    Not Collective
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith    Input Parameter:
1685c6c1daeSBarry Smith .   viewer - the PetscViewer
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith    Output Parameter:
1715c6c1daeSBarry Smith .  type - PetscViewer type (see below)
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith    Available Types Include:
1745c6c1daeSBarry Smith .  PETSCVIEWERSOCKET - Socket PetscViewer
1755c6c1daeSBarry Smith .  PETSCVIEWERASCII - ASCII PetscViewer
1765c6c1daeSBarry Smith .  PETSCVIEWERBINARY - binary file PetscViewer
1775c6c1daeSBarry Smith .  PETSCVIEWERSTRING - string PetscViewer
1785c6c1daeSBarry Smith .  PETSCVIEWERDRAW - drawing PetscViewer
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith    Level: intermediate
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith    Note:
1835c6c1daeSBarry Smith    See include/petscviewer.h for a complete list of PetscViewers.
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith    PetscViewerType is actually a string
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith @*/
1905c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
1915c6c1daeSBarry Smith {
1925c6c1daeSBarry Smith   PetscFunctionBegin;
1935c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1945c6c1daeSBarry Smith   PetscValidPointer(type,2);
1955c6c1daeSBarry Smith   *type = ((PetscObject)viewer)->type_name;
1965c6c1daeSBarry Smith   PetscFunctionReturn(0);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith /*@C
2005c6c1daeSBarry Smith    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
2015c6c1daeSBarry Smith    PetscViewer options in the database.
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith    Logically Collective on PetscViewer
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith    Input Parameter:
2065c6c1daeSBarry Smith +  viewer - the PetscViewer context
2075c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith    Notes:
2105c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2115c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2125c6c1daeSBarry Smith 
2135c6c1daeSBarry Smith    Level: advanced
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith .keywords: PetscViewer, set, options, prefix, database
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith .seealso: PetscViewerSetFromOptions()
2185c6c1daeSBarry Smith @*/
2195c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
2205c6c1daeSBarry Smith {
2215c6c1daeSBarry Smith   PetscErrorCode ierr;
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith   PetscFunctionBegin;
2245c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2255c6c1daeSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2265c6c1daeSBarry Smith   PetscFunctionReturn(0);
2275c6c1daeSBarry Smith }
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith /*@C
2305c6c1daeSBarry Smith    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
2315c6c1daeSBarry Smith    PetscViewer options in the database.
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith    Logically Collective on PetscViewer
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith    Input Parameters:
2365c6c1daeSBarry Smith +  viewer - the PetscViewer context
2375c6c1daeSBarry Smith -  prefix - the prefix to prepend to all option names
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith    Notes:
2405c6c1daeSBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2415c6c1daeSBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2425c6c1daeSBarry Smith 
2435c6c1daeSBarry Smith    Level: advanced
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith .keywords: PetscViewer, append, options, prefix, database
2465c6c1daeSBarry Smith 
2475c6c1daeSBarry Smith .seealso: PetscViewerGetOptionsPrefix()
2485c6c1daeSBarry Smith @*/
2495c6c1daeSBarry Smith PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
2505c6c1daeSBarry Smith {
2515c6c1daeSBarry Smith   PetscErrorCode ierr;
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith   PetscFunctionBegin;
2545c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2555c6c1daeSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2565c6c1daeSBarry Smith   PetscFunctionReturn(0);
2575c6c1daeSBarry Smith }
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith /*@C
2605c6c1daeSBarry Smith    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
2615c6c1daeSBarry Smith    PetscViewer options in the database.
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith    Not Collective
2645c6c1daeSBarry Smith 
2655c6c1daeSBarry Smith    Input Parameter:
2665c6c1daeSBarry Smith .  viewer - the PetscViewer context
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith    Output Parameter:
2695c6c1daeSBarry Smith .  prefix - pointer to the prefix string used
2705c6c1daeSBarry Smith 
2715c6c1daeSBarry Smith    Notes: On the fortran side, the user should pass in a string 'prefix' of
2725c6c1daeSBarry Smith    sufficient length to hold the prefix.
2735c6c1daeSBarry Smith 
2745c6c1daeSBarry Smith    Level: advanced
2755c6c1daeSBarry Smith 
2765c6c1daeSBarry Smith .keywords: PetscViewer, get, options, prefix, database
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith .seealso: PetscViewerAppendOptionsPrefix()
2795c6c1daeSBarry Smith @*/
2805c6c1daeSBarry Smith PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
2815c6c1daeSBarry Smith {
2825c6c1daeSBarry Smith   PetscErrorCode ierr;
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith   PetscFunctionBegin;
2855c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2865c6c1daeSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
2875c6c1daeSBarry Smith   PetscFunctionReturn(0);
2885c6c1daeSBarry Smith }
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith /*@
2915c6c1daeSBarry Smith    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith    Collective on PetscViewer
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith    Input Parameters:
2965c6c1daeSBarry Smith .  viewer - the PetscViewer context
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith    Notes:
2995c6c1daeSBarry Smith    For basic use of the PetscViewer classes the user need not explicitly call
3005c6c1daeSBarry Smith    PetscViewerSetUp(), since these actions will happen automatically.
3015c6c1daeSBarry Smith 
3025c6c1daeSBarry Smith    Level: advanced
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith .keywords: PetscViewer, setup
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerDestroy()
3075c6c1daeSBarry Smith @*/
3085c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
3095c6c1daeSBarry Smith {
310c98fd787SBarry Smith   PetscErrorCode ierr;
311c98fd787SBarry Smith 
3125c6c1daeSBarry Smith   PetscFunctionBegin;
3135c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
314c98fd787SBarry Smith   if (viewer->setupcalled) PetscFunctionReturn(0);
315c98fd787SBarry Smith   if (viewer->ops->setup) {
316c98fd787SBarry Smith     ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr);
317c98fd787SBarry Smith   }
318c98fd787SBarry Smith   viewer->setupcalled = PETSC_TRUE;
3195c6c1daeSBarry Smith   PetscFunctionReturn(0);
3205c6c1daeSBarry Smith }
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith /*@C
3235c6c1daeSBarry Smith    PetscViewerView - Visualizes a viewer object.
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith    Collective on PetscViewer
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith    Input Parameters:
3285c6c1daeSBarry Smith +  v - the viewer
3295c6c1daeSBarry Smith -  viewer - visualization context
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith   Notes:
3325c6c1daeSBarry Smith   The available visualization contexts include
3335c6c1daeSBarry Smith +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
3345c6c1daeSBarry Smith .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
3355c6c1daeSBarry Smith         output where only the first processor opens
3365c6c1daeSBarry Smith         the file.  All other processors send their
3375c6c1daeSBarry Smith         data to the first processor to print.
3385c6c1daeSBarry Smith -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith    Level: beginner
3415c6c1daeSBarry Smith 
3426a9046bcSBarry Smith .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
3435c6c1daeSBarry Smith           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
3445c6c1daeSBarry Smith @*/
3455c6c1daeSBarry Smith PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
3465c6c1daeSBarry Smith {
3475c6c1daeSBarry Smith   PetscErrorCode    ierr;
3485c6c1daeSBarry Smith   PetscBool         iascii;
3495c6c1daeSBarry Smith   PetscViewerFormat format;
350e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
351536b137fSBarry Smith   PetscBool         issaws;
3520076e027SBarry Smith #endif
3535c6c1daeSBarry Smith 
3545c6c1daeSBarry Smith   PetscFunctionBegin;
3555c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3565c6c1daeSBarry Smith   PetscValidType(v,1);
3575c6c1daeSBarry Smith   if (!viewer) {
358ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
3595c6c1daeSBarry Smith   }
3605c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3615c6c1daeSBarry Smith   PetscCheckSameComm(v,1,viewer,2);
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
364e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
365536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
3660076e027SBarry Smith #endif
3675c6c1daeSBarry Smith   if (iascii) {
3685c6c1daeSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
369dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr);
37098c3331eSBarry Smith     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
3712f234a98SBarry Smith       if (v->format) {
3722f234a98SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
3732f234a98SBarry Smith       }
3745c6c1daeSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
3752bf49c77SBarry Smith       if (v->ops->view) {
3762bf49c77SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
3775c6c1daeSBarry Smith       }
3785c6c1daeSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
3795c6c1daeSBarry Smith     }
380e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
381536b137fSBarry Smith   } else if (issaws) {
3820076e027SBarry Smith     if (!((PetscObject)v)->amsmem) {
383e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr);
3840076e027SBarry Smith       if (v->ops->view) {
3850076e027SBarry Smith         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
3860076e027SBarry Smith       }
3870076e027SBarry Smith     }
3880076e027SBarry Smith #endif
3895c6c1daeSBarry Smith   }
3905c6c1daeSBarry Smith   PetscFunctionReturn(0);
3915c6c1daeSBarry Smith }
3921d641e7bSMichael Lange 
3931d641e7bSMichael Lange /*@C
3941d641e7bSMichael Lange    PetscViewerRead - Reads data from a PetscViewer
3951d641e7bSMichael Lange 
3961d641e7bSMichael Lange    Collective on MPI_Comm
3971d641e7bSMichael Lange 
3981d641e7bSMichael Lange    Input Parameters:
3991d641e7bSMichael Lange +  viewer   - The viewer
4001d641e7bSMichael Lange .  data     - Location to write the data
401060da220SMatthew G. Knepley .  num      - Number of items of data to read
4021d641e7bSMichael Lange -  datatype - Type of data to read
4031d641e7bSMichael Lange 
404f8e4bde8SMatthew G. Knepley    Output Parameters:
405060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
406f8e4bde8SMatthew G. Knepley 
407*632e26b4SStefano Zampini    Notes:
408*632e26b4SStefano Zampini    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
409*632e26b4SStefano Zampini    until a maximum of (-num - 1) chars.
410*632e26b4SStefano Zampini 
4111d641e7bSMichael Lange    Level: beginner
4121d641e7bSMichael Lange 
4131d641e7bSMichael Lange    Concepts: binary files, ascii files
4141d641e7bSMichael Lange 
4156a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
4161d641e7bSMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
4171d641e7bSMichael Lange           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
4181d641e7bSMichael Lange @*/
419060da220SMatthew G. Knepley PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
4201d641e7bSMichael Lange {
4211d641e7bSMichael Lange   PetscErrorCode ierr;
4221d641e7bSMichael Lange 
4231d641e7bSMichael Lange   PetscFunctionBegin;
4241d641e7bSMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4251d641e7bSMichael Lange   if (dtype == PETSC_STRING) {
426060da220SMatthew G. Knepley     PetscInt c, i = 0, cnt;
4271d641e7bSMichael Lange     char *s = (char *)data;
428*632e26b4SStefano Zampini     if (num >= 0) {
429060da220SMatthew G. Knepley       for (c = 0; c < num; c++) {
4301d641e7bSMichael Lange         /* Skip leading whitespaces */
431060da220SMatthew G. Knepley         do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
432eb2700f0SMichael Lange         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
4331d641e7bSMichael Lange         i++;
4341d641e7bSMichael Lange         /* Read strings one char at a time */
435060da220SMatthew G. Knepley         do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
436eb2700f0SMichael 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');
4371d641e7bSMichael Lange         /* Terminate final string */
438060da220SMatthew G. Knepley         if (c == num-1) s[i-1] = '\0';
4391d641e7bSMichael Lange       }
440*632e26b4SStefano Zampini     } else {
441*632e26b4SStefano Zampini       /* Read until a \n is encountered (-num is the max size allowed) */
442*632e26b4SStefano Zampini       do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (i == -num && !cnt) break;}
443*632e26b4SStefano Zampini       while (s[i-1]!='\n');
444*632e26b4SStefano Zampini       /* Terminate final string */
445*632e26b4SStefano Zampini       s[i-1] = '\0';
446*632e26b4SStefano Zampini       c      = i;
447*632e26b4SStefano Zampini     }
448060da220SMatthew G. Knepley     if (count) *count = c;
449060da220SMatthew G. Knepley     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
4501d641e7bSMichael Lange   } else {
451060da220SMatthew G. Knepley     ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr);
4521d641e7bSMichael Lange   }
4531d641e7bSMichael Lange   PetscFunctionReturn(0);
4541d641e7bSMichael Lange }
455