xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 34e79e724ed5272e30e829101ec0b1afacc04871)
15c6c1daeSBarry Smith 
2f5860696SSatish Balay #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h> /*I     "petscviewer.h"   I*/
35c6c1daeSBarry Smith 
4e2dcd6d3SBarry Smith /*
5e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
6e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
7e2dcd6d3SBarry Smith */
8d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
9e2dcd6d3SBarry Smith 
109c5ded49SBarry Smith /*@
11811af0c4SBarry Smith   PetscViewerASCIIGetStdout - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
12811af0c4SBarry Smith   in a communicator. Error returning version of `PETSC_VIEWER_STDOUT_()`
135c6c1daeSBarry Smith 
14d083f849SBarry Smith   Collective
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith   Input Parameter:
17811af0c4SBarry Smith . comm - the MPI communicator to share the `PetscViewer`
185c6c1daeSBarry Smith 
1910450e9eSJacob Faibussowitsch   Output Parameter:
2010450e9eSJacob Faibussowitsch . viewer - the viewer
2110450e9eSJacob Faibussowitsch 
225c6c1daeSBarry Smith   Level: beginner
235c6c1daeSBarry Smith 
24811af0c4SBarry Smith   Note:
253f423023SBarry Smith   This should be used in all PETSc source code instead of `PETSC_VIEWER_STDOUT_()` since it allows error checking
265c6c1daeSBarry Smith 
27d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
28db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
295c6c1daeSBarry Smith @*/
30d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetStdout(MPI_Comm comm, PetscViewer *viewer)
31d71ae5a4SJacob Faibussowitsch {
32e2dcd6d3SBarry Smith   PetscBool flg;
33e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith   PetscFunctionBegin;
369566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStdout));
379566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
3848a46eb9SPierre Jolivet   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Stdout_keyval, NULL));
399566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void **)viewer, (PetscMPIInt *)&flg));
40e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
419566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stdout", viewer));
429566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
439566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void *)*viewer));
44e2dcd6d3SBarry Smith   }
459566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
469566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout));
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
485c6c1daeSBarry Smith }
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith /*@C
51c410d8ccSBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
525c6c1daeSBarry Smith                     in a communicator.
535c6c1daeSBarry Smith 
54d083f849SBarry Smith    Collective
555c6c1daeSBarry Smith 
565c6c1daeSBarry Smith    Input Parameter:
57811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith    Level: beginner
605c6c1daeSBarry Smith 
61811af0c4SBarry Smith    Note:
625c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
635c6c1daeSBarry Smith    an error code. Usually used in the form
645c6c1daeSBarry Smith $      XXXView(XXX object, PETSC_VIEWER_STDOUT_(comm));
655c6c1daeSBarry Smith 
66d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
67db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
685c6c1daeSBarry Smith @*/
69d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
70d71ae5a4SJacob Faibussowitsch {
715c6c1daeSBarry Smith   PetscErrorCode ierr;
725c6c1daeSBarry Smith   PetscViewer    viewer;
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith   PetscFunctionBegin;
755c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm, &viewer);
769371c9d4SSatish Balay   if (ierr) {
773ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDOUT_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
789371c9d4SSatish Balay     PetscFunctionReturn(NULL);
799371c9d4SSatish Balay   }
805c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
815c6c1daeSBarry Smith }
825c6c1daeSBarry Smith 
83e2dcd6d3SBarry Smith /*
84e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
85e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
86e2dcd6d3SBarry Smith */
87d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
88e2dcd6d3SBarry Smith 
899c5ded49SBarry Smith /*@
90c410d8ccSBarry Smith   PetscViewerASCIIGetStderr - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
91811af0c4SBarry Smith   in a communicator. Error returning version of `PETSC_VIEWER_STDERR_()`
925c6c1daeSBarry Smith 
93d083f849SBarry Smith   Collective
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   Input Parameter:
96811af0c4SBarry Smith . comm - the MPI communicator to share the `PetscViewer`
975c6c1daeSBarry Smith 
9810450e9eSJacob Faibussowitsch   Output Parameter:
9910450e9eSJacob Faibussowitsch . viewer - the viewer
10010450e9eSJacob Faibussowitsch 
1015c6c1daeSBarry Smith   Level: beginner
1025c6c1daeSBarry Smith 
103811af0c4SBarry Smith   Note:
1043f423023SBarry Smith   This should be used in all PETSc source code instead of `PETSC_VIEWER_STDERR_()` since it allows error checking
1055c6c1daeSBarry Smith 
106d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDERR_WORLD`,
107db781477SPatrick Sanan           `PETSC_VIEWER_STDERR_SELF`
1085c6c1daeSBarry Smith @*/
109d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetStderr(MPI_Comm comm, PetscViewer *viewer)
110d71ae5a4SJacob Faibussowitsch {
111e2dcd6d3SBarry Smith   PetscBool flg;
112e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
113e2dcd6d3SBarry Smith 
114e2dcd6d3SBarry Smith   PetscFunctionBegin;
1159566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStderr));
1169566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
11748a46eb9SPierre Jolivet   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Stderr_keyval, NULL));
1189566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void **)viewer, (PetscMPIInt *)&flg));
119e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
1209566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stderr", viewer));
1219566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
1229566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void *)*viewer));
123e2dcd6d3SBarry Smith   }
1249566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
1259566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr));
1263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1275c6c1daeSBarry Smith }
1285c6c1daeSBarry Smith 
1295c6c1daeSBarry Smith /*@C
130c410d8ccSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
1315c6c1daeSBarry Smith                     in a communicator.
1325c6c1daeSBarry Smith 
133d083f849SBarry Smith    Collective
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith    Input Parameter:
136811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
1375c6c1daeSBarry Smith 
1385c6c1daeSBarry Smith    Level: beginner
1395c6c1daeSBarry Smith 
1403f423023SBarry Smith    Notes:
1415c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1425c6c1daeSBarry Smith    an error code. Usually used in the form
1435c6c1daeSBarry Smith $      XXXView(XXX object, PETSC_VIEWER_STDERR_(comm));
1445c6c1daeSBarry Smith 
1453f423023SBarry Smith    `PetscViewerASCIIGetStderr()` is preferred  since it allows error checking
1463f423023SBarry Smith 
147d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDOUT_WORLD`,
148db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDERR_WORLD`, `PETSC_VIEWER_STDERR_SELF`
1495c6c1daeSBarry Smith @*/
150d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
151d71ae5a4SJacob Faibussowitsch {
1525c6c1daeSBarry Smith   PetscErrorCode ierr;
1535c6c1daeSBarry Smith   PetscViewer    viewer;
1545c6c1daeSBarry Smith 
1555c6c1daeSBarry Smith   PetscFunctionBegin;
1565c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm, &viewer);
1579371c9d4SSatish Balay   if (ierr) {
1583ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDERR_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
1599371c9d4SSatish Balay     PetscFunctionReturn(NULL);
1609371c9d4SSatish Balay   }
1615c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1625c6c1daeSBarry Smith }
1635c6c1daeSBarry Smith 
1645c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1655c6c1daeSBarry Smith /*
1663f423023SBarry Smith    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed
1673f423023SBarry Smith    because that is managed by PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith   This is called by MPI, not by users.
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith */
172*34e79e72SJacob Faibussowitsch PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
173d71ae5a4SJacob Faibussowitsch {
1745c6c1daeSBarry Smith   PetscFunctionBegin;
175*34e79e72SJacob Faibussowitsch   (void)keyval;
176*34e79e72SJacob Faibussowitsch   (void)attr_val;
177*34e79e72SJacob Faibussowitsch   (void)extra_state;
178*34e79e72SJacob Faibussowitsch   PetscCallMPI(PetscInfo(NULL, "Removing viewer data attribute in an MPI_Comm %" PETSC_INTPTR_T_FMT "\n", (PETSC_INTPTR_T)comm));
1795c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1805c6c1daeSBarry Smith }
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith /*@C
183c410d8ccSBarry Smith   PetscViewerASCIIOpen - Opens an ASCII file for writing as a `PETSCVIEWERASCII` `PetscViewer`.
1845c6c1daeSBarry Smith 
185d083f849SBarry Smith   Collective
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith   Input Parameters:
1885c6c1daeSBarry Smith + comm - the communicator
1895c6c1daeSBarry Smith - name - the file name
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith   Output Parameter:
1923f423023SBarry Smith . lab - the `PetscViewer` to use with the specified file
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   Level: beginner
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith   Notes:
197f8859db6SBarry Smith   To open a ASCII file as a viewer for reading one must use the sequence
198811af0c4SBarry Smith .vb
199811af0c4SBarry Smith    PetscViewerCreate(comm,&lab);
200811af0c4SBarry Smith    PetscViewerSetType(lab,PETSCVIEWERASCII);
201811af0c4SBarry Smith    PetscViewerFileSetMode(lab,FILE_MODE_READ);
202811af0c4SBarry Smith    PetscViewerFileSetName(lab,name);
203811af0c4SBarry Smith .ve
204f8859db6SBarry Smith 
205811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`.
2065c6c1daeSBarry Smith 
2072ea3bc1cSBarry Smith   The MPI communicator used here must match that used by the object one is viewing. For example if the
208811af0c4SBarry Smith   Mat was created with a `PETSC_COMM_WORLD`, then the Viewer must be created with `PETSC_COMM_WORLD`
2095c6c1daeSBarry Smith 
210811af0c4SBarry Smith   As shown below, `PetscViewerASCIIOpen()` is useful in conjunction with
211811af0c4SBarry Smith   `MatView()` and `VecView()`
2125c6c1daeSBarry Smith .vb
2135c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2145c6c1daeSBarry Smith      MatView(matrix,viewer);
2155c6c1daeSBarry Smith .ve
2165c6c1daeSBarry Smith 
217d1f92df0SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIRead()`, `PETSCVIEWERASCII`
218db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
219db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`,
2205c6c1daeSBarry Smith @*/
221d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *lab)
222d71ae5a4SJacob Faibussowitsch {
2235c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
2245c6c1daeSBarry Smith   PetscBool        flg, eq;
2255c6c1daeSBarry Smith   size_t           len;
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith   PetscFunctionBegin;
2289566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
2295c6c1daeSBarry Smith   if (!len) {
2309566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(comm, lab));
2319566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)*lab));
2323ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2335c6c1daeSBarry Smith   }
2349566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
23548a46eb9SPierre Jolivet   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_DelViewer, &Petsc_Viewer_keyval, (void *)0));
2362bf49c77SBarry Smith   /*
2372bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2382bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2392bf49c77SBarry Smith 
2402bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2412bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2422bf49c77SBarry Smith   */
2435c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2449566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2455c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2475c6c1daeSBarry Smith   if (flg) {
2485c6c1daeSBarry Smith     while (vlink) {
2499566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)(vlink->viewer->data))->filename, &eq));
2505c6c1daeSBarry Smith       if (eq) {
2519566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
2525c6c1daeSBarry Smith         *lab = vlink->viewer;
2539566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2549566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2553ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
2565c6c1daeSBarry Smith       }
2575c6c1daeSBarry Smith       vlink = vlink->next;
2585c6c1daeSBarry Smith     }
2595c6c1daeSBarry Smith   }
2609566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2619566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2621baa6e33SBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*lab, name));
2635c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2649566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
2655c6c1daeSBarry Smith   nv->viewer = *lab;
2665c6c1daeSBarry Smith   if (!flg) {
2679566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2685c6c1daeSBarry Smith   } else {
2699566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2705c6c1daeSBarry Smith     if (vlink) {
2715c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2725c6c1daeSBarry Smith       vlink->next = nv;
2735c6c1daeSBarry Smith     } else {
2749566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2755c6c1daeSBarry Smith     }
2765c6c1daeSBarry Smith   }
2779566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2789566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2805c6c1daeSBarry Smith }
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith /*@C
2833f423023SBarry Smith   PetscViewerASCIIOpenWithFILE - Given an open file creates an `PETSCVIEWERASCII` viewer that prints to it.
2845c6c1daeSBarry Smith 
285d083f849SBarry Smith   Collective
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith   Input Parameters:
2885c6c1daeSBarry Smith + comm - the communicator
28920f4b53cSBarry Smith - fd   - the `FILE` pointer
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith   Output Parameter:
292811af0c4SBarry Smith . lab - the `PetscViewer` to use with the specified file
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith   Level: beginner
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith   Notes:
297811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2985c6c1daeSBarry Smith 
299811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
3005c6c1daeSBarry Smith   then only the first processor in the group uses the file.  All other
3015c6c1daeSBarry Smith   processors send their data to the first processor to print.
3025c6c1daeSBarry Smith 
303aec76313SJacob Faibussowitsch   Fortran Notes:
304e4096674SBarry Smith   Use `PetscViewerASCIIOpenWithFileUnit()`
305e4096674SBarry Smith 
306e4096674SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIOpenWithFileUnit()`,
307db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
308811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
3095c6c1daeSBarry Smith @*/
310d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *lab)
311d71ae5a4SJacob Faibussowitsch {
3125c6c1daeSBarry Smith   PetscFunctionBegin;
3139566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
3149566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
3159566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetFILE(*lab, fd));
3163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3175c6c1daeSBarry Smith }
3185c6c1daeSBarry Smith 
319811af0c4SBarry Smith /*@C
3203f423023SBarry Smith   PetscViewerASCIISetFILE - Given an open file sets the `PETSCVIEWERASCII` viewer to use the file for output
321811af0c4SBarry Smith 
32220f4b53cSBarry Smith   Not Collective
323811af0c4SBarry Smith 
324811af0c4SBarry Smith   Input Parameters:
325811af0c4SBarry Smith + viewer - the `PetscViewer` to use with the specified file
32620f4b53cSBarry Smith - fd     - the `FILE` pointer
327811af0c4SBarry Smith 
328811af0c4SBarry Smith   Level: beginner
329811af0c4SBarry Smith 
330811af0c4SBarry Smith   Notes:
331c410d8ccSBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the `fd` will NOT be closed.
332811af0c4SBarry Smith 
333811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
334811af0c4SBarry Smith   then only the first processor in the group uses the file.  All other
335811af0c4SBarry Smith   processors send their data to the first processor to print.
336811af0c4SBarry Smith 
337aec76313SJacob Faibussowitsch   Fortran Notes:
338e4096674SBarry Smith   Use `PetscViewerASCIISetFileUnit()`
339e4096674SBarry Smith 
340e4096674SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIISetFileUnit()`,
341811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
342811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
343811af0c4SBarry Smith @*/
344d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd)
345d71ae5a4SJacob Faibussowitsch {
3465c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3475c6c1daeSBarry Smith 
3485c6c1daeSBarry Smith   PetscFunctionBegin;
3495c6c1daeSBarry Smith   vascii->fd        = fd;
3505c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3525c6c1daeSBarry Smith }
353