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 PetscCall(PetscInitialize(&argc,&argv,NULL,help)); 60 PetscCall(PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL)); 61 PetscCall(PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap)); 62 63 PetscCall(PetscDrawCreate(PETSC_COMM_WORLD,NULL,title,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&draw)); 64 PetscCall(PetscObjectSetName((PetscObject)draw,"Peaks")); 65 PetscCall(PetscDrawSetFromOptions(draw)); 66 PetscCall(PetscDrawSetCoordinates(draw,-3,-3,+3,+3)); 67 PetscCall(PetscDrawZoom(draw,DrawFunction,&ctx)); 68 PetscCall(PetscDrawSave(draw)); 69 70 PetscCall(PetscDrawDestroy(&draw)); 71 PetscCall(PetscFinalize()); 72 return 0; 73 } 74 75 /*TEST 76 77 build: 78 requires: x 79 80 test: 81 args: -draw_cmap hue 82 output_file: output/ex1_1.out 83 84 test: 85 suffix: 2 86 args: -draw_cmap gray 87 output_file: output/ex1_1.out 88 89 test: 90 suffix: 3 91 args: -draw_cmap bone 92 output_file: output/ex1_1.out 93 94 test: 95 suffix: 4 96 args: -draw_cmap jet 97 output_file: output/ex1_1.out 98 99 test: 100 suffix: 5 101 args: -draw_cmap coolwarm 102 output_file: output/ex1_1.out 103 104 test: 105 suffix: 6 106 args: -draw_cmap parula 107 output_file: output/ex1_1.out 108 109 test: 110 suffix: 7 111 args: -draw_cmap viridis 112 output_file: output/ex1_1.out 113 114 test: 115 suffix: 8 116 args: -draw_cmap plasma 117 output_file: output/ex1_1.out 118 119 TEST*/ 120