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