PetscErrorCode PetscRMTree(const char dir[]) { PetscErrorCode ierr; struct dirent *data; char loc[PETSC_MAX_PATH_LEN]; PetscBool flg1, flg2; DIR *dirp; struct stat statbuf; PetscFunctionBegin; dirp = opendir(dir); if(!dirp) { PetscBool flg; ierr = PetscTestDirectory(dir,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); ierr = PetscTestFile(dir,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); PetscFunctionReturn(0); /* perhaps the dir was not yet created */ } while((data = readdir(dirp))) { ierr = PetscStrcmp(data->d_name, ".",&flg1);CHKERRQ(ierr); ierr = PetscStrcmp(data->d_name, "..",&flg2);CHKERRQ(ierr); if (flg1 || flg2) continue; ierr = PetscPathJoin(dir,data->d_name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); if (lstat(loc,&statbuf) <0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"cannot run lstat() on: %s",loc); if (S_ISDIR(statbuf.st_mode)) { ierr = PetscRMTree(loc);CHKERRQ(ierr); } else { if (unlink(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc); } } closedir(dirp); if (rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir); PetscFunctionReturn(0); }
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); }
PetscErrorCode PetscRMTree(const char dir[]) { PetscErrorCode ierr; struct _finddata_t data; char loc[PETSC_MAX_PATH_LEN]; PetscBool flg1, flg2; #if defined (PETSC_HAVE_STDINT_H) intptr_t handle; #else long handle; #endif PetscFunctionBegin; ierr = PetscPathJoin(dir,"*",PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); handle = _findfirst(loc, &data); if(handle == -1) { PetscBool flg; ierr = PetscTestDirectory(loc,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); ierr = PetscTestFile(loc,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); PetscFunctionReturn(0); /* perhaps the dir was not yet created */ } while(_findnext(handle, &data) != -1) { ierr = PetscStrcmp(data.name, ".",&flg1);CHKERRQ(ierr); ierr = PetscStrcmp(data.name, "..",&flg2);CHKERRQ(ierr); if (flg1 || flg2) continue; ierr = PetscPathJoin(dir,data.name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); if(data.attrib & _A_SUBDIR) { ierr = PetscRMTree(loc);CHKERRQ(ierr); } else{ if (remove(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc); } } _findclose(handle); if (_rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir); PetscFunctionReturn(0); }