/*@C
PetscDrawAppendTitle - Appends to the title of a PetscDraw context.
Not collective (any processor or all can call this)
Input Parameters:
+ draw - the graphics context
- title - the title
Note:
A copy of the string is made, so you may destroy the
title string after calling this routine.
Level: advanced
.seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
@*/
PetscErrorCode PetscDrawAppendTitle(PetscDraw draw,const char title[])
{
PetscErrorCode ierr;
size_t len1,len2,len;
char *newtitle;
PetscFunctionBegin;
PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
if (!title) PetscFunctionReturn(0);
if (draw->title) {
ierr = PetscStrlen(title,&len1);CHKERRQ(ierr);
ierr = PetscStrlen(draw->title,&len2);CHKERRQ(ierr);
len = len1 + len2;
ierr = PetscMalloc1((len + 1),&newtitle);CHKERRQ(ierr);
ierr = PetscStrcpy(newtitle,draw->title);CHKERRQ(ierr);
ierr = PetscStrcat(newtitle,title);CHKERRQ(ierr);
ierr = PetscFree(draw->title);CHKERRQ(ierr);
draw->title = newtitle;
} else {
ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
}
if (draw->ops->settitle) {
ierr = (*draw->ops->settitle)(draw,draw->title);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
/*@C
PetscGetHostName - Returns the name of the host. This attempts to
return the entire Internet name. It may not return the same name
as MPI_Get_processor_name().
Not Collective
Input Parameter:
. nlen - length of name
Output Parameter:
. name - contains host name. Must be long enough to hold the name
This is the fully qualified name, including the domain.
Level: developer
Concepts: machine name
Concepts: host name
Fortran Version:
In Fortran this routine has the format
$ character*(64) name
$ call PetscGetHostName(name,ierr)
.seealso: PetscGetUserName(),PetscGetArchType()
@*/
PetscErrorCode PetscGetHostName(char name[],size_t nlen)
{
char *domain;
PetscErrorCode ierr;
#if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
struct utsname utname;
#endif
PetscFunctionBegin;
#if defined(PETSC_HAVE_GETCOMPUTERNAME)
{
size_t nnlen = nlen;
GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
}
#elif defined(PETSC_HAVE_UNAME)
uname(&utname);
ierr = PetscStrncpy(name,utname.nodename,nlen);
CHKERRQ(ierr);
#elif defined(PETSC_HAVE_GETHOSTNAME)
gethostname(name,nlen);
#elif defined(PETSC_HAVE_SYSINFO_3ARG)
sysinfo(SI_HOSTNAME,name,nlen);
#endif
/* if there was not enough room then system call will not null terminate name */
name[nlen-1] = 0;
/* See if this name includes the domain */
ierr = PetscStrchr(name,'.',&domain);
CHKERRQ(ierr);
if (!domain) {
size_t l,ll;
ierr = PetscStrlen(name,&l);
CHKERRQ(ierr);
if (l == nlen-1) PetscFunctionReturn(0);
name[l++] = '.';
name[l] = 0;
#if defined(PETSC_HAVE_SYSINFO_3ARG)
sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l);
#elif defined(PETSC_HAVE_GETDOMAINNAME)
if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()");
#endif
/* check if domain name is not a dnsdomainname and nuke it */
ierr = PetscStrlen(name,&ll);
CHKERRQ(ierr);
if (ll > 4) {
const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
PetscInt index;
ierr = PetscStrendswithwhich(name,suffixes,&index);
CHKERRQ(ierr);
if (!suffixes[index]) {
ierr = PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);
CHKERRQ(ierr);
name[l-1] = 0;
}
}
}
PetscFunctionReturn(0);
}
/*@C
PetscDrawSetSave - Saves images produced in a PetscDraw into a file
Collective on PetscDraw
Input Parameter:
+ draw - the graphics context
. filename - name of the file, if .ext then uses name of draw object plus .ext using .ext to determine the image type
- movieext - if not NULL, produces a movie of all the images
Options Database Command:
+ -draw_save <filename> - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .png)
. -draw_save_movie <.ext> - saves a movie to filename.ext
. -draw_save_final_image [optional filename] - saves the final image displayed in a window
- -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename/filename_%d.ext
Level: intermediate
Concepts: X windows^graphics
Notes: You should call this BEFORE creating your image and calling PetscDrawSave().
The supported image types are .png, .gif, .jpg, and .ppm (PETSc chooses the default in that order).
Support for .png images requires configure --with-libpng.
Support for .gif images requires configure --with-giflib.
Support for .jpg images requires configure --with-libjpeg.
Support for .ppm images is built-in. The PPM format has no compression (640x480 pixels ~ 900 KiB).
The ffmpeg utility must be in your path to make the movie.
.seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage()
@*/
PetscErrorCode PetscDrawSetSave(PetscDraw draw,const char filename[],const char movieext[])
{
const char *savename = NULL;
const char *imageext = NULL;
char buf[PETSC_MAX_PATH_LEN];
PetscErrorCode ierr;
PetscFunctionBegin;
PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
if (filename) PetscValidCharPointer(filename,2);
if (movieext) PetscValidCharPointer(movieext,2);
/* determine save filename and image extension */
if (filename && filename[0]) {
ierr = PetscStrchr(filename,'.',(char **)&imageext);CHKERRQ(ierr);
if (!imageext) savename = filename;
else if (imageext != filename) {
size_t l1 = 0,l2 = 0;
ierr = PetscStrlen(filename,&l1);CHKERRQ(ierr);
ierr = PetscStrlen(imageext,&l2);CHKERRQ(ierr);
ierr = PetscStrncpy(buf,filename,l1-l2+1);CHKERRQ(ierr);
savename = buf;
}
}
if (!savename) {ierr = PetscObjectGetName((PetscObject)draw,&savename);CHKERRQ(ierr);}
ierr = PetscDrawImageCheckFormat(&imageext);CHKERRQ(ierr);
if (movieext) {ierr = PetscDrawMovieCheckFormat(&movieext);CHKERRQ(ierr);}
if (movieext) draw->savesinglefile = PETSC_FALSE; /* otherwise we cannot generage movies */
if (draw->savesinglefile) {
ierr = PetscInfo2(NULL,"Will save image to file %s%s\n",savename,imageext);CHKERRQ(ierr);
} else {
ierr = PetscInfo3(NULL,"Will save images to file %s/%s_%%d%s\n",savename,savename,imageext);CHKERRQ(ierr);
}
if (movieext) {
ierr = PetscInfo2(NULL,"Will save movie to file %s%s\n",savename,movieext);CHKERRQ(ierr);
}
draw->savefilecount = 0;
ierr = PetscFree(draw->savefilename);CHKERRQ(ierr);
ierr = PetscFree(draw->saveimageext);CHKERRQ(ierr);
ierr = PetscFree(draw->savemovieext);CHKERRQ(ierr);
ierr = PetscStrallocpy(savename,&draw->savefilename);CHKERRQ(ierr);
ierr = PetscStrallocpy(imageext,&draw->saveimageext);CHKERRQ(ierr);
ierr = PetscStrallocpy(movieext,&draw->savemovieext);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
开发者ID:plguhur,项目名称:petsc,代码行数:78,代码来源:dsave.c
示例6: PetscStripTrailingZeros
/*
Removes trailing zeros
*/
PetscErrorCode PetscStripTrailingZeros(char *buf)
{
PetscErrorCode ierr;
char *found;
size_t i,n,m = PETSC_MAX_INT;
PetscFunctionBegin;
/* if there is an e in string DO NOT strip trailing zeros */
ierr = PetscStrchr(buf,'e',&found);CHKERRQ(ierr);
if (found) PetscFunctionReturn(0);
ierr = PetscStrlen(buf,&n);CHKERRQ(ierr);
/* locate decimal point */
for (i=0; i<n; i++) {
if (buf[i] == '.') {m = i; break;}
}
/* if not decimal point then no zeros to remove */
if (m == PETSC_MAX_INT) PetscFunctionReturn(0);
/* start at right end of string removing 0s */
for (i=n-1; i>m; i++) {
if (buf[i] != '0') PetscFunctionReturn(0);
buf[i] = 0;
}
PetscFunctionReturn(0);
}
/*@C
PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can
can be replaced with something that does not simply write to a file.
To use, write your own function for example,
$PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp)
${
$ PetscErrorCode ierr;
$
$ PetscFunctionBegin;
$ if (fd != stdout && fd != stderr) { handle regular files
$ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr);
$ } else {
$ char buff[BIG];
$ size_t length;
$ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr);
$ now send buff to whatever stream or whatever you want
$ }
$ PetscFunctionReturn(0);
$}
then before the call to PetscInitialize() do the assignment
$ PetscVFPrintf = mypetscvfprintf;
Notes: For error messages this may be called by any process, for regular standard out it is
called only by process 0 of a given communicator
Developer Notes: this could be called by an error handler, if that happens then a recursion of the error handler may occur
and a crash
Level: developer
.seealso: PetscVSNPrintf(), PetscErrorPrintf()
@*/
PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp)
{
char *newformat;
char formatbuf[8*1024];
size_t oldLength;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr);
if (oldLength < 8*1024) {
newformat = formatbuf;
oldLength = 8*1024-1;
} else {
oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength);
ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr);
}
ierr = PetscFormatConvert(format,newformat,oldLength);CHKERRQ(ierr);
#if defined(PETSC_HAVE_VFPRINTF_CHAR)
vfprintf(fd,newformat,(char*)Argp);
#else
vfprintf(fd,newformat,Argp);
#endif
fflush(fd);
if (oldLength >= 8*1024) {
ierr = PetscFree(newformat);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
{
PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
PetscErrorCode ierr;
PetscBool isvtk,isvts,isvtu,isvtr;
size_t len;
PetscFunctionBegin;
ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
ierr = PetscFree(vtk->filename);CHKERRQ(ierr);
ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
ierr = PetscStrcasecmp(name+len-4,".vtk",&isvtk);CHKERRQ(ierr);
ierr = PetscStrcasecmp(name+len-4,".vts",&isvts);CHKERRQ(ierr);
ierr = PetscStrcasecmp(name+len-4,".vtu",&isvtu);CHKERRQ(ierr);
ierr = PetscStrcasecmp(name+len-4,".vtr",&isvtr);CHKERRQ(ierr);
if (isvtk) {
if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);}
if (viewer->format != PETSC_VIEWER_ASCII_VTK) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtk' extension",name,PetscViewerFormats[viewer->format]);
} else if (isvts) {
if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTS);CHKERRQ(ierr);}
if (viewer->format != PETSC_VIEWER_VTK_VTS) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vts' extension",name,PetscViewerFormats[viewer->format]);
} else if (isvtu) {
if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTU);CHKERRQ(ierr);}
if (viewer->format != PETSC_VIEWER_VTK_VTU) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtu' extension",name,PetscViewerFormats[viewer->format]);
} else if (isvtr) {
if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTR);CHKERRQ(ierr);}
if (viewer->format != PETSC_VIEWER_VTK_VTR) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtr' extension",name,PetscViewerFormats[viewer->format]);
} else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
ierr = PetscStrallocpy(name,&vtk->filename);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/*@C
PetscPOpen - Runs a program on processor zero and sends either its input or output to
a file.
Logically Collective on MPI_Comm, but only process 0 runs the command
Input Parameters:
+ comm - MPI communicator, only processor zero runs the program
. machine - machine to run command on or NULL, or string with 0 in first location
. program - name of program to run
- mode - either r or w
Output Parameter:
. fp - the file pointer where program input or output may be read or NULL if don't care
Level: intermediate
Notes:
Use PetscPClose() to close the file pointer when you are finished with it
Does not work under Windows
If machine is not provided will use the value set with PetsPOpenSetMachine() if that was provided, otherwise
will use the machine running node zero of the communicator
The program string may contain ${DISPLAY}, ${HOMEDIRECTORY} or ${WORKINGDIRECTORY}; these
will be replaced with relevent values.
.seealso: PetscFOpen(), PetscFClose(), PetscPClose(), PetscPOpenSetMachine()
@*/
PetscErrorCode PetscPOpen(MPI_Comm comm,const char machine[],const char program[],const char mode[],FILE **fp)
{
PetscErrorCode ierr;
PetscMPIInt rank;
size_t i,len,cnt;
char commandt[PETSC_MAX_PATH_LEN],command[PETSC_MAX_PATH_LEN];
FILE *fd;
PetscFunctionBegin;
/* all processors have to do the string manipulation because PetscStrreplace() is a collective operation */
if (PetscPOpenMachine[0] || (machine && machine[0])) {
ierr = PetscStrcpy(command,"ssh ");CHKERRQ(ierr);
if (PetscPOpenMachine[0]) {
ierr = PetscStrcat(command,PetscPOpenMachine);CHKERRQ(ierr);
} else {
ierr = PetscStrcat(command,machine);CHKERRQ(ierr);
}
ierr = PetscStrcat(command," \" export DISPLAY=${DISPLAY}; ");CHKERRQ(ierr);
/*
Copy program into command but protect the " with a \ in front of it
*/
ierr = PetscStrlen(command,&cnt);CHKERRQ(ierr);
ierr = PetscStrlen(program,&len);CHKERRQ(ierr);
for (i=0; i<len; i++) {
if (program[i] == '\"') command[cnt++] = '\\';
command[cnt++] = program[i];
}
command[cnt] = 0;
ierr = PetscStrcat(command,"\"");CHKERRQ(ierr);
} else {
ierr = PetscStrcpy(command,program);CHKERRQ(ierr);
}
ierr = PetscStrreplace(comm,command,commandt,1024);CHKERRQ(ierr);
ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
if (!rank) {
ierr = PetscInfo1(0,"Running command :%s\n",commandt);CHKERRQ(ierr);
if (!(fd = popen(commandt,mode))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot run command %s",commandt);
if (fp) *fp = fd;
}
PetscFunctionReturn(0);
}
请发表评论