Actual source code: ex6.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  1: static char help[] = "Demonstrates named colormaps\n";

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


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

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

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

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

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

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

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

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

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

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

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


 79: /*TEST

 81:       build:
 82:          requires: x

 84:       test:
 85:          args: -draw_cmap hue
 86:          output_file: output/ex1_1.out

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

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

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

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

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

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

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

123: TEST*/