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: PetscFunctionBegin;
29: PetscCall(PetscDrawGetWindowSize(draw, &w, &h));
30: PetscCallMPI(MPI_Comm_size(comm, &size));
31: PetscCallMPI(MPI_Comm_rank(comm, &rank));
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);
48: PetscCall(PetscDrawGetPopup(draw, &popup));
49: PetscCall(PetscDrawScalePopup(popup, -8, +8));
50: PetscFunctionReturn(PETSC_SUCCESS);
51: }
53: int main(int argc, char **argv)
54: {
55: char title[64], cmap[32] = "";
56: PetscDraw draw;
57: FunctionCtx ctx;
59: ctx.function = Peaks;
60: PetscFunctionBeginUser;
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));
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));
72: PetscCall(PetscDrawDestroy(&draw));
73: PetscCall(PetscFinalize());
74: return 0;
75: }
77: /*TEST
79: build:
80: requires: x
82: test:
83: args: -draw_cmap hue
84: output_file: output/ex1_1.out
86: test:
87: suffix: 2
88: args: -draw_cmap gray
89: output_file: output/ex1_1.out
91: test:
92: suffix: 3
93: args: -draw_cmap bone
94: output_file: output/ex1_1.out
96: test:
97: suffix: 4
98: args: -draw_cmap jet
99: output_file: output/ex1_1.out
101: test:
102: suffix: 5
103: args: -draw_cmap coolwarm
104: output_file: output/ex1_1.out
106: test:
107: suffix: 6
108: args: -draw_cmap parula
109: output_file: output/ex1_1.out
111: test:
112: suffix: 7
113: args: -draw_cmap viridis
114: output_file: output/ex1_1.out
116: test:
117: suffix: 8
118: args: -draw_cmap plasma
119: output_file: output/ex1_1.out
121: TEST*/