Actual source code: viewers.c
petsc-3.7.1 2016-05-15
Report Typos and Errors

      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(v);
     67:   (*v)->n    = 64;
     68:   (*v)->comm = comm;

     70:   PetscCalloc1(64,&(*v)->viewer);
     71:   return(0);
     72: }

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

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

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

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

     88:    Level: intermediate

     90:    Concepts: PetscViewer^array of

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

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

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

    105:     PetscCalloc1(newn,&v);
    106:     PetscMemcpy(v,viewers->viewer,viewers->n*sizeof(PetscViewer));
    107:     PetscFree(viewers->viewer);

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