Actual source code: ex6.c
1: static char help[] = "Demonstrates named colormaps\n";
3: #include <petscsys.h>
4: #include <petscdraw.h>
6: typedef PetscReal (*Function)(PetscReal, PetscReal);
8: typedef struct {
9: Function function;
10: } FunctionCtx;
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)) - 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: }
19: static PetscErrorCode DrawFunction(PetscDraw draw, void *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;
28: PetscDrawGetWindowSize(draw, &w, &h);
29: MPI_Comm_size(comm, &size);
30: MPI_Comm_rank(comm, &rank);
32: PetscDrawCollectiveBegin(draw);
33: for (j = rank; j < h; j += size) {
34: for (i = 0; i < w; i++) {
35: PetscReal x, y, f;
36: int color;
37: PetscDrawPixelToCoordinate(draw, i, j, &x, &y);
38: f = function(x, y);
39: color = PetscDrawRealToColor(f, -8, +8);
40: PetscDrawPointPixel(draw, i, j, color);
41: min = PetscMin(f, min);
42: max = PetscMax(f, max);
43: }
44: }
45: PetscDrawCollectiveEnd(draw);
47: PetscDrawGetPopup(draw, &popup);
48: PetscDrawScalePopup(popup, -8, +8);
49: return 0;
50: }
52: int main(int argc, char **argv)
53: {
54: char title[64], cmap[32] = "";
55: PetscDraw draw;
56: FunctionCtx ctx;
58: ctx.function = Peaks;
60: PetscInitialize(&argc, &argv, NULL, help);
61: PetscOptionsGetString(NULL, NULL, "-draw_cmap", cmap, sizeof(cmap), NULL);
62: PetscSNPrintf(title, sizeof(title), "Colormap: %s", cmap);
64: PetscDrawCreate(PETSC_COMM_WORLD, NULL, title, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, &draw);
65: PetscObjectSetName((PetscObject)draw, "Peaks");
66: PetscDrawSetFromOptions(draw);
67: PetscDrawSetCoordinates(draw, -3, -3, +3, +3);
68: PetscDrawZoom(draw, DrawFunction, &ctx);
69: PetscDrawSave(draw);
71: PetscDrawDestroy(&draw);
72: PetscFinalize();
73: return 0;
74: }
76: /*TEST
78: build:
79: requires: x
81: test:
82: args: -draw_cmap hue
83: output_file: output/ex1_1.out
85: test:
86: suffix: 2
87: args: -draw_cmap gray
88: output_file: output/ex1_1.out
90: test:
91: suffix: 3
92: args: -draw_cmap bone
93: output_file: output/ex1_1.out
95: test:
96: suffix: 4
97: args: -draw_cmap jet
98: output_file: output/ex1_1.out
100: test:
101: suffix: 5
102: args: -draw_cmap coolwarm
103: output_file: output/ex1_1.out
105: test:
106: suffix: 6
107: args: -draw_cmap parula
108: output_file: output/ex1_1.out
110: test:
111: suffix: 7
112: args: -draw_cmap viridis
113: output_file: output/ex1_1.out
115: test:
116: suffix: 8
117: args: -draw_cmap plasma
118: output_file: output/ex1_1.out
120: TEST*/