xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 377f809a66ddb103acb624d65d678901d378fe03)
1f5860696SSatish Balay #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h> /*I     "petscviewer.h"   I*/
25c6c1daeSBarry Smith 
3e2dcd6d3SBarry Smith /*
4e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
5e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
6e2dcd6d3SBarry Smith */
7d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
8e2dcd6d3SBarry Smith 
95c6c1daeSBarry Smith /*@C
10c410d8ccSBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
115c6c1daeSBarry Smith                     in a communicator.
125c6c1daeSBarry Smith 
13d083f849SBarry Smith    Collective
145c6c1daeSBarry Smith 
155c6c1daeSBarry Smith    Input Parameter:
16811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith    Level: beginner
195c6c1daeSBarry Smith 
2034fa283eSBarry Smith    Notes:
2134fa283eSBarry Smith    This object is destroyed in `PetscFinalize()`, `PetscViewerDestroy()` should never be called on it
2234fa283eSBarry Smith 
235c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
245c6c1daeSBarry Smith    an error code. Usually used in the form
255c6c1daeSBarry Smith $      XXXView(XXX object, PETSC_VIEWER_STDOUT_(comm));
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 PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
31d71ae5a4SJacob Faibussowitsch {
325c6c1daeSBarry Smith   PetscErrorCode ierr;
335c6c1daeSBarry Smith   PetscViewer    viewer;
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith   PetscFunctionBegin;
365c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm, &viewer);
379371c9d4SSatish Balay   if (ierr) {
383ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDOUT_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
399371c9d4SSatish Balay     PetscFunctionReturn(NULL);
409371c9d4SSatish Balay   }
415c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
425c6c1daeSBarry Smith }
435c6c1daeSBarry Smith 
44e2dcd6d3SBarry Smith /*
45e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
46e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
47e2dcd6d3SBarry Smith */
48d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
49e2dcd6d3SBarry Smith 
509c5ded49SBarry Smith /*@
51c410d8ccSBarry Smith   PetscViewerASCIIGetStderr - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
52811af0c4SBarry Smith   in a communicator. Error returning version of `PETSC_VIEWER_STDERR_()`
535c6c1daeSBarry Smith 
54d083f849SBarry Smith   Collective
555c6c1daeSBarry Smith 
565c6c1daeSBarry Smith   Input Parameter:
57811af0c4SBarry Smith . comm - the MPI communicator to share the `PetscViewer`
585c6c1daeSBarry Smith 
5910450e9eSJacob Faibussowitsch   Output Parameter:
6010450e9eSJacob Faibussowitsch . viewer - the viewer
6110450e9eSJacob Faibussowitsch 
625c6c1daeSBarry Smith   Level: beginner
635c6c1daeSBarry Smith 
64811af0c4SBarry Smith   Note:
6534fa283eSBarry Smith   This object is destroyed in `PetscFinalize()`, `PetscViewerDestroy()` should never be called on it
6634fa283eSBarry Smith 
6734fa283eSBarry Smith   Developer Note:
683f423023SBarry Smith   This should be used in all PETSc source code instead of `PETSC_VIEWER_STDERR_()` since it allows error checking
695c6c1daeSBarry Smith 
70d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDERR_WORLD`,
71db781477SPatrick Sanan           `PETSC_VIEWER_STDERR_SELF`
725c6c1daeSBarry Smith @*/
73d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetStderr(MPI_Comm comm, PetscViewer *viewer)
74d71ae5a4SJacob Faibussowitsch {
75e2dcd6d3SBarry Smith   PetscBool flg;
76e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
77e2dcd6d3SBarry Smith 
78e2dcd6d3SBarry Smith   PetscFunctionBegin;
799566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStderr));
809566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
8148a46eb9SPierre 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));
829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void **)viewer, (PetscMPIInt *)&flg));
83e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
849566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stderr", viewer));
8534fa283eSBarry Smith     ((PetscObject)*viewer)->persistent = PETSC_TRUE;
869566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
879566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void *)*viewer));
88e2dcd6d3SBarry Smith   }
899566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
909566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr));
913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
925c6c1daeSBarry Smith }
935c6c1daeSBarry Smith 
945c6c1daeSBarry Smith /*@C
95c410d8ccSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all MPI processes
965c6c1daeSBarry Smith                     in a communicator.
975c6c1daeSBarry Smith 
98d083f849SBarry Smith    Collective
995c6c1daeSBarry Smith 
1005c6c1daeSBarry Smith    Input Parameter:
101811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith    Level: beginner
1045c6c1daeSBarry Smith 
1053f423023SBarry Smith    Notes:
10634fa283eSBarry Smith    This object is destroyed in `PetscFinalize()`, `PetscViewerDestroy()` should never be called on it
10734fa283eSBarry Smith 
1085c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1095c6c1daeSBarry Smith    an error code. Usually used in the form
1105c6c1daeSBarry Smith $      XXXView(XXX object, PETSC_VIEWER_STDERR_(comm));
1115c6c1daeSBarry Smith 
1123f423023SBarry Smith    `PetscViewerASCIIGetStderr()` is preferred  since it allows error checking
1133f423023SBarry Smith 
114d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDOUT_WORLD`,
115db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDERR_WORLD`, `PETSC_VIEWER_STDERR_SELF`
1165c6c1daeSBarry Smith @*/
117d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
118d71ae5a4SJacob Faibussowitsch {
1195c6c1daeSBarry Smith   PetscErrorCode ierr;
1205c6c1daeSBarry Smith   PetscViewer    viewer;
1215c6c1daeSBarry Smith 
1225c6c1daeSBarry Smith   PetscFunctionBegin;
1235c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm, &viewer);
1249371c9d4SSatish Balay   if (ierr) {
1253ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDERR_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
1269371c9d4SSatish Balay     PetscFunctionReturn(NULL);
1279371c9d4SSatish Balay   }
1285c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1295c6c1daeSBarry Smith }
1305c6c1daeSBarry Smith 
1315c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1325c6c1daeSBarry Smith /*
1333f423023SBarry Smith    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed
1343f423023SBarry Smith    because that is managed by PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1355c6c1daeSBarry Smith 
1365c6c1daeSBarry Smith   This is called by MPI, not by users.
1375c6c1daeSBarry Smith 
1385c6c1daeSBarry Smith */
13934e79e72SJacob Faibussowitsch PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
140d71ae5a4SJacob Faibussowitsch {
1415c6c1daeSBarry Smith   PetscFunctionBegin;
14234e79e72SJacob Faibussowitsch   (void)keyval;
14334e79e72SJacob Faibussowitsch   (void)attr_val;
14434e79e72SJacob Faibussowitsch   (void)extra_state;
14534e79e72SJacob Faibussowitsch   PetscCallMPI(PetscInfo(NULL, "Removing viewer data attribute in an MPI_Comm %" PETSC_INTPTR_T_FMT "\n", (PETSC_INTPTR_T)comm));
1465c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1475c6c1daeSBarry Smith }
1485c6c1daeSBarry Smith 
149cc4c1da9SBarry Smith /*@
150c410d8ccSBarry Smith   PetscViewerASCIIOpen - Opens an ASCII file for writing as a `PETSCVIEWERASCII` `PetscViewer`.
1515c6c1daeSBarry Smith 
152d083f849SBarry Smith   Collective
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith   Input Parameters:
1555c6c1daeSBarry Smith + comm - the communicator
1565c6c1daeSBarry Smith - name - the file name
1575c6c1daeSBarry Smith 
1585c6c1daeSBarry Smith   Output Parameter:
159*377f809aSBarry Smith . viewer - the `PetscViewer` to use with the specified file
1605c6c1daeSBarry Smith 
1615c6c1daeSBarry Smith   Level: beginner
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith   Notes:
164f8859db6SBarry Smith   To open a ASCII file as a viewer for reading one must use the sequence
165811af0c4SBarry Smith .vb
166*377f809aSBarry Smith    PetscViewerCreate(comm,&viewer);
167*377f809aSBarry Smith    PetscViewerSetType(viewer,PETSCVIEWERASCII);
168*377f809aSBarry Smith    PetscViewerFileSetMode(viewer,FILE_MODE_READ);
169*377f809aSBarry Smith    PetscViewerFileSetName(viewer,name);
170811af0c4SBarry Smith .ve
171f8859db6SBarry Smith 
172811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`.
1735c6c1daeSBarry Smith 
1742ea3bc1cSBarry Smith   The MPI communicator used here must match that used by the object one is viewing. For example if the
175811af0c4SBarry Smith   Mat was created with a `PETSC_COMM_WORLD`, then the Viewer must be created with `PETSC_COMM_WORLD`
1765c6c1daeSBarry Smith 
177811af0c4SBarry Smith   As shown below, `PetscViewerASCIIOpen()` is useful in conjunction with
178811af0c4SBarry Smith   `MatView()` and `VecView()`
1795c6c1daeSBarry Smith .vb
1805c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
1815c6c1daeSBarry Smith      MatView(matrix,viewer);
1825c6c1daeSBarry Smith .ve
1835c6c1daeSBarry Smith 
184d1f92df0SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIRead()`, `PETSCVIEWERASCII`
185db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
186db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`,
1875c6c1daeSBarry Smith @*/
188*377f809aSBarry Smith PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *viewer)
189d71ae5a4SJacob Faibussowitsch {
1905c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
1915c6c1daeSBarry Smith   PetscBool        flg, eq;
1925c6c1daeSBarry Smith   size_t           len;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   PetscFunctionBegin;
195*377f809aSBarry Smith   PetscAssertPointer(viewer, 3);
1969566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
1975c6c1daeSBarry Smith   if (!len) {
198*377f809aSBarry Smith     PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
199*377f809aSBarry Smith     PetscCall(PetscObjectReference((PetscObject)*viewer));
2003ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2015c6c1daeSBarry Smith   }
2029566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
20348a46eb9SPierre 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));
2042bf49c77SBarry Smith   /*
2052bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2062bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2072bf49c77SBarry Smith 
2082bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2092bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2102bf49c77SBarry Smith   */
2115c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2129566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2135c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2155c6c1daeSBarry Smith   if (flg) {
2165c6c1daeSBarry Smith     while (vlink) {
217f4f49eeaSPierre Jolivet       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)vlink->viewer->data)->filename, &eq));
2185c6c1daeSBarry Smith       if (eq) {
2199566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
220*377f809aSBarry Smith         *viewer = vlink->viewer;
2219566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2229566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2233ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
2245c6c1daeSBarry Smith       }
2255c6c1daeSBarry Smith       vlink = vlink->next;
2265c6c1daeSBarry Smith     }
2275c6c1daeSBarry Smith   }
228*377f809aSBarry Smith   PetscCall(PetscViewerCreate(comm, viewer));
229*377f809aSBarry Smith   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERASCII));
230*377f809aSBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*viewer, name));
2315c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2329566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
233*377f809aSBarry Smith   nv->viewer = *viewer;
2345c6c1daeSBarry Smith   if (!flg) {
2359566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2365c6c1daeSBarry Smith   } else {
2379566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2385c6c1daeSBarry Smith     if (vlink) {
2395c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2405c6c1daeSBarry Smith       vlink->next = nv;
2415c6c1daeSBarry Smith     } else {
2429566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2435c6c1daeSBarry Smith     }
2445c6c1daeSBarry Smith   }
2459566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2469566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2485c6c1daeSBarry Smith }
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith /*@C
2513f423023SBarry Smith   PetscViewerASCIIOpenWithFILE - Given an open file creates an `PETSCVIEWERASCII` viewer that prints to it.
2525c6c1daeSBarry Smith 
253d083f849SBarry Smith   Collective
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith   Input Parameters:
2565c6c1daeSBarry Smith + comm - the communicator
25720f4b53cSBarry Smith - fd   - the `FILE` pointer
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith   Output Parameter:
260*377f809aSBarry Smith . viewer - the `PetscViewer` to use with the specified file
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith   Level: beginner
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith   Notes:
265811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2665c6c1daeSBarry Smith 
267811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
2685c6c1daeSBarry Smith   then only the first processor in the group uses the file.  All other
2695c6c1daeSBarry Smith   processors send their data to the first processor to print.
2705c6c1daeSBarry Smith 
271aec76313SJacob Faibussowitsch   Fortran Notes:
272e4096674SBarry Smith   Use `PetscViewerASCIIOpenWithFileUnit()`
273e4096674SBarry Smith 
274e4096674SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIOpenWithFileUnit()`,
275db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
276811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
2775c6c1daeSBarry Smith @*/
278*377f809aSBarry Smith PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *viewer)
279d71ae5a4SJacob Faibussowitsch {
2805c6c1daeSBarry Smith   PetscFunctionBegin;
281*377f809aSBarry Smith   PetscCall(PetscViewerCreate(comm, viewer));
282*377f809aSBarry Smith   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERASCII));
283*377f809aSBarry Smith   PetscCall(PetscViewerASCIISetFILE(*viewer, fd));
2843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2855c6c1daeSBarry Smith }
2865c6c1daeSBarry Smith 
287811af0c4SBarry Smith /*@C
2883f423023SBarry Smith   PetscViewerASCIISetFILE - Given an open file sets the `PETSCVIEWERASCII` viewer to use the file for output
289811af0c4SBarry Smith 
29020f4b53cSBarry Smith   Not Collective
291811af0c4SBarry Smith 
292811af0c4SBarry Smith   Input Parameters:
293811af0c4SBarry Smith + viewer - the `PetscViewer` to use with the specified file
29420f4b53cSBarry Smith - fd     - the `FILE` pointer
295811af0c4SBarry Smith 
296811af0c4SBarry Smith   Level: beginner
297811af0c4SBarry Smith 
298811af0c4SBarry Smith   Notes:
299c410d8ccSBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the `fd` will NOT be closed.
300811af0c4SBarry Smith 
301811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
302811af0c4SBarry Smith   then only the first processor in the group uses the file.  All other
303811af0c4SBarry Smith   processors send their data to the first processor to print.
304811af0c4SBarry Smith 
305aec76313SJacob Faibussowitsch   Fortran Notes:
306e4096674SBarry Smith   Use `PetscViewerASCIISetFileUnit()`
307e4096674SBarry Smith 
308e4096674SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIISetFileUnit()`,
309811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
310811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
311811af0c4SBarry Smith @*/
312d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd)
313d71ae5a4SJacob Faibussowitsch {
3145c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3155c6c1daeSBarry Smith 
3165c6c1daeSBarry Smith   PetscFunctionBegin;
3175c6c1daeSBarry Smith   vascii->fd        = fd;
3185c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3205c6c1daeSBarry Smith }
321