コード例 #1
0
ファイル: ex7.c プロジェクト: masa-ito/PETScToPoisson
int main(int argc,char **argv)
{
  PetscDraw      draw;
  PetscErrorCode ierr;

  int i,j,w,h;
  int k  = PETSC_DRAW_BLACK;
  int r  = PETSC_DRAW_RED;
  int g  = PETSC_DRAW_GREEN;
  int b  = PETSC_DRAW_BLUE;
  int y  = PETSC_DRAW_YELLOW;
  int c0 = PETSC_DRAW_BASIC_COLORS;
  int c2 = 255;
  int c1 = (c0+c2)/2;

  ierr = PetscInitialize(&argc,&argv,NULL,help);CHKERRQ(ierr);

  ierr = PetscDrawCreate(PETSC_COMM_WORLD,0,"Draw Example",PETSC_DECIDE,PETSC_DECIDE,101,101,&draw);CHKERRQ(ierr);
  /*ierr = PetscDrawSetPause(draw,2.0);CHKERRQ(ierr);*/
  ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);

  ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
  ierr = PetscDrawGetWindowSize(draw,&w,&h);CHKERRQ(ierr);
  ierr = PetscDrawSetCoordinates(draw,0,0,--w,--h);CHKERRQ(ierr);
  ierr = PetscDrawClear(draw);CHKERRQ(ierr);
  /* one-pixel lines in the window corners */
  ierr = PetscDrawLine(draw,0,0,0,0,r);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,w,0,w,0,r);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,0,h,0,h,r);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,w,h,w,h,r);CHKERRQ(ierr);
  /* border lines with two pixels from  borders */
  ierr = PetscDrawLine(draw,0+2,0,w-2,0,k);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,0+2,h,w-2,h,k);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,0,0+2,0,h-2,k);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,w,0+2,w,h-2,k);CHKERRQ(ierr);
  /* oblique lines */
  ierr = PetscDrawLine(draw,0+2,h/2,w-2,h-2,b);CHKERRQ(ierr);
  ierr = PetscDrawLine(draw,0+1,h-1,w-1,0+1,b);CHKERRQ(ierr);
  /* vertical up and down arrow, two pixels from borders  */
  ierr = PetscDrawArrow(draw,1*w/4,0+2,1*w/4,h-2,g);CHKERRQ(ierr);
  ierr = PetscDrawArrow(draw,3*w/4,h-2,3*w/4,0+2,g);CHKERRQ(ierr);
  /* horizontal right and left arrow, two pixels from borders  */
  ierr = PetscDrawArrow(draw,0+2,3*h/4,w-2,3*h/4,g);CHKERRQ(ierr);
  ierr = PetscDrawArrow(draw,w-2,1*h/4,0+2,1*h/4,g);CHKERRQ(ierr);
  /* flush, save, and pause */
  ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
  ierr = PetscDrawSave(draw);CHKERRQ(ierr);
  ierr = PetscDrawPause(draw);CHKERRQ(ierr);

  ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
  ierr = PetscDrawGetWindowSize(draw,&w,&h);CHKERRQ(ierr);
  ierr = PetscDrawSetCoordinates(draw,0,0,--w,--h);CHKERRQ(ierr);
  ierr = PetscDrawClear(draw);CHKERRQ(ierr);
  /* one-pixel rectangles in the window corners */
  ierr = PetscDrawRectangle(draw,0,0,0,0,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,w,0,w,0,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,0,h,0,h,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,w,h,w,h,k,k,k,k);CHKERRQ(ierr);
  /* border rectangles with two pixels from  borders */
  ierr = PetscDrawRectangle(draw,0+2,0,w-2,0,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,0+2,h,w-2,h,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,0,0+2,0,h-2,k,k,k,k);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,w,0+2,w,h-2,k,k,k,k);CHKERRQ(ierr);
  /* more rectangles */
  ierr = PetscDrawRectangle(draw,0+2,0+2,w/2-1,h/2-1,b,b,b,b);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,0+2,h/2+1,w/2-1,h-2,r,r,r,r);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,w/2+1,h/2+1,w-2,h-2,g,g,g,g);CHKERRQ(ierr);
  ierr = PetscDrawRectangle(draw,w/2+1,0+2,w-2,h/2-1,y,y,y,y);CHKERRQ(ierr);
  /* flush, save, and pause */
  ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
  ierr = PetscDrawSave(draw);CHKERRQ(ierr);
  ierr = PetscDrawPause(draw);CHKERRQ(ierr);

  ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
  ierr = PetscDrawGetWindowSize(draw,&w,&h);CHKERRQ(ierr);
  ierr = PetscDrawSetCoordinates(draw,0,0,--w,--h);CHKERRQ(ierr);
  ierr = PetscDrawClear(draw);CHKERRQ(ierr);
  /* interpolated triangles, one pixel from borders */
  ierr = PetscDrawTriangle(draw,0+1,0+1,w-1,0+1,w-1,h-1,c0,c1,c2);CHKERRQ(ierr);
  ierr = PetscDrawTriangle(draw,0+1,0+1,0+1,h-1,w-1,h-1,c0,c1,c2);CHKERRQ(ierr);
  /* interpolated triangle, oblique, inside canvas */
  ierr = PetscDrawTriangle(draw,w/4,h/4,w/2,3*h/4,3*w/4,h/2,c2,c1,c0);CHKERRQ(ierr);
  /* flush, save, and pause */
  ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
  ierr = PetscDrawSave(draw);CHKERRQ(ierr);
  ierr = PetscDrawPause(draw);CHKERRQ(ierr);

  ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
  ierr = PetscDrawGetWindowSize(draw,&w,&h);CHKERRQ(ierr);
  ierr = PetscDrawSetCoordinates(draw,0,0,--w,--h);CHKERRQ(ierr);
  ierr = PetscDrawClear(draw);CHKERRQ(ierr);
  /* circles and ellipses */
  ierr = PetscDrawEllipse(draw,w/2,h/2,w-1,h-1,r);CHKERRQ(ierr);
  ierr = PetscDrawEllipse(draw,w,h/2,w/2,h,g);CHKERRQ(ierr);
  ierr = PetscDrawEllipse(draw,0,0,w,h/2,b);CHKERRQ(ierr);
  ierr = PetscDrawEllipse(draw,w/4,3*h/4,w/2,h/4,y);CHKERRQ(ierr);
  ierr = PetscDrawCoordinateToPixel(draw,w/2,h/2,&i,&j);CHKERRQ(ierr);
  ierr = PetscDrawPointPixel(draw,i,j,k);CHKERRQ(ierr);
  /* flush, save, and pause */
  ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
  ierr = PetscDrawSave(draw);CHKERRQ(ierr);
  ierr = PetscDrawPause(draw);CHKERRQ(ierr);

  ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
  ierr = PetscFinalize();
  return 0;
}
コード例 #2
0
ファイル: heat.c プロジェクト: firedrakeproject/petsc
/*
    This routine is not parallel
*/
PetscErrorCode  MyMonitor(TS ts,PetscInt step,PetscReal time,Vec U,void *ptr)
{
  UserCtx            *ctx = (UserCtx*)ptr;
  PetscDrawLG        lg;
  PetscErrorCode     ierr;
  PetscScalar        *u;
  PetscInt           Mx,i,xs,xm,cnt;
  PetscReal          x,y,hx,pause,sx,len,max,xx[2],yy[2];
  PetscDraw          draw;
  Vec                localU;
  DM                 da;
  int                colors[] = {PETSC_DRAW_YELLOW,PETSC_DRAW_RED,PETSC_DRAW_BLUE};
  const char*const   legend[] = {"-kappa (\\grad u,\\grad u)","(1 - u^2)^2"};
  PetscDrawAxis      axis;
  PetscDrawViewPorts *ports;
  PetscReal          vbounds[] = {-1.1,1.1};

  PetscFunctionBegin;
  ierr = PetscViewerDrawSetBounds(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),1,vbounds);CHKERRQ(ierr);
  ierr = PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),1200,800);CHKERRQ(ierr);
  ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
  ierr = DMGetLocalVector(da,&localU);CHKERRQ(ierr);
  ierr = DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
  ierr = DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
  hx   = 1.0/(PetscReal)Mx; sx = 1.0/(hx*hx);
  ierr = DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);CHKERRQ(ierr);
  ierr = DMDAVecGetArrayRead(da,localU,&u);CHKERRQ(ierr);

  ierr = PetscViewerDrawGetDrawLG(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),1,&lg);CHKERRQ(ierr);
  ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
  ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
  if (!ctx->ports) {
    ierr = PetscDrawViewPortsCreateRect(draw,1,3,&ctx->ports);CHKERRQ(ierr);
  }
  ports = ctx->ports;
  ierr  = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr);
  ierr  = PetscDrawLGReset(lg);CHKERRQ(ierr);

  xx[0] = 0.0; xx[1] = 1.0; cnt = 2;
  ierr  = PetscOptionsGetRealArray(NULL,NULL,"-zoom",xx,&cnt,NULL);CHKERRQ(ierr);
  xs    = xx[0]/hx; xm = (xx[1] - xx[0])/hx;

  /*
      Plot the  energies
  */
  ierr = PetscDrawLGSetDimension(lg,1 + (ctx->allencahn ? 1 : 0));CHKERRQ(ierr);
  ierr = PetscDrawLGSetColors(lg,colors+1);CHKERRQ(ierr);
  ierr = PetscDrawViewPortsSet(ports,2);CHKERRQ(ierr);
  x    = hx*xs;
  for (i=xs; i<xs+xm; i++) {
    xx[0] = xx[1] = x;
    yy[0] = PetscRealPart(.25*ctx->kappa*(u[i-1] - u[i+1])*(u[i-1] - u[i+1])*sx);
    if (ctx->allencahn) yy[1] = .25*PetscRealPart((1. - u[i]*u[i])*(1. - u[i]*u[i]));
    ierr = PetscDrawLGAddPoint(lg,xx,yy);CHKERRQ(ierr);
    x   += hx;
  }
  ierr = PetscDrawGetPause(draw,&pause);CHKERRQ(ierr);
  ierr = PetscDrawSetPause(draw,0.0);CHKERRQ(ierr);
  ierr = PetscDrawAxisSetLabels(axis,"Energy","","");CHKERRQ(ierr);
  ierr = PetscDrawLGSetLegend(lg,legend);CHKERRQ(ierr);
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);

  /*
      Plot the  forces
  */
  ierr = PetscDrawViewPortsSet(ports,1);CHKERRQ(ierr);
  ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
  x    = xs*hx;;
  max  = 0.;
  for (i=xs; i<xs+xm; i++) {
    xx[0] = xx[1] = x;
    yy[0] = PetscRealPart(ctx->kappa*(u[i-1] + u[i+1] - 2.0*u[i])*sx);
    max   = PetscMax(max,PetscAbs(yy[0]));
    if (ctx->allencahn) {
      yy[1] = PetscRealPart(u[i] - u[i]*u[i]*u[i]);
      max   = PetscMax(max,PetscAbs(yy[1]));
    }
    ierr = PetscDrawLGAddPoint(lg,xx,yy);CHKERRQ(ierr);
    x   += hx;
  }
  ierr = PetscDrawAxisSetLabels(axis,"Right hand side","","");CHKERRQ(ierr);
  ierr = PetscDrawLGSetLegend(lg,NULL);CHKERRQ(ierr);
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);

  /*
        Plot the solution
  */
  ierr = PetscDrawLGSetDimension(lg,1);CHKERRQ(ierr);
  ierr = PetscDrawViewPortsSet(ports,0);CHKERRQ(ierr);
  ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
  x    = hx*xs;
  ierr = PetscDrawLGSetLimits(lg,x,x+(xm-1)*hx,-1.1,1.1);CHKERRQ(ierr);
  ierr = PetscDrawLGSetColors(lg,colors);CHKERRQ(ierr);
  for (i=xs; i<xs+xm; i++) {
    xx[0] = x;
    yy[0] = PetscRealPart(u[i]);
    ierr  = PetscDrawLGAddPoint(lg,xx,yy);CHKERRQ(ierr);
    x    += hx;
  }
  ierr = PetscDrawAxisSetLabels(axis,"Solution","","");CHKERRQ(ierr);
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);

  /*
      Print the  forces as arrows on the solution
  */
  x   = hx*xs;
  cnt = xm/60;
  cnt = (!cnt) ? 1 : cnt;

  for (i=xs; i<xs+xm; i += cnt) {
    y    = PetscRealPart(u[i]);
    len  = .5*PetscRealPart(ctx->kappa*(u[i-1] + u[i+1] - 2.0*u[i])*sx)/max;
    ierr = PetscDrawArrow(draw,x,y,x,y+len,PETSC_DRAW_RED);CHKERRQ(ierr);
    if (ctx->allencahn) {
      len  = .5*PetscRealPart(u[i] - u[i]*u[i]*u[i])/max;
      ierr = PetscDrawArrow(draw,x,y,x,y+len,PETSC_DRAW_BLUE);CHKERRQ(ierr);
    }
    x += cnt*hx;
  }
  ierr = DMDAVecRestoreArrayRead(da,localU,&x);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(da,&localU);CHKERRQ(ierr);
  ierr = PetscDrawStringSetSize(draw,.2,.2);CHKERRQ(ierr);
  ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
  ierr = PetscDrawSetPause(draw,pause);CHKERRQ(ierr);
  ierr = PetscDrawPause(draw);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
コード例 #3
0
PETSC_EXTERN void PETSC_STDCALL  petscdrawarrow_(PetscDraw draw,PetscReal *xl,PetscReal *yl,PetscReal *xr,PetscReal *yr,int *cl, int *__ierr ) {
    *__ierr = PetscDrawArrow(
                  (PetscDraw)PetscToPointer((draw) ),*xl,*yl,*xr,*yr,*cl);
}