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: #if defined(__GNUC__) && !defined(__clang__)
 20:   #pragma GCC diagnostic push
 21:   #pragma GCC diagnostic ignored "-Wclobbered"
 22: #endif
 23: static PetscErrorCode DrawFunction(PetscDraw draw, PetscCtx ctx)
 24: {
 25:   int         i, j, w, h;
 26:   Function    function = ((FunctionCtx *)ctx)->function;
 27:   PetscReal   min = PETSC_MAX_REAL, max = PETSC_MIN_REAL;
 28:   MPI_Comm    comm = PetscObjectComm((PetscObject)draw);
 29:   PetscMPIInt size, rank;
 30:   PetscDraw   popup;

 32:   PetscFunctionBegin;
 33:   PetscCall(PetscDrawGetWindowSize(draw, &w, &h));
 34:   PetscCallMPI(MPI_Comm_size(comm, &size));
 35:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

 37:   PetscDrawCollectiveBegin(draw);
 38:   for (j = rank; j < h; j += size) {
 39:     for (i = 0; i < w; i++) {
 40:       PetscReal x, y, f;
 41:       int       color;
 42:       PetscCall(PetscDrawPixelToCoordinate(draw, i, j, &x, &y));
 43:       f     = function(x, y);
 44:       color = PetscDrawRealToColor(f, -8, +8);
 45:       PetscCall(PetscDrawPointPixel(draw, i, j, color));
 46:       min = PetscMin(f, min);
 47:       max = PetscMax(f, max);
 48:     }
 49:   }
 50:   PetscDrawCollectiveEnd(draw);

 52:   PetscCall(PetscDrawGetPopup(draw, &popup));
 53:   PetscCall(PetscDrawScalePopup(popup, -8, +8));
 54:   PetscFunctionReturn(PETSC_SUCCESS);
 55: }
 56: #if defined(__GNUC__) && !defined(__clang__)
 57:   #pragma GCC diagnostic pop
 58: #endif

 60: int main(int argc, char **argv)
 61: {
 62:   char        title[64], cmap[32] = "";
 63:   PetscDraw   draw;
 64:   FunctionCtx ctx;

 66:   PetscFunctionBeginUser;
 67:   ctx.function = Peaks;
 68:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 69:   PetscCall(PetscOptionsGetString(NULL, NULL, "-draw_cmap", cmap, sizeof(cmap), NULL));
 70:   PetscCall(PetscSNPrintf(title, sizeof(title), "Colormap: %s", cmap));

 72:   PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, title, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, &draw));
 73:   PetscCall(PetscObjectSetName((PetscObject)draw, "Peaks"));
 74:   PetscCall(PetscDrawSetFromOptions(draw));
 75:   PetscCall(PetscDrawSetCoordinates(draw, -3, -3, +3, +3));
 76:   PetscCall(PetscDrawZoom(draw, DrawFunction, &ctx));
 77:   PetscCall(PetscDrawSave(draw));

 79:   PetscCall(PetscDrawDestroy(&draw));
 80:   PetscCall(PetscFinalize());
 81:   return 0;
 82: }

 84: /*TEST

 86:       build:
 87:          requires: x

 89:       test:
 90:          args: -draw_cmap hue
 91:          output_file: output/empty.out

 93:       test:
 94:          suffix: 2
 95:          args: -draw_cmap gray
 96:          output_file: output/empty.out

 98:       test:
 99:          suffix: 3
100:          args: -draw_cmap bone
101:          output_file: output/empty.out

103:       test:
104:          suffix: 4
105:          args: -draw_cmap jet
106:          output_file: output/empty.out

108:       test:
109:          suffix: 5
110:          args: -draw_cmap coolwarm
111:          output_file: output/empty.out

113:       test:
114:          suffix: 6
115:          args: -draw_cmap parula
116:          output_file: output/empty.out

118:       test:
119:          suffix: 7
120:          args: -draw_cmap viridis
121:          output_file: output/empty.out

123:       test:
124:          suffix: 8
125:          args: -draw_cmap plasma
126:          output_file: output/empty.out

128: TEST*/