xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision ae41402952cc28c063c4130a4e6f45eaebc47552)
1af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
2e8f14785SLisandro Dalcin #include <petsc/private/hashtable.h>
3e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
4e04113cfSBarry Smith   #include <petscviewersaws.h>
5bfb97211SBarry Smith #endif
65c6c1daeSBarry Smith 
702c9f0b5SLisandro Dalcin PetscFunctionList PetscViewerList = NULL;
85c6c1daeSBarry Smith 
99de0f6ecSBarry Smith PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton = NULL;
109de0f6ecSBarry Smith KHASH_SET_INIT_STR(HTPrinted)
119de0f6ecSBarry Smith struct _n_PetscOptionsHelpPrinted {
129de0f6ecSBarry Smith   khash_t(HTPrinted) *printed;
139de0f6ecSBarry Smith   PetscSegBuffer      strings;
149de0f6ecSBarry Smith };
159de0f6ecSBarry Smith 
PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted * hp)16d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp)
17d71ae5a4SJacob Faibussowitsch {
189de0f6ecSBarry Smith   PetscFunctionBegin;
193ba16761SJacob Faibussowitsch   if (!*hp) PetscFunctionReturn(PETSC_SUCCESS);
209de0f6ecSBarry Smith   kh_destroy(HTPrinted, (*hp)->printed);
219566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferDestroy(&(*hp)->strings));
229566063dSJacob Faibussowitsch   PetscCall(PetscFree(*hp));
233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
3010450e9eSJacob Faibussowitsch   Output Parameter:
3110450e9eSJacob Faibussowitsch . hp - the created object
3210450e9eSJacob Faibussowitsch 
3320f4b53cSBarry Smith   Not Collective
349de0f6ecSBarry Smith 
359de0f6ecSBarry Smith   Level: developer
369de0f6ecSBarry Smith 
37db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCheck()`, `PetscOptionsHelpPrintChecked()`
389de0f6ecSBarry Smith @*/
PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted * hp)39d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
40d71ae5a4SJacob Faibussowitsch {
419de0f6ecSBarry Smith   PetscFunctionBegin;
429566063dSJacob Faibussowitsch   PetscCall(PetscNew(hp));
439de0f6ecSBarry Smith   (*hp)->printed = kh_init(HTPrinted);
449566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferCreate(sizeof(char), 10000, &(*hp)->strings));
453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
469de0f6ecSBarry Smith }
479de0f6ecSBarry Smith 
489de0f6ecSBarry Smith /*@C
499de0f6ecSBarry Smith   PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
509de0f6ecSBarry Smith 
5120f4b53cSBarry Smith   Not Collective
529de0f6ecSBarry Smith 
539de0f6ecSBarry Smith   Input Parameters:
549de0f6ecSBarry Smith + hp   - the object used to manage tracking what help messages have been printed
553f423023SBarry Smith . pre  - the prefix part of the string, many be `NULL`
563f423023SBarry Smith - name - the string to look for (cannot be `NULL`)
579de0f6ecSBarry Smith 
589de0f6ecSBarry Smith   Output Parameter:
593f423023SBarry Smith . found - `PETSC_TRUE` if the string was already set
609de0f6ecSBarry Smith 
619de0f6ecSBarry Smith   Level: intermediate
629de0f6ecSBarry Smith 
63db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCreate()`
649de0f6ecSBarry Smith @*/
PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp,const char * pre,const char * name,PetscBool * found)65d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp, const char *pre, const char *name, PetscBool *found)
66d71ae5a4SJacob Faibussowitsch {
679de0f6ecSBarry Smith   size_t l1, l2;
68c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
699de0f6ecSBarry Smith   char *both;
70e8f14785SLisandro Dalcin   int   newitem;
71c1449d8eSBarry Smith #endif
729de0f6ecSBarry Smith 
739de0f6ecSBarry Smith   PetscFunctionBegin;
749566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(pre, &l1));
759566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &l2));
769de0f6ecSBarry Smith   if (l1 + l2 == 0) {
779de0f6ecSBarry Smith     *found = PETSC_FALSE;
783ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
799de0f6ecSBarry Smith   }
80c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
81c6a7a370SJeremy L Thompson   size_t lboth = l1 + l2 + 1;
82c6a7a370SJeremy L Thompson   PetscCall(PetscSegBufferGet(hp->strings, lboth, &both));
83c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(both, pre, lboth));
84c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(both + l1, name, l2 + 1));
859de0f6ecSBarry Smith   kh_put(HTPrinted, hp->printed, both, &newitem);
86c6a7a370SJeremy L Thompson   if (!newitem) PetscCall(PetscSegBufferUnuse(hp->strings, lboth));
879de0f6ecSBarry Smith   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
88c1449d8eSBarry Smith #else
89c1449d8eSBarry Smith   *found = PETSC_FALSE;
90c1449d8eSBarry Smith #endif
913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9294d6a431SBarry Smith }
9394d6a431SBarry Smith 
94eb55bdffSLawrence Mitchell static PetscBool noviewer = PETSC_FALSE;
95648c30bcSBarry Smith static PetscBool noviewers[PETSCVIEWERCREATEVIEWEROFFPUSHESMAX];
96eb55bdffSLawrence Mitchell static PetscInt  inoviewers = 0;
97eb55bdffSLawrence Mitchell 
98eb55bdffSLawrence Mitchell /*@
99*a0449d98SPierre Jolivet   PetscOptionsPushCreateViewerOff - sets if `PetscOptionsCreateViewer()`, `PetscOptionsViewer()`, and `PetscOptionsCreateViewers()` return viewers.
100eb55bdffSLawrence Mitchell 
101eb55bdffSLawrence Mitchell   Logically Collective
102eb55bdffSLawrence Mitchell 
103eb55bdffSLawrence Mitchell   Input Parameter:
104811af0c4SBarry Smith . flg - `PETSC_TRUE` to turn off viewer creation, `PETSC_FALSE` to turn it on.
105eb55bdffSLawrence Mitchell 
106eb55bdffSLawrence Mitchell   Level: developer
107eb55bdffSLawrence Mitchell 
108811af0c4SBarry Smith   Note:
109c410d8ccSBarry Smith   Calling `XXXViewFromOptions` in an inner loop can be expensive.  This can appear, for example, when using
110648c30bcSBarry Smith   many small subsolves.  Call this function to control viewer creation in `PetscOptionsCreateViewer()`, thus removing the expensive `XXXViewFromOptions` calls.
111c410d8ccSBarry Smith 
11210450e9eSJacob Faibussowitsch   Developer Notes:
113648c30bcSBarry Smith   Instead of using this approach, the calls to `PetscOptionsCreateViewer()` can be moved into `XXXSetFromOptions()`
114eb55bdffSLawrence Mitchell 
115648c30bcSBarry Smith .seealso: [](sec_viewers), `PetscOptionsCreateViewer()`, `PetscOptionsPopCreateViewerOff()`
116eb55bdffSLawrence Mitchell @*/
PetscOptionsPushCreateViewerOff(PetscBool flg)117648c30bcSBarry Smith PetscErrorCode PetscOptionsPushCreateViewerOff(PetscBool flg)
118d71ae5a4SJacob Faibussowitsch {
119eb55bdffSLawrence Mitchell   PetscFunctionBegin;
120648c30bcSBarry Smith   PetscCheck(inoviewers < PETSCVIEWERCREATEVIEWEROFFPUSHESMAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPushCreateViewerOff(), perhaps you forgot PetscOptionsPopCreateViewerOff()?");
121eb55bdffSLawrence Mitchell 
122eb55bdffSLawrence Mitchell   noviewers[inoviewers++] = noviewer;
123eb55bdffSLawrence Mitchell   noviewer                = flg;
1243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
125eb55bdffSLawrence Mitchell }
126eb55bdffSLawrence Mitchell 
127eb55bdffSLawrence Mitchell /*@
128648c30bcSBarry Smith   PetscOptionsPopCreateViewerOff - reset whether `PetscOptionsCreateViewer()` returns a viewer.
129eb55bdffSLawrence Mitchell 
130eb55bdffSLawrence Mitchell   Logically Collective
131eb55bdffSLawrence Mitchell 
132eb55bdffSLawrence Mitchell   Level: developer
133eb55bdffSLawrence Mitchell 
134811af0c4SBarry Smith   Note:
135648c30bcSBarry Smith   See `PetscOptionsPushCreateViewerOff()`
136eb55bdffSLawrence Mitchell 
137648c30bcSBarry Smith .seealso: [](sec_viewers), `PetscOptionsCreateViewer()`, `PetscOptionsPushCreateViewerOff()`
138eb55bdffSLawrence Mitchell @*/
PetscOptionsPopCreateViewerOff(void)139648c30bcSBarry Smith PetscErrorCode PetscOptionsPopCreateViewerOff(void)
140d71ae5a4SJacob Faibussowitsch {
141eb55bdffSLawrence Mitchell   PetscFunctionBegin;
142648c30bcSBarry Smith   PetscCheck(inoviewers, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPopCreateViewerOff(), perhaps you forgot PetscOptionsPushCreateViewerOff()?");
143eb55bdffSLawrence Mitchell   noviewer = noviewers[--inoviewers];
1443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
145eb55bdffSLawrence Mitchell }
146eb55bdffSLawrence Mitchell 
147eb55bdffSLawrence Mitchell /*@
148*a0449d98SPierre Jolivet   PetscOptionsGetCreateViewerOff - do `PetscOptionsCreateViewer()`, `PetscOptionsViewer()`, and `PetscOptionsCreateViewers()` return viewers
149eb55bdffSLawrence Mitchell 
150eb55bdffSLawrence Mitchell   Logically Collective
151eb55bdffSLawrence Mitchell 
152eb55bdffSLawrence Mitchell   Output Parameter:
153eb55bdffSLawrence Mitchell . flg - whether viewers are returned.
154eb55bdffSLawrence Mitchell 
155eb55bdffSLawrence Mitchell   Level: developer
156eb55bdffSLawrence Mitchell 
157648c30bcSBarry Smith .seealso: [](sec_viewers), `PetscOptionsCreateViewer()`, `PetscOptionsPushCreateViewerOff()`, `PetscOptionsPopCreateViewerOff()`
158eb55bdffSLawrence Mitchell @*/
PetscOptionsGetCreateViewerOff(PetscBool * flg)159648c30bcSBarry Smith PetscErrorCode PetscOptionsGetCreateViewerOff(PetscBool *flg)
160d71ae5a4SJacob Faibussowitsch {
161eb55bdffSLawrence Mitchell   PetscFunctionBegin;
1624f572ea9SToby Isaac   PetscAssertPointer(flg, 1);
163eb55bdffSLawrence Mitchell   *flg = noviewer;
1643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
165eb55bdffSLawrence Mitchell }
166eb55bdffSLawrence Mitchell 
PetscOptionsCreateViewers_Single(MPI_Comm comm,const char value[],PetscViewer * viewer,PetscViewerFormat * format)167648c30bcSBarry Smith static PetscErrorCode PetscOptionsCreateViewers_Single(MPI_Comm comm, const char value[], PetscViewer *viewer, PetscViewerFormat *format)
168d71ae5a4SJacob Faibussowitsch {
16909222b14SToby Isaac   char    *loc0_vtype = NULL, *loc1_fname = NULL, *loc2_fmt = NULL, *loc3_fmode = NULL;
17009222b14SToby Isaac   PetscInt cnt;
17109222b14SToby Isaac   size_t   viewer_string_length;
17222d6dc08SStefano Zampini   const char *viewers[] = {PETSCVIEWERASCII, PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSOCKET, PETSCVIEWERMATLAB, PETSCVIEWERSAWS, PETSCVIEWERVTK, PETSCVIEWERHDF5, PETSCVIEWERGLVIS, PETSCVIEWEREXODUSII, PETSCVIEWERPYTHON, PETSCVIEWERPYVISTA, NULL}; /* list should be automatically generated from PetscViewersList */
1732bf49c77SBarry Smith 
1742bf49c77SBarry Smith   PetscFunctionBegin;
17509222b14SToby Isaac   PetscCall(PetscStrlen(value, &viewer_string_length));
17609222b14SToby Isaac   if (!viewer_string_length) {
1776348e711SLisandro Dalcin     if (format) *format = PETSC_VIEWER_DEFAULT;
178648c30bcSBarry Smith     if (viewer) {
179648c30bcSBarry Smith       PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
180648c30bcSBarry Smith       PetscCall(PetscObjectReference((PetscObject)*viewer));
181648c30bcSBarry Smith     }
18209222b14SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
18309222b14SToby Isaac   }
1842bf49c77SBarry Smith 
1859566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(value, &loc0_vtype));
1869566063dSJacob Faibussowitsch   PetscCall(PetscStrchr(loc0_vtype, ':', &loc1_fname));
18735d27ee3SJed Brown   if (loc1_fname) {
188e565330aSJed Brown     PetscBool is_daos;
18935d27ee3SJed Brown     *loc1_fname++ = 0;
190e565330aSJed Brown     // When using DAOS, the filename will have the form "daos:/path/to/file.h5", so capture the rest of it.
191e565330aSJed Brown     PetscCall(PetscStrncmp(loc1_fname, "daos:", 5, &is_daos));
192e565330aSJed Brown     PetscCall(PetscStrchr(loc1_fname + (is_daos == PETSC_TRUE ? 5 : 0), ':', &loc2_fmt));
19335d27ee3SJed Brown   }
19435d27ee3SJed Brown   if (loc2_fmt) {
19535d27ee3SJed Brown     *loc2_fmt++ = 0;
1969566063dSJacob Faibussowitsch     PetscCall(PetscStrchr(loc2_fmt, ':', &loc3_fmode));
19735d27ee3SJed Brown   }
19835d27ee3SJed Brown   if (loc3_fmode) *loc3_fmode++ = 0;
1999566063dSJacob Faibussowitsch   PetscCall(PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii", viewers, &cnt));
20008401ef6SPierre Jolivet   PetscCheck(cnt <= (PetscInt)sizeof(viewers) - 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown viewer type: %s", loc0_vtype);
201bb1d7374SBarry Smith   if (viewer) {
20235d27ee3SJed Brown     if (!loc1_fname) {
20343b63833SBarry Smith       switch (cnt) {
204d71ae5a4SJacob Faibussowitsch       case 0:
205d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
206648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
207d71ae5a4SJacob Faibussowitsch         break;
20843b63833SBarry Smith       case 1:
2099566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) PetscCall(PETSC_ERR_PLIB);
210648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
21143b63833SBarry Smith         break;
21243b63833SBarry Smith       case 2:
2139566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) PetscCall(PETSC_ERR_PLIB);
214648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
21543b63833SBarry Smith         break;
216b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
21743b63833SBarry Smith       case 3:
2189566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) PetscCall(PETSC_ERR_PLIB);
219648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
22043b63833SBarry Smith         break;
221b58ca069SBarry Smith #endif
222d1e78c4fSBarry Smith #if defined(PETSC_HAVE_MATLAB)
22343b63833SBarry Smith       case 4:
2249566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) PetscCall(PETSC_ERR_PLIB);
225648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
22643b63833SBarry Smith         break;
22743b63833SBarry Smith #endif
228e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
229bfb97211SBarry Smith       case 5:
2309566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) PetscCall(PETSC_ERR_PLIB);
231648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
232bfb97211SBarry Smith         break;
233bfb97211SBarry Smith #endif
234a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
235a75e6a4aSMatthew G. Knepley       case 7:
2369566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) PetscCall(PETSC_ERR_PLIB);
237648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
238a75e6a4aSMatthew G. Knepley         break;
239a75e6a4aSMatthew G. Knepley #endif
2408135c375SStefano Zampini       case 8:
2419566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) PetscCall(PETSC_ERR_PLIB);
242648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
2438135c375SStefano Zampini         break;
2441e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
2451e50132fSMatthew G. Knepley       case 9:
2469566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) PetscCall(PETSC_ERR_PLIB);
247648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
2481e50132fSMatthew G. Knepley         break;
2491e50132fSMatthew G. Knepley #endif
25022d6dc08SStefano Zampini       case 10:
25122d6dc08SStefano Zampini         if (!(*viewer = PETSC_VIEWER_PYTHON_(comm))) PetscCall(PETSC_ERR_PLIB);
25222d6dc08SStefano Zampini         PetscCall(PetscObjectReference((PetscObject)*viewer));
25322d6dc08SStefano Zampini         break;
25422d6dc08SStefano Zampini       case 11:
25522d6dc08SStefano Zampini         if (!(*viewer = PETSC_VIEWER_PYVISTA_(comm))) PetscCall(PETSC_ERR_PLIB);
25622d6dc08SStefano Zampini         PetscCall(PetscObjectReference((PetscObject)*viewer));
25722d6dc08SStefano Zampini         break;
258d71ae5a4SJacob Faibussowitsch       default:
25909222b14SToby Isaac         SETERRQ(comm, PETSC_ERR_SUP, "Unsupported viewer %s", loc0_vtype);
2607f677774SBarry Smith       }
2617f677774SBarry Smith     } else {
26235d27ee3SJed Brown       if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
2639566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
264648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
2657f677774SBarry Smith       } else {
2663550efbcSJed Brown         PetscFileMode fmode;
26709222b14SToby Isaac         PetscBool     flag = PETSC_FALSE;
26809222b14SToby Isaac 
2699566063dSJacob Faibussowitsch         PetscCall(PetscViewerCreate(comm, viewer));
2709566063dSJacob Faibussowitsch         PetscCall(PetscViewerSetType(*viewer, *loc0_vtype ? loc0_vtype : "ascii"));
2713550efbcSJed Brown         fmode = FILE_MODE_WRITE;
2723550efbcSJed Brown         if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
2739566063dSJacob Faibussowitsch           PetscCall(PetscEnumFind(PetscFileModes, loc3_fmode, (PetscEnum *)&fmode, &flag));
27428b400f6SJacob Faibussowitsch           PetscCheck(flag, comm, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown file mode: %s", loc3_fmode);
2757f677774SBarry Smith         }
276acd7d2deSBarry Smith         if (loc2_fmt) {
2770ecfd9fcSLisandro Dalcin           PetscBool tk, im;
2789566063dSJacob Faibussowitsch           PetscCall(PetscStrcmp(loc1_fname, "tikz", &tk));
2799566063dSJacob Faibussowitsch           PetscCall(PetscStrcmp(loc1_fname, "image", &im));
2800ecfd9fcSLisandro Dalcin           if (tk || im) {
2819566063dSJacob Faibussowitsch             PetscCall(PetscViewerDrawSetInfo(*viewer, NULL, loc2_fmt, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE));
282acd7d2deSBarry Smith             *loc2_fmt = 0;
283acd7d2deSBarry Smith           }
284acd7d2deSBarry Smith         }
2859566063dSJacob Faibussowitsch         PetscCall(PetscViewerFileSetMode(*viewer, flag ? fmode : FILE_MODE_WRITE));
2869566063dSJacob Faibussowitsch         PetscCall(PetscViewerFileSetName(*viewer, loc1_fname));
2871baa6e33SBarry Smith         if (*loc1_fname) PetscCall(PetscViewerDrawSetDrawType(*viewer, loc1_fname));
2889566063dSJacob Faibussowitsch         PetscCall(PetscViewerSetFromOptions(*viewer));
28905315717SToby Isaac       }
290bb1d7374SBarry Smith     }
29152f76066SLisandro Dalcin   }
2921baa6e33SBarry Smith   if (viewer) PetscCall(PetscViewerSetUp(*viewer));
29335d27ee3SJed Brown   if (loc2_fmt && *loc2_fmt) {
294e156c29bSStefano Zampini     PetscViewerFormat tfmt;
29509222b14SToby Isaac     PetscBool         flag;
296d6acdc46SStefano Zampini 
2979566063dSJacob Faibussowitsch     PetscCall(PetscEnumFind(PetscViewerFormats, loc2_fmt, (PetscEnum *)&tfmt, &flag));
298d6acdc46SStefano Zampini     if (format) *format = tfmt;
2996adde796SStefano Zampini     PetscCheck(flag, comm, PETSC_ERR_SUP, "Unknown viewer format %s", loc2_fmt);
300d6acdc46SStefano Zampini   } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
3019566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(*viewer, format));
3027f677774SBarry Smith   }
3039566063dSJacob Faibussowitsch   PetscCall(PetscFree(loc0_vtype));
30409222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
30509222b14SToby Isaac }
30609222b14SToby Isaac 
PetscOptionsCreateViewers_Internal(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscInt * n_max_p,PetscViewer viewer[],PetscViewerFormat format[],PetscBool * set,const char func_name[],PetscBool allow_multiple)307648c30bcSBarry Smith static PetscErrorCode PetscOptionsCreateViewers_Internal(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscInt *n_max_p, PetscViewer viewer[], PetscViewerFormat format[], PetscBool *set, const char func_name[], PetscBool allow_multiple)
30809222b14SToby Isaac {
30909222b14SToby Isaac   const char *value;
31009222b14SToby Isaac   PetscBool   flag, hashelp;
31109222b14SToby Isaac   PetscInt    n_max;
31209222b14SToby Isaac 
31309222b14SToby Isaac   PetscFunctionBegin;
31409222b14SToby Isaac   PetscAssertPointer(name, 4);
31509222b14SToby Isaac   PetscAssertPointer(n_max_p, 5);
31609222b14SToby Isaac   n_max = *n_max_p;
31709222b14SToby Isaac   PetscCheck(n_max >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid size %" PetscInt_FMT " of passed arrays", *n_max_p);
31809222b14SToby Isaac   *n_max_p = 0;
31909222b14SToby Isaac 
32009222b14SToby Isaac   if (set) *set = PETSC_FALSE;
321648c30bcSBarry Smith   PetscCall(PetscOptionsGetCreateViewerOff(&flag));
32209222b14SToby Isaac   if (flag) PetscFunctionReturn(PETSC_SUCCESS);
32309222b14SToby Isaac 
32409222b14SToby Isaac   PetscCall(PetscOptionsHasHelp(NULL, &hashelp));
32509222b14SToby Isaac   if (hashelp) {
32609222b14SToby Isaac     PetscBool found;
32709222b14SToby Isaac 
32809222b14SToby Isaac     if (!PetscOptionsHelpPrintedSingleton) PetscCall(PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton));
32909222b14SToby Isaac     PetscCall(PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton, pre, name, &found));
33009222b14SToby Isaac     if (!found && viewer) {
33109222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\nViewer (-%s%s) options:\n", pre ? pre : "", name + 1));
33209222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n", pre ? pre : "", name + 1, "Prints object to stdout or ASCII file", func_name));
33309222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n", pre ? pre : "", name + 1, "Saves object to a binary file", func_name));
33409222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n", pre ? pre : "", name + 1, "Draws object", func_name));
33509222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s socket[:port]: %s (%s)\n", pre ? pre : "", name + 1, "Pushes object to a Unix socket", func_name));
33609222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s saws[:communicatorname]: %s (%s)\n", pre ? pre : "", name + 1, "Publishes object to SAWs", func_name));
33709222b14SToby Isaac       if (allow_multiple) PetscCall((*PetscHelpPrintf)(comm, "  -%s%s v1[,v2,...]: %s (%s)\n", pre ? pre : "", name + 1, "Multiple viewers", func_name));
3382bf49c77SBarry Smith     }
3392bf49c77SBarry Smith   }
34009222b14SToby Isaac 
34109222b14SToby Isaac   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
34209222b14SToby Isaac   if (flag) {
34309222b14SToby Isaac     if (set) *set = PETSC_TRUE;
34409222b14SToby Isaac     if (!value) {
34509222b14SToby Isaac       PetscCheck(n_max > 0, comm, PETSC_ERR_ARG_SIZ, "More viewers (1) than max available (0)");
34609222b14SToby Isaac       if (format) *format = PETSC_VIEWER_DEFAULT;
347648c30bcSBarry Smith       if (viewer) {
348648c30bcSBarry Smith         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
349648c30bcSBarry Smith         PetscCall(PetscObjectReference((PetscObject)*viewer));
350648c30bcSBarry Smith       }
35109222b14SToby Isaac       *n_max_p = 1;
35209222b14SToby Isaac     } else {
35309222b14SToby Isaac       char  *loc0_viewer_string = NULL, *this_viewer_string = NULL;
35409222b14SToby Isaac       size_t viewer_string_length;
35509222b14SToby Isaac 
35609222b14SToby Isaac       PetscCall(PetscStrallocpy(value, &loc0_viewer_string));
35709222b14SToby Isaac       PetscCall(PetscStrlen(loc0_viewer_string, &viewer_string_length));
35809222b14SToby Isaac       this_viewer_string = loc0_viewer_string;
35909222b14SToby Isaac 
36009222b14SToby Isaac       do {
36109222b14SToby Isaac         PetscViewer       *this_viewer;
36209222b14SToby Isaac         PetscViewerFormat *this_viewer_format;
36309222b14SToby Isaac         char              *next_viewer_string = NULL;
36409222b14SToby Isaac         char              *comma_separator    = NULL;
36509222b14SToby Isaac         PetscInt           n                  = *n_max_p;
36609222b14SToby Isaac 
367835f2295SStefano Zampini         PetscCheck(n < n_max, comm, PETSC_ERR_PLIB, "More viewers than max available (%" PetscInt_FMT ")", n_max);
36809222b14SToby Isaac 
36909222b14SToby Isaac         PetscCall(PetscStrchr(this_viewer_string, ',', &comma_separator));
37009222b14SToby Isaac         if (comma_separator) {
371648c30bcSBarry Smith           PetscCheck(allow_multiple, comm, PETSC_ERR_ARG_OUTOFRANGE, "Trying to pass multiple viewers to %s: only one allowed.  Use PetscOptionsCreateViewers() instead", func_name);
37209222b14SToby Isaac           *comma_separator   = 0;
37309222b14SToby Isaac           next_viewer_string = comma_separator + 1;
37409222b14SToby Isaac         }
3758e3a54c0SPierre Jolivet         this_viewer = PetscSafePointerPlusOffset(viewer, n);
37609222b14SToby Isaac         if (this_viewer) *this_viewer = NULL;
3778e3a54c0SPierre Jolivet         this_viewer_format = PetscSafePointerPlusOffset(format, n);
37809222b14SToby Isaac         if (this_viewer_format) *this_viewer_format = PETSC_VIEWER_DEFAULT;
379648c30bcSBarry Smith         PetscCall(PetscOptionsCreateViewers_Single(comm, this_viewer_string, this_viewer, this_viewer_format));
38009222b14SToby Isaac         this_viewer_string = next_viewer_string;
38109222b14SToby Isaac         (*n_max_p)++;
38209222b14SToby Isaac       } while (this_viewer_string);
38309222b14SToby Isaac       PetscCall(PetscFree(loc0_viewer_string));
38409222b14SToby Isaac     }
38509222b14SToby Isaac   }
38609222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
38709222b14SToby Isaac }
38809222b14SToby Isaac 
38909222b14SToby Isaac /*@C
390648c30bcSBarry Smith   PetscOptionsCreateViewer - Creates a viewer appropriate for the type indicated by the user
39109222b14SToby Isaac 
39209222b14SToby Isaac   Collective
39309222b14SToby Isaac 
39409222b14SToby Isaac   Input Parameters:
39509222b14SToby Isaac + comm    - the communicator to own the viewer
39609222b14SToby Isaac . options - options database, use `NULL` for default global database
39709222b14SToby Isaac . pre     - the string to prepend to the name or `NULL`
398cd791dc2SBarry Smith - name    - the options database name that will be checked for
39909222b14SToby Isaac 
40009222b14SToby Isaac   Output Parameters:
40109222b14SToby Isaac + viewer - the viewer, pass `NULL` if not needed
40209222b14SToby Isaac . format - the `PetscViewerFormat` requested by the user, pass `NULL` if not needed
40309222b14SToby Isaac - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`
40409222b14SToby Isaac 
40509222b14SToby Isaac   Level: intermediate
40609222b14SToby Isaac 
40709222b14SToby Isaac   Notes:
40889efdf64SMatthew G. Knepley   The argument has the following form
40989efdf64SMatthew G. Knepley .vb
41089efdf64SMatthew G. Knepley     type:filename:format:filemode
41189efdf64SMatthew G. Knepley .ve
41289efdf64SMatthew G. Knepley   where all parts are optional, but you need to include the colon to access the next part. The mode argument must a valid `PetscFileMode`, i.e. read, write, append, update, or append_update. For example, to read from an HDF5 file, use
41389efdf64SMatthew G. Knepley .vb
41489efdf64SMatthew G. Knepley     hdf5:sol.h5::read
41589efdf64SMatthew G. Knepley .ve
41689efdf64SMatthew G. Knepley 
41709222b14SToby Isaac   If no value is provided ascii:stdout is used
41809222b14SToby Isaac +       ascii[:[filename][:[format][:append]]]  -  defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
41909222b14SToby Isaac   for example ascii::ascii_info prints just the information about the object not all details
42009222b14SToby Isaac   unless :append is given filename opens in write mode, overwriting what was already there
42109222b14SToby Isaac .       binary[:[filename][:[format][:append]]] -  defaults to the file binaryoutput
42209222b14SToby Isaac .       draw[:drawtype[:filename]]              -  for example, draw:tikz, draw:tikz:figure.tex  or draw:x
42309222b14SToby Isaac .       socket[:port]                           -  defaults to the standard output port
42409222b14SToby Isaac -       saws[:communicatorname]                 -   publishes object to the Scientific Application Webserver (SAWs)
42509222b14SToby Isaac 
42609222b14SToby Isaac   You can control whether calls to this function create a viewer (or return early with *set of `PETSC_FALSE`) with
427648c30bcSBarry Smith   `PetscOptionsPushCreateViewerOff()`.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
42809222b14SToby Isaac   an appreciable fraction of the runtime.
42909222b14SToby Isaac 
43009222b14SToby Isaac   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with *set of `PETSC_FALSE`
43109222b14SToby Isaac 
432cd791dc2SBarry Smith   This routine is thread-safe for accessing predefined `PetscViewer`s like `PETSC_VIEWER_STDOUT_SELF` but not for accessing
433cd791dc2SBarry Smith   files by name.
434cd791dc2SBarry Smith 
435648c30bcSBarry Smith .seealso: [](sec_viewers), `PetscViewerDestroy()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
43609222b14SToby Isaac           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
43709222b14SToby Isaac           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
43809222b14SToby Isaac           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
43909222b14SToby Isaac           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
44009222b14SToby Isaac           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
441648c30bcSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushCreateViewerOff()`, `PetscOptionsPopCreateViewerOff()`,
442648c30bcSBarry Smith           `PetscOptionsCreateViewerOff()`
44309222b14SToby Isaac @*/
PetscOptionsCreateViewer(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscViewer * viewer,PetscViewerFormat * format,PetscBool * set)444648c30bcSBarry Smith PetscErrorCode PetscOptionsCreateViewer(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscViewer *viewer, PetscViewerFormat *format, PetscBool *set)
44509222b14SToby Isaac {
44609222b14SToby Isaac   PetscInt  n_max = 1;
44709222b14SToby Isaac   PetscBool set_internal;
44809222b14SToby Isaac 
44909222b14SToby Isaac   PetscFunctionBegin;
45009222b14SToby Isaac   if (viewer) *viewer = NULL;
45109222b14SToby Isaac   if (format) *format = PETSC_VIEWER_DEFAULT;
452648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewers_Internal(comm, options, pre, name, &n_max, viewer, format, &set_internal, PETSC_FUNCTION_NAME, PETSC_FALSE));
453835f2295SStefano Zampini   if (set_internal) PetscAssert(n_max == 1, comm, PETSC_ERR_PLIB, "Unexpected: %" PetscInt_FMT " != 1 viewers set", n_max);
45409222b14SToby Isaac   if (set) *set = set_internal;
45509222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
45609222b14SToby Isaac }
45709222b14SToby Isaac 
45809222b14SToby Isaac /*@C
459648c30bcSBarry Smith   PetscOptionsCreateViewers - Create multiple viewers from a comma-separated list in the options database
46009222b14SToby Isaac 
46109222b14SToby Isaac   Collective
46209222b14SToby Isaac 
46309222b14SToby Isaac   Input Parameters:
464cd791dc2SBarry Smith + comm    - the communicator to own the viewers
46509222b14SToby Isaac . options - options database, use `NULL` for default global database
46609222b14SToby Isaac . pre     - the string to prepend to the name or `NULL`
467cd791dc2SBarry Smith . name    - the options database name that will be checked for
46809222b14SToby Isaac - n_max   - on input: the maximum number of viewers; on output: the number of viewers in the comma-separated list
46909222b14SToby Isaac 
47009222b14SToby Isaac   Output Parameters:
47109222b14SToby Isaac + viewers - an array to hold at least `n_max` `PetscViewer`s, or `NULL` if not needed; on output: if not `NULL`, the
47209222b14SToby Isaac             first `n_max` entries are initialized `PetscViewer`s
47309222b14SToby Isaac . formats - an array to hold at least `n_max` `PetscViewerFormat`s, or `NULL` if not needed; on output: if not
47409222b14SToby Isaac             `NULL`, the first `n_max` entries are valid `PetscViewewFormat`s
47509222b14SToby Isaac - set     - `PETSC_TRUE` if found, else `PETSC_FALSE`
47609222b14SToby Isaac 
47709222b14SToby Isaac   Level: intermediate
47809222b14SToby Isaac 
47909222b14SToby Isaac   Note:
480648c30bcSBarry Smith   See `PetscOptionsCreateViewer()` for how the format strings for the viewers are interpreted.
481648c30bcSBarry Smith 
482648c30bcSBarry Smith   Use `PetscViewerDestroy()` on each viewer, otherwise a memory leak will occur.
48309222b14SToby Isaac 
48409222b14SToby Isaac   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with `n_max` of 0 and `set` of `PETSC_FALSE`
48509222b14SToby Isaac 
486648c30bcSBarry Smith .seealso: [](sec_viewers), `PetscOptionsCreateViewer()`
48709222b14SToby Isaac @*/
PetscOptionsCreateViewers(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscInt * n_max,PetscViewer viewers[],PetscViewerFormat formats[],PetscBool * set)488648c30bcSBarry Smith PetscErrorCode PetscOptionsCreateViewers(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscInt *n_max, PetscViewer viewers[], PetscViewerFormat formats[], PetscBool *set)
48909222b14SToby Isaac {
49009222b14SToby Isaac   PetscFunctionBegin;
491648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewers_Internal(comm, options, pre, name, n_max, viewers, formats, set, PETSC_FUNCTION_NAME, PETSC_TRUE));
4923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4932bf49c77SBarry Smith }
4942bf49c77SBarry Smith 
4955c6c1daeSBarry Smith /*@
496c410d8ccSBarry Smith   PetscViewerCreate - Creates a viewing context. A `PetscViewer` represents a file, a graphical window, a Unix socket or a variety of other ways
497c410d8ccSBarry Smith   of viewing a PETSc object
4985c6c1daeSBarry Smith 
499d083f849SBarry Smith   Collective
5005c6c1daeSBarry Smith 
5015c6c1daeSBarry Smith   Input Parameter:
5025c6c1daeSBarry Smith . comm - MPI communicator
5035c6c1daeSBarry Smith 
5045c6c1daeSBarry Smith   Output Parameter:
505811af0c4SBarry Smith . inviewer - location to put the `PetscViewer` context
5065c6c1daeSBarry Smith 
5075c6c1daeSBarry Smith   Level: advanced
5085c6c1daeSBarry Smith 
509d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerType`
5105c6c1daeSBarry Smith @*/
PetscViewerCreate(MPI_Comm comm,PetscViewer * inviewer)511d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCreate(MPI_Comm comm, PetscViewer *inviewer)
512d71ae5a4SJacob Faibussowitsch {
5135c6c1daeSBarry Smith   PetscViewer viewer;
5145c6c1daeSBarry Smith 
5155c6c1daeSBarry Smith   PetscFunctionBegin;
516377f809aSBarry Smith   PetscAssertPointer(inviewer, 2);
5179566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
5189566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(viewer, PETSC_VIEWER_CLASSID, "PetscViewer", "PetscViewer", "Viewer", comm, PetscViewerDestroy, PetscViewerView));
5195c6c1daeSBarry Smith   *inviewer    = viewer;
52002c9f0b5SLisandro Dalcin   viewer->data = NULL;
5213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5225c6c1daeSBarry Smith }
5235c6c1daeSBarry Smith 
524cc4c1da9SBarry Smith /*@
525811af0c4SBarry Smith   PetscViewerSetType - Builds `PetscViewer` for a particular implementation.
5265c6c1daeSBarry Smith 
527c3339decSBarry Smith   Collective
5285c6c1daeSBarry Smith 
529d8d19677SJose E. Roman   Input Parameters:
530811af0c4SBarry Smith + viewer - the `PetscViewer` context obtained with `PetscViewerCreate()`
531811af0c4SBarry Smith - type   - for example, `PETSCVIEWERASCII`
5325c6c1daeSBarry Smith 
5333c7db156SBarry Smith   Options Database Key:
5343c7db156SBarry Smith . -viewer_type  <type> - Sets the type; use -help for a list of available methods (for instance, ascii)
5355c6c1daeSBarry Smith 
5365c6c1daeSBarry Smith   Level: advanced
5375c6c1daeSBarry Smith 
538811af0c4SBarry Smith   Note:
5393f423023SBarry Smith   See `PetscViewerType` for possible values
5405c6c1daeSBarry Smith 
541d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerGetType()`, `PetscViewerType`, `PetscViewerPushFormat()`
5425c6c1daeSBarry Smith @*/
PetscViewerSetType(PetscViewer viewer,PetscViewerType type)543d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetType(PetscViewer viewer, PetscViewerType type)
544d71ae5a4SJacob Faibussowitsch {
5455c6c1daeSBarry Smith   PetscBool match;
5465f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PetscViewer);
5475c6c1daeSBarry Smith 
5485c6c1daeSBarry Smith   PetscFunctionBegin;
5495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5504f572ea9SToby Isaac   PetscAssertPointer(type, 2);
5519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, type, &match));
5523ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
5535c6c1daeSBarry Smith 
5545c6c1daeSBarry Smith   /* cleanup any old type that may be there */
555dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, destroy);
5560298fd71SBarry Smith   viewer->ops->destroy = NULL;
55702c9f0b5SLisandro Dalcin   viewer->data         = NULL;
558dbbe0bcdSBarry Smith 
5599566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(viewer->ops, sizeof(struct _PetscViewerOps)));
5605c6c1daeSBarry Smith 
5619566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscViewerList, type, &r));
5626adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscViewer type given: %s", type);
5635c6c1daeSBarry Smith 
5649566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)viewer, type));
5659566063dSJacob Faibussowitsch   PetscCall((*r)(viewer));
5663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5675c6c1daeSBarry Smith }
5685c6c1daeSBarry Smith 
5691c84c290SBarry Smith /*@C
570c410d8ccSBarry Smith   PetscViewerRegister - Adds a viewer to those available for use with `PetscViewerSetType()`
5711c84c290SBarry Smith 
572cc4c1da9SBarry Smith   Not Collective, No Fortran Support
5731c84c290SBarry Smith 
5741c84c290SBarry Smith   Input Parameters:
5752fe279fdSBarry Smith + sname    - name of a new user-defined viewer
5762fe279fdSBarry Smith - function - routine to create method context
5771c84c290SBarry Smith 
5781c84c290SBarry Smith   Level: developer
579811af0c4SBarry Smith 
580811af0c4SBarry Smith   Note:
581811af0c4SBarry Smith   `PetscViewerRegister()` may be called multiple times to add several user-defined viewers.
5821c84c290SBarry Smith 
583aec76313SJacob Faibussowitsch   Example Usage:
5841c84c290SBarry Smith .vb
585bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type", MyViewerCreate);
5861c84c290SBarry Smith .ve
5871c84c290SBarry Smith 
5881c84c290SBarry Smith   Then, your solver can be chosen with the procedural interface via
589b44f4de4SBarry Smith .vb
590b44f4de4SBarry Smith   PetscViewerSetType(viewer, "my_viewer_type")
591b44f4de4SBarry Smith .ve
5921c84c290SBarry Smith   or at runtime via the option
593b44f4de4SBarry Smith .vb
594b44f4de4SBarry Smith   -viewer_type my_viewer_type
595b44f4de4SBarry Smith .ve
5961c84c290SBarry Smith 
597d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerRegisterAll()`
5981c84c290SBarry Smith  @*/
PetscViewerRegister(const char * sname,PetscErrorCode (* function)(PetscViewer))599d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRegister(const char *sname, PetscErrorCode (*function)(PetscViewer))
600d71ae5a4SJacob Faibussowitsch {
6015c6c1daeSBarry Smith   PetscFunctionBegin;
6029566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
6039566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscViewerList, sname, function));
6043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6055c6c1daeSBarry Smith }
6065c6c1daeSBarry Smith 
6075c6c1daeSBarry Smith /*@C
6083f423023SBarry Smith   PetscViewerSetFromOptions - Sets various options for a viewer based on values in the options database.
6095c6c1daeSBarry Smith 
610c3339decSBarry Smith   Collective
6115c6c1daeSBarry Smith 
6125c6c1daeSBarry Smith   Input Parameter:
613811af0c4SBarry Smith . viewer - the viewer context
6145c6c1daeSBarry Smith 
6155c6c1daeSBarry Smith   Level: intermediate
6165c6c1daeSBarry Smith 
617811af0c4SBarry Smith   Note:
618c410d8ccSBarry Smith   Must be called after `PetscViewerCreate()` but before the `PetscViewer` is used.
6195c6c1daeSBarry Smith 
620d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
6215c6c1daeSBarry Smith @*/
PetscViewerSetFromOptions(PetscViewer viewer)622d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer)
623d71ae5a4SJacob Faibussowitsch {
6245c6c1daeSBarry Smith   char      vtype[256];
6255c6c1daeSBarry Smith   PetscBool flg;
6265c6c1daeSBarry Smith 
6275c6c1daeSBarry Smith   PetscFunctionBegin;
6285c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6295c6c1daeSBarry Smith 
63048a46eb9SPierre Jolivet   if (!PetscViewerList) PetscCall(PetscViewerRegisterAll());
631d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)viewer);
6329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-viewer_type", "Type of PetscViewer", "None", PetscViewerList, (char *)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII), vtype, 256, &flg));
6331baa6e33SBarry Smith   if (flg) PetscCall(PetscViewerSetType(viewer, vtype));
6345c6c1daeSBarry Smith   /* type has not been set? */
63548a46eb9SPierre Jolivet   if (!((PetscObject)viewer)->type_name) PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
636dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setfromoptions, PetscOptionsObject);
6375c6c1daeSBarry Smith 
6385c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
639dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)viewer, PetscOptionsObject));
6409566063dSJacob Faibussowitsch   PetscCall(PetscViewerViewFromOptions(viewer, NULL, "-viewer_view"));
641d0609cedSBarry Smith   PetscOptionsEnd();
6423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6435c6c1daeSBarry Smith }
644816f7b76SBarry Smith 
PetscViewerFlowControlStart(PetscViewer viewer,PetscInt * mcnt,PetscInt * cnt)645d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer, PetscInt *mcnt, PetscInt *cnt)
646d71ae5a4SJacob Faibussowitsch {
647816f7b76SBarry Smith   PetscFunctionBegin;
6489566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, mcnt));
6499566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, cnt));
6503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
651816f7b76SBarry Smith }
652816f7b76SBarry Smith 
PetscViewerFlowControlStepMain(PetscViewer viewer,PetscInt i,PetscInt * mcnt,PetscInt cnt)653d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer, PetscInt i, PetscInt *mcnt, PetscInt cnt)
654d71ae5a4SJacob Faibussowitsch {
655816f7b76SBarry Smith   MPI_Comm comm;
656816f7b76SBarry Smith 
657816f7b76SBarry Smith   PetscFunctionBegin;
6589566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
659816f7b76SBarry Smith   if (i >= *mcnt) {
660816f7b76SBarry Smith     *mcnt += cnt;
6619566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
662816f7b76SBarry Smith   }
6633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
664816f7b76SBarry Smith }
665816f7b76SBarry Smith 
PetscViewerFlowControlEndMain(PetscViewer viewer,PetscInt * mcnt)666d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer, PetscInt *mcnt)
667d71ae5a4SJacob Faibussowitsch {
668816f7b76SBarry Smith   MPI_Comm comm;
6694d86920dSPierre Jolivet 
670816f7b76SBarry Smith   PetscFunctionBegin;
6719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
672816f7b76SBarry Smith   *mcnt = 0;
6739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
6743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
675816f7b76SBarry Smith }
676816f7b76SBarry Smith 
PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt * mcnt)677d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer, PetscMPIInt rank, PetscInt *mcnt)
678d71ae5a4SJacob Faibussowitsch {
679816f7b76SBarry Smith   MPI_Comm comm;
6804d86920dSPierre Jolivet 
681816f7b76SBarry Smith   PetscFunctionBegin;
6829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
683816f7b76SBarry Smith   while (PETSC_TRUE) {
684816f7b76SBarry Smith     if (rank < *mcnt) break;
6859566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
686816f7b76SBarry Smith   }
6873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
688816f7b76SBarry Smith }
689816f7b76SBarry Smith 
PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt * mcnt)690d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer, PetscInt *mcnt)
691d71ae5a4SJacob Faibussowitsch {
692816f7b76SBarry Smith   MPI_Comm comm;
6934d86920dSPierre Jolivet 
694816f7b76SBarry Smith   PetscFunctionBegin;
6959566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
696816f7b76SBarry Smith   while (PETSC_TRUE) {
6979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
698816f7b76SBarry Smith     if (!*mcnt) break;
699816f7b76SBarry Smith   }
7003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
701816f7b76SBarry Smith }
702