Actual source code: lgc.c
petsc-3.13.6 2020-09-29
2: #include <petscviewer.h>
3: #include <../src/sys/classes/draw/utils/lgimpl.h>
4: PetscClassId PETSC_DRAWLG_CLASSID = 0;
6: /*@
7: PetscDrawLGGetAxis - Gets the axis context associated with a line graph.
8: This is useful if one wants to change some axis property, such as
9: labels, color, etc. The axis context should not be destroyed by the
10: Section 1.5 Writing Application Codes with PETSc code.
12: Not Collective, if PetscDrawLG is parallel then PetscDrawAxis is parallel
14: Input Parameter:
15: . lg - the line graph context
17: Output Parameter:
18: . axis - the axis context
20: Level: advanced
22: .seealso: PetscDrawLGCreate(), PetscDrawAxis
24: @*/
25: PetscErrorCode PetscDrawLGGetAxis(PetscDrawLG lg,PetscDrawAxis *axis)
26: {
30: *axis = lg->axis;
31: return(0);
32: }
34: /*@
35: PetscDrawLGGetDraw - Gets the draw context associated with a line graph.
37: Not Collective, if PetscDrawLG is parallel then PetscDraw is parallel
39: Input Parameter:
40: . lg - the line graph context
42: Output Parameter:
43: . draw - the draw context
45: Level: intermediate
47: .seealso: PetscDrawLGCreate(), PetscDraw
48: @*/
49: PetscErrorCode PetscDrawLGGetDraw(PetscDrawLG lg,PetscDraw *draw)
50: {
54: *draw = lg->win;
55: return(0);
56: }
59: /*@
60: PetscDrawLGSPDraw - Redraws a line graph.
62: Collective on PetscDrawLG
64: Input Parameter:
65: . lg - the line graph context
67: Level: intermediate
69: .seealso: PetscDrawLGDraw(), PetscDrawSPDraw()
71: Developer Notes:
72: This code cheats and uses the fact that the LG and SP structs are the same
74: @*/
75: PetscErrorCode PetscDrawLGSPDraw(PetscDrawLG lg,PetscDrawSP spin)
76: {
77: PetscDrawLG sp = (PetscDrawLG)spin;
78: PetscReal xmin,xmax,ymin,ymax;
80: PetscBool isnull;
81: PetscMPIInt rank;
82: PetscDraw draw;
87: PetscDrawIsNull(lg->win,&isnull);
88: if (isnull) return(0);
89: MPI_Comm_rank(PetscObjectComm((PetscObject)lg),&rank);
91: draw = lg->win;
92: PetscDrawCheckResizedWindow(draw);
93: PetscDrawClear(draw);
95: xmin = PetscMin(lg->xmin,sp->xmin); ymin = PetscMin(lg->ymin,sp->ymin);
96: xmax = PetscMax(lg->xmax,sp->xmax); ymax = PetscMax(lg->ymax,sp->ymax);
97: PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);
98: PetscDrawAxisDraw(lg->axis);
100: PetscDrawCollectiveBegin(draw);
101: if (!rank) {
102: int i,j,dim,nopts;
103: dim = lg->dim;
104: nopts = lg->nopts;
105: for (i=0; i<dim; i++) {
106: for (j=1; j<nopts; j++) {
107: PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_BLACK+i);
108: if (lg->use_markers) {
109: PetscDrawMarker(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED);
110: }
111: }
112: }
113: dim = sp->dim;
114: nopts = sp->nopts;
115: for (i=0; i<dim; i++) {
116: for (j=0; j<nopts; j++) {
117: PetscDrawMarker(draw,sp->x[j*dim+i],sp->y[j*dim+i],PETSC_DRAW_RED);
118: }
119: }
120: }
121: PetscDrawCollectiveEnd(draw);
123: PetscDrawFlush(draw);
124: PetscDrawPause(draw);
125: return(0);
126: }
129: /*@
130: PetscDrawLGCreate - Creates a line graph data structure.
132: Collective on PetscDraw
134: Input Parameters:
135: + draw - the window where the graph will be made.
136: - dim - the number of curves which will be drawn
138: Output Parameters:
139: . outlg - the line graph context
141: Level: intermediate
143: Notes:
144: The MPI communicator that owns the PetscDraw owns this PetscDrawLG, but the calls to set options and add points are ignored on all processes except the
145: zeroth MPI process in the communicator. All MPI processes in the communicator must call PetscDrawLGDraw() to display the updated graph.
147: .seealso: PetscDrawLGDestroy(), PetscDrawLGAddPoint(), PetscDrawLGAddCommonPoint(), PetscDrawLGAddPoints(), PetscDrawLGDraw(), PetscDrawLGSave(),
148: PetscDrawLGView(), PetscDrawLGReset(), PetscDrawLGSetDimension(), PetscDrawLGGetDimension(), PetscDrawLGSetLegend(), PetscDrawLGGetAxis(),
149: PetscDrawLGGetDraw(), PetscDrawLGSetUseMarkers(), PetscDrawLGSetLimits(), PetscDrawLGSetColors(), PetscDrawLGSetOptionsPrefix(), PetscDrawLGSetFromOptions()
150: @*/
151: PetscErrorCode PetscDrawLGCreate(PetscDraw draw,PetscInt dim,PetscDrawLG *outlg)
152: {
153: PetscDrawLG lg;
161: PetscHeaderCreate(lg,PETSC_DRAWLG_CLASSID,"DrawLG","Line Graph","Draw",PetscObjectComm((PetscObject)draw),PetscDrawLGDestroy,NULL);
162: PetscLogObjectParent((PetscObject)draw,(PetscObject)lg);
163: PetscDrawLGSetOptionsPrefix(lg,((PetscObject)draw)->prefix);
165: PetscObjectReference((PetscObject)draw);
166: lg->win = draw;
168: lg->view = NULL;
169: lg->destroy = NULL;
170: lg->nopts = 0;
171: lg->dim = dim;
172: lg->xmin = 1.e20;
173: lg->ymin = 1.e20;
174: lg->xmax = -1.e20;
175: lg->ymax = -1.e20;
177: PetscMalloc2(dim*CHUNCKSIZE,&lg->x,dim*CHUNCKSIZE,&lg->y);
178: PetscLogObjectMemory((PetscObject)lg,2*dim*CHUNCKSIZE*sizeof(PetscReal));
180: lg->len = dim*CHUNCKSIZE;
181: lg->loc = 0;
182: lg->use_markers = PETSC_FALSE;
184: PetscDrawAxisCreate(draw,&lg->axis);
185: PetscLogObjectParent((PetscObject)lg,(PetscObject)lg->axis);
187: *outlg = lg;
188: return(0);
189: }
191: /*@
192: PetscDrawLGSetColors - Sets the color of each line graph drawn
194: Logically Collective on PetscDrawLG
196: Input Parameter:
197: + lg - the line graph context.
198: - colors - the colors
200: Level: intermediate
202: .seealso: PetscDrawLGCreate()
204: @*/
205: PetscErrorCode PetscDrawLGSetColors(PetscDrawLG lg,const int colors[])
206: {
213: PetscFree(lg->colors);
214: PetscMalloc1(lg->dim,&lg->colors);
215: PetscArraycpy(lg->colors,colors,lg->dim);
216: return(0);
217: }
219: /*@C
220: PetscDrawLGSetLegend - sets the names of each curve plotted
222: Logically Collective on PetscDrawLG
224: Input Parameter:
225: + lg - the line graph context.
226: - names - the names for each curve
228: Level: intermediate
230: Notes:
231: Call PetscDrawLGGetAxis() and then change properties of the PetscDrawAxis for detailed control of the plot
233: .seealso: PetscDrawLGGetAxis(), PetscDrawAxis, PetscDrawAxisSetColors(), PetscDrawAxisSetLabels(), PetscDrawAxisSetHoldLimits()
235: @*/
236: PetscErrorCode PetscDrawLGSetLegend(PetscDrawLG lg,const char *const *names)
237: {
239: PetscInt i;
245: if (lg->legend) {
246: for (i=0; i<lg->dim; i++) {
247: PetscFree(lg->legend[i]);
248: }
249: PetscFree(lg->legend);
250: }
251: if (names) {
252: PetscMalloc1(lg->dim,&lg->legend);
253: for (i=0; i<lg->dim; i++) {
254: PetscStrallocpy(names[i],&lg->legend[i]);
255: }
256: }
257: return(0);
258: }
260: /*@
261: PetscDrawLGGetDimension - Change the number of lines that are to be drawn.
263: Not Collective
265: Input Parameter:
266: . lg - the line graph context.
268: Output Parameter:
269: . dim - the number of curves.
271: Level: intermediate
273: .seealso: PetscDrawLGCreate(), PetscDrawLGSetDimension()
275: @*/
276: PetscErrorCode PetscDrawLGGetDimension(PetscDrawLG lg,PetscInt *dim)
277: {
281: *dim = lg->dim;
282: return(0);
283: }
285: /*@
286: PetscDrawLGSetDimension - Change the number of lines that are to be drawn.
288: Logically Collective on PetscDrawLG
290: Input Parameter:
291: + lg - the line graph context.
292: - dim - the number of curves.
294: Level: intermediate
296: .seealso: PetscDrawLGCreate(), PetscDrawLGGetDimension()
297: @*/
298: PetscErrorCode PetscDrawLGSetDimension(PetscDrawLG lg,PetscInt dim)
299: {
301: PetscInt i;
306: if (lg->dim == dim) return(0);
308: PetscFree2(lg->x,lg->y);
309: if (lg->legend) {
310: for (i=0; i<lg->dim; i++) {
311: PetscFree(lg->legend[i]);
312: }
313: PetscFree(lg->legend);
314: }
315: PetscFree(lg->colors);
316: lg->dim = dim;
317: PetscMalloc2(dim*CHUNCKSIZE,&lg->x,dim*CHUNCKSIZE,&lg->y);
318: PetscLogObjectMemory((PetscObject)lg,2*dim*CHUNCKSIZE*sizeof(PetscReal));
319: lg->len = dim*CHUNCKSIZE;
320: return(0);
321: }
324: /*@
325: PetscDrawLGSetLimits - Sets the axis limits for a line graph. If more
326: points are added after this call, the limits will be adjusted to
327: include those additional points.
329: Logically Collective on PetscDrawLG
331: Input Parameters:
332: + xlg - the line graph context
333: - x_min,x_max,y_min,y_max - the limits
335: Level: intermediate
337: .seealso: PetscDrawLGCreate()
339: @*/
340: PetscErrorCode PetscDrawLGSetLimits(PetscDrawLG lg,PetscReal x_min,PetscReal x_max,PetscReal y_min,PetscReal y_max)
341: {
345: (lg)->xmin = x_min;
346: (lg)->xmax = x_max;
347: (lg)->ymin = y_min;
348: (lg)->ymax = y_max;
349: return(0);
350: }
352: /*@
353: PetscDrawLGReset - Clears line graph to allow for reuse with new data.
355: Logically Collective on PetscDrawLG
357: Input Parameter:
358: . lg - the line graph context.
360: Level: intermediate
362: .seealso: PetscDrawLGCreate()
364: @*/
365: PetscErrorCode PetscDrawLGReset(PetscDrawLG lg)
366: {
369: lg->xmin = 1.e20;
370: lg->ymin = 1.e20;
371: lg->xmax = -1.e20;
372: lg->ymax = -1.e20;
373: lg->loc = 0;
374: lg->nopts = 0;
375: return(0);
376: }
378: /*@
379: PetscDrawLGDestroy - Frees all space taken up by line graph data structure.
381: Collective on PetscDrawLG
383: Input Parameter:
384: . lg - the line graph context
386: Level: intermediate
388: .seealso: PetscDrawLGCreate()
389: @*/
390: PetscErrorCode PetscDrawLGDestroy(PetscDrawLG *lg)
391: {
393: PetscInt i;
396: if (!*lg) return(0);
398: if (--((PetscObject)(*lg))->refct > 0) {*lg = NULL; return(0);}
400: if ((*lg)->legend) {
401: for (i=0; i<(*lg)->dim; i++) {
402: PetscFree((*lg)->legend[i]);
403: }
404: PetscFree((*lg)->legend);
405: }
406: PetscFree((*lg)->colors);
407: PetscFree2((*lg)->x,(*lg)->y);
408: PetscDrawAxisDestroy(&(*lg)->axis);
409: PetscDrawDestroy(&(*lg)->win);
410: PetscHeaderDestroy(lg);
411: return(0);
412: }
413: /*@
414: PetscDrawLGSetUseMarkers - Causes LG to draw a marker for each data-point.
416: Logically Collective on PetscDrawLG
418: Input Parameters:
419: + lg - the linegraph context
420: - flg - should mark each data point
422: Options Database:
423: . -lg_use_markers <true,false>
425: Level: intermediate
427: .seealso: PetscDrawLGCreate()
429: @*/
430: PetscErrorCode PetscDrawLGSetUseMarkers(PetscDrawLG lg,PetscBool flg)
431: {
435: lg->use_markers = flg;
436: return(0);
437: }
439: /*@
440: PetscDrawLGDraw - Redraws a line graph.
442: Collective on PetscDrawLG
444: Input Parameter:
445: . lg - the line graph context
447: Level: intermediate
449: .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw(), PetscDrawLGReset()
451: @*/
452: PetscErrorCode PetscDrawLGDraw(PetscDrawLG lg)
453: {
454: PetscReal xmin,xmax,ymin,ymax;
456: PetscMPIInt rank;
457: PetscDraw draw;
458: PetscBool isnull;
462: PetscDrawIsNull(lg->win,&isnull);
463: if (isnull) return(0);
464: MPI_Comm_rank(PetscObjectComm((PetscObject)lg),&rank);
466: draw = lg->win;
467: PetscDrawCheckResizedWindow(draw);
468: PetscDrawClear(draw);
470: xmin = lg->xmin; xmax = lg->xmax; ymin = lg->ymin; ymax = lg->ymax;
471: PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);
472: PetscDrawAxisDraw(lg->axis);
474: PetscDrawCollectiveBegin(draw);
475: if (!rank) {
476: int i,j,dim=lg->dim,nopts=lg->nopts,cl;
477: for (i=0; i<dim; i++) {
478: for (j=1; j<nopts; j++) {
479: cl = lg->colors ? lg->colors[i] : ((PETSC_DRAW_BLACK + i) % PETSC_DRAW_MAXCOLOR);
480: PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],cl);
481: if (lg->use_markers) {PetscDrawMarker(draw,lg->x[j*dim+i],lg->y[j*dim+i],cl);}
482: }
483: }
484: }
485: if (!rank && lg->legend) {
486: int i,dim=lg->dim,cl;
487: PetscReal xl,yl,xr,yr,tw,th;
488: size_t slen,len=0;
489: PetscDrawAxisGetLimits(lg->axis,&xl,&xr,&yl,&yr);
490: PetscDrawStringGetSize(draw,&tw,&th);
491: for (i=0; i<dim; i++) {
492: PetscStrlen(lg->legend[i],&slen);
493: len = PetscMax(len,slen);
494: }
495: xr = xr - 1.5*tw; xl = xr - (len + 7)*tw;
496: yr = yr - 1.0*th; yl = yr - (dim + 1)*th;
497: PetscDrawLine(draw,xl,yl,xr,yl,PETSC_DRAW_BLACK);
498: PetscDrawLine(draw,xr,yl,xr,yr,PETSC_DRAW_BLACK);
499: PetscDrawLine(draw,xr,yr,xl,yr,PETSC_DRAW_BLACK);
500: PetscDrawLine(draw,xl,yr,xl,yl,PETSC_DRAW_BLACK);
501: for (i=0; i<dim; i++) {
502: cl = lg->colors ? lg->colors[i] : (PETSC_DRAW_BLACK + i);
503: PetscDrawLine(draw,xl + 1*tw,yr - (i + 1)*th,xl + 5*tw,yr - (i + 1)*th,cl);
504: PetscDrawString(draw,xl + 6*tw,yr - (i + 1.5)*th,PETSC_DRAW_BLACK,lg->legend[i]);
505: }
506: }
507: PetscDrawCollectiveEnd(draw);
509: PetscDrawFlush(draw);
510: PetscDrawPause(draw);
511: return(0);
512: }
514: /*@
515: PetscDrawLGSave - Saves a drawn image
517: Collective on PetscDrawLG
519: Input Parameter:
520: . lg - The line graph context
522: Level: intermediate
524: .seealso: PetscDrawLGCreate(), PetscDrawLGGetDraw(), PetscDrawSetSave(), PetscDrawSave()
525: @*/
526: PetscErrorCode PetscDrawLGSave(PetscDrawLG lg)
527: {
532: PetscDrawSave(lg->win);
533: return(0);
534: }
536: /*@
537: PetscDrawLGView - Prints a line graph.
539: Collective on PetscDrawLG
541: Input Parameter:
542: . lg - the line graph context
544: Level: beginner
546: .seealso: PetscDrawLGCreate()
548: @*/
549: PetscErrorCode PetscDrawLGView(PetscDrawLG lg,PetscViewer viewer)
550: {
551: PetscReal xmin=lg->xmin, xmax=lg->xmax, ymin=lg->ymin, ymax=lg->ymax;
552: PetscInt i, j, dim = lg->dim, nopts = lg->nopts;
558: if (nopts < 1) return(0);
559: if (xmin > xmax || ymin > ymax) return(0);
561: if (!viewer){
562: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)lg),&viewer);
563: }
564: PetscObjectPrintClassNamePrefixType((PetscObject)lg,viewer);
565: for (i = 0; i < dim; i++) {
566: PetscViewerASCIIPrintf(viewer, "Line %D>\n", i);
567: for (j = 0; j < nopts; j++) {
568: PetscViewerASCIIPrintf(viewer, " X: %g Y: %g\n", (double)lg->x[j*dim+i], (double)lg->y[j*dim+i]);
569: }
570: }
571: return(0);
572: }
574: /*@C
575: PetscDrawLGSetOptionsPrefix - Sets the prefix used for searching for all
576: PetscDrawLG options in the database.
578: Logically Collective on PetscDrawLG
580: Input Parameter:
581: + lg - the line graph context
582: - prefix - the prefix to prepend to all option names
584: Level: advanced
586: .seealso: PetscDrawLGSetFromOptions(), PetscDrawLGCreate()
587: @*/
588: PetscErrorCode PetscDrawLGSetOptionsPrefix(PetscDrawLG lg,const char prefix[])
589: {
594: PetscObjectSetOptionsPrefix((PetscObject)lg,prefix);
595: return(0);
596: }
598: /*@
599: PetscDrawLGSetFromOptions - Sets options related to the PetscDrawLG
601: Collective on PetscDrawLG
603: Options Database:
605: Level: intermediate
607: .seealso: PetscDrawLGDestroy(), PetscDrawLGCreate()
608: @*/
609: PetscErrorCode PetscDrawLGSetFromOptions(PetscDrawLG lg)
610: {
611: PetscErrorCode ierr;
612: PetscBool usemarkers,set;
613: PetscDrawMarkerType markertype;
618: PetscDrawGetMarkerType(lg->win,&markertype);
619: PetscOptionsGetEnum(((PetscObject)lg)->options,((PetscObject)lg)->prefix,"-lg_marker_type",PetscDrawMarkerTypes,(PetscEnum*)&markertype,&set);
620: if (set) {
621: PetscDrawLGSetUseMarkers(lg,PETSC_TRUE);
622: PetscDrawSetMarkerType(lg->win,markertype);
623: }
624: usemarkers = lg->use_markers;
625: PetscOptionsGetBool(((PetscObject)lg)->options,((PetscObject)lg)->prefix,"-lg_use_markers",&usemarkers,&set);
626: if (set) {PetscDrawLGSetUseMarkers(lg,usemarkers);}
627: return(0);
628: }