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