Actual source code: ex6.c

petsc-3.7.2 2016-06-05
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: #define Exp PetscExpReal
 10: #define Pow PetscPowReal
 11: static PetscReal Peaks(PetscReal x,PetscReal y)
 12: {
 13:   return 3 * Pow(1-x,2) * Exp(-Pow(x,2) - Pow(y+1,2))
 14:        - 10 * (x/5 - Pow(x,3) - Pow(y,5)) * Exp(-Pow(x,2) - Pow(y,2))
 15:        - 1./3 * Exp(-Pow(x+1,2) - Pow(y,2));
 16: }

 20: static PetscErrorCode DrawFunction(PetscDraw draw,void *ctx)
 21: {
 22:   int            i,j,w,h;
 23:   Function       function = (Function)ctx;
 24:   PetscReal      min = PETSC_MAX_REAL, max = PETSC_MIN_REAL;
 25:   MPI_Comm       comm = PetscObjectComm((PetscObject)draw);
 26:   PetscMPIInt    size,rank;
 27:   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: }

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

 60:   PetscInitialize(&argc,&argv,NULL,help);
 61:   PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL);
 62:   PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap);

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

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