xref: /petsc/src/sys/classes/draw/tests/ex6.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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   CHKERRQ(PetscDrawGetWindowSize(draw,&w,&h));
33   CHKERRMPI(MPI_Comm_size(comm,&size));
34   CHKERRMPI(MPI_Comm_rank(comm,&rank));
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       CHKERRQ(PetscDrawPixelToCoordinate(draw,i,j,&x,&y));
41       f = function(x,y); color = PetscDrawRealToColor(f,-8,+8);
42       CHKERRQ(PetscDrawPointPixel(draw,i,j,color));
43       min = PetscMin(f,min); max = PetscMax(f,max);
44     }
45   }
46   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47 
48   CHKERRQ(PetscDrawGetPopup(draw,&popup));
49   CHKERRQ(PetscDrawScalePopup(popup,-8,+8));
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   CHKERRQ(PetscOptionsGetString(NULL,NULL,"-draw_cmap",cmap,sizeof(cmap),NULL));
63   CHKERRQ(PetscSNPrintf(title,sizeof(title),"Colormap: %s",cmap));
64 
65   CHKERRQ(PetscDrawCreate(PETSC_COMM_WORLD,NULL,title,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&draw));
66   CHKERRQ(PetscObjectSetName((PetscObject)draw,"Peaks"));
67   CHKERRQ(PetscDrawSetFromOptions(draw));
68   CHKERRQ(PetscDrawSetCoordinates(draw,-3,-3,+3,+3));
69   CHKERRQ(PetscDrawZoom(draw,DrawFunction,&ctx));
70   CHKERRQ(PetscDrawSave(draw));
71 
72   CHKERRQ(PetscDrawDestroy(&draw));
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