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;

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

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

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

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

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

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

 70:   PetscDrawDestroy(&draw);
 71:   PetscFinalize();
 72:   return 0;
 73: }

 75: /*TEST

 77:       build:
 78:          requires: x

 80:       test:
 81:          args: -draw_cmap hue
 82:          output_file: output/ex1_1.out

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

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

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

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

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

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

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

119: TEST*/