Actual source code: ex3.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Plots a simple line graph.\n";

  4: #if defined(PETSC_APPLE_FRAMEWORK)
  5: #import <PETSc/petscsys.h>
  6: #import <PETSc/petscdraw.h>
  7: #else
  8: #include <petscsys.h>
  9: #include <petscdraw.h>
 10: #endif

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

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

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

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

 60:   for (i=0; i<=n; i++) {
 61:     xd   = (PetscReal)(i - 5); yd = xd*xd;
 62:     PetscDrawLGAddPoint(lg,&xd,&yd);
 63:   }
 64:   PetscDrawLGDraw(lg);
 65:   PetscDrawLGSave(lg);

 67:   PetscDrawViewPortsDestroy(ports);
 68:   PetscDrawLGDestroy(&lg);
 69:   PetscDrawDestroy(&draw);
 70:   PetscFinalize();
 71:   return 0;
 72: }