1 2 /* 3 Provides the calling sequences for all the basic PetscDraw routines. 4 */ 5 #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/ 6 7 /*@ 8 PetscDrawTriangle - PetscDraws a triangle onto a drawable. 9 10 Not Collective 11 12 Input Parameters: 13 + draw - the drawing context 14 . x1,y1,x2,y2,x3,y3 - the coordinates of the vertices 15 - c1,c2,c3 - the colors of the three vertices in the same order as the xi,yi 16 17 Level: beginner 18 19 .seealso: `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawEllipse()`, `PetscDrawMarker()`, `PetscDrawPoint()`, `PetscDrawArrow()` 20 @*/ 21 PetscErrorCode PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3,int c1,int c2,int c3) 22 { 23 PetscFunctionBegin; 24 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 25 PetscUseTypeMethod(draw,triangle ,x1,y_1,x2,y2,x3,y3,c1,c2,c3); 26 PetscFunctionReturn(0); 27 } 28 29 /*@ 30 PetscDrawScalePopup - draws a contour scale window. 31 32 Collective on PetscDraw 33 34 Input Parameters: 35 + popup - the window (often a window obtained via PetscDrawGetPopup() 36 . min - minimum value being plotted 37 - max - maximum value being plotted 38 39 Level: intermediate 40 41 Notes: 42 All processors that share the draw MUST call this routine 43 44 .seealso: `PetscDrawGetPopup()`, `PetscDrawTensorContour()` 45 46 @*/ 47 PetscErrorCode PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max) 48 { 49 PetscBool isnull; 50 PetscReal xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0; 51 PetscMPIInt rank; 52 int i; 53 char string[32]; 54 55 PetscFunctionBegin; 56 if (!popup) PetscFunctionReturn(0); 57 PetscValidHeaderSpecific(popup,PETSC_DRAW_CLASSID,1); 58 PetscCall(PetscDrawIsNull(popup,&isnull)); 59 if (isnull) PetscFunctionReturn(0); 60 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)popup),&rank)); 61 62 PetscCall(PetscDrawCheckResizedWindow(popup)); 63 PetscCall(PetscDrawClear(popup)); 64 PetscCall(PetscDrawSetTitle(popup,"Contour Scale")); 65 PetscCall(PetscDrawSetCoordinates(popup,xl,yl,xr,yr)); 66 PetscDrawCollectiveBegin(popup); 67 if (rank == 0) { 68 for (i=0; i<10; i++) { 69 int c = PetscDrawRealToColor((PetscReal)i/9,0,1); 70 PetscCall(PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c)); 71 yl += 0.1; 72 } 73 for (i=0; i<10; i++) { 74 PetscReal value = min + i*(max-min)/9; 75 /* look for a value that should be zero, but is not due to round-off */ 76 if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0; 77 PetscCall(PetscSNPrintf(string,sizeof(string),"%18.16e",(double)value)); 78 PetscCall(PetscDrawString(popup,0.2,0.02+i/10.0,PETSC_DRAW_BLACK,string)); 79 } 80 } 81 PetscDrawCollectiveEnd(popup); 82 PetscCall(PetscDrawFlush(popup)); 83 PetscCall(PetscDrawSave(popup)); 84 PetscFunctionReturn(0); 85 } 86 87 typedef struct { 88 int m,n; 89 PetscReal *x,*y,min,max,*v; 90 PetscBool showgrid; 91 } ZoomCtx; 92 93 static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx) 94 { 95 int i; 96 ZoomCtx *ctx = (ZoomCtx*)dctx; 97 98 PetscFunctionBegin; 99 PetscCall(PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->min,ctx->max,ctx->v)); 100 if (ctx->showgrid) { 101 for (i=0; i<ctx->m; i++) { 102 PetscCall(PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK)); 103 } 104 for (i=0; i<ctx->n; i++) { 105 PetscCall(PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK)); 106 } 107 } 108 PetscFunctionReturn(0); 109 } 110 111 /*@C 112 PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array 113 that is stored as a PETSc vector. 114 115 Collective on PetscDraw, but PetscDraw must be sequential 116 117 Input Parameters: 118 + draw - the draw context 119 . m,n - the global number of mesh points in the x and y directions 120 . xi,yi - the locations of the global mesh points (optional, use NULL 121 to indicate uniform spacing on [0,1]) 122 - V - the values 123 124 Options Database Keys: 125 + -draw_x_shared_colormap - Indicates use of private colormap 126 - -draw_contour_grid - PetscDraws grid contour 127 128 Level: intermediate 129 130 .seealso: `PetscDrawTensorContourPatch()`, `PetscDrawScalePopup()` 131 132 @*/ 133 PetscErrorCode PetscDrawTensorContour(PetscDraw draw,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v) 134 { 135 int N = m*n; 136 PetscBool isnull; 137 PetscDraw popup; 138 int xin=1,yin=1,i; 139 PetscMPIInt size; 140 PetscReal h; 141 ZoomCtx ctx; 142 143 PetscFunctionBegin; 144 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 145 PetscCall(PetscDrawIsNull(draw,&isnull)); 146 if (isnull) PetscFunctionReturn(0); 147 PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size)); 148 PetscCheck(size <= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw"); 149 PetscCheck(N > 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n); 150 151 ctx.v = v; 152 ctx.m = m; 153 ctx.n = n; 154 ctx.max = ctx.min = v[0]; 155 for (i=0; i<N; i++) { 156 if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i]; 157 if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i]; 158 } 159 if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;} 160 161 /* PetscDraw the scale window */ 162 PetscCall(PetscDrawGetPopup(draw,&popup)); 163 PetscCall(PetscDrawScalePopup(popup,ctx.min,ctx.max)); 164 165 ctx.showgrid = PETSC_FALSE; 166 PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_contour_grid",&ctx.showgrid,NULL)); 167 168 /* fill up x and y coordinates */ 169 if (!xi) { 170 xin = 0; 171 PetscCall(PetscMalloc1(ctx.m,&ctx.x)); 172 h = 1.0/(ctx.m-1); 173 ctx.x[0] = 0.0; 174 for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h; 175 } else ctx.x = (PetscReal*)xi; 176 177 if (!yi) { 178 yin = 0; 179 PetscCall(PetscMalloc1(ctx.n,&ctx.y)); 180 h = 1.0/(ctx.n-1); 181 ctx.y[0] = 0.0; 182 for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h; 183 } else ctx.y = (PetscReal*)yi; 184 185 PetscCall(PetscDrawZoom(draw,PetscDrawTensorContour_Zoom,&ctx)); 186 187 if (!xin) PetscCall(PetscFree(ctx.x)); 188 if (!yin) PetscCall(PetscFree(ctx.y)); 189 PetscFunctionReturn(0); 190 } 191 192 /*@ 193 PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot 194 for a two-dimensional array. 195 196 Not Collective 197 198 Input Parameters: 199 + draw - the draw context 200 . m,n - the number of local mesh points in the x and y direction 201 . x,y - the locations of the local mesh points 202 . min,max - the minimum and maximum value in the entire contour 203 - v - the data 204 205 Options Database Keys: 206 . -draw_x_shared_colormap - Activates private colormap 207 208 Level: advanced 209 210 Note: 211 This is a lower level support routine, usually the user will call 212 PetscDrawTensorContour(). 213 214 .seealso: `PetscDrawTensorContour()` 215 216 @*/ 217 PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal min,PetscReal max,PetscReal *v) 218 { 219 int c1,c2,c3,c4,i,j; 220 PetscReal x1,x2,x3,x4,y_1,y2,y3,y4; 221 222 PetscFunctionBegin; 223 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 224 /* PetscDraw the contour plot patch */ 225 for (j=0; j<n-1; j++) { 226 for (i=0; i<m-1; i++) { 227 x1 = x[i]; y_1 = y[j]; c1 = PetscDrawRealToColor(v[i+j*m],min,max); 228 x2 = x[i+1]; y2 = y_1; c2 = PetscDrawRealToColor(v[i+j*m+1],min,max); 229 x3 = x2; y3 = y[j+1]; c3 = PetscDrawRealToColor(v[i+j*m+1+m],min,max); 230 x4 = x1; y4 = y3; c4 = PetscDrawRealToColor(v[i+j*m+m],min,max); 231 232 PetscCall(PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3)); 233 PetscCall(PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4)); 234 } 235 } 236 PetscFunctionReturn(0); 237 } 238