Actual source code: ex6.c

petsc-3.7.3 2016-08-01
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: }

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

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

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

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

 58: int main(int argc,char **argv)
 59: {
 60:   char           title[64],cmap[32] = "";
 61:   PetscDraw      draw;
 62:   FunctionCtx    ctx;

 65:   ctx.function = Peaks;
 66:   PetscInitialize(&argc,&argv,NULL,help);
 67:   PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL);
 68:   PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap);

 70:   PetscDrawCreate(PETSC_COMM_WORLD,NULL,title,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&draw);
 71:   PetscObjectSetName((PetscObject)draw,"Peaks");
 72:   PetscDrawSetFromOptions(draw);
 73:   PetscDrawSetCoordinates(draw,-3,-3,+3,+3);
 74:   PetscDrawZoom(draw,DrawFunction,&ctx);
 75:   PetscDrawSave(draw);

 77:   PetscDrawDestroy(&draw);
 78:   PetscFinalize();
 79:   return 0;
 80: }