xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision cc4c1da905d89950b196b027190941013bd3e15a)
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 
149*cc4c1da9SBarry 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:
1593f423023SBarry Smith . lab - 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
166811af0c4SBarry Smith    PetscViewerCreate(comm,&lab);
167811af0c4SBarry Smith    PetscViewerSetType(lab,PETSCVIEWERASCII);
168811af0c4SBarry Smith    PetscViewerFileSetMode(lab,FILE_MODE_READ);
169811af0c4SBarry Smith    PetscViewerFileSetName(lab,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 @*/
188d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *lab)
189d71ae5a4SJacob Faibussowitsch {
1905c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
1915c6c1daeSBarry Smith   PetscBool        flg, eq;
1925c6c1daeSBarry Smith   size_t           len;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   PetscFunctionBegin;
1959566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
1965c6c1daeSBarry Smith   if (!len) {
1979566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(comm, lab));
1989566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)*lab));
1993ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2005c6c1daeSBarry Smith   }
2019566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
20248a46eb9SPierre 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));
2032bf49c77SBarry Smith   /*
2042bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2052bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2062bf49c77SBarry Smith 
2072bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2082bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2092bf49c77SBarry Smith   */
2105c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2119566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2125c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2145c6c1daeSBarry Smith   if (flg) {
2155c6c1daeSBarry Smith     while (vlink) {
216f4f49eeaSPierre Jolivet       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)vlink->viewer->data)->filename, &eq));
2175c6c1daeSBarry Smith       if (eq) {
2189566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
2195c6c1daeSBarry Smith         *lab = vlink->viewer;
2209566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2219566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2223ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
2235c6c1daeSBarry Smith       }
2245c6c1daeSBarry Smith       vlink = vlink->next;
2255c6c1daeSBarry Smith     }
2265c6c1daeSBarry Smith   }
2279566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2289566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2291baa6e33SBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*lab, name));
2305c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2319566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
2325c6c1daeSBarry Smith   nv->viewer = *lab;
2335c6c1daeSBarry Smith   if (!flg) {
2349566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2355c6c1daeSBarry Smith   } else {
2369566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2375c6c1daeSBarry Smith     if (vlink) {
2385c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2395c6c1daeSBarry Smith       vlink->next = nv;
2405c6c1daeSBarry Smith     } else {
2419566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2425c6c1daeSBarry Smith     }
2435c6c1daeSBarry Smith   }
2449566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2459566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2475c6c1daeSBarry Smith }
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith /*@C
2503f423023SBarry Smith   PetscViewerASCIIOpenWithFILE - Given an open file creates an `PETSCVIEWERASCII` viewer that prints to it.
2515c6c1daeSBarry Smith 
252d083f849SBarry Smith   Collective
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith   Input Parameters:
2555c6c1daeSBarry Smith + comm - the communicator
25620f4b53cSBarry Smith - fd   - the `FILE` pointer
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith   Output Parameter:
259811af0c4SBarry Smith . lab - the `PetscViewer` to use with the specified file
2605c6c1daeSBarry Smith 
2615c6c1daeSBarry Smith   Level: beginner
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith   Notes:
264811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2655c6c1daeSBarry Smith 
266811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
2675c6c1daeSBarry Smith   then only the first processor in the group uses the file.  All other
2685c6c1daeSBarry Smith   processors send their data to the first processor to print.
2695c6c1daeSBarry Smith 
270aec76313SJacob Faibussowitsch   Fortran Notes:
271e4096674SBarry Smith   Use `PetscViewerASCIIOpenWithFileUnit()`
272e4096674SBarry Smith 
273e4096674SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIOpenWithFileUnit()`,
274db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
275811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
2765c6c1daeSBarry Smith @*/
277d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *lab)
278d71ae5a4SJacob Faibussowitsch {
2795c6c1daeSBarry Smith   PetscFunctionBegin;
2809566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2819566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2829566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetFILE(*lab, fd));
2833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2845c6c1daeSBarry Smith }
2855c6c1daeSBarry Smith 
286811af0c4SBarry Smith /*@C
2873f423023SBarry Smith   PetscViewerASCIISetFILE - Given an open file sets the `PETSCVIEWERASCII` viewer to use the file for output
288811af0c4SBarry Smith 
28920f4b53cSBarry Smith   Not Collective
290811af0c4SBarry Smith 
291811af0c4SBarry Smith   Input Parameters:
292811af0c4SBarry Smith + viewer - the `PetscViewer` to use with the specified file
29320f4b53cSBarry Smith - fd     - the `FILE` pointer
294811af0c4SBarry Smith 
295811af0c4SBarry Smith   Level: beginner
296811af0c4SBarry Smith 
297811af0c4SBarry Smith   Notes:
298c410d8ccSBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the `fd` will NOT be closed.
299811af0c4SBarry Smith 
300811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
301811af0c4SBarry Smith   then only the first processor in the group uses the file.  All other
302811af0c4SBarry Smith   processors send their data to the first processor to print.
303811af0c4SBarry Smith 
304aec76313SJacob Faibussowitsch   Fortran Notes:
305e4096674SBarry Smith   Use `PetscViewerASCIISetFileUnit()`
306e4096674SBarry Smith 
307e4096674SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIISetFileUnit()`,
308811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
309811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
310811af0c4SBarry Smith @*/
311d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd)
312d71ae5a4SJacob Faibussowitsch {
3135c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith   PetscFunctionBegin;
3165c6c1daeSBarry Smith   vascii->fd        = fd;
3175c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3195c6c1daeSBarry Smith }
320