Actual source code: viewers.c

petsc-3.3-p7 2013-05-11
  2: #include <petscsys.h>

  4: struct _n_PetscViewers {
  5:    MPI_Comm    comm;
  6:    PetscViewer *viewer;
  7:    int         n;
  8: } ;

 12: /*@C
 13:    PetscViewersDestroy - Destroys a set of PetscViewers created with PetscViewersCreate().

 15:    Collective on PetscViewers

 17:    Input Parameters:
 18: .  v - the PetscViewers to be destroyed.

 20:    Level: intermediate

 22: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewersCreate()

 24: @*/
 25: PetscErrorCode  PetscViewersDestroy(PetscViewers *v)
 26: {
 27:   int            i;

 31:   if (!*v) return(0);
 32:   for (i=0; i<(*v)->n; i++) {
 33:     PetscViewerDestroy(&(*v)->viewer[i]);
 34:   }
 35:   PetscFree((*v)->viewer);
 36:   PetscFree(*v);
 37:   return(0);
 38: }

 42: /*@C
 43:    PetscViewersCreate - Creates a container to hold a set of PetscViewers.

 45:    Collective on MPI_Comm

 47:    Input Parameter:
 48: .   comm - the MPI communicator

 50:    Output Parameter:
 51: .  v - the collection of PetscViewers

 53:    Level: intermediate

 55:    Concepts: PetscViewer^array of

 57: .seealso: PetscViewerCreate(), PetscViewersDestroy()

 59: @*/
 60: PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
 61: {

 65:   PetscNew(struct _n_PetscViewers,v);
 66:   (*v)->n      = 64;
 67:   (*v)->comm   = comm;
 68:   PetscMalloc(64*sizeof(PetscViewer),&(*v)->viewer);
 69:   PetscMemzero((*v)->viewer,64*sizeof(PetscViewer));
 70:   return(0);
 71: }

 75: /*@C
 76:    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection

 78:    Not Collective, but PetscViewer will be collective object on PetscViewers

 80:    Input Parameter:
 81: +   viewers - object created with PetscViewersCreate()
 82: -   n - number of PetscViewer you want

 84:    Output Parameter:
 85: .  viewer - the PetscViewer

 87:    Level: intermediate

 89:    Concepts: PetscViewer^array of

 91: .seealso: PetscViewersCreate(), PetscViewersDestroy()

 93: @*/
 94: PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
 95: {

 99:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
100:   if (n >= viewers->n) {
101:     PetscViewer *v;
102:     int    newn = n + 64; /* add 64 new ones at a time */
103: 
104:     PetscMalloc(newn*sizeof(PetscViewer),&v);
105:     PetscMemzero(v,newn*sizeof(PetscViewer));
106:     PetscMemcpy(v,viewers->viewer,viewers->n*sizeof(PetscViewer));
107:     PetscFree(viewers->viewer);
108:     viewers->viewer = v;
109:   }
110:   if (!viewers->viewer[n]) {
111:     PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
112:   }
113:   *viewer = viewers->viewer[n];
114:   return(0);
115: }