xref: /petsc/src/sys/classes/viewer/interface/viewers.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petscsys.h>
3af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith struct _n_PetscViewers {
65c6c1daeSBarry Smith   MPI_Comm     comm;
75c6c1daeSBarry Smith   PetscViewer *viewer;
85c6c1daeSBarry Smith   int          n;
95c6c1daeSBarry Smith };
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith /*@C
12*811af0c4SBarry Smith    PetscViewersDestroy - Destroys a set of `PetscViewer`s created with `PetscViewersCreate()`.
135c6c1daeSBarry Smith 
14*811af0c4SBarry Smith    Collective on v
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith    Input Parameters:
17*811af0c4SBarry Smith .  v - the `PetscViewer`s to be destroyed.
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith    Level: intermediate
205c6c1daeSBarry Smith 
21*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewersCreate()`
225c6c1daeSBarry Smith @*/
239371c9d4SSatish Balay PetscErrorCode PetscViewersDestroy(PetscViewers *v) {
245c6c1daeSBarry Smith   int i;
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith   PetscFunctionBegin;
275c6c1daeSBarry Smith   if (!*v) PetscFunctionReturn(0);
2848a46eb9SPierre Jolivet   for (i = 0; i < (*v)->n; i++) PetscCall(PetscViewerDestroy(&(*v)->viewer[i]));
299566063dSJacob Faibussowitsch   PetscCall(PetscFree((*v)->viewer));
309566063dSJacob Faibussowitsch   PetscCall(PetscFree(*v));
315c6c1daeSBarry Smith   PetscFunctionReturn(0);
325c6c1daeSBarry Smith }
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith /*@C
35*811af0c4SBarry Smith    PetscViewersCreate - Creates a container to hold a set of `PetscViewer`s. The container is essentially a sparse, growable in length array of `PetscViewer`s
365c6c1daeSBarry Smith 
37d083f849SBarry Smith    Collective
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith    Input Parameter:
405c6c1daeSBarry Smith .   comm - the MPI communicator
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith    Output Parameter:
43*811af0c4SBarry Smith .  v - the collection of `PetscViewer`s
445c6c1daeSBarry Smith 
455c6c1daeSBarry Smith    Level: intermediate
465c6c1daeSBarry Smith 
47*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewerCreate()`, `PetscViewersDestroy()`
485c6c1daeSBarry Smith @*/
499371c9d4SSatish Balay PetscErrorCode PetscViewersCreate(MPI_Comm comm, PetscViewers *v) {
505c6c1daeSBarry Smith   PetscFunctionBegin;
513ca90d2dSJacob Faibussowitsch   PetscValidPointer(v, 2);
529566063dSJacob Faibussowitsch   PetscCall(PetscNew(v));
535c6c1daeSBarry Smith   (*v)->n    = 64;
545c6c1daeSBarry Smith   (*v)->comm = comm;
55a297a907SKarl Rupp 
569566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(64, &(*v)->viewer));
575c6c1daeSBarry Smith   PetscFunctionReturn(0);
585c6c1daeSBarry Smith }
595c6c1daeSBarry Smith 
605c6c1daeSBarry Smith /*@C
61*811af0c4SBarry Smith    PetscViewersGetViewer - Gets a `PetscViewer` from a `PetscViewer` collection
625c6c1daeSBarry Smith 
63*811af0c4SBarry Smith    Not Collective, but the resulting `PetscViewer` will be collective object on viewers
645c6c1daeSBarry Smith 
65d8d19677SJose E. Roman    Input Parameters:
66*811af0c4SBarry Smith +   viewers - object created with `PetscViewersCreate()`
67*811af0c4SBarry Smith -   n - number of `PetscViewer `you want
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith    Output Parameter:
70*811af0c4SBarry Smith .  viewer - the `PetscViewer`
715c6c1daeSBarry Smith 
725c6c1daeSBarry Smith    Level: intermediate
735c6c1daeSBarry Smith 
74*811af0c4SBarry Smith .seealso: `PetscViewer`, `PetscViewersCreate()`, `PetscViewersDestroy()`
755c6c1daeSBarry Smith @*/
769371c9d4SSatish Balay PetscErrorCode PetscViewersGetViewer(PetscViewers viewers, PetscInt n, PetscViewer *viewer) {
775c6c1daeSBarry Smith   PetscFunctionBegin;
783ca90d2dSJacob Faibussowitsch   PetscValidPointer(viewers, 1);
793ca90d2dSJacob Faibussowitsch   PetscValidPointer(viewer, 3);
8008401ef6SPierre Jolivet   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot access using a negative index - %" PetscInt_FMT, n);
815c6c1daeSBarry Smith   if (n >= viewers->n) {
825c6c1daeSBarry Smith     PetscViewer *v;
835c6c1daeSBarry Smith     int          newn = n + 64; /* add 64 new ones at a time */
845c6c1daeSBarry Smith 
859566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(newn, &v));
869566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(v, viewers->viewer, viewers->n));
879566063dSJacob Faibussowitsch     PetscCall(PetscFree(viewers->viewer));
88a297a907SKarl Rupp 
895c6c1daeSBarry Smith     viewers->viewer = v;
905c6c1daeSBarry Smith   }
9148a46eb9SPierre Jolivet   if (!viewers->viewer[n]) PetscCall(PetscViewerCreate(viewers->comm, &viewers->viewer[n]));
925c6c1daeSBarry Smith   *viewer = viewers->viewer[n];
935c6c1daeSBarry Smith   PetscFunctionReturn(0);
945c6c1daeSBarry Smith }
955c6c1daeSBarry Smith 
96a50e088cSMatthew G. Knepley /*
97a50e088cSMatthew G. Knepley   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one
985c6c1daeSBarry Smith 
99a50e088cSMatthew G. Knepley   Not collective
1005c6c1daeSBarry Smith 
101a50e088cSMatthew G. Knepley   Input Parameters:
102a50e088cSMatthew G. Knepley + nmon      - The new monitor
103a50e088cSMatthew G. Knepley . nmctx     - The new monitor context, or NULL
104a50e088cSMatthew G. Knepley . nmdestroy - The new monitor destroy function, or NULL
105a50e088cSMatthew G. Knepley . mon       - The old monitor
106a50e088cSMatthew G. Knepley . mctx      - The old monitor context, or NULL
107a50e088cSMatthew G. Knepley - mdestroy  - The old monitor destroy function, or NULL
1085c6c1daeSBarry Smith 
109a50e088cSMatthew G. Knepley   Output Parameter:
110*811af0c4SBarry Smith . identical - `PETSC_TRUE` if the monitors are the same
1115c6c1daeSBarry Smith 
112a50e088cSMatthew G. Knepley   Level: developer
1135c6c1daeSBarry Smith 
114*811af0c4SBarry Smith .seealso: `DMMonitorSetFromOptions()`, `KSPMonitorSetFromOptions()`, `SNESMonitorSetFromOptions()`
115a50e088cSMatthew G. Knepley */
1169371c9d4SSatish Balay PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical) {
117362febeeSStefano Zampini   PetscFunctionBegin;
1183ca90d2dSJacob Faibussowitsch   PetscValidBoolPointer(identical, 7);
119a50e088cSMatthew G. Knepley   *identical = PETSC_FALSE;
120a50e088cSMatthew G. Knepley   if (nmon == mon && nmdestroy == mdestroy) {
121a50e088cSMatthew G. Knepley     if (nmctx == mctx) *identical = PETSC_TRUE;
122a50e088cSMatthew G. Knepley     else if (nmdestroy == (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy) {
123a50e088cSMatthew G. Knepley       PetscViewerAndFormat *old = (PetscViewerAndFormat *)mctx, *newo = (PetscViewerAndFormat *)nmctx;
124a50e088cSMatthew G. Knepley       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
125a50e088cSMatthew G. Knepley     }
126a50e088cSMatthew G. Knepley     if (*identical) {
12748a46eb9SPierre Jolivet       if (mdestroy) PetscCall((*mdestroy)(&nmctx));
128a50e088cSMatthew G. Knepley     }
129a50e088cSMatthew G. Knepley   }
130a50e088cSMatthew G. Knepley   PetscFunctionReturn(0);
131a50e088cSMatthew G. Knepley }
132