static PetscErrorCode TSTrajectorySet_Visualization(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X) { PetscViewer viewer; char filename[PETSC_MAX_PATH_LEN]; PetscReal tprev; PetscErrorCode ierr; PetscFunctionBegin; ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr); if (stepnum == 0) { PetscMPIInt rank; ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscRMTree("Visualization-data");CHKERRQ(ierr); ierr = PetscMkdir("Visualization-data");CHKERRQ(ierr); } ierr = PetscSNPrintf(filename,sizeof(filename),"Visualization-data/SA-%06d.bin",stepnum);CHKERRQ(ierr); ierr = OutputBIN(filename,&viewer);CHKERRQ(ierr); ierr = VecView(X,viewer);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); PetscFunctionReturn(0); } ierr = PetscSNPrintf(filename,sizeof(filename),"Visualization-data/SA-%06d.bin",stepnum);CHKERRQ(ierr); ierr = OutputBIN(filename,&viewer);CHKERRQ(ierr); ierr = VecView(X,viewer);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr); ierr = TSGetPrevTime(ts,&tprev);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(viewer,&tprev,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PetscDrawSave - Saves a drawn image Collective on PetscDraw Input Parameters: . draw - the drawing context Level: advanced Notes: this is not normally called by the user, it is called by PetscDrawFlush() to save a sequence of images. .seealso: PetscDrawSetSave() @*/ PetscErrorCode PetscDrawSave(PetscDraw draw) { PetscInt savecount; char basename[PETSC_MAX_PATH_LEN]; unsigned char palette[256][3]; unsigned int w,h; unsigned char *pixels = NULL; PetscMPIInt rank; PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); if (draw->ops->save) {ierr = (*draw->ops->save)(draw);CHKERRQ(ierr); goto finally;} if (!draw->savefilename) PetscFunctionReturn(0); if (!draw->ops->getimage) PetscFunctionReturn(0); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr); savecount = draw->savefilecount++; if (!rank && !savecount) { char path[PETSC_MAX_PATH_LEN]; if (draw->savesinglefile) { ierr = PetscSNPrintf(path,sizeof(path),"%s%s",draw->savefilename,draw->saveimageext);CHKERRQ(ierr); (void)remove(path); } else { ierr = PetscSNPrintf(path,sizeof(path),"%s",draw->savefilename);CHKERRQ(ierr); ierr = PetscRMTree(path);CHKERRQ(ierr); ierr = PetscMkdir(path);CHKERRQ(ierr); } if (draw->savemovieext) { ierr = PetscSNPrintf(path,sizeof(path),"%s%s",draw->savefilename,draw->savemovieext);CHKERRQ(ierr); (void)remove(path); } } if (draw->savesinglefile) { ierr = PetscSNPrintf(basename,sizeof(basename),"%s",draw->savefilename);CHKERRQ(ierr); } else { ierr = PetscSNPrintf(basename,sizeof(basename),"%s/%s_%d",draw->savefilename,draw->savefilename,(int)savecount);CHKERRQ(ierr); } /* this call is collective, only the first process gets the image data */ ierr = (*draw->ops->getimage)(draw,palette,&w,&h,&pixels);CHKERRQ(ierr); /* only the first process handles the saving business */ if (!rank) {ierr = PetscDrawImageSave(basename,draw->saveimageext,palette,w,h,pixels);CHKERRQ(ierr);} ierr = PetscFree(pixels);CHKERRQ(ierr); ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr); finally: #if defined(PETSC_HAVE_SAWS) ierr = PetscDrawSave_SAWs(draw);CHKERRQ(ierr); #endif PetscFunctionReturn(0); }