Actual source code: viewers.c

      1: #define PETSC_DLL

     3:  #include petsc.h

      5: struct _p_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 PETSC_DLLEXPORT PetscViewersDestroy(PetscViewers v)
     27: {
     28:   int         i;

     32:   for (i=0; i<v->n; i++) {
     33:     if (v->viewer[i]) {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: .  PetscViewers - the collection of PetscViewers

     53:    Level: intermediate

     55:    Concepts: PetscViewer^array of

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

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

     65:   PetscNew(struct _p_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 PETSC_DLLEXPORT PetscViewersGetViewer(PetscViewers viewers,int n,PetscViewer *viewer)
     95: {

     99:   if (n < 0) SETERRQ1(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: }