xref: /petsc/src/sys/classes/viewer/interface/viewers.c (revision 27f49a208b01d2e827ab9db411a2d16003fe9262)
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
15 
16    Input Parameters:
17 .  v - the `PetscViewer`s to be destroyed.
18 
19    Level: intermediate
20 
21 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerDestroy()`, `PetscViewers()`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewersCreate()`
22 @*/
23 PetscErrorCode PetscViewersDestroy(PetscViewers *v)
24 {
25   int i;
26 
27   PetscFunctionBegin;
28   if (!*v) PetscFunctionReturn(PETSC_SUCCESS);
29   for (i = 0; i < (*v)->n; i++) PetscCall(PetscViewerDestroy(&(*v)->viewer[i]));
30   PetscCall(PetscFree((*v)->viewer));
31   PetscCall(PetscFree(*v));
32   PetscFunctionReturn(PETSC_SUCCESS);
33 }
34 
35 /*@C
36    PetscViewersCreate - Creates a container to hold a set of `PetscViewer`s. The container is essentially a sparse, growable in length array of `PetscViewer`s
37 
38    Collective
39 
40    Input Parameter:
41 .   comm - the MPI communicator
42 
43    Output Parameter:
44 .  v - the collection of `PetscViewer`s
45 
46    Level: intermediate
47 
48 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewersDestroy()`
49 @*/
50 PetscErrorCode PetscViewersCreate(MPI_Comm comm, PetscViewers *v)
51 {
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(PETSC_SUCCESS);
60 }
61 
62 /*@C
63    PetscViewersGetViewer - Gets a `PetscViewer` from a `PetscViewer` collection
64 
65    Collective if the viewer has not previously be obtained.
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: [](sec_viewers), `PetscViewer`, `PetscViewersCreate()`, `PetscViewersDestroy()`
77 @*/
78 PetscErrorCode PetscViewersGetViewer(PetscViewers viewers, PetscInt n, PetscViewer *viewer)
79 {
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(PETSC_SUCCESS);
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 .seealso: [](sec_viewers), `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 {
121   PetscFunctionBegin;
122   PetscValidBoolPointer(identical, 7);
123   *identical = PETSC_FALSE;
124   if (nmon == mon && nmdestroy == mdestroy) {
125     if (nmctx == mctx) *identical = PETSC_TRUE;
126     else if (nmdestroy == (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy) {
127       PetscViewerAndFormat *old = (PetscViewerAndFormat *)mctx, *newo = (PetscViewerAndFormat *)nmctx;
128       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
129     }
130     if (*identical) {
131       if (mdestroy) PetscCall((*mdestroy)(&nmctx));
132     }
133   }
134   PetscFunctionReturn(PETSC_SUCCESS);
135 }
136