1 static char help[] = "Demonstrates named colormaps\n"; 2 3 #include <petscsys.h> 4 #include <petscdraw.h> 5 6 typedef PetscReal (*Function)(PetscReal,PetscReal); 7 8 typedef struct { 9 Function function; 10 } FunctionCtx; 11 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 } 20 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; 29 30 PetscFunctionBegin; 31 PetscCall(PetscDrawGetWindowSize(draw,&w,&h)); 32 PetscCallMPI(MPI_Comm_size(comm,&size)); 33 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 34 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 PetscCall(PetscDrawPixelToCoordinate(draw,i,j,&x,&y)); 40 f = function(x,y); color = PetscDrawRealToColor(f,-8,+8); 41 PetscCall(PetscDrawPointPixel(draw,i,j,color)); 42 min = PetscMin(f,min); max = PetscMax(f,max); 43 } 44 } 45 PetscDrawCollectiveEnd(draw); 46 47 PetscCall(PetscDrawGetPopup(draw,&popup)); 48 PetscCall(PetscDrawScalePopup(popup,-8,+8)); 49 PetscFunctionReturn(0); 50 } 51 52 int main(int argc,char **argv) 53 { 54 char title[64],cmap[32] = ""; 55 PetscDraw draw; 56 FunctionCtx ctx; 57 58 ctx.function = Peaks; 59 PetscFunctionBeginUser; 60 PetscCall(PetscInitialize(&argc,&argv,NULL,help)); 61 PetscCall(PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL)); 62 PetscCall(PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap)); 63 64 PetscCall(PetscDrawCreate(PETSC_COMM_WORLD,NULL,title,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&draw)); 65 PetscCall(PetscObjectSetName((PetscObject)draw,"Peaks")); 66 PetscCall(PetscDrawSetFromOptions(draw)); 67 PetscCall(PetscDrawSetCoordinates(draw,-3,-3,+3,+3)); 68 PetscCall(PetscDrawZoom(draw,DrawFunction,&ctx)); 69 PetscCall(PetscDrawSave(draw)); 70 71 PetscCall(PetscDrawDestroy(&draw)); 72 PetscCall(PetscFinalize()); 73 return 0; 74 } 75 76 /*TEST 77 78 build: 79 requires: x 80 81 test: 82 args: -draw_cmap hue 83 output_file: output/ex1_1.out 84 85 test: 86 suffix: 2 87 args: -draw_cmap gray 88 output_file: output/ex1_1.out 89 90 test: 91 suffix: 3 92 args: -draw_cmap bone 93 output_file: output/ex1_1.out 94 95 test: 96 suffix: 4 97 args: -draw_cmap jet 98 output_file: output/ex1_1.out 99 100 test: 101 suffix: 5 102 args: -draw_cmap coolwarm 103 output_file: output/ex1_1.out 104 105 test: 106 suffix: 6 107 args: -draw_cmap parula 108 output_file: output/ex1_1.out 109 110 test: 111 suffix: 7 112 args: -draw_cmap viridis 113 output_file: output/ex1_1.out 114 115 test: 116 suffix: 8 117 args: -draw_cmap plasma 118 output_file: output/ex1_1.out 119 120 TEST*/ 121