xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 9af95d99258ce94b0f9e45befdb85990aec81b31)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
4e04113cfSBarry Smith #include <petscviewersaws.h>
5bfb97211SBarry Smith #endif
65c6c1daeSBarry Smith 
7140e18c1SBarry Smith PetscFunctionList PetscViewerList = 0;
85c6c1daeSBarry Smith 
994d6a431SBarry Smith /*
1094d6a431SBarry Smith   A standard run of an implicit TS generated 60 help message hashes,
1194d6a431SBarry Smith   thus we reserve room for 5 combinations of prefixes. If that is not
1294d6a431SBarry Smith   enough then any additional help messages will be printed multiple times.
1394d6a431SBarry Smith */
1494d6a431SBarry Smith #define PETSCSTRINGHASHSIZE 300
1594d6a431SBarry Smith typedef struct {
1694d6a431SBarry Smith   int           cnt;
1794d6a431SBarry Smith   unsigned long hashes[PETSCSTRINGHASHSIZE];
1894d6a431SBarry Smith } PetscStrHash;
1994d6a431SBarry Smith 
20*9af95d99SBarry Smith #include "../src/sys/utils/hash.h"
21*9af95d99SBarry Smith 
22*9af95d99SBarry Smith KHASH_SET_INIT_STR(PetscOptionsGetViewerPrinted)
23*9af95d99SBarry Smith static khash_t(PetscOptionsGetViewerPrinted) *PrintedOptions = NULL;
24*9af95d99SBarry Smith static PetscSegBuffer strings = NULL;
25*9af95d99SBarry Smith 
26*9af95d99SBarry Smith static PetscErrorCode PetscOptionsGetViewerPrintedDestroy(void)
2794d6a431SBarry Smith {
28*9af95d99SBarry Smith   PetscErrorCode ierr;
29*9af95d99SBarry Smith   kh_destroy(PetscOptionsGetViewerPrinted,PrintedOptions);
30*9af95d99SBarry Smith   ierr = PetscSegBufferDestroy(&strings);CHKERRQ(ierr);
31*9af95d99SBarry Smith   return 0;
3294d6a431SBarry Smith }
3394d6a431SBarry Smith 
342bf49c77SBarry Smith #undef __FUNCT__
352bf49c77SBarry Smith #define __FUNCT__ "PetscOptionsGetViewer"
362bf49c77SBarry Smith /*@C
372bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
382bf49c77SBarry Smith 
392bf49c77SBarry Smith    Collective on MPI_Comm
402bf49c77SBarry Smith 
412bf49c77SBarry Smith    Input Parameters:
422bf49c77SBarry Smith +  comm - the communicator to own the viewer
430298fd71SBarry Smith .  pre - the string to prepend to the name or NULL
442bf49c77SBarry Smith -  name - the option one is seeking
452bf49c77SBarry Smith 
462bf49c77SBarry Smith    Output Parameter:
47bb1d7374SBarry Smith +  viewer - the viewer, pass NULL if not needed
48bb1d7374SBarry Smith .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
492bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
502bf49c77SBarry Smith 
512bf49c77SBarry Smith    Level: intermediate
522bf49c77SBarry Smith 
532bf49c77SBarry Smith    Notes: If no value is provided ascii:stdout is used
54d1da0b69SBarry Smith $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
55d1da0b69SBarry Smith                                                   for example ascii::ascii_info prints just the information about the object not all details
56d1da0b69SBarry Smith                                                   unless :append is given filename opens in write mode, overwriting what was already there
57d1da0b69SBarry Smith $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
5820610d12SBarry Smith $       draw[:drawtype]                           for example, draw:tikz  or draw:x
592bf49c77SBarry Smith $       socket[:port]                             defaults to the standard output port
602a359c20SBarry Smith $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
612bf49c77SBarry Smith 
62cffb1e40SBarry Smith    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
632bf49c77SBarry Smith 
6427b0f280SBarry Smith    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
6527b0f280SBarry Smith 
662bf49c77SBarry Smith .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
672bf49c77SBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
682bf49c77SBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
692bf49c77SBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
702bf49c77SBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
712bf49c77SBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
72a264d7a6SBarry Smith           PetscOptionsFList(), PetscOptionsEList()
732bf49c77SBarry Smith @*/
74cffb1e40SBarry Smith PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
752bf49c77SBarry Smith {
762bf49c77SBarry Smith   char                 *value;
772bf49c77SBarry Smith   PetscErrorCode       ierr;
7820610d12SBarry Smith   PetscBool            flag,hashelp;
792bf49c77SBarry Smith 
802bf49c77SBarry Smith   PetscFunctionBegin;
812bf49c77SBarry Smith   PetscValidCharPointer(name,3);
822bf49c77SBarry Smith 
8327b0f280SBarry Smith   if (set) *set = PETSC_FALSE;
8427b0f280SBarry Smith #if defined(PETSC_SKIP_VIEWFROMOPTIONS)
8527b0f280SBarry Smith   PetscFunctionReturn(0);
8627b0f280SBarry Smith #endif
8727b0f280SBarry Smith 
88c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-help",&hashelp);CHKERRQ(ierr);
8920610d12SBarry Smith   if (hashelp) {
90*9af95d99SBarry Smith     khint_t newitem;
91*9af95d99SBarry Smith     if (!PrintedOptions) {
92*9af95d99SBarry Smith       PrintedOptions = kh_init(PetscOptionsGetViewerPrinted);
93*9af95d99SBarry Smith       ierr = PetscSegBufferCreate(sizeof(char),10000,&strings);CHKERRQ(ierr);
94*9af95d99SBarry Smith       ierr = PetscRegisterFinalize(PetscOptionsGetViewerPrintedDestroy);CHKERRQ(ierr);
95*9af95d99SBarry Smith     }
96*9af95d99SBarry Smith     if (!pre) {
97*9af95d99SBarry Smith       kh_put(PetscOptionsGetViewerPrinted,PrintedOptions,name+1,&newitem);
98*9af95d99SBarry Smith     } else {
99*9af95d99SBarry Smith       size_t l1,l2;
100*9af95d99SBarry Smith       char   *both;
101*9af95d99SBarry Smith 
102*9af95d99SBarry Smith       ierr = PetscStrlen(pre,&l1);CHKERRQ(ierr);
103*9af95d99SBarry Smith       ierr = PetscStrlen(name+1,&l2);CHKERRQ(ierr);
104*9af95d99SBarry Smith       ierr = PetscSegBufferGet(strings,l1+l2+1,&both);CHKERRQ(ierr);
105*9af95d99SBarry Smith       ierr = PetscStrcpy(both,pre);CHKERRQ(ierr);
106*9af95d99SBarry Smith       ierr = PetscStrcat(both,name+1);CHKERRQ(ierr);
107*9af95d99SBarry Smith       kh_put(PetscOptionsGetViewerPrinted,PrintedOptions,both,&newitem);
108*9af95d99SBarry Smith     }
109*9af95d99SBarry Smith     if (newitem) {
11094d6a431SBarry Smith       if (viewer) {
11194d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"\n  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Prints object to stdout or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
11294d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"    -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Saves object to a binary file","PetscOptionsGetViewer");CHKERRQ(ierr);
11394d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"    -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Draws object","PetscOptionsGetViewer");CHKERRQ(ierr);
11494d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"    -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Pushes object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
11594d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"    -%s%s saws[:communicatorname]: %s (%s)\n\n",pre ? pre : "",name+1,"Publishes object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
11694d6a431SBarry Smith       } else {
11794d6a431SBarry Smith         ierr = (*PetscHelpPrintf)(comm,"    -%s%s\n",pre ? pre : "",name+1);CHKERRQ(ierr);
11894d6a431SBarry Smith       }
11994d6a431SBarry Smith     }
12020610d12SBarry Smith   }
121685405a1SBarry Smith 
122e3f3e4b6SBarry Smith   if (format) *format = PETSC_VIEWER_DEFAULT;
123c5929fdfSBarry Smith   ierr = PetscOptionsFindPair_Private(NULL,pre,name,&value,&flag);CHKERRQ(ierr);
1242bf49c77SBarry Smith   if (flag) {
1252bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
1262bf49c77SBarry Smith     if (!value) {
127bb1d7374SBarry Smith       if (viewer) {
1282bf49c77SBarry Smith         ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
129706a11cbSBarry Smith         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
130bb1d7374SBarry Smith       }
1312bf49c77SBarry Smith     } else {
13235d27ee3SJed Brown       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
1332bf49c77SBarry Smith       PetscInt   cnt;
134a75e6a4aSMatthew G. Knepley       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};
1352bf49c77SBarry Smith 
13635d27ee3SJed Brown       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
13735d27ee3SJed Brown       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
13835d27ee3SJed Brown       if (loc1_fname) {
13935d27ee3SJed Brown         *loc1_fname++ = 0;
14035d27ee3SJed Brown         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
14135d27ee3SJed Brown       }
14235d27ee3SJed Brown       if (loc2_fmt) {
14335d27ee3SJed Brown         *loc2_fmt++ = 0;
14435d27ee3SJed Brown         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
14535d27ee3SJed Brown       }
14635d27ee3SJed Brown       if (loc3_fmode) *loc3_fmode++ = 0;
14735d27ee3SJed Brown       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
14835d27ee3SJed Brown       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
149bb1d7374SBarry Smith       if (viewer) {
15035d27ee3SJed Brown         if (!loc1_fname) {
15143b63833SBarry Smith           switch (cnt) {
15243b63833SBarry Smith           case 0:
1532bf49c77SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
15443b63833SBarry Smith             break;
15543b63833SBarry Smith           case 1:
156aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
15743b63833SBarry Smith             break;
15843b63833SBarry Smith           case 2:
159aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
16043b63833SBarry Smith             break;
161b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
16243b63833SBarry Smith           case 3:
163aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
16443b63833SBarry Smith             break;
165b58ca069SBarry Smith #endif
16643b63833SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
16743b63833SBarry Smith           case 4:
168aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
16943b63833SBarry Smith             break;
17043b63833SBarry Smith #endif
171e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
172bfb97211SBarry Smith           case 5:
173e04113cfSBarry Smith             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
174bfb97211SBarry Smith             break;
175bfb97211SBarry Smith #endif
176a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
177a75e6a4aSMatthew G. Knepley           case 7:
178a75e6a4aSMatthew G. Knepley             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
179a75e6a4aSMatthew G. Knepley             break;
180a75e6a4aSMatthew G. Knepley #endif
181aa1c909bSJed Brown           default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
1827f677774SBarry Smith           }
183706a11cbSBarry Smith           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
1847f677774SBarry Smith         } else {
18535d27ee3SJed Brown           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
1867f677774SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
187706a11cbSBarry Smith             ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
1887f677774SBarry Smith           } else {
1893550efbcSJed Brown             PetscFileMode fmode;
1902bf49c77SBarry Smith             ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
19135d27ee3SJed Brown             ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
1923550efbcSJed Brown             fmode = FILE_MODE_WRITE;
1933550efbcSJed Brown             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
19435d27ee3SJed Brown               ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
1953550efbcSJed Brown               if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
1967f677774SBarry Smith             }
1973550efbcSJed Brown             ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
19835d27ee3SJed Brown             ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
199d1da0b69SBarry Smith             ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
20005315717SToby Isaac           }
20105315717SToby Isaac         }
202bb1d7374SBarry Smith       }
203bb1d7374SBarry Smith       if (viewer) {
204bb1d7374SBarry Smith         ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
205bb1d7374SBarry Smith       }
20635d27ee3SJed Brown       if (loc2_fmt && *loc2_fmt) {
20735d27ee3SJed Brown         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr);
20835d27ee3SJed Brown         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr);
2097f677774SBarry Smith       }
21035d27ee3SJed Brown       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
2112bf49c77SBarry Smith     }
2122bf49c77SBarry Smith   }
2132bf49c77SBarry Smith   PetscFunctionReturn(0);
2142bf49c77SBarry Smith }
2152bf49c77SBarry Smith 
2162bf49c77SBarry Smith #undef __FUNCT__
2175c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate"
2185c6c1daeSBarry Smith /*@
2195c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith    Collective on MPI_Comm
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith    Input Parameter:
2245c6c1daeSBarry Smith .  comm - MPI communicator
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith    Output Parameter:
2275c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith    Level: advanced
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith    Concepts: graphics^creating PetscViewer
2325c6c1daeSBarry Smith    Concepts: file input/output^creating PetscViewer
2335c6c1daeSBarry Smith    Concepts: sockets^creating PetscViewer
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith @*/
2385c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
2395c6c1daeSBarry Smith {
2405c6c1daeSBarry Smith   PetscViewer    viewer;
2415c6c1daeSBarry Smith   PetscErrorCode ierr;
2425c6c1daeSBarry Smith 
2435c6c1daeSBarry Smith   PetscFunctionBegin;
2445c6c1daeSBarry Smith   *inviewer = 0;
245607a6623SBarry Smith   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
24673107ff1SLisandro Dalcin   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);CHKERRQ(ierr);
2475c6c1daeSBarry Smith   *inviewer    = viewer;
2485c6c1daeSBarry Smith   viewer->data = 0;
2495c6c1daeSBarry Smith   PetscFunctionReturn(0);
2505c6c1daeSBarry Smith }
2515c6c1daeSBarry Smith 
2525c6c1daeSBarry Smith #undef __FUNCT__
2535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetType"
2545c6c1daeSBarry Smith /*@C
2555c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith    Collective on PetscViewer
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith    Input Parameter:
2605c6c1daeSBarry Smith +  viewer      - the PetscViewer context
2618f6c3df8SBarry Smith -  type        - for example, PETSCVIEWERASCII
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith    Options Database Command:
2645c6c1daeSBarry Smith .  -draw_type  <type> - Sets the type; use -help for a list
2655c6c1daeSBarry Smith     of available methods (for instance, ascii)
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith    Level: advanced
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith    Notes:
2705c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
2718f6c3df8SBarry Smith    PETSCVIEWERSOCKET)
2725c6c1daeSBarry Smith 
2736a9046bcSBarry Smith .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
2745c6c1daeSBarry Smith @*/
2755c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
2765c6c1daeSBarry Smith {
2775c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscViewer);
2785c6c1daeSBarry Smith   PetscBool      match;
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith   PetscFunctionBegin;
2815c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2825c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
2835c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
2845c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
2855c6c1daeSBarry Smith 
2865c6c1daeSBarry Smith   /* cleanup any old type that may be there */
2875c6c1daeSBarry Smith   if (viewer->data) {
2885c6c1daeSBarry Smith     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
289a297a907SKarl Rupp 
2900298fd71SBarry Smith     viewer->ops->destroy = NULL;
2915c6c1daeSBarry Smith     viewer->data         = 0;
2925c6c1daeSBarry Smith   }
2935c6c1daeSBarry Smith   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
2945c6c1daeSBarry Smith 
2951c9cd337SJed Brown   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
2965c6c1daeSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
2995c6c1daeSBarry Smith   ierr = (*r)(viewer);CHKERRQ(ierr);
3005c6c1daeSBarry Smith   PetscFunctionReturn(0);
3015c6c1daeSBarry Smith }
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith #undef __FUNCT__
3045c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegister"
3051c84c290SBarry Smith /*@C
3061c84c290SBarry Smith    PetscViewerRegister - Adds a viewer
3071c84c290SBarry Smith 
3081c84c290SBarry Smith    Not Collective
3091c84c290SBarry Smith 
3101c84c290SBarry Smith    Input Parameters:
3111c84c290SBarry Smith +  name_solver - name of a new user-defined viewer
3121c84c290SBarry Smith -  routine_create - routine to create method context
3131c84c290SBarry Smith 
3141c84c290SBarry Smith    Level: developer
3151c84c290SBarry Smith    Notes:
3161c84c290SBarry Smith    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
3171c84c290SBarry Smith 
3181c84c290SBarry Smith    Sample usage:
3191c84c290SBarry Smith .vb
320bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type",MyViewerCreate);
3211c84c290SBarry Smith .ve
3221c84c290SBarry Smith 
3231c84c290SBarry Smith    Then, your solver can be chosen with the procedural interface via
3241c84c290SBarry Smith $     PetscViewerSetType(viewer,"my_viewer_type")
3251c84c290SBarry Smith    or at runtime via the option
3261c84c290SBarry Smith $     -viewer_type my_viewer_type
3271c84c290SBarry Smith 
3281c84c290SBarry Smith   Concepts: registering^Viewers
3291c84c290SBarry Smith 
3301c84c290SBarry Smith .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
3311c84c290SBarry Smith  @*/
332bdf89e91SBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
3335c6c1daeSBarry Smith {
3345c6c1daeSBarry Smith   PetscErrorCode ierr;
3355c6c1daeSBarry Smith 
3365c6c1daeSBarry Smith   PetscFunctionBegin;
337a240a19fSJed Brown   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
3385c6c1daeSBarry Smith   PetscFunctionReturn(0);
3395c6c1daeSBarry Smith }
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith #undef __FUNCT__
3425c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetFromOptions"
3435c6c1daeSBarry Smith /*@C
3445c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
3455c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith    Collective on PetscViewer
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith    Input Parameter:
3505c6c1daeSBarry Smith .     PetscViewer - the graphics context
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith    Level: intermediate
3535c6c1daeSBarry Smith 
3545c6c1daeSBarry Smith    Notes:
3555c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith   Concepts: PetscViewer^setting options
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
3605c6c1daeSBarry Smith 
3615c6c1daeSBarry Smith @*/
3625c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
3635c6c1daeSBarry Smith {
3645c6c1daeSBarry Smith   PetscErrorCode    ierr;
3655c6c1daeSBarry Smith   char              vtype[256];
3665c6c1daeSBarry Smith   PetscBool         flg;
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith   PetscFunctionBegin;
3695c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3705c6c1daeSBarry Smith 
3715c6c1daeSBarry Smith   if (!PetscViewerList) {
372607a6623SBarry Smith     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
3735c6c1daeSBarry Smith   }
3745c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
375a264d7a6SBarry Smith   ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
3765c6c1daeSBarry Smith   if (flg) {
3775c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
3785c6c1daeSBarry Smith   }
3795c6c1daeSBarry Smith   /* type has not been set? */
3805c6c1daeSBarry Smith   if (!((PetscObject)viewer)->type_name) {
3815c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
3825c6c1daeSBarry Smith   }
3835c6c1daeSBarry Smith   if (viewer->ops->setfromoptions) {
384e55864a3SBarry Smith     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
3855c6c1daeSBarry Smith   }
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3880633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);CHKERRQ(ierr);
389ce1779c8SBarry Smith   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
3905c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3915c6c1daeSBarry Smith   PetscFunctionReturn(0);
3925c6c1daeSBarry Smith }
393816f7b76SBarry Smith 
394816f7b76SBarry Smith #undef __FUNCT__
395816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStart"
396816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
397816f7b76SBarry Smith {
398816f7b76SBarry Smith   PetscErrorCode ierr;
399816f7b76SBarry Smith   PetscFunctionBegin;
400816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,mcnt);CHKERRQ(ierr);
401816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,cnt);CHKERRQ(ierr);
402816f7b76SBarry Smith   PetscFunctionReturn(0);
403816f7b76SBarry Smith }
404816f7b76SBarry Smith 
405816f7b76SBarry Smith #undef __FUNCT__
406816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStepMaster"
407816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
408816f7b76SBarry Smith {
409816f7b76SBarry Smith   PetscErrorCode ierr;
410816f7b76SBarry Smith   MPI_Comm       comm;
411816f7b76SBarry Smith 
412816f7b76SBarry Smith   PetscFunctionBegin;
413816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
414816f7b76SBarry Smith   if (i >= *mcnt) {
415816f7b76SBarry Smith     *mcnt += cnt;
416816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
417816f7b76SBarry Smith   }
418816f7b76SBarry Smith   PetscFunctionReturn(0);
419816f7b76SBarry Smith }
420816f7b76SBarry Smith 
421816f7b76SBarry Smith #undef __FUNCT__
422816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlEndMaster"
423816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
424816f7b76SBarry Smith {
425816f7b76SBarry Smith   PetscErrorCode ierr;
426816f7b76SBarry Smith   MPI_Comm       comm;
427816f7b76SBarry Smith   PetscFunctionBegin;
428816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
429816f7b76SBarry Smith   *mcnt = 0;
430816f7b76SBarry Smith   ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
431816f7b76SBarry Smith   PetscFunctionReturn(0);
432816f7b76SBarry Smith }
433816f7b76SBarry Smith 
434816f7b76SBarry Smith #undef __FUNCT__
435816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStepWorker"
436816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
437816f7b76SBarry Smith {
438816f7b76SBarry Smith   PetscErrorCode ierr;
439816f7b76SBarry Smith   MPI_Comm       comm;
440816f7b76SBarry Smith   PetscFunctionBegin;
441816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
442816f7b76SBarry Smith   while (PETSC_TRUE) {
443816f7b76SBarry Smith     if (rank < *mcnt) break;
444816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
445816f7b76SBarry Smith   }
446816f7b76SBarry Smith   PetscFunctionReturn(0);
447816f7b76SBarry Smith }
448816f7b76SBarry Smith 
449816f7b76SBarry Smith #undef __FUNCT__
450816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlEndWorker"
451816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
452816f7b76SBarry Smith {
453816f7b76SBarry Smith   PetscErrorCode ierr;
454816f7b76SBarry Smith   MPI_Comm       comm;
455816f7b76SBarry Smith   PetscFunctionBegin;
456816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
457816f7b76SBarry Smith   while (PETSC_TRUE) {
458816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
459816f7b76SBarry Smith     if (!*mcnt) break;
460816f7b76SBarry Smith   }
461816f7b76SBarry Smith   PetscFunctionReturn(0);
462816f7b76SBarry Smith }
463