Actual source code: drawv.c

  1: #include <../src/sys/classes/viewer/impls/draw/vdraw.h>

  3: static PetscErrorCode PetscViewerDestroy_Draw(PetscViewer v)
  4: {
  5:   PetscInt          i;
  6:   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)v->data;

  8:   PetscFunctionBegin;
  9:   PetscCheck(vdraw->singleton_made == PETSC_FALSE, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Destroying PetscViewer without first restoring singleton");
 10:   for (i = 0; i < vdraw->draw_max; i++) {
 11:     PetscCall(PetscDrawAxisDestroy(&vdraw->drawaxis[i]));
 12:     PetscCall(PetscDrawLGDestroy(&vdraw->drawlg[i]));
 13:     PetscCall(PetscDrawDestroy(&vdraw->draw[i]));
 14:   }
 15:   PetscCall(PetscFree(vdraw->display));
 16:   PetscCall(PetscFree(vdraw->title));
 17:   PetscCall(PetscFree3(vdraw->draw, vdraw->drawlg, vdraw->drawaxis));
 18:   PetscCall(PetscFree(vdraw->bounds));
 19:   PetscCall(PetscFree(vdraw->drawtype));
 20:   PetscCall(PetscFree(v->data));
 21:   PetscFunctionReturn(PETSC_SUCCESS);
 22: }

 24: static PetscErrorCode PetscViewerFlush_Draw(PetscViewer v)
 25: {
 26:   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)v->data;

 28:   PetscFunctionBegin;
 29:   for (PetscInt i = 0; i < vdraw->draw_max; i++) {
 30:     if (vdraw->draw[i] != NULL) PetscCall(PetscDrawFlush(vdraw->draw[i]));
 31:   }
 32:   PetscFunctionReturn(PETSC_SUCCESS);
 33: }

 35: /*@
 36:   PetscViewerDrawBaseAdd - add to the base integer that is added to the `windownumber` passed to `PetscViewerDrawGetDraw()`

 38:   Logically Collective

 40:   Input Parameters:
 41: + viewer       - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
 42: - windownumber - how much to add to the base

 44:   Level: developer

 46:   Note:
 47:   A `PETSCVIEWERDRAW` may have multiple `PetscDraw` subwindows, this increases the number of the subwindow that is returned with `PetscViewerDrawGetDraw()`

 49: .seealso: [](sec_viewers), `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PetscViewerDrawBaseSet()`
 50: @*/
 51: PetscErrorCode PetscViewerDrawBaseAdd(PetscViewer viewer, PetscInt windownumber)
 52: {
 53:   PetscViewer_Draw *vdraw;
 54:   PetscBool         isdraw;

 56:   PetscFunctionBegin;
 59:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
 60:   PetscCheck(isdraw == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
 61:   vdraw = (PetscViewer_Draw *)viewer->data;

 63:   PetscCheck(windownumber + vdraw->draw_base >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Resulting base %" PetscInt_FMT " cannot be negative", windownumber + vdraw->draw_base);
 64:   vdraw->draw_base += windownumber;
 65:   PetscFunctionReturn(PETSC_SUCCESS);
 66: }

 68: /*@
 69:   PetscViewerDrawBaseSet - sets the base integer that is added to the `windownumber` passed to `PetscViewerDrawGetDraw()`

 71:   Logically Collective

 73:   Input Parameters:
 74: + viewer       - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
 75: - windownumber - value to set the base

 77:   Level: developer

 79:   Note:
 80:   A `PETSCVIEWERDRAW` may have multiple `PetscDraw` subwindows, this increases the number of the subwindow that is returned with `PetscViewerDrawGetDraw()`

 82: .seealso: [](sec_viewers), `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PetscViewerDrawBaseAdd()`
 83: @*/
 84: PetscErrorCode PetscViewerDrawBaseSet(PetscViewer viewer, PetscInt windownumber)
 85: {
 86:   PetscViewer_Draw *vdraw;
 87:   PetscBool         isdraw;

 89:   PetscFunctionBegin;
 92:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
 93:   PetscCheck(isdraw == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
 94:   vdraw = (PetscViewer_Draw *)viewer->data;

 96:   PetscCheck(windownumber >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Resulting base %" PetscInt_FMT " cannot be negative", windownumber);
 97:   vdraw->draw_base = windownumber;
 98:   PetscFunctionReturn(PETSC_SUCCESS);
 99: }

101: /*@
102:   PetscViewerDrawResize - Set the default width and height (in pixels) for `PetscDraw` windows created by a `PETSCVIEWERDRAW` viewer.

104:   Logically Collective

106:   Input Parameters:
107: + v - the `PETSCVIEWERDRAW` viewer
108: . w - the new default window width in pixels; values less than 1 are ignored
109: - h - the new default window height in pixels; values less than 1 are ignored

111:   Level: intermediate

113:   Note:
114:   If `v` is not a `PETSCVIEWERDRAW` viewer, the call is a no-op.

116: .seealso: `PetscViewer`, `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetInfo()`
117: @*/
118: PetscErrorCode PetscViewerDrawResize(PetscViewer v, int w, int h)
119: {
120:   PetscViewer_Draw *vdraw;
121:   PetscBool         isdraw;

123:   PetscFunctionBegin;
125:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
126:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
127:   vdraw = (PetscViewer_Draw *)v->data;

129:   if (w >= 1) vdraw->w = w;
130:   if (h >= 1) vdraw->h = h;
131:   PetscFunctionReturn(PETSC_SUCCESS);
132: }

134: /*@
135:   PetscViewerDrawSetInfo - Record the default display, title, position, and size to use for `PetscDraw` windows
136:   created by a `PETSCVIEWERDRAW` viewer.

138:   Logically Collective

140:   Input Parameters:
141: + v       - the `PETSCVIEWERDRAW` viewer
142: . display - the X display name, or `NULL` for the local machine
143: . title   - the window title, or `NULL`
144: . x       - the horizontal screen coordinate of the upper left corner (unused; retained for API symmetry)
145: . y       - the vertical screen coordinate of the upper left corner (unused; retained for API symmetry)
146: . w       - the default window width in pixels; values less than 1 are ignored
147: - h       - the default window height in pixels; values less than 1 are ignored

149:   Level: intermediate

151:   Note:
152:   If `v` is not a `PETSCVIEWERDRAW` viewer, the call is a no-op.

154: .seealso: `PetscViewer`, `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetTitle()`, `PetscViewerDrawResize()`
155: @*/
156: PetscErrorCode PetscViewerDrawSetInfo(PetscViewer v, const char display[], const char title[], int x, int y, int w, int h)
157: {
158:   PetscViewer_Draw *vdraw;
159:   PetscBool         isdraw;

161:   PetscFunctionBegin;
163:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
164:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
165:   vdraw = (PetscViewer_Draw *)v->data;

167:   PetscCall(PetscStrallocpy(display, &vdraw->display));
168:   PetscCall(PetscStrallocpy(title, &vdraw->title));
169:   if (w >= 1) vdraw->w = w;
170:   if (h >= 1) vdraw->h = h;
171:   PetscFunctionReturn(PETSC_SUCCESS);
172: }

174: /*@
175:   PetscViewerDrawSetTitle - Set the default title used for `PetscDraw` windows created by a `PETSCVIEWERDRAW` viewer.

177:   Logically Collective

179:   Input Parameters:
180: + v     - the `PETSCVIEWERDRAW` viewer
181: - title - the window title, or `NULL` for no title

183:   Level: intermediate

185:   Note:
186:   If `v` is not a `PETSCVIEWERDRAW` viewer, the call is a no-op.

188: .seealso: `PetscViewer`, `PETSCVIEWERDRAW`, `PetscViewerDrawGetTitle()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetInfo()`
189: @*/
190: PetscErrorCode PetscViewerDrawSetTitle(PetscViewer v, const char title[])
191: {
192:   PetscViewer_Draw *vdraw;
193:   PetscBool         isdraw;

195:   PetscFunctionBegin;
197:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
198:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
199:   vdraw = (PetscViewer_Draw *)v->data;

201:   PetscCall(PetscFree(vdraw->title));
202:   PetscCall(PetscStrallocpy(title, &vdraw->title));
203:   PetscFunctionReturn(PETSC_SUCCESS);
204: }

206: /*@
207:   PetscViewerDrawGetTitle - Get the default title used for `PetscDraw` windows created by a `PETSCVIEWERDRAW` viewer.

209:   Not Collective; No Fortran Support

211:   Input Parameter:
212: . v - the `PETSCVIEWERDRAW` viewer

214:   Output Parameter:
215: . title - the window title (owned by the viewer; do not free)

217:   Level: intermediate

219: .seealso: `PetscViewer`, `PETSCVIEWERDRAW`, `PetscViewerDrawSetTitle()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetInfo()`
220: @*/
221: PetscErrorCode PetscViewerDrawGetTitle(PetscViewer v, const char *title[])
222: {
223:   PetscViewer_Draw *vdraw;
224:   PetscBool         isdraw;

226:   PetscFunctionBegin;
228:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERDRAW, &isdraw));
229:   PetscCheck(isdraw == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Must be draw type PetscViewer");
230:   vdraw = (PetscViewer_Draw *)v->data;

232:   *title = vdraw->title;
233:   PetscFunctionReturn(PETSC_SUCCESS);
234: }

236: /*@
237:   PetscViewerDrawOpen - Opens a `PetscDraw` window for use as a `PetscViewer` with type
238:   `PETSCVIEWERDRAW`.

240:   Collective

242:   Input Parameters:
243: + comm    - communicator that will share window
244: . display - the X display on which to open, or `NULL` for the local machine
245: . title   - the title to put in the title bar, or `NULL` for no title
246: . x       - horizontal screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
247: . y       - vertical screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
248: . 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`
249: - 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`

251:   Output Parameter:
252: . viewer - the `PetscViewer`

254:   Options Database Keys:
255: + -draw_type (x|sdl3|null|win32|tikz|image) - Sets the type; see `PetscDrawType`
256: . -nox                                      - Disables all x-windows output
257: . -display name                             - Specifies name of machine for the X display
258: . -geometry x,y,w,h                         - allows setting the window location and size
259: - -draw_pause pause                         - Sets time (in seconds) that the program pauses after `PetscDrawPause()` has been called
260:                                               (0 is default, -1 implies until user input)

262:   Level: beginner

264:   Notes:
265:   If you want to do graphics in this window, you must call `PetscViewerDrawGetDraw()` and
266:   perform the graphics on the `PetscDraw` object.

268:   Format options include\:
269: + `PETSC_VIEWER_DRAW_BASIC` - displays with basic format
270: - `PETSC_VIEWER_DRAW_LG`    - displays using a line graph

272:   Fortran Note:
273:   Whenever indicating null character data in a Fortran code,
274:   `PETSC_NULL_CHARACTER` must be employed. Thus, `PETSC_NULL_CHARACTER` can be
275:   used for the `display` and `title` input parameters.

277: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscDrawCreate()`, `PetscViewerDestroy()`, `PetscViewerDrawGetDraw()`, `PetscViewerCreate()`, `PETSC_VIEWER_DRAW_`,
278:           `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_DRAW_SELF`
279: @*/
280: PetscErrorCode PetscViewerDrawOpen(MPI_Comm comm, const char display[], const char title[], int x, int y, int w, int h, PetscViewer *viewer)
281: {
282:   PetscFunctionBegin;
283:   PetscCall(PetscViewerCreate(comm, viewer));
284:   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERDRAW));
285:   PetscCall(PetscViewerDrawSetInfo(*viewer, display, title, x, y, w, h));
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: #include <petsc/private/drawimpl.h>

291: static PetscErrorCode PetscViewerGetSubViewer_Draw(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer)
292: {
293:   PetscMPIInt       rank;
294:   PetscInt          i;
295:   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data, *svdraw;

297:   PetscFunctionBegin;
298:   PetscCheck(vdraw->singleton_made == PETSC_FALSE, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Trying to get SubViewer without first restoring previous");
299:   /* only processor zero can use the PetscViewer draw singleton */
300:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
301:   if (rank == 0) {
302:     PetscMPIInt flg;
303:     PetscDraw   draw, sdraw;

305:     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, comm, &flg));
306:     PetscCheck(flg == MPI_IDENT || flg == MPI_CONGRUENT, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscViewerGetSubViewer() for PETSCVIEWERDRAW requires a singleton MPI_Comm");
307:     PetscCall(PetscViewerCreate(comm, sviewer));
308:     PetscCall(PetscViewerSetType(*sviewer, PETSCVIEWERDRAW));
309:     svdraw             = (PetscViewer_Draw *)(*sviewer)->data;
310:     (*sviewer)->format = viewer->format;
311:     for (i = 0; i < vdraw->draw_max; i++) { /* XXX this is wrong if svdraw->draw_max (initially 5) < vdraw->draw_max */
312:       if (vdraw->draw[i] != NULL) PetscCall(PetscDrawGetSingleton(vdraw->draw[i], &svdraw->draw[i]));
313:     }
314:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
315:     PetscCall(PetscViewerDrawGetDraw(*sviewer, 0, &sdraw));
316:     if (draw->savefilename != NULL) {
317:       PetscCall(PetscDrawSetSave(sdraw, draw->savefilename));
318:       sdraw->savefilecount  = draw->savefilecount;
319:       sdraw->savesinglefile = draw->savesinglefile;
320:       sdraw->savemoviefps   = draw->savemoviefps;
321:       sdraw->saveonclear    = draw->saveonclear;
322:       sdraw->saveonflush    = draw->saveonflush;
323:     }
324:     if (draw->savefinalfilename != NULL) PetscCall(PetscDrawSetSaveFinalImage(sdraw, draw->savefinalfilename));
325:   } else {
326:     PetscDraw draw;
327:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
328:   }
329:   vdraw->singleton_made = PETSC_TRUE;
330:   PetscFunctionReturn(PETSC_SUCCESS);
331: }

333: static PetscErrorCode PetscViewerRestoreSubViewer_Draw(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer)
334: {
335:   PetscMPIInt       rank;
336:   PetscInt          i;
337:   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data, *svdraw;

339:   PetscFunctionBegin;
340:   PetscCheck(vdraw->singleton_made == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Trying to restore a singleton that was not gotten");
341:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
342:   if (rank == 0) {
343:     PetscDraw draw, sdraw;

345:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
346:     PetscCall(PetscViewerDrawGetDraw(*sviewer, 0, &sdraw));
347:     if (draw->savefilename != NULL) {
348:       draw->savefilecount = sdraw->savefilecount;
349:       PetscCallMPI(MPI_Bcast(&draw->savefilecount, 1, MPIU_INT, 0, PetscObjectComm((PetscObject)draw)));
350:     }
351:     svdraw = (PetscViewer_Draw *)(*sviewer)->data;
352:     for (i = 0; i < vdraw->draw_max; i++) {
353:       if (vdraw->draw[i] != NULL && svdraw->draw[i] != NULL) PetscCall(PetscDrawRestoreSingleton(vdraw->draw[i], &svdraw->draw[i]));
354:     }
355:     PetscCall(PetscFree3(svdraw->draw, svdraw->drawlg, svdraw->drawaxis));
356:     PetscCall(PetscFree((*sviewer)->data));
357:     PetscCall(PetscHeaderDestroy(sviewer));
358:   } else {
359:     PetscDraw draw;

361:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
362:     if (draw->savefilename != NULL) PetscCallMPI(MPI_Bcast(&draw->savefilecount, 1, MPIU_INT, 0, PetscObjectComm((PetscObject)draw)));
363:   }

365:   vdraw->singleton_made = PETSC_FALSE;
366:   PetscFunctionReturn(PETSC_SUCCESS);
367: }

369: static PetscErrorCode PetscViewerSetFromOptions_Draw(PetscViewer v, PetscOptionItems PetscOptionsObject)
370: {
371:   PetscReal bounds[16];
372:   PetscInt  nbounds = 16;
373:   PetscBool flg;

375:   PetscFunctionBegin;
376:   PetscOptionsHeadBegin(PetscOptionsObject, "Draw PetscViewer Options");
377:   PetscCall(PetscOptionsRealArray("-draw_bounds", "Bounds to put on plots axis", "PetscViewerDrawSetBounds", bounds, &nbounds, &flg));
378:   if (flg == PETSC_TRUE) PetscCall(PetscViewerDrawSetBounds(v, nbounds / 2, bounds));
379:   PetscOptionsHeadEnd();
380:   PetscFunctionReturn(PETSC_SUCCESS);
381: }

383: static PetscErrorCode PetscViewerView_Draw(PetscViewer viewer, PetscViewer v)
384: {
385:   PetscDraw         draw;
386:   PetscInt          i;
387:   PetscViewer_Draw *vdraw = (PetscViewer_Draw *)viewer->data;
388:   PetscBool         isascii;

390:   PetscFunctionBegin;
391:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERASCII, &isascii));
392:   if (isascii == PETSC_TRUE) PetscCall(PetscViewerASCIIPrintf(v, "Draw viewer is of type %s\n", vdraw->drawtype));
393:   /*  If the PetscViewer has just been created then no vdraw->draw yet
394:       exists so this will not actually call the viewer on any draws. */
395:   for (i = 0; i < vdraw->draw_base; i++) {
396:     if (vdraw->draw[i] != NULL) {
397:       PetscCall(PetscViewerDrawGetDraw(viewer, i, &draw));
398:       PetscCall(PetscDrawView(draw, v));
399:     }
400:   }
401:   PetscFunctionReturn(PETSC_SUCCESS);
402: }

404: /*MC
405:    PETSCVIEWERDRAW - A viewer that generates graphics, either to the screen or a file

407:   Level: beginner

409: .seealso: [](sec_viewers), `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`, `PETSC_VIEWER_DRAW_()`, `PETSC_VIEWER_DRAW_SELF`, `PETSC_VIEWER_DRAW_WORLD`,
410:           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`,
411:           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
412:           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
413: M*/
414: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Draw(PetscViewer viewer)
415: {
416:   PetscViewer_Draw *vdraw;

418:   PetscFunctionBegin;
419:   PetscCall(PetscNew(&vdraw));
420:   viewer->data = (void *)vdraw;

422:   viewer->ops->flush            = PetscViewerFlush_Draw;
423:   viewer->ops->view             = PetscViewerView_Draw;
424:   viewer->ops->destroy          = PetscViewerDestroy_Draw;
425:   viewer->ops->setfromoptions   = PetscViewerSetFromOptions_Draw;
426:   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_Draw;
427:   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_Draw;

429:   /* these are created on the fly if requested */
430:   vdraw->draw_max  = 5;
431:   vdraw->draw_base = 0;
432:   vdraw->w         = PETSC_DECIDE;
433:   vdraw->h         = PETSC_DECIDE;

435:   PetscCall(PetscCalloc3(vdraw->draw_max, &vdraw->draw, vdraw->draw_max, &vdraw->drawlg, vdraw->draw_max, &vdraw->drawaxis));
436:   vdraw->singleton_made = PETSC_FALSE;
437:   PetscFunctionReturn(PETSC_SUCCESS);
438: }

440: /*@
441:   PetscViewerDrawClear - Clears a `PetscDraw` graphic associated with a `PetscViewer`.

443:   Not Collective

445:   Input Parameter:
446: . viewer - the `PetscViewer`

448:   Level: intermediate

450: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`
451: @*/
452: PetscErrorCode PetscViewerDrawClear(PetscViewer viewer)
453: {
454:   PetscViewer_Draw *vdraw;
455:   PetscBool         isdraw;

457:   PetscFunctionBegin;
459:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
460:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
461:   vdraw = (PetscViewer_Draw *)viewer->data;

463:   for (PetscInt i = 0; i < vdraw->draw_max; i++) {
464:     if (vdraw->draw[i] != NULL) PetscCall(PetscDrawClear(vdraw->draw[i]));
465:   }
466:   PetscFunctionReturn(PETSC_SUCCESS);
467: }

469: /*@
470:   PetscViewerDrawGetPause - Gets the pause value (how long to pause before an image is changed)  in the `PETSCVIEWERDRAW` `PetscViewer`

472:   Not Collective

474:   Input Parameter:
475: . viewer - the `PetscViewer`

477:   Output Parameter:
478: . pause - the pause value

480:   Level: intermediate

482: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`
483: @*/
484: PetscErrorCode PetscViewerDrawGetPause(PetscViewer viewer, PetscReal *pause)
485: {
486:   PetscViewer_Draw *vdraw;
487:   PetscBool         isdraw;
488:   PetscDraw         draw;

490:   PetscFunctionBegin;
492:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
493:   if (isdraw == PETSC_FALSE) {
494:     *pause = 0.0;
495:     PetscFunctionReturn(PETSC_SUCCESS);
496:   }
497:   vdraw = (PetscViewer_Draw *)viewer->data;

499:   for (PetscInt i = 0; i < vdraw->draw_max; i++) {
500:     if (vdraw->draw[i] != NULL) {
501:       PetscCall(PetscDrawGetPause(vdraw->draw[i], pause));
502:       PetscFunctionReturn(PETSC_SUCCESS);
503:     }
504:   }
505:   /* none exist yet so create one and get its pause */
506:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
507:   PetscCall(PetscDrawGetPause(draw, pause));
508:   PetscFunctionReturn(PETSC_SUCCESS);
509: }

511: /*@
512:   PetscViewerDrawSetPause - Sets a pause for each `PetscDraw` in the `PETSCVIEWERDRAW` `PetscViewer`

514:   Not Collective

516:   Input Parameters:
517: + viewer - the `PetscViewer`
518: - pause  - the pause value

520:   Level: intermediate

522: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`
523: @*/
524: PetscErrorCode PetscViewerDrawSetPause(PetscViewer viewer, PetscReal pause)
525: {
526:   PetscViewer_Draw *vdraw;
527:   PetscBool         isdraw;

529:   PetscFunctionBegin;
531:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
532:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
533:   vdraw = (PetscViewer_Draw *)viewer->data;

535:   vdraw->pause = pause;
536:   for (PetscInt i = 0; i < vdraw->draw_max; i++) {
537:     if (vdraw->draw[i] != NULL) PetscCall(PetscDrawSetPause(vdraw->draw[i], pause));
538:   }
539:   PetscFunctionReturn(PETSC_SUCCESS);
540: }

542: /*@
543:   PetscViewerDrawSetHold - Holds previous image when drawing new image in a `PETSCVIEWERDRAW`

545:   Not Collective

547:   Input Parameters:
548: + viewer - the `PetscViewer`
549: - hold   - `PETSC_TRUE` indicates to hold the previous image

551:   Level: intermediate

553: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`
554: @*/
555: PetscErrorCode PetscViewerDrawSetHold(PetscViewer viewer, PetscBool hold)
556: {
557:   PetscViewer_Draw *vdraw;
558:   PetscBool         isdraw;

560:   PetscFunctionBegin;
562:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
563:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
564:   vdraw = (PetscViewer_Draw *)viewer->data;

566:   vdraw->hold = hold;
567:   PetscFunctionReturn(PETSC_SUCCESS);
568: }

570: /*@
571:   PetscViewerDrawGetHold - Checks if the `PETSCVIEWERDRAW` `PetscViewer` holds previous image when drawing new image

573:   Not Collective

575:   Input Parameter:
576: . viewer - the `PetscViewer`

578:   Output Parameter:
579: . hold - indicates to hold or not

581:   Level: intermediate

583: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawOpen()`, `PetscViewerDrawGetDraw()`
584: @*/
585: PetscErrorCode PetscViewerDrawGetHold(PetscViewer viewer, PetscBool *hold)
586: {
587:   PetscViewer_Draw *vdraw;
588:   PetscBool         isdraw;

590:   PetscFunctionBegin;
592:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
593:   if (isdraw == PETSC_FALSE) {
594:     *hold = PETSC_FALSE;
595:     PetscFunctionReturn(PETSC_SUCCESS);
596:   }
597:   vdraw = (PetscViewer_Draw *)viewer->data;

599:   *hold = vdraw->hold;
600:   PetscFunctionReturn(PETSC_SUCCESS);
601: }

603: /*
604:     The variable Petsc_Viewer_Draw_keyval is used to indicate an MPI attribute that
605:   is attached to a communicator, in this case the attribute is a PetscViewer.
606: */
607: PetscMPIInt Petsc_Viewer_Draw_keyval = MPI_KEYVAL_INVALID;

609: /*@
610:    PETSC_VIEWER_DRAW_ - Creates a window `PETSCVIEWERDRAW` `PetscViewer` shared by all processors
611:                      in an MPI communicator.

613:    Collective

615:    Input Parameter:
616: .  comm - the MPI communicator to share the window `PetscViewer`

618:    Level: intermediate

620:    Notes:
621:    This object is destroyed in `PetscFinalize()`, `PetscViewerDestroy()` should never be called on it

623:    Unlike almost all other PETSc routines, `PETSC_VIEWER_DRAW_()` does not return
624:    an error code.  The window is usually used in the form
625: .vb
626:    XXXView(XXX object, PETSC_VIEWER_DRAW_(comm));
627: .ve

629: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewer`, `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_DRAW_SELF`, `PetscViewerDrawOpen()`,
630: @*/
631: PetscViewer PETSC_VIEWER_DRAW_(MPI_Comm comm)
632: {
633:   PetscMPIInt iflg;
634:   PetscViewer viewer;
635:   MPI_Comm    ncomm;

637:   PetscFunctionBegin;
638:   PetscCallNull(PetscCommDuplicate(comm, &ncomm, NULL));
639:   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));
640:   PetscCallMPINull(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Draw_keyval, (void **)&viewer, &iflg));
641:   if (!iflg) { /* PetscViewer not yet created */
642:     PetscCallNull(PetscViewerDrawOpen(ncomm, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, &viewer));
643:     PetscCallNull(PetscObjectRegisterDestroy((PetscObject)viewer));
644:     PetscCallMPINull(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Draw_keyval, (void *)viewer));
645:   }
646:   PetscCallNull(PetscCommDestroy(&ncomm));
647:   PetscFunctionReturn(viewer);
648: }

650: /*@
651:   PetscViewerDrawSetBounds - sets the upper and lower bounds to be used in plotting in a `PETSCVIEWERDRAW` `PetscViewer`

653:   Collective

655:   Input Parameters:
656: + viewer  - the `PetscViewer` (created with `PetscViewerDrawOpen()`)
657: . nbounds - number of plots that can be made with this viewer, for example the dof passed to `DMDACreate()`
658: - 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, .....

660:   Options Database Key:
661: . -draw_bounds  minF0,maxF0,minF1,maxF1 - the lower left and upper right bounds

663:   Level: intermediate

665:   Note:
666:   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
667:   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
668:   this viewer. Otherwise the color to physical value meaning changes with each new image if this is not set.

670: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`
671: @*/
672: PetscErrorCode PetscViewerDrawSetBounds(PetscViewer viewer, PetscInt nbounds, const PetscReal *bounds)
673: {
674:   PetscViewer_Draw *vdraw;
675:   PetscBool         isdraw;

677:   PetscFunctionBegin;
679:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
680:   if (isdraw == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
681:   vdraw = (PetscViewer_Draw *)viewer->data;

683:   vdraw->nbounds = nbounds;
684:   PetscCall(PetscFree(vdraw->bounds));
685:   PetscCall(PetscMalloc1(2 * nbounds, &vdraw->bounds));
686:   PetscCall(PetscArraycpy(vdraw->bounds, bounds, 2 * nbounds));
687:   PetscFunctionReturn(PETSC_SUCCESS);
688: }

690: /*@
691:   PetscViewerDrawGetBounds - gets the upper and lower bounds to be used in plotting set with `PetscViewerDrawSetBounds()`

693:   Collective

695:   Input Parameter:
696: . viewer - the `PetscViewer` (created with `PetscViewerDrawOpen()`)

698:   Output Parameters:
699: + nbounds - number of plots that can be made with this viewer, for example the dof passed to `DMDACreate()`
700: - 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, .....

702:   Level: intermediate

704: .seealso: [](sec_viewers), `PETSCVIEWERDRAW`, `PetscViewerDrawGetLG()`, `PetscViewerDrawGetAxis()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetBounds()`
705: @*/
706: PetscErrorCode PetscViewerDrawGetBounds(PetscViewer viewer, PetscInt *nbounds, const PetscReal *bounds[])
707: {
708:   PetscViewer_Draw *vdraw;
709:   PetscBool         isdraw;

711:   PetscFunctionBegin;
713:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
714:   if (isdraw == PETSC_FALSE) {
715:     if (nbounds != NULL) *nbounds = 0;
716:     if (bounds != NULL) *bounds = NULL;
717:     PetscFunctionReturn(PETSC_SUCCESS);
718:   }
719:   vdraw = (PetscViewer_Draw *)viewer->data;

721:   if (nbounds != NULL) *nbounds = vdraw->nbounds;
722:   if (bounds != NULL) *bounds = vdraw->bounds;
723:   PetscFunctionReturn(PETSC_SUCCESS);
724: }

726: /*@
727:   PetscViewerMonitorLGSetUp - sets up a viewer to be used by line graph monitoring routines such as `KSPMonitorResidualDrawLG()`

729:   Collective

731:   Input Parameters:
732: + viewer - the viewer in which to display the line graphs, it not a `PETSCVIEWERDRAW` it will set to that `PetscViewerType`
733: . host   - the host to open the window on, `NULL` indicates the local host
734: . title  - the title at the top of the window
735: . metric - the label above the graph
736: . l      - the number of curves
737: . names  - the names of each curve to be used in displaying the legend. May be `NULL`
738: . x      - horizontal screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
739: . y      - vertical screen coordinate of the upper left corner of window, or use `PETSC_DECIDE`
740: . 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`
741: - 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`

743:   Level: developer

745: .seealso: `PetscViewer()`, `PETSCVIEWERDRAW`, `PetscViewerDrawGetDrawLG()`, `PetscViewerDrawOpen()`, `PetscViewerDrawSetInfo()`
746: @*/
747: 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
748: {
749:   PetscDrawAxis axis;
750:   PetscDrawLG   lg;

752:   PetscFunctionBegin;
753:   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERDRAW));
754:   PetscCall(PetscViewerDrawSetInfo(viewer, host, title, x, y, m, n));
755:   PetscCall(PetscViewerDrawGetDrawLG(viewer, 0, &lg));
756:   if (names != NULL) PetscCall(PetscDrawLGSetLegend(lg, names));
757:   PetscCall(PetscDrawLGSetFromOptions(lg));
758:   PetscCall(PetscDrawLGGetAxis(lg, &axis));
759:   PetscCall(PetscDrawAxisSetLabels(axis, "Convergence", "Iteration", metric));
760:   PetscFunctionReturn(PETSC_SUCCESS);
761: }