Actual source code: ex3.c

  1: static char help[] = "Plots a simple line graph.\n";

  3: #if defined(PETSC_APPLE_FRAMEWORK)
  4:   #import <PETSc/petscsys.h>
  5:   #import <PETSc/petscdraw.h>
  6: #else

  8: #include <petscsys.h>
  9: #include <petscdraw.h>
 10: #endif

 12: int main(int argc, char **argv)
 13: {
 14:   PetscDraw           draw;
 15:   PetscDrawLG         lg;
 16:   PetscDrawAxis       axis;
 17:   PetscInt            n = 15, i, x = 0, y = 0, width = 400, height = 300, nports = 1;
 18:   PetscBool           useports, flg;
 19:   const char         *xlabel, *ylabel, *toplabel, *legend;
 20:   PetscReal           xd, yd;
 21:   PetscDrawViewPorts *ports = NULL;

 23:   toplabel = "Top Label";
 24:   xlabel   = "X-axis Label";
 25:   ylabel   = "Y-axis Label";
 26:   legend   = "Legend";

 28:   PetscFunctionBeginUser;
 29:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 30:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-x", &x, NULL));
 31:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-y", &y, NULL));
 32:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-width", &width, NULL));
 33:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-height", &height, NULL));
 34:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 35:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nports", &nports, &useports));
 36:   PetscCall(PetscOptionsHasName(NULL, NULL, "-nolegend", &flg));
 37:   if (flg) legend = NULL;
 38:   PetscCall(PetscOptionsHasName(NULL, NULL, "-notoplabel", &flg));
 39:   if (flg) toplabel = NULL;
 40:   PetscCall(PetscOptionsHasName(NULL, NULL, "-noxlabel", &flg));
 41:   if (flg) xlabel = NULL;
 42:   PetscCall(PetscOptionsHasName(NULL, NULL, "-noylabel", &flg));
 43:   if (flg) ylabel = NULL;
 44:   PetscCall(PetscOptionsHasName(NULL, NULL, "-nolabels", &flg));
 45:   if (flg) {
 46:     toplabel = NULL;
 47:     xlabel   = NULL;
 48:     ylabel   = NULL;
 49:   }

 51:   PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, 0, "Title", x, y, width, height, &draw));
 52:   PetscCall(PetscDrawSetFromOptions(draw));
 53:   if (useports) {
 54:     PetscCall(PetscDrawViewPortsCreate(draw, nports, &ports));
 55:     PetscCall(PetscDrawViewPortsSet(ports, 0));
 56:   }
 57:   PetscCall(PetscDrawLGCreate(draw, 1, &lg));
 58:   PetscCall(PetscDrawLGSetUseMarkers(lg, PETSC_TRUE));
 59:   PetscCall(PetscDrawLGGetAxis(lg, &axis));
 60:   PetscCall(PetscDrawAxisSetColors(axis, PETSC_DRAW_BLACK, PETSC_DRAW_RED, PETSC_DRAW_BLUE));
 61:   PetscCall(PetscDrawAxisSetLabels(axis, toplabel, xlabel, ylabel));
 62:   PetscCall(PetscDrawLGSetLegend(lg, &legend));
 63:   PetscCall(PetscDrawLGSetFromOptions(lg));

 65:   for (i = 0; i <= n; i++) {
 66:     xd = (PetscReal)(i - 5);
 67:     yd = xd * xd;
 68:     PetscCall(PetscDrawLGAddPoint(lg, &xd, &yd));
 69:   }
 70:   PetscCall(PetscDrawLGDraw(lg));
 71:   PetscCall(PetscDrawLGSave(lg));

 73:   PetscCall(PetscDrawViewPortsDestroy(ports));
 74:   PetscCall(PetscDrawLGDestroy(&lg));
 75:   PetscCall(PetscDrawDestroy(&draw));
 76:   PetscCall(PetscFinalize());
 77:   return 0;
 78: }

 80: /*TEST

 82:    build:
 83:      requires: x

 85:    test:
 86:      output_file: output/ex1_1.out

 88: TEST*/