Actual source code: viewers.c

petsc-master 2019-12-10
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: };

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

 14:    Collective on PetscViewers

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

 19:    Level: intermediate

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

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

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

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

 42:    Collective

 44:    Input Parameter:
 45: .   comm - the MPI communicator

 47:    Output Parameter:
 48: .  v - the collection of PetscViewers

 50:    Level: intermediate

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

 54: @*/
 55: PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
 56: {

 60:   PetscNew(v);
 61:   (*v)->n    = 64;
 62:   (*v)->comm = comm;

 64:   PetscCalloc1(64,&(*v)->viewer);
 65:   return(0);
 66: }

 68: /*@C
 69:    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection

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

 73:    Input Parameter:
 74: +   viewers - object created with PetscViewersCreate()
 75: -   n - number of PetscViewer you want

 77:    Output Parameter:
 78: .  viewer - the PetscViewer

 80:    Level: intermediate

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

 84: @*/
 85: PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
 86: {

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

 95:     PetscCalloc1(newn,&v);
 96:     PetscArraycpy(v,viewers->viewer,viewers->n);
 97:     PetscFree(viewers->viewer);

 99:     viewers->viewer = v;
100:   }
101:   if (!viewers->viewer[n]) {
102:     PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
103:   }
104:   *viewer = viewers->viewer[n];
105:   return(0);
106: }