Actual source code: dtri.c

      1: #define PETSC_DLL
      2: /*
      3:        Provides the calling sequences for all the basic PetscDraw routines.
      4: */
     5:  #include src/sys/src/draw/drawimpl.h

      9: /*@
     10:    PetscDrawTriangle - PetscDraws a triangle  onto a drawable.

     12:    Not Collective

     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

     19:    Level: beginner

     21:    Concepts: drawing^triangle
     22:    Concepts: graphics^triangle
     23:    Concepts: triangle

     25: @*/
     26: PetscErrorCode PETSC_DLLEXPORT PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3,
     27:                  int c1,int c2,int c3)
     28: {
     30:   PetscTruth isnull;

     34:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
     35:   if (isnull) return(0);
     36:   (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
     37:   return(0);
     38: }

     43: /*@
     44:        PetscDrawScalePopup - PetscDraws a contour scale window.

     46:      Collective on PetscDraw

     48:   Input Parameters:
     49: +    popup - the window (often a window obtained via PetscDrawGetPopup()
     50: .    min - minimum value being plotted
     51: -    max - maximum value being plotted

     53:   Level: intermediate

     55:   Notes:
     56:      All processors that share the draw MUST call this routine

     58: @*/
     59: PetscErrorCode PETSC_DLLEXPORT PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
     60: {
     61:   PetscReal xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0,value;
     63:   int       i,c = PETSC_DRAW_BASIC_COLORS,rank;
     64:   char      string[32];
     65:   MPI_Comm  comm;

     68:   PetscDrawCheckResizedWindow(popup);
     69:   PetscObjectGetComm((PetscObject)popup,&comm);
     70:   MPI_Comm_rank(comm,&rank);
     71:   if (rank) return(0);

     73:   for (i=0; i<10; i++) {
     74:     PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);
     75:     yl += .1; yr += .1; c = (int)((double)c + (245. - PETSC_DRAW_BASIC_COLORS)/9.);
     76:   }
     77:   for (i=0; i<10; i++) {
     78:     value = min + i*(max-min)/9.0;
     79:     /* look for a value that should be zero, but is not due to round-off */
     80:     if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
     81:     sprintf(string,"%g",value);
     82:     PetscDrawString(popup,.2,.02 + i/10.0,PETSC_DRAW_BLACK,string);
     83:   }
     84:   PetscDrawSetTitle(popup,"Contour Scale");
     85:   PetscDrawFlush(popup);
     86:   return(0);
     87: }

     89: typedef struct {
     90:   int        m,n;
     91:   PetscReal  *x,*y,min,max,*v;
     92:   PetscTruth showgrid;
     93: } ZoomCtx;

     97: static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
     98: {
    100:   int     i;
    101:   ZoomCtx *ctx = (ZoomCtx*)dctx;

    104:   PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->max,ctx->min,ctx->v);
    105:   if (ctx->showgrid) {
    106:     for (i=0; i<ctx->m; i++) {
    107:       PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);
    108:     }
    109:     for (i=0; i<ctx->n; i++) {
    110:       PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);
    111:     }
    112:   }
    113:   return(0);
    114: }

    118: /*@C
    119:    PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array
    120:    that is stored as a PETSc vector.

    122:    Collective on PetscDraw, but PetscDraw must be sequential

    124:    Input Parameters:
    125: +   win   - the window to draw in
    126: .   m,n   - the global number of mesh points in the x and y directions
    127: .   xi,yi - the locations of the global mesh points (optional, use PETSC_NULL
    128:             to indicate uniform spacing on [0,1])
    129: -   V     - the values

    131:    Options Database Keys:
    132: +  -draw_x_shared_colormap - Indicates use of private colormap
    133: -  -draw_contour_grid - PetscDraws grid contour

    135:    Level: intermediate

    137:    Concepts: contour plot
    138:    Concepts: drawing^contour plot

    140: .seealso: PetscDrawTensorContourPatch()

    142: @*/
    143: PetscErrorCode PETSC_DLLEXPORT PetscDrawTensorContour(PetscDraw win,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
    144: {
    146:   int           N = m*n;
    147:   PetscTruth    isnull;
    148:   PetscDraw     popup;
    149:   MPI_Comm      comm;
    150:   int           xin=1,yin=1,i;
    151:   PetscMPIInt   size;
    152:   PetscReal     h;
    153:   ZoomCtx       ctx;

    156:   PetscDrawIsNull(win,&isnull); if (isnull) return(0);
    157:   PetscObjectGetComm((PetscObject)win,&comm);
    158:   MPI_Comm_size(comm,&size);
    159:   if (size > 1) SETERRQ(PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw");

    161:   if (N <= 0) {
    162:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n);
    163:   }

    165:   /* create scale window */
    166:   PetscDrawGetPopup(win,&popup);
    167:   PetscDrawCheckResizedWindow(win);

    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;}

    179:   /* PetscDraw the scale window */
    180:   if (popup) {PetscDrawScalePopup(popup,ctx.min,ctx.max);}

    182:   PetscOptionsHasName(PETSC_NULL,"-draw_contour_grid",&ctx.showgrid);

    184:   /* fill up x and y coordinates */
    185:   if (!xi) {
    186:     xin      = 0;
    187:     PetscMalloc(ctx.m*sizeof(PetscReal),&ctx.x);
    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 {
    192:     ctx.x = (PetscReal*)xi;
    193:   }
    194:   if (!yi) {
    195:     yin      = 0;
    196:     PetscMalloc(ctx.n*sizeof(PetscReal),&ctx.y);
    197:     h        = 1.0/(ctx.n-1);
    198:     ctx.y[0] = 0.0;
    199:     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
    200:   } else {
    201:     ctx.y = (PetscReal *)yi;
    202:   }

    204:   PetscDrawZoom(win,(PetscErrorCode (*)(PetscDraw,void *))PetscDrawTensorContour_Zoom,&ctx);
    205:
    206:   if (!xin) {PetscFree(ctx.x);}
    207:   if (!yin) {PetscFree(ctx.y);}

    209:   return(0);
    210: }

    214: /*@
    215:    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot
    216:    for a two-dimensional array.

    218:    Not Collective

    220:    Input Parameters:
    221: +  win - the window to draw in
    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: .  max,min - the maximum and minimum value in the entire contour
    225: -  v - the data

    227:    Options Database Keys:
    228: .  -draw_x_shared_colormap - Activates private colormap

    230:    Level: advanced

    232:    Note:
    233:    This is a lower level support routine, usually the user will call
    234:    PetscDrawTensorContour().

    236:    Concepts: contour plot

    238: .seealso: PetscDrawTensorContour()

    240: @*/
    241: PetscErrorCode PETSC_DLLEXPORT PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal max,PetscReal min,PetscReal *v)
    242: {
    244:   int           c1,c2,c3,c4,i,j;
    245:   PetscReal     x1,x2,x3,x4,y_1,y2,y3,y4,scale;

    248:   scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(max - min);

    250:   /* PetscDraw the contour plot patch */
    251:   for (j=0; j<n-1; j++) {
    252:     for (i=0; i<m-1; i++) {
    253:       x1 = x[i];  y_1 = y[j];  c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min));
    254:       x2 = x[i+1];y2 = y_1;    c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min));
    255:       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min));
    256:       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min));
    257:       PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
    258:       PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
    259:     }
    260:   }

    262:   return(0);
    263: }