Actual source code: viewers.c

petsc-3.4.4 2014-03-13
  2: #include <petscsys.h>
  3: #include <petsc-private/viewerimpl.h>

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

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

 16:    Collective on PetscViewers

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

 21:    Level: intermediate

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

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

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

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

 46:    Collective on MPI_Comm

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

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

 54:    Level: intermediate

 56:    Concepts: PetscViewer^array of

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

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

 66:   PetscNew(struct _n_PetscViewers,v);
 67:   (*v)->n    = 64;
 68:   (*v)->comm = comm;

 70:   PetscMalloc(64*sizeof(PetscViewer),&(*v)->viewer);
 71:   PetscMemzero((*v)->viewer,64*sizeof(PetscViewer));
 72:   return(0);
 73: }

 77: /*@C
 78:    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection

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

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

 86:    Output Parameter:
 87: .  viewer - the PetscViewer

 89:    Level: intermediate

 91:    Concepts: PetscViewer^array of

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

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

101:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
102:   if (n >= viewers->n) {
103:     PetscViewer *v;
104:     int         newn = n + 64; /* add 64 new ones at a time */

106:     PetscMalloc(newn*sizeof(PetscViewer),&v);
107:     PetscMemzero(v,newn*sizeof(PetscViewer));
108:     PetscMemcpy(v,viewers->viewer,viewers->n*sizeof(PetscViewer));
109:     PetscFree(viewers->viewer);

111:     viewers->viewer = v;
112:   }
113:   if (!viewers->viewer[n]) {
114:     PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
115:   }
116:   *viewer = viewers->viewer[n];
117:   return(0);
118: }