xref: /petsc/src/sys/classes/draw/utils/dscatter.c (revision aaa8cc7d2a5c3913edcbb923e20f154fe9c4aa65)
15c6c1daeSBarry Smith /*
25c6c1daeSBarry Smith        Contains the data structure for drawing scatter plots
35c6c1daeSBarry Smith     graphs in a window with an axis. This is intended for scatter
45c6c1daeSBarry Smith     plots that change dynamically.
55c6c1daeSBarry Smith */
65c6c1daeSBarry Smith 
79804daf3SBarry Smith #include <petscdraw.h>              /*I "petscdraw.h" I*/
8999739cfSJacob Faibussowitsch #include <petsc/private/drawimpl.h> /*I "petscsys.h" I*/
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith PetscClassId PETSC_DRAWSP_CLASSID = 0;
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith /*@C
135c6c1daeSBarry Smith   PetscDrawSPCreate - Creates a scatter plot data structure.
145c6c1daeSBarry Smith 
15c3339decSBarry Smith   Collective
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith   Input Parameters:
185c6c1daeSBarry Smith + win - the window where the graph will be made.
195c6c1daeSBarry Smith - dim - the number of sets of points which will be drawn
205c6c1daeSBarry Smith 
2120f4b53cSBarry Smith   Output Parameter:
225c6c1daeSBarry Smith . drawsp - the scatter plot context
235c6c1daeSBarry Smith 
245c6c1daeSBarry Smith   Level: intermediate
255c6c1daeSBarry Smith 
2695452b02SPatrick Sanan   Notes:
27811af0c4SBarry Smith   Add points to the plot with `PetscDrawSPAddPoint()` or `PetscDrawSPAddPoints()`; the new points are not displayed until `PetscDrawSPDraw()` is called.
280afdd333SBarry Smith 
29811af0c4SBarry Smith   `PetscDrawSPReset()` removes all the points that have been added
300afdd333SBarry Smith 
31811af0c4SBarry Smith   `PetscDrawSPSetDimension()` determines how many point curves are being plotted.
32811af0c4SBarry Smith 
33811af0c4SBarry Smith   The MPI communicator that owns the `PetscDraw` owns this `PetscDrawSP`, and each process can add points. All MPI ranks in the communicator must call `PetscDrawSPDraw()` to display the updated graph.
347e25d57eSBarry Smith 
35db781477SPatrick Sanan .seealso: `PetscDrawLGCreate()`, `PetscDrawLG`, `PetscDrawBarCreate()`, `PetscDrawBar`, `PetscDrawHGCreate()`, `PetscDrawHG`, `PetscDrawSPDestroy()`, `PetscDraw`, `PetscDrawSP`, `PetscDrawSPSetDimension()`, `PetscDrawSPReset()`,
36c2e3fba1SPatrick Sanan           `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`, `PetscDrawSPDraw()`, `PetscDrawSPSave()`, `PetscDrawSPSetLimits()`, `PetscDrawSPGetAxis()`, `PetscDrawAxis`, `PetscDrawSPGetDraw()`
375c6c1daeSBarry Smith @*/
38d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPCreate(PetscDraw draw, int dim, PetscDrawSP *drawsp)
39d71ae5a4SJacob Faibussowitsch {
405c6c1daeSBarry Smith   PetscDrawSP sp;
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith   PetscFunctionBegin;
435c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
445c6c1daeSBarry Smith   PetscValidPointer(drawsp, 3);
45e118a51fSLisandro Dalcin 
469566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(sp, PETSC_DRAWSP_CLASSID, "DrawSP", "Scatter Plot", "Draw", PetscObjectComm((PetscObject)draw), PetscDrawSPDestroy, NULL));
479566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)draw));
485c6c1daeSBarry Smith   sp->win       = draw;
49e118a51fSLisandro Dalcin   sp->view      = NULL;
50e118a51fSLisandro Dalcin   sp->destroy   = NULL;
51e118a51fSLisandro Dalcin   sp->nopts     = 0;
52f98b2f00SMatthew G. Knepley   sp->dim       = -1;
535c6c1daeSBarry Smith   sp->xmin      = 1.e20;
545c6c1daeSBarry Smith   sp->ymin      = 1.e20;
55f98b2f00SMatthew G. Knepley   sp->zmin      = 1.e20;
565c6c1daeSBarry Smith   sp->xmax      = -1.e20;
575c6c1daeSBarry Smith   sp->ymax      = -1.e20;
58f98b2f00SMatthew G. Knepley   sp->zmax      = -1.e20;
598c87cf4dSdanfinn   sp->colorized = PETSC_FALSE;
605c6c1daeSBarry Smith   sp->loc       = 0;
61a297a907SKarl Rupp 
62f98b2f00SMatthew G. Knepley   PetscCall(PetscDrawSPSetDimension(sp, dim));
639566063dSJacob Faibussowitsch   PetscCall(PetscDrawAxisCreate(draw, &sp->axis));
64a297a907SKarl Rupp 
655c6c1daeSBarry Smith   *drawsp = sp;
663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
675c6c1daeSBarry Smith }
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith /*@
70811af0c4SBarry Smith   PetscDrawSPSetDimension - Change the number of points that are added at each  `PetscDrawSPAddPoint()`
715c6c1daeSBarry Smith 
7220f4b53cSBarry Smith   Not Collective
735c6c1daeSBarry Smith 
74d8d19677SJose E. Roman   Input Parameters:
75811af0c4SBarry Smith + sp  - the scatter plot context.
76811af0c4SBarry Smith - dim - the number of point curves on this process
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith   Level: intermediate
795c6c1daeSBarry Smith 
80db781477SPatrick Sanan .seealso: `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`
815c6c1daeSBarry Smith @*/
82d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPSetDimension(PetscDrawSP sp, int dim)
83d71ae5a4SJacob Faibussowitsch {
845c6c1daeSBarry Smith   PetscFunctionBegin;
855c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
863ba16761SJacob Faibussowitsch   if (sp->dim == dim) PetscFunctionReturn(PETSC_SUCCESS);
875c6c1daeSBarry Smith   sp->dim = dim;
88f98b2f00SMatthew G. Knepley   PetscCall(PetscFree3(sp->x, sp->y, sp->z));
89f98b2f00SMatthew G. Knepley   PetscCall(PetscMalloc3(dim * PETSC_DRAW_SP_CHUNK_SIZE, &sp->x, dim * PETSC_DRAW_SP_CHUNK_SIZE, &sp->y, dim * PETSC_DRAW_SP_CHUNK_SIZE, &sp->z));
90999739cfSJacob Faibussowitsch   sp->len = dim * PETSC_DRAW_SP_CHUNK_SIZE;
913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
925c6c1daeSBarry Smith }
935c6c1daeSBarry Smith 
945c6c1daeSBarry Smith /*@
95811af0c4SBarry Smith   PetscDrawSPGetDimension - Get the number of sets of points that are to be drawn at each `PetscDrawSPAddPoint()`
96f98b2f00SMatthew G. Knepley 
9720f4b53cSBarry Smith   Not Collective
98f98b2f00SMatthew G. Knepley 
9920f4b53cSBarry Smith   Input Parameter:
100811af0c4SBarry Smith . sp  - the scatter plot context.
101f98b2f00SMatthew G. Knepley 
102f98b2f00SMatthew G. Knepley   Output Parameter:
103811af0c4SBarry Smith . dim - the number of point curves on this process
104f98b2f00SMatthew G. Knepley 
105f98b2f00SMatthew G. Knepley   Level: intermediate
106f98b2f00SMatthew G. Knepley 
107db781477SPatrick Sanan .seealso: `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`
108f98b2f00SMatthew G. Knepley @*/
109d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPGetDimension(PetscDrawSP sp, int *dim)
110d71ae5a4SJacob Faibussowitsch {
111f98b2f00SMatthew G. Knepley   PetscFunctionBegin;
112f98b2f00SMatthew G. Knepley   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
113f98b2f00SMatthew G. Knepley   PetscValidPointer(dim, 2);
114f98b2f00SMatthew G. Knepley   *dim = sp->dim;
1153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
116f98b2f00SMatthew G. Knepley }
117f98b2f00SMatthew G. Knepley 
118f98b2f00SMatthew G. Knepley /*@
119811af0c4SBarry Smith   PetscDrawSPReset - Clears scatter plot to allow for reuse with new data.
1205c6c1daeSBarry Smith 
12120f4b53cSBarry Smith   Not Collective
1225c6c1daeSBarry Smith 
1235c6c1daeSBarry Smith   Input Parameter:
124811af0c4SBarry Smith . sp - the scatter plot context.
1255c6c1daeSBarry Smith 
1265c6c1daeSBarry Smith   Level: intermediate
1275c6c1daeSBarry Smith 
128db781477SPatrick Sanan .seealso: `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`, `PetscDrawSPDraw()`
1295c6c1daeSBarry Smith @*/
130d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPReset(PetscDrawSP sp)
131d71ae5a4SJacob Faibussowitsch {
1325c6c1daeSBarry Smith   PetscFunctionBegin;
1335c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
1345c6c1daeSBarry Smith   sp->xmin  = 1.e20;
1355c6c1daeSBarry Smith   sp->ymin  = 1.e20;
1368c87cf4dSdanfinn   sp->zmin  = 1.e20;
1375c6c1daeSBarry Smith   sp->xmax  = -1.e20;
1385c6c1daeSBarry Smith   sp->ymax  = -1.e20;
1398c87cf4dSdanfinn   sp->zmax  = -1.e20;
1405c6c1daeSBarry Smith   sp->loc   = 0;
1415c6c1daeSBarry Smith   sp->nopts = 0;
1423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1435c6c1daeSBarry Smith }
1445c6c1daeSBarry Smith 
145f98b2f00SMatthew G. Knepley /*@
1465c6c1daeSBarry Smith   PetscDrawSPDestroy - Frees all space taken up by scatter plot data structure.
1475c6c1daeSBarry Smith 
148c3339decSBarry Smith   Collective
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith   Input Parameter:
151811af0c4SBarry Smith . sp - the scatter plot context
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith   Level: intermediate
1545c6c1daeSBarry Smith 
155db781477SPatrick Sanan .seealso: `PetscDrawSPCreate()`, `PetscDrawSP`, `PetscDrawSPReset()`
1565c6c1daeSBarry Smith @*/
157d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPDestroy(PetscDrawSP *sp)
158d71ae5a4SJacob Faibussowitsch {
1595c6c1daeSBarry Smith   PetscFunctionBegin;
1603ba16761SJacob Faibussowitsch   if (!*sp) PetscFunctionReturn(PETSC_SUCCESS);
161e118a51fSLisandro Dalcin   PetscValidHeaderSpecific(*sp, PETSC_DRAWSP_CLASSID, 1);
1629371c9d4SSatish Balay   if (--((PetscObject)(*sp))->refct > 0) {
1639371c9d4SSatish Balay     *sp = NULL;
1643ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1659371c9d4SSatish Balay   }
1665c6c1daeSBarry Smith 
1679566063dSJacob Faibussowitsch   PetscCall(PetscFree3((*sp)->x, (*sp)->y, (*sp)->z));
1689566063dSJacob Faibussowitsch   PetscCall(PetscDrawAxisDestroy(&(*sp)->axis));
1699566063dSJacob Faibussowitsch   PetscCall(PetscDrawDestroy(&(*sp)->win));
1709566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(sp));
1713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1725c6c1daeSBarry Smith }
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith /*@
175811af0c4SBarry Smith   PetscDrawSPAddPoint - Adds another point to each of the scatter plot point curves.
1765c6c1daeSBarry Smith 
17720f4b53cSBarry Smith   Not Collective
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   Input Parameters:
1805c6c1daeSBarry Smith + sp - the scatter plot data structure
181811af0c4SBarry Smith - x, y - two arrays of length dim containing the new x and y coordinate values for each of the point curves. Here  dim is the number of point curves passed to PetscDrawSPCreate()
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith   Level: intermediate
1845c6c1daeSBarry Smith 
185811af0c4SBarry Smith   Note:
186811af0c4SBarry Smith   The new points will not be displayed until a call to `PetscDrawSPDraw()` is made
1870afdd333SBarry Smith 
188db781477SPatrick Sanan .seealso: `PetscDrawSPAddPoints()`, `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPReset()`, `PetscDrawSPDraw()`, `PetscDrawSPAddPointColorized()`
1895c6c1daeSBarry Smith @*/
190d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPAddPoint(PetscDrawSP sp, PetscReal *x, PetscReal *y)
191d71ae5a4SJacob Faibussowitsch {
1925c6c1daeSBarry Smith   PetscInt i;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   PetscFunctionBegin;
1955c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
196e118a51fSLisandro Dalcin 
1975c6c1daeSBarry Smith   if (sp->loc + sp->dim >= sp->len) { /* allocate more space */
198f98b2f00SMatthew G. Knepley     PetscReal *tmpx, *tmpy, *tmpz;
199f98b2f00SMatthew G. Knepley     PetscCall(PetscMalloc3(sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpx, sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpy, sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpz));
2009566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpx, sp->x, sp->len));
2019566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpy, sp->y, sp->len));
202f98b2f00SMatthew G. Knepley     PetscCall(PetscArraycpy(tmpz, sp->z, sp->len));
203f98b2f00SMatthew G. Knepley     PetscCall(PetscFree3(sp->x, sp->y, sp->z));
2045c6c1daeSBarry Smith     sp->x = tmpx;
2055c6c1daeSBarry Smith     sp->y = tmpy;
206f98b2f00SMatthew G. Knepley     sp->z = tmpz;
207999739cfSJacob Faibussowitsch     sp->len += sp->dim * PETSC_DRAW_SP_CHUNK_SIZE;
2085c6c1daeSBarry Smith   }
209f98b2f00SMatthew G. Knepley   for (i = 0; i < sp->dim; ++i) {
2105c6c1daeSBarry Smith     if (x[i] > sp->xmax) sp->xmax = x[i];
2115c6c1daeSBarry Smith     if (x[i] < sp->xmin) sp->xmin = x[i];
2125c6c1daeSBarry Smith     if (y[i] > sp->ymax) sp->ymax = y[i];
2135c6c1daeSBarry Smith     if (y[i] < sp->ymin) sp->ymin = y[i];
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith     sp->x[sp->loc]   = x[i];
2165c6c1daeSBarry Smith     sp->y[sp->loc++] = y[i];
2175c6c1daeSBarry Smith   }
218f98b2f00SMatthew G. Knepley   ++sp->nopts;
2193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2205c6c1daeSBarry Smith }
2215c6c1daeSBarry Smith 
2225c6c1daeSBarry Smith /*@C
223811af0c4SBarry Smith   PetscDrawSPAddPoints - Adds several points to each of the scatter plot point curves.
2245c6c1daeSBarry Smith 
22520f4b53cSBarry Smith   Not Collective
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith   Input Parameters:
228811af0c4SBarry Smith + sp - the scatter plot context
2292fe279fdSBarry Smith . xx - array of pointers that point to arrays containing the new x coordinates for each curve.
2302fe279fdSBarry Smith . yy - array of pointers that point to arrays containing the new y points for each curve.
231811af0c4SBarry Smith - n - number of points being added, each represents a subarray of length dim where dim is the value from `PetscDrawSPGetDimension()`
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith   Level: intermediate
2345c6c1daeSBarry Smith 
235811af0c4SBarry Smith   Note:
236811af0c4SBarry Smith   The new points will not be displayed until a call to `PetscDrawSPDraw()` is made
2370afdd333SBarry Smith 
238db781477SPatrick Sanan .seealso: `PetscDrawSPAddPoint()`, `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPReset()`, `PetscDrawSPDraw()`, `PetscDrawSPAddPointColorized()`
2395c6c1daeSBarry Smith @*/
240d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPAddPoints(PetscDrawSP sp, int n, PetscReal **xx, PetscReal **yy)
241d71ae5a4SJacob Faibussowitsch {
2425c6c1daeSBarry Smith   PetscInt   i, j, k;
2435c6c1daeSBarry Smith   PetscReal *x, *y;
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith   PetscFunctionBegin;
2465c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith   if (sp->loc + n * sp->dim >= sp->len) { /* allocate more space */
249f98b2f00SMatthew G. Knepley     PetscReal *tmpx, *tmpy, *tmpz;
250999739cfSJacob Faibussowitsch     PetscInt   chunk = PETSC_DRAW_SP_CHUNK_SIZE;
2515c6c1daeSBarry Smith     if (n > chunk) chunk = n;
252f98b2f00SMatthew G. Knepley     PetscCall(PetscMalloc3(sp->len + sp->dim * chunk, &tmpx, sp->len + sp->dim * chunk, &tmpy, sp->len + sp->dim * chunk, &tmpz));
2539566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpx, sp->x, sp->len));
2549566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpy, sp->y, sp->len));
255f98b2f00SMatthew G. Knepley     PetscCall(PetscArraycpy(tmpz, sp->z, sp->len));
256f98b2f00SMatthew G. Knepley     PetscCall(PetscFree3(sp->x, sp->y, sp->z));
257a297a907SKarl Rupp 
2585c6c1daeSBarry Smith     sp->x = tmpx;
2595c6c1daeSBarry Smith     sp->y = tmpy;
260f98b2f00SMatthew G. Knepley     sp->z = tmpz;
261999739cfSJacob Faibussowitsch     sp->len += sp->dim * PETSC_DRAW_SP_CHUNK_SIZE;
2625c6c1daeSBarry Smith   }
263f98b2f00SMatthew G. Knepley   for (j = 0; j < sp->dim; ++j) {
2649371c9d4SSatish Balay     x = xx[j];
2659371c9d4SSatish Balay     y = yy[j];
2665c6c1daeSBarry Smith     k = sp->loc + j;
267f98b2f00SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2685c6c1daeSBarry Smith       if (x[i] > sp->xmax) sp->xmax = x[i];
2695c6c1daeSBarry Smith       if (x[i] < sp->xmin) sp->xmin = x[i];
2705c6c1daeSBarry Smith       if (y[i] > sp->ymax) sp->ymax = y[i];
2715c6c1daeSBarry Smith       if (y[i] < sp->ymin) sp->ymin = y[i];
2725c6c1daeSBarry Smith 
2735c6c1daeSBarry Smith       sp->x[k] = x[i];
2745c6c1daeSBarry Smith       sp->y[k] = y[i];
2755c6c1daeSBarry Smith       k += sp->dim;
2765c6c1daeSBarry Smith     }
2775c6c1daeSBarry Smith   }
2785c6c1daeSBarry Smith   sp->loc += n * sp->dim;
2795c6c1daeSBarry Smith   sp->nopts += n;
2803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2815c6c1daeSBarry Smith }
2825c6c1daeSBarry Smith 
2835c6c1daeSBarry Smith /*@
2848c87cf4dSdanfinn   PetscDrawSPAddPointColorized - Adds another point to each of the scatter plots as well as a numeric value to be used to colorize the scatter point.
2858c87cf4dSdanfinn 
28620f4b53cSBarry Smith   Not Collective
2878c87cf4dSdanfinn 
2888c87cf4dSdanfinn   Input Parameters:
2898c87cf4dSdanfinn + sp - the scatter plot data structure
2902fe279fdSBarry Smith . x - array of length dim containing the new x coordinate values for each of the point curves.
2912fe279fdSBarry Smith . y - array of length dim containing the new y coordinate values for each of the point curves.
2928c87cf4dSdanfinn - z - array of length dim containing the numeric values that will be mapped to [0,255] and used for scatter point colors.
2938c87cf4dSdanfinn 
2948c87cf4dSdanfinn   Level: intermediate
2958c87cf4dSdanfinn 
296811af0c4SBarry Smith   Note:
2972fe279fdSBarry Smith   The dimensions of the arrays is the number of point curves passed to `PetscDrawSPCreate()`.
298811af0c4SBarry Smith   The new points will not be displayed until a call to `PetscDrawSPDraw()` is made
2998c87cf4dSdanfinn 
300db781477SPatrick Sanan .seealso: `PetscDrawSPAddPoints()`, `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPReset()`, `PetscDrawSPDraw()`, `PetscDrawSPAddPoint()`
3018c87cf4dSdanfinn @*/
302d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPAddPointColorized(PetscDrawSP sp, PetscReal *x, PetscReal *y, PetscReal *z)
303d71ae5a4SJacob Faibussowitsch {
3048c87cf4dSdanfinn   PetscInt i;
3058c87cf4dSdanfinn 
3068c87cf4dSdanfinn   PetscFunctionBegin;
3078c87cf4dSdanfinn   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
3088c87cf4dSdanfinn   sp->colorized = PETSC_TRUE;
3098c87cf4dSdanfinn   if (sp->loc + sp->dim >= sp->len) { /* allocate more space */
3108c87cf4dSdanfinn     PetscReal *tmpx, *tmpy, *tmpz;
3119566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpx, sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpy, sp->len + sp->dim * PETSC_DRAW_SP_CHUNK_SIZE, &tmpz));
3129566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpx, sp->x, sp->len));
3139566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpy, sp->y, sp->len));
3149566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(tmpz, sp->z, sp->len));
3159566063dSJacob Faibussowitsch     PetscCall(PetscFree3(sp->x, sp->y, sp->z));
3168c87cf4dSdanfinn     sp->x = tmpx;
3178c87cf4dSdanfinn     sp->y = tmpy;
3188c87cf4dSdanfinn     sp->z = tmpz;
3198c87cf4dSdanfinn     sp->len += sp->dim * PETSC_DRAW_SP_CHUNK_SIZE;
3208c87cf4dSdanfinn   }
321f98b2f00SMatthew G. Knepley   for (i = 0; i < sp->dim; ++i) {
3228c87cf4dSdanfinn     if (x[i] > sp->xmax) sp->xmax = x[i];
3238c87cf4dSdanfinn     if (x[i] < sp->xmin) sp->xmin = x[i];
3248c87cf4dSdanfinn     if (y[i] > sp->ymax) sp->ymax = y[i];
3258c87cf4dSdanfinn     if (y[i] < sp->ymin) sp->ymin = y[i];
3268c87cf4dSdanfinn     if (z[i] < sp->zmin) sp->zmin = z[i];
3278c87cf4dSdanfinn     if (z[i] > sp->zmax) sp->zmax = z[i];
3288c87cf4dSdanfinn     // if (z[i] > sp->zmax && z[i] < 5.) sp->zmax = z[i];
3298c87cf4dSdanfinn 
3308c87cf4dSdanfinn     sp->x[sp->loc]   = x[i];
3318c87cf4dSdanfinn     sp->y[sp->loc]   = y[i];
3328c87cf4dSdanfinn     sp->z[sp->loc++] = z[i];
3338c87cf4dSdanfinn   }
334f98b2f00SMatthew G. Knepley   ++sp->nopts;
3353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3368c87cf4dSdanfinn }
3378c87cf4dSdanfinn 
3388c87cf4dSdanfinn /*@
3395c6c1daeSBarry Smith   PetscDrawSPDraw - Redraws a scatter plot.
3405c6c1daeSBarry Smith 
341c3339decSBarry Smith   Collective
3425c6c1daeSBarry Smith 
343d8d19677SJose E. Roman   Input Parameters:
344811af0c4SBarry Smith + sp - the scatter plot context
3455c6c1daeSBarry Smith - clear - clear the window before drawing the new plot
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith   Level: intermediate
3485c6c1daeSBarry Smith 
349db781477SPatrick Sanan .seealso: `PetscDrawLGDraw()`, `PetscDrawLGSPDraw()`, `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPReset()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`
3505c6c1daeSBarry Smith @*/
351d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPDraw(PetscDrawSP sp, PetscBool clear)
352d71ae5a4SJacob Faibussowitsch {
353e118a51fSLisandro Dalcin   PetscDraw   draw;
354f98b2f00SMatthew G. Knepley   PetscBool   isnull;
355f98b2f00SMatthew G. Knepley   PetscMPIInt rank, size;
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith   PetscFunctionBegin;
3585c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
359f98b2f00SMatthew G. Knepley   draw = sp->win;
360f98b2f00SMatthew G. Knepley   PetscCall(PetscDrawIsNull(draw, &isnull));
3613ba16761SJacob Faibussowitsch   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
3629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)sp), &rank));
363f98b2f00SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)sp), &size));
364e118a51fSLisandro Dalcin 
3655c6c1daeSBarry Smith   if (clear) {
3669566063dSJacob Faibussowitsch     PetscCall(PetscDrawCheckResizedWindow(draw));
3679566063dSJacob Faibussowitsch     PetscCall(PetscDrawClear(draw));
3685c6c1daeSBarry Smith   }
369f98b2f00SMatthew G. Knepley   {
370f98b2f00SMatthew G. Knepley     PetscReal lower[2] = {sp->xmin, sp->ymin}, glower[2];
371f98b2f00SMatthew G. Knepley     PetscReal upper[2] = {sp->xmax, sp->ymax}, gupper[2];
372f98b2f00SMatthew G. Knepley     PetscCall(MPIU_Allreduce(lower, glower, 2, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)sp)));
373f98b2f00SMatthew G. Knepley     PetscCall(MPIU_Allreduce(upper, gupper, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)sp)));
374f98b2f00SMatthew G. Knepley     PetscCall(PetscDrawAxisSetLimits(sp->axis, glower[0], gupper[0], glower[1], gupper[1]));
3759566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisDraw(sp->axis));
376f98b2f00SMatthew G. Knepley   }
3775c6c1daeSBarry Smith 
378d0609cedSBarry Smith   PetscDrawCollectiveBegin(draw);
379f98b2f00SMatthew G. Knepley   {
380f98b2f00SMatthew G. Knepley     const int dim = sp->dim, nopts = sp->nopts;
381f98b2f00SMatthew G. Knepley 
382f98b2f00SMatthew G. Knepley     for (int i = 0; i < dim; ++i) {
383f98b2f00SMatthew G. Knepley       for (int p = 0; p < nopts; ++p) {
384f98b2f00SMatthew G. Knepley         PetscInt color = sp->colorized ? PetscDrawRealToColor(sp->z[p * dim], sp->zmin, sp->zmax) : (size > 1 ? PetscDrawRealToColor(rank, 0, size - 1) : PETSC_DRAW_RED);
385f98b2f00SMatthew G. Knepley 
386f98b2f00SMatthew G. Knepley         PetscCall(PetscDrawPoint(draw, sp->x[p * dim + i], sp->y[p * dim + i], color));
3875c6c1daeSBarry Smith       }
3885c6c1daeSBarry Smith     }
3898c87cf4dSdanfinn   }
390d0609cedSBarry Smith   PetscDrawCollectiveEnd(draw);
3915b399a63SLisandro Dalcin 
3929566063dSJacob Faibussowitsch   PetscCall(PetscDrawFlush(draw));
3939566063dSJacob Faibussowitsch   PetscCall(PetscDrawPause(draw));
3943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3955c6c1daeSBarry Smith }
3965c6c1daeSBarry Smith 
39757fd6651SLisandro Dalcin /*@
39857fd6651SLisandro Dalcin   PetscDrawSPSave - Saves a drawn image
39957fd6651SLisandro Dalcin 
400c3339decSBarry Smith   Collective
40157fd6651SLisandro Dalcin 
40257fd6651SLisandro Dalcin   Input Parameter:
40357fd6651SLisandro Dalcin . sp - the scatter plot context
40457fd6651SLisandro Dalcin 
40557fd6651SLisandro Dalcin   Level: intermediate
40657fd6651SLisandro Dalcin 
407811af0c4SBarry Smith .seealso: `PetscDrawSPSave()`, `PetscDrawSPCreate()`, `PetscDrawSPGetDraw()`, `PetscDrawSetSave()`, `PetscDrawSave()`
40857fd6651SLisandro Dalcin @*/
409d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPSave(PetscDrawSP sp)
410d71ae5a4SJacob Faibussowitsch {
41157fd6651SLisandro Dalcin   PetscFunctionBegin;
41257fd6651SLisandro Dalcin   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
4139566063dSJacob Faibussowitsch   PetscCall(PetscDrawSave(sp->win));
4143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41557fd6651SLisandro Dalcin }
41657fd6651SLisandro Dalcin 
4175c6c1daeSBarry Smith /*@
418f98b2f00SMatthew G. Knepley   PetscDrawSPSetLimits - Sets the axis limits for a scatter plot. If more points are added after this call, the limits will be adjusted to include those additional points.
4195c6c1daeSBarry Smith 
42020f4b53cSBarry Smith   Not Collective
4215c6c1daeSBarry Smith 
4225c6c1daeSBarry Smith   Input Parameters:
4235c6c1daeSBarry Smith + xsp - the line graph context
4242fe279fdSBarry Smith . x_min - the horizontal lower limit
425*aaa8cc7dSPierre Jolivet . x_max - the horizontal upper limit
4262fe279fdSBarry Smith . y_min - the vertical lower limit
4272fe279fdSBarry Smith - y_max - the vertical upper limit
4285c6c1daeSBarry Smith 
4295c6c1daeSBarry Smith   Level: intermediate
4305c6c1daeSBarry Smith 
431811af0c4SBarry Smith .seealso: `PetscDrawSP`, `PetscDrawAxis`, `PetscDrawSPCreate()`, `PetscDrawSPDraw()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`, `PetscDrawSPGetAxis()`
4325c6c1daeSBarry Smith @*/
433d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPSetLimits(PetscDrawSP sp, PetscReal x_min, PetscReal x_max, PetscReal y_min, PetscReal y_max)
434d71ae5a4SJacob Faibussowitsch {
4355c6c1daeSBarry Smith   PetscFunctionBegin;
4365c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
4375c6c1daeSBarry Smith   sp->xmin = x_min;
4385c6c1daeSBarry Smith   sp->xmax = x_max;
4395c6c1daeSBarry Smith   sp->ymin = y_min;
4405c6c1daeSBarry Smith   sp->ymax = y_max;
4413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4425c6c1daeSBarry Smith }
4435c6c1daeSBarry Smith 
444f98b2f00SMatthew G. Knepley /*@
445811af0c4SBarry Smith   PetscDrawSPGetAxis - Gets the axis context associated with a scatter plot
4465c6c1daeSBarry Smith 
447f98b2f00SMatthew G. Knepley   Not Collective
4485c6c1daeSBarry Smith 
4495c6c1daeSBarry Smith   Input Parameter:
450811af0c4SBarry Smith . sp - the scatter plot context
4515c6c1daeSBarry Smith 
4525c6c1daeSBarry Smith   Output Parameter:
4535c6c1daeSBarry Smith . axis - the axis context
4545c6c1daeSBarry Smith 
45520f4b53cSBarry Smith   Level: intermediate
45620f4b53cSBarry Smith 
457f98b2f00SMatthew G. Knepley   Note:
458f98b2f00SMatthew G. Knepley   This is useful if one wants to change some axis property, such as labels, color, etc. The axis context should not be destroyed by the application code.
459f98b2f00SMatthew G. Knepley 
460db781477SPatrick Sanan .seealso: `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPDraw()`, `PetscDrawSPAddPoint()`, `PetscDrawSPAddPoints()`, `PetscDrawAxis`, `PetscDrawAxisCreate()`
4615c6c1daeSBarry Smith @*/
462d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPGetAxis(PetscDrawSP sp, PetscDrawAxis *axis)
463d71ae5a4SJacob Faibussowitsch {
4645c6c1daeSBarry Smith   PetscFunctionBegin;
4655c6c1daeSBarry Smith   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
46645f3bb6eSLisandro Dalcin   PetscValidPointer(axis, 2);
4675c6c1daeSBarry Smith   *axis = sp->axis;
4683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4695c6c1daeSBarry Smith }
4705c6c1daeSBarry Smith 
471f98b2f00SMatthew G. Knepley /*@
472811af0c4SBarry Smith   PetscDrawSPGetDraw - Gets the draw context associated with a scatter plot
4735c6c1daeSBarry Smith 
474f98b2f00SMatthew G. Knepley   Not Collective
4755c6c1daeSBarry Smith 
4765c6c1daeSBarry Smith   Input Parameter:
477811af0c4SBarry Smith . sp - the scatter plot context
4785c6c1daeSBarry Smith 
4795c6c1daeSBarry Smith   Output Parameter:
4805c6c1daeSBarry Smith . draw - the draw context
4815c6c1daeSBarry Smith 
4825c6c1daeSBarry Smith   Level: intermediate
4835c6c1daeSBarry Smith 
484db781477SPatrick Sanan .seealso: `PetscDrawSP`, `PetscDrawSPCreate()`, `PetscDrawSPDraw()`, `PetscDraw`
4855c6c1daeSBarry Smith @*/
486d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDrawSPGetDraw(PetscDrawSP sp, PetscDraw *draw)
487d71ae5a4SJacob Faibussowitsch {
4885c6c1daeSBarry Smith   PetscFunctionBegin;
489e118a51fSLisandro Dalcin   PetscValidHeaderSpecific(sp, PETSC_DRAWSP_CLASSID, 1);
49045f3bb6eSLisandro Dalcin   PetscValidPointer(draw, 2);
491e118a51fSLisandro Dalcin   *draw = sp->win;
4923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4935c6c1daeSBarry Smith }
494