xref: /petsc/src/sys/classes/viewer/interface/viewers.c (revision 362febeeeb69b91ebadcb4b2dc0a22cb6dfc4097)
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
125c6c1daeSBarry Smith    PetscViewersDestroy - Destroys a set of PetscViewers created with PetscViewersCreate().
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith    Collective on PetscViewers
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith    Input Parameters:
175c6c1daeSBarry Smith .  v - the PetscViewers to be destroyed.
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith    Level: intermediate
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewersCreate()
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith @*/
245c6c1daeSBarry Smith PetscErrorCode  PetscViewersDestroy(PetscViewers *v)
255c6c1daeSBarry Smith {
265c6c1daeSBarry Smith   int            i;
275c6c1daeSBarry Smith   PetscErrorCode ierr;
285c6c1daeSBarry Smith 
295c6c1daeSBarry Smith   PetscFunctionBegin;
305c6c1daeSBarry Smith   if (!*v) PetscFunctionReturn(0);
315c6c1daeSBarry Smith   for (i=0; i<(*v)->n; i++) {
325c6c1daeSBarry Smith     ierr = PetscViewerDestroy(&(*v)->viewer[i]);CHKERRQ(ierr);
335c6c1daeSBarry Smith   }
345c6c1daeSBarry Smith   ierr = PetscFree((*v)->viewer);CHKERRQ(ierr);
355c6c1daeSBarry Smith   ierr = PetscFree(*v);CHKERRQ(ierr);
365c6c1daeSBarry Smith   PetscFunctionReturn(0);
375c6c1daeSBarry Smith }
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith /*@C
405c6c1daeSBarry Smith    PetscViewersCreate - Creates a container to hold a set of PetscViewers.
415c6c1daeSBarry Smith 
42d083f849SBarry Smith    Collective
435c6c1daeSBarry Smith 
445c6c1daeSBarry Smith    Input Parameter:
455c6c1daeSBarry Smith .   comm - the MPI communicator
465c6c1daeSBarry Smith 
475c6c1daeSBarry Smith    Output Parameter:
485c6c1daeSBarry Smith .  v - the collection of PetscViewers
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith    Level: intermediate
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewersDestroy()
535c6c1daeSBarry Smith 
545c6c1daeSBarry Smith @*/
555c6c1daeSBarry Smith PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
565c6c1daeSBarry Smith {
575c6c1daeSBarry Smith   PetscErrorCode ierr;
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith   PetscFunctionBegin;
60b00a9115SJed Brown   ierr       = PetscNew(v);CHKERRQ(ierr);
615c6c1daeSBarry Smith   (*v)->n    = 64;
625c6c1daeSBarry Smith   (*v)->comm = comm;
63a297a907SKarl Rupp 
641795a4d1SJed Brown   ierr = PetscCalloc1(64,&(*v)->viewer);CHKERRQ(ierr);
655c6c1daeSBarry Smith   PetscFunctionReturn(0);
665c6c1daeSBarry Smith }
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith /*@C
695c6c1daeSBarry Smith    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith    Not Collective, but PetscViewer will be collective object on PetscViewers
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith    Input Parameter:
745c6c1daeSBarry Smith +   viewers - object created with PetscViewersCreate()
755c6c1daeSBarry Smith -   n - number of PetscViewer you want
765c6c1daeSBarry Smith 
775c6c1daeSBarry Smith    Output Parameter:
785c6c1daeSBarry Smith .  viewer - the PetscViewer
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith    Level: intermediate
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith .seealso: PetscViewersCreate(), PetscViewersDestroy()
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith @*/
855c6c1daeSBarry Smith PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
865c6c1daeSBarry Smith {
875c6c1daeSBarry Smith   PetscErrorCode ierr;
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith   PetscFunctionBegin;
905c6c1daeSBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
915c6c1daeSBarry Smith   if (n >= viewers->n) {
925c6c1daeSBarry Smith     PetscViewer *v;
935c6c1daeSBarry Smith     int         newn = n + 64; /* add 64 new ones at a time */
945c6c1daeSBarry Smith 
951795a4d1SJed Brown     ierr = PetscCalloc1(newn,&v);CHKERRQ(ierr);
96580bdb30SBarry Smith     ierr = PetscArraycpy(v,viewers->viewer,viewers->n);CHKERRQ(ierr);
975c6c1daeSBarry Smith     ierr = PetscFree(viewers->viewer);CHKERRQ(ierr);
98a297a907SKarl Rupp 
995c6c1daeSBarry Smith     viewers->viewer = v;
1005c6c1daeSBarry Smith   }
1015c6c1daeSBarry Smith   if (!viewers->viewer[n]) {
1025c6c1daeSBarry Smith     ierr = PetscViewerCreate(viewers->comm,&viewers->viewer[n]);CHKERRQ(ierr);
1035c6c1daeSBarry Smith   }
1045c6c1daeSBarry Smith   *viewer = viewers->viewer[n];
1055c6c1daeSBarry Smith   PetscFunctionReturn(0);
1065c6c1daeSBarry Smith }
1075c6c1daeSBarry Smith 
108a50e088cSMatthew G. Knepley /*
109a50e088cSMatthew G. Knepley   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one
1105c6c1daeSBarry Smith 
111a50e088cSMatthew G. Knepley   Not collective
1125c6c1daeSBarry Smith 
113a50e088cSMatthew G. Knepley   Input Parameters:
114a50e088cSMatthew G. Knepley + nmon      - The new monitor
115a50e088cSMatthew G. Knepley . nmctx     - The new monitor context, or NULL
116a50e088cSMatthew G. Knepley . nmdestroy - The new monitor destroy function, or NULL
117a50e088cSMatthew G. Knepley . mon       - The old monitor
118a50e088cSMatthew G. Knepley . mctx      - The old monitor context, or NULL
119a50e088cSMatthew G. Knepley - mdestroy  - The old monitor destroy function, or NULL
1205c6c1daeSBarry Smith 
121a50e088cSMatthew G. Knepley   Output Parameter:
122a50e088cSMatthew G. Knepley . identical - PETSC_TRUE if the monitors are the same
1235c6c1daeSBarry Smith 
124a50e088cSMatthew G. Knepley   Level: developer
1255c6c1daeSBarry Smith 
126a50e088cSMatthew G. Knepley .seealsp: DMMonitorSetFromOptions(), KSPMonitorSetFromOptions(), SNESMonitorSetFromOptions()
127a50e088cSMatthew G. Knepley */
128a50e088cSMatthew G. Knepley PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
129a50e088cSMatthew G. Knepley {
130*362febeeSStefano Zampini   PetscFunctionBegin;
131a50e088cSMatthew G. Knepley   *identical = PETSC_FALSE;
132a50e088cSMatthew G. Knepley   if (nmon == mon && nmdestroy == mdestroy) {
133a50e088cSMatthew G. Knepley     if (nmctx == mctx) *identical = PETSC_TRUE;
134a50e088cSMatthew G. Knepley     else if (nmdestroy == (PetscErrorCode (*)(void**)) PetscViewerAndFormatDestroy) {
135a50e088cSMatthew G. Knepley       PetscViewerAndFormat *old = (PetscViewerAndFormat*)mctx, *newo = (PetscViewerAndFormat*)nmctx;
136a50e088cSMatthew G. Knepley       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
137a50e088cSMatthew G. Knepley     }
138a50e088cSMatthew G. Knepley     if (*identical) {
139a50e088cSMatthew G. Knepley       if (mdestroy) {
140a50e088cSMatthew G. Knepley         PetscErrorCode ierr;
141a50e088cSMatthew G. Knepley         ierr = (*mdestroy)(&nmctx);CHKERRQ(ierr);
142a50e088cSMatthew G. Knepley       }
143a50e088cSMatthew G. Knepley     }
144a50e088cSMatthew G. Knepley   }
145a50e088cSMatthew G. Knepley   PetscFunctionReturn(0);
146a50e088cSMatthew G. Knepley }
147