xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 10450e9e44b354a0a3da7bbd573407bdf051df10)
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 
19*10450e9eSJacob Faibussowitsch   Output Parameter:
20*10450e9eSJacob Faibussowitsch . viewer - the viewer
21*10450e9eSJacob 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 
98*10450e9eSJacob Faibussowitsch   Output Parameter:
99*10450e9eSJacob Faibussowitsch . viewer - the viewer
100*10450e9eSJacob 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 */
172d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
173d71ae5a4SJacob Faibussowitsch {
1745c6c1daeSBarry Smith   PetscFunctionBegin;
1759566063dSJacob Faibussowitsch   PetscCallMPI(PetscInfo(NULL, "Removing viewer data attribute in an MPI_Comm %ld\n", (long)comm));
1765c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1775c6c1daeSBarry Smith }
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith /*@C
180c410d8ccSBarry Smith   PetscViewerASCIIOpen - Opens an ASCII file for writing as a `PETSCVIEWERASCII` `PetscViewer`.
1815c6c1daeSBarry Smith 
182d083f849SBarry Smith   Collective
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith   Input Parameters:
1855c6c1daeSBarry Smith + comm - the communicator
1865c6c1daeSBarry Smith - name - the file name
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith   Output Parameter:
1893f423023SBarry Smith . lab - the `PetscViewer` to use with the specified file
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith   Level: beginner
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith   Notes:
194f8859db6SBarry Smith   To open a ASCII file as a viewer for reading one must use the sequence
195811af0c4SBarry Smith .vb
196811af0c4SBarry Smith    PetscViewerCreate(comm,&lab);
197811af0c4SBarry Smith    PetscViewerSetType(lab,PETSCVIEWERASCII);
198811af0c4SBarry Smith    PetscViewerFileSetMode(lab,FILE_MODE_READ);
199811af0c4SBarry Smith    PetscViewerFileSetName(lab,name);
200811af0c4SBarry Smith .ve
201f8859db6SBarry Smith 
202811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`.
2035c6c1daeSBarry Smith 
2042ea3bc1cSBarry Smith   The MPI communicator used here must match that used by the object one is viewing. For example if the
205811af0c4SBarry Smith   Mat was created with a `PETSC_COMM_WORLD`, then the Viewer must be created with `PETSC_COMM_WORLD`
2065c6c1daeSBarry Smith 
207811af0c4SBarry Smith   As shown below, `PetscViewerASCIIOpen()` is useful in conjunction with
208811af0c4SBarry Smith   `MatView()` and `VecView()`
2095c6c1daeSBarry Smith .vb
2105c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2115c6c1daeSBarry Smith      MatView(matrix,viewer);
2125c6c1daeSBarry Smith .ve
2135c6c1daeSBarry Smith 
214d1f92df0SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIRead()`, `PETSCVIEWERASCII`
215db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
216db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`,
2175c6c1daeSBarry Smith @*/
218d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *lab)
219d71ae5a4SJacob Faibussowitsch {
2205c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
2215c6c1daeSBarry Smith   PetscBool        flg, eq;
2225c6c1daeSBarry Smith   size_t           len;
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith   PetscFunctionBegin;
2259566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
2265c6c1daeSBarry Smith   if (!len) {
2279566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(comm, lab));
2289566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)*lab));
2293ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2305c6c1daeSBarry Smith   }
2319566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
23248a46eb9SPierre 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));
2332bf49c77SBarry Smith   /*
2342bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2352bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2362bf49c77SBarry Smith 
2372bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2382bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2392bf49c77SBarry Smith   */
2405c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2419566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2425c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2445c6c1daeSBarry Smith   if (flg) {
2455c6c1daeSBarry Smith     while (vlink) {
2469566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)(vlink->viewer->data))->filename, &eq));
2475c6c1daeSBarry Smith       if (eq) {
2489566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
2495c6c1daeSBarry Smith         *lab = vlink->viewer;
2509566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2519566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2523ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
2535c6c1daeSBarry Smith       }
2545c6c1daeSBarry Smith       vlink = vlink->next;
2555c6c1daeSBarry Smith     }
2565c6c1daeSBarry Smith   }
2579566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2589566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2591baa6e33SBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*lab, name));
2605c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2619566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
2625c6c1daeSBarry Smith   nv->viewer = *lab;
2635c6c1daeSBarry Smith   if (!flg) {
2649566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2655c6c1daeSBarry Smith   } else {
2669566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2675c6c1daeSBarry Smith     if (vlink) {
2685c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2695c6c1daeSBarry Smith       vlink->next = nv;
2705c6c1daeSBarry Smith     } else {
2719566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2725c6c1daeSBarry Smith     }
2735c6c1daeSBarry Smith   }
2749566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2759566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2775c6c1daeSBarry Smith }
2785c6c1daeSBarry Smith 
2795c6c1daeSBarry Smith /*@C
2803f423023SBarry Smith   PetscViewerASCIIOpenWithFILE - Given an open file creates an `PETSCVIEWERASCII` viewer that prints to it.
2815c6c1daeSBarry Smith 
282d083f849SBarry Smith   Collective
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith   Input Parameters:
2855c6c1daeSBarry Smith + comm - the communicator
28620f4b53cSBarry Smith - fd   - the `FILE` pointer
2875c6c1daeSBarry Smith 
2885c6c1daeSBarry Smith   Output Parameter:
289811af0c4SBarry Smith . lab - the `PetscViewer` to use with the specified file
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith   Level: beginner
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith   Notes:
294811af0c4SBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2955c6c1daeSBarry Smith 
296811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
2975c6c1daeSBarry Smith   then only the first processor in the group uses the file.  All other
2985c6c1daeSBarry Smith   processors send their data to the first processor to print.
2995c6c1daeSBarry Smith 
300aec76313SJacob Faibussowitsch   Fortran Notes:
301e4096674SBarry Smith   Use `PetscViewerASCIIOpenWithFileUnit()`
302e4096674SBarry Smith 
303e4096674SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIOpenWithFileUnit()`,
304db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
305811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
3065c6c1daeSBarry Smith @*/
307d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *lab)
308d71ae5a4SJacob Faibussowitsch {
3095c6c1daeSBarry Smith   PetscFunctionBegin;
3109566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
3119566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
3129566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetFILE(*lab, fd));
3133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3145c6c1daeSBarry Smith }
3155c6c1daeSBarry Smith 
316811af0c4SBarry Smith /*@C
3173f423023SBarry Smith   PetscViewerASCIISetFILE - Given an open file sets the `PETSCVIEWERASCII` viewer to use the file for output
318811af0c4SBarry Smith 
31920f4b53cSBarry Smith   Not Collective
320811af0c4SBarry Smith 
321811af0c4SBarry Smith   Input Parameters:
322811af0c4SBarry Smith + viewer - the `PetscViewer` to use with the specified file
32320f4b53cSBarry Smith - fd     - the `FILE` pointer
324811af0c4SBarry Smith 
325811af0c4SBarry Smith   Level: beginner
326811af0c4SBarry Smith 
327811af0c4SBarry Smith   Notes:
328c410d8ccSBarry Smith   This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the `fd` will NOT be closed.
329811af0c4SBarry Smith 
330811af0c4SBarry Smith   If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
331811af0c4SBarry Smith   then only the first processor in the group uses the file.  All other
332811af0c4SBarry Smith   processors send their data to the first processor to print.
333811af0c4SBarry Smith 
334aec76313SJacob Faibussowitsch   Fortran Notes:
335e4096674SBarry Smith   Use `PetscViewerASCIISetFileUnit()`
336e4096674SBarry Smith 
337e4096674SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIISetFileUnit()`,
338811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
339811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
340811af0c4SBarry Smith @*/
341d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd)
342d71ae5a4SJacob Faibussowitsch {
3435c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3445c6c1daeSBarry Smith 
3455c6c1daeSBarry Smith   PetscFunctionBegin;
3465c6c1daeSBarry Smith   vascii->fd        = fd;
3475c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3495c6c1daeSBarry Smith }
350