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