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