xref: /petsc/src/sys/classes/viewer/interface/viewers.c (revision 89669be4d29968dc8d4c19ce1b69194a6a561ea4)
1 
2 #include <petscsys.h>
3 #include <petsc/private/viewerimpl.h>
4 
5 struct _n_PetscViewers {
6   MPI_Comm    comm;
7   PetscViewer *viewer;
8   int         n;
9 };
10 
11 /*@C
12    PetscViewersDestroy - Destroys a set of PetscViewers created with PetscViewersCreate().
13 
14    Collective on PetscViewers
15 
16    Input Parameters:
17 .  v - the PetscViewers to be destroyed.
18 
19    Level: intermediate
20 
21 .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewersCreate()`
22 
23 @*/
24 PetscErrorCode  PetscViewersDestroy(PetscViewers *v)
25 {
26   int            i;
27 
28   PetscFunctionBegin;
29   if (!*v) PetscFunctionReturn(0);
30   for (i=0; i<(*v)->n; i++) {
31     PetscCall(PetscViewerDestroy(&(*v)->viewer[i]));
32   }
33   PetscCall(PetscFree((*v)->viewer));
34   PetscCall(PetscFree(*v));
35   PetscFunctionReturn(0);
36 }
37 
38 /*@C
39    PetscViewersCreate - Creates a container to hold a set of PetscViewers.
40 
41    Collective
42 
43    Input Parameter:
44 .   comm - the MPI communicator
45 
46    Output Parameter:
47 .  v - the collection of PetscViewers
48 
49    Level: intermediate
50 
51 .seealso: `PetscViewerCreate()`, `PetscViewersDestroy()`
52 
53 @*/
54 PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
55 {
56   PetscFunctionBegin;
57   PetscValidPointer(v,2);
58   PetscCall(PetscNew(v));
59   (*v)->n    = 64;
60   (*v)->comm = comm;
61 
62   PetscCall(PetscCalloc1(64,&(*v)->viewer));
63   PetscFunctionReturn(0);
64 }
65 
66 /*@C
67    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection
68 
69    Not Collective, but PetscViewer will be collective object on PetscViewers
70 
71    Input Parameters:
72 +   viewers - object created with PetscViewersCreate()
73 -   n - number of PetscViewer you want
74 
75    Output Parameter:
76 .  viewer - the PetscViewer
77 
78    Level: intermediate
79 
80 .seealso: `PetscViewersCreate()`, `PetscViewersDestroy()`
81 
82 @*/
83 PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
84 {
85   PetscFunctionBegin;
86   PetscValidPointer(viewers,1);
87   PetscValidPointer(viewer,3);
88   PetscCheck(n >= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %" PetscInt_FMT,n);
89   if (n >= viewers->n) {
90     PetscViewer *v;
91     int         newn = n + 64; /* add 64 new ones at a time */
92 
93     PetscCall(PetscCalloc1(newn,&v));
94     PetscCall(PetscArraycpy(v,viewers->viewer,viewers->n));
95     PetscCall(PetscFree(viewers->viewer));
96 
97     viewers->viewer = v;
98   }
99   if (!viewers->viewer[n]) {
100     PetscCall(PetscViewerCreate(viewers->comm,&viewers->viewer[n]));
101   }
102   *viewer = viewers->viewer[n];
103   PetscFunctionReturn(0);
104 }
105 
106 /*
107   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one
108 
109   Not collective
110 
111   Input Parameters:
112 + nmon      - The new monitor
113 . nmctx     - The new monitor context, or NULL
114 . nmdestroy - The new monitor destroy function, or NULL
115 . mon       - The old monitor
116 . mctx      - The old monitor context, or NULL
117 - mdestroy  - The old monitor destroy function, or NULL
118 
119   Output Parameter:
120 . identical - PETSC_TRUE if the monitors are the same
121 
122   Level: developer
123 
124 .seealsp: DMMonitorSetFromOptions(), KSPMonitorSetFromOptions(), SNESMonitorSetFromOptions()
125 */
126 PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
127 {
128   PetscFunctionBegin;
129   PetscValidBoolPointer(identical,7);
130   *identical = PETSC_FALSE;
131   if (nmon == mon && nmdestroy == mdestroy) {
132     if (nmctx == mctx) *identical = PETSC_TRUE;
133     else if (nmdestroy == (PetscErrorCode (*)(void**)) PetscViewerAndFormatDestroy) {
134       PetscViewerAndFormat *old = (PetscViewerAndFormat*)mctx, *newo = (PetscViewerAndFormat*)nmctx;
135       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
136     }
137     if (*identical) {
138       if (mdestroy) {
139         PetscCall((*mdestroy)(&nmctx));
140       }
141     }
142   }
143   PetscFunctionReturn(0);
144 }
145