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