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