xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
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 
179371c9d4SSatish Balay PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp) {
189de0f6ecSBarry Smith   PetscFunctionBegin;
199de0f6ecSBarry Smith   if (!*hp) PetscFunctionReturn(0);
209de0f6ecSBarry Smith   kh_destroy(HTPrinted, (*hp)->printed);
219566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferDestroy(&(*hp)->strings));
229566063dSJacob Faibussowitsch   PetscCall(PetscFree(*hp));
239de0f6ecSBarry Smith   PetscFunctionReturn(0);
249de0f6ecSBarry Smith }
259de0f6ecSBarry Smith 
269de0f6ecSBarry Smith /*@C
279de0f6ecSBarry Smith       PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
289de0f6ecSBarry Smith          been printed so they will not be printed again.
299de0f6ecSBarry Smith 
309de0f6ecSBarry Smith      Not collective
319de0f6ecSBarry Smith 
329de0f6ecSBarry Smith     Level: developer
339de0f6ecSBarry Smith 
34db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCheck()`, `PetscOptionsHelpPrintChecked()`
359de0f6ecSBarry Smith @*/
369371c9d4SSatish Balay PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp) {
379de0f6ecSBarry Smith   PetscFunctionBegin;
389566063dSJacob Faibussowitsch   PetscCall(PetscNew(hp));
399de0f6ecSBarry Smith   (*hp)->printed = kh_init(HTPrinted);
409566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferCreate(sizeof(char), 10000, &(*hp)->strings));
419de0f6ecSBarry Smith   PetscFunctionReturn(0);
429de0f6ecSBarry Smith }
439de0f6ecSBarry Smith 
449de0f6ecSBarry Smith /*@C
459de0f6ecSBarry Smith       PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
469de0f6ecSBarry Smith 
479de0f6ecSBarry Smith      Not collective
489de0f6ecSBarry Smith 
499de0f6ecSBarry Smith     Input Parameters:
509de0f6ecSBarry Smith +     hp - the object used to manage tracking what help messages have been printed
519de0f6ecSBarry Smith .     pre - the prefix part of the string, many be NULL
529de0f6ecSBarry Smith -     name - the string to look for (cannot be NULL)
539de0f6ecSBarry Smith 
549de0f6ecSBarry Smith     Output Parameter:
559de0f6ecSBarry Smith .     found - PETSC_TRUE if the string was already set
569de0f6ecSBarry Smith 
579de0f6ecSBarry Smith     Level: intermediate
589de0f6ecSBarry Smith 
59db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCreate()`
609de0f6ecSBarry Smith @*/
619371c9d4SSatish Balay PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp, const char *pre, const char *name, PetscBool *found) {
629de0f6ecSBarry Smith   size_t l1, l2;
63c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
649de0f6ecSBarry Smith   char *both;
65e8f14785SLisandro Dalcin   int   newitem;
66c1449d8eSBarry Smith #endif
679de0f6ecSBarry Smith 
689de0f6ecSBarry Smith   PetscFunctionBegin;
699566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(pre, &l1));
709566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &l2));
719de0f6ecSBarry Smith   if (l1 + l2 == 0) {
729de0f6ecSBarry Smith     *found = PETSC_FALSE;
739de0f6ecSBarry Smith     PetscFunctionReturn(0);
749de0f6ecSBarry Smith   }
75c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
769566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(hp->strings, l1 + l2 + 1, &both));
779566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(both, pre));
789566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(both, name));
799de0f6ecSBarry Smith   kh_put(HTPrinted, hp->printed, both, &newitem);
80*48a46eb9SPierre Jolivet   if (!newitem) PetscCall(PetscSegBufferUnuse(hp->strings, l1 + l2 + 1));
819de0f6ecSBarry Smith   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
82c1449d8eSBarry Smith #else
83c1449d8eSBarry Smith   *found = PETSC_FALSE;
84c1449d8eSBarry Smith #endif
859de0f6ecSBarry Smith   PetscFunctionReturn(0);
8694d6a431SBarry Smith }
8794d6a431SBarry Smith 
88eb55bdffSLawrence Mitchell static PetscBool noviewer = PETSC_FALSE;
89eb55bdffSLawrence Mitchell static PetscBool noviewers[PETSCVIEWERGETVIEWEROFFPUSHESMAX];
90eb55bdffSLawrence Mitchell static PetscInt  inoviewers = 0;
91eb55bdffSLawrence Mitchell 
92eb55bdffSLawrence Mitchell /*@
93eb55bdffSLawrence Mitchell   PetscOptionsPushGetViewerOff - control whether PetscOptionsGetViewer returns a viewer.
94eb55bdffSLawrence Mitchell 
95eb55bdffSLawrence Mitchell   Logically Collective
96eb55bdffSLawrence Mitchell 
97eb55bdffSLawrence Mitchell   Input Parameter:
98eb55bdffSLawrence Mitchell . flg - PETSC_TRUE to turn off viewer creation, PETSC_FALSE to turn it on.
99eb55bdffSLawrence Mitchell 
100eb55bdffSLawrence Mitchell   Level: developer
101eb55bdffSLawrence Mitchell 
10295452b02SPatrick Sanan   Notes:
10395452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
104eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
105eb55bdffSLawrence Mitchell 
106db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPopGetViewerOff()`
107eb55bdffSLawrence Mitchell @*/
1089371c9d4SSatish Balay PetscErrorCode PetscOptionsPushGetViewerOff(PetscBool flg) {
109eb55bdffSLawrence Mitchell   PetscFunctionBegin;
110cc73adaaSBarry Smith   PetscCheck(inoviewers < PETSCVIEWERGETVIEWEROFFPUSHESMAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPushGetViewerOff(), perhaps you forgot PetscOptionsPopGetViewerOff()?");
111eb55bdffSLawrence Mitchell 
112eb55bdffSLawrence Mitchell   noviewers[inoviewers++] = noviewer;
113eb55bdffSLawrence Mitchell   noviewer                = flg;
114eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
115eb55bdffSLawrence Mitchell }
116eb55bdffSLawrence Mitchell 
117eb55bdffSLawrence Mitchell /*@
118eb55bdffSLawrence Mitchell   PetscOptionsPopGetViewerOff - reset whether PetscOptionsGetViewer returns a viewer.
119eb55bdffSLawrence Mitchell 
120eb55bdffSLawrence Mitchell   Logically Collective
121eb55bdffSLawrence Mitchell 
122eb55bdffSLawrence Mitchell   Level: developer
123eb55bdffSLawrence Mitchell 
12495452b02SPatrick Sanan   Notes:
12595452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
126eb55bdffSLawrence Mitchell    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
127eb55bdffSLawrence Mitchell 
128db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`
129eb55bdffSLawrence Mitchell @*/
1309371c9d4SSatish Balay PetscErrorCode PetscOptionsPopGetViewerOff(void) {
131eb55bdffSLawrence Mitchell   PetscFunctionBegin;
13228b400f6SJacob Faibussowitsch   PetscCheck(inoviewers, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPopGetViewerOff(), perhaps you forgot PetscOptionsPushGetViewerOff()?");
133eb55bdffSLawrence Mitchell   noviewer = noviewers[--inoviewers];
134eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
135eb55bdffSLawrence Mitchell }
136eb55bdffSLawrence Mitchell 
137eb55bdffSLawrence Mitchell /*@
138eb55bdffSLawrence Mitchell   PetscOptionsGetViewerOff - does PetscOptionsGetViewer return a viewer?
139eb55bdffSLawrence Mitchell 
140eb55bdffSLawrence Mitchell   Logically Collective
141eb55bdffSLawrence Mitchell 
142eb55bdffSLawrence Mitchell   Output Parameter:
143eb55bdffSLawrence Mitchell . flg - whether viewers are returned.
144eb55bdffSLawrence Mitchell 
145eb55bdffSLawrence Mitchell   Level: developer
146eb55bdffSLawrence Mitchell 
14795452b02SPatrick Sanan   Notes:
14895452b02SPatrick Sanan     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
149eb55bdffSLawrence Mitchell    many small subsolves.
150eb55bdffSLawrence Mitchell 
151db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`
152eb55bdffSLawrence Mitchell @*/
1539371c9d4SSatish Balay PetscErrorCode PetscOptionsGetViewerOff(PetscBool *flg) {
154eb55bdffSLawrence Mitchell   PetscFunctionBegin;
155534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 1);
156eb55bdffSLawrence Mitchell   *flg = noviewer;
157eb55bdffSLawrence Mitchell   PetscFunctionReturn(0);
158eb55bdffSLawrence Mitchell }
159eb55bdffSLawrence Mitchell 
1602bf49c77SBarry Smith /*@C
1612bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
1622bf49c77SBarry Smith 
163d083f849SBarry Smith    Collective
1642bf49c77SBarry Smith 
1652bf49c77SBarry Smith    Input Parameters:
1662bf49c77SBarry Smith +  comm - the communicator to own the viewer
1676b867d5aSJose E. Roman .  options - options database, use NULL for default global database
1680298fd71SBarry Smith .  pre - the string to prepend to the name or NULL
1692bf49c77SBarry Smith -  name - the option one is seeking
1702bf49c77SBarry Smith 
171d8d19677SJose E. Roman    Output Parameters:
172bb1d7374SBarry Smith +  viewer - the viewer, pass NULL if not needed
173bb1d7374SBarry Smith .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
1742bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
1752bf49c77SBarry Smith 
1762bf49c77SBarry Smith    Level: intermediate
1772bf49c77SBarry Smith 
17895452b02SPatrick Sanan    Notes:
17995452b02SPatrick Sanan     If no value is provided ascii:stdout is used
180d1da0b69SBarry Smith $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
181d1da0b69SBarry Smith                                                   for example ascii::ascii_info prints just the information about the object not all details
182d1da0b69SBarry Smith                                                   unless :append is given filename opens in write mode, overwriting what was already there
183d1da0b69SBarry Smith $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
184acd7d2deSBarry Smith $       draw[:drawtype[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  or draw:x
1852bf49c77SBarry Smith $       socket[:port]                             defaults to the standard output port
1862a359c20SBarry Smith $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
1872bf49c77SBarry Smith 
188cffb1e40SBarry Smith    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
1892bf49c77SBarry Smith 
190eb55bdffSLawrence Mitchell    You can control whether calls to this function create a viewer (or return early with *set of PETSC_FALSE) with
191eb55bdffSLawrence Mitchell    PetscOptionsPushGetViewerOff.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
192eb55bdffSLawrence Mitchell    an appreciable fraction of the runtime.
193eb55bdffSLawrence Mitchell 
19427b0f280SBarry Smith    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
19527b0f280SBarry Smith 
196db781477SPatrick Sanan .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
197db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
198db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
199db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
200c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
201db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
202db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
203db781477SPatrick Sanan           `PetscOptionsGetViewerOff()`
2042bf49c77SBarry Smith @*/
2059371c9d4SSatish Balay PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscViewer *viewer, PetscViewerFormat *format, PetscBool *set) {
2062d747510SLisandro Dalcin   const char *value;
20720610d12SBarry Smith   PetscBool   flag, hashelp;
2082bf49c77SBarry Smith 
2092bf49c77SBarry Smith   PetscFunctionBegin;
210064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 4);
2112bf49c77SBarry Smith 
2126348e711SLisandro Dalcin   if (viewer) *viewer = NULL;
2136348e711SLisandro Dalcin   if (format) *format = PETSC_VIEWER_DEFAULT;
21427b0f280SBarry Smith   if (set) *set = PETSC_FALSE;
2159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewerOff(&flag));
216eb55bdffSLawrence Mitchell   if (flag) PetscFunctionReturn(0);
21727b0f280SBarry Smith 
2189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasHelp(NULL, &hashelp));
21920610d12SBarry Smith   if (hashelp) {
2209de0f6ecSBarry Smith     PetscBool found;
2219af95d99SBarry Smith 
222*48a46eb9SPierre Jolivet     if (!PetscOptionsHelpPrintedSingleton) PetscCall(PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton));
2239566063dSJacob Faibussowitsch     PetscCall(PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton, pre, name, &found));
22456071f75SVaclav Hapla     if (!found && viewer) {
2259566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\nViewer (-%s%s) options:\n", pre ? pre : "", name + 1));
2269566063dSJacob 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"));
2279566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n", pre ? pre : "", name + 1, "Saves object to a binary file", "PetscOptionsGetViewer"));
2289566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n", pre ? pre : "", name + 1, "Draws object", "PetscOptionsGetViewer"));
2299566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s socket[:port]: %s (%s)\n", pre ? pre : "", name + 1, "Pushes object to a Unix socket", "PetscOptionsGetViewer"));
2309566063dSJacob Faibussowitsch       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s saws[:communicatorname]: %s (%s)\n", pre ? pre : "", name + 1, "Publishes object to SAWs", "PetscOptionsGetViewer"));
23194d6a431SBarry Smith     }
23220610d12SBarry Smith   }
233685405a1SBarry Smith 
234e3f3e4b6SBarry Smith   if (format) *format = PETSC_VIEWER_DEFAULT;
2359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2362bf49c77SBarry Smith   if (flag) {
2372bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
2382bf49c77SBarry Smith     if (!value) {
239bb1d7374SBarry Smith       if (viewer) {
2409566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
2419566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)*viewer));
242bb1d7374SBarry Smith       }
2432bf49c77SBarry Smith     } else {
24435d27ee3SJed Brown       char       *loc0_vtype, *loc1_fname, *loc2_fmt = NULL, *loc3_fmode = NULL;
2452bf49c77SBarry Smith       PetscInt    cnt;
2461e50132fSMatthew G. Knepley       const char *viewers[] = {PETSCVIEWERASCII, PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSOCKET, PETSCVIEWERMATLAB, PETSCVIEWERSAWS, PETSCVIEWERVTK, PETSCVIEWERHDF5, PETSCVIEWERGLVIS, PETSCVIEWEREXODUSII, NULL};
2472bf49c77SBarry Smith 
2489566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(value, &loc0_vtype));
2499566063dSJacob Faibussowitsch       PetscCall(PetscStrchr(loc0_vtype, ':', &loc1_fname));
25035d27ee3SJed Brown       if (loc1_fname) {
25135d27ee3SJed Brown         *loc1_fname++ = 0;
2529566063dSJacob Faibussowitsch         PetscCall(PetscStrchr(loc1_fname, ':', &loc2_fmt));
25335d27ee3SJed Brown       }
25435d27ee3SJed Brown       if (loc2_fmt) {
25535d27ee3SJed Brown         *loc2_fmt++ = 0;
2569566063dSJacob Faibussowitsch         PetscCall(PetscStrchr(loc2_fmt, ':', &loc3_fmode));
25735d27ee3SJed Brown       }
25835d27ee3SJed Brown       if (loc3_fmode) *loc3_fmode++ = 0;
2599566063dSJacob Faibussowitsch       PetscCall(PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii", viewers, &cnt));
26008401ef6SPierre Jolivet       PetscCheck(cnt <= (PetscInt)sizeof(viewers) - 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown viewer type: %s", loc0_vtype);
261bb1d7374SBarry Smith       if (viewer) {
26235d27ee3SJed Brown         if (!loc1_fname) {
26343b63833SBarry Smith           switch (cnt) {
2649371c9d4SSatish Balay           case 0: PetscCall(PetscViewerASCIIGetStdout(comm, viewer)); break;
26543b63833SBarry Smith           case 1:
2669566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) PetscCall(PETSC_ERR_PLIB);
26743b63833SBarry Smith             break;
26843b63833SBarry Smith           case 2:
2699566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) PetscCall(PETSC_ERR_PLIB);
27043b63833SBarry Smith             break;
271b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
27243b63833SBarry Smith           case 3:
2739566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) PetscCall(PETSC_ERR_PLIB);
27443b63833SBarry Smith             break;
275b58ca069SBarry Smith #endif
27643b63833SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
27743b63833SBarry Smith           case 4:
2789566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) PetscCall(PETSC_ERR_PLIB);
27943b63833SBarry Smith             break;
28043b63833SBarry Smith #endif
281e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
282bfb97211SBarry Smith           case 5:
2839566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) PetscCall(PETSC_ERR_PLIB);
284bfb97211SBarry Smith             break;
285bfb97211SBarry Smith #endif
286a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
287a75e6a4aSMatthew G. Knepley           case 7:
2889566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) PetscCall(PETSC_ERR_PLIB);
289a75e6a4aSMatthew G. Knepley             break;
290a75e6a4aSMatthew G. Knepley #endif
2918135c375SStefano Zampini           case 8:
2929566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) PetscCall(PETSC_ERR_PLIB);
2938135c375SStefano Zampini             break;
2941e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
2951e50132fSMatthew G. Knepley           case 9:
2969566063dSJacob Faibussowitsch             if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) PetscCall(PETSC_ERR_PLIB);
2971e50132fSMatthew G. Knepley             break;
2981e50132fSMatthew G. Knepley #endif
29998921bdaSJacob Faibussowitsch           default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unsupported viewer %s", loc0_vtype);
3007f677774SBarry Smith           }
3019566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)*viewer));
3027f677774SBarry Smith         } else {
30335d27ee3SJed Brown           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
3049566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
3059566063dSJacob Faibussowitsch             PetscCall(PetscObjectReference((PetscObject)*viewer));
3067f677774SBarry Smith           } else {
3073550efbcSJed Brown             PetscFileMode fmode;
3089566063dSJacob Faibussowitsch             PetscCall(PetscViewerCreate(comm, viewer));
3099566063dSJacob Faibussowitsch             PetscCall(PetscViewerSetType(*viewer, *loc0_vtype ? loc0_vtype : "ascii"));
3103550efbcSJed Brown             fmode = FILE_MODE_WRITE;
3113550efbcSJed Brown             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
3129566063dSJacob Faibussowitsch               PetscCall(PetscEnumFind(PetscFileModes, loc3_fmode, (PetscEnum *)&fmode, &flag));
31328b400f6SJacob Faibussowitsch               PetscCheck(flag, comm, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown file mode: %s", loc3_fmode);
3147f677774SBarry Smith             }
315acd7d2deSBarry Smith             if (loc2_fmt) {
3160ecfd9fcSLisandro Dalcin               PetscBool tk, im;
3179566063dSJacob Faibussowitsch               PetscCall(PetscStrcmp(loc1_fname, "tikz", &tk));
3189566063dSJacob Faibussowitsch               PetscCall(PetscStrcmp(loc1_fname, "image", &im));
3190ecfd9fcSLisandro Dalcin               if (tk || im) {
3209566063dSJacob Faibussowitsch                 PetscCall(PetscViewerDrawSetInfo(*viewer, NULL, loc2_fmt, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE));
321acd7d2deSBarry Smith                 *loc2_fmt = 0;
322acd7d2deSBarry Smith               }
323acd7d2deSBarry Smith             }
3249566063dSJacob Faibussowitsch             PetscCall(PetscViewerFileSetMode(*viewer, flag ? fmode : FILE_MODE_WRITE));
3259566063dSJacob Faibussowitsch             PetscCall(PetscViewerFileSetName(*viewer, loc1_fname));
3261baa6e33SBarry Smith             if (*loc1_fname) PetscCall(PetscViewerDrawSetDrawType(*viewer, loc1_fname));
3279566063dSJacob Faibussowitsch             PetscCall(PetscViewerSetFromOptions(*viewer));
32805315717SToby Isaac           }
329bb1d7374SBarry Smith         }
33052f76066SLisandro Dalcin       }
3311baa6e33SBarry Smith       if (viewer) PetscCall(PetscViewerSetUp(*viewer));
33235d27ee3SJed Brown       if (loc2_fmt && *loc2_fmt) {
333e156c29bSStefano Zampini         PetscViewerFormat tfmt;
334d6acdc46SStefano Zampini 
3359566063dSJacob Faibussowitsch         PetscCall(PetscEnumFind(PetscViewerFormats, loc2_fmt, (PetscEnum *)&tfmt, &flag));
336d6acdc46SStefano Zampini         if (format) *format = tfmt;
33728b400f6SJacob Faibussowitsch         PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_SUP, "Unknown viewer format %s", loc2_fmt);
338d6acdc46SStefano Zampini       } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
3399566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetFormat(*viewer, format));
3407f677774SBarry Smith       }
3419566063dSJacob Faibussowitsch       PetscCall(PetscFree(loc0_vtype));
3422bf49c77SBarry Smith     }
3432bf49c77SBarry Smith   }
3442bf49c77SBarry Smith   PetscFunctionReturn(0);
3452bf49c77SBarry Smith }
3462bf49c77SBarry Smith 
3475c6c1daeSBarry Smith /*@
3485c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
3495c6c1daeSBarry Smith 
350d083f849SBarry Smith    Collective
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith    Input Parameter:
3535c6c1daeSBarry Smith .  comm - MPI communicator
3545c6c1daeSBarry Smith 
3555c6c1daeSBarry Smith    Output Parameter:
3565c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
3575c6c1daeSBarry Smith 
3585c6c1daeSBarry Smith    Level: advanced
3595c6c1daeSBarry Smith 
360db781477SPatrick Sanan .seealso: `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerType`
3615c6c1daeSBarry Smith 
3625c6c1daeSBarry Smith @*/
3639371c9d4SSatish Balay PetscErrorCode PetscViewerCreate(MPI_Comm comm, PetscViewer *inviewer) {
3645c6c1daeSBarry Smith   PetscViewer viewer;
3655c6c1daeSBarry Smith 
3665c6c1daeSBarry Smith   PetscFunctionBegin;
36702c9f0b5SLisandro Dalcin   *inviewer = NULL;
3689566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
3699566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(viewer, PETSC_VIEWER_CLASSID, "PetscViewer", "PetscViewer", "Viewer", comm, PetscViewerDestroy, PetscViewerView));
3705c6c1daeSBarry Smith   *inviewer    = viewer;
37102c9f0b5SLisandro Dalcin   viewer->data = NULL;
3725c6c1daeSBarry Smith   PetscFunctionReturn(0);
3735c6c1daeSBarry Smith }
3745c6c1daeSBarry Smith 
3755c6c1daeSBarry Smith /*@C
3765c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith    Collective on PetscViewer
3795c6c1daeSBarry Smith 
380d8d19677SJose E. Roman    Input Parameters:
3815c6c1daeSBarry Smith +  viewer      - the PetscViewer context
3828f6c3df8SBarry Smith -  type        - for example, PETSCVIEWERASCII
3835c6c1daeSBarry Smith 
3845c6c1daeSBarry Smith    Options Database Command:
385621f4a4dSVáclav Hapla .  -viewer_type  <type> - Sets the type; use -help for a list
3865c6c1daeSBarry Smith     of available methods (for instance, ascii)
3875c6c1daeSBarry Smith 
3885c6c1daeSBarry Smith    Level: advanced
3895c6c1daeSBarry Smith 
3905c6c1daeSBarry Smith    Notes:
3915c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
3928f6c3df8SBarry Smith    PETSCVIEWERSOCKET)
3935c6c1daeSBarry Smith 
394db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerGetType()`, `PetscViewerType`, `PetscViewerPushFormat()`
3955c6c1daeSBarry Smith @*/
3969371c9d4SSatish Balay PetscErrorCode PetscViewerSetType(PetscViewer viewer, PetscViewerType type) {
3975c6c1daeSBarry Smith   PetscBool match;
3985f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PetscViewer);
3995c6c1daeSBarry Smith 
4005c6c1daeSBarry Smith   PetscFunctionBegin;
4015c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4025c6c1daeSBarry Smith   PetscValidCharPointer(type, 2);
4039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, type, &match));
4045c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
4055c6c1daeSBarry Smith 
4065c6c1daeSBarry Smith   /* cleanup any old type that may be there */
407dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, destroy);
4080298fd71SBarry Smith   viewer->ops->destroy = NULL;
40902c9f0b5SLisandro Dalcin   viewer->data         = NULL;
410dbbe0bcdSBarry Smith 
4119566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(viewer->ops, sizeof(struct _PetscViewerOps)));
4125c6c1daeSBarry Smith 
4139566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscViewerList, type, &r));
41428b400f6SJacob Faibussowitsch   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscViewer type given: %s", type);
4155c6c1daeSBarry Smith 
4169566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)viewer, type));
4179566063dSJacob Faibussowitsch   PetscCall((*r)(viewer));
4185c6c1daeSBarry Smith   PetscFunctionReturn(0);
4195c6c1daeSBarry Smith }
4205c6c1daeSBarry Smith 
4211c84c290SBarry Smith /*@C
4221c84c290SBarry Smith    PetscViewerRegister - Adds a viewer
4231c84c290SBarry Smith 
4241c84c290SBarry Smith    Not Collective
4251c84c290SBarry Smith 
4261c84c290SBarry Smith    Input Parameters:
4271c84c290SBarry Smith +  name_solver - name of a new user-defined viewer
4281c84c290SBarry Smith -  routine_create - routine to create method context
4291c84c290SBarry Smith 
4301c84c290SBarry Smith    Level: developer
4311c84c290SBarry Smith    Notes:
4321c84c290SBarry Smith    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
4331c84c290SBarry Smith 
4341c84c290SBarry Smith    Sample usage:
4351c84c290SBarry Smith .vb
436bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type",MyViewerCreate);
4371c84c290SBarry Smith .ve
4381c84c290SBarry Smith 
4391c84c290SBarry Smith    Then, your solver can be chosen with the procedural interface via
4401c84c290SBarry Smith $     PetscViewerSetType(viewer,"my_viewer_type")
4411c84c290SBarry Smith    or at runtime via the option
4421c84c290SBarry Smith $     -viewer_type my_viewer_type
4431c84c290SBarry Smith 
444db781477SPatrick Sanan .seealso: `PetscViewerRegisterAll()`
4451c84c290SBarry Smith  @*/
4469371c9d4SSatish Balay PetscErrorCode PetscViewerRegister(const char *sname, PetscErrorCode (*function)(PetscViewer)) {
4475c6c1daeSBarry Smith   PetscFunctionBegin;
4489566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
4499566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscViewerList, sname, function));
4505c6c1daeSBarry Smith   PetscFunctionReturn(0);
4515c6c1daeSBarry Smith }
4525c6c1daeSBarry Smith 
4535c6c1daeSBarry Smith /*@C
4545c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
4555c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
4565c6c1daeSBarry Smith 
4575c6c1daeSBarry Smith    Collective on PetscViewer
4585c6c1daeSBarry Smith 
4595c6c1daeSBarry Smith    Input Parameter:
4605c6c1daeSBarry Smith .     PetscViewer - the graphics context
4615c6c1daeSBarry Smith 
4625c6c1daeSBarry Smith    Level: intermediate
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith    Notes:
4655c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
4665c6c1daeSBarry Smith 
467db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
4685c6c1daeSBarry Smith 
4695c6c1daeSBarry Smith @*/
4709371c9d4SSatish Balay PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) {
4715c6c1daeSBarry Smith   char      vtype[256];
4725c6c1daeSBarry Smith   PetscBool flg;
4735c6c1daeSBarry Smith 
4745c6c1daeSBarry Smith   PetscFunctionBegin;
4755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4765c6c1daeSBarry Smith 
477*48a46eb9SPierre Jolivet   if (!PetscViewerList) PetscCall(PetscViewerRegisterAll());
478d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)viewer);
4799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-viewer_type", "Type of PetscViewer", "None", PetscViewerList, (char *)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII), vtype, 256, &flg));
4801baa6e33SBarry Smith   if (flg) PetscCall(PetscViewerSetType(viewer, vtype));
4815c6c1daeSBarry Smith   /* type has not been set? */
482*48a46eb9SPierre Jolivet   if (!((PetscObject)viewer)->type_name) PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
483dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setfromoptions, PetscOptionsObject);
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
486dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)viewer, PetscOptionsObject));
4879566063dSJacob Faibussowitsch   PetscCall(PetscViewerViewFromOptions(viewer, NULL, "-viewer_view"));
488d0609cedSBarry Smith   PetscOptionsEnd();
4895c6c1daeSBarry Smith   PetscFunctionReturn(0);
4905c6c1daeSBarry Smith }
491816f7b76SBarry Smith 
4929371c9d4SSatish Balay PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer, PetscInt *mcnt, PetscInt *cnt) {
493816f7b76SBarry Smith   PetscFunctionBegin;
4949566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, mcnt));
4959566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, cnt));
496816f7b76SBarry Smith   PetscFunctionReturn(0);
497816f7b76SBarry Smith }
498816f7b76SBarry Smith 
4999371c9d4SSatish Balay PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer, PetscInt i, PetscInt *mcnt, PetscInt cnt) {
500816f7b76SBarry Smith   MPI_Comm comm;
501816f7b76SBarry Smith 
502816f7b76SBarry Smith   PetscFunctionBegin;
5039566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
504816f7b76SBarry Smith   if (i >= *mcnt) {
505816f7b76SBarry Smith     *mcnt += cnt;
5069566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
507816f7b76SBarry Smith   }
508816f7b76SBarry Smith   PetscFunctionReturn(0);
509816f7b76SBarry Smith }
510816f7b76SBarry Smith 
5119371c9d4SSatish Balay PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer, PetscInt *mcnt) {
512816f7b76SBarry Smith   MPI_Comm comm;
513816f7b76SBarry Smith   PetscFunctionBegin;
5149566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
515816f7b76SBarry Smith   *mcnt = 0;
5169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
517816f7b76SBarry Smith   PetscFunctionReturn(0);
518816f7b76SBarry Smith }
519816f7b76SBarry Smith 
5209371c9d4SSatish Balay PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer, PetscMPIInt rank, PetscInt *mcnt) {
521816f7b76SBarry Smith   MPI_Comm comm;
522816f7b76SBarry Smith   PetscFunctionBegin;
5239566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
524816f7b76SBarry Smith   while (PETSC_TRUE) {
525816f7b76SBarry Smith     if (rank < *mcnt) break;
5269566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
527816f7b76SBarry Smith   }
528816f7b76SBarry Smith   PetscFunctionReturn(0);
529816f7b76SBarry Smith }
530816f7b76SBarry Smith 
5319371c9d4SSatish Balay PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer, PetscInt *mcnt) {
532816f7b76SBarry Smith   MPI_Comm comm;
533816f7b76SBarry Smith   PetscFunctionBegin;
5349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
535816f7b76SBarry Smith   while (PETSC_TRUE) {
5369566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
537816f7b76SBarry Smith     if (!*mcnt) break;
538816f7b76SBarry Smith   }
539816f7b76SBarry Smith   PetscFunctionReturn(0);
540816f7b76SBarry Smith }
541