xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 27b0f280e5bf9c639a7764debf82fd1b76d8a199)
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 
92bf49c77SBarry Smith #undef __FUNCT__
102bf49c77SBarry Smith #define __FUNCT__ "PetscOptionsGetViewer"
112bf49c77SBarry Smith /*@C
122bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
132bf49c77SBarry Smith 
142bf49c77SBarry Smith    Collective on MPI_Comm
152bf49c77SBarry Smith 
162bf49c77SBarry Smith    Input Parameters:
172bf49c77SBarry Smith +  comm - the communicator to own the viewer
180298fd71SBarry Smith .  pre - the string to prepend to the name or NULL
192bf49c77SBarry Smith -  name - the option one is seeking
202bf49c77SBarry Smith 
212bf49c77SBarry Smith    Output Parameter:
22bb1d7374SBarry Smith +  viewer - the viewer, pass NULL if not needed
23bb1d7374SBarry Smith .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
242bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
252bf49c77SBarry Smith 
262bf49c77SBarry Smith    Level: intermediate
272bf49c77SBarry Smith 
282bf49c77SBarry Smith    Notes: If no value is provided ascii:stdout is used
29d1da0b69SBarry Smith $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
30d1da0b69SBarry Smith                                                   for example ascii::ascii_info prints just the information about the object not all details
31d1da0b69SBarry Smith                                                   unless :append is given filename opens in write mode, overwriting what was already there
32d1da0b69SBarry Smith $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
3320610d12SBarry Smith $       draw[:drawtype]                           for example, draw:tikz  or draw:x
342bf49c77SBarry Smith $       socket[:port]                             defaults to the standard output port
352a359c20SBarry Smith $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
362bf49c77SBarry Smith 
37cffb1e40SBarry Smith    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
382bf49c77SBarry Smith 
39*27b0f280SBarry Smith    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
40*27b0f280SBarry Smith 
412bf49c77SBarry Smith .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
422bf49c77SBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
432bf49c77SBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
442bf49c77SBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
452bf49c77SBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
462bf49c77SBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
47a264d7a6SBarry Smith           PetscOptionsFList(), PetscOptionsEList()
482bf49c77SBarry Smith @*/
49cffb1e40SBarry Smith PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
502bf49c77SBarry Smith {
512bf49c77SBarry Smith   char           *value;
522bf49c77SBarry Smith   PetscErrorCode ierr;
5320610d12SBarry Smith   PetscBool      flag,hashelp;
542bf49c77SBarry Smith 
552bf49c77SBarry Smith   PetscFunctionBegin;
562bf49c77SBarry Smith   PetscValidCharPointer(name,3);
572bf49c77SBarry Smith 
58*27b0f280SBarry Smith   if (set) *set = PETSC_FALSE;
59*27b0f280SBarry Smith #if defined(PETSC_SKIP_VIEWFROMOPTIONS)
60*27b0f280SBarry Smith   PetscFunctionReturn(0);
61*27b0f280SBarry Smith #endif
62*27b0f280SBarry Smith 
63c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-help",&hashelp);CHKERRQ(ierr);
6420610d12SBarry Smith   if (hashelp) {
6520610d12SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers display of a PETSc object to screen or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
6620610d12SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers saving of a PETSc object to a binary file","PetscOptionsGetViewer");CHKERRQ(ierr);
6720610d12SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"  -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Triggers drawing of a PETSc object","PetscOptionsGetViewer");CHKERRQ(ierr);
6820610d12SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Triggers push of a PETSc object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
6920610d12SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Triggers publishing of a PETSc object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
7020610d12SBarry Smith   }
71685405a1SBarry Smith 
72e3f3e4b6SBarry Smith   if (format) *format = PETSC_VIEWER_DEFAULT;
73c5929fdfSBarry Smith   ierr = PetscOptionsFindPair_Private(NULL,pre,name,&value,&flag);CHKERRQ(ierr);
742bf49c77SBarry Smith   if (flag) {
752bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
762bf49c77SBarry Smith     if (!value) {
77bb1d7374SBarry Smith       if (viewer) {
782bf49c77SBarry Smith         ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
79706a11cbSBarry Smith         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
80bb1d7374SBarry Smith       }
812bf49c77SBarry Smith     } else {
8235d27ee3SJed Brown       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
832bf49c77SBarry Smith       PetscInt   cnt;
84a75e6a4aSMatthew G. Knepley       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};
852bf49c77SBarry Smith 
8635d27ee3SJed Brown       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
8735d27ee3SJed Brown       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
8835d27ee3SJed Brown       if (loc1_fname) {
8935d27ee3SJed Brown         *loc1_fname++ = 0;
9035d27ee3SJed Brown         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
9135d27ee3SJed Brown       }
9235d27ee3SJed Brown       if (loc2_fmt) {
9335d27ee3SJed Brown         *loc2_fmt++ = 0;
9435d27ee3SJed Brown         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
9535d27ee3SJed Brown       }
9635d27ee3SJed Brown       if (loc3_fmode) *loc3_fmode++ = 0;
9735d27ee3SJed Brown       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
9835d27ee3SJed Brown       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
99bb1d7374SBarry Smith       if (viewer) {
10035d27ee3SJed Brown         if (!loc1_fname) {
10143b63833SBarry Smith           switch (cnt) {
10243b63833SBarry Smith           case 0:
1032bf49c77SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
10443b63833SBarry Smith             break;
10543b63833SBarry Smith           case 1:
106aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
10743b63833SBarry Smith             break;
10843b63833SBarry Smith           case 2:
109aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
11043b63833SBarry Smith             break;
111b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
11243b63833SBarry Smith           case 3:
113aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
11443b63833SBarry Smith             break;
115b58ca069SBarry Smith #endif
11643b63833SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
11743b63833SBarry Smith           case 4:
118aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
11943b63833SBarry Smith             break;
12043b63833SBarry Smith #endif
121e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
122bfb97211SBarry Smith           case 5:
123e04113cfSBarry Smith             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
124bfb97211SBarry Smith             break;
125bfb97211SBarry Smith #endif
126a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
127a75e6a4aSMatthew G. Knepley           case 7:
128a75e6a4aSMatthew G. Knepley             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
129a75e6a4aSMatthew G. Knepley             break;
130a75e6a4aSMatthew G. Knepley #endif
131aa1c909bSJed Brown           default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
1327f677774SBarry Smith           }
133706a11cbSBarry Smith           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
1347f677774SBarry Smith         } else {
13535d27ee3SJed Brown           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
1367f677774SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
137706a11cbSBarry Smith             ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
1387f677774SBarry Smith           } else {
1393550efbcSJed Brown             PetscFileMode fmode;
1402bf49c77SBarry Smith             ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
14135d27ee3SJed Brown             ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
1423550efbcSJed Brown             fmode = FILE_MODE_WRITE;
1433550efbcSJed Brown             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
14435d27ee3SJed Brown               ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
1453550efbcSJed Brown               if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
1467f677774SBarry Smith             }
1473550efbcSJed Brown             ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
14835d27ee3SJed Brown             ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
149d1da0b69SBarry Smith             ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
15005315717SToby Isaac           }
15105315717SToby Isaac         }
152bb1d7374SBarry Smith       }
153bb1d7374SBarry Smith       if (viewer) {
154bb1d7374SBarry Smith         ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
155bb1d7374SBarry Smith       }
15635d27ee3SJed Brown       if (loc2_fmt && *loc2_fmt) {
15735d27ee3SJed Brown         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr);
15835d27ee3SJed Brown         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr);
1597f677774SBarry Smith       }
16035d27ee3SJed Brown       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
1612bf49c77SBarry Smith     }
1622bf49c77SBarry Smith   }
1632bf49c77SBarry Smith   PetscFunctionReturn(0);
1642bf49c77SBarry Smith }
1652bf49c77SBarry Smith 
1662bf49c77SBarry Smith #undef __FUNCT__
1675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate"
1685c6c1daeSBarry Smith /*@
1695c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith    Collective on MPI_Comm
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith    Input Parameter:
1745c6c1daeSBarry Smith .  comm - MPI communicator
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith    Output Parameter:
1775c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith    Level: advanced
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith    Concepts: graphics^creating PetscViewer
1825c6c1daeSBarry Smith    Concepts: file input/output^creating PetscViewer
1835c6c1daeSBarry Smith    Concepts: sockets^creating PetscViewer
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith @*/
1885c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
1895c6c1daeSBarry Smith {
1905c6c1daeSBarry Smith   PetscViewer    viewer;
1915c6c1daeSBarry Smith   PetscErrorCode ierr;
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith   PetscFunctionBegin;
1945c6c1daeSBarry Smith   *inviewer = 0;
195607a6623SBarry Smith   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
19673107ff1SLisandro Dalcin   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);CHKERRQ(ierr);
1975c6c1daeSBarry Smith   *inviewer    = viewer;
1985c6c1daeSBarry Smith   viewer->data = 0;
1995c6c1daeSBarry Smith   PetscFunctionReturn(0);
2005c6c1daeSBarry Smith }
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith #undef __FUNCT__
2035c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetType"
2045c6c1daeSBarry Smith /*@C
2055c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith    Collective on PetscViewer
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith    Input Parameter:
2105c6c1daeSBarry Smith +  viewer      - the PetscViewer context
2118f6c3df8SBarry Smith -  type        - for example, PETSCVIEWERASCII
2125c6c1daeSBarry Smith 
2135c6c1daeSBarry Smith    Options Database Command:
2145c6c1daeSBarry Smith .  -draw_type  <type> - Sets the type; use -help for a list
2155c6c1daeSBarry Smith     of available methods (for instance, ascii)
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith    Level: advanced
2185c6c1daeSBarry Smith 
2195c6c1daeSBarry Smith    Notes:
2205c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
2218f6c3df8SBarry Smith    PETSCVIEWERSOCKET)
2225c6c1daeSBarry Smith 
2236a9046bcSBarry Smith .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
2245c6c1daeSBarry Smith @*/
2255c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
2265c6c1daeSBarry Smith {
2275c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscViewer);
2285c6c1daeSBarry Smith   PetscBool      match;
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith   PetscFunctionBegin;
2315c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2325c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
2335c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
2345c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
2355c6c1daeSBarry Smith 
2365c6c1daeSBarry Smith   /* cleanup any old type that may be there */
2375c6c1daeSBarry Smith   if (viewer->data) {
2385c6c1daeSBarry Smith     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
239a297a907SKarl Rupp 
2400298fd71SBarry Smith     viewer->ops->destroy = NULL;
2415c6c1daeSBarry Smith     viewer->data         = 0;
2425c6c1daeSBarry Smith   }
2435c6c1daeSBarry Smith   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
2445c6c1daeSBarry Smith 
2451c9cd337SJed Brown   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
2465c6c1daeSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
2495c6c1daeSBarry Smith   ierr = (*r)(viewer);CHKERRQ(ierr);
2505c6c1daeSBarry Smith   PetscFunctionReturn(0);
2515c6c1daeSBarry Smith }
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith #undef __FUNCT__
2545c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegister"
2551c84c290SBarry Smith /*@C
2561c84c290SBarry Smith    PetscViewerRegister - Adds a viewer
2571c84c290SBarry Smith 
2581c84c290SBarry Smith    Not Collective
2591c84c290SBarry Smith 
2601c84c290SBarry Smith    Input Parameters:
2611c84c290SBarry Smith +  name_solver - name of a new user-defined viewer
2621c84c290SBarry Smith -  routine_create - routine to create method context
2631c84c290SBarry Smith 
2641c84c290SBarry Smith    Level: developer
2651c84c290SBarry Smith    Notes:
2661c84c290SBarry Smith    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
2671c84c290SBarry Smith 
2681c84c290SBarry Smith    Sample usage:
2691c84c290SBarry Smith .vb
270bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type",MyViewerCreate);
2711c84c290SBarry Smith .ve
2721c84c290SBarry Smith 
2731c84c290SBarry Smith    Then, your solver can be chosen with the procedural interface via
2741c84c290SBarry Smith $     PetscViewerSetType(viewer,"my_viewer_type")
2751c84c290SBarry Smith    or at runtime via the option
2761c84c290SBarry Smith $     -viewer_type my_viewer_type
2771c84c290SBarry Smith 
2781c84c290SBarry Smith   Concepts: registering^Viewers
2791c84c290SBarry Smith 
2801c84c290SBarry Smith .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
2811c84c290SBarry Smith  @*/
282bdf89e91SBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
2835c6c1daeSBarry Smith {
2845c6c1daeSBarry Smith   PetscErrorCode ierr;
2855c6c1daeSBarry Smith 
2865c6c1daeSBarry Smith   PetscFunctionBegin;
287a240a19fSJed Brown   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
2885c6c1daeSBarry Smith   PetscFunctionReturn(0);
2895c6c1daeSBarry Smith }
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith #undef __FUNCT__
2925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetFromOptions"
2935c6c1daeSBarry Smith /*@C
2945c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
2955c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith    Collective on PetscViewer
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith    Input Parameter:
3005c6c1daeSBarry Smith .     PetscViewer - the graphics context
3015c6c1daeSBarry Smith 
3025c6c1daeSBarry Smith    Level: intermediate
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith    Notes:
3055c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith   Concepts: PetscViewer^setting options
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
3105c6c1daeSBarry Smith 
3115c6c1daeSBarry Smith @*/
3125c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
3135c6c1daeSBarry Smith {
3145c6c1daeSBarry Smith   PetscErrorCode    ierr;
3155c6c1daeSBarry Smith   char              vtype[256];
3165c6c1daeSBarry Smith   PetscBool         flg;
3175c6c1daeSBarry Smith 
3185c6c1daeSBarry Smith   PetscFunctionBegin;
3195c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith   if (!PetscViewerList) {
322607a6623SBarry Smith     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
3235c6c1daeSBarry Smith   }
3245c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
325a264d7a6SBarry 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);
3265c6c1daeSBarry Smith   if (flg) {
3275c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
3285c6c1daeSBarry Smith   }
3295c6c1daeSBarry Smith   /* type has not been set? */
3305c6c1daeSBarry Smith   if (!((PetscObject)viewer)->type_name) {
3315c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
3325c6c1daeSBarry Smith   }
3335c6c1daeSBarry Smith   if (viewer->ops->setfromoptions) {
334e55864a3SBarry Smith     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
3355c6c1daeSBarry Smith   }
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3380633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);CHKERRQ(ierr);
339ce1779c8SBarry Smith   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
3405c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3415c6c1daeSBarry Smith   PetscFunctionReturn(0);
3425c6c1daeSBarry Smith }
343816f7b76SBarry Smith 
344816f7b76SBarry Smith #undef __FUNCT__
345816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStart"
346816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
347816f7b76SBarry Smith {
348816f7b76SBarry Smith   PetscErrorCode ierr;
349816f7b76SBarry Smith   PetscFunctionBegin;
350816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,mcnt);CHKERRQ(ierr);
351816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,cnt);CHKERRQ(ierr);
352816f7b76SBarry Smith   PetscFunctionReturn(0);
353816f7b76SBarry Smith }
354816f7b76SBarry Smith 
355816f7b76SBarry Smith #undef __FUNCT__
356816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStepMaster"
357816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
358816f7b76SBarry Smith {
359816f7b76SBarry Smith   PetscErrorCode ierr;
360816f7b76SBarry Smith   MPI_Comm       comm;
361816f7b76SBarry Smith 
362816f7b76SBarry Smith   PetscFunctionBegin;
363816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
364816f7b76SBarry Smith   if (i >= *mcnt) {
365816f7b76SBarry Smith     *mcnt += cnt;
366816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
367816f7b76SBarry Smith   }
368816f7b76SBarry Smith   PetscFunctionReturn(0);
369816f7b76SBarry Smith }
370816f7b76SBarry Smith 
371816f7b76SBarry Smith #undef __FUNCT__
372816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlEndMaster"
373816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
374816f7b76SBarry Smith {
375816f7b76SBarry Smith   PetscErrorCode ierr;
376816f7b76SBarry Smith   MPI_Comm       comm;
377816f7b76SBarry Smith   PetscFunctionBegin;
378816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
379816f7b76SBarry Smith   *mcnt = 0;
380816f7b76SBarry Smith   ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
381816f7b76SBarry Smith   PetscFunctionReturn(0);
382816f7b76SBarry Smith }
383816f7b76SBarry Smith 
384816f7b76SBarry Smith #undef __FUNCT__
385816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlStepWorker"
386816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
387816f7b76SBarry Smith {
388816f7b76SBarry Smith   PetscErrorCode ierr;
389816f7b76SBarry Smith   MPI_Comm       comm;
390816f7b76SBarry Smith   PetscFunctionBegin;
391816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
392816f7b76SBarry Smith   while (PETSC_TRUE) {
393816f7b76SBarry Smith     if (rank < *mcnt) break;
394816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
395816f7b76SBarry Smith   }
396816f7b76SBarry Smith   PetscFunctionReturn(0);
397816f7b76SBarry Smith }
398816f7b76SBarry Smith 
399816f7b76SBarry Smith #undef __FUNCT__
400816f7b76SBarry Smith #define __FUNCT__ "PetscViewerFlowControlEndWorker"
401816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
402816f7b76SBarry Smith {
403816f7b76SBarry Smith   PetscErrorCode ierr;
404816f7b76SBarry Smith   MPI_Comm       comm;
405816f7b76SBarry Smith   PetscFunctionBegin;
406816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
407816f7b76SBarry Smith   while (PETSC_TRUE) {
408816f7b76SBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
409816f7b76SBarry Smith     if (!*mcnt) break;
410816f7b76SBarry Smith   }
411816f7b76SBarry Smith   PetscFunctionReturn(0);
412816f7b76SBarry Smith }
413