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