xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 2c71b3e237ead271e4f3aa1505f92bf476e3413d)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3e8f14785SLisandro Dalcin #include <petsc/private/hashtable.h>
4e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
5e04113cfSBarry Smith #include <petscviewersaws.h>
6bfb97211SBarry Smith #endif
75c6c1daeSBarry Smith 
802c9f0b5SLisandro Dalcin PetscFunctionList PetscViewerList = NULL;
95c6c1daeSBarry Smith 
109de0f6ecSBarry Smith PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton = NULL;
119de0f6ecSBarry Smith KHASH_SET_INIT_STR(HTPrinted)
129de0f6ecSBarry Smith struct  _n_PetscOptionsHelpPrinted{
139de0f6ecSBarry Smith   khash_t(HTPrinted) *printed;
149de0f6ecSBarry Smith   PetscSegBuffer     strings;
159de0f6ecSBarry Smith };
169de0f6ecSBarry Smith 
179de0f6ecSBarry Smith PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp)
1894d6a431SBarry Smith {
199af95d99SBarry Smith   PetscErrorCode ierr;
209de0f6ecSBarry Smith 
219de0f6ecSBarry Smith   PetscFunctionBegin;
229de0f6ecSBarry Smith   if (!*hp) PetscFunctionReturn(0);
239de0f6ecSBarry Smith   kh_destroy(HTPrinted,(*hp)->printed);
249de0f6ecSBarry Smith   ierr = PetscSegBufferDestroy(&(*hp)->strings);CHKERRQ(ierr);
259de0f6ecSBarry Smith   ierr = PetscFree(*hp);CHKERRQ(ierr);
269de0f6ecSBarry Smith   PetscFunctionReturn(0);
279de0f6ecSBarry Smith }
289de0f6ecSBarry Smith 
299de0f6ecSBarry Smith /*@C
309de0f6ecSBarry Smith       PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
319de0f6ecSBarry Smith          been printed so they will not be printed again.
329de0f6ecSBarry Smith 
339de0f6ecSBarry Smith      Not collective
349de0f6ecSBarry Smith 
359de0f6ecSBarry Smith     Level: developer
369de0f6ecSBarry Smith 
379de0f6ecSBarry Smith .seealso: PetscOptionsHelpPrintedCheck(), PetscOptionsHelpPrintChecked()
389de0f6ecSBarry Smith @*/
399de0f6ecSBarry Smith PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
409de0f6ecSBarry Smith {
419de0f6ecSBarry Smith   PetscErrorCode             ierr;
429de0f6ecSBarry Smith 
439de0f6ecSBarry Smith   PetscFunctionBegin;
449de0f6ecSBarry Smith   ierr = PetscNew(hp);CHKERRQ(ierr);
459de0f6ecSBarry Smith   (*hp)->printed = kh_init(HTPrinted);
469de0f6ecSBarry Smith   ierr = PetscSegBufferCreate(sizeof(char),10000,&(*hp)->strings);CHKERRQ(ierr);
479de0f6ecSBarry Smith   PetscFunctionReturn(0);
489de0f6ecSBarry Smith }
499de0f6ecSBarry Smith 
509de0f6ecSBarry Smith /*@C
519de0f6ecSBarry Smith       PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
529de0f6ecSBarry Smith 
539de0f6ecSBarry Smith      Not collective
549de0f6ecSBarry Smith 
559de0f6ecSBarry Smith     Input Parameters:
569de0f6ecSBarry Smith +     hp - the object used to manage tracking what help messages have been printed
579de0f6ecSBarry Smith .     pre - the prefix part of the string, many be NULL
589de0f6ecSBarry Smith -     name - the string to look for (cannot be NULL)
599de0f6ecSBarry Smith 
609de0f6ecSBarry Smith     Output Parameter:
619de0f6ecSBarry Smith .     found - PETSC_TRUE if the string was already set
629de0f6ecSBarry Smith 
639de0f6ecSBarry Smith     Level: intermediate
649de0f6ecSBarry Smith 
659de0f6ecSBarry Smith .seealso: PetscOptionsHelpPrintedCreate()
669de0f6ecSBarry Smith @*/
679de0f6ecSBarry Smith PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp,const char *pre,const char* name,PetscBool *found)
689de0f6ecSBarry Smith {
699de0f6ecSBarry Smith   size_t          l1,l2;
70c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
719de0f6ecSBarry Smith   char            *both;
72e8f14785SLisandro Dalcin   int             newitem;
73c1449d8eSBarry Smith #endif
749de0f6ecSBarry Smith   PetscErrorCode  ierr;
759de0f6ecSBarry Smith 
769de0f6ecSBarry Smith   PetscFunctionBegin;
779de0f6ecSBarry Smith   ierr = PetscStrlen(pre,&l1);CHKERRQ(ierr);
789de0f6ecSBarry Smith   ierr = PetscStrlen(name,&l2);CHKERRQ(ierr);
799de0f6ecSBarry Smith   if (l1+l2 == 0) {
809de0f6ecSBarry Smith     *found = PETSC_FALSE;
819de0f6ecSBarry Smith     PetscFunctionReturn(0);
829de0f6ecSBarry Smith   }
83c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
849de0f6ecSBarry Smith   ierr = PetscSegBufferGet(hp->strings,l1+l2+1,&both);CHKERRQ(ierr);
859de0f6ecSBarry Smith   ierr = PetscStrcpy(both,pre);CHKERRQ(ierr);
869de0f6ecSBarry Smith   ierr = PetscStrcat(both,name);CHKERRQ(ierr);
879de0f6ecSBarry Smith   kh_put(HTPrinted,hp->printed,both,&newitem);
889de0f6ecSBarry Smith   if (!newitem) {
899de0f6ecSBarry Smith     ierr = PetscSegBufferUnuse(hp->strings,l1+l2+1);CHKERRQ(ierr);
909de0f6ecSBarry Smith   }
919de0f6ecSBarry Smith   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
92c1449d8eSBarry Smith #else
93c1449d8eSBarry Smith   *found = PETSC_FALSE;
94c1449d8eSBarry Smith #endif
959de0f6ecSBarry Smith   PetscFunctionReturn(0);
9694d6a431SBarry Smith }
9794d6a431SBarry Smith 
98eb55bdffSLawrence Mitchell static PetscBool noviewer = PETSC_FALSE;
99eb55bdffSLawrence Mitchell static PetscBool noviewers[PETSCVIEWERGETVIEWEROFFPUSHESMAX];
100eb55bdffSLawrence Mitchell static PetscInt  inoviewers = 0;
101eb55bdffSLawrence Mitchell 
102eb55bdffSLawrence Mitchell /*@
103eb55bdffSLawrence Mitchell   PetscOptionsPushGetViewerOff - control whether PetscOptionsGetViewer returns a viewer.
104eb55bdffSLawrence Mitchell 
105eb55bdffSLawrence Mitchell   Logically Collective
106eb55bdffSLawrence Mitchell 
107eb55bdffSLawrence Mitchell   Input Parameter:
108eb55bdffSLawrence Mitchell . flg - PETSC_TRUE to turn off viewer creation, PETSC_FALSE to turn it on.
109eb55bdffSLawrence Mitchell 
110eb55bdffSLawrence Mitchell   Level: developer
111eb55bdffSLawrence Mitchell 
11295452b02SPatrick Sanan   Notes:
11395452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
114eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
115eb55bdffSLawrence Mitchell 
116eb55bdffSLawrence Mitchell .seealso: PetscOptionsGetViewer(), PetscOptionsPopGetViewerOff()
117eb55bdffSLawrence Mitchell @*/
118eb55bdffSLawrence Mitchell PetscErrorCode  PetscOptionsPushGetViewerOff(PetscBool flg)
119eb55bdffSLawrence Mitchell {
120eb55bdffSLawrence Mitchell   PetscFunctionBegin;
121*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(inoviewers > PETSCVIEWERGETVIEWEROFFPUSHESMAX - 1,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPushGetViewerOff(), perhaps you forgot PetscOptionsPopGetViewerOff()?");
122eb55bdffSLawrence Mitchell 
123eb55bdffSLawrence Mitchell   noviewers[inoviewers++] = noviewer;
124eb55bdffSLawrence Mitchell   noviewer = flg;
125eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
126eb55bdffSLawrence Mitchell }
127eb55bdffSLawrence Mitchell 
128eb55bdffSLawrence Mitchell /*@
129eb55bdffSLawrence Mitchell   PetscOptionsPopGetViewerOff - reset whether PetscOptionsGetViewer returns a viewer.
130eb55bdffSLawrence Mitchell 
131eb55bdffSLawrence Mitchell   Logically Collective
132eb55bdffSLawrence Mitchell 
133eb55bdffSLawrence Mitchell   Level: developer
134eb55bdffSLawrence Mitchell 
13595452b02SPatrick Sanan   Notes:
13695452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
137eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
138eb55bdffSLawrence Mitchell 
139eb55bdffSLawrence Mitchell .seealso: PetscOptionsGetViewer(), PetscOptionsPushGetViewerOff()
140eb55bdffSLawrence Mitchell @*/
141dd63322aSSatish Balay PetscErrorCode  PetscOptionsPopGetViewerOff(void)
142eb55bdffSLawrence Mitchell {
143eb55bdffSLawrence Mitchell   PetscFunctionBegin;
144*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!inoviewers,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPopGetViewerOff(), perhaps you forgot PetscOptionsPushGetViewerOff()?");
145eb55bdffSLawrence Mitchell   noviewer = noviewers[--inoviewers];
146eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
147eb55bdffSLawrence Mitchell }
148eb55bdffSLawrence Mitchell 
149eb55bdffSLawrence Mitchell /*@
150eb55bdffSLawrence Mitchell   PetscOptionsGetViewerOff - does PetscOptionsGetViewer return a viewer?
151eb55bdffSLawrence Mitchell 
152eb55bdffSLawrence Mitchell   Logically Collective
153eb55bdffSLawrence Mitchell 
154eb55bdffSLawrence Mitchell   Output Parameter:
155eb55bdffSLawrence Mitchell . flg - whether viewers are returned.
156eb55bdffSLawrence Mitchell 
157eb55bdffSLawrence Mitchell   Level: developer
158eb55bdffSLawrence Mitchell 
15995452b02SPatrick Sanan   Notes:
16095452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
161eb55bdffSLawrence Mitchell    many small subsolves.
162eb55bdffSLawrence Mitchell 
163eb55bdffSLawrence Mitchell .seealso: PetscOptionsGetViewer(), PetscOptionsPushGetViewerOff(), PetscOptionsPopGetViewerOff()
164eb55bdffSLawrence Mitchell @*/
165eb55bdffSLawrence Mitchell PetscErrorCode  PetscOptionsGetViewerOff(PetscBool *flg)
166eb55bdffSLawrence Mitchell {
167eb55bdffSLawrence Mitchell   PetscFunctionBegin;
168534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,1);
169eb55bdffSLawrence Mitchell   *flg = noviewer;
170eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
171eb55bdffSLawrence Mitchell }
172eb55bdffSLawrence Mitchell 
1732bf49c77SBarry Smith /*@C
1742bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
1752bf49c77SBarry Smith 
176d083f849SBarry Smith    Collective
1772bf49c77SBarry Smith 
1782bf49c77SBarry Smith    Input Parameters:
1792bf49c77SBarry Smith +  comm - the communicator to own the viewer
1806b867d5aSJose E. Roman .  options - options database, use NULL for default global database
1810298fd71SBarry Smith .  pre - the string to prepend to the name or NULL
1822bf49c77SBarry Smith -  name - the option one is seeking
1832bf49c77SBarry Smith 
184d8d19677SJose E. Roman    Output Parameters:
185bb1d7374SBarry Smith +  viewer - the viewer, pass NULL if not needed
186bb1d7374SBarry Smith .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
1872bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
1882bf49c77SBarry Smith 
1892bf49c77SBarry Smith    Level: intermediate
1902bf49c77SBarry Smith 
19195452b02SPatrick Sanan    Notes:
19295452b02SPatrick Sanan     If no value is provided ascii:stdout is used
193d1da0b69SBarry Smith $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
194d1da0b69SBarry Smith                                                   for example ascii::ascii_info prints just the information about the object not all details
195d1da0b69SBarry Smith                                                   unless :append is given filename opens in write mode, overwriting what was already there
196d1da0b69SBarry Smith $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
197acd7d2deSBarry Smith $       draw[:drawtype[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  or draw:x
1982bf49c77SBarry Smith $       socket[:port]                             defaults to the standard output port
1992a359c20SBarry Smith $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
2002bf49c77SBarry Smith 
201cffb1e40SBarry Smith    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
2022bf49c77SBarry Smith 
203eb55bdffSLawrence Mitchell    You can control whether calls to this function create a viewer (or return early with *set of PETSC_FALSE) with
204eb55bdffSLawrence Mitchell    PetscOptionsPushGetViewerOff.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
205eb55bdffSLawrence Mitchell    an appreciable fraction of the runtime.
206eb55bdffSLawrence Mitchell 
20727b0f280SBarry Smith    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
20827b0f280SBarry Smith 
2092bf49c77SBarry Smith .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
2102bf49c77SBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
2112bf49c77SBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
2122bf49c77SBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2132bf49c77SBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2142bf49c77SBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
215eb55bdffSLawrence Mitchell           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsPushGetViewerOff(), PetscOptionsPopGetViewerOff(),
216eb55bdffSLawrence Mitchell           PetscOptionsGetViewerOff()
2172bf49c77SBarry Smith @*/
21816413a6aSBarry Smith PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
2192bf49c77SBarry Smith {
2202d747510SLisandro Dalcin   const char                     *value;
2212bf49c77SBarry Smith   PetscErrorCode                 ierr;
22220610d12SBarry Smith   PetscBool                      flag,hashelp;
2232bf49c77SBarry Smith 
2242bf49c77SBarry Smith   PetscFunctionBegin;
225064a246eSJacob Faibussowitsch   PetscValidCharPointer(name,4);
2262bf49c77SBarry Smith 
2276348e711SLisandro Dalcin   if (viewer) *viewer = NULL;
2286348e711SLisandro Dalcin   if (format) *format = PETSC_VIEWER_DEFAULT;
22927b0f280SBarry Smith   if (set)    *set    = PETSC_FALSE;
230eb55bdffSLawrence Mitchell   ierr = PetscOptionsGetViewerOff(&flag);CHKERRQ(ierr);
231eb55bdffSLawrence Mitchell   if (flag) PetscFunctionReturn(0);
23227b0f280SBarry Smith 
2332d747510SLisandro Dalcin   ierr = PetscOptionsHasHelp(NULL,&hashelp);CHKERRQ(ierr);
23420610d12SBarry Smith   if (hashelp) {
2359de0f6ecSBarry Smith     PetscBool found;
2369af95d99SBarry Smith 
2379de0f6ecSBarry Smith     if (!PetscOptionsHelpPrintedSingleton) {
2389de0f6ecSBarry Smith       ierr = PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton);CHKERRQ(ierr);
2399af95d99SBarry Smith     }
2409de0f6ecSBarry Smith     ierr = PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton,pre,name,&found);CHKERRQ(ierr);
24156071f75SVaclav Hapla     if (!found && viewer) {
242c0bb3764SVaclav Hapla       ierr = (*PetscHelpPrintf)(comm,"----------------------------------------\nViewer (-%s%s) options:\n",pre ? pre : "",name+1);CHKERRQ(ierr);
24356071f75SVaclav Hapla       ierr = (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Prints object to stdout or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
24494d6a431SBarry 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);
24552f76066SLisandro Dalcin       ierr = (*PetscHelpPrintf)(comm,"  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n",pre ? pre : "",name+1,"Draws object","PetscOptionsGetViewer");CHKERRQ(ierr);
24694d6a431SBarry Smith       ierr = (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Pushes object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
24756071f75SVaclav Hapla       ierr = (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Publishes object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
24894d6a431SBarry Smith     }
24920610d12SBarry Smith   }
250685405a1SBarry Smith 
251e3f3e4b6SBarry Smith   if (format) *format = PETSC_VIEWER_DEFAULT;
25216413a6aSBarry Smith   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2532bf49c77SBarry Smith   if (flag) {
2542bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
2552bf49c77SBarry Smith     if (!value) {
256bb1d7374SBarry Smith       if (viewer) {
2572bf49c77SBarry Smith         ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
258706a11cbSBarry Smith         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
259bb1d7374SBarry Smith       }
2602bf49c77SBarry Smith     } else {
26135d27ee3SJed Brown       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
2622bf49c77SBarry Smith       PetscInt   cnt;
2631e50132fSMatthew G. Knepley       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,PETSCVIEWERGLVIS,PETSCVIEWEREXODUSII,NULL};
2642bf49c77SBarry Smith 
26535d27ee3SJed Brown       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
26635d27ee3SJed Brown       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
26735d27ee3SJed Brown       if (loc1_fname) {
26835d27ee3SJed Brown         *loc1_fname++ = 0;
26935d27ee3SJed Brown         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
27035d27ee3SJed Brown       }
27135d27ee3SJed Brown       if (loc2_fmt) {
27235d27ee3SJed Brown         *loc2_fmt++ = 0;
27335d27ee3SJed Brown         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
27435d27ee3SJed Brown       }
27535d27ee3SJed Brown       if (loc3_fmode) *loc3_fmode++ = 0;
27635d27ee3SJed Brown       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
277*2c71b3e2SJacob Faibussowitsch       PetscCheckFalse(cnt > (PetscInt) sizeof(viewers)-1,comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
278bb1d7374SBarry Smith       if (viewer) {
27935d27ee3SJed Brown         if (!loc1_fname) {
28043b63833SBarry Smith           switch (cnt) {
28143b63833SBarry Smith           case 0:
2822bf49c77SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
28343b63833SBarry Smith             break;
28443b63833SBarry Smith           case 1:
285aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
28643b63833SBarry Smith             break;
28743b63833SBarry Smith           case 2:
288aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
28943b63833SBarry Smith             break;
290b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
29143b63833SBarry Smith           case 3:
292aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
29343b63833SBarry Smith             break;
294b58ca069SBarry Smith #endif
29543b63833SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
29643b63833SBarry Smith           case 4:
297aa1c909bSJed Brown             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
29843b63833SBarry Smith             break;
29943b63833SBarry Smith #endif
300e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
301bfb97211SBarry Smith           case 5:
302e04113cfSBarry Smith             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
303bfb97211SBarry Smith             break;
304bfb97211SBarry Smith #endif
305a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
306a75e6a4aSMatthew G. Knepley           case 7:
307a75e6a4aSMatthew G. Knepley             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
308a75e6a4aSMatthew G. Knepley             break;
309a75e6a4aSMatthew G. Knepley #endif
3108135c375SStefano Zampini           case 8:
3118135c375SStefano Zampini             if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
3128135c375SStefano Zampini             break;
3131e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
3141e50132fSMatthew G. Knepley           case 9:
3151e50132fSMatthew G. Knepley             if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) CHKERRQ(PETSC_ERR_PLIB);
3161e50132fSMatthew G. Knepley             break;
3171e50132fSMatthew G. Knepley #endif
31898921bdaSJacob Faibussowitsch           default: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
3197f677774SBarry Smith           }
320706a11cbSBarry Smith           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
3217f677774SBarry Smith         } else {
32235d27ee3SJed Brown           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
3237f677774SBarry Smith             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
324706a11cbSBarry Smith             ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
3257f677774SBarry Smith           } else {
3263550efbcSJed Brown             PetscFileMode fmode;
3272bf49c77SBarry Smith             ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
32835d27ee3SJed Brown             ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
3293550efbcSJed Brown             fmode = FILE_MODE_WRITE;
3303550efbcSJed Brown             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
33135d27ee3SJed Brown               ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
332*2c71b3e2SJacob Faibussowitsch               PetscCheckFalse(!flag,comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
3337f677774SBarry Smith             }
334acd7d2deSBarry Smith             if (loc2_fmt) {
3350ecfd9fcSLisandro Dalcin               PetscBool tk,im;
336acd7d2deSBarry Smith               ierr = PetscStrcmp(loc1_fname,"tikz",&tk);CHKERRQ(ierr);
3370ecfd9fcSLisandro Dalcin               ierr = PetscStrcmp(loc1_fname,"image",&im);CHKERRQ(ierr);
3380ecfd9fcSLisandro Dalcin               if (tk || im) {
3390ecfd9fcSLisandro Dalcin                 ierr = PetscViewerDrawSetInfo(*viewer,NULL,loc2_fmt,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
340acd7d2deSBarry Smith                 *loc2_fmt = 0;
341acd7d2deSBarry Smith               }
342acd7d2deSBarry Smith             }
3433550efbcSJed Brown             ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
34435d27ee3SJed Brown             ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
34552f76066SLisandro Dalcin             if (*loc1_fname) {
346d1da0b69SBarry Smith               ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
34705315717SToby Isaac             }
34873365f8bSLisandro Dalcin             ierr = PetscViewerSetFromOptions(*viewer);CHKERRQ(ierr);
34905315717SToby Isaac           }
350bb1d7374SBarry Smith         }
35152f76066SLisandro Dalcin       }
352bb1d7374SBarry Smith       if (viewer) {
353bb1d7374SBarry Smith         ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
354bb1d7374SBarry Smith       }
35535d27ee3SJed Brown       if (loc2_fmt && *loc2_fmt) {
356e156c29bSStefano Zampini         PetscViewerFormat tfmt;
357d6acdc46SStefano Zampini 
358e156c29bSStefano Zampini         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)&tfmt,&flag);CHKERRQ(ierr);
359d6acdc46SStefano Zampini         if (format) *format = tfmt;
360*2c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!flag,PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
361d6acdc46SStefano Zampini       } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
362a97b86ecSLisandro Dalcin         ierr = PetscViewerGetFormat(*viewer,format);CHKERRQ(ierr);
3637f677774SBarry Smith       }
36435d27ee3SJed Brown       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
3652bf49c77SBarry Smith     }
3662bf49c77SBarry Smith   }
3672bf49c77SBarry Smith   PetscFunctionReturn(0);
3682bf49c77SBarry Smith }
3692bf49c77SBarry Smith 
3705c6c1daeSBarry Smith /*@
3715c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
3725c6c1daeSBarry Smith 
373d083f849SBarry Smith    Collective
3745c6c1daeSBarry Smith 
3755c6c1daeSBarry Smith    Input Parameter:
3765c6c1daeSBarry Smith .  comm - MPI communicator
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith    Output Parameter:
3795c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
3805c6c1daeSBarry Smith 
3815c6c1daeSBarry Smith    Level: advanced
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
3845c6c1daeSBarry Smith 
3855c6c1daeSBarry Smith @*/
3865c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
3875c6c1daeSBarry Smith {
3885c6c1daeSBarry Smith   PetscViewer    viewer;
3895c6c1daeSBarry Smith   PetscErrorCode ierr;
3905c6c1daeSBarry Smith 
3915c6c1daeSBarry Smith   PetscFunctionBegin;
39202c9f0b5SLisandro Dalcin   *inviewer = NULL;
393607a6623SBarry Smith   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
3942d629456SLisandro Dalcin   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,PetscViewerView);CHKERRQ(ierr);
3955c6c1daeSBarry Smith   *inviewer    = viewer;
39602c9f0b5SLisandro Dalcin   viewer->data = NULL;
3975c6c1daeSBarry Smith   PetscFunctionReturn(0);
3985c6c1daeSBarry Smith }
3995c6c1daeSBarry Smith 
4005c6c1daeSBarry Smith /*@C
4015c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
4025c6c1daeSBarry Smith 
4035c6c1daeSBarry Smith    Collective on PetscViewer
4045c6c1daeSBarry Smith 
405d8d19677SJose E. Roman    Input Parameters:
4065c6c1daeSBarry Smith +  viewer      - the PetscViewer context
4078f6c3df8SBarry Smith -  type        - for example, PETSCVIEWERASCII
4085c6c1daeSBarry Smith 
4095c6c1daeSBarry Smith    Options Database Command:
410621f4a4dSVáclav Hapla .  -viewer_type  <type> - Sets the type; use -help for a list
4115c6c1daeSBarry Smith     of available methods (for instance, ascii)
4125c6c1daeSBarry Smith 
4135c6c1daeSBarry Smith    Level: advanced
4145c6c1daeSBarry Smith 
4155c6c1daeSBarry Smith    Notes:
4165c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
4178f6c3df8SBarry Smith    PETSCVIEWERSOCKET)
4185c6c1daeSBarry Smith 
4196a9046bcSBarry Smith .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
4205c6c1daeSBarry Smith @*/
4215c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
4225c6c1daeSBarry Smith {
4235c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscViewer);
4245c6c1daeSBarry Smith   PetscBool      match;
4255c6c1daeSBarry Smith 
4265c6c1daeSBarry Smith   PetscFunctionBegin;
4275c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4285c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
4295c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
4305c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
4315c6c1daeSBarry Smith 
4325c6c1daeSBarry Smith   /* cleanup any old type that may be there */
4335c6c1daeSBarry Smith   if (viewer->data) {
4345c6c1daeSBarry Smith     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
435a297a907SKarl Rupp 
4360298fd71SBarry Smith     viewer->ops->destroy = NULL;
43702c9f0b5SLisandro Dalcin     viewer->data         = NULL;
4385c6c1daeSBarry Smith   }
4395c6c1daeSBarry Smith   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
4405c6c1daeSBarry Smith 
4411c9cd337SJed Brown   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
442*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
4435c6c1daeSBarry Smith 
4445c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
4455c6c1daeSBarry Smith   ierr = (*r)(viewer);CHKERRQ(ierr);
4465c6c1daeSBarry Smith   PetscFunctionReturn(0);
4475c6c1daeSBarry Smith }
4485c6c1daeSBarry Smith 
4491c84c290SBarry Smith /*@C
4501c84c290SBarry Smith    PetscViewerRegister - Adds a viewer
4511c84c290SBarry Smith 
4521c84c290SBarry Smith    Not Collective
4531c84c290SBarry Smith 
4541c84c290SBarry Smith    Input Parameters:
4551c84c290SBarry Smith +  name_solver - name of a new user-defined viewer
4561c84c290SBarry Smith -  routine_create - routine to create method context
4571c84c290SBarry Smith 
4581c84c290SBarry Smith    Level: developer
4591c84c290SBarry Smith    Notes:
4601c84c290SBarry Smith    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
4611c84c290SBarry Smith 
4621c84c290SBarry Smith    Sample usage:
4631c84c290SBarry Smith .vb
464bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type",MyViewerCreate);
4651c84c290SBarry Smith .ve
4661c84c290SBarry Smith 
4671c84c290SBarry Smith    Then, your solver can be chosen with the procedural interface via
4681c84c290SBarry Smith $     PetscViewerSetType(viewer,"my_viewer_type")
4691c84c290SBarry Smith    or at runtime via the option
4701c84c290SBarry Smith $     -viewer_type my_viewer_type
4711c84c290SBarry Smith 
472446c23c1SPierre Jolivet .seealso: PetscViewerRegisterAll()
4731c84c290SBarry Smith  @*/
474bdf89e91SBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
4755c6c1daeSBarry Smith {
4765c6c1daeSBarry Smith   PetscErrorCode ierr;
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith   PetscFunctionBegin;
4791d36bdfdSBarry Smith   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
480a240a19fSJed Brown   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
4815c6c1daeSBarry Smith   PetscFunctionReturn(0);
4825c6c1daeSBarry Smith }
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith /*@C
4855c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
4865c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
4875c6c1daeSBarry Smith 
4885c6c1daeSBarry Smith    Collective on PetscViewer
4895c6c1daeSBarry Smith 
4905c6c1daeSBarry Smith    Input Parameter:
4915c6c1daeSBarry Smith .     PetscViewer - the graphics context
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith    Level: intermediate
4945c6c1daeSBarry Smith 
4955c6c1daeSBarry Smith    Notes:
4965c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
4975c6c1daeSBarry Smith 
4985c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
4995c6c1daeSBarry Smith 
5005c6c1daeSBarry Smith @*/
5015c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
5025c6c1daeSBarry Smith {
5035c6c1daeSBarry Smith   PetscErrorCode    ierr;
5045c6c1daeSBarry Smith   char              vtype[256];
5055c6c1daeSBarry Smith   PetscBool         flg;
5065c6c1daeSBarry Smith 
5075c6c1daeSBarry Smith   PetscFunctionBegin;
5085c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5095c6c1daeSBarry Smith 
5105c6c1daeSBarry Smith   if (!PetscViewerList) {
511607a6623SBarry Smith     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
5125c6c1daeSBarry Smith   }
5135c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
514a264d7a6SBarry 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);
5155c6c1daeSBarry Smith   if (flg) {
5165c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
5175c6c1daeSBarry Smith   }
5185c6c1daeSBarry Smith   /* type has not been set? */
5195c6c1daeSBarry Smith   if (!((PetscObject)viewer)->type_name) {
5205c6c1daeSBarry Smith     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
5215c6c1daeSBarry Smith   }
5225c6c1daeSBarry Smith   if (viewer->ops->setfromoptions) {
523e55864a3SBarry Smith     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
5245c6c1daeSBarry Smith   }
5255c6c1daeSBarry Smith 
5265c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5270633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);CHKERRQ(ierr);
528ce1779c8SBarry Smith   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
5295c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5305c6c1daeSBarry Smith   PetscFunctionReturn(0);
5315c6c1daeSBarry Smith }
532816f7b76SBarry Smith 
533816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
534816f7b76SBarry Smith {
535816f7b76SBarry Smith   PetscErrorCode ierr;
536816f7b76SBarry Smith   PetscFunctionBegin;
537816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,mcnt);CHKERRQ(ierr);
538816f7b76SBarry Smith   ierr = PetscViewerBinaryGetFlowControl(viewer,cnt);CHKERRQ(ierr);
539816f7b76SBarry Smith   PetscFunctionReturn(0);
540816f7b76SBarry Smith }
541816f7b76SBarry Smith 
5429dddd249SSatish Balay PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
543816f7b76SBarry Smith {
544816f7b76SBarry Smith   PetscErrorCode ierr;
545816f7b76SBarry Smith   MPI_Comm       comm;
546816f7b76SBarry Smith 
547816f7b76SBarry Smith   PetscFunctionBegin;
548816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
549816f7b76SBarry Smith   if (i >= *mcnt) {
550816f7b76SBarry Smith     *mcnt += cnt;
551ffc4695bSBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
552816f7b76SBarry Smith   }
553816f7b76SBarry Smith   PetscFunctionReturn(0);
554816f7b76SBarry Smith }
555816f7b76SBarry Smith 
5569dddd249SSatish Balay PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer,PetscInt *mcnt)
557816f7b76SBarry Smith {
558816f7b76SBarry Smith   PetscErrorCode ierr;
559816f7b76SBarry Smith   MPI_Comm       comm;
560816f7b76SBarry Smith   PetscFunctionBegin;
561816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
562816f7b76SBarry Smith   *mcnt = 0;
563ffc4695bSBarry Smith   ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
564816f7b76SBarry Smith   PetscFunctionReturn(0);
565816f7b76SBarry Smith }
566816f7b76SBarry Smith 
567816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
568816f7b76SBarry Smith {
569816f7b76SBarry Smith   PetscErrorCode ierr;
570816f7b76SBarry Smith   MPI_Comm       comm;
571816f7b76SBarry Smith   PetscFunctionBegin;
572816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
573816f7b76SBarry Smith   while (PETSC_TRUE) {
574816f7b76SBarry Smith     if (rank < *mcnt) break;
575ffc4695bSBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
576816f7b76SBarry Smith   }
577816f7b76SBarry Smith   PetscFunctionReturn(0);
578816f7b76SBarry Smith }
579816f7b76SBarry Smith 
580816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
581816f7b76SBarry Smith {
582816f7b76SBarry Smith   PetscErrorCode ierr;
583816f7b76SBarry Smith   MPI_Comm       comm;
584816f7b76SBarry Smith   PetscFunctionBegin;
585816f7b76SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
586816f7b76SBarry Smith   while (PETSC_TRUE) {
587ffc4695bSBarry Smith     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
588816f7b76SBarry Smith     if (!*mcnt) break;
589816f7b76SBarry Smith   }
590816f7b76SBarry Smith   PetscFunctionReturn(0);
591816f7b76SBarry Smith }
592