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