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 8b6dade52SBarry 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 .seealso: PetscFinalize() 145c6c1daeSBarry Smith @*/ 155c6c1daeSBarry Smith PetscErrorCode PetscViewerFinalizePackage(void) 165c6c1daeSBarry Smith { 1737e93019SBarry Smith PetscErrorCode ierr; 1837e93019SBarry Smith 195c6c1daeSBarry Smith PetscFunctionBegin; 20b6dade52SBarry Smith if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) { 2147435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_keyval);CHKERRQ(ierr); 22b6dade52SBarry Smith } 23b6dade52SBarry Smith if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) { 2447435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);CHKERRQ(ierr); 25b6dade52SBarry Smith } 26b6dade52SBarry Smith if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) { 2747435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);CHKERRQ(ierr); 28b6dade52SBarry Smith } 29b6dade52SBarry Smith if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) { 3047435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);CHKERRQ(ierr); 31b6dade52SBarry Smith } 32b6dade52SBarry Smith if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) { 3347435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);CHKERRQ(ierr); 34b6dade52SBarry Smith } 35b6dade52SBarry Smith #if defined(PETSC_HAVE_HDF5) 36b6dade52SBarry Smith if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) { 3747435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);CHKERRQ(ierr); 38b6dade52SBarry Smith } 39b6dade52SBarry Smith #endif 40b6dade52SBarry Smith #if defined(PETSC_USE_SOCKETVIEWER) 41b6dade52SBarry Smith if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) { 4247435625SJed Brown ierr = MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);CHKERRQ(ierr); 43b6dade52SBarry Smith } 44b6dade52SBarry Smith #endif 4537e93019SBarry Smith ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr); 465c6c1daeSBarry Smith PetscViewerPackageInitialized = PETSC_FALSE; 470f51fdf8SToby Isaac PetscViewerRegisterAllCalled = PETSC_FALSE; 485c6c1daeSBarry Smith PetscFunctionReturn(0); 495c6c1daeSBarry Smith } 505c6c1daeSBarry Smith 515c6c1daeSBarry Smith /*@C 525c6c1daeSBarry Smith PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package. 535c6c1daeSBarry Smith 545c6c1daeSBarry Smith Level: developer 555c6c1daeSBarry Smith 565c6c1daeSBarry Smith .seealso: PetscInitialize() 575c6c1daeSBarry Smith @*/ 58607a6623SBarry Smith PetscErrorCode PetscViewerInitializePackage(void) 595c6c1daeSBarry Smith { 605c6c1daeSBarry Smith char logList[256]; 618e81d068SLisandro Dalcin PetscBool opt,pkg; 625c6c1daeSBarry Smith PetscErrorCode ierr; 635c6c1daeSBarry Smith 645c6c1daeSBarry Smith PetscFunctionBegin; 655c6c1daeSBarry Smith if (PetscViewerPackageInitialized) PetscFunctionReturn(0); 665c6c1daeSBarry Smith PetscViewerPackageInitialized = PETSC_TRUE; 675c6c1daeSBarry Smith /* Register Classes */ 685c6c1daeSBarry Smith ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr); 695c6c1daeSBarry Smith /* Register Constructors */ 70607a6623SBarry Smith ierr = PetscViewerRegisterAll();CHKERRQ(ierr); 715c6c1daeSBarry Smith /* Process info exclusions */ 728e81d068SLisandro Dalcin ierr = PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr); 735c6c1daeSBarry Smith if (opt) { 748e81d068SLisandro Dalcin ierr = PetscStrInList("viewer",logList,',',&pkg);CHKERRQ(ierr); 758e81d068SLisandro Dalcin if (pkg) {ierr = PetscInfoDeactivateClass(PETSC_VIEWER_CLASSID);CHKERRQ(ierr);} 765c6c1daeSBarry Smith } 775c6c1daeSBarry Smith /* Process summary exclusions */ 788e81d068SLisandro Dalcin ierr = PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr); 795c6c1daeSBarry Smith if (opt) { 808e81d068SLisandro Dalcin ierr = PetscStrInList("viewer",logList,',',&pkg);CHKERRQ(ierr); 81fa2bb9feSLisandro Dalcin if (pkg) {ierr = PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);CHKERRQ(ierr);} 825c6c1daeSBarry Smith } 835c6c1daeSBarry Smith #if defined(PETSC_HAVE_MATHEMATICA) 84607a6623SBarry Smith ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr); 855c6c1daeSBarry Smith #endif 868e81d068SLisandro Dalcin /* Register package finalizer */ 875c6c1daeSBarry Smith ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr); 885c6c1daeSBarry Smith PetscFunctionReturn(0); 895c6c1daeSBarry Smith } 905c6c1daeSBarry Smith 915c6c1daeSBarry Smith /*@ 925c6c1daeSBarry Smith PetscViewerDestroy - Destroys a PetscViewer. 935c6c1daeSBarry Smith 945c6c1daeSBarry Smith Collective on PetscViewer 955c6c1daeSBarry Smith 965c6c1daeSBarry Smith Input Parameters: 975c6c1daeSBarry Smith . viewer - the PetscViewer to be destroyed. 985c6c1daeSBarry Smith 995c6c1daeSBarry Smith Level: beginner 1005c6c1daeSBarry Smith 1015c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen() 1025c6c1daeSBarry Smith 1035c6c1daeSBarry Smith @*/ 1045c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy(PetscViewer *viewer) 1055c6c1daeSBarry Smith { 1065c6c1daeSBarry Smith PetscErrorCode ierr; 1075c6c1daeSBarry Smith 1085c6c1daeSBarry Smith PetscFunctionBegin; 1095c6c1daeSBarry Smith if (!*viewer) PetscFunctionReturn(0); 1105c6c1daeSBarry Smith PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1); 1115c6c1daeSBarry Smith 1125c6c1daeSBarry Smith ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr); 1135c6c1daeSBarry Smith if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);} 1145c6c1daeSBarry Smith 115e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr); 1165c6c1daeSBarry Smith if ((*viewer)->ops->destroy) { 1175c6c1daeSBarry Smith ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr); 1185c6c1daeSBarry Smith } 1195c6c1daeSBarry Smith ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr); 1205c6c1daeSBarry Smith PetscFunctionReturn(0); 1215c6c1daeSBarry Smith } 1225c6c1daeSBarry Smith 123d7cbc13eSBarry Smith /*@C 124d7cbc13eSBarry Smith PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct. 125d7cbc13eSBarry Smith 126d7cbc13eSBarry Smith Collective on PetscViewer 127d7cbc13eSBarry Smith 128d7cbc13eSBarry Smith Input Parameters: 129d7cbc13eSBarry Smith + viewer - the viewer 130d7cbc13eSBarry Smith - format - the format 131d7cbc13eSBarry Smith 132d7cbc13eSBarry Smith Output Parameter: 133d7cbc13eSBarry Smith . vf - viewer and format object 134d7cbc13eSBarry Smith 13595452b02SPatrick Sanan Notes: 13695452b02SPatrick Sanan This increases the reference count of the viewer so you can destroy the viewer object after this call 137d7cbc13eSBarry Smith Level: developer 138d7cbc13eSBarry Smith 139d7cbc13eSBarry Smith This is used as the context variable for many of the TS, SNES, and KSP monitor functions 140d7cbc13eSBarry Smith 141d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy() 142d7cbc13eSBarry Smith 143d7cbc13eSBarry Smith @*/ 144d7cbc13eSBarry Smith PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf) 145d7cbc13eSBarry Smith { 146d7cbc13eSBarry Smith PetscErrorCode ierr; 147d7cbc13eSBarry Smith 148d7cbc13eSBarry Smith PetscFunctionBegin; 149d7cbc13eSBarry Smith ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 150d7cbc13eSBarry Smith ierr = PetscNew(vf);CHKERRQ(ierr); 151d7cbc13eSBarry Smith (*vf)->viewer = viewer; 152d7cbc13eSBarry Smith (*vf)->format = format; 153d7cbc13eSBarry Smith PetscFunctionReturn(0); 154d7cbc13eSBarry Smith } 155d7cbc13eSBarry Smith 156d7cbc13eSBarry Smith 157fe01d993SBarry Smith /*@C 158fe01d993SBarry Smith PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct. 159fe01d993SBarry Smith 160fe01d993SBarry Smith Collective on PetscViewer 161fe01d993SBarry Smith 162fe01d993SBarry Smith Input Parameters: 163fe01d993SBarry Smith . viewer - the PetscViewerAndFormat to be destroyed. 164fe01d993SBarry Smith 165d7cbc13eSBarry Smith Level: developer 166fe01d993SBarry Smith 167d7cbc13eSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate() 168fe01d993SBarry Smith 169fe01d993SBarry Smith @*/ 170fe01d993SBarry Smith PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf) 171fe01d993SBarry Smith { 172fe01d993SBarry Smith PetscErrorCode ierr; 173fe01d993SBarry Smith 174fe01d993SBarry Smith PetscFunctionBegin; 175fe01d993SBarry Smith ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr); 176fe01d993SBarry Smith ierr = PetscFree(*vf);CHKERRQ(ierr); 177fe01d993SBarry Smith PetscFunctionReturn(0); 178fe01d993SBarry Smith } 179fe01d993SBarry Smith 1805c6c1daeSBarry Smith /*@C 1815c6c1daeSBarry Smith PetscViewerGetType - Returns the type of a PetscViewer. 1825c6c1daeSBarry Smith 1835c6c1daeSBarry Smith Not Collective 1845c6c1daeSBarry Smith 1855c6c1daeSBarry Smith Input Parameter: 1865c6c1daeSBarry Smith . viewer - the PetscViewer 1875c6c1daeSBarry Smith 1885c6c1daeSBarry Smith Output Parameter: 1895c6c1daeSBarry Smith . type - PetscViewer type (see below) 1905c6c1daeSBarry Smith 1915c6c1daeSBarry Smith Available Types Include: 1925c6c1daeSBarry Smith . PETSCVIEWERSOCKET - Socket PetscViewer 1935c6c1daeSBarry Smith . PETSCVIEWERASCII - ASCII PetscViewer 1945c6c1daeSBarry Smith . PETSCVIEWERBINARY - binary file PetscViewer 1955c6c1daeSBarry Smith . PETSCVIEWERSTRING - string PetscViewer 1965c6c1daeSBarry Smith . PETSCVIEWERDRAW - drawing PetscViewer 1975c6c1daeSBarry Smith 1985c6c1daeSBarry Smith Level: intermediate 1995c6c1daeSBarry Smith 2005c6c1daeSBarry Smith Note: 2015c6c1daeSBarry Smith See include/petscviewer.h for a complete list of PetscViewers. 2025c6c1daeSBarry Smith 2035c6c1daeSBarry Smith PetscViewerType is actually a string 2045c6c1daeSBarry Smith 2055c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 2065c6c1daeSBarry Smith 2075c6c1daeSBarry Smith @*/ 2085c6c1daeSBarry Smith PetscErrorCode PetscViewerGetType(PetscViewer viewer,PetscViewerType *type) 2095c6c1daeSBarry Smith { 2105c6c1daeSBarry Smith PetscFunctionBegin; 2115c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2125c6c1daeSBarry Smith PetscValidPointer(type,2); 2135c6c1daeSBarry Smith *type = ((PetscObject)viewer)->type_name; 2145c6c1daeSBarry Smith PetscFunctionReturn(0); 2155c6c1daeSBarry Smith } 2165c6c1daeSBarry Smith 2175c6c1daeSBarry Smith /*@C 2185c6c1daeSBarry Smith PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all 2195c6c1daeSBarry Smith PetscViewer options in the database. 2205c6c1daeSBarry Smith 2215c6c1daeSBarry Smith Logically Collective on PetscViewer 2225c6c1daeSBarry Smith 2235c6c1daeSBarry Smith Input Parameter: 2245c6c1daeSBarry Smith + viewer - the PetscViewer context 2255c6c1daeSBarry Smith - prefix - the prefix to prepend to all option names 2265c6c1daeSBarry Smith 2275c6c1daeSBarry Smith Notes: 2285c6c1daeSBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2295c6c1daeSBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 2305c6c1daeSBarry Smith 2315c6c1daeSBarry Smith Level: advanced 2325c6c1daeSBarry Smith 2335c6c1daeSBarry Smith .seealso: PetscViewerSetFromOptions() 2345c6c1daeSBarry Smith @*/ 2355c6c1daeSBarry Smith PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[]) 2365c6c1daeSBarry Smith { 2375c6c1daeSBarry Smith PetscErrorCode ierr; 2385c6c1daeSBarry Smith 2395c6c1daeSBarry Smith PetscFunctionBegin; 2405c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2415c6c1daeSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 2425c6c1daeSBarry Smith PetscFunctionReturn(0); 2435c6c1daeSBarry Smith } 2445c6c1daeSBarry Smith 2455c6c1daeSBarry Smith /*@C 2465c6c1daeSBarry Smith PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all 2475c6c1daeSBarry Smith PetscViewer options in the database. 2485c6c1daeSBarry Smith 2495c6c1daeSBarry Smith Logically Collective on PetscViewer 2505c6c1daeSBarry Smith 2515c6c1daeSBarry Smith Input Parameters: 2525c6c1daeSBarry Smith + viewer - the PetscViewer context 2535c6c1daeSBarry Smith - prefix - the prefix to prepend to all option names 2545c6c1daeSBarry Smith 2555c6c1daeSBarry Smith Notes: 2565c6c1daeSBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2575c6c1daeSBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 2585c6c1daeSBarry Smith 2595c6c1daeSBarry Smith Level: advanced 2605c6c1daeSBarry Smith 2615c6c1daeSBarry Smith .seealso: PetscViewerGetOptionsPrefix() 2625c6c1daeSBarry Smith @*/ 2635c6c1daeSBarry Smith PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[]) 2645c6c1daeSBarry Smith { 2655c6c1daeSBarry Smith PetscErrorCode ierr; 2665c6c1daeSBarry Smith 2675c6c1daeSBarry Smith PetscFunctionBegin; 2685c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2695c6c1daeSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 2705c6c1daeSBarry Smith PetscFunctionReturn(0); 2715c6c1daeSBarry Smith } 2725c6c1daeSBarry Smith 2735c6c1daeSBarry Smith /*@C 2745c6c1daeSBarry Smith PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all 2755c6c1daeSBarry Smith PetscViewer options in the database. 2765c6c1daeSBarry Smith 2775c6c1daeSBarry Smith Not Collective 2785c6c1daeSBarry Smith 2795c6c1daeSBarry Smith Input Parameter: 2805c6c1daeSBarry Smith . viewer - the PetscViewer context 2815c6c1daeSBarry Smith 2825c6c1daeSBarry Smith Output Parameter: 2835c6c1daeSBarry Smith . prefix - pointer to the prefix string used 2845c6c1daeSBarry Smith 28595452b02SPatrick Sanan Notes: 28695452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prefix' of 2875c6c1daeSBarry Smith sufficient length to hold the prefix. 2885c6c1daeSBarry Smith 2895c6c1daeSBarry Smith Level: advanced 2905c6c1daeSBarry Smith 2915c6c1daeSBarry Smith .seealso: PetscViewerAppendOptionsPrefix() 2925c6c1daeSBarry Smith @*/ 2935c6c1daeSBarry Smith PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[]) 2945c6c1daeSBarry Smith { 2955c6c1daeSBarry Smith PetscErrorCode ierr; 2965c6c1daeSBarry Smith 2975c6c1daeSBarry Smith PetscFunctionBegin; 2985c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2995c6c1daeSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 3005c6c1daeSBarry Smith PetscFunctionReturn(0); 3015c6c1daeSBarry Smith } 3025c6c1daeSBarry Smith 3035c6c1daeSBarry Smith /*@ 3045c6c1daeSBarry Smith PetscViewerSetUp - Sets up the internal viewer data structures for the later use. 3055c6c1daeSBarry Smith 3065c6c1daeSBarry Smith Collective on PetscViewer 3075c6c1daeSBarry Smith 3085c6c1daeSBarry Smith Input Parameters: 3095c6c1daeSBarry Smith . viewer - the PetscViewer context 3105c6c1daeSBarry Smith 3115c6c1daeSBarry Smith Notes: 3125c6c1daeSBarry Smith For basic use of the PetscViewer classes the user need not explicitly call 3135c6c1daeSBarry Smith PetscViewerSetUp(), since these actions will happen automatically. 3145c6c1daeSBarry Smith 3155c6c1daeSBarry Smith Level: advanced 3165c6c1daeSBarry Smith 3175c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerDestroy() 3185c6c1daeSBarry Smith @*/ 3195c6c1daeSBarry Smith PetscErrorCode PetscViewerSetUp(PetscViewer viewer) 3205c6c1daeSBarry Smith { 321c98fd787SBarry Smith PetscErrorCode ierr; 322c98fd787SBarry Smith 3235c6c1daeSBarry Smith PetscFunctionBegin; 3245c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 325c98fd787SBarry Smith if (viewer->setupcalled) PetscFunctionReturn(0); 326c98fd787SBarry Smith if (viewer->ops->setup) { 327c98fd787SBarry Smith ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr); 328c98fd787SBarry Smith } 329c98fd787SBarry Smith viewer->setupcalled = PETSC_TRUE; 3305c6c1daeSBarry Smith PetscFunctionReturn(0); 3315c6c1daeSBarry Smith } 3325c6c1daeSBarry Smith 3335c6c1daeSBarry Smith /*@C 3345c6c1daeSBarry Smith PetscViewerView - Visualizes a viewer object. 3355c6c1daeSBarry Smith 3365c6c1daeSBarry Smith Collective on PetscViewer 3375c6c1daeSBarry Smith 3385c6c1daeSBarry Smith Input Parameters: 339f0d4698bSVaclav Hapla + v - the viewer to be viewed 3405c6c1daeSBarry Smith - viewer - visualization context 3415c6c1daeSBarry Smith 3425c6c1daeSBarry Smith Notes: 3435c6c1daeSBarry Smith The available visualization contexts include 3445c6c1daeSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 3455c6c1daeSBarry Smith . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 3465c6c1daeSBarry Smith output where only the first processor opens 3475c6c1daeSBarry Smith the file. All other processors send their 3485c6c1daeSBarry Smith data to the first processor to print. 3495c6c1daeSBarry Smith - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 3505c6c1daeSBarry Smith 3515c6c1daeSBarry Smith Level: beginner 3525c6c1daeSBarry Smith 3536a9046bcSBarry Smith .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 3545c6c1daeSBarry Smith PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad() 3555c6c1daeSBarry Smith @*/ 3565c6c1daeSBarry Smith PetscErrorCode PetscViewerView(PetscViewer v,PetscViewer viewer) 3575c6c1daeSBarry Smith { 3585c6c1daeSBarry Smith PetscErrorCode ierr; 3595c6c1daeSBarry Smith PetscBool iascii; 3605c6c1daeSBarry Smith PetscViewerFormat format; 361e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 362536b137fSBarry Smith PetscBool issaws; 3630076e027SBarry Smith #endif 3645c6c1daeSBarry Smith 3655c6c1daeSBarry Smith PetscFunctionBegin; 3665c6c1daeSBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 3675c6c1daeSBarry Smith PetscValidType(v,1); 3685c6c1daeSBarry Smith if (!viewer) { 369ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr); 3705c6c1daeSBarry Smith } 3715c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 3725c6c1daeSBarry Smith PetscCheckSameComm(v,1,viewer,2); 3735c6c1daeSBarry Smith 3745c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 375e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 376536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 3770076e027SBarry Smith #endif 3785c6c1daeSBarry Smith if (iascii) { 3795c6c1daeSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 380dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr); 38198c3331eSBarry Smith if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 3822f234a98SBarry Smith if (v->format) { 3832f234a98SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr); 3842f234a98SBarry Smith } 3855c6c1daeSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 3862bf49c77SBarry Smith if (v->ops->view) { 3872bf49c77SBarry Smith ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 3885c6c1daeSBarry Smith } 3895c6c1daeSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 3905c6c1daeSBarry Smith } 391e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 392536b137fSBarry Smith } else if (issaws) { 3930076e027SBarry Smith if (!((PetscObject)v)->amsmem) { 394e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr); 3950076e027SBarry Smith if (v->ops->view) { 3960076e027SBarry Smith ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 3970076e027SBarry Smith } 3980076e027SBarry Smith } 3990076e027SBarry Smith #endif 4005c6c1daeSBarry Smith } 4015c6c1daeSBarry Smith PetscFunctionReturn(0); 4025c6c1daeSBarry Smith } 4031d641e7bSMichael Lange 4041d641e7bSMichael Lange /*@C 4051d641e7bSMichael Lange PetscViewerRead - Reads data from a PetscViewer 4061d641e7bSMichael Lange 407*d083f849SBarry Smith Collective 4081d641e7bSMichael Lange 4091d641e7bSMichael Lange Input Parameters: 4101d641e7bSMichael Lange + viewer - The viewer 4111d641e7bSMichael Lange . data - Location to write the data 412060da220SMatthew G. Knepley . num - Number of items of data to read 4131d641e7bSMichael Lange - datatype - Type of data to read 4141d641e7bSMichael Lange 415f8e4bde8SMatthew G. Knepley Output Parameters: 416060da220SMatthew G. Knepley . count - number of items of data actually read, or NULL 417f8e4bde8SMatthew G. Knepley 418632e26b4SStefano Zampini Notes: 419632e26b4SStefano Zampini If datatype is PETSC_STRING and num is negative, reads until a newline character is found, 420632e26b4SStefano Zampini until a maximum of (-num - 1) chars. 421632e26b4SStefano Zampini 4221d641e7bSMichael Lange Level: beginner 4231d641e7bSMichael Lange 4246a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 4251d641e7bSMichael Lange VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 4261d641e7bSMichael Lange PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer 4271d641e7bSMichael Lange @*/ 428060da220SMatthew G. Knepley PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype) 4291d641e7bSMichael Lange { 4301d641e7bSMichael Lange PetscErrorCode ierr; 4311d641e7bSMichael Lange 4321d641e7bSMichael Lange PetscFunctionBegin; 4331d641e7bSMichael Lange PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 4341d641e7bSMichael Lange if (dtype == PETSC_STRING) { 435060da220SMatthew G. Knepley PetscInt c, i = 0, cnt; 4361d641e7bSMichael Lange char *s = (char *)data; 437632e26b4SStefano Zampini if (num >= 0) { 438060da220SMatthew G. Knepley for (c = 0; c < num; c++) { 4391d641e7bSMichael Lange /* Skip leading whitespaces */ 4409860990eSLisandro Dalcin do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (!cnt) break;} 441eb2700f0SMichael Lange while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r'); 4421d641e7bSMichael Lange i++; 4431d641e7bSMichael Lange /* Read strings one char at a time */ 4449860990eSLisandro Dalcin do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (!cnt) break;} 445eb2700f0SMichael 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'); 4461d641e7bSMichael Lange /* Terminate final string */ 447060da220SMatthew G. Knepley if (c == num-1) s[i-1] = '\0'; 4481d641e7bSMichael Lange } 449632e26b4SStefano Zampini } else { 450632e26b4SStefano Zampini /* Read until a \n is encountered (-num is the max size allowed) */ 4519860990eSLisandro Dalcin do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (i == -num || !cnt) break;} 452632e26b4SStefano Zampini while (s[i-1]!='\n'); 453632e26b4SStefano Zampini /* Terminate final string */ 454632e26b4SStefano Zampini s[i-1] = '\0'; 455632e26b4SStefano Zampini c = i; 456632e26b4SStefano Zampini } 457060da220SMatthew G. Knepley if (count) *count = c; 458060da220SMatthew G. Knepley else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num); 4591d641e7bSMichael Lange } else { 460060da220SMatthew G. Knepley ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr); 4611d641e7bSMichael Lange } 4621d641e7bSMichael Lange PetscFunctionReturn(0); 4631d641e7bSMichael Lange } 464e24fdd67SVaclav Hapla 465af684e28SVaclav Hapla /*@ 466af684e28SVaclav Hapla PetscViewerReadable - Return a flag whether the viewer can be read from 467af684e28SVaclav Hapla 468af684e28SVaclav Hapla Not Collective 469af684e28SVaclav Hapla 470af684e28SVaclav Hapla Input Parameters: 471af684e28SVaclav Hapla . viewer - the PetscViewer context 472af684e28SVaclav Hapla 473af684e28SVaclav Hapla Output Parameters: 474af684e28SVaclav Hapla . flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise 475af684e28SVaclav Hapla 476af684e28SVaclav Hapla Notes: 477af684e28SVaclav Hapla PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY) 478af684e28SVaclav Hapla and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode() 479af684e28SVaclav Hapla returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE. 480af684e28SVaclav Hapla 481af684e28SVaclav Hapla Level: intermediate 482af684e28SVaclav Hapla 483af684e28SVaclav Hapla .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType() 484af684e28SVaclav Hapla @*/ 485e24fdd67SVaclav Hapla PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg) 486e24fdd67SVaclav Hapla { 487e24fdd67SVaclav Hapla PetscErrorCode ierr; 488e24fdd67SVaclav Hapla PetscFileMode mode; 489e24fdd67SVaclav Hapla PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL; 490e24fdd67SVaclav Hapla 491e24fdd67SVaclav Hapla PetscFunctionBegin; 492e24fdd67SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 493e24fdd67SVaclav Hapla PetscValidIntPointer(flg,2); 494e24fdd67SVaclav Hapla ierr = PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);CHKERRQ(ierr); 495e24fdd67SVaclav Hapla *flg = PETSC_FALSE; 496e24fdd67SVaclav Hapla if (!f) PetscFunctionReturn(0); 497e24fdd67SVaclav Hapla ierr = (*f)(viewer, &mode);CHKERRQ(ierr); 498e24fdd67SVaclav Hapla switch (mode) { 499e24fdd67SVaclav Hapla case FILE_MODE_READ: 500e24fdd67SVaclav Hapla case FILE_MODE_UPDATE: 501e24fdd67SVaclav Hapla case FILE_MODE_APPEND_UPDATE: 502e24fdd67SVaclav Hapla *flg = PETSC_TRUE; 503e24fdd67SVaclav Hapla default: break; 504e24fdd67SVaclav Hapla } 505e24fdd67SVaclav Hapla PetscFunctionReturn(0); 506e24fdd67SVaclav Hapla } 507e24fdd67SVaclav Hapla 508af684e28SVaclav Hapla /*@ 509af684e28SVaclav Hapla PetscViewerWritable - Return a flag whether the viewer can be written to 510af684e28SVaclav Hapla 511af684e28SVaclav Hapla Not Collective 512af684e28SVaclav Hapla 513af684e28SVaclav Hapla Input Parameters: 514af684e28SVaclav Hapla . viewer - the PetscViewer context 515af684e28SVaclav Hapla 516af684e28SVaclav Hapla Output Parameters: 517af684e28SVaclav Hapla . flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise 518af684e28SVaclav Hapla 519af684e28SVaclav Hapla Notes: 520af684e28SVaclav Hapla PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode() 521af684e28SVaclav Hapla returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE. 522af684e28SVaclav Hapla 523af684e28SVaclav Hapla Level: intermediate 524af684e28SVaclav Hapla 525af684e28SVaclav Hapla .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType() 526af684e28SVaclav Hapla @*/ 527e24fdd67SVaclav Hapla PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg) 528e24fdd67SVaclav Hapla { 529e24fdd67SVaclav Hapla PetscErrorCode ierr; 530e24fdd67SVaclav Hapla PetscFileMode mode; 531e24fdd67SVaclav Hapla PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL; 532e24fdd67SVaclav Hapla 533e24fdd67SVaclav Hapla PetscFunctionBegin; 534e24fdd67SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 535e24fdd67SVaclav Hapla PetscValidIntPointer(flg,2); 536e24fdd67SVaclav Hapla ierr = PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);CHKERRQ(ierr); 537e24fdd67SVaclav Hapla *flg = PETSC_TRUE; 538e24fdd67SVaclav Hapla if (!f) PetscFunctionReturn(0); 539e24fdd67SVaclav Hapla ierr = (*f)(viewer, &mode);CHKERRQ(ierr); 540e24fdd67SVaclav Hapla if (mode == FILE_MODE_READ) *flg = PETSC_FALSE; 541e24fdd67SVaclav Hapla PetscFunctionReturn(0); 542e24fdd67SVaclav Hapla } 543e24fdd67SVaclav Hapla 544af684e28SVaclav Hapla /*@ 545af684e28SVaclav Hapla PetscViewerCheckReadable - Check whether the viewer can be read from 546af684e28SVaclav Hapla 547af684e28SVaclav Hapla Collective 548af684e28SVaclav Hapla 549af684e28SVaclav Hapla Input Parameters: 550af684e28SVaclav Hapla . viewer - the PetscViewer context 551af684e28SVaclav Hapla 552af684e28SVaclav Hapla Level: intermediate 553af684e28SVaclav Hapla 554af684e28SVaclav Hapla .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType() 555af684e28SVaclav Hapla @*/ 556d01f05b1SVaclav Hapla PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer) 557d01f05b1SVaclav Hapla { 558d01f05b1SVaclav Hapla PetscBool flg; 559d01f05b1SVaclav Hapla PetscErrorCode ierr; 560d01f05b1SVaclav Hapla 561d01f05b1SVaclav Hapla PetscFunctionBegin; 5620af448b7SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 563d01f05b1SVaclav Hapla ierr = PetscViewerReadable(viewer, &flg);CHKERRQ(ierr); 564d01f05b1SVaclav 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)"); 565d01f05b1SVaclav Hapla PetscFunctionReturn(0); 566d01f05b1SVaclav Hapla } 567d01f05b1SVaclav Hapla 568af684e28SVaclav Hapla /*@ 569af684e28SVaclav Hapla PetscViewerCheckWritable - Check whether the viewer can be written to 570af684e28SVaclav Hapla 571af684e28SVaclav Hapla Collective 572af684e28SVaclav Hapla 573af684e28SVaclav Hapla Input Parameters: 574af684e28SVaclav Hapla . viewer - the PetscViewer context 575af684e28SVaclav Hapla 576af684e28SVaclav Hapla Level: intermediate 577af684e28SVaclav Hapla 578af684e28SVaclav Hapla .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType() 579af684e28SVaclav Hapla @*/ 580d01f05b1SVaclav Hapla PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer) 581d01f05b1SVaclav Hapla { 582d01f05b1SVaclav Hapla PetscBool flg; 583d01f05b1SVaclav Hapla PetscErrorCode ierr; 584d01f05b1SVaclav Hapla 585d01f05b1SVaclav Hapla PetscFunctionBegin; 5860af448b7SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 587d01f05b1SVaclav Hapla ierr = PetscViewerWritable(viewer, &flg);CHKERRQ(ierr); 588d01f05b1SVaclav Hapla if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode"); 589d01f05b1SVaclav Hapla PetscFunctionReturn(0); 590d01f05b1SVaclav Hapla } 591