xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision dbbe0bcd3f3a8fbab5a45420dc06f8387e5764c6)
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 {
199de0f6ecSBarry Smith   PetscFunctionBegin;
209de0f6ecSBarry Smith   if (!*hp) PetscFunctionReturn(0);
219de0f6ecSBarry Smith   kh_destroy(HTPrinted,(*hp)->printed);
229566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferDestroy(&(*hp)->strings));
239566063dSJacob Faibussowitsch   PetscCall(PetscFree(*hp));
249de0f6ecSBarry Smith   PetscFunctionReturn(0);
259de0f6ecSBarry Smith }
269de0f6ecSBarry Smith 
279de0f6ecSBarry Smith /*@C
289de0f6ecSBarry Smith       PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
299de0f6ecSBarry Smith          been printed so they will not be printed again.
309de0f6ecSBarry Smith 
319de0f6ecSBarry Smith      Not collective
329de0f6ecSBarry Smith 
339de0f6ecSBarry Smith     Level: developer
349de0f6ecSBarry Smith 
35db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCheck()`, `PetscOptionsHelpPrintChecked()`
369de0f6ecSBarry Smith @*/
379de0f6ecSBarry Smith PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
389de0f6ecSBarry Smith {
399de0f6ecSBarry Smith   PetscFunctionBegin;
409566063dSJacob Faibussowitsch   PetscCall(PetscNew(hp));
419de0f6ecSBarry Smith   (*hp)->printed = kh_init(HTPrinted);
429566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferCreate(sizeof(char),10000,&(*hp)->strings));
439de0f6ecSBarry Smith   PetscFunctionReturn(0);
449de0f6ecSBarry Smith }
459de0f6ecSBarry Smith 
469de0f6ecSBarry Smith /*@C
479de0f6ecSBarry Smith       PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
489de0f6ecSBarry Smith 
499de0f6ecSBarry Smith      Not collective
509de0f6ecSBarry Smith 
519de0f6ecSBarry Smith     Input Parameters:
529de0f6ecSBarry Smith +     hp - the object used to manage tracking what help messages have been printed
539de0f6ecSBarry Smith .     pre - the prefix part of the string, many be NULL
549de0f6ecSBarry Smith -     name - the string to look for (cannot be NULL)
559de0f6ecSBarry Smith 
569de0f6ecSBarry Smith     Output Parameter:
579de0f6ecSBarry Smith .     found - PETSC_TRUE if the string was already set
589de0f6ecSBarry Smith 
599de0f6ecSBarry Smith     Level: intermediate
609de0f6ecSBarry Smith 
61db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCreate()`
629de0f6ecSBarry Smith @*/
639de0f6ecSBarry Smith PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp,const char *pre,const char* name,PetscBool *found)
649de0f6ecSBarry Smith {
659de0f6ecSBarry Smith   size_t          l1,l2;
66c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
679de0f6ecSBarry Smith   char            *both;
68e8f14785SLisandro Dalcin   int             newitem;
69c1449d8eSBarry Smith #endif
709de0f6ecSBarry Smith 
719de0f6ecSBarry Smith   PetscFunctionBegin;
729566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(pre,&l1));
739566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name,&l2));
749de0f6ecSBarry Smith   if (l1+l2 == 0) {
759de0f6ecSBarry Smith     *found = PETSC_FALSE;
769de0f6ecSBarry Smith     PetscFunctionReturn(0);
779de0f6ecSBarry Smith   }
78c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
799566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(hp->strings,l1+l2+1,&both));
809566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(both,pre));
819566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(both,name));
829de0f6ecSBarry Smith   kh_put(HTPrinted,hp->printed,both,&newitem);
839de0f6ecSBarry Smith   if (!newitem) {
849566063dSJacob Faibussowitsch     PetscCall(PetscSegBufferUnuse(hp->strings,l1+l2+1));
859de0f6ecSBarry Smith   }
869de0f6ecSBarry Smith   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
87c1449d8eSBarry Smith #else
88c1449d8eSBarry Smith   *found = PETSC_FALSE;
89c1449d8eSBarry Smith #endif
909de0f6ecSBarry Smith   PetscFunctionReturn(0);
9194d6a431SBarry Smith }
9294d6a431SBarry Smith 
93eb55bdffSLawrence Mitchell static PetscBool noviewer = PETSC_FALSE;
94eb55bdffSLawrence Mitchell static PetscBool noviewers[PETSCVIEWERGETVIEWEROFFPUSHESMAX];
95eb55bdffSLawrence Mitchell static PetscInt  inoviewers = 0;
96eb55bdffSLawrence Mitchell 
97eb55bdffSLawrence Mitchell /*@
98eb55bdffSLawrence Mitchell   PetscOptionsPushGetViewerOff - control whether PetscOptionsGetViewer returns a viewer.
99eb55bdffSLawrence Mitchell 
100eb55bdffSLawrence Mitchell   Logically Collective
101eb55bdffSLawrence Mitchell 
102eb55bdffSLawrence Mitchell   Input Parameter:
103eb55bdffSLawrence Mitchell . flg - PETSC_TRUE to turn off viewer creation, PETSC_FALSE to turn it on.
104eb55bdffSLawrence Mitchell 
105eb55bdffSLawrence Mitchell   Level: developer
106eb55bdffSLawrence Mitchell 
10795452b02SPatrick Sanan   Notes:
10895452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
109eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
110eb55bdffSLawrence Mitchell 
111db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPopGetViewerOff()`
112eb55bdffSLawrence Mitchell @*/
113eb55bdffSLawrence Mitchell PetscErrorCode  PetscOptionsPushGetViewerOff(PetscBool flg)
114eb55bdffSLawrence Mitchell {
115eb55bdffSLawrence Mitchell   PetscFunctionBegin;
116cc73adaaSBarry Smith   PetscCheck(inoviewers < PETSCVIEWERGETVIEWEROFFPUSHESMAX,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPushGetViewerOff(), perhaps you forgot PetscOptionsPopGetViewerOff()?");
117eb55bdffSLawrence Mitchell 
118eb55bdffSLawrence Mitchell   noviewers[inoviewers++] = noviewer;
119eb55bdffSLawrence Mitchell   noviewer = flg;
120eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
121eb55bdffSLawrence Mitchell }
122eb55bdffSLawrence Mitchell 
123eb55bdffSLawrence Mitchell /*@
124eb55bdffSLawrence Mitchell   PetscOptionsPopGetViewerOff - reset whether PetscOptionsGetViewer returns a viewer.
125eb55bdffSLawrence Mitchell 
126eb55bdffSLawrence Mitchell   Logically Collective
127eb55bdffSLawrence Mitchell 
128eb55bdffSLawrence Mitchell   Level: developer
129eb55bdffSLawrence Mitchell 
13095452b02SPatrick Sanan   Notes:
13195452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
132eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
133eb55bdffSLawrence Mitchell 
134db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`
135eb55bdffSLawrence Mitchell @*/
136dd63322aSSatish Balay PetscErrorCode  PetscOptionsPopGetViewerOff(void)
137eb55bdffSLawrence Mitchell {
138eb55bdffSLawrence Mitchell   PetscFunctionBegin;
13928b400f6SJacob Faibussowitsch   PetscCheck(inoviewers,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPopGetViewerOff(), perhaps you forgot PetscOptionsPushGetViewerOff()?");
140eb55bdffSLawrence Mitchell   noviewer = noviewers[--inoviewers];
141eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
142eb55bdffSLawrence Mitchell }
143eb55bdffSLawrence Mitchell 
144eb55bdffSLawrence Mitchell /*@
145eb55bdffSLawrence Mitchell   PetscOptionsGetViewerOff - does PetscOptionsGetViewer return a viewer?
146eb55bdffSLawrence Mitchell 
147eb55bdffSLawrence Mitchell   Logically Collective
148eb55bdffSLawrence Mitchell 
149eb55bdffSLawrence Mitchell   Output Parameter:
150eb55bdffSLawrence Mitchell . flg - whether viewers are returned.
151eb55bdffSLawrence Mitchell 
152eb55bdffSLawrence Mitchell   Level: developer
153eb55bdffSLawrence Mitchell 
15495452b02SPatrick Sanan   Notes:
15595452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
156eb55bdffSLawrence Mitchell    many small subsolves.
157eb55bdffSLawrence Mitchell 
158db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`
159eb55bdffSLawrence Mitchell @*/
160eb55bdffSLawrence Mitchell PetscErrorCode  PetscOptionsGetViewerOff(PetscBool *flg)
161eb55bdffSLawrence Mitchell {
162eb55bdffSLawrence Mitchell   PetscFunctionBegin;
163534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,1);
164eb55bdffSLawrence Mitchell   *flg = noviewer;
165eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
166eb55bdffSLawrence Mitchell }
167eb55bdffSLawrence Mitchell 
1682bf49c77SBarry Smith /*@C
1692bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
1702bf49c77SBarry Smith 
171d083f849SBarry Smith    Collective
1722bf49c77SBarry Smith 
1732bf49c77SBarry Smith    Input Parameters:
1742bf49c77SBarry Smith +  comm - the communicator to own the viewer
1756b867d5aSJose E. Roman .  options - options database, use NULL for default global database
1760298fd71SBarry Smith .  pre - the string to prepend to the name or NULL
1772bf49c77SBarry Smith -  name - the option one is seeking
1782bf49c77SBarry Smith 
179d8d19677SJose E. Roman    Output Parameters:
180bb1d7374SBarry Smith +  viewer - the viewer, pass NULL if not needed
181bb1d7374SBarry Smith .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
1822bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
1832bf49c77SBarry Smith 
1842bf49c77SBarry Smith    Level: intermediate
1852bf49c77SBarry Smith 
18695452b02SPatrick Sanan    Notes:
18795452b02SPatrick Sanan     If no value is provided ascii:stdout is used
188d1da0b69SBarry Smith $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
189d1da0b69SBarry Smith                                                   for example ascii::ascii_info prints just the information about the object not all details
190d1da0b69SBarry Smith                                                   unless :append is given filename opens in write mode, overwriting what was already there
191d1da0b69SBarry Smith $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
192acd7d2deSBarry Smith $       draw[:drawtype[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  or draw:x
1932bf49c77SBarry Smith $       socket[:port]                             defaults to the standard output port
1942a359c20SBarry Smith $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
1952bf49c77SBarry Smith 
196cffb1e40SBarry Smith    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
1972bf49c77SBarry Smith 
198eb55bdffSLawrence Mitchell    You can control whether calls to this function create a viewer (or return early with *set of PETSC_FALSE) with
199eb55bdffSLawrence Mitchell    PetscOptionsPushGetViewerOff.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
200eb55bdffSLawrence Mitchell    an appreciable fraction of the runtime.
201eb55bdffSLawrence Mitchell 
20227b0f280SBarry Smith    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
20327b0f280SBarry Smith 
204db781477SPatrick Sanan .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
205db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
206db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
207db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
208c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
209db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
210db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
211db781477SPatrick Sanan           `PetscOptionsGetViewerOff()`
2122bf49c77SBarry Smith @*/
21316413a6aSBarry Smith PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
2142bf49c77SBarry Smith {
2152d747510SLisandro Dalcin   const char                     *value;
21620610d12SBarry Smith   PetscBool                      flag,hashelp;
2172bf49c77SBarry Smith 
2182bf49c77SBarry Smith   PetscFunctionBegin;
219064a246eSJacob Faibussowitsch   PetscValidCharPointer(name,4);
2202bf49c77SBarry Smith 
2216348e711SLisandro Dalcin   if (viewer) *viewer = NULL;
2226348e711SLisandro Dalcin   if (format) *format = PETSC_VIEWER_DEFAULT;
22327b0f280SBarry Smith   if (set)    *set    = PETSC_FALSE;
2249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewerOff(&flag));
225eb55bdffSLawrence Mitchell   if (flag) PetscFunctionReturn(0);
22627b0f280SBarry Smith 
2279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasHelp(NULL,&hashelp));
22820610d12SBarry Smith   if (hashelp) {
2299de0f6ecSBarry Smith     PetscBool found;
2309af95d99SBarry Smith 
2319de0f6ecSBarry Smith     if (!PetscOptionsHelpPrintedSingleton) {
2329566063dSJacob Faibussowitsch       PetscCall(PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton));
2339af95d99SBarry Smith     }
2349566063dSJacob Faibussowitsch     PetscCall(PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton,pre,name,&found));
23556071f75SVaclav Hapla     if (!found && viewer) {
2369566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"----------------------------------------\nViewer (-%s%s) options:\n",pre ? pre : "",name+1));
2379566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Prints object to stdout or ASCII file","PetscOptionsGetViewer"));
2389566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Saves object to a binary file","PetscOptionsGetViewer"));
2399566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n",pre ? pre : "",name+1,"Draws object","PetscOptionsGetViewer"));
2409566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Pushes object to a Unix socket","PetscOptionsGetViewer"));
2419566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Publishes object to SAWs","PetscOptionsGetViewer"));
24294d6a431SBarry Smith     }
24320610d12SBarry Smith   }
244685405a1SBarry Smith 
245e3f3e4b6SBarry Smith   if (format) *format = PETSC_VIEWER_DEFAULT;
2469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFindPair(options,pre,name,&value,&flag));
2472bf49c77SBarry Smith   if (flag) {
2482bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
2492bf49c77SBarry Smith     if (!value) {
250bb1d7374SBarry Smith       if (viewer) {
2519566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm,viewer));
2529566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)*viewer));
253bb1d7374SBarry Smith       }
2542bf49c77SBarry Smith     } else {
25535d27ee3SJed Brown       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
2562bf49c77SBarry Smith       PetscInt   cnt;
2571e50132fSMatthew G. Knepley       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,PETSCVIEWERGLVIS,PETSCVIEWEREXODUSII,NULL};
2582bf49c77SBarry Smith 
2599566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(value,&loc0_vtype));
2609566063dSJacob Faibussowitsch       PetscCall(PetscStrchr(loc0_vtype,':',&loc1_fname));
26135d27ee3SJed Brown       if (loc1_fname) {
26235d27ee3SJed Brown         *loc1_fname++ = 0;
2639566063dSJacob Faibussowitsch         PetscCall(PetscStrchr(loc1_fname,':',&loc2_fmt));
26435d27ee3SJed Brown       }
26535d27ee3SJed Brown       if (loc2_fmt) {
26635d27ee3SJed Brown         *loc2_fmt++ = 0;
2679566063dSJacob Faibussowitsch         PetscCall(PetscStrchr(loc2_fmt,':',&loc3_fmode));
26835d27ee3SJed Brown       }
26935d27ee3SJed Brown       if (loc3_fmode) *loc3_fmode++ = 0;
2709566063dSJacob Faibussowitsch       PetscCall(PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt));
27108401ef6SPierre Jolivet       PetscCheck(cnt <= (PetscInt) sizeof(viewers)-1,comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
272bb1d7374SBarry Smith       if (viewer) {
27335d27ee3SJed Brown         if (!loc1_fname) {
27443b63833SBarry Smith           switch (cnt) {
27543b63833SBarry Smith           case 0:
2769566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIIGetStdout(comm,viewer));
27743b63833SBarry Smith             break;
27843b63833SBarry Smith           case 1:
2799566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) PetscCall(PETSC_ERR_PLIB);
28043b63833SBarry Smith             break;
28143b63833SBarry Smith           case 2:
2829566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) PetscCall(PETSC_ERR_PLIB);
28343b63833SBarry Smith             break;
284b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
28543b63833SBarry Smith           case 3:
2869566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) PetscCall(PETSC_ERR_PLIB);
28743b63833SBarry Smith             break;
288b58ca069SBarry Smith #endif
28943b63833SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
29043b63833SBarry Smith           case 4:
2919566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) PetscCall(PETSC_ERR_PLIB);
29243b63833SBarry Smith             break;
29343b63833SBarry Smith #endif
294e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
295bfb97211SBarry Smith           case 5:
2969566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) PetscCall(PETSC_ERR_PLIB);
297bfb97211SBarry Smith             break;
298bfb97211SBarry Smith #endif
299a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
300a75e6a4aSMatthew G. Knepley           case 7:
3019566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) PetscCall(PETSC_ERR_PLIB);
302a75e6a4aSMatthew G. Knepley             break;
303a75e6a4aSMatthew G. Knepley #endif
3048135c375SStefano Zampini           case 8:
3059566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) PetscCall(PETSC_ERR_PLIB);
3068135c375SStefano Zampini             break;
3071e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
3081e50132fSMatthew G. Knepley           case 9:
3099566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) PetscCall(PETSC_ERR_PLIB);
3101e50132fSMatthew G. Knepley             break;
3111e50132fSMatthew G. Knepley #endif
31298921bdaSJacob Faibussowitsch           default: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
3137f677774SBarry Smith           }
3149566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)*viewer));
3157f677774SBarry Smith         } else {
31635d27ee3SJed Brown           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
3179566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIIGetStdout(comm,viewer));
3189566063dSJacob Faibussowitsch             PetscCall(PetscObjectReference((PetscObject)*viewer));
3197f677774SBarry Smith           } else {
3203550efbcSJed Brown             PetscFileMode fmode;
3219566063dSJacob Faibussowitsch             PetscCall(PetscViewerCreate(comm,viewer));
3229566063dSJacob Faibussowitsch             PetscCall(PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii"));
3233550efbcSJed Brown             fmode = FILE_MODE_WRITE;
3243550efbcSJed Brown             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
3259566063dSJacob Faibussowitsch               PetscCall(PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag));
32628b400f6SJacob Faibussowitsch               PetscCheck(flag,comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
3277f677774SBarry Smith             }
328acd7d2deSBarry Smith             if (loc2_fmt) {
3290ecfd9fcSLisandro Dalcin               PetscBool tk,im;
3309566063dSJacob Faibussowitsch               PetscCall(PetscStrcmp(loc1_fname,"tikz",&tk));
3319566063dSJacob Faibussowitsch               PetscCall(PetscStrcmp(loc1_fname,"image",&im));
3320ecfd9fcSLisandro Dalcin               if (tk || im) {
3339566063dSJacob Faibussowitsch                 PetscCall(PetscViewerDrawSetInfo(*viewer,NULL,loc2_fmt,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE));
334acd7d2deSBarry Smith                 *loc2_fmt = 0;
335acd7d2deSBarry Smith               }
336acd7d2deSBarry Smith             }
3379566063dSJacob Faibussowitsch             PetscCall(PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE));
3389566063dSJacob Faibussowitsch             PetscCall(PetscViewerFileSetName(*viewer,loc1_fname));
3391baa6e33SBarry Smith             if (*loc1_fname) PetscCall(PetscViewerDrawSetDrawType(*viewer,loc1_fname));
3409566063dSJacob Faibussowitsch             PetscCall(PetscViewerSetFromOptions(*viewer));
34105315717SToby Isaac           }
342bb1d7374SBarry Smith         }
34352f76066SLisandro Dalcin       }
3441baa6e33SBarry Smith       if (viewer) PetscCall(PetscViewerSetUp(*viewer));
34535d27ee3SJed Brown       if (loc2_fmt && *loc2_fmt) {
346e156c29bSStefano Zampini         PetscViewerFormat tfmt;
347d6acdc46SStefano Zampini 
3489566063dSJacob Faibussowitsch         PetscCall(PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)&tfmt,&flag));
349d6acdc46SStefano Zampini         if (format) *format = tfmt;
35028b400f6SJacob Faibussowitsch         PetscCheck(flag,PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
351d6acdc46SStefano Zampini       } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
3529566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetFormat(*viewer,format));
3537f677774SBarry Smith       }
3549566063dSJacob Faibussowitsch       PetscCall(PetscFree(loc0_vtype));
3552bf49c77SBarry Smith     }
3562bf49c77SBarry Smith   }
3572bf49c77SBarry Smith   PetscFunctionReturn(0);
3582bf49c77SBarry Smith }
3592bf49c77SBarry Smith 
3605c6c1daeSBarry Smith /*@
3615c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
3625c6c1daeSBarry Smith 
363d083f849SBarry Smith    Collective
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith    Input Parameter:
3665c6c1daeSBarry Smith .  comm - MPI communicator
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith    Output Parameter:
3695c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
3705c6c1daeSBarry Smith 
3715c6c1daeSBarry Smith    Level: advanced
3725c6c1daeSBarry Smith 
373db781477SPatrick Sanan .seealso: `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerType`
3745c6c1daeSBarry Smith 
3755c6c1daeSBarry Smith @*/
3765c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
3775c6c1daeSBarry Smith {
3785c6c1daeSBarry Smith   PetscViewer    viewer;
3795c6c1daeSBarry Smith 
3805c6c1daeSBarry Smith   PetscFunctionBegin;
38102c9f0b5SLisandro Dalcin   *inviewer = NULL;
3829566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
3839566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,PetscViewerView));
3845c6c1daeSBarry Smith   *inviewer    = viewer;
38502c9f0b5SLisandro Dalcin   viewer->data = NULL;
3865c6c1daeSBarry Smith   PetscFunctionReturn(0);
3875c6c1daeSBarry Smith }
3885c6c1daeSBarry Smith 
3895c6c1daeSBarry Smith /*@C
3905c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith    Collective on PetscViewer
3935c6c1daeSBarry Smith 
394d8d19677SJose E. Roman    Input Parameters:
3955c6c1daeSBarry Smith +  viewer      - the PetscViewer context
3968f6c3df8SBarry Smith -  type        - for example, PETSCVIEWERASCII
3975c6c1daeSBarry Smith 
3985c6c1daeSBarry Smith    Options Database Command:
399621f4a4dSVáclav Hapla .  -viewer_type  <type> - Sets the type; use -help for a list
4005c6c1daeSBarry Smith     of available methods (for instance, ascii)
4015c6c1daeSBarry Smith 
4025c6c1daeSBarry Smith    Level: advanced
4035c6c1daeSBarry Smith 
4045c6c1daeSBarry Smith    Notes:
4055c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
4068f6c3df8SBarry Smith    PETSCVIEWERSOCKET)
4075c6c1daeSBarry Smith 
408db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerGetType()`, `PetscViewerType`, `PetscViewerPushFormat()`
4095c6c1daeSBarry Smith @*/
4105c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
4115c6c1daeSBarry Smith {
4125c6c1daeSBarry Smith   PetscBool      match;
4135f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PetscViewer);
4145c6c1daeSBarry Smith 
4155c6c1daeSBarry Smith   PetscFunctionBegin;
4165c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4175c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
4189566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,type,&match));
4195c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
4205c6c1daeSBarry Smith 
4215c6c1daeSBarry Smith   /* cleanup any old type that may be there */
422*dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer,destroy);
4230298fd71SBarry Smith   viewer->ops->destroy = NULL;
42402c9f0b5SLisandro Dalcin   viewer->data         = NULL;
425*dbbe0bcdSBarry Smith 
4269566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps)));
4275c6c1daeSBarry Smith 
4289566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscViewerList,type,&r));
42928b400f6SJacob Faibussowitsch   PetscCheck(r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
4305c6c1daeSBarry Smith 
4319566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)viewer,type));
4329566063dSJacob Faibussowitsch   PetscCall((*r)(viewer));
4335c6c1daeSBarry Smith   PetscFunctionReturn(0);
4345c6c1daeSBarry Smith }
4355c6c1daeSBarry Smith 
4361c84c290SBarry Smith /*@C
4371c84c290SBarry Smith    PetscViewerRegister - Adds a viewer
4381c84c290SBarry Smith 
4391c84c290SBarry Smith    Not Collective
4401c84c290SBarry Smith 
4411c84c290SBarry Smith    Input Parameters:
4421c84c290SBarry Smith +  name_solver - name of a new user-defined viewer
4431c84c290SBarry Smith -  routine_create - routine to create method context
4441c84c290SBarry Smith 
4451c84c290SBarry Smith    Level: developer
4461c84c290SBarry Smith    Notes:
4471c84c290SBarry Smith    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
4481c84c290SBarry Smith 
4491c84c290SBarry Smith    Sample usage:
4501c84c290SBarry Smith .vb
451bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type",MyViewerCreate);
4521c84c290SBarry Smith .ve
4531c84c290SBarry Smith 
4541c84c290SBarry Smith    Then, your solver can be chosen with the procedural interface via
4551c84c290SBarry Smith $     PetscViewerSetType(viewer,"my_viewer_type")
4561c84c290SBarry Smith    or at runtime via the option
4571c84c290SBarry Smith $     -viewer_type my_viewer_type
4581c84c290SBarry Smith 
459db781477SPatrick Sanan .seealso: `PetscViewerRegisterAll()`
4601c84c290SBarry Smith  @*/
461bdf89e91SBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
4625c6c1daeSBarry Smith {
4635c6c1daeSBarry Smith   PetscFunctionBegin;
4649566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
4659566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscViewerList,sname,function));
4665c6c1daeSBarry Smith   PetscFunctionReturn(0);
4675c6c1daeSBarry Smith }
4685c6c1daeSBarry Smith 
4695c6c1daeSBarry Smith /*@C
4705c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
4715c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
4725c6c1daeSBarry Smith 
4735c6c1daeSBarry Smith    Collective on PetscViewer
4745c6c1daeSBarry Smith 
4755c6c1daeSBarry Smith    Input Parameter:
4765c6c1daeSBarry Smith .     PetscViewer - the graphics context
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith    Level: intermediate
4795c6c1daeSBarry Smith 
4805c6c1daeSBarry Smith    Notes:
4815c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
4825c6c1daeSBarry Smith 
483db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith @*/
4865c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
4875c6c1daeSBarry Smith {
4885c6c1daeSBarry Smith   char              vtype[256];
4895c6c1daeSBarry Smith   PetscBool         flg;
4905c6c1daeSBarry Smith 
4915c6c1daeSBarry Smith   PetscFunctionBegin;
4925c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4935c6c1daeSBarry Smith 
4945c6c1daeSBarry Smith   if (!PetscViewerList) {
4959566063dSJacob Faibussowitsch     PetscCall(PetscViewerRegisterAll());
4965c6c1daeSBarry Smith   }
497d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)viewer);
4989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg));
4991baa6e33SBarry Smith   if (flg) PetscCall(PetscViewerSetType(viewer,vtype));
5005c6c1daeSBarry Smith   /* type has not been set? */
5015c6c1daeSBarry Smith   if (!((PetscObject)viewer)->type_name) {
5029566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer,PETSCVIEWERASCII));
5035c6c1daeSBarry Smith   }
504*dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer,setfromoptions,PetscOptionsObject);
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
507*dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)viewer,PetscOptionsObject));
5089566063dSJacob Faibussowitsch   PetscCall(PetscViewerViewFromOptions(viewer,NULL,"-viewer_view"));
509d0609cedSBarry Smith   PetscOptionsEnd();
5105c6c1daeSBarry Smith   PetscFunctionReturn(0);
5115c6c1daeSBarry Smith }
512816f7b76SBarry Smith 
513816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
514816f7b76SBarry Smith {
515816f7b76SBarry Smith   PetscFunctionBegin;
5169566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer,mcnt));
5179566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer,cnt));
518816f7b76SBarry Smith   PetscFunctionReturn(0);
519816f7b76SBarry Smith }
520816f7b76SBarry Smith 
5219dddd249SSatish Balay PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
522816f7b76SBarry Smith {
523816f7b76SBarry Smith   MPI_Comm       comm;
524816f7b76SBarry Smith 
525816f7b76SBarry Smith   PetscFunctionBegin;
5269566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm));
527816f7b76SBarry Smith   if (i >= *mcnt) {
528816f7b76SBarry Smith     *mcnt += cnt;
5299566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt,1,MPIU_INT,0,comm));
530816f7b76SBarry Smith   }
531816f7b76SBarry Smith   PetscFunctionReturn(0);
532816f7b76SBarry Smith }
533816f7b76SBarry Smith 
5349dddd249SSatish Balay PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer,PetscInt *mcnt)
535816f7b76SBarry Smith {
536816f7b76SBarry Smith   MPI_Comm       comm;
537816f7b76SBarry Smith   PetscFunctionBegin;
5389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm));
539816f7b76SBarry Smith   *mcnt = 0;
5409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(mcnt,1,MPIU_INT,0,comm));
541816f7b76SBarry Smith   PetscFunctionReturn(0);
542816f7b76SBarry Smith }
543816f7b76SBarry Smith 
544816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
545816f7b76SBarry Smith {
546816f7b76SBarry Smith   MPI_Comm       comm;
547816f7b76SBarry Smith   PetscFunctionBegin;
5489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm));
549816f7b76SBarry Smith   while (PETSC_TRUE) {
550816f7b76SBarry Smith     if (rank < *mcnt) break;
5519566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt,1,MPIU_INT,0,comm));
552816f7b76SBarry Smith   }
553816f7b76SBarry Smith   PetscFunctionReturn(0);
554816f7b76SBarry Smith }
555816f7b76SBarry Smith 
556816f7b76SBarry Smith PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
557816f7b76SBarry Smith {
558816f7b76SBarry Smith   MPI_Comm       comm;
559816f7b76SBarry Smith   PetscFunctionBegin;
5609566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer,&comm));
561816f7b76SBarry Smith   while (PETSC_TRUE) {
5629566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt,1,MPIU_INT,0,comm));
563816f7b76SBarry Smith     if (!*mcnt) break;
564816f7b76SBarry Smith   }
565816f7b76SBarry Smith   PetscFunctionReturn(0);
566816f7b76SBarry Smith }
567