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