int main(int argc,char **argv) { PetscDraw draw; PetscErrorCode ierr; int x = 0,y = 0,width = 300,height = 300; ierr = PetscInitialize(&argc,&argv,NULL,help);CHKERRQ(ierr); ierr = PetscDrawCreate(PETSC_COMM_WORLD,0,"Title",x,y,width,height,&draw);CHKERRQ(ierr); ierr = PetscDrawSetPause(draw,2.0);CHKERRQ(ierr); ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); ierr = PetscDrawSetViewPort(draw,.25,.25,.75,.75);CHKERRQ(ierr); ierr = PetscDrawLine(draw,0.0,0.0,1.0,1.0,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawString(draw,.2,.2,PETSC_DRAW_RED,"Some Text");CHKERRQ(ierr); ierr = PetscDrawString(draw,.5,.5,PETSC_DRAW_GREEN,"Some Text");CHKERRQ(ierr); ierr = PetscDrawString(draw,.2,.8,PETSC_DRAW_BLUE,"Some Text");CHKERRQ(ierr); ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); ierr = PetscDrawSave(draw);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); /*ierr = PetscDrawStringSetSize(draw,.5,.5);CHKERRQ(ierr);*/ ierr = PetscDrawString(draw,.2,.2,PETSC_DRAW_RED,"Some Text");CHKERRQ(ierr); ierr = PetscDrawString(draw,.5,.5,PETSC_DRAW_GREEN,"Some Text");CHKERRQ(ierr); ierr = PetscDrawString(draw,.2,.8,PETSC_DRAW_BLUE,"Some Text");CHKERRQ(ierr); ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); ierr = PetscDrawSave(draw);CHKERRQ(ierr); ierr = PetscDrawResizeWindow(draw,600,600);CHKERRQ(ierr); ierr = PetscDrawGetWindowSize(draw,&width,&height);CHKERRQ(ierr); ierr = PetscDrawSetViewPort(draw,0,0,1,1);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); /*ierr = PetscDrawLine(draw,0.0,0.0,1.0,1.0,PETSC_DRAW_RED);CHKERRQ(ierr);*/ /*ierr = PetscDrawLine(draw,0.0,1.0,1.0,0.0,PETSC_DRAW_BLUE);CHKERRQ(ierr);*/ ierr = PetscDrawString(draw,.2,.2,PETSC_DRAW_RED,"Some Text\n Some Other Text");CHKERRQ(ierr); ierr = PetscDrawString(draw,.5,.5,PETSC_DRAW_RED,"ABCygj\n()[]F$");CHKERRQ(ierr); ierr = PetscDrawString(draw,0,0,PETSC_DRAW_RED,"Horizontal Text (ABCygj)");CHKERRQ(ierr); ierr = PetscDrawStringVertical(draw,0,1,PETSC_DRAW_RED,"Vertical Text");CHKERRQ(ierr); ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); ierr = PetscDrawSave(draw);CHKERRQ(ierr); ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
/*@ PetscDrawDestroy - Deletes a draw context. Collective on PetscDraw Input Parameters: . draw - the drawing context Level: beginner .seealso: PetscDrawCreate() @*/ PetscErrorCode PetscDrawDestroy(PetscDraw *draw) { PetscErrorCode ierr; PetscFunctionBegin; if (!*draw) PetscFunctionReturn(0); PetscValidHeaderSpecific(*draw,PETSC_DRAW_CLASSID,1); if (--((PetscObject)(*draw))->refct > 0) PetscFunctionReturn(0); if ((*draw)->pause == -2) { (*draw)->pause = -1; ierr = PetscDrawPause(*draw);CHKERRQ(ierr); } /* if memory was published then destroy it */ ierr = PetscObjectSAWsViewOff((PetscObject)*draw);CHKERRQ(ierr); if ((*draw)->ops->destroy) { ierr = (*(*draw)->ops->destroy)(*draw);CHKERRQ(ierr); } ierr = PetscFree((*draw)->title);CHKERRQ(ierr); ierr = PetscFree((*draw)->display);CHKERRQ(ierr); ierr = PetscFree((*draw)->savefilename);CHKERRQ(ierr); ierr = PetscFree((*draw)->savefinalfilename);CHKERRQ(ierr); ierr = PetscHeaderDestroy(draw);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PetscDrawLGDraw - Redraws a line graph. Not Collective,but ignored by all processors except processor 0 in PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw() @*/ PetscErrorCode PetscDrawLGDraw(PetscDrawLG lg) { PetscReal xmin=lg->xmin,xmax=lg->xmax,ymin=lg->ymin,ymax=lg->ymax; PetscErrorCode ierr; int i,j,dim = lg->dim,nopts = lg->nopts,rank,cl; PetscDraw draw = lg->win; PetscFunctionBegin; if (lg && ((PetscObject)lg)->classid == PETSC_DRAW_CLASSID) PetscFunctionReturn(0); PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); ierr = PetscDrawClear(draw);CHKERRQ(ierr); ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = MPI_Comm_rank(((PetscObject)lg)->comm,&rank);CHKERRQ(ierr); if (!rank) { for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { if (lg->colors) cl = lg->colors[i]; else cl = PETSC_DRAW_BLACK+i; ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],cl);CHKERRQ(ierr); if (lg->use_dots) { ierr = PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED,"x");CHKERRQ(ierr); } } } } if (lg->legend) { PetscReal xl,yl,xr,yr,tw,th; size_t len,mlen = 0; int cl; ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); for (i=0; i<dim; i++) { ierr = PetscStrlen(lg->legend[i],&len);CHKERRQ(ierr); mlen = PetscMax(mlen,len); } ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - 2*tw,yr - 3*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); for (i=0; i<dim; i++) { cl = (lg->colors ? lg->colors[i] : i + 1); ierr = PetscDrawLine(draw,xr - (mlen + 6.7)*tw,yr - (4 + i)*th,xr - (mlen + 3.2)*tw,yr - (4 + i)*th,cl);CHKERRQ(ierr); ierr = PetscDrawString(draw,xr - (mlen + 3)*tw,yr - (4.5 + i)*th,PETSC_DRAW_BLACK,lg->legend[i]);CHKERRQ(ierr); } ierr = PetscDrawLine(draw,xr - 2*tw,yr - 3*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); } ierr = PetscDrawFlush(lg->win);CHKERRQ(ierr); ierr = PetscDrawPause(lg->win);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PetscDrawLGSPDraw - Redraws a line graph. Collective on PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawLGDraw(), PetscDrawSPDraw() Developer Notes: This code cheats and uses the fact that the LG and SP structs are the same @*/ PetscErrorCode PetscDrawLGSPDraw(PetscDrawLG lg,PetscDrawSP spin) { PetscDrawLG sp = (PetscDrawLG)spin; PetscReal xmin,xmax,ymin,ymax; PetscErrorCode ierr; PetscBool isnull; PetscMPIInt rank; PetscDraw draw; PetscFunctionBegin; PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); PetscValidHeaderSpecific(sp,PETSC_DRAWSP_CLASSID,2); ierr = PetscDrawIsNull(lg->win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)lg),&rank);CHKERRQ(ierr); draw = lg->win; ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); xmin = PetscMin(lg->xmin,sp->xmin); ymin = PetscMin(lg->ymin,sp->ymin); xmax = PetscMax(lg->xmax,sp->xmax); ymax = PetscMax(lg->ymax,sp->ymax); ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); if (!rank) { int i,j,dim,nopts; dim = lg->dim; nopts = lg->nopts; for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_BLACK+i);CHKERRQ(ierr); if (lg->use_markers) { ierr = PetscDrawMarker(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED);CHKERRQ(ierr); } } } dim = sp->dim; nopts = sp->nopts; for (i=0; i<dim; i++) { for (j=0; j<nopts; j++) { ierr = PetscDrawMarker(draw,sp->x[j*dim+i],sp->y[j*dim+i],PETSC_DRAW_RED);CHKERRQ(ierr); } } } ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PetscDrawLGSPDraw - Redraws a line graph. Not Collective,but ignored by all processors except processor 0 in PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawLGDraw(), PetscDrawSPDraw() Developer Notes: This code cheats and uses the fact that the LG and SP structs are the same @*/ PetscErrorCode PetscDrawLGSPDraw(PetscDrawLG lg,PetscDrawSP spin) { PetscDrawLG sp = (PetscDrawLG)spin; PetscReal xmin,xmax,ymin,ymax; PetscErrorCode ierr; int i,j,dim,nopts,rank; PetscDraw draw = lg->win; PetscFunctionBegin; if (lg && ((PetscObject)lg)->classid == PETSC_DRAW_CLASSID) PetscFunctionReturn(0); PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); PetscValidHeaderSpecific(sp,PETSC_DRAWSP_CLASSID,2); xmin = PetscMin(lg->xmin,sp->xmin); ymin = PetscMin(lg->ymin,sp->ymin); xmax = PetscMax(lg->xmax,sp->xmax); ymax = PetscMax(lg->ymax,sp->ymax); ierr = PetscDrawClear(draw);CHKERRQ(ierr); ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = MPI_Comm_rank(((PetscObject)lg)->comm,&rank);CHKERRQ(ierr); if (!rank) { dim = lg->dim; nopts = lg->nopts; for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_BLACK+i);CHKERRQ(ierr); if (lg->use_dots) { ierr = PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED,"x");CHKERRQ(ierr); } } } dim = sp->dim; nopts = sp->nopts; for (i=0; i<dim; i++) { for (j=0; j<nopts; j++) { ierr = PetscDrawString(draw,sp->x[j*dim+i],sp->y[j*dim+i],PETSC_DRAW_RED,"x");CHKERRQ(ierr); } } } ierr = PetscDrawFlush(lg->win);CHKERRQ(ierr); ierr = PetscDrawPause(lg->win);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscDraw draw; PetscMPIInt size,rank; PetscErrorCode ierr; int x = 0,y = 0,width = 256,height = 256,i; ierr = PetscInitialize(&argc,&argv,NULL,help);CHKERRQ(ierr); ierr = PetscDrawCreate(PETSC_COMM_WORLD,0,"Title",x,y,width,height,&draw);CHKERRQ(ierr); ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); for (i=rank; i<height; i+=size) { PetscReal y = ((PetscReal)i)/(height-1); ierr = PetscDrawLine(draw,0.0,y,1.0,y,i%256);CHKERRQ(ierr); } ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); ierr = PetscDrawSave(draw);CHKERRQ(ierr); ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode DMView_DA_1d(DM da,PetscViewer viewer) { PetscErrorCode ierr; PetscMPIInt rank; PetscBool iascii,isdraw,isbinary; DM_DA *dd = (DM_DA*)da->data; #if defined(PETSC_HAVE_MATLAB_ENGINE) PetscBool ismatlab; #endif PetscFunctionBegin; ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab);CHKERRQ(ierr); #endif if (iascii) { PetscViewerFormat format; ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); if (format != PETSC_VIEWER_ASCII_VTK && format != PETSC_VIEWER_ASCII_VTK_CELL) { DMDALocalInfo info; ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Processor [%d] M %D m %D w %D s %D\n",rank,dd->M,dd->m,dd->w,dd->s);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(viewer,"X range of indices: %D %D\n",info.xs,info.xs+info.xm);CHKERRQ(ierr); ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); } else { ierr = DMView_DA_VTK(da, viewer);CHKERRQ(ierr); } } else if (isdraw) { PetscDraw draw; double ymin = -1,ymax = 1,xmin = -1,xmax = dd->M,x; PetscInt base; char node[10]; PetscBool isnull; ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax);CHKERRQ(ierr); ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); /* first processor draws all node lines */ if (!rank) { PetscInt xmin_tmp; ymin = 0.0; ymax = 0.3; for (xmin_tmp=0; xmin_tmp < dd->M; xmin_tmp++) { ierr = PetscDrawLine(draw,(double)xmin_tmp,ymin,(double)xmin_tmp,ymax,PETSC_DRAW_BLACK);CHKERRQ(ierr); } xmin = 0.0; xmax = dd->M - 1; ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_BLACK);CHKERRQ(ierr); } ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); /* draw my box */ ymin = 0; ymax = 0.3; xmin = dd->xs / dd->w; xmax = (dd->xe / dd->w) - 1; ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_RED);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmin,ymin,xmin,ymax,PETSC_DRAW_RED);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_RED);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmax,ymin,xmax,ymax,PETSC_DRAW_RED);CHKERRQ(ierr); /* Put in index numbers */ base = dd->base / dd->w; for (x=xmin; x<=xmax; x++) { sprintf(node,"%d",(int)base++); ierr = PetscDrawString(draw,x,ymin,PETSC_DRAW_RED,node);CHKERRQ(ierr); } ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); } else if (isbinary) { ierr = DMView_DA_Binary(da,viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) } else if (ismatlab) { ierr = DMView_DA_Matlab(da,viewer);CHKERRQ(ierr); #endif } PetscFunctionReturn(0); }
PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin,PetscViewer v) { DM da; PetscErrorCode ierr; PetscMPIInt rank,size,tag1,tag2; PetscInt i,n,N,step,istart,isize,j,nbounds; MPI_Status status; PetscReal coors[4],ymin,ymax,min,max,xmin = 0.0,xmax = 0.0,tmp = 0.0,xgtmp = 0.0; const PetscScalar *array,*xg; PetscDraw draw; PetscBool isnull,showpoints = PETSC_FALSE; MPI_Comm comm; PetscDrawAxis axis; Vec xcoor; DMBoundaryType bx; const PetscReal *bounds; PetscInt *displayfields; PetscInt k,ndisplayfields; PetscBool hold; PetscFunctionBegin; ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = PetscViewerDrawGetBounds(v,&nbounds,&bounds);CHKERRQ(ierr); ierr = VecGetDM(xin,&da);CHKERRQ(ierr); if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); ierr = PetscOptionsGetBool(NULL,"-draw_vec_mark_points",&showpoints,NULL);CHKERRQ(ierr); ierr = DMDAGetInfo(da,0,&N,0,0,0,0,0,&step,0,&bx,0,0,0);CHKERRQ(ierr); ierr = DMDAGetCorners(da,&istart,0,0,&isize,0,0);CHKERRQ(ierr); ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr); ierr = VecGetLocalSize(xin,&n);CHKERRQ(ierr); n = n/step; /* get coordinates of nodes */ ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); if (!xcoor) { ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);CHKERRQ(ierr); ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); } ierr = VecGetArrayRead(xcoor,&xg);CHKERRQ(ierr); ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr); ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); /* Determine the min and max x coordinate in plot */ if (!rank) { xmin = PetscRealPart(xg[0]); } if (rank == size-1) { xmax = PetscRealPart(xg[n-1]); } ierr = MPI_Bcast(&xmin,1,MPIU_REAL,0,comm);CHKERRQ(ierr); ierr = MPI_Bcast(&xmax,1,MPIU_REAL,size-1,comm);CHKERRQ(ierr); ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr); for (k=0; k<ndisplayfields; k++) { j = displayfields[k]; ierr = PetscViewerDrawGetDraw(v,k,&draw);CHKERRQ(ierr); ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); /* Determine the min and max y coordinate in plot */ min = 1.e20; max = -1.e20; for (i=0; i<n; i++) { if (PetscRealPart(array[j+i*step]) < min) min = PetscRealPart(array[j+i*step]); if (PetscRealPart(array[j+i*step]) > max) max = PetscRealPart(array[j+i*step]); } if (min + 1.e-10 > max) { min -= 1.e-5; max += 1.e-5; } if (j < nbounds) { min = PetscMin(min,bounds[2*j]); max = PetscMax(max,bounds[2*j+1]); } ierr = MPI_Reduce(&min,&ymin,1,MPIU_REAL,MPIU_MIN,0,comm);CHKERRQ(ierr); ierr = MPI_Reduce(&max,&ymax,1,MPIU_REAL,MPIU_MAX,0,comm);CHKERRQ(ierr); ierr = PetscViewerDrawGetHold(v,&hold);CHKERRQ(ierr); if (!hold) { ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); } ierr = PetscViewerDrawGetDrawAxis(v,k,&axis);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)draw,(PetscObject)axis);CHKERRQ(ierr); if (!rank) { const char *title; ierr = PetscDrawAxisSetLimits(axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); ierr = PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);CHKERRQ(ierr); ierr = DMDAGetFieldName(da,j,&title);CHKERRQ(ierr); if (title) {ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);} } ierr = MPI_Bcast(coors,4,MPIU_REAL,0,comm);CHKERRQ(ierr); if (rank) { ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr); } /* draw local part of vector */ ierr = PetscObjectGetNewTag((PetscObject)xin,&tag1);CHKERRQ(ierr); ierr = PetscObjectGetNewTag((PetscObject)xin,&tag2);CHKERRQ(ierr); if (rank < size-1) { /*send value to right */ ierr = MPI_Send((void*)&array[j+(n-1)*step],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); ierr = MPI_Send((void*)&xg[n-1],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); } if (!rank && bx == DM_BOUNDARY_PERIODIC && size > 1) { /* first processor sends first value to last */ ierr = MPI_Send((void*)&array[j],1,MPIU_REAL,size-1,tag2,comm);CHKERRQ(ierr); } for (i=1; i<n; i++) { ierr = PetscDrawLine(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+step*(i-1)]),PetscRealPart(xg[i]),PetscRealPart(array[j+step*i]),PETSC_DRAW_RED);CHKERRQ(ierr); if (showpoints) { ierr = PetscDrawPoint(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+step*(i-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); } } if (rank) { /* receive value from left */ ierr = MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); ierr = MPI_Recv(&xgtmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xgtmp,tmp,PetscRealPart(xg[0]),PetscRealPart(array[j]),PETSC_DRAW_RED);CHKERRQ(ierr); if (showpoints) { ierr = PetscDrawPoint(draw,xgtmp,tmp,PETSC_DRAW_BLACK);CHKERRQ(ierr); } } if (rank == size-1 && bx == DM_BOUNDARY_PERIODIC && size > 1) { ierr = MPI_Recv(&tmp,1,MPIU_REAL,0,tag2,comm,&status);CHKERRQ(ierr); /* If the mesh is not uniform we do not know the mesh spacing between the last point on the right and the first ghost point */ ierr = PetscDrawLine(draw,PetscRealPart(xg[n-1]),PetscRealPart(array[j+step*(n-1)]),PetscRealPart(xg[n-1]+(xg[n-1]-xg[n-2])),tmp,PETSC_DRAW_RED);CHKERRQ(ierr); if (showpoints) { ierr = PetscDrawPoint(draw,PetscRealPart(xg[n-2]),PetscRealPart(array[j+step*(n-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); } } ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); } ierr = PetscFree(displayfields);CHKERRQ(ierr); ierr = VecRestoreArrayRead(xcoor,&xg);CHKERRQ(ierr); ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode DMView_DA_2d(DM da,PetscViewer viewer) { PetscErrorCode ierr; PetscMPIInt rank; PetscBool iascii,isdraw,isbinary; DM_DA *dd = (DM_DA*)da->data; #if defined(PETSC_HAVE_MATLAB_ENGINE) PetscBool ismatlab; #endif PetscFunctionBegin; ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank); CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii); CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw); CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary); CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab); CHKERRQ(ierr); #endif if (iascii) { PetscViewerFormat format; ierr = PetscViewerGetFormat(viewer, &format); CHKERRQ(ierr); if (format != PETSC_VIEWER_ASCII_VTK && format != PETSC_VIEWER_ASCII_VTK_CELL) { DMDALocalInfo info; ierr = DMDAGetLocalInfo(da,&info); CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE); CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Processor [%d] M %D N %D m %D n %D w %D s %D\n",rank,dd->M,dd->N,dd->m,dd->n,dd->w,dd->s); CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(viewer,"X range of indices: %D %D, Y range of indices: %D %D\n",info.xs,info.xs+info.xm,info.ys,info.ys+info.ym); CHKERRQ(ierr); ierr = PetscViewerFlush(viewer); CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE); CHKERRQ(ierr); } else { ierr = DMView_DA_VTK(da,viewer); CHKERRQ(ierr); } } else if (isdraw) { PetscDraw draw; double ymin = -1*dd->s-1,ymax = dd->N+dd->s; double xmin = -1*dd->s-1,xmax = dd->M+dd->s; double x,y; PetscInt base,*idx; char node[10]; PetscBool isnull; ierr = PetscViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); if (!da->coordinates) { ierr = PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax); CHKERRQ(ierr); } ierr = PetscDrawSynchronizedClear(draw); CHKERRQ(ierr); /* first processor draw all node lines */ if (!rank) { ymin = 0.0; ymax = dd->N - 1; for (xmin=0; xmin<dd->M; xmin++) { ierr = PetscDrawLine(draw,xmin,ymin,xmin,ymax,PETSC_DRAW_BLACK); CHKERRQ(ierr); } xmin = 0.0; xmax = dd->M - 1; for (ymin=0; ymin<dd->N; ymin++) { ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_BLACK); CHKERRQ(ierr); } } ierr = PetscDrawSynchronizedFlush(draw); CHKERRQ(ierr); ierr = PetscDrawPause(draw); CHKERRQ(ierr); /* draw my box */ ymin = dd->ys; ymax = dd->ye - 1; xmin = dd->xs/dd->w; xmax =(dd->xe-1)/dd->w; ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_RED); CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmin,ymin,xmin,ymax,PETSC_DRAW_RED); CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_RED); CHKERRQ(ierr); ierr = PetscDrawLine(draw,xmax,ymin,xmax,ymax,PETSC_DRAW_RED); CHKERRQ(ierr); /* put in numbers */ base = (dd->base)/dd->w; for (y=ymin; y<=ymax; y++) { for (x=xmin; x<=xmax; x++) { sprintf(node,"%d",(int)base++); ierr = PetscDrawString(draw,x,y,PETSC_DRAW_BLACK,node); CHKERRQ(ierr); } } ierr = PetscDrawSynchronizedFlush(draw); CHKERRQ(ierr); ierr = PetscDrawPause(draw); CHKERRQ(ierr); /* overlay ghost numbers, useful for error checking */ /* put in numbers */ base = 0; idx = dd->idx; ymin = dd->Ys; ymax = dd->Ye; xmin = dd->Xs; xmax = dd->Xe; for (y=ymin; y<ymax; y++) { for (x=xmin; x<xmax; x++) { if ((base % dd->w) == 0) { sprintf(node,"%d",(int)(idx[base]/dd->w)); ierr = PetscDrawString(draw,x/dd->w,y,PETSC_DRAW_BLUE,node); CHKERRQ(ierr); } base++; } } ierr = PetscDrawSynchronizedFlush(draw); CHKERRQ(ierr); ierr = PetscDrawPause(draw); CHKERRQ(ierr); } else if (isbinary) { ierr = DMView_DA_Binary(da,viewer); CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) } else if (ismatlab) { ierr = DMView_DA_Matlab(da,viewer); CHKERRQ(ierr); #endif } PetscFunctionReturn(0); }
/*@ PetscDrawBarDraw - Redraws a bar graph. Collective on PetscDrawBar Input Parameter: . bar - The bar graph context Level: intermediate .seealso: PetscDrawBar, PetscDrawBarCreate(), PetscDrawBarSetData() @*/ PetscErrorCode PetscDrawBarDraw(PetscDrawBar bar) { PetscDraw draw; PetscBool isnull; PetscReal xmin,xmax,ymin,ymax,*values,binLeft,binRight; PetscInt numValues,i,bcolor,color,idx,*perm,nplot; PetscMPIInt rank; PetscErrorCode ierr; char **labels; PetscFunctionBegin; PetscValidHeaderSpecific(bar,PETSC_DRAWBAR_CLASSID,1); ierr = PetscDrawIsNull(bar->win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)bar),&rank);CHKERRQ(ierr); if (bar->numBins < 1) PetscFunctionReturn(0); color = bar->color; if (color == PETSC_DRAW_ROTATE) bcolor = PETSC_DRAW_BLACK+1; else bcolor = color; numValues = bar->numBins; values = bar->values; if (bar->ymin == bar->ymax) { /* user has not set bounds on bars so set them based on the data */ ymin = PETSC_MAX_REAL; ymax = PETSC_MIN_REAL; for (i=0; i<numValues; i++) { ymin = PetscMin(ymin,values[i]); ymax = PetscMax(ymax,values[i]); } } else { ymin = bar->ymin; ymax = bar->ymax; } nplot = numValues; /* number of points to actually plot; if some are lower than requested tolerance */ xmin = 0.0; xmax = nplot; labels = bar->labels; if (bar->sort) { ierr = PetscMalloc1(numValues,&perm);CHKERRQ(ierr); for (i=0; i<numValues;i++) perm[i] = i; ierr = PetscSortRealWithPermutation(numValues,values,perm);CHKERRQ(ierr); if (bar->sorttolerance) { for (i=0; i<numValues;i++) { if (values[perm[numValues - i - 1]] < bar->sorttolerance) { nplot = i; break; } } } } draw = bar->win; ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); ierr = PetscDrawAxisSetLimits(bar->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(bar->axis);CHKERRQ(ierr); ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); if (!rank) { /* Draw bins */ for (i=0; i<nplot; i++) { idx = (bar->sort ? perm[numValues - i - 1] : i); binLeft = xmin + i; binRight = xmin + i + 1; ierr = PetscDrawRectangle(draw,binLeft,ymin,binRight,values[idx],bcolor,bcolor,bcolor,bcolor);CHKERRQ(ierr); ierr = PetscDrawLine(draw,binLeft,ymin,binLeft,values[idx],PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,binRight,ymin,binRight,values[idx],PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,binLeft,values[idx],binRight,values[idx],PETSC_DRAW_BLACK);CHKERRQ(ierr); if (labels) { PetscReal h; ierr = PetscDrawStringGetSize(draw,NULL,&h);CHKERRQ(ierr); ierr = PetscDrawStringCentered(draw,.5*(binLeft+binRight),ymin - 1.5*h,bcolor,labels[idx]);CHKERRQ(ierr); } if (color == PETSC_DRAW_ROTATE) bcolor++; if (bcolor > PETSC_DRAW_BASIC_COLORS-1) bcolor = PETSC_DRAW_BLACK+1; } } ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); if (bar->sort) {ierr = PetscFree(perm);CHKERRQ(ierr);} ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscDrawLG lg; PetscErrorCode ierr; PetscInt Mx = 100,i; PetscReal x,hx = .1/Mx,pause,xx[3],yy[3]; PetscDraw draw; const char *const legend[] = {"(1 - u^2)^2","1 - u^2","-(1 - u)log(1 - u)"}; PetscDrawAxis axis; static PetscDrawViewPorts *ports = 0; PetscFunctionBegin; PetscInitialize(&argc,&argv,0,help); ierr = PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),1200,800);CHKERRQ(ierr); ierr = PetscViewerDrawGetDrawLG(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),0,&lg);CHKERRQ(ierr); ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); if (!ports) { ierr = PetscDrawViewPortsCreateRect(draw,1,2,&ports);CHKERRQ(ierr); } ierr = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr); ierr = PetscDrawLGReset(lg);CHKERRQ(ierr); /* Plot the energies */ ierr = PetscDrawLGSetDimension(lg,3);CHKERRQ(ierr); ierr = PetscDrawViewPortsSet(ports,1);CHKERRQ(ierr); x = .9; for (i=0; i<Mx; i++) { xx[0] = xx[1] = xx[2] = x; yy[0] = (1.-x*x)*(1. - x*x); yy[1] = (1. - x*x); yy[2] = -(1.-x)*PetscLogScalar(1.-x); 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,0);CHKERRQ(ierr); ierr = PetscDrawLGReset(lg);CHKERRQ(ierr); x = .9; for (i=0; i<Mx; i++) { xx[0] = xx[1] = xx[2] = x; yy[0] = x*x*x - x; yy[1] = -x; yy[2] = 1.0 + PetscLogScalar(1. - x); ierr = PetscDrawLGAddPoint(lg,xx,yy);CHKERRQ(ierr); x += hx; } ierr = PetscDrawAxisSetLabels(axis,"Derivative","","");CHKERRQ(ierr); ierr = PetscDrawLGSetLegend(lg,PETSC_NULL);CHKERRQ(ierr); ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); ierr = PetscDrawSetPause(draw,pause);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PetscDrawLGDraw - Redraws a line graph. Not Collective,but ignored by all processors except processor 0 in PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw() @*/ PetscErrorCode PetscDrawLGDraw(PetscDrawLG lg) { PetscReal xmin=lg->xmin,xmax=lg->xmax,ymin=lg->ymin,ymax=lg->ymax; PetscErrorCode ierr; int i,j,dim = lg->dim,nopts = lg->nopts,rank,cl; PetscDraw draw = lg->win; PetscBool isnull; PetscFunctionBegin; if (lg && ((PetscObject)lg)->classid == PETSC_DRAW_CLASSID) PetscFunctionReturn(0); PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); ierr = PetscDrawIsNull(lg->win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_HAVE_X) if (!setjmp(PetscXIOErrorJumpBuf)) XSetIOErrorHandler((XIOErrorHandler)PetscXIOHandler); else { XSetIOErrorHandler(PETSC_NULL); ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr); PetscFunctionReturn(0); } #endif ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = MPI_Comm_rank(((PetscObject)lg)->comm,&rank);CHKERRQ(ierr); if (!rank) { for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { if (lg->colors) cl = lg->colors[i]; else cl = PETSC_DRAW_BLACK+i; ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],cl);CHKERRQ(ierr); if (lg->use_dots) { ierr = PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],cl,"x");CHKERRQ(ierr); } } } } if (lg->legend) { PetscReal xl,yl,xr,yr,tw,th; size_t len,mlen = 0; int cl; ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); for (i=0; i<dim; i++) { ierr = PetscStrlen(lg->legend[i],&len);CHKERRQ(ierr); mlen = PetscMax(mlen,len); } ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - 2*tw,yr - 3*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); for (i=0; i<dim; i++) { cl = (lg->colors ? lg->colors[i] : i + 1); ierr = PetscDrawLine(draw,xr - (mlen + 6.7)*tw,yr - (4 + i)*th,xr - (mlen + 3.2)*tw,yr - (4 + i)*th,cl);CHKERRQ(ierr); ierr = PetscDrawString(draw,xr - (mlen + 3)*tw,yr - (4.5 + i)*th,PETSC_DRAW_BLACK,lg->legend[i]);CHKERRQ(ierr); } ierr = PetscDrawLine(draw,xr - 2*tw,yr - 3*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); } ierr = PetscDrawFlush(lg->win);CHKERRQ(ierr); ierr = PetscDrawPause(lg->win);CHKERRQ(ierr); #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_HAVE_X) XSetIOErrorHandler(PETSC_NULL); #endif PetscFunctionReturn(0); }
/* 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); }
/*@ PetscDrawLGDraw - Redraws a line graph. Collective on PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw(), PetscDrawLGReset() @*/ PetscErrorCode PetscDrawLGDraw(PetscDrawLG lg) { PetscReal xmin,xmax,ymin,ymax; PetscErrorCode ierr; PetscMPIInt rank; PetscDraw draw; PetscBool isnull; PetscFunctionBegin; PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); ierr = PetscDrawIsNull(lg->win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)lg),&rank);CHKERRQ(ierr); draw = lg->win; ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); ierr = PetscDrawClear(draw);CHKERRQ(ierr); xmin = lg->xmin; xmax = lg->xmax; ymin = lg->ymin; ymax = lg->ymax; ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); if (!rank) { int i,j,dim=lg->dim,nopts=lg->nopts,cl; for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { cl = lg->colors ? lg->colors[i] : (PETSC_DRAW_BLACK + i); ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],cl);CHKERRQ(ierr); if (lg->use_markers) {ierr = PetscDrawMarker(draw,lg->x[j*dim+i],lg->y[j*dim+i],cl);CHKERRQ(ierr);} } } } if (!rank && lg->legend) { int i,dim=lg->dim,cl; PetscReal xl,yl,xr,yr,tw,th; size_t slen,len=0; ierr = PetscDrawAxisGetLimits(lg->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); for (i=0; i<dim; i++) { ierr = PetscStrlen(lg->legend[i],&slen);CHKERRQ(ierr); len = PetscMax(len,slen); } xr = xr - 1.5*tw; xl = xr - (len + 7)*tw; yr = yr - 1.0*th; yl = yr - (dim + 1)*th; ierr = PetscDrawLine(draw,xl,yl,xr,yl,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr,yl,xr,yr,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr,yr,xl,yr,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xl,yr,xl,yl,PETSC_DRAW_BLACK);CHKERRQ(ierr); for (i=0; i<dim; i++) { cl = lg->colors ? lg->colors[i] : (PETSC_DRAW_BLACK + i); ierr = PetscDrawLine(draw,xl + 1*tw,yr - (i + 1)*th,xl + 5*tw,yr - (i + 1)*th,cl);CHKERRQ(ierr); ierr = PetscDrawString(draw,xl + 6*tw,yr - (i + 1.5)*th,PETSC_DRAW_BLACK,lg->legend[i]);CHKERRQ(ierr); } } ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); ierr = PetscDrawFlush(draw);CHKERRQ(ierr); ierr = PetscDrawPause(draw);CHKERRQ(ierr); PetscFunctionReturn(0); }
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; }