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