Actual source code: ex6.c

  1: static char help[] = "Demonstrates named colormaps\n";

  3: #include <petscsys.h>
  4: #include <petscdraw.h>

  6: typedef PetscReal (*Function)(PetscReal,PetscReal);

  8: typedef struct {
  9:   Function function;
 10: } FunctionCtx;

 12: #define Exp PetscExpReal
 13: #define Pow PetscPowReal
 14: static PetscReal Peaks(PetscReal x,PetscReal y)
 15: {
 16:   return 3 * Pow(1-x,2) * Exp(-Pow(x,2) - Pow(y+1,2))
 17:        - 10 * (x/5 - Pow(x,3) - Pow(y,5)) * Exp(-Pow(x,2) - Pow(y,2))
 18:        - 1./3 * Exp(-Pow(x+1,2) - Pow(y,2));
 19: }

 21: static PetscErrorCode DrawFunction(PetscDraw draw,void *ctx)
 22: {
 23:   int            i,j,w,h;
 24:   Function       function = ((FunctionCtx*)ctx)->function;
 25:   PetscReal      min = PETSC_MAX_REAL, max = PETSC_MIN_REAL;
 26:   MPI_Comm       comm = PetscObjectComm((PetscObject)draw);
 27:   PetscMPIInt    size,rank;
 28:   PetscDraw      popup;

 32:   PetscDrawGetWindowSize(draw,&w,&h);
 33:   MPI_Comm_size(comm,&size);
 34:   MPI_Comm_rank(comm,&rank);

 36:   PetscDrawCollectiveBegin(draw);
 37:   for (j=rank; j<h; j+=size) {
 38:     for (i=0; i<w; i++) {
 39:       PetscReal x,y,f; int color;
 40:       PetscDrawPixelToCoordinate(draw,i,j,&x,&y);
 41:       f = function(x,y); color = PetscDrawRealToColor(f,-8,+8);
 42:       PetscDrawPointPixel(draw,i,j,color);
 43:       min = PetscMin(f,min); max = PetscMax(f,max);
 44:     }
 45:   }
 46:   PetscDrawCollectiveEnd(draw);

 48:   PetscDrawGetPopup(draw,&popup);
 49:   PetscDrawScalePopup(popup,-8,+8);
 50:   return(0);
 51: }

 53: int main(int argc,char **argv)
 54: {
 55:   char           title[64],cmap[32] = "";
 56:   PetscDraw      draw;
 57:   FunctionCtx    ctx;

 60:   ctx.function = Peaks;
 61:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
 62:   PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL);
 63:   PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap);

 65:   PetscDrawCreate(PETSC_COMM_WORLD,NULL,title,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&draw);
 66:   PetscObjectSetName((PetscObject)draw,"Peaks");
 67:   PetscDrawSetFromOptions(draw);
 68:   PetscDrawSetCoordinates(draw,-3,-3,+3,+3);
 69:   PetscDrawZoom(draw,DrawFunction,&ctx);
 70:   PetscDrawSave(draw);

 72:   PetscDrawDestroy(&draw);
 73:   PetscFinalize();
 74:   return ierr;
 75: }

 77: /*TEST

 79:       build:
 80:          requires: x

 82:       test:
 83:          args: -draw_cmap hue
 84:          output_file: output/ex1_1.out

 86:       test:
 87:          suffix: 2
 88:          args: -draw_cmap gray
 89:          output_file: output/ex1_1.out

 91:       test:
 92:          suffix: 3
 93:          args: -draw_cmap bone
 94:          output_file: output/ex1_1.out

 96:       test:
 97:          suffix: 4
 98:          args: -draw_cmap jet
 99:          output_file: output/ex1_1.out

101:       test:
102:          suffix: 5
103:          args: -draw_cmap coolwarm
104:          output_file: output/ex1_1.out

106:       test:
107:          suffix: 6
108:          args: -draw_cmap parula
109:          output_file: output/ex1_1.out

111:       test:
112:          suffix: 7
113:          args: -draw_cmap viridis
114:          output_file: output/ex1_1.out

116:       test:
117:          suffix: 8
118:          args: -draw_cmap plasma
119:          output_file: output/ex1_1.out

121: TEST*/