xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 9c5ded49d1522df7a1a8ade075fe8b0a6b7eb2f1)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
55c6c1daeSBarry Smith 
6e2dcd6d3SBarry Smith /*
7e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
8e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
9e2dcd6d3SBarry Smith */
10d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
11e2dcd6d3SBarry Smith 
12*9c5ded49SBarry Smith /*@
135c6c1daeSBarry Smith    PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
145c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith    Collective on MPI_Comm
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith    Input Parameter:
195c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith    Level: beginner
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith    Notes:
245c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
275c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
285c6c1daeSBarry Smith 
295c6c1daeSBarry Smith @*/
305c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
315c6c1daeSBarry Smith {
325c6c1daeSBarry Smith   PetscErrorCode ierr;
33e2dcd6d3SBarry Smith   PetscBool      flg;
34e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith   PetscFunctionBegin;
375ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
38e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
39e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
4012801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);CHKERRQ(ierr);
41e2dcd6d3SBarry Smith   }
4247435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Stdout_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
43e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
44e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stdout",viewer);CHKERRQ(ierr);
45e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
4647435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);CHKERRQ(ierr);
47e2dcd6d3SBarry Smith   }
48e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
495ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
505c6c1daeSBarry Smith   PetscFunctionReturn(0);
515c6c1daeSBarry Smith }
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith /*@C
545c6c1daeSBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
555c6c1daeSBarry Smith                     in a communicator.
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith    Collective on MPI_Comm
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith    Input Parameter:
605c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
615c6c1daeSBarry Smith 
625c6c1daeSBarry Smith    Level: beginner
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith    Notes:
655c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
665c6c1daeSBarry Smith    an error code. Usually used in the form
675c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
705c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
715c6c1daeSBarry Smith 
725c6c1daeSBarry Smith @*/
735c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDOUT_(MPI_Comm comm)
745c6c1daeSBarry Smith {
755c6c1daeSBarry Smith   PetscErrorCode ierr;
765c6c1daeSBarry Smith   PetscViewer    viewer;
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith   PetscFunctionBegin;
795c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm,&viewer);
80efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
815c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
825c6c1daeSBarry Smith }
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
855c6c1daeSBarry Smith 
86e2dcd6d3SBarry Smith /*
87e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
88e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
89e2dcd6d3SBarry Smith */
90d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
91e2dcd6d3SBarry Smith 
92*9c5ded49SBarry Smith /*@
935c6c1daeSBarry Smith    PetscViewerASCIIGetStderr - Creates a ASCII PetscViewer shared by all processors
945c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDERR_()
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith    Collective on MPI_Comm
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith    Input Parameter:
995c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1005c6c1daeSBarry Smith 
1015c6c1daeSBarry Smith    Level: beginner
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith    Notes:
1045c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDERR_()
1055c6c1daeSBarry Smith 
1065c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDERR_WORLD,
1075c6c1daeSBarry Smith           PETSC_VIEWER_STDERR_SELF
1085c6c1daeSBarry Smith 
1095c6c1daeSBarry Smith @*/
1105c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStderr(MPI_Comm comm,PetscViewer *viewer)
1115c6c1daeSBarry Smith {
1125c6c1daeSBarry Smith   PetscErrorCode ierr;
113e2dcd6d3SBarry Smith   PetscBool      flg;
114e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
115e2dcd6d3SBarry Smith 
116e2dcd6d3SBarry Smith   PetscFunctionBegin;
1175ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
118e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
119e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
12012801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);CHKERRQ(ierr);
121e2dcd6d3SBarry Smith   }
12247435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Stderr_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
123e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
124e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stderr",viewer);CHKERRQ(ierr);
125e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
12647435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);CHKERRQ(ierr);
127e2dcd6d3SBarry Smith   }
128e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
1295ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
1305c6c1daeSBarry Smith   PetscFunctionReturn(0);
1315c6c1daeSBarry Smith }
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith /*@C
1345c6c1daeSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
1355c6c1daeSBarry Smith                     in a communicator.
1365c6c1daeSBarry Smith 
1375c6c1daeSBarry Smith    Collective on MPI_Comm
1385c6c1daeSBarry Smith 
1395c6c1daeSBarry Smith    Input Parameter:
1405c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith    Level: beginner
1435c6c1daeSBarry Smith 
1445c6c1daeSBarry Smith    Note:
1455c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1465c6c1daeSBarry Smith    an error code. Usually used in the form
1475c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1485c6c1daeSBarry Smith 
1495c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
1505c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
1515c6c1daeSBarry Smith @*/
1525c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDERR_(MPI_Comm comm)
1535c6c1daeSBarry Smith {
1545c6c1daeSBarry Smith   PetscErrorCode ierr;
1555c6c1daeSBarry Smith   PetscViewer    viewer;
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith   PetscFunctionBegin;
1585c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm,&viewer);
159efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
1605c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1615c6c1daeSBarry Smith }
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith 
1645c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1655c6c1daeSBarry Smith /*
1665c6c1daeSBarry Smith    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed because that is managed by
1675c6c1daeSBarry Smith    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 */
1728cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
1735c6c1daeSBarry Smith {
1745c6c1daeSBarry Smith   PetscErrorCode ierr;
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith   PetscFunctionBegin;
17712801b39SBarry Smith   ierr = PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
1785c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1795c6c1daeSBarry Smith }
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith /*@C
182f8859db6SBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file for writing as a PetscViewer.
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith    Collective on MPI_Comm
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith    Input Parameters:
1875c6c1daeSBarry Smith +  comm - the communicator
1885c6c1daeSBarry Smith -  name - the file name
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith    Output Parameter:
1915c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith    Level: beginner
1945c6c1daeSBarry Smith 
1955c6c1daeSBarry Smith    Notes:
196f8859db6SBarry Smith    To open a ASCII file as a viewer for reading one must use the sequence
197f8859db6SBarry Smith $     PetscViewerCreate(comm,&lab);
198f8859db6SBarry Smith $     PetscViewerSetType(lab,PETSCVIEWERASCII);
199f8859db6SBarry Smith $     PetscViewerFileSetMode(lab,FILE_MODE_READ);
200f8859db6SBarry Smith $     PetscViewerFileSetName(lab,name);
201f8859db6SBarry Smith 
2025c6c1daeSBarry 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
2052ea3bc1cSBarry Smith    Mat was created with a PETSC_COMM_WORLD, then the Viewer must be created with PETSC_COMM_WORLD
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith    As shown below, PetscViewerASCIIOpen() is useful in conjunction with
2085c6c1daeSBarry 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 
2145c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
2155c6c1daeSBarry Smith   Concepts: printf
2165c6c1daeSBarry Smith   Concepts: printing
2175c6c1daeSBarry Smith   Concepts: accessing remote file
2185c6c1daeSBarry Smith   Concepts: remote file
2195c6c1daeSBarry Smith 
220f8859db6SBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(), PetscViewerASCIIRead()
2216a9046bcSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
2225c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
2235c6c1daeSBarry Smith @*/
2245c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
2255c6c1daeSBarry Smith {
2265c6c1daeSBarry Smith   PetscErrorCode  ierr;
2275c6c1daeSBarry Smith   PetscViewerLink *vlink,*nv;
2285c6c1daeSBarry Smith   PetscBool       flg,eq;
2295c6c1daeSBarry Smith   size_t          len;
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith   PetscFunctionBegin;
2325c6c1daeSBarry Smith   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
2335c6c1daeSBarry Smith   if (!len) {
2345c6c1daeSBarry Smith     ierr = PetscViewerASCIIGetStdout(comm,lab);CHKERRQ(ierr);
2355c6c1daeSBarry Smith     ierr = PetscObjectReference((PetscObject)*lab);CHKERRQ(ierr);
2365c6c1daeSBarry Smith     PetscFunctionReturn(0);
2375c6c1daeSBarry Smith   }
2385ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2395c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
24012801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
2415c6c1daeSBarry Smith   }
2422bf49c77SBarry Smith   /*
2432bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2442bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2452bf49c77SBarry Smith 
2462bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2472bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2482bf49c77SBarry Smith   */
2495c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2500298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr);
2515c6c1daeSBarry Smith   /* has file already been opened into a viewer */
25247435625SJed Brown   ierr = MPI_Comm_get_attr(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2535c6c1daeSBarry Smith   if (flg) {
2545c6c1daeSBarry Smith     while (vlink) {
2555c6c1daeSBarry Smith       ierr = PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);CHKERRQ(ierr);
2565c6c1daeSBarry Smith       if (eq) {
2575c6c1daeSBarry Smith         ierr = PetscObjectReference((PetscObject)vlink->viewer);CHKERRQ(ierr);
2585c6c1daeSBarry Smith         *lab = vlink->viewer;
2595c6c1daeSBarry Smith         ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2605ad9ad5bSBarry Smith         ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2615c6c1daeSBarry Smith         PetscFunctionReturn(0);
2625c6c1daeSBarry Smith       }
2635c6c1daeSBarry Smith       vlink = vlink->next;
2645c6c1daeSBarry Smith     }
2655c6c1daeSBarry Smith   }
2665c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
2675c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
2685c6c1daeSBarry Smith   if (name) {
2695c6c1daeSBarry Smith     ierr = PetscViewerFileSetName(*lab,name);CHKERRQ(ierr);
2705c6c1daeSBarry Smith   }
2715c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
272b00a9115SJed Brown   ierr       = PetscNew(&nv);CHKERRQ(ierr);
2735c6c1daeSBarry Smith   nv->viewer = *lab;
2745c6c1daeSBarry Smith   if (!flg) {
27547435625SJed Brown     ierr = MPI_Comm_set_attr(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2765c6c1daeSBarry Smith   } else {
27747435625SJed Brown     ierr = MPI_Comm_get_attr(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2785c6c1daeSBarry Smith     if (vlink) {
2795c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2805c6c1daeSBarry Smith       vlink->next = nv;
2815c6c1daeSBarry Smith     } else {
28247435625SJed Brown       ierr = MPI_Comm_set_attr(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2835c6c1daeSBarry Smith     }
2845c6c1daeSBarry Smith   }
2855c6c1daeSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2865ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2875c6c1daeSBarry Smith   PetscFunctionReturn(0);
2885c6c1daeSBarry Smith }
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith /*@C
2915c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith    Collective on MPI_Comm
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith    Input Parameters:
2965c6c1daeSBarry Smith +  comm - the communicator
2975c6c1daeSBarry Smith -  fd - the FILE pointer
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith    Output Parameter:
3005c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
3015c6c1daeSBarry Smith 
3025c6c1daeSBarry Smith    Level: beginner
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith    Notes:
3055c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
3085c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
3095c6c1daeSBarry Smith    processors send their data to the first processor to print.
3105c6c1daeSBarry Smith 
3115c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
3125c6c1daeSBarry Smith   Concepts: printf
3135c6c1daeSBarry Smith   Concepts: printing
3145c6c1daeSBarry Smith   Concepts: accessing remote file
3155c6c1daeSBarry Smith   Concepts: remote file
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
3186a9046bcSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
3195c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
3205c6c1daeSBarry Smith @*/
3215c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
3225c6c1daeSBarry Smith {
3235c6c1daeSBarry Smith   PetscErrorCode ierr;
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith   PetscFunctionBegin;
3265c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
3275c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
3285c6c1daeSBarry Smith   ierr = PetscViewerASCIISetFILE(*lab,fd);CHKERRQ(ierr);
3295c6c1daeSBarry Smith   PetscFunctionReturn(0);
3305c6c1daeSBarry Smith }
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
3335c6c1daeSBarry Smith {
3345c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
3355c6c1daeSBarry Smith 
3365c6c1daeSBarry Smith   PetscFunctionBegin;
3375c6c1daeSBarry Smith   vascii->fd        = fd;
3385c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3395c6c1daeSBarry Smith   PetscFunctionReturn(0);
3405c6c1daeSBarry Smith }
3415c6c1daeSBarry Smith 
3425c6c1daeSBarry Smith 
3435c6c1daeSBarry Smith 
344