Actual source code: draw.c
petsc-3.12.5 2020-03-29
2: /*
3: Provides the calling sequences for all the basic PetscDraw routines.
4: */
5: #include <petsc/private/drawimpl.h>
6: #include <petscviewer.h>
8: PetscClassId PETSC_DRAW_CLASSID;
10: static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
11: /*@C
12: PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
13: called from PetscFinalize().
15: Level: developer
17: .seealso: PetscFinalize()
18: @*/
19: PetscErrorCode PetscDrawFinalizePackage(void)
20: {
24: PetscFunctionListDestroy(&PetscDrawList);
25: PetscDrawPackageInitialized = PETSC_FALSE;
26: PetscDrawRegisterAllCalled = PETSC_FALSE;
27: return(0);
28: }
30: /*@C
31: PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
32: from PetscDLLibraryRegister_petsc() when using dynamic libraries, and on the call to PetscInitialize()
33: when using shared or static libraries.
35: Level: developer
37: .seealso: PetscInitialize()
38: @*/
39: PetscErrorCode PetscDrawInitializePackage(void)
40: {
41: char logList[256];
42: PetscBool opt,pkg;
46: if (PetscDrawPackageInitialized) return(0);
47: PetscDrawPackageInitialized = PETSC_TRUE;
48: /* Register Classes */
49: PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
50: PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
51: PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
52: PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
53: PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
54: PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
55: /* Register Constructors */
56: PetscDrawRegisterAll();
57: /* Process info exclusions */
58: PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);
59: if (opt) {
60: PetscStrInList("draw",logList,',',&pkg);
61: if (pkg) {
62: PetscInfoDeactivateClass(PETSC_DRAW_CLASSID);
63: PetscInfoDeactivateClass(PETSC_DRAWAXIS_CLASSID);
64: PetscInfoDeactivateClass(PETSC_DRAWLG_CLASSID);
65: PetscInfoDeactivateClass(PETSC_DRAWHG_CLASSID);
66: PetscInfoDeactivateClass(PETSC_DRAWBAR_CLASSID);
67: PetscInfoDeactivateClass(PETSC_DRAWSP_CLASSID);
68: }
69: }
70: /* Process summary exclusions */
71: PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
72: if (opt) {
73: PetscStrInList("draw",logList,',',&pkg);
74: if (pkg) {
75: PetscLogEventExcludeClass(PETSC_DRAW_CLASSID);
76: PetscLogEventExcludeClass(PETSC_DRAWAXIS_CLASSID);
77: PetscLogEventExcludeClass(PETSC_DRAWLG_CLASSID);
78: PetscLogEventExcludeClass(PETSC_DRAWHG_CLASSID);
79: PetscLogEventExcludeClass(PETSC_DRAWBAR_CLASSID);
80: PetscLogEventExcludeClass(PETSC_DRAWSP_CLASSID);
81: }
82: }
83: /* Register package finalizer */
84: PetscRegisterFinalize(PetscDrawFinalizePackage);
85: return(0);
86: }
88: /*@
89: PetscDrawResizeWindow - Allows one to resize a window from a program.
91: Collective on PetscDraw
93: Input Parameter:
94: + draw - the window
95: - w,h - the new width and height of the window
97: Level: intermediate
99: .seealso: PetscDrawCheckResizedWindow()
100: @*/
101: PetscErrorCode PetscDrawResizeWindow(PetscDraw draw,int w,int h)
102: {
109: if (draw->ops->resizewindow) {
110: (*draw->ops->resizewindow)(draw,w,h);
111: }
112: return(0);
113: }
115: /*@
116: PetscDrawGetWindowSize - Gets the size of the window.
118: Not collective
120: Input Parameter:
121: . draw - the window
123: Output Parameters:
124: . w,h - the window width and height
126: Level: intermediate
128: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
129: @*/
130: PetscErrorCode PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
131: {
136: if (w) *w = draw->w;
137: if (h) *h = draw->h;
138: return(0);
139: }
141: /*@
142: PetscDrawCheckResizedWindow - Checks if the user has resized the window.
144: Collective on PetscDraw
146: Input Parameter:
147: . draw - the window
149: Level: advanced
151: .seealso: PetscDrawResizeWindow()
153: @*/
154: PetscErrorCode PetscDrawCheckResizedWindow(PetscDraw draw)
155: {
160: if (draw->ops->checkresizedwindow) {
161: (*draw->ops->checkresizedwindow)(draw);
162: }
163: return(0);
164: }
166: /*@C
167: PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.
169: Not collective
171: Input Parameter:
172: . draw - the graphics context
174: Output Parameter:
175: . title - the title
177: Level: intermediate
179: .seealso: PetscDrawSetTitle()
180: @*/
181: PetscErrorCode PetscDrawGetTitle(PetscDraw draw,const char *title[])
182: {
186: *title = draw->title;
187: return(0);
188: }
190: /*@C
191: PetscDrawSetTitle - Sets the title of a PetscDraw context.
193: Collective on PetscDraw
195: Input Parameters:
196: + draw - the graphics context
197: - title - the title
199: Level: intermediate
201: Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
202: in the image.
204: A copy of the string is made, so you may destroy the
205: title string after calling this routine.
207: You can use PetscDrawAxisSetLabels() to indicate a title within the window
209: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
210: @*/
211: PetscErrorCode PetscDrawSetTitle(PetscDraw draw,const char title[])
212: {
218: PetscFree(draw->title);
219: PetscStrallocpy(title,&draw->title);
220: if (draw->ops->settitle) {
221: (*draw->ops->settitle)(draw,draw->title);
222: }
223: return(0);
224: }
226: /*@C
227: PetscDrawAppendTitle - Appends to the title of a PetscDraw context.
229: Collective on PetscDraw
231: Input Parameters:
232: + draw - the graphics context
233: - title - the title
235: Note:
236: A copy of the string is made, so you may destroy the
237: title string after calling this routine.
239: Level: advanced
241: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
242: @*/
243: PetscErrorCode PetscDrawAppendTitle(PetscDraw draw,const char title[])
244: {
250: if (!title || !title[0]) return(0);
252: if (draw->title) {
253: size_t len1,len2;
254: char *newtitle;
255: PetscStrlen(title,&len1);
256: PetscStrlen(draw->title,&len2);
257: PetscMalloc1(len1 + len2 + 1,&newtitle);
258: PetscStrcpy(newtitle,draw->title);
259: PetscStrcat(newtitle,title);
260: PetscFree(draw->title);
261: draw->title = newtitle;
262: } else {
263: PetscStrallocpy(title,&draw->title);
264: }
265: if (draw->ops->settitle) {
266: (*draw->ops->settitle)(draw,draw->title);
267: }
268: return(0);
269: }
271: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
272: {
276: if (!draw->ops->save && !draw->ops->getimage) return(0);
277: PetscDrawSaveMovie(draw);
278: if (draw->savefinalfilename) {
279: draw->savesinglefile = PETSC_TRUE;
280: PetscDrawSetSave(draw,draw->savefinalfilename);
281: PetscDrawSave(draw);
282: }
283: PetscBarrier((PetscObject)draw);
284: return(0);
285: }
287: /*@
288: PetscDrawDestroy - Deletes a draw context.
290: Collective on PetscDraw
292: Input Parameters:
293: . draw - the drawing context
295: Level: beginner
297: .seealso: PetscDrawCreate()
299: @*/
300: PetscErrorCode PetscDrawDestroy(PetscDraw *draw)
301: {
305: if (!*draw) return(0);
307: if (--((PetscObject)(*draw))->refct > 0) return(0);
309: if ((*draw)->pause == -2) {
310: (*draw)->pause = -1;
311: PetscDrawPause(*draw);
312: }
314: /* if memory was published then destroy it */
315: PetscObjectSAWsViewOff((PetscObject)*draw);
317: PetscDrawDestroy_Private(*draw);
319: if ((*draw)->ops->destroy) {
320: (*(*draw)->ops->destroy)(*draw);
321: }
322: PetscDrawDestroy(&(*draw)->popup);
323: PetscFree((*draw)->title);
324: PetscFree((*draw)->display);
325: PetscFree((*draw)->savefilename);
326: PetscFree((*draw)->saveimageext);
327: PetscFree((*draw)->savemovieext);
328: PetscFree((*draw)->savefinalfilename);
329: PetscHeaderDestroy(draw);
330: return(0);
331: }
333: /*@
334: PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.
336: Collective on PetscDraw
338: Input Parameter:
339: . draw - the original window
341: Output Parameter:
342: . popup - the new popup window
344: Level: advanced
346: .seealso: PetscDrawScalePopup(), PetscDrawCreate()
348: @*/
349: PetscErrorCode PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
350: {
357: if (draw->popup) *popup = draw->popup;
358: else if (draw->ops->getpopup) {
359: (*draw->ops->getpopup)(draw,popup);
360: if (*popup) {
361: PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
362: (*popup)->pause = 0.0;
363: PetscDrawSetFromOptions(*popup);
364: }
365: } else *popup = NULL;
366: return(0);
367: }
369: /*@C
370: PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
372: Input Parameter:
373: + draw - the drawing context
374: - display - the X windows display
376: Level: advanced
378: .seealso: PetscDrawCreate()
380: @*/
381: PetscErrorCode PetscDrawSetDisplay(PetscDraw draw,const char display[])
382: {
386: PetscFree(draw->display);
387: PetscStrallocpy(display,&draw->display);
388: return(0);
389: }
392: /*@
393: PetscDrawSetDoubleBuffer - Sets a window to be double buffered.
395: Logically Collective on PetscDraw
397: Input Parameter:
398: . draw - the drawing context
400: Level: intermediate
402: @*/
403: PetscErrorCode PetscDrawSetDoubleBuffer(PetscDraw draw)
404: {
409: if (draw->ops->setdoublebuffer) {
410: (*draw->ops->setdoublebuffer)(draw);
411: }
412: return(0);
413: }
415: /*@C
416: PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
417: by the one process.
419: Collective on PetscDraw
421: Input Parameter:
422: . draw - the original window
424: Output Parameter:
425: . sdraw - the singleton window
427: Level: advanced
429: .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
431: @*/
432: PetscErrorCode PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
433: {
435: PetscMPIInt size;
441: MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
442: if (size == 1) {
443: PetscObjectReference((PetscObject)draw);
444: *sdraw = draw;
445: } else {
446: if (draw->ops->getsingleton) {
447: (*draw->ops->getsingleton)(draw,sdraw);
448: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
449: }
450: return(0);
451: }
453: /*@C
454: PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
455: by the one process.
457: Collective on PetscDraw
459: Input Parameters:
460: + draw - the original window
461: - sdraw - the singleton window
463: Level: advanced
465: .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
467: @*/
468: PetscErrorCode PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
469: {
471: PetscMPIInt size;
478: MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
479: if (size == 1) {
480: if (draw == *sdraw) {
481: PetscObjectDereference((PetscObject)draw);
482: *sdraw = NULL;
483: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
484: } else {
485: if (draw->ops->restoresingleton) {
486: (*draw->ops->restoresingleton)(draw,sdraw);
487: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
488: }
489: return(0);
490: }