Actual source code: viewers.c
petsc-3.10.5 2019-03-28
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 on MPI_Comm
44: Input Parameter:
45: . comm - the MPI communicator
47: Output Parameter:
48: . v - the collection of PetscViewers
50: Level: intermediate
52: Concepts: PetscViewer^array of
54: .seealso: PetscViewerCreate(), PetscViewersDestroy()
56: @*/
57: PetscErrorCode PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
58: {
62: PetscNew(v);
63: (*v)->n = 64;
64: (*v)->comm = comm;
66: PetscCalloc1(64,&(*v)->viewer);
67: return(0);
68: }
70: /*@C
71: PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection
73: Not Collective, but PetscViewer will be collective object on PetscViewers
75: Input Parameter:
76: + viewers - object created with PetscViewersCreate()
77: - n - number of PetscViewer you want
79: Output Parameter:
80: . viewer - the PetscViewer
82: Level: intermediate
84: Concepts: PetscViewer^array of
86: .seealso: PetscViewersCreate(), PetscViewersDestroy()
88: @*/
89: PetscErrorCode PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
90: {
94: if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
95: if (n >= viewers->n) {
96: PetscViewer *v;
97: int newn = n + 64; /* add 64 new ones at a time */
99: PetscCalloc1(newn,&v);
100: PetscMemcpy(v,viewers->viewer,viewers->n*sizeof(PetscViewer));
101: PetscFree(viewers->viewer);
103: viewers->viewer = v;
104: }
105: if (!viewers->viewer[n]) {
106: PetscViewerCreate(viewers->comm,&viewers->viewer[n]);
107: }
108: *viewer = viewers->viewer[n];
109: return(0);
110: }