xref: /petsc/src/sys/classes/viewer/impls/draw/drawv.c (revision 51b144c619aff302b570817d6f78637b8418d403)
15c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/draw/vdraw.h> /*I "petscdraw.h" I*/
2665c2dedSJed Brown #include <petscviewer.h>                                /*I "petscviewer.h" I*/
35c6c1daeSBarry Smith 
PetscViewerDestroy_Draw(PetscViewer v)4d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_Draw(PetscViewer v)
5d71ae5a4SJacob Faibussowitsch {
65c6c1daeSBarry Smith   PetscInt          i;
75c6c1daeSBarry Smith   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)v->data;
85c6c1daeSBarry Smith 
95c6c1daeSBarry Smith   PetscFunctionBegin;
1028b400f6SJacob Faibussowitsch   PetscCheck(!vdraw->singleton_made, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Destroying PetscViewer without first restoring singleton");
115c6c1daeSBarry Smith   for (i = 0; i < vdraw->draw_max; i++) {
129566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisDestroy(&vdraw->drawaxis[i]));
139566063dSJacob Faibussowitsch     PetscCall(PetscDrawLGDestroy(&vdraw->drawlg[i]));
149566063dSJacob Faibussowitsch     PetscCall(PetscDrawDestroy(&vdraw->draw[i]));
155c6c1daeSBarry Smith   }
169566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->display));
179566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->title));
189566063dSJacob Faibussowitsch   PetscCall(PetscFree3(vdraw->draw, vdraw->drawlg, vdraw->drawaxis));
199566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->bounds));
209566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->drawtype));
219566063dSJacob Faibussowitsch   PetscCall(PetscFree(v->data));
223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
235c6c1daeSBarry Smith }
245c6c1daeSBarry Smith 
PetscViewerFlush_Draw(PetscViewer v)25d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFlush_Draw(PetscViewer v)
26d71ae5a4SJacob Faibussowitsch {
275c6c1daeSBarry Smith   PetscInt          i;
285c6c1daeSBarry Smith   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)v->data;
295c6c1daeSBarry Smith 
305c6c1daeSBarry Smith   PetscFunctionBegin;
315c6c1daeSBarry Smith   for (i = 0; i < vdraw->draw_max; i++) {
329566063dSJacob Faibussowitsch     if (vdraw->draw[i]) PetscCall(PetscDrawFlush(vdraw->draw[i]));
335c6c1daeSBarry Smith   }
343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
355c6c1daeSBarry Smith }
365c6c1daeSBarry Smith 
375d83a8b1SBarry Smith /*@
38c410d8ccSBarry Smith   PetscViewerDrawBaseAdd - add to the base integer that is added to the `windownumber` passed to `PetscViewerDrawGetDraw()`
395c6c1daeSBarry Smith 
40c3339decSBarry Smith   Logically Collective
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith   Input Parameters:
43811af0c4SBarry Smith + viewer       - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
445c6c1daeSBarry Smith - windownumber - how much to add to the base
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith   Level: developer
475c6c1daeSBarry Smith 
48811af0c4SBarry Smith   Note:
49811af0c4SBarry Smith   A `PETSCVIEWERDRAW` may have multiple `PetscDraw` subwindows, this increases the number of the subwindow that is returned with `PetscViewerDrawGetDraw()`
50811af0c4SBarry Smith 
51d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PetscViewerDrawBaseSet()`
525c6c1daeSBarry Smith @*/
PetscViewerDrawBaseAdd(PetscViewer viewer,PetscInt windownumber)53d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawBaseAdd(PetscViewer viewer, PetscInt windownumber)
54d71ae5a4SJacob Faibussowitsch {
55e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
565c6c1daeSBarry Smith   PetscBool         isdraw;
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith   PetscFunctionBegin;
595c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
60e5ab1681SLisandro Dalcin   PetscValidLogicalCollectiveInt(viewer, windownumber, 2);
619566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
6228b400f6SJacob Faibussowitsch   PetscCheck(isdraw, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
63e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)viewer->data;
64e5ab1681SLisandro Dalcin 
65cc73adaaSBarry Smith   PetscCheck(windownumber + vdraw->draw_base >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Resulting base %" PetscInt_FMT " cannot be negative", windownumber + vdraw->draw_base);
665c6c1daeSBarry Smith   vdraw->draw_base += windownumber;
673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
685c6c1daeSBarry Smith }
695c6c1daeSBarry Smith 
705d83a8b1SBarry Smith /*@
71c410d8ccSBarry Smith   PetscViewerDrawBaseSet - sets the base integer that is added to the `windownumber` passed to `PetscViewerDrawGetDraw()`
725c6c1daeSBarry Smith 
73c3339decSBarry Smith   Logically Collective
745c6c1daeSBarry Smith 
755c6c1daeSBarry Smith   Input Parameters:
76811af0c4SBarry Smith + viewer       - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
775c6c1daeSBarry Smith - windownumber - value to set the base
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith   Level: developer
805c6c1daeSBarry Smith 
81811af0c4SBarry Smith   Note:
82811af0c4SBarry Smith   A `PETSCVIEWERDRAW` may have multiple `PetscDraw` subwindows, this increases the number of the subwindow that is returned with `PetscViewerDrawGetDraw()`
83811af0c4SBarry Smith 
84d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PetscViewerDrawBaseAdd()`
855c6c1daeSBarry Smith @*/
PetscViewerDrawBaseSet(PetscViewer viewer,PetscInt windownumber)86d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawBaseSet(PetscViewer viewer, PetscInt windownumber)
87d71ae5a4SJacob Faibussowitsch {
88e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
895c6c1daeSBarry Smith   PetscBool         isdraw;
905c6c1daeSBarry Smith 
915c6c1daeSBarry Smith   PetscFunctionBegin;
925c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
93e5ab1681SLisandro Dalcin   PetscValidLogicalCollectiveInt(viewer, windownumber, 2);
949566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
9528b400f6SJacob Faibussowitsch   PetscCheck(isdraw, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
96e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)viewer->data;
97e5ab1681SLisandro Dalcin 
9808401ef6SPierre Jolivet   PetscCheck(windownumber >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Resulting base %" PetscInt_FMT " cannot be negative", windownumber);
995c6c1daeSBarry Smith   vdraw->draw_base = windownumber;
1003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1015c6c1daeSBarry Smith }
1025c6c1daeSBarry Smith 
PetscViewerDrawResize(PetscViewer v,int w,int h)103d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawResize(PetscViewer v, int w, int h)
104d71ae5a4SJacob Faibussowitsch {
105e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
106e5ab1681SLisandro Dalcin   PetscBool         isdraw;
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith   PetscFunctionBegin;
109e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
1109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
1113ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
112e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)v->data;
113e5ab1681SLisandro Dalcin 
114e5ab1681SLisandro Dalcin   if (w >= 1) vdraw->w = w;
115e5ab1681SLisandro Dalcin   if (h >= 1) vdraw->h = h;
1163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1175c6c1daeSBarry Smith }
1185c6c1daeSBarry Smith 
PetscViewerDrawSetInfo(PetscViewer v,const char display[],const char title[],int x,int y,int w,int h)119d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawSetInfo(PetscViewer v, const char display[], const char title[], int x, int y, int w, int h)
120d71ae5a4SJacob Faibussowitsch {
121e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
122e5ab1681SLisandro Dalcin   PetscBool         isdraw;
1235c6c1daeSBarry Smith 
1245c6c1daeSBarry Smith   PetscFunctionBegin;
125e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
1269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
1273ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
128e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)v->data;
129e5ab1681SLisandro Dalcin 
1309566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(display, &vdraw->display));
1319566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(title, &vdraw->title));
132e5ab1681SLisandro Dalcin   if (w >= 1) vdraw->w = w;
133e5ab1681SLisandro Dalcin   if (h >= 1) vdraw->h = h;
1343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1355c6c1daeSBarry Smith }
1365c6c1daeSBarry Smith 
PetscViewerDrawSetTitle(PetscViewer v,const char title[])137d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawSetTitle(PetscViewer v, const char title[])
138d71ae5a4SJacob Faibussowitsch {
139f55236e4SLisandro Dalcin   PetscViewer_Draw *vdraw;
140f55236e4SLisandro Dalcin   PetscBool         isdraw;
141f55236e4SLisandro Dalcin 
142f55236e4SLisandro Dalcin   PetscFunctionBegin;
143f55236e4SLisandro Dalcin   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
1449566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
1453ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
146f55236e4SLisandro Dalcin   vdraw = (PetscViewer_Draw *)v->data;
147f55236e4SLisandro Dalcin 
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->title));
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(title, &vdraw->title));
1503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151f55236e4SLisandro Dalcin }
152f55236e4SLisandro Dalcin 
PetscViewerDrawGetTitle(PetscViewer v,const char * title[])153d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawGetTitle(PetscViewer v, const char *title[])
154d71ae5a4SJacob Faibussowitsch {
155f55236e4SLisandro Dalcin   PetscViewer_Draw *vdraw;
156f55236e4SLisandro Dalcin   PetscBool         isdraw;
157f55236e4SLisandro Dalcin 
158f55236e4SLisandro Dalcin   PetscFunctionBegin;
159f55236e4SLisandro Dalcin   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
1609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
16128b400f6SJacob Faibussowitsch   PetscCheck(isdraw, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
162f55236e4SLisandro Dalcin   vdraw = (PetscViewer_Draw *)v->data;
163f55236e4SLisandro Dalcin 
164f55236e4SLisandro Dalcin   *title = vdraw->title;
1653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
166f55236e4SLisandro Dalcin }
167f55236e4SLisandro Dalcin 
168cc4c1da9SBarry Smith /*@
16910450e9eSJacob Faibussowitsch   PetscViewerDrawOpen - Opens a `PetscDraw` window for use as a `PetscViewer` with type
17010450e9eSJacob Faibussowitsch   `PETSCVIEWERDRAW`.
1715c6c1daeSBarry Smith 
172d083f849SBarry Smith   Collective
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   Input Parameters:
1755c6c1daeSBarry Smith + comm    - communicator that will share window
1763f423023SBarry Smith . display - the X display on which to open, or `NULL` for the local machine
1773f423023SBarry Smith . title   - the title to put in the title bar, or `NULL` for no title
178aaa8cc7dSPierre Jolivet . x       - horizontal screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
1792fe279fdSBarry Smith . y       - vertical screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
1802fe279fdSBarry Smith . w       - window width in pixels, or may use `PETSC_DECIDE` or `PETSC_DRAW_FULL_SIZE`, `PETSC_DRAW_HALF_SIZE`,`PETSC_DRAW_THIRD_SIZE`, `PETSC_DRAW_QUARTER_SIZE`
1812fe279fdSBarry Smith - h       - window height in pixels, or may use `PETSC_DECIDE` or `PETSC_DRAW_FULL_SIZE`, `PETSC_DRAW_HALF_SIZE`,`PETSC_DRAW_THIRD_SIZE`, `PETSC_DRAW_QUARTER_SIZE`
1825c6c1daeSBarry Smith 
183f899ff85SJose E. Roman   Output Parameter:
184811af0c4SBarry Smith . viewer - the `PetscViewer`
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith   Options Database Keys:
18710699b91SBarry Smith + -draw_type          - use x or null
1885c6c1daeSBarry Smith . -nox                - Disables all x-windows output
1895c6c1daeSBarry Smith . -display <name>     - Specifies name of machine for the X display
1905c6c1daeSBarry Smith . -geometry <x,y,w,h> - allows setting the window location and size
1915c6c1daeSBarry Smith - -draw_pause <pause> - Sets time (in seconds) that the
192feaf08eaSBarry Smith                         program pauses after `PetscDrawPause()` has been called
1935c6c1daeSBarry Smith                         (0 is default, -1 implies until user input).
1945c6c1daeSBarry Smith 
1955c6c1daeSBarry Smith   Level: beginner
1965c6c1daeSBarry Smith 
19710450e9eSJacob Faibussowitsch   Notes:
19810450e9eSJacob Faibussowitsch   If you want to do graphics in this window, you must call `PetscViewerDrawGetDraw()` and
19910450e9eSJacob Faibussowitsch   perform the graphics on the `PetscDraw` object.
20010450e9eSJacob Faibussowitsch 
20110450e9eSJacob Faibussowitsch   Format options include\:
20210450e9eSJacob Faibussowitsch + `PETSC_VIEWER_DRAW_BASIC` - displays with basic format
20310450e9eSJacob Faibussowitsch - `PETSC_VIEWER_DRAW_LG`    - displays using a line graph
20410450e9eSJacob Faibussowitsch 
205feaf08eaSBarry Smith   Fortran Note:
2065c6c1daeSBarry Smith   Whenever indicating null character data in a Fortran code,
207feaf08eaSBarry Smith   `PETSC_NULL_CHARACTER` must be employed. Thus, `PETSC_NULL_CHARACTER` can be
208feaf08eaSBarry Smith   used for the `display` and `title` input parameters.
2095c6c1daeSBarry Smith 
210d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscDrawCreate()`, `PetscViewerDestroy()`, `PetscViewerDrawGetDraw()`, `PetscViewerCreate()`, `PETSC_VIEWER_DRAW_`,
211db781477SPatrick Sanan           `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_DRAW_SELF`
2125c6c1daeSBarry Smith @*/
PetscViewerDrawOpen(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscViewer * viewer)213d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawOpen(MPI_Comm comm, const char display[], const char title[], int x, int y, int w, int h, PetscViewer *viewer)
214d71ae5a4SJacob Faibussowitsch {
2155c6c1daeSBarry Smith   PetscFunctionBegin;
2169566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, viewer));
2179566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERDRAW));
2189566063dSJacob Faibussowitsch   PetscCall(PetscViewerDrawSetInfo(*viewer, display, title, x, y, w, h));
2193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2205c6c1daeSBarry Smith }
2215c6c1daeSBarry Smith 
222a9db196aSBarry Smith #include <petsc/private/drawimpl.h>
223a9db196aSBarry Smith 
PetscViewerGetSubViewer_Draw(PetscViewer viewer,MPI_Comm comm,PetscViewer * sviewer)22434e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerGetSubViewer_Draw(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer)
225d71ae5a4SJacob Faibussowitsch {
2265c6c1daeSBarry Smith   PetscMPIInt       rank;
2275c6c1daeSBarry Smith   PetscInt          i;
228c6228bbaSLisandro Dalcin   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data, *svdraw;
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith   PetscFunctionBegin;
23128b400f6SJacob Faibussowitsch   PetscCheck(!vdraw->singleton_made, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Trying to get SubViewer without first restoring previous");
2325c6c1daeSBarry Smith   /* only processor zero can use the PetscViewer draw singleton */
2339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
234dd400576SPatrick Sanan   if (rank == 0) {
235e5afcf28SBarry Smith     PetscMPIInt flg;
236a9db196aSBarry Smith     PetscDraw   draw, sdraw;
237e5afcf28SBarry Smith 
2389566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, comm, &flg));
239cc73adaaSBarry Smith     PetscCheck(flg == MPI_IDENT || flg == MPI_CONGRUENT, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscViewerGetSubViewer() for PETSCVIEWERDRAW requires a singleton MPI_Comm");
2409566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, sviewer));
2419566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(*sviewer, PETSCVIEWERDRAW));
242c6228bbaSLisandro Dalcin     svdraw             = (PetscViewer_Draw *)(*sviewer)->data;
243c6228bbaSLisandro Dalcin     (*sviewer)->format = viewer->format;
244c6228bbaSLisandro Dalcin     for (i = 0; i < vdraw->draw_max; i++) { /* XXX this is wrong if svdraw->draw_max (initially 5) < vdraw->draw_max */
2459566063dSJacob Faibussowitsch       if (vdraw->draw[i]) PetscCall(PetscDrawGetSingleton(vdraw->draw[i], &svdraw->draw[i]));
2465c6c1daeSBarry Smith     }
2479566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2489566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(*sviewer, 0, &sdraw));
249a9db196aSBarry Smith     if (draw->savefilename) {
2509566063dSJacob Faibussowitsch       PetscCall(PetscDrawSetSave(sdraw, draw->savefilename));
251a9db196aSBarry Smith       sdraw->savefilecount  = draw->savefilecount;
252a9db196aSBarry Smith       sdraw->savesinglefile = draw->savesinglefile;
253a9db196aSBarry Smith       sdraw->savemoviefps   = draw->savemoviefps;
254a9db196aSBarry Smith       sdraw->saveonclear    = draw->saveonclear;
255a9db196aSBarry Smith       sdraw->saveonflush    = draw->saveonflush;
256a9db196aSBarry Smith     }
2579566063dSJacob Faibussowitsch     if (draw->savefinalfilename) PetscCall(PetscDrawSetSaveFinalImage(sdraw, draw->savefinalfilename));
258a9db196aSBarry Smith   } else {
259a9db196aSBarry Smith     PetscDraw draw;
2609566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2615c6c1daeSBarry Smith   }
2625c6c1daeSBarry Smith   vdraw->singleton_made = PETSC_TRUE;
2633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2645c6c1daeSBarry Smith }
2655c6c1daeSBarry Smith 
PetscViewerRestoreSubViewer_Draw(PetscViewer viewer,MPI_Comm comm,PetscViewer * sviewer)26634e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerRestoreSubViewer_Draw(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer)
267d71ae5a4SJacob Faibussowitsch {
2685c6c1daeSBarry Smith   PetscMPIInt       rank;
2695c6c1daeSBarry Smith   PetscInt          i;
270c6228bbaSLisandro Dalcin   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data, *svdraw;
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith   PetscFunctionBegin;
27328b400f6SJacob Faibussowitsch   PetscCheck(vdraw->singleton_made, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Trying to restore a singleton that was not gotten");
2749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
275dd400576SPatrick Sanan   if (rank == 0) {
276a9db196aSBarry Smith     PetscDraw draw, sdraw;
277a9db196aSBarry Smith 
2789566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2799566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(*sviewer, 0, &sdraw));
280a9db196aSBarry Smith     if (draw->savefilename) {
281a9db196aSBarry Smith       draw->savefilecount = sdraw->savefilecount;
2829566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Bcast(&draw->savefilecount, 1, MPIU_INT, 0, PetscObjectComm((PetscObject)draw)));
283a9db196aSBarry Smith     }
284c6228bbaSLisandro Dalcin     svdraw = (PetscViewer_Draw *)(*sviewer)->data;
2855c6c1daeSBarry Smith     for (i = 0; i < vdraw->draw_max; i++) {
28648a46eb9SPierre Jolivet       if (vdraw->draw[i] && svdraw->draw[i]) PetscCall(PetscDrawRestoreSingleton(vdraw->draw[i], &svdraw->draw[i]));
2875c6c1daeSBarry Smith     }
2889566063dSJacob Faibussowitsch     PetscCall(PetscFree3(svdraw->draw, svdraw->drawlg, svdraw->drawaxis));
2899566063dSJacob Faibussowitsch     PetscCall(PetscFree((*sviewer)->data));
2909566063dSJacob Faibussowitsch     PetscCall(PetscHeaderDestroy(sviewer));
291a9db196aSBarry Smith   } else {
292a9db196aSBarry Smith     PetscDraw draw;
293a9db196aSBarry Smith 
2949566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
29548a46eb9SPierre Jolivet     if (draw->savefilename) PetscCallMPI(MPI_Bcast(&draw->savefilecount, 1, MPIU_INT, 0, PetscObjectComm((PetscObject)draw)));
296a9db196aSBarry Smith   }
297a9db196aSBarry Smith 
2985c6c1daeSBarry Smith   vdraw->singleton_made = PETSC_FALSE;
2993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3005c6c1daeSBarry Smith }
3015c6c1daeSBarry Smith 
PetscViewerSetFromOptions_Draw(PetscViewer v,PetscOptionItems PetscOptionsObject)302ce78bad3SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_Draw(PetscViewer v, PetscOptionItems PetscOptionsObject)
303d71ae5a4SJacob Faibussowitsch {
304e9457bf7SBarry Smith   PetscReal bounds[16];
305e9457bf7SBarry Smith   PetscInt  nbounds = 16;
306e9457bf7SBarry Smith   PetscBool flg;
307e9457bf7SBarry Smith 
308e9457bf7SBarry Smith   PetscFunctionBegin;
309d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Draw PetscViewer Options");
3109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsRealArray("-draw_bounds", "Bounds to put on plots axis", "PetscViewerDrawSetBounds", bounds, &nbounds, &flg));
31148a46eb9SPierre Jolivet   if (flg) PetscCall(PetscViewerDrawSetBounds(v, nbounds / 2, bounds));
312d0609cedSBarry Smith   PetscOptionsHeadEnd();
3133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
314e9457bf7SBarry Smith }
315e9457bf7SBarry Smith 
PetscViewerView_Draw(PetscViewer viewer,PetscViewer v)31634e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerView_Draw(PetscViewer viewer, PetscViewer v)
317d71ae5a4SJacob Faibussowitsch {
3180076e027SBarry Smith   PetscDraw         draw;
3190076e027SBarry Smith   PetscInt          i;
3200076e027SBarry Smith   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data;
3219f196a02SMartin Diehl   PetscBool         isascii;
3220076e027SBarry Smith 
3230076e027SBarry Smith   PetscFunctionBegin;
3249f196a02SMartin Diehl   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERASCII, &isascii));
3259f196a02SMartin Diehl   if (isascii) PetscCall(PetscViewerASCIIPrintf(v, "Draw viewer is of type %s\n", vdraw->drawtype));
3260076e027SBarry Smith   /*  If the PetscViewer has just been created then no vdraw->draw yet
3270076e027SBarry Smith       exists so this will not actually call the viewer on any draws. */
3280076e027SBarry Smith   for (i = 0; i < vdraw->draw_base; i++) {
3290076e027SBarry Smith     if (vdraw->draw[i]) {
3309566063dSJacob Faibussowitsch       PetscCall(PetscViewerDrawGetDraw(viewer, i, &draw));
3319566063dSJacob Faibussowitsch       PetscCall(PetscDrawView(draw, v));
3320076e027SBarry Smith     }
3330076e027SBarry Smith   }
3343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3350076e027SBarry Smith }
3360076e027SBarry Smith 
3378556b5ebSBarry Smith /*MC
3388556b5ebSBarry Smith    PETSCVIEWERDRAW - A viewer that generates graphics, either to the screen or a file
3398556b5ebSBarry Smith 
340811af0c4SBarry Smith   Level: beginner
341811af0c4SBarry Smith 
342d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PETSC_VIEWER_DRAW_()`, `PETSC_VIEWER_DRAW_SELF`, `PETSC_VIEWER_DRAW_WORLD`,
343db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`,
344db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
345db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
3468556b5ebSBarry Smith M*/
PetscViewerCreate_Draw(PetscViewer viewer)347d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscViewerCreate_Draw(PetscViewer viewer)
348d71ae5a4SJacob Faibussowitsch {
3495c6c1daeSBarry Smith   PetscViewer_Draw *vdraw;
3505c6c1daeSBarry Smith 
3515c6c1daeSBarry Smith   PetscFunctionBegin;
3524dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&vdraw));
3535c6c1daeSBarry Smith   viewer->data = (void *)vdraw;
3545c6c1daeSBarry Smith 
3555c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_Draw;
3560076e027SBarry Smith   viewer->ops->view             = PetscViewerView_Draw;
3575c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_Draw;
358e9457bf7SBarry Smith   viewer->ops->setfromoptions   = PetscViewerSetFromOptions_Draw;
359559f443fSBarry Smith   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_Draw;
360559f443fSBarry Smith   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_Draw;
3615c6c1daeSBarry Smith 
3625c6c1daeSBarry Smith   /* these are created on the fly if requested */
3635c6c1daeSBarry Smith   vdraw->draw_max  = 5;
3645c6c1daeSBarry Smith   vdraw->draw_base = 0;
365ccad63c3SBarry Smith   vdraw->w         = PETSC_DECIDE;
366ccad63c3SBarry Smith   vdraw->h         = PETSC_DECIDE;
367a297a907SKarl Rupp 
3689566063dSJacob Faibussowitsch   PetscCall(PetscCalloc3(vdraw->draw_max, &vdraw->draw, vdraw->draw_max, &vdraw->drawlg, vdraw->draw_max, &vdraw->drawaxis));
3695c6c1daeSBarry Smith   vdraw->singleton_made = PETSC_FALSE;
3703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3715c6c1daeSBarry Smith }
3725c6c1daeSBarry Smith 
3735c6c1daeSBarry Smith /*@
374811af0c4SBarry Smith   PetscViewerDrawClear - Clears a `PetscDraw` graphic associated with a `PetscViewer`.
3755c6c1daeSBarry Smith 
3765c6c1daeSBarry Smith   Not Collective
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith   Input Parameter:
379811af0c4SBarry Smith . viewer - the `PetscViewer`
3805c6c1daeSBarry Smith 
3815c6c1daeSBarry Smith   Level: intermediate
3825c6c1daeSBarry Smith 
383d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`,
3845c6c1daeSBarry Smith @*/
PetscViewerDrawClear(PetscViewer viewer)385d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawClear(PetscViewer viewer)
386d71ae5a4SJacob Faibussowitsch {
3875c6c1daeSBarry Smith   PetscViewer_Draw *vdraw;
388e5ab1681SLisandro Dalcin   PetscBool         isdraw;
389e5ab1681SLisandro Dalcin   PetscInt          i;
3905c6c1daeSBarry Smith 
3915c6c1daeSBarry Smith   PetscFunctionBegin;
392e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
3939566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
3943ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
3955c6c1daeSBarry Smith   vdraw = (PetscViewer_Draw *)viewer->data;
396e5ab1681SLisandro Dalcin 
3975c6c1daeSBarry Smith   for (i = 0; i < vdraw->draw_max; i++) {
3989566063dSJacob Faibussowitsch     if (vdraw->draw[i]) PetscCall(PetscDrawClear(vdraw->draw[i]));
3995c6c1daeSBarry Smith   }
4003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4015c6c1daeSBarry Smith }
4025c6c1daeSBarry Smith 
4035c6c1daeSBarry Smith /*@
404811af0c4SBarry Smith   PetscViewerDrawGetPause - Gets the pause value (how long to pause before an image is changed)  in the `PETSCVIEWERDRAW` `PetscViewer`
4055c6c1daeSBarry Smith 
4065c6c1daeSBarry Smith   Not Collective
4075c6c1daeSBarry Smith 
4085c6c1daeSBarry Smith   Input Parameter:
409811af0c4SBarry Smith . viewer - the `PetscViewer`
4105c6c1daeSBarry Smith 
4115c6c1daeSBarry Smith   Output Parameter:
4125c6c1daeSBarry Smith . pause - the pause value
4135c6c1daeSBarry Smith 
4145c6c1daeSBarry Smith   Level: intermediate
4155c6c1daeSBarry Smith 
416d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`,
4175c6c1daeSBarry Smith @*/
PetscViewerDrawGetPause(PetscViewer viewer,PetscReal * pause)418d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawGetPause(PetscViewer viewer, PetscReal *pause)
419d71ae5a4SJacob Faibussowitsch {
4205c6c1daeSBarry Smith   PetscViewer_Draw *vdraw;
421e5ab1681SLisandro Dalcin   PetscBool         isdraw;
422e5ab1681SLisandro Dalcin   PetscInt          i;
4235c6c1daeSBarry Smith   PetscDraw         draw;
4245c6c1daeSBarry Smith 
4255c6c1daeSBarry Smith   PetscFunctionBegin;
426e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4279566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
4289371c9d4SSatish Balay   if (!isdraw) {
4299371c9d4SSatish Balay     *pause = 0.0;
4303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4319371c9d4SSatish Balay   }
4325c6c1daeSBarry Smith   vdraw = (PetscViewer_Draw *)viewer->data;
433e5ab1681SLisandro Dalcin 
4345c6c1daeSBarry Smith   for (i = 0; i < vdraw->draw_max; i++) {
4355c6c1daeSBarry Smith     if (vdraw->draw[i]) {
4369566063dSJacob Faibussowitsch       PetscCall(PetscDrawGetPause(vdraw->draw[i], pause));
4373ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
4385c6c1daeSBarry Smith     }
4395c6c1daeSBarry Smith   }
4405c6c1daeSBarry Smith   /* none exist yet so create one and get its pause */
4419566063dSJacob Faibussowitsch   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
4429566063dSJacob Faibussowitsch   PetscCall(PetscDrawGetPause(draw, pause));
4433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4445c6c1daeSBarry Smith }
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith /*@
447811af0c4SBarry Smith   PetscViewerDrawSetPause - Sets a pause for each `PetscDraw` in the `PETSCVIEWERDRAW` `PetscViewer`
4485c6c1daeSBarry Smith 
4495c6c1daeSBarry Smith   Not Collective
4505c6c1daeSBarry Smith 
4515c6c1daeSBarry Smith   Input Parameters:
452811af0c4SBarry Smith + viewer - the `PetscViewer`
4535c6c1daeSBarry Smith - pause  - the pause value
4545c6c1daeSBarry Smith 
4555c6c1daeSBarry Smith   Level: intermediate
4565c6c1daeSBarry Smith 
457d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`,
4585c6c1daeSBarry Smith @*/
PetscViewerDrawSetPause(PetscViewer viewer,PetscReal pause)459d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawSetPause(PetscViewer viewer, PetscReal pause)
460d71ae5a4SJacob Faibussowitsch {
461e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
4625c6c1daeSBarry Smith   PetscBool         isdraw;
463e5ab1681SLisandro Dalcin   PetscInt          i;
4645c6c1daeSBarry Smith 
4655c6c1daeSBarry Smith   PetscFunctionBegin;
466e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4679566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
4683ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
469e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)viewer->data;
470afe78b3cSBarry Smith 
471afe78b3cSBarry Smith   vdraw->pause = pause;
4725c6c1daeSBarry Smith   for (i = 0; i < vdraw->draw_max; i++) {
4739566063dSJacob Faibussowitsch     if (vdraw->draw[i]) PetscCall(PetscDrawSetPause(vdraw->draw[i], pause));
4745c6c1daeSBarry Smith   }
4753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4765c6c1daeSBarry Smith }
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith /*@
479c410d8ccSBarry Smith   PetscViewerDrawSetHold - Holds previous image when drawing new image in a `PETSCVIEWERDRAW`
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith   Not Collective
4825c6c1daeSBarry Smith 
4835c6c1daeSBarry Smith   Input Parameters:
484811af0c4SBarry Smith + viewer - the `PetscViewer`
485811af0c4SBarry Smith - hold   - `PETSC_TRUE` indicates to hold the previous image
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith   Level: intermediate
4885c6c1daeSBarry Smith 
489d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`,
4905c6c1daeSBarry Smith @*/
PetscViewerDrawSetHold(PetscViewer viewer,PetscBool hold)491d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawSetHold(PetscViewer viewer, PetscBool hold)
492d71ae5a4SJacob Faibussowitsch {
4935c6c1daeSBarry Smith   PetscViewer_Draw *vdraw;
4945c6c1daeSBarry Smith   PetscBool         isdraw;
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith   PetscFunctionBegin;
497e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4989566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
4993ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
5005c6c1daeSBarry Smith   vdraw = (PetscViewer_Draw *)viewer->data;
501e5ab1681SLisandro Dalcin 
5025c6c1daeSBarry Smith   vdraw->hold = hold;
5033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5045c6c1daeSBarry Smith }
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith /*@
507811af0c4SBarry Smith   PetscViewerDrawGetHold - Checks if the `PETSCVIEWERDRAW` `PetscViewer` holds previous image when drawing new image
5085c6c1daeSBarry Smith 
5095c6c1daeSBarry Smith   Not Collective
5105c6c1daeSBarry Smith 
5115c6c1daeSBarry Smith   Input Parameter:
512811af0c4SBarry Smith . viewer - the `PetscViewer`
5135c6c1daeSBarry Smith 
5145c6c1daeSBarry Smith   Output Parameter:
5155c6c1daeSBarry Smith . hold - indicates to hold or not
5165c6c1daeSBarry Smith 
5175c6c1daeSBarry Smith   Level: intermediate
5185c6c1daeSBarry Smith 
519d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`,
5205c6c1daeSBarry Smith @*/
PetscViewerDrawGetHold(PetscViewer viewer,PetscBool * hold)521d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawGetHold(PetscViewer viewer, PetscBool *hold)
522d71ae5a4SJacob Faibussowitsch {
5235c6c1daeSBarry Smith   PetscViewer_Draw *vdraw;
5245c6c1daeSBarry Smith   PetscBool         isdraw;
5255c6c1daeSBarry Smith 
5265c6c1daeSBarry Smith   PetscFunctionBegin;
527e5ab1681SLisandro Dalcin   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5289566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
5299371c9d4SSatish Balay   if (!isdraw) {
5309371c9d4SSatish Balay     *hold = PETSC_FALSE;
5313ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
5329371c9d4SSatish Balay   }
5335c6c1daeSBarry Smith   vdraw = (PetscViewer_Draw *)viewer->data;
534e5ab1681SLisandro Dalcin 
5355c6c1daeSBarry Smith   *hold = vdraw->hold;
5363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5375c6c1daeSBarry Smith }
5385c6c1daeSBarry Smith 
5395c6c1daeSBarry Smith /*
5405c6c1daeSBarry Smith     The variable Petsc_Viewer_Draw_keyval is used to indicate an MPI attribute that
5415c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
5425c6c1daeSBarry Smith */
543d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Draw_keyval = MPI_KEYVAL_INVALID;
5445c6c1daeSBarry Smith 
5455c6c1daeSBarry Smith /*@C
546811af0c4SBarry Smith    PETSC_VIEWER_DRAW_ - Creates a window `PETSCVIEWERDRAW` `PetscViewer` shared by all processors
547c410d8ccSBarry Smith                      in an MPI communicator.
5485c6c1daeSBarry Smith 
549d083f849SBarry Smith    Collective
5505c6c1daeSBarry Smith 
5515c6c1daeSBarry Smith    Input Parameter:
552811af0c4SBarry Smith .  comm - the MPI communicator to share the window `PetscViewer`
5535c6c1daeSBarry Smith 
5545c6c1daeSBarry Smith    Level: intermediate
5555c6c1daeSBarry Smith 
55634fa283eSBarry Smith    Notes:
55734fa283eSBarry Smith    This object is destroyed in `PetscFinalize()`, `PetscViewerDestroy()` should never be called on it
55834fa283eSBarry Smith 
559811af0c4SBarry Smith    Unlike almost all other PETSc routines, `PETSC_VIEWER_DRAW_()` does not return
5605c6c1daeSBarry Smith    an error code.  The window is usually used in the form
561ce78bad3SBarry Smith .vb
562ce78bad3SBarry Smith    XXXView(XXX object, PETSC_VIEWER_DRAW_(comm));
563ce78bad3SBarry Smith .ve
5645c6c1daeSBarry Smith 
565d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewer`, `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_DRAW_SELF`, `PetscViewerDrawOpen()`,
5665c6c1daeSBarry Smith @*/
PETSC_VIEWER_DRAW_(MPI_Comm comm)567d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_DRAW_(MPI_Comm comm)
568d71ae5a4SJacob Faibussowitsch {
569*b8b5be36SMartin Diehl   PetscMPIInt iflg;
5705c6c1daeSBarry Smith   PetscViewer viewer;
5715c6c1daeSBarry Smith   MPI_Comm    ncomm;
5725c6c1daeSBarry Smith 
5735c6c1daeSBarry Smith   PetscFunctionBegin;
574648c30bcSBarry Smith   PetscCallNull(PetscCommDuplicate(comm, &ncomm, NULL));
5753a7d0413SPierre Jolivet   if (Petsc_Viewer_Draw_keyval == MPI_KEYVAL_INVALID) PetscCallMPINull(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Draw_keyval, NULL));
576*b8b5be36SMartin Diehl   PetscCallMPINull(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Draw_keyval, (void **)&viewer, &iflg));
577*b8b5be36SMartin Diehl   if (!iflg) { /* PetscViewer not yet created */
578648c30bcSBarry Smith     PetscCallNull(PetscViewerDrawOpen(ncomm, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, &viewer));
579648c30bcSBarry Smith     PetscCallNull(PetscObjectRegisterDestroy((PetscObject)viewer));
580648c30bcSBarry Smith     PetscCallMPINull(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Draw_keyval, (void *)viewer));
5819371c9d4SSatish Balay   }
582648c30bcSBarry Smith   PetscCallNull(PetscCommDestroy(&ncomm));
5835c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
5845c6c1daeSBarry Smith }
5855c6c1daeSBarry Smith 
5865c6c1daeSBarry Smith /*@
587c410d8ccSBarry Smith   PetscViewerDrawSetBounds - sets the upper and lower bounds to be used in plotting in a `PETSCVIEWERDRAW` `PetscViewer`
5885c6c1daeSBarry Smith 
589c3339decSBarry Smith   Collective
5905c6c1daeSBarry Smith 
5915c6c1daeSBarry Smith   Input Parameters:
592f0b74427SPierre Jolivet + viewer  - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
593811af0c4SBarry Smith . nbounds - number of plots that can be made with this viewer, for example the dof passed to `DMDACreate()`
594c410d8ccSBarry Smith - bounds  - the actual bounds, the size of this is 2*`nbounds`, the values are stored in the order min F_0, max F_0, min F_1, max F_1, .....
5955c6c1daeSBarry Smith 
596811af0c4SBarry Smith   Options Database Key:
59710699b91SBarry Smith . -draw_bounds  minF0,maxF0,minF1,maxF1 - the lower left and upper right bounds
598e9457bf7SBarry Smith 
5995c6c1daeSBarry Smith   Level: intermediate
6005c6c1daeSBarry Smith 
601811af0c4SBarry Smith   Note:
602811af0c4SBarry Smith   this determines the colors used in 2d contour plots generated with VecView() for `DMDA` in 2d. Any values in the vector below or above the
603f3f0eb19SBarry Smith   bounds are moved to the bound value before plotting. In this way the color index from color to physical value remains the same for all plots generated with
604f3f0eb19SBarry Smith   this viewer. Otherwise the color to physical value meaning changes with each new image if this is not set.
605f3f0eb19SBarry Smith 
606d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`
6075c6c1daeSBarry Smith @*/
PetscViewerDrawSetBounds(PetscViewer viewer,PetscInt nbounds,const PetscReal * bounds)608d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDrawSetBounds(PetscViewer viewer, PetscInt nbounds, const PetscReal *bounds)
609d71ae5a4SJacob Faibussowitsch {
610e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
611e5ab1681SLisandro Dalcin   PetscBool         isdraw;
6125c6c1daeSBarry Smith 
6135c6c1daeSBarry Smith   PetscFunctionBegin;
6145c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6159566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
6163ba16761SJacob Faibussowitsch   if (!isdraw) PetscFunctionReturn(PETSC_SUCCESS);
617e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)viewer->data;
618a297a907SKarl Rupp 
619e5ab1681SLisandro Dalcin   vdraw->nbounds = nbounds;
6209566063dSJacob Faibussowitsch   PetscCall(PetscFree(vdraw->bounds));
6219566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * nbounds, &vdraw->bounds));
6229566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(vdraw->bounds, bounds, 2 * nbounds));
6233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6245c6c1daeSBarry Smith }
6255c6c1daeSBarry Smith 
6265c6c1daeSBarry Smith /*@C
627811af0c4SBarry Smith   PetscViewerDrawGetBounds - gets the upper and lower bounds to be used in plotting set with `PetscViewerDrawSetBounds()`
6285c6c1daeSBarry Smith 
629c3339decSBarry Smith   Collective
6305c6c1daeSBarry Smith 
6315c6c1daeSBarry Smith   Input Parameter:
632811af0c4SBarry Smith . viewer - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
6335c6c1daeSBarry Smith 
634fd292e60Sprj-   Output Parameters:
635811af0c4SBarry Smith + nbounds - number of plots that can be made with this viewer, for example the dof passed to `DMDACreate()`
636c410d8ccSBarry Smith - bounds  - the actual bounds, the size of this is 2*`nbounds`, the values are stored in the order min F_0, max F_0, min F_1, max F_1, .....
6375c6c1daeSBarry Smith 
6385c6c1daeSBarry Smith   Level: intermediate
6395c6c1daeSBarry Smith 
640d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetBounds()`
6415c6c1daeSBarry Smith @*/
PetscViewerDrawGetBounds(PetscViewer viewer,PetscInt * nbounds,const PetscReal * bounds[])642cc4c1da9SBarry Smith PetscErrorCode PetscViewerDrawGetBounds(PetscViewer viewer, PetscInt *nbounds, const PetscReal *bounds[])
643d71ae5a4SJacob Faibussowitsch {
644e5ab1681SLisandro Dalcin   PetscViewer_Draw *vdraw;
645e5ab1681SLisandro Dalcin   PetscBool         isdraw;
6465c6c1daeSBarry Smith 
6475c6c1daeSBarry Smith   PetscFunctionBegin;
6485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6499566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
6509371c9d4SSatish Balay   if (!isdraw) {
6519371c9d4SSatish Balay     if (nbounds) *nbounds = 0;
6529371c9d4SSatish Balay     if (bounds) *bounds = NULL;
6533ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6549371c9d4SSatish Balay   }
655e5ab1681SLisandro Dalcin   vdraw = (PetscViewer_Draw *)viewer->data;
656e5ab1681SLisandro Dalcin 
657e5ab1681SLisandro Dalcin   if (nbounds) *nbounds = vdraw->nbounds;
658e5ab1681SLisandro Dalcin   if (bounds) *bounds = vdraw->bounds;
6593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6605c6c1daeSBarry Smith }
661ce78bad3SBarry Smith 
662ce78bad3SBarry Smith /*@C
663ce78bad3SBarry Smith   PetscViewerMonitorLGSetUp - sets up a viewer to be used by line graph monitoring routines such as `KSPMonitorResidualDrawLG()`
664ce78bad3SBarry Smith 
665ce78bad3SBarry Smith   Collective
666ce78bad3SBarry Smith 
667ce78bad3SBarry Smith   Input Parameters:
668ce78bad3SBarry Smith + viewer - the viewer in which to display the line graphs, it not a `PETSCVIEWERDRAW` it will set to that `PetscViewerType`
669ce78bad3SBarry Smith . host   - the host to open the window on, 'NULL' indicates the local host
670ce78bad3SBarry Smith . title  - the title at the top of the window
671ce78bad3SBarry Smith . metric - the label above the graph
672ce78bad3SBarry Smith . l      - the number of curves
673ce78bad3SBarry Smith . names  - the names of each curve to be used in displaying the legend. May be 'NULL'
674ce78bad3SBarry Smith . x      - horizontal screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
675ce78bad3SBarry Smith . y      - vertical screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
676ce78bad3SBarry Smith . m      - window width in pixels, or may use `PETSC_DECIDE` or `PETSC_DRAW_FULL_SIZE`, `PETSC_DRAW_HALF_SIZE`,`PETSC_DRAW_THIRD_SIZE`, `PETSC_DRAW_QUARTER_SIZE`
677ce78bad3SBarry Smith - n      - window height in pixels, or may use `PETSC_DECIDE` or `PETSC_DRAW_FULL_SIZE`, `PETSC_DRAW_HALF_SIZE`,`PETSC_DRAW_THIRD_SIZE`, `PETSC_DRAW_QUARTER_SIZE`
678ce78bad3SBarry Smith 
679ce78bad3SBarry Smith   Level: developer
680ce78bad3SBarry Smith 
681ce78bad3SBarry Smith .seealso: `PetscViewer()`, `PETSCVIEWERDRAW`, `PetscViewerDrawGetDrawLG()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetInfo()`
682ce78bad3SBarry Smith @*/
PetscViewerMonitorLGSetUp(PetscViewer viewer,const char host[],const char title[],const char metric[],PetscInt l,const char * names[],int x,int y,int m,int n)683ce78bad3SBarry Smith PetscErrorCode PetscViewerMonitorLGSetUp(PetscViewer viewer, const char host[], const char title[], const char metric[], PetscInt l, const char *names[], int x, int y, int m, int n) PeNS
684ce78bad3SBarry Smith {
685ce78bad3SBarry Smith   PetscDrawAxis axis;
686ce78bad3SBarry Smith   PetscDrawLG   lg;
687ce78bad3SBarry Smith 
688ce78bad3SBarry Smith   PetscFunctionBegin;
689ce78bad3SBarry Smith   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERDRAW));
690ce78bad3SBarry Smith   PetscCall(PetscViewerDrawSetInfo(viewer, host, title, x, y, m, n));
691ce78bad3SBarry Smith   PetscCall(PetscViewerDrawGetDrawLG(viewer, 0, &lg));
692ce78bad3SBarry Smith   if (names) PetscCall(PetscDrawLGSetLegend(lg, names));
693ce78bad3SBarry Smith   PetscCall(PetscDrawLGSetFromOptions(lg));
694ce78bad3SBarry Smith   PetscCall(PetscDrawLGGetAxis(lg, &axis));
695ce78bad3SBarry Smith   PetscCall(PetscDrawAxisSetLabels(axis, "Convergence", "Iteration", metric));
696ce78bad3SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
697ce78bad3SBarry Smith }
698